Skip to content

Commit

Permalink
feat(torii/graphql): use union types for token balances and transfers
Browse files Browse the repository at this point in the history
commit-id:73b9b2b4
  • Loading branch information
lambda-0x committed Nov 13, 2024
1 parent eb95d49 commit 39233db
Show file tree
Hide file tree
Showing 9 changed files with 368 additions and 215 deletions.
28 changes: 0 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions crates/torii/graphql/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ pub const MODEL_ORDER_FIELD_TYPE_NAME: &str = "World__ModelOrderField";
pub const TOKEN_BALANCE_TYPE_NAME: &str = "Token__Balance";
pub const TOKEN_TRANSFER_TYPE_NAME: &str = "Token__Transfer";
pub const TOKEN_TYPE_NAME: &str = "ERC__Token";
pub const ERC721_METADATA_TYPE_NAME: &str = "ERC721__Metadata";
// pub const ERC721_METADATA_TYPE_NAME: &str = "ERC721__Metadata";

pub const ERC20_TYPE_NAME: &str = "ERC20__Token";
pub const ERC721_TYPE_NAME: &str = "ERC721__Token";

// objects' single and plural names
pub const ENTITY_NAMES: (&str, &str) = ("entity", "entities");
Expand All @@ -48,11 +51,14 @@ pub const CONTENT_NAMES: (&str, &str) = ("content", "contents");
pub const METADATA_NAMES: (&str, &str) = ("metadata", "metadatas");
pub const TRANSACTION_NAMES: (&str, &str) = ("transaction", "transactions");
pub const PAGE_INFO_NAMES: (&str, &str) = ("pageInfo", "");
pub const TOKEN_NAME: (&str, &str) = ("token", "tokens");

pub const ERC20_TOKEN_NAME: (&str, &str) = ("erc20Token", "");
pub const ERC721_TOKEN_NAME: (&str, &str) = ("erc721Token", "");

pub const TOKEN_BALANCE_NAME: (&str, &str) = ("", "tokenBalances");
pub const TOKEN_TRANSFER_NAME: (&str, &str) = ("", "tokenTransfers");

pub const ERC721_METADATA_NAME: (&str, &str) = ("erc721Metadata", "");
// pub const ERC721_METADATA_NAME: (&str, &str) = ("erc721Metadata", "");

// misc
pub const ORDER_DIR_TYPE_NAME: &str = "OrderDirection";
Expand Down
35 changes: 15 additions & 20 deletions crates/torii/graphql/src/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use async_graphql::Name;
use dojo_types::primitive::Primitive;
use lazy_static::lazy_static;

use crate::constants::{
CONTENT_TYPE_NAME, ERC721_METADATA_TYPE_NAME, SOCIAL_TYPE_NAME, TOKEN_TYPE_NAME,
};
use crate::constants::{CONTENT_TYPE_NAME, SOCIAL_TYPE_NAME, TOKEN_TYPE_NAME};
use crate::types::{GraphqlType, TypeData, TypeMapping};

lazy_static! {
Expand Down Expand Up @@ -148,38 +146,35 @@ lazy_static! {
]);

pub static ref TOKEN_BALANCE_TYPE_MAPPING: TypeMapping = IndexMap::from([
(Name::new("balance"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("type"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("tokenMetadata"), TypeData::Simple(TypeRef::named_nn(TOKEN_TYPE_NAME))),
(Name::new("tokenMetadata"), TypeData::Nested((TypeRef::named_nn(TOKEN_TYPE_NAME), IndexMap::new()))),
]);

pub static ref TOKEN_TRANSFER_TYPE_MAPPING: TypeMapping = IndexMap::from([
(Name::new("from"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("to"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("amount"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("type"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("executedAt"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("tokenMetadata"), TypeData::Simple(TypeRef::named_nn(TOKEN_TYPE_NAME))),
(Name::new("tokenMetadata"), TypeData::Nested((TypeRef::named_nn(TOKEN_TYPE_NAME), IndexMap::new()))),
(Name::new("transactionHash"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
]);

pub static ref TOKEN_TYPE_MAPPING: TypeMapping = IndexMap::from([
pub static ref ERC20_TOKEN_TYPE_MAPPING: TypeMapping = IndexMap::from([
(Name::new("name"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("symbol"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("decimals"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("contractAddress"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("decimals"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
(
Name::new("erc721"),
TypeData::Nested((TypeRef::named(ERC721_METADATA_TYPE_NAME), IndexMap::new()))
),
(Name::new("amount"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
]);

pub static ref ERC721_METADATA_TYPE_MAPPING: TypeMapping = IndexMap::from([
(Name::new("tokenId"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
pub static ref ERC721_TOKEN_TYPE_MAPPING: TypeMapping = IndexMap::from([
(Name::new("name"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("description"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("attributes"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("imagePath"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("symbol"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("tokenId"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("contractAddress"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("metadata"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("metadataName"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("metadataDescription"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("metadataAttributes"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("imagePath"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
]);

}
99 changes: 84 additions & 15 deletions crates/torii/graphql/src/object/erc/erc_token.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,109 @@
use crate::constants::{
ERC721_METADATA_NAME, ERC721_METADATA_TYPE_NAME, TOKEN_NAME, TOKEN_TYPE_NAME,
};
use crate::mapping::{ERC721_METADATA_TYPE_MAPPING, TOKEN_TYPE_MAPPING};
use async_graphql::dynamic::FieldValue;
use async_graphql::{Name, Value};

use crate::constants::{ERC20_TOKEN_NAME, ERC20_TYPE_NAME, ERC721_TOKEN_NAME, ERC721_TYPE_NAME};
use crate::mapping::{ERC20_TOKEN_TYPE_MAPPING, ERC721_TOKEN_TYPE_MAPPING};
use crate::object::BasicObject;
use crate::types::TypeMapping;
use crate::types::{TypeMapping, ValueMapping};

#[derive(Debug)]
pub struct ErcTokenObject;
pub struct Erc20TokenObject;

impl BasicObject for ErcTokenObject {
impl BasicObject for Erc20TokenObject {
fn name(&self) -> (&str, &str) {
TOKEN_NAME
ERC20_TOKEN_NAME
}

fn type_name(&self) -> &str {
TOKEN_TYPE_NAME
ERC20_TYPE_NAME
}

fn type_mapping(&self) -> &TypeMapping {
&TOKEN_TYPE_MAPPING
&ERC20_TOKEN_TYPE_MAPPING
}
}

#[derive(Debug)]
pub struct Erc721MetadataObject;
pub struct Erc721TokenObject;

impl BasicObject for Erc721MetadataObject {
impl BasicObject for Erc721TokenObject {
fn name(&self) -> (&str, &str) {
ERC721_METADATA_NAME
ERC721_TOKEN_NAME
}

fn type_name(&self) -> &str {
ERC721_METADATA_TYPE_NAME
ERC721_TYPE_NAME
}

fn type_mapping(&self) -> &TypeMapping {
&ERC721_METADATA_TYPE_MAPPING
&ERC721_TOKEN_TYPE_MAPPING
}
}

#[derive(Debug, Clone)]
pub enum ErcTokenType {
Erc20(Erc20Token),
Erc721(Erc721Token),
}

#[derive(Debug, Clone)]
pub struct Erc20Token {
pub name: String,
pub symbol: String,
pub decimals: u8,
pub contract_address: String,
pub amount: String,
}

#[derive(Debug, Clone)]
pub struct Erc721Token {
pub name: String,
pub symbol: String,
pub token_id: String,
pub contract_address: String,
pub metadata: String,
pub metadata_name: Option<String>,
pub metadata_description: Option<String>,
pub metadata_attributes: Option<String>,
pub image_path: String,
}

impl ErcTokenType {
pub fn to_field_value<'a>(self) -> FieldValue<'a> {
match self {
ErcTokenType::Erc20(token) => FieldValue::with_type(
FieldValue::value(Value::Object(ValueMapping::from([
(Name::new("name"), Value::String(token.name)),
(Name::new("symbol"), Value::String(token.symbol)),
(Name::new("decimals"), Value::from(token.decimals)),
(Name::new("contractAddress"), Value::String(token.contract_address)),
(Name::new("amount"), Value::String(token.amount)),
]))),
ERC20_TYPE_NAME.to_string(),
),
ErcTokenType::Erc721(token) => FieldValue::with_type(
FieldValue::value(Value::Object(ValueMapping::from([
(Name::new("name"), Value::String(token.name)),
(Name::new("symbol"), Value::String(token.symbol)),
(Name::new("tokenId"), Value::String(token.token_id)),
(Name::new("contractAddress"), Value::String(token.contract_address)),
(Name::new("metadata"), Value::String(token.metadata)),
(
Name::new("metadataName"),
token.metadata_name.map(Value::String).unwrap_or(Value::Null),
),
(
Name::new("metadataDescription"),
token.metadata_description.map(Value::String).unwrap_or(Value::Null),
),
(
Name::new("metadataAttributes"),
token.metadata_attributes.map(Value::String).unwrap_or(Value::Null),
),
(Name::new("imagePath"), Value::String(token.image_path)),
]))),
ERC721_TYPE_NAME.to_string(),
),
}
}
}
15 changes: 15 additions & 0 deletions crates/torii/graphql/src/object/erc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use async_graphql::Value;

use super::connection::cursor;
use crate::query::order::CursorDirection;

Expand All @@ -15,3 +17,16 @@ fn handle_cursor(
Err(_) => Err(sqlx::Error::Decode("Invalid cursor format".into())),
}
}

#[derive(Debug, Clone)]
pub struct ConnectionEdge<T> {
pub node: T,
pub cursor: String,
}

#[derive(Debug, Clone)]
pub struct Connection<T> {
pub total_count: i64,
pub edges: Vec<ConnectionEdge<T>>,
pub page_info: Value,
}
Loading

0 comments on commit 39233db

Please sign in to comment.