Skip to content

Commit

Permalink
feat(torii): add transaction hash to erc_transfers table
Browse files Browse the repository at this point in the history
commit-id:02be81af
  • Loading branch information
lambda-0x committed Oct 13, 2024
1 parent 0c056f2 commit 49fd019
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 24 deletions.
9 changes: 9 additions & 0 deletions crates/torii/core/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,8 @@ impl<P: Provider + Send + Sync + std::fmt::Debug + 'static> Engine<P> {
let mut unique_contracts = HashSet::new();
// Contract -> Cursor
for (event_idx, event) in events.iter().enumerate() {
// NOTE: erc* processors expect the event_id to be in this format to get
// transaction_hash:
let event_id =
format!("{:#064x}:{:#x}:{:#04x}", block_number, transaction_hash, event_idx);

Expand Down Expand Up @@ -681,6 +683,8 @@ impl<P: Provider + Send + Sync + std::fmt::Debug + 'static> Engine<P> {

unique_contracts.insert(event.from_address);

// NOTE: erc* processors expect the event_id to be in this format to get
// transaction_hash:
let event_id =
format!("{:#064x}:{:#x}:{:#04x}", block_number, *transaction_hash, event_idx);

Expand Down Expand Up @@ -899,3 +903,8 @@ where
MaybePendingBlockWithTxHashes::PendingBlock(block) => Ok(block.timestamp),
}
}

// event_id format: block_number:transaction_hash:event_idx
pub(crate) fn get_transaction_hash_from_event_id(event_id: &str) -> String {
event_id.split(':').nth(1).unwrap().to_string()
}
16 changes: 13 additions & 3 deletions crates/torii/core/src/processors/erc20_legacy_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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 @@ -40,18 +41,27 @@ where
db: &mut Sql,
_block_number: u64,
block_timestamp: u64,
_event_id: &str,
event_id: &str,
event: &Event,
) -> Result<(), Error> {
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);

db.handle_erc20_transfer(token_address, from, to, value, world.provider(), block_timestamp)
.await?;
db.handle_erc20_transfer(
token_address,
from,
to,
value,
world.provider(),
block_timestamp,
&transaction_hash,
)
.await?;
debug!(target: LOG_TARGET,from = ?from, to = ?to, value = ?value, "Legacy ERC20 Transfer");

Ok(())
Expand Down
16 changes: 13 additions & 3 deletions crates/torii/core/src/processors/erc20_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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 @@ -40,7 +41,7 @@ where
db: &mut Sql,
_block_number: u64,
block_timestamp: u64,
_event_id: &str,
event_id: &str,
event: &Event,
) -> Result<(), Error> {
let token_address = event.from_address;
Expand All @@ -49,9 +50,18 @@ 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, from, to, value, world.provider(), block_timestamp)
.await?;
db.handle_erc20_transfer(
token_address,
from,
to,
value,
world.provider(),
block_timestamp,
&transaction_hash,
)
.await?;
debug!(target: LOG_TARGET,from = ?from, to = ?to, value = ?value, "ERC20 Transfer");

Ok(())
Expand Down
5 changes: 4 additions & 1 deletion crates/torii/core/src/processors/erc721_legacy_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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 @@ -40,7 +41,7 @@ where
db: &mut Sql,
_block_number: u64,
block_timestamp: u64,
_event_id: &str,
event_id: &str,
event: &Event,
) -> Result<(), Error> {
let token_address = event.from_address;
Expand All @@ -49,6 +50,7 @@ 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 @@ -57,6 +59,7 @@ where
token_id,
world.provider(),
block_timestamp,
&transaction_hash,
)
.await?;
debug!(target: LOG_TARGET, from = ?from, to = ?to, token_id = ?token_id, "ERC721 Transfer");
Expand Down
5 changes: 4 additions & 1 deletion crates/torii/core/src/processors/erc721_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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 @@ -40,7 +41,7 @@ where
db: &mut Sql,
_block_number: u64,
block_timestamp: u64,
_event_id: &str,
event_id: &str,
event: &Event,
) -> Result<(), Error> {
let token_address = event.from_address;
Expand All @@ -49,6 +50,7 @@ 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 @@ -57,6 +59,7 @@ where
token_id,
world.provider(),
block_timestamp,
&transaction_hash,
)
.await?;
debug!(target: LOG_TARGET, from = ?from, to = ?to, token_id = ?token_id, "ERC721 Transfer");
Expand Down
10 changes: 9 additions & 1 deletion crates/torii/core/src/sql/erc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ impl Sql {
amount: U256,
provider: &P,
block_timestamp: u64,
transaction_hash: &str,
) -> Result<()> {
// contract_address
let token_id = felt_to_sql_string(&contract_address);
Expand All @@ -43,6 +44,7 @@ impl Sql {
amount,
&token_id,
block_timestamp,
transaction_hash,
)?;

if from_address != Felt::ZERO {
Expand Down Expand Up @@ -79,6 +81,7 @@ impl Sql {
token_id: U256,
provider: &P,
block_timestamp: u64,
transaction_hash: &str,
) -> Result<()> {
// contract_address:id
let token_id = felt_and_u256_to_sql_string(&contract_address, &token_id);
Expand All @@ -96,6 +99,7 @@ impl Sql {
U256::from(1u8),
&token_id,
block_timestamp,
transaction_hash,
)?;

// from_address/contract_address:id
Expand Down Expand Up @@ -307,6 +311,7 @@ impl Sql {
Ok(())
}

#[allow(clippy::too_many_arguments)]
fn store_erc_transfer_event(
&mut self,
contract_address: Felt,
Expand All @@ -315,9 +320,11 @@ impl Sql {
amount: U256,
token_id: &str,
block_timestamp: u64,
transaction_hash: &str,
) -> Result<()> {
let insert_query = "INSERT INTO erc_transfers (contract_address, from_address, \
to_address, amount, token_id, executed_at) VALUES (?, ?, ?, ?, ?, ?)";
to_address, amount, token_id, executed_at, transaction_hash) VALUES \
(?, ?, ?, ?, ?, ?, ?)";

self.executor.send(QueryMessage::other(
insert_query.to_string(),
Expand All @@ -328,6 +335,7 @@ impl Sql {
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
29 changes: 15 additions & 14 deletions crates/torii/graphql/src/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,25 +146,26 @@ lazy_static! {
]);

pub static ref ERC_BALANCE_TYPE_MAPPING: TypeMapping = IndexMap::from([
(Name::new("balance"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
(Name::new("type"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
(Name::new("tokenMetadata"), TypeData::Simple(TypeRef::named(ERC_TOKEN_TYPE_NAME))),
(Name::new("balance"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("type"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("tokenMetadata"), TypeData::Simple(TypeRef::named_nn(ERC_TOKEN_TYPE_NAME))),
]);

pub static ref ERC_TRANSFER_TYPE_MAPPING: TypeMapping = IndexMap::from([
(Name::new("from"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
(Name::new("to"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
(Name::new("amount"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
(Name::new("type"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
(Name::new("executedAt"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
(Name::new("tokenMetadata"), TypeData::Simple(TypeRef::named(ERC_TOKEN_TYPE_NAME))),
(Name::new("from"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("to"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("amount"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("type"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("executedAt"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("tokenMetadata"), TypeData::Simple(TypeRef::named_nn(ERC_TOKEN_TYPE_NAME))),
(Name::new("transactionHash"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
]);

pub static ref ERC_TOKEN_TYPE_MAPPING: TypeMapping = IndexMap::from([
(Name::new("name"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
(Name::new("symbol"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
(Name::new("tokenId"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
(Name::new("decimals"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
(Name::new("contractAddress"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
(Name::new("name"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("symbol"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("tokenId"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("decimals"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
(Name::new("contractAddress"), TypeData::Simple(TypeRef::named_nn(TypeRef::STRING))),
]);
}
6 changes: 5 additions & 1 deletion crates/torii/graphql/src/object/erc/erc_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ SELECT
t.name,
t.symbol,
t.decimals,
c.contract_type
c.contract_type,
et.transaction_hash
FROM
erc_transfers et
JOIN
Expand Down Expand Up @@ -121,6 +122,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())),
]))
}
"erc721" => {
Expand All @@ -143,6 +145,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())),
]))
}
_ => {
Expand Down Expand Up @@ -178,4 +181,5 @@ struct TransferQueryResultRaw {
pub symbol: String,
pub decimals: u8,
pub contract_type: String,
pub transaction_hash: String,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE erc_transfers ADD COLUMN transaction_hash TEXT NOT NULL;

0 comments on commit 49fd019

Please sign in to comment.