Skip to content

Commit

Permalink
fix(torii): handle case where token_id no longer exists and few graph…
Browse files Browse the repository at this point in the history
…ql changes

commit-id:e92e39b0
  • Loading branch information
lambda-0x committed Dec 3, 2024
1 parent 6644564 commit acf546b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
12 changes: 6 additions & 6 deletions crates/torii/graphql/src/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ lazy_static! {
),
]);
pub static ref PAGE_INFO_TYPE_MAPPING: TypeMapping = TypeMapping::from([
(Name::new("hasPreviousPage"), TypeData::Simple(TypeRef::named(TypeRef::BOOLEAN))),
(Name::new("hasNextPage"), TypeData::Simple(TypeRef::named(TypeRef::BOOLEAN))),
(Name::new("hasPreviousPage"), TypeData::Simple(TypeRef::named_nn(TypeRef::BOOLEAN))),
(Name::new("hasNextPage"), TypeData::Simple(TypeRef::named_nn(TypeRef::BOOLEAN))),
(
Name::new("startCursor"),
TypeData::Simple(TypeRef::named(GraphqlType::Cursor.to_string())),
Expand Down Expand Up @@ -160,7 +160,7 @@ lazy_static! {
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("decimals"), TypeData::Simple(TypeRef::named_nn(TypeRef::INT))),
(Name::new("contractAddress"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("amount"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
]);
Expand All @@ -171,9 +171,9 @@ lazy_static! {
(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("metadataName"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
(Name::new("metadataDescription"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
(Name::new("metadataAttributes"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
(Name::new("imagePath"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
]);

Expand Down
12 changes: 9 additions & 3 deletions crates/torii/graphql/src/object/erc/token_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,14 @@ fn token_balances_connection_output<'a>(
let token_id = row.token_id.split(':').collect::<Vec<&str>>();
assert!(token_id.len() == 2);

// skip the token if metadata is null
if row.metadata.is_none() {
continue;
}
let metadata_str = row.metadata.as_ref().unwrap();

Check warning on line 242 in crates/torii/graphql/src/object/erc/token_balance.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/graphql/src/object/erc/token_balance.rs#L238-L242

Added lines #L238 - L242 were not covered by tests
let metadata: serde_json::Value =
serde_json::from_str(&row.metadata).expect("metadata is always json");
serde_json::from_str(metadata_str).expect("metadata is always json");

Check warning on line 244 in crates/torii/graphql/src/object/erc/token_balance.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/graphql/src/object/erc/token_balance.rs#L244

Added line #L244 was not covered by tests
let metadata_name =
metadata.get("name").map(|v| v.to_string().trim_matches('"').to_string());
let metadata_description = metadata
Expand All @@ -248,7 +254,7 @@ fn token_balances_connection_output<'a>(

let token_metadata = Erc721Token {
name: row.name,
metadata: row.metadata,
metadata: metadata_str.to_owned(),

Check warning on line 257 in crates/torii/graphql/src/object/erc/token_balance.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/graphql/src/object/erc/token_balance.rs#L257

Added line #L257 was not covered by tests
contract_address: row.contract_address,
symbol: row.symbol,
token_id: token_id[1].to_string(),
Expand Down Expand Up @@ -295,5 +301,5 @@ struct BalanceQueryResultRaw {
pub token_id: String,
pub balance: String,
pub contract_type: String,
pub metadata: String,
pub metadata: Option<String>,
}
12 changes: 9 additions & 3 deletions crates/torii/graphql/src/object/erc/token_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,14 @@ fn token_transfers_connection_output<'a>(
let token_id = row.token_id.split(':').collect::<Vec<&str>>();
assert!(token_id.len() == 2);

// skip the token if metadata is null
if row.metadata.is_none() {
continue;
}

let metadata_str = row.metadata.as_ref().unwrap();

Check warning on line 271 in crates/torii/graphql/src/object/erc/token_transfer.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/graphql/src/object/erc/token_transfer.rs#L267-L271

Added lines #L267 - L271 were not covered by tests
let metadata: serde_json::Value =
serde_json::from_str(&row.metadata).expect("metadata is always json");
serde_json::from_str(metadata_str).expect("metadata is always json");

Check warning on line 273 in crates/torii/graphql/src/object/erc/token_transfer.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/graphql/src/object/erc/token_transfer.rs#L273

Added line #L273 was not covered by tests
let metadata_name =
metadata.get("name").map(|v| v.to_string().trim_matches('"').to_string());
let metadata_description = metadata
Expand All @@ -277,7 +283,7 @@ fn token_transfers_connection_output<'a>(

let token_metadata = ErcTokenType::Erc721(Erc721Token {
name: row.name,
metadata: row.metadata,
metadata: metadata_str.to_owned(),

Check warning on line 286 in crates/torii/graphql/src/object/erc/token_transfer.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/graphql/src/object/erc/token_transfer.rs#L286

Added line #L286 was not covered by tests
contract_address: row.contract_address,
symbol: row.symbol,
token_id: token_id[1].to_string(),
Expand Down Expand Up @@ -333,7 +339,7 @@ struct TransferQueryResultRaw {
pub symbol: String,
pub decimals: u8,
pub contract_type: String,
pub metadata: String,
pub metadata: Option<String>,
}

#[derive(Debug, Clone)]
Expand Down

0 comments on commit acf546b

Please sign in to comment.