Skip to content

Commit 7617ce0

Browse files
committed
feat(storage): name from metadata for nft token in tokens table
1 parent 15a3a3d commit 7617ce0

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

crates/sqlite/sqlite/src/executor/erc.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ pub struct TokenTrait {
4444
pub trait_value: String,
4545
}
4646

47+
/// Extract the name from NFT metadata JSON if present
48+
pub fn extract_name_from_metadata(metadata: &str) -> Option<String> {
49+
if metadata.is_empty() {
50+
return None;
51+
}
52+
53+
let metadata_json: serde_json::Value = serde_json::from_str(metadata).ok()?;
54+
metadata_json
55+
.get("name")
56+
.and_then(|v| v.as_str())
57+
.map(|s| s.to_string())
58+
.filter(|s| !s.is_empty())
59+
}
60+
4761
/// Extract traits from NFT metadata JSON
4862
pub fn extract_traits_from_json(metadata: &str) -> Result<Vec<TokenTrait>, serde_json::Error> {
4963
let metadata_json: serde_json::Value = serde_json::from_str(metadata)?;
@@ -473,6 +487,34 @@ mod tests {
473487
use super::*;
474488
use serde_json::json;
475489

490+
#[test]
491+
fn test_extract_name_from_metadata() {
492+
// Test with valid name
493+
let metadata = r#"{"name": "Cool NFT #42", "description": "A cool NFT"}"#;
494+
assert_eq!(
495+
extract_name_from_metadata(metadata),
496+
Some("Cool NFT #42".to_string())
497+
);
498+
499+
// Test with empty name
500+
let metadata = r#"{"name": "", "description": "A cool NFT"}"#;
501+
assert_eq!(extract_name_from_metadata(metadata), None);
502+
503+
// Test with no name field
504+
let metadata = r#"{"description": "A cool NFT"}"#;
505+
assert_eq!(extract_name_from_metadata(metadata), None);
506+
507+
// Test with empty metadata
508+
assert_eq!(extract_name_from_metadata(""), None);
509+
510+
// Test with invalid JSON
511+
assert_eq!(extract_name_from_metadata("invalid json"), None);
512+
513+
// Test with name as number (should return None)
514+
let metadata = r#"{"name": 123}"#;
515+
assert_eq!(extract_name_from_metadata(metadata), None);
516+
}
517+
476518
#[test]
477519
fn test_extract_traits_from_json() {
478520
let metadata = r#"{

crates/sqlite/sqlite/src/executor/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::time::{SystemTime, UNIX_EPOCH};
66
use cainome::cairo_serde::{ByteArray, CairoSerde};
77
use dojo_types::schema::{Struct, Ty};
88
use erc::{
9-
store_token_attributes, update_contract_traits_from_metadata,
9+
extract_name_from_metadata, store_token_attributes, update_contract_traits_from_metadata,
1010
update_contract_traits_on_metadata_change, UpdateTokenMetadataQuery,
1111
};
1212
use metrics::{counter, histogram};
@@ -969,7 +969,7 @@ impl<P: Provider + Sync + Send + Clone + 'static> Executor<'_, P> {
969969

970970
// If we find a token already registered for this contract_address we dont need to
971971
// refetch the data since its same for all tokens of this contract
972-
let (name, symbol) = match res {
972+
let (contract_name, symbol) = match res {
973973
Ok((name, symbol)) => {
974974
debug!(
975975
target: LOG_TARGET,
@@ -1058,6 +1058,10 @@ impl<P: Provider + Sync + Send + Clone + 'static> Executor<'_, P> {
10581058
}
10591059
};
10601060

1061+
// Try to get name from token metadata, fallback to contract name
1062+
let name = extract_name_from_metadata(&register_nft_token.metadata)
1063+
.unwrap_or(contract_name);
1064+
10611065
let query = sqlx::query_as::<_, torii_sqlite_types::Token>(
10621066
"INSERT INTO tokens (id, contract_address, token_id, name, symbol, decimals, \
10631067
metadata, total_supply, traits) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING *",

0 commit comments

Comments
 (0)