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 2, 2024
1 parent a0a173e commit 752a88e
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 37 deletions.
86 changes: 61 additions & 25 deletions crates/torii/core/src/executor/erc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use starknet::core::types::{BlockId, BlockTag, FunctionCall, U256};
use starknet::core::utils::{get_selector_from_name, parse_cairo_short_string};
use starknet::providers::Provider;
use starknet_crypto::Felt;
use tracing::{debug, trace};
use tracing::{debug, error, trace};

use super::{ApplyBalanceDiffQuery, Executor};
use crate::constants::{IPFS_CLIENT_MAX_RETRY, SQL_FELT_DELIMITER, TOKEN_BALANCE_TABLE};
Expand All @@ -30,7 +30,7 @@ pub struct RegisterErc721TokenMetadata {
pub query: RegisterErc721TokenQuery,
pub name: String,
pub symbol: String,
pub metadata: String,
pub metadata: Option<String>,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -159,7 +159,7 @@ impl<'c, P: Provider + Sync + Send + 'static> Executor<'c, P> {
)
.await
{
token_uri
Some(token_uri)

Check warning on line 162 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L162

Added line #L162 was not covered by tests
} else if let Ok(token_uri) = provider
.call(
FunctionCall {
Expand All @@ -174,32 +174,68 @@ impl<'c, P: Provider + Sync + Send + 'static> Executor<'c, P> {
)
.await
{
token_uri
Some(token_uri)

Check warning on line 177 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L177

Added line #L177 was not covered by tests
} else {
return Err(anyhow::anyhow!("Failed to fetch token_uri"));
error!(
"Failed to fetch token_uri for token_id: {}",

Check warning on line 180 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L179-L180

Added lines #L179 - L180 were not covered by tests
register_erc721_token.actual_token_id
);
None

Check warning on line 183 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L183

Added line #L183 was not covered by tests
};

let token_uri = if let Ok(byte_array) = ByteArray::cairo_deserialize(&token_uri, 0) {
byte_array.to_string().expect("Return value not String")
} else if let Ok(felt_array) = Vec::<Felt>::cairo_deserialize(&token_uri, 0) {
felt_array
.iter()
.map(parse_cairo_short_string)
.collect::<Result<Vec<String>, _>>()
.map(|strings| strings.join(""))
.map_err(|_| anyhow::anyhow!("Failed parsing Array<Felt> to String"))?
} else {
return Err(anyhow::anyhow!("token_uri is neither ByteArray nor Array<Felt>"));
};
if let Some(token_uri) = token_uri {
let token_uri = if let Ok(byte_array) = ByteArray::cairo_deserialize(&token_uri, 0) {
Some(byte_array.to_string().expect("Return value not String"))
} else if let Ok(felt_array) = Vec::<Felt>::cairo_deserialize(&token_uri, 0) {

Check warning on line 189 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L186-L189

Added lines #L186 - L189 were not covered by tests
Some(
felt_array
.iter()
.map(parse_cairo_short_string)
.collect::<Result<Vec<String>, _>>()
.map(|strings| strings.join(""))
.map_err(|_| anyhow::anyhow!("Failed parsing Array<Felt> to String"))?,

Check warning on line 196 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L191-L196

Added lines #L191 - L196 were not covered by tests
)
} else {
error!(
"token_uri is neither ByteArray nor Array<Felt> for token_id: {}",

Check warning on line 200 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L199-L200

Added lines #L199 - L200 were not covered by tests
register_erc721_token.actual_token_id
);
None

Check warning on line 203 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L203

Added line #L203 was not covered by tests
};

let metadata = if let Some(token_uri) = token_uri {
match Self::fetch_metadata(&token_uri).await {
Ok(metadata) => {
let metadata = serde_json::to_string(&metadata)
.context("Failed to serialize metadata")?;
Some(metadata)

Check warning on line 211 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L206-L211

Added lines #L206 - L211 were not covered by tests
}
Err(e) => {
error!(
"Failed to fetch metadata for token_id: {}, error: {}",

Check warning on line 215 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L213-L215

Added lines #L213 - L215 were not covered by tests
register_erc721_token.actual_token_id, e
);
None

Check warning on line 218 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L218

Added line #L218 was not covered by tests
}
}
} else {
None

Check warning on line 222 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L222

Added line #L222 was not covered by tests
};

return Ok(RegisterErc721TokenMetadata {
query: register_erc721_token,
metadata,
name,
symbol,
});
}

Check warning on line 231 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L225-L231

Added lines #L225 - L231 were not covered by tests

let metadata = Self::fetch_metadata(&token_uri).await.with_context(|| {
format!(
"Failed to fetch metadata for token_id: {}",
register_erc721_token.actual_token_id
)
})?;
let metadata = serde_json::to_string(&metadata).context("Failed to serialize metadata")?;
Ok(RegisterErc721TokenMetadata { query: register_erc721_token, metadata, name, symbol })
Ok(RegisterErc721TokenMetadata {
query: register_erc721_token,
metadata: None,
name,
symbol,
})

Check warning on line 238 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L233-L238

Added lines #L233 - L238 were not covered by tests
}

// given a uri which can be either http/https url or data uri, fetch the metadata erc721
Expand Down
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 752a88e

Please sign in to comment.