Skip to content

Commit

Permalink
torii!: change type of id in erc_transfers
Browse files Browse the repository at this point in the history
commit-id:dd61fbb9
  • Loading branch information
lambda-0x committed Oct 13, 2024
1 parent 04e1dc4 commit 7035d54
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 29 deletions.
2 changes: 1 addition & 1 deletion crates/torii/core/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
4 changes: 1 addition & 3 deletions crates/torii/core/src/processors/erc20_legacy_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand All @@ -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");
Expand Down
4 changes: 1 addition & 3 deletions crates/torii/core/src/processors/erc20_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Expand All @@ -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");
Expand Down
4 changes: 1 addition & 3 deletions crates/torii/core/src/processors/erc721_legacy_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Expand All @@ -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");
Expand Down
4 changes: 1 addition & 3 deletions crates/torii/core/src/processors/erc721_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Expand All @@ -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");
Expand Down
18 changes: 9 additions & 9 deletions crates/torii/core/src/sql/erc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -44,7 +44,7 @@ impl Sql {
amount,
&token_id,
block_timestamp,
transaction_hash,
event_id,
)?;

if from_address != Felt::ZERO {
Expand Down Expand Up @@ -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);
Expand All @@ -99,7 +99,7 @@ impl Sql {
U256::from(1u8),
&token_id,
block_timestamp,
transaction_hash,
event_id,
)?;

// from_address/contract_address:id
Expand Down Expand Up @@ -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()),
],
))?;

Expand Down
12 changes: 7 additions & 5 deletions crates/torii/graphql/src/object/erc/erc_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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" => {
Expand All @@ -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" => {
Expand All @@ -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)),
]))
}
_ => {
Expand All @@ -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,
Expand All @@ -181,5 +184,4 @@ struct TransferQueryResultRaw {
pub symbol: String,
pub decimals: u8,
pub contract_type: String,
pub transaction_hash: String,
}
2 changes: 1 addition & 1 deletion crates/torii/migrations/20240913104418_add_erc.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

This file was deleted.

0 comments on commit 7035d54

Please sign in to comment.