Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

torii!: change type of id in erc_transfers #2526

Merged
merged 2 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@

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 @@
MaybePendingBlockWithTxHashes::PendingBlock(block) => Ok(block.timestamp),
}
}

// event_id format: block_number:transaction_hash:event_idx
pub fn get_transaction_hash_from_event_id(event_id: &str) -> String {

Check warning on line 908 in crates/torii/core/src/engine.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/engine.rs#L908

Added line #L908 was not covered by tests
event_id.split(':').nth(1).unwrap().to_string()
}
14 changes: 11 additions & 3 deletions crates/torii/core/src/processors/erc20_legacy_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
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 @@ -50,8 +50,16 @@
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,
event_id,

Check warning on line 60 in crates/torii/core/src/processors/erc20_legacy_transfer.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/processors/erc20_legacy_transfer.rs#L60

Added line #L60 was not covered by tests
)
.await?;
debug!(target: LOG_TARGET,from = ?from, to = ?to, value = ?value, "Legacy ERC20 Transfer");

Ok(())
Expand Down
14 changes: 11 additions & 3 deletions crates/torii/core/src/processors/erc20_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
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 @@ -50,8 +50,16 @@
let value = U256Cainome::cairo_deserialize(&event.data, 0)?;
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,
event_id,

Check warning on line 60 in crates/torii/core/src/processors/erc20_transfer.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/processors/erc20_transfer.rs#L60

Added line #L60 was not covered by tests
)
.await?;
debug!(target: LOG_TARGET,from = ?from, to = ?to, value = ?value, "ERC20 Transfer");

Ok(())
Expand Down
3 changes: 2 additions & 1 deletion crates/torii/core/src/processors/erc721_legacy_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
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 @@ -57,6 +57,7 @@
token_id,
world.provider(),
block_timestamp,
event_id,

Check warning on line 60 in crates/torii/core/src/processors/erc721_legacy_transfer.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/processors/erc721_legacy_transfer.rs#L60

Added line #L60 was not covered by tests
)
.await?;
debug!(target: LOG_TARGET, from = ?from, to = ?to, token_id = ?token_id, "ERC721 Transfer");
Expand Down
3 changes: 2 additions & 1 deletion crates/torii/core/src/processors/erc721_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
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 @@ -57,6 +57,7 @@
token_id,
world.provider(),
block_timestamp,
event_id,

Check warning on line 60 in crates/torii/core/src/processors/erc721_transfer.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/processors/erc721_transfer.rs#L60

Added line #L60 was not covered by tests
)
.await?;
debug!(target: LOG_TARGET, from = ?from, to = ?to, token_id = ?token_id, "ERC721 Transfer");
Expand Down
12 changes: 10 additions & 2 deletions crates/torii/core/src/sql/erc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
amount: U256,
provider: &P,
block_timestamp: u64,
event_id: &str,

Check warning on line 28 in crates/torii/core/src/sql/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/sql/erc.rs#L28

Added line #L28 was not covered by tests
) -> Result<()> {
// contract_address
let token_id = felt_to_sql_string(&contract_address);
Expand All @@ -43,6 +44,7 @@
amount,
&token_id,
block_timestamp,
event_id,

Check warning on line 47 in crates/torii/core/src/sql/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/sql/erc.rs#L47

Added line #L47 was not covered by tests
)?;

if from_address != Felt::ZERO {
Expand Down Expand Up @@ -79,6 +81,7 @@
token_id: U256,
provider: &P,
block_timestamp: u64,
event_id: &str,

Check warning on line 84 in crates/torii/core/src/sql/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/sql/erc.rs#L84

Added line #L84 was not covered by tests
) -> Result<()> {
// contract_address:id
let token_id = felt_and_u256_to_sql_string(&contract_address, &token_id);
Expand All @@ -96,6 +99,7 @@
U256::from(1u8),
&token_id,
block_timestamp,
event_id,

Check warning on line 102 in crates/torii/core/src/sql/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/sql/erc.rs#L102

Added line #L102 was not covered by tests
)?;

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

#[allow(clippy::too_many_arguments)]
fn store_erc_transfer_event(
&mut self,
contract_address: Felt,
Expand All @@ -315,13 +320,16 @@
amount: U256,
token_id: &str,
block_timestamp: u64,
event_id: &str,

Check warning on line 323 in crates/torii/core/src/sql/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/sql/erc.rs#L323

Added line #L323 was not covered by tests
) -> Result<()> {
let insert_query = "INSERT INTO erc_transfers (contract_address, from_address, \
to_address, amount, token_id, executed_at) VALUES (?, ?, ?, ?, ?, ?)";
let insert_query = "INSERT INTO erc_transfers (id, contract_address, from_address, \
to_address, amount, token_id, executed_at) VALUES (?, ?, ?, ?, ?, ?, \
?)";

Check warning on line 327 in crates/torii/core/src/sql/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/sql/erc.rs#L325-L327

Added lines #L325 - L327 were not covered by tests

self.executor.send(QueryMessage::other(
insert_query.to_string(),
vec![
Argument::String(event_id.to_string()),

Check warning on line 332 in crates/torii/core/src/sql/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/sql/erc.rs#L332

Added line #L332 was not covered by tests
Argument::FieldElement(contract_address),
Argument::FieldElement(from),
Argument::FieldElement(to),
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: 6 additions & 0 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 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 @@
let query = format!(
r#"
SELECT
et.id,

Check warning on line 74 in crates/torii/graphql/src/object/erc/erc_transfer.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/graphql/src/object/erc/erc_transfer.rs#L74

Added line #L74 was not covered by tests
et.contract_address,
et.from_address,
et.to_address,
Expand All @@ -79,7 +81,7 @@
t.name,
t.symbol,
t.decimals,
c.contract_type

Check warning on line 84 in crates/torii/graphql/src/object/erc/erc_transfer.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/graphql/src/object/erc/erc_transfer.rs#L84

Added line #L84 was not covered by tests
FROM
erc_transfers et
JOIN
Expand All @@ -102,6 +104,7 @@

for row in rows {
let row = TransferQueryResultRaw::from_row(&row)?;
let transaction_hash = get_transaction_hash_from_event_id(&row.id);

Check warning on line 107 in crates/torii/graphql/src/object/erc/erc_transfer.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/graphql/src/object/erc/erc_transfer.rs#L107

Added line #L107 was not covered by tests

let transfer_value = match row.contract_type.to_lowercase().as_str() {
"erc20" => {
Expand All @@ -121,6 +124,7 @@
(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(transaction_hash)),

Check warning on line 127 in crates/torii/graphql/src/object/erc/erc_transfer.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/graphql/src/object/erc/erc_transfer.rs#L127

Added line #L127 was not covered by tests
]))
}
"erc721" => {
Expand All @@ -143,6 +147,7 @@
(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(transaction_hash)),

Check warning on line 150 in crates/torii/graphql/src/object/erc/erc_transfer.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/graphql/src/object/erc/erc_transfer.rs#L150

Added line #L150 was not covered by tests
]))
}
_ => {
Expand All @@ -168,6 +173,7 @@
#[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 Down
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
Loading