Skip to content

Commit

Permalink
fix(torii): add erc20 patch for eth mainnet token
Browse files Browse the repository at this point in the history
commit-id:93263be6
  • Loading branch information
lambda-0x committed Nov 28, 2024
1 parent d1c7629 commit 6d9fbee
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 16 deletions.
14 changes: 13 additions & 1 deletion crates/torii/core/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,17 @@ pub enum FetchDataResult {
None,
}

impl FetchDataResult {
pub fn block_id(&self) -> BlockId {
match self {
FetchDataResult::Range(range) => BlockId::Number(range.latest_block_number),
FetchDataResult::Pending(_pending) => BlockId::Tag(BlockTag::Pending),

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

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/engine.rs#L173-L176

Added lines #L173 - L176 were not covered by tests
// we dont require block_id when result is none, so this is a dummy value
FetchDataResult::None => BlockId::Number(0),

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L178 was not covered by tests
}
}

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L180 was not covered by tests
}

#[derive(Debug)]
pub struct FetchRangeResult {
// (block_number, transaction_hash) -> events
Expand Down Expand Up @@ -263,10 +274,11 @@ impl<P: Provider + Send + Sync + std::fmt::Debug + 'static> Engine<P> {
info!(target: LOG_TARGET, "Syncing reestablished.");
}

let block_id = fetch_result.block_id();
match self.process(fetch_result).await {
Ok(_) => {
self.db.flush().await?;
self.db.apply_cache_diff().await?;
self.db.apply_cache_diff(block_id).await?;
self.db.execute().await?;
},
Err(e) => {
Expand Down
41 changes: 38 additions & 3 deletions crates/torii/core/src/executor/erc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ use starknet::core::types::{BlockId, BlockTag, FunctionCall, U256};
use starknet::core::utils::{get_selector_from_name, parse_cairo_short_string};
use starknet::providers::Provider;
use starknet_crypto::Felt;
use tracing::{debug, error, trace};
use tracing::{debug, error, trace, warn};

use super::{ApplyBalanceDiffQuery, Executor};
use crate::constants::{IPFS_CLIENT_MAX_RETRY, SQL_FELT_DELIMITER, TOKEN_BALANCE_TABLE};
use crate::executor::LOG_TARGET;
use crate::sql::utils::{felt_to_sql_string, sql_string_to_u256, u256_to_sql_string, I256};
use crate::types::ContractType;
use crate::utils::fetch_content_from_ipfs;
Expand Down Expand Up @@ -46,6 +47,7 @@ impl<'c, P: Provider + Sync + Send + 'static> Executor<'c, P> {
pub async fn apply_balance_diff(
&mut self,
apply_balance_diff: ApplyBalanceDiffQuery,
provider: Arc<P>,

Check warning on line 50 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L50

Added line #L50 was not covered by tests
) -> Result<()> {
let erc_cache = apply_balance_diff.erc_cache;
for ((contract_type, id_str), balance) in erc_cache.iter() {
Expand All @@ -66,6 +68,8 @@ impl<'c, P: Provider + Sync + Send + 'static> Executor<'c, P> {
contract_address,
token_id,
balance,
Arc::clone(&provider),
apply_balance_diff.block_id,

Check warning on line 72 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L71-L72

Added lines #L71 - L72 were not covered by tests
)
.await
.with_context(|| "Failed to apply balance diff in apply_cache_diff")?;
Expand All @@ -83,6 +87,8 @@ impl<'c, P: Provider + Sync + Send + 'static> Executor<'c, P> {
contract_address,
token_id,
balance,
Arc::clone(&provider),
apply_balance_diff.block_id,

Check warning on line 91 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L90-L91

Added lines #L90 - L91 were not covered by tests
)
.await
.with_context(|| "Failed to apply balance diff in apply_cache_diff")?;
Expand All @@ -93,13 +99,16 @@ impl<'c, P: Provider + Sync + Send + 'static> Executor<'c, P> {
Ok(())
}

#[allow(clippy::too_many_arguments)]
pub async fn apply_balance_diff_helper(
&mut self,
id: &str,
account_address: &str,
contract_address: &str,
token_id: &str,
balance_diff: &I256,
provider: Arc<P>,
block_id: BlockId,

Check warning on line 111 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L110-L111

Added lines #L110 - L111 were not covered by tests
) -> Result<()> {
let tx = &mut self.transaction;
let balance: Option<(String,)> =
Expand All @@ -116,9 +125,35 @@ impl<'c, P: Provider + Sync + Send + 'static> Executor<'c, P> {

if balance_diff.is_negative {
if balance < balance_diff.value {
dbg!(&balance_diff, balance, id);
// HACK: ideally we should never hit this case. But ETH on starknet mainnet didn't
// emit transfer events properly so they are broken. For those cases
// we manually fetch the balance of the address using RPC

let current_balance = provider
.call(
FunctionCall {
contract_address: Felt::from_str(contract_address).unwrap(),
entry_point_selector: get_selector_from_name("balanceOf").unwrap(),
calldata: vec![Felt::from_str(account_address).unwrap()],
},
block_id,
)
.await
.with_context(|| format!("Failed to fetch balance for id: {}", id))?;

Check warning on line 142 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L132-L142

Added lines #L132 - L142 were not covered by tests

let current_balance =
cainome::cairo_serde::U256::cairo_deserialize(&current_balance, 0).unwrap();

warn!(

Check warning on line 147 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L144-L147

Added lines #L144 - L147 were not covered by tests
target: LOG_TARGET,
id = id,
"Invalid transfer event detected, overriding balance by querying RPC directly"

Check warning on line 150 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L150

Added line #L150 was not covered by tests
);
// override the balance from onchain data
balance = U256::from_words(current_balance.low, current_balance.high);
} else {
balance -= balance_diff.value;

Check warning on line 155 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L153-L155

Added lines #L153 - L155 were not covered by tests
}
balance -= balance_diff.value;
} else {
balance += balance_diff.value;
}
Expand Down
5 changes: 4 additions & 1 deletion crates/torii/core/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub struct DeleteEntityQuery {
#[derive(Debug, Clone)]
pub struct ApplyBalanceDiffQuery {
pub erc_cache: HashMap<(ContractType, String), I256>,
pub block_id: BlockId,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -613,7 +614,7 @@ impl<'c, P: Provider + Sync + Send + 'static> Executor<'c, P> {
QueryType::ApplyBalanceDiff(apply_balance_diff) => {
debug!(target: LOG_TARGET, "Applying balance diff.");
let instant = Instant::now();
self.apply_balance_diff(apply_balance_diff).await?;
self.apply_balance_diff(apply_balance_diff, self.provider.clone()).await?;

Check warning on line 617 in crates/torii/core/src/executor/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/mod.rs#L617

Added line #L617 was not covered by tests
debug!(target: LOG_TARGET, duration = ?instant.elapsed(), "Applied balance diff.");
}
QueryType::RegisterErc721Token(register_erc721_token) => {
Expand Down Expand Up @@ -685,6 +686,8 @@ impl<'c, P: Provider + Sync + Send + 'static> Executor<'c, P> {

self.register_tasks.spawn(async move {
let permit = semaphore.acquire().await.unwrap();
let span = tracing::span!(tracing::Level::INFO, "contract_address_span", contract_address = %register_erc721_token.contract_address);
let _enter = span.enter();

Check warning on line 690 in crates/torii/core/src/executor/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/mod.rs#L689-L690

Added lines #L689 - L690 were not covered by tests

let result = Self::process_register_erc721_token_query(
register_erc721_token,
Expand Down
3 changes: 2 additions & 1 deletion crates/torii/core/src/processors/erc20_legacy_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ where
&self,
world: &WorldContractReader<P>,
db: &mut Sql,
_block_number: u64,
block_number: u64,
block_timestamp: u64,
event_id: &str,
event: &Event,
Expand All @@ -59,6 +59,7 @@ where
world.provider(),
block_timestamp,
event_id,
block_number,

Check warning on line 62 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#L62

Added line #L62 was not covered by tests
)
.await?;
debug!(target: LOG_TARGET,from = ?from, to = ?to, value = ?value, "Legacy ERC20 Transfer");
Expand Down
3 changes: 2 additions & 1 deletion crates/torii/core/src/processors/erc20_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ where
&self,
world: &WorldContractReader<P>,
db: &mut Sql,
_block_number: u64,
block_number: u64,
block_timestamp: u64,
event_id: &str,
event: &Event,
Expand All @@ -59,6 +59,7 @@ where
world.provider(),
block_timestamp,
event_id,
block_number,

Check warning on line 62 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#L62

Added line #L62 was not covered by tests
)
.await?;
debug!(target: LOG_TARGET,from = ?from, to = ?to, value = ?value, "ERC20 Transfer");
Expand Down
14 changes: 11 additions & 3 deletions crates/torii/core/src/processors/erc721_legacy_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ where
&self,
_world: &WorldContractReader<P>,
db: &mut Sql,
_block_number: u64,
block_number: u64,
block_timestamp: u64,
event_id: &str,
event: &Event,
Expand All @@ -51,8 +51,16 @@ where
let token_id = U256Cainome::cairo_deserialize(&event.data, 2)?;
let token_id = U256::from_words(token_id.low, token_id.high);

db.handle_erc721_transfer(token_address, from, to, token_id, block_timestamp, event_id)
.await?;
db.handle_erc721_transfer(
token_address,
from,
to,
token_id,
block_timestamp,
event_id,
block_number,
)
.await?;

Check warning on line 63 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#L54-L63

Added lines #L54 - L63 were not covered by tests
debug!(target: LOG_TARGET, from = ?from, to = ?to, token_id = ?token_id, "ERC721 Transfer");

Ok(())
Expand Down
14 changes: 11 additions & 3 deletions crates/torii/core/src/processors/erc721_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ where
&self,
_world: &WorldContractReader<P>,
db: &mut Sql,
_block_number: u64,
block_number: u64,
block_timestamp: u64,
event_id: &str,
event: &Event,
Expand All @@ -51,8 +51,16 @@ where
let token_id = U256Cainome::cairo_deserialize(&event.keys, 3)?;
let token_id = U256::from_words(token_id.low, token_id.high);

db.handle_erc721_transfer(token_address, from, to, token_id, block_timestamp, event_id)
.await?;
db.handle_erc721_transfer(
token_address,
from,
to,
token_id,
block_timestamp,
event_id,
block_number,
)
.await?;

Check warning on line 63 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#L54-L63

Added lines #L54 - L63 were not covered by tests
debug!(target: LOG_TARGET, from = ?from, to = ?to, token_id = ?token_id, "ERC721 Transfer");

Ok(())
Expand Down
11 changes: 8 additions & 3 deletions crates/torii/core/src/sql/erc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ impl Sql {
provider: &P,
block_timestamp: u64,
event_id: &str,
block_number: u64,

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L32 was not covered by tests
) -> Result<()> {
// contract_address
let token_id = felt_to_sql_string(&contract_address);
Expand Down Expand Up @@ -66,10 +67,11 @@ impl Sql {
self.local_cache.erc_cache.entry((ContractType::ERC20, to_balance_id)).or_default();
*to_balance += I256::from(amount);
}
let block_id = BlockId::Number(block_number);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L70 was not covered by tests

if self.local_cache.erc_cache.len() >= 100000 {
self.flush().await.with_context(|| "Failed to flush in handle_erc20_transfer")?;
self.apply_cache_diff().await?;
self.apply_cache_diff(block_id).await?;

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L74 was not covered by tests
}

Ok(())
Expand All @@ -84,6 +86,7 @@ impl Sql {
token_id: U256,
block_timestamp: u64,
event_id: &str,
block_number: u64,

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L89 was not covered by tests
) -> Result<()> {
// contract_address:id
let actual_token_id = token_id;
Expand Down Expand Up @@ -127,10 +130,11 @@ impl Sql {
.or_default();
*to_balance += I256::from(1u8);
}
let block_id = BlockId::Number(block_number);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L133 was not covered by tests

if self.local_cache.erc_cache.len() >= 100000 {
self.flush().await.with_context(|| "Failed to flush in handle_erc721_transfer")?;
self.apply_cache_diff().await?;
self.apply_cache_diff(block_id).await?;

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L137 was not covered by tests
}

Ok(())
Expand Down Expand Up @@ -272,7 +276,7 @@ impl Sql {
Ok(())
}

pub async fn apply_cache_diff(&mut self) -> Result<()> {
pub async fn apply_cache_diff(&mut self, block_id: BlockId) -> Result<()> {

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L279 was not covered by tests
if !self.local_cache.erc_cache.is_empty() {
self.executor.send(QueryMessage::new(
"".to_string(),
Expand All @@ -282,6 +286,7 @@ impl Sql {
&mut self.local_cache.erc_cache,
HashMap::with_capacity(64),
),
block_id,

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L289 was not covered by tests
}),
))?;
}
Expand Down

0 comments on commit 6d9fbee

Please sign in to comment.