From a7204acbba5ed92e4f55ac626e8085d0ee08233f Mon Sep 17 00:00:00 2001 From: lambda-0x <0xlambda@protonmail.com> Date: Sat, 12 Oct 2024 15:24:36 +0530 Subject: [PATCH] torii!: change type of `id` in erc_transfers commit-id:dd61fbb9 --- crates/torii/core/src/engine.rs | 2 +- .../src/processors/erc20_legacy_transfer.rs | 4 +--- .../core/src/processors/erc20_transfer.rs | 4 +--- .../src/processors/erc721_legacy_transfer.rs | 4 +--- .../core/src/processors/erc721_transfer.rs | 4 +--- crates/torii/core/src/sql/erc.rs | 18 +++++++++--------- .../graphql/src/object/erc/erc_transfer.rs | 12 +++++++----- .../migrations/20240913104418_add_erc.sql | 2 +- ...transaction_hash_in_erc_transfers_table.sql | 1 - 9 files changed, 22 insertions(+), 29 deletions(-) delete mode 100644 crates/torii/migrations/20241011103804_add_transaction_hash_in_erc_transfers_table.sql diff --git a/crates/torii/core/src/engine.rs b/crates/torii/core/src/engine.rs index f616ee64fb..ed51840ae6 100644 --- a/crates/torii/core/src/engine.rs +++ b/crates/torii/core/src/engine.rs @@ -905,6 +905,6 @@ where } // event_id format: block_number:transaction_hash:event_idx -pub(crate) fn get_transaction_hash_from_event_id(event_id: &str) -> String { +pub fn get_transaction_hash_from_event_id(event_id: &str) -> String { event_id.split(':').nth(1).unwrap().to_string() } diff --git a/crates/torii/core/src/processors/erc20_legacy_transfer.rs b/crates/torii/core/src/processors/erc20_legacy_transfer.rs index 138ed79f19..bf4fd33e49 100644 --- a/crates/torii/core/src/processors/erc20_legacy_transfer.rs +++ b/crates/torii/core/src/processors/erc20_legacy_transfer.rs @@ -7,7 +7,6 @@ use starknet::providers::Provider; use tracing::debug; use super::EventProcessor; -use crate::engine::get_transaction_hash_from_event_id; use crate::sql::Sql; pub(crate) const LOG_TARGET: &str = "torii_core::processors::erc20_legacy_transfer"; @@ -47,7 +46,6 @@ where let token_address = event.from_address; let from = event.data[0]; let to = event.data[1]; - let transaction_hash = get_transaction_hash_from_event_id(event_id); let value = U256Cainome::cairo_deserialize(&event.data, 2)?; let value = U256::from_words(value.low, value.high); @@ -59,7 +57,7 @@ where value, world.provider(), block_timestamp, - &transaction_hash, + event_id, ) .await?; debug!(target: LOG_TARGET,from = ?from, to = ?to, value = ?value, "Legacy ERC20 Transfer"); diff --git a/crates/torii/core/src/processors/erc20_transfer.rs b/crates/torii/core/src/processors/erc20_transfer.rs index 895c4b8427..7ed1620503 100644 --- a/crates/torii/core/src/processors/erc20_transfer.rs +++ b/crates/torii/core/src/processors/erc20_transfer.rs @@ -7,7 +7,6 @@ use starknet::providers::Provider; use tracing::debug; use super::EventProcessor; -use crate::engine::get_transaction_hash_from_event_id; use crate::sql::Sql; pub(crate) const LOG_TARGET: &str = "torii_core::processors::erc20_transfer"; @@ -50,7 +49,6 @@ where let value = U256Cainome::cairo_deserialize(&event.data, 0)?; let value = U256::from_words(value.low, value.high); - let transaction_hash = get_transaction_hash_from_event_id(event_id); db.handle_erc20_transfer( token_address, @@ -59,7 +57,7 @@ where value, world.provider(), block_timestamp, - &transaction_hash, + event_id, ) .await?; debug!(target: LOG_TARGET,from = ?from, to = ?to, value = ?value, "ERC20 Transfer"); diff --git a/crates/torii/core/src/processors/erc721_legacy_transfer.rs b/crates/torii/core/src/processors/erc721_legacy_transfer.rs index c38957516c..198a1ebbd9 100644 --- a/crates/torii/core/src/processors/erc721_legacy_transfer.rs +++ b/crates/torii/core/src/processors/erc721_legacy_transfer.rs @@ -7,7 +7,6 @@ use starknet::providers::Provider; use tracing::debug; use super::EventProcessor; -use crate::engine::get_transaction_hash_from_event_id; use crate::sql::Sql; pub(crate) const LOG_TARGET: &str = "torii_core::processors::erc721_legacy_transfer"; @@ -50,7 +49,6 @@ where let token_id = U256Cainome::cairo_deserialize(&event.data, 2)?; let token_id = U256::from_words(token_id.low, token_id.high); - let transaction_hash = get_transaction_hash_from_event_id(event_id); db.handle_erc721_transfer( token_address, @@ -59,7 +57,7 @@ where token_id, world.provider(), block_timestamp, - &transaction_hash, + event_id, ) .await?; debug!(target: LOG_TARGET, from = ?from, to = ?to, token_id = ?token_id, "ERC721 Transfer"); diff --git a/crates/torii/core/src/processors/erc721_transfer.rs b/crates/torii/core/src/processors/erc721_transfer.rs index c55e700725..349bdbea24 100644 --- a/crates/torii/core/src/processors/erc721_transfer.rs +++ b/crates/torii/core/src/processors/erc721_transfer.rs @@ -7,7 +7,6 @@ use starknet::providers::Provider; use tracing::debug; use super::EventProcessor; -use crate::engine::get_transaction_hash_from_event_id; use crate::sql::Sql; pub(crate) const LOG_TARGET: &str = "torii_core::processors::erc721_transfer"; @@ -50,7 +49,6 @@ where let token_id = U256Cainome::cairo_deserialize(&event.keys, 3)?; let token_id = U256::from_words(token_id.low, token_id.high); - let transaction_hash = get_transaction_hash_from_event_id(event_id); db.handle_erc721_transfer( token_address, @@ -59,7 +57,7 @@ where token_id, world.provider(), block_timestamp, - &transaction_hash, + event_id, ) .await?; debug!(target: LOG_TARGET, from = ?from, to = ?to, token_id = ?token_id, "ERC721 Transfer"); diff --git a/crates/torii/core/src/sql/erc.rs b/crates/torii/core/src/sql/erc.rs index 4dea567af7..f82eacb1a2 100644 --- a/crates/torii/core/src/sql/erc.rs +++ b/crates/torii/core/src/sql/erc.rs @@ -25,7 +25,7 @@ impl Sql { amount: U256, provider: &P, block_timestamp: u64, - transaction_hash: &str, + event_id: &str, ) -> Result<()> { // contract_address let token_id = felt_to_sql_string(&contract_address); @@ -44,7 +44,7 @@ impl Sql { amount, &token_id, block_timestamp, - transaction_hash, + event_id, )?; if from_address != Felt::ZERO { @@ -81,7 +81,7 @@ impl Sql { token_id: U256, provider: &P, block_timestamp: u64, - transaction_hash: &str, + event_id: &str, ) -> Result<()> { // contract_address:id let token_id = felt_and_u256_to_sql_string(&contract_address, &token_id); @@ -99,7 +99,7 @@ impl Sql { U256::from(1u8), &token_id, block_timestamp, - transaction_hash, + event_id, )?; // from_address/contract_address:id @@ -320,22 +320,22 @@ impl Sql { amount: U256, token_id: &str, block_timestamp: u64, - transaction_hash: &str, + event_id: &str, ) -> Result<()> { - let insert_query = "INSERT INTO erc_transfers (contract_address, from_address, \ - to_address, amount, token_id, executed_at, transaction_hash) VALUES \ - (?, ?, ?, ?, ?, ?, ?)"; + let insert_query = "INSERT INTO erc_transfers (id, contract_address, from_address, \ + to_address, amount, token_id, executed_at) VALUES (?, ?, ?, ?, ?, ?, \ + ?)"; self.executor.send(QueryMessage::other( insert_query.to_string(), vec![ + Argument::String(event_id.to_string()), Argument::FieldElement(contract_address), Argument::FieldElement(from), Argument::FieldElement(to), Argument::String(u256_to_sql_string(&amount)), Argument::String(token_id.to_string()), Argument::String(utc_dt_string_from_timestamp(block_timestamp)), - Argument::String(transaction_hash.to_string()), ], ))?; diff --git a/crates/torii/graphql/src/object/erc/erc_transfer.rs b/crates/torii/graphql/src/object/erc/erc_transfer.rs index 16fed939cb..ee522fef20 100644 --- a/crates/torii/graphql/src/object/erc/erc_transfer.rs +++ b/crates/torii/graphql/src/object/erc/erc_transfer.rs @@ -4,6 +4,7 @@ use convert_case::{Case, Casing}; use serde::Deserialize; use sqlx::{FromRow, Pool, Sqlite, SqliteConnection}; use starknet_crypto::Felt; +use torii_core::engine::get_transaction_hash_from_event_id; use torii_core::sql::utils::felt_to_sql_string; use tracing::warn; @@ -70,6 +71,7 @@ async fn fetch_erc_transfers( let query = format!( r#" SELECT + et.id, et.contract_address, et.from_address, et.to_address, @@ -79,8 +81,7 @@ SELECT t.name, t.symbol, t.decimals, - c.contract_type, - et.transaction_hash + c.contract_type FROM erc_transfers et JOIN @@ -103,6 +104,7 @@ LIMIT {}; for row in rows { let row = TransferQueryResultRaw::from_row(&row)?; + let transaction_hash = get_transaction_hash_from_event_id(&row.id); let transfer_value = match row.contract_type.to_lowercase().as_str() { "erc20" => { @@ -122,7 +124,7 @@ LIMIT {}; (Name::new("type"), Value::String(row.contract_type)), (Name::new("executedAt"), Value::String(row.executed_at)), (Name::new("tokenMetadata"), token_metadata), - (Name::new("transactionHash"), Value::String(row.transaction_hash.clone())), + (Name::new("transactionHash"), Value::String(transaction_hash)), ])) } "erc721" => { @@ -145,7 +147,7 @@ LIMIT {}; (Name::new("type"), Value::String(row.contract_type)), (Name::new("executedAt"), Value::String(row.executed_at)), (Name::new("tokenMetadata"), token_metadata), - (Name::new("transactionHash"), Value::String(row.transaction_hash.clone())), + (Name::new("transactionHash"), Value::String(transaction_hash)), ])) } _ => { @@ -171,6 +173,7 @@ LIMIT {}; #[derive(FromRow, Deserialize, Debug, Clone)] #[serde(rename_all = "camelCase")] struct TransferQueryResultRaw { + pub id: String, pub contract_address: String, pub from_address: String, pub to_address: String, @@ -181,5 +184,4 @@ struct TransferQueryResultRaw { pub symbol: String, pub decimals: u8, pub contract_type: String, - pub transaction_hash: String, } diff --git a/crates/torii/migrations/20240913104418_add_erc.sql b/crates/torii/migrations/20240913104418_add_erc.sql index 4366acac27..2d288b7aca 100644 --- a/crates/torii/migrations/20240913104418_add_erc.sql +++ b/crates/torii/migrations/20240913104418_add_erc.sql @@ -23,7 +23,7 @@ CREATE TABLE tokens ( ); CREATE TABLE erc_transfers ( - id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + id TEXT NOT NULL PRIMARY KEY, contract_address TEXT NOT NULL, from_address TEXT NOT NULL, to_address TEXT NOT NULL, diff --git a/crates/torii/migrations/20241011103804_add_transaction_hash_in_erc_transfers_table.sql b/crates/torii/migrations/20241011103804_add_transaction_hash_in_erc_transfers_table.sql deleted file mode 100644 index e83643cecb..0000000000 --- a/crates/torii/migrations/20241011103804_add_transaction_hash_in_erc_transfers_table.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE erc_transfers ADD COLUMN transaction_hash TEXT NOT NULL; \ No newline at end of file