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

feat(torii-indexer): relation between txns and contracts #3055

Merged
merged 8 commits into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from 6 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
8 changes: 6 additions & 2 deletions crates/torii/indexer/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,8 +656,8 @@ impl<P: Provider + Send + Sync + std::fmt::Debug + 'static> Engine<P> {
.await?;
}

for contract in unique_contracts {
let entry = cursor_map.entry(contract).or_insert((transaction_hash, 0));
for contract in &unique_contracts {
let entry = cursor_map.entry(*contract).or_insert((transaction_hash, 0));
entry.1 += 1;
}

Expand All @@ -667,6 +667,7 @@ impl<P: Provider + Send + Sync + std::fmt::Debug + 'static> Engine<P> {
block_number,
block_timestamp,
transaction_hash,
&unique_contracts,
transaction,
)
.await?;
Expand Down Expand Up @@ -721,6 +722,7 @@ impl<P: Provider + Send + Sync + std::fmt::Debug + 'static> Engine<P> {
block_number,
block_timestamp,
*transaction_hash,
&unique_contracts,
&transaction_with_receipt.transaction,
)
.await?;
Expand Down Expand Up @@ -752,6 +754,7 @@ impl<P: Provider + Send + Sync + std::fmt::Debug + 'static> Engine<P> {
block_number: u64,
block_timestamp: u64,
transaction_hash: Felt,
contract_addresses: &HashSet<Felt>,
transaction: &Transaction,
) -> Result<()> {
for processor in &self.processors.transaction {
Expand All @@ -762,6 +765,7 @@ impl<P: Provider + Send + Sync + std::fmt::Debug + 'static> Engine<P> {
block_number,
block_timestamp,
transaction_hash,
contract_addresses,
transaction,
)
.await?
Expand Down
1 change: 1 addition & 0 deletions crates/torii/indexer/src/processors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ pub trait TransactionProcessor<P: Provider + Sync>: Send + Sync {
block_number: u64,
block_timestamp: u64,
transaction_hash: Felt,
contract_addresses: &HashSet<Felt>,
transaction: &Transaction,
) -> Result<(), Error>;
}
5 changes: 4 additions & 1 deletion crates/torii/indexer/src/processors/store_transaction.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashSet;

use anyhow::{Error, Ok, Result};
use async_trait::async_trait;
use starknet::core::types::{Felt, Transaction};
Expand All @@ -18,10 +20,11 @@ impl<P: Provider + Sync + std::fmt::Debug> TransactionProcessor<P> for StoreTran
block_number: u64,
block_timestamp: u64,
transaction_hash: Felt,
contract_addresses: &HashSet<Felt>,
transaction: &Transaction,
) -> Result<(), Error> {
let transaction_id = format!("{:#064x}:{:#x}", block_number, transaction_hash);
db.store_transaction(transaction, &transaction_id, block_timestamp)?;
db.store_transaction(transaction, &transaction_id, contract_addresses, block_timestamp)?;
Ok(())
}
}
10 changes: 10 additions & 0 deletions crates/torii/migrations/20250220094437_transactions_contract.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE IF NOT EXISTS transaction_contract (
transaction_hash TEXT NOT NULL,
contract_address TEXT NOT NULL,
UNIQUE (transaction_hash, contract_address),
FOREIGN KEY (transaction_hash) REFERENCES transactions(id),
FOREIGN KEY (contract_address) REFERENCES contracts(id)
);

CREATE INDEX IF NOT EXISTS idx_transaction_contract_transaction_hash ON transaction_contract (transaction_hash);
CREATE INDEX IF NOT EXISTS idx_transaction_contract_contract_address ON transaction_contract (contract_address);
16 changes: 13 additions & 3 deletions crates/torii/sqlite/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::convert::TryInto;
use std::str::FromStr;
use std::sync::Arc;
Expand Down Expand Up @@ -515,6 +515,7 @@ impl Sql {
&mut self,
transaction: &Transaction,
transaction_id: &str,
contract_addresses: &HashSet<Felt>,
block_timestamp: u64,
) -> Result<()> {
let id = Argument::String(transaction_id.to_string());
Expand Down Expand Up @@ -560,8 +561,8 @@ impl Sql {
?, ?, ?)"
.to_string(),
vec![
id,
transaction_hash,
id.clone(),
transaction_hash.clone(),
sender_address,
calldata,
max_fee,
Expand All @@ -572,6 +573,15 @@ impl Sql {
],
))?;

for contract_address in contract_addresses {
self.executor.send(QueryMessage::other(
"INSERT OR IGNORE INTO transaction_contract (transaction_hash, contract_address) \
VALUES (?, ?)"
.to_string(),
vec![id.clone(), Argument::FieldElement(*contract_address)],
))?;
}

Ok(())
}

Expand Down
Loading