Skip to content

Commit

Permalink
test(torii): add metadata to erc1155 and update (#3054)
Browse files Browse the repository at this point in the history
* test(torii): add metadata to erc1155 and update

* add dataurl prefix

* fix erc1155 update metadata test

* fix

* fmt
  • Loading branch information
Larkooo authored Feb 21, 2025
1 parent 7facdb7 commit c48484e
Show file tree
Hide file tree
Showing 9 changed files with 481 additions and 56 deletions.
98 changes: 98 additions & 0 deletions crates/torii/indexer/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,104 @@ async fn test_load_from_remote_update(sequencer: &RunnerCtx) {
assert_eq!(name, "mimi");
}

#[tokio::test(flavor = "multi_thread")]
#[katana_runner::test(accounts = 10, db_dir = copy_spawn_and_move_db().as_str())]
async fn test_update_token_metadata_erc1155(sequencer: &RunnerCtx) {
let setup = CompilerTestSetup::from_examples("../../dojo/core", "../../../examples/");
let config = setup.build_test_config("spawn-and-move", Profile::DEV);

let ws = scarb::ops::read_workspace(config.manifest_path(), &config).unwrap();

let account = sequencer.account(0);
let provider = Arc::new(JsonRpcClient::new(HttpTransport::new(sequencer.url())));

let world_local = ws.load_world_local().unwrap();
let world_address = world_local.deterministic_world_address().unwrap();

let world_reader = WorldContractReader::new(world_address, Arc::clone(&provider));

let rewards_address = world_local
.external_contracts
.iter()
.find(|c| c.instance_name == "Rewards")
.unwrap()
.address;

let world = WorldContract::new(world_address, &account);

let res = world
.grant_writer(&compute_bytearray_hash("ns"), &ContractAddress(rewards_address))
.send_with_cfg(&TxnConfig::init_wait())
.await
.unwrap();

TransactionWaiter::new(res.transaction_hash, &provider).await.unwrap();

let tx = &account
.execute_v1(vec![Call {
to: rewards_address,
selector: get_selector_from_name("mint").unwrap(),
calldata: vec![Felt::from(1), Felt::ZERO, Felt::from(1), Felt::ZERO],
}])
.send()
.await
.unwrap();

TransactionWaiter::new(tx.transaction_hash, &provider).await.unwrap();

let owner_account = sequencer.account(3);
let tx = &owner_account
.execute_v1(vec![Call {
to: rewards_address,
selector: get_selector_from_name("update_token_metadata").unwrap(),
calldata: vec![Felt::from(1), Felt::ZERO],
}])
.send()
.await
.unwrap();

TransactionWaiter::new(tx.transaction_hash, &provider).await.unwrap();

let block_number = provider.block_number().await.unwrap();

let tempfile = NamedTempFile::new().unwrap();
let path = tempfile.path().to_string_lossy();
let options = SqliteConnectOptions::from_str(&path).unwrap().create_if_missing(true);
let pool = SqlitePoolOptions::new().connect_with(options).await.unwrap();
sqlx::migrate!("../migrations").run(&pool).await.unwrap();

let (shutdown_tx, _) = broadcast::channel(1);
let (mut executor, sender) =
Executor::new(pool.clone(), shutdown_tx.clone(), Arc::clone(&provider), 100).await.unwrap();
tokio::spawn(async move {
executor.run().await.unwrap();
});

let contracts = vec![Contract { address: rewards_address, r#type: ContractType::ERC1155 }];
let model_cache = Arc::new(ModelCache::new(pool.clone()));
let db = Sql::new(pool.clone(), sender.clone(), &contracts, model_cache.clone()).await.unwrap();

let _ = bootstrap_engine(world_reader, db.clone(), Arc::clone(&provider), &contracts)
.await
.unwrap();

let token = sqlx::query_as::<_, Token>(
format!(
"SELECT * from tokens where contract_address = '{:#x}' ORDER BY token_id",
rewards_address
)
.as_str(),
)
.fetch_one(&pool)
.await
.unwrap();

assert!(token.metadata.contains(&format!(
"https://api.dicebear.com/9.x/lorelei-neutral/png?seed={}",
block_number + 1
)));
}

/// Count the number of rows in a table.
///
/// # Arguments
Expand Down
70 changes: 39 additions & 31 deletions crates/torii/sqlite/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,47 +60,55 @@ pub fn sanitize_json_string(s: &str) -> String {
let mut result = String::new();
let mut chars = s.chars().peekable();
let mut in_string = false;
let mut backslash_count = 0;

while let Some(c) = chars.next() {
match c {
'"' => {
if !in_string {
// Starting a string
result.push('"');
in_string = true;
} else {
// Check next char to see if this is the end of the string
match chars.peek() {
Some(&':') | Some(&',') | Some(&'}') => {
// This is end of a JSON string
result.push('"');
in_string = false;
}
_ => {
// This is an internal quote that needs escaping
result.push_str("\\\"");
}
if !in_string {
if c == '"' {
in_string = true;
backslash_count = 0;
result.push('"');
} else {
result.push(c);
}
} else if c == '\\' {
backslash_count += 1;
result.push('\\');
} else if c == '"' {
if backslash_count % 2 == 0 {
// Unescaped double quote
let mut temp_chars = chars.clone();
// Skip whitespace
while let Some(&next_c) = temp_chars.peek() {
if next_c.is_whitespace() {
temp_chars.next();
} else {
break;
}
}
}
'\\' => {
if let Some(&next) = chars.peek() {
if next == '"' {
// Already escaped quote, preserve it without adding extra escapes
result.push('\\');
// Check next non-whitespace character
if let Some(&next_c) = temp_chars.peek() {
if next_c == ':' || next_c == ',' || next_c == '}' {
// End of string
result.push('"');
chars.next(); // Consume the quote
in_string = false;
} else {
// Regular backslash
result.push('\\');
// Internal unescaped quote, escape it
result.push_str("\\\"");
}
} else {
result.push('\\');
// End of input, treat as end of string
result.push('"');
in_string = false;
}
} else {
// Escaped double quote, part of string
result.push('"');
}
_ => {
result.push(c);
}
backslash_count = 0;
} else {
result.push(c);
backslash_count = 0;
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/torii/types-test/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ version = "2.9.2"

[[package]]
name = "types_test"
version = "1.2.0"
version = "1.2.1"
dependencies = [
"dojo",
]
2 changes: 1 addition & 1 deletion examples/simple/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version = 1

[[package]]
name = "dojo"
version = "1.2.0"
version = "1.2.1"
dependencies = [
"dojo_plugin",
]
Expand Down
2 changes: 1 addition & 1 deletion examples/spawn-and-move/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dependencies = [

[[package]]
name = "dojo_examples"
version = "1.2.0"
version = "1.2.1"
dependencies = [
"armory",
"bestiary",
Expand Down
Loading

0 comments on commit c48484e

Please sign in to comment.