refactor: remove unnecessary clones in UniswapV3 module (#177)

Also fixes a bug that would lead to emitting wrong balances if there is a `CollectProtocol` event triggered (currently not affecting because it's not enabled)

Co-authored-by: zizou <111426680+flopell@users.noreply.github.com>
This commit is contained in:
Zizou
2025-03-13 15:24:48 +01:00
committed by GitHub
parent d49b3f59cb
commit 503a83595e
9 changed files with 56 additions and 42 deletions

2
substreams/Cargo.lock generated
View File

@@ -384,7 +384,7 @@ dependencies = [
[[package]]
name = "ethereum-uniswap-v3-logs-only"
version = "0.1.0"
version = "0.1.1"
dependencies = [
"anyhow",
"ethabi 18.0.0",

View File

@@ -1,6 +1,6 @@
[package]
name = "ethereum-uniswap-v3-logs-only"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
[lib]

View File

@@ -1,7 +1,7 @@
specVersion: v0.1.0
package:
name: "base_uniswap_v3_logs_only"
version: v0.1.0
version: v0.1.1
protobuf:
files:

View File

@@ -1,7 +1,7 @@
specVersion: v0.1.0
package:
name: "ethereum_uniswap_v3_logs_only"
version: v0.1.0
version: v0.1.1
protobuf:
files:

View File

@@ -32,15 +32,19 @@ pub fn map_events(
.into_iter()
.filter(|tx| tx.status == 1)
.flat_map(|tx| {
tx.clone()
let receipt = tx
.receipt
.into_iter()
.flat_map(|receipt| receipt.logs)
.as_ref()
.expect("all transaction traces have a receipt");
receipt
.logs
.iter()
.filter_map(|log| {
let key = format!("{}:{}", "Pool", log.address.to_hex());
// Skip if the log is not from a known uniswapV3 pool.
if let Some(pool) = pools_store.get_last(key) {
log_to_event(&log, pool, tx.clone())
log_to_event(log, pool, &tx)
} else {
None
}
@@ -54,7 +58,7 @@ pub fn map_events(
Ok(Events { pool_events })
}
fn log_to_event(event: &Log, pool: Pool, tx: TransactionTrace) -> Option<PoolEvent> {
fn log_to_event(event: &Log, pool: Pool, tx: &TransactionTrace) -> Option<PoolEvent> {
if let Some(init) = Initialize::match_and_decode(event) {
Some(PoolEvent {
log_ordinal: event.ordinal,

View File

@@ -35,7 +35,7 @@ fn event_to_balance_deltas(event: PoolEvent) -> Vec<BalanceDelta> {
match event.r#type.unwrap() {
pool_event::Type::Mint(e) => vec![
BalanceDelta {
token: hex::decode(event.token0.clone()).unwrap(),
token: hex::decode(event.token0).unwrap(),
delta: BigInt::from_str(&e.amount_0)
.unwrap()
.to_signed_bytes_be(),
@@ -43,11 +43,11 @@ fn event_to_balance_deltas(event: PoolEvent) -> Vec<BalanceDelta> {
ord: event.log_ordinal,
tx: event
.transaction
.clone()
.as_ref()
.map(Into::into),
},
BalanceDelta {
token: hex::decode(event.token1.clone()).unwrap(),
token: hex::decode(event.token1).unwrap(),
delta: BigInt::from_str(&e.amount_1)
.unwrap()
.to_signed_bytes_be(),
@@ -58,7 +58,7 @@ fn event_to_balance_deltas(event: PoolEvent) -> Vec<BalanceDelta> {
],
pool_event::Type::Collect(e) => vec![
BalanceDelta {
token: hex::decode(event.token0.clone()).unwrap(),
token: hex::decode(event.token0).unwrap(),
delta: BigInt::from_str(&e.amount_0)
.unwrap()
.neg()
@@ -67,11 +67,11 @@ fn event_to_balance_deltas(event: PoolEvent) -> Vec<BalanceDelta> {
ord: event.log_ordinal,
tx: event
.transaction
.clone()
.as_ref()
.map(Into::into),
},
BalanceDelta {
token: hex::decode(event.token1.clone()).unwrap(),
token: hex::decode(event.token1).unwrap(),
delta: BigInt::from_str(&e.amount_1)
.unwrap()
.neg()
@@ -86,7 +86,7 @@ fn event_to_balance_deltas(event: PoolEvent) -> Vec<BalanceDelta> {
pool_event::Type::Swap(e) => {
vec![
BalanceDelta {
token: hex::decode(event.token0.clone()).unwrap(),
token: hex::decode(event.token0).unwrap(),
delta: BigInt::from_str(&e.amount_0)
.unwrap()
.to_signed_bytes_be(),
@@ -94,15 +94,15 @@ fn event_to_balance_deltas(event: PoolEvent) -> Vec<BalanceDelta> {
ord: event.log_ordinal,
tx: event
.transaction
.clone()
.as_ref()
.map(Into::into),
},
BalanceDelta {
token: hex::decode(event.token1.clone()).unwrap(),
token: hex::decode(event.token1).unwrap(),
delta: BigInt::from_str(&e.amount_1)
.unwrap()
.to_signed_bytes_be(),
component_id: address.clone(),
component_id: address,
ord: event.log_ordinal,
tx: event.transaction.map(Into::into),
},
@@ -113,20 +113,18 @@ fn event_to_balance_deltas(event: PoolEvent) -> Vec<BalanceDelta> {
token: hex::decode(event.token0).unwrap(),
delta: BigInt::from_str(&e.paid_0)
.unwrap()
.clone()
.to_signed_bytes_be(),
component_id: address.clone(),
ord: event.log_ordinal,
tx: event
.transaction
.clone()
.as_ref()
.map(Into::into),
},
BalanceDelta {
token: hex::decode(event.token1).unwrap(),
delta: BigInt::from_str(&e.paid_1)
.unwrap()
.clone()
.to_signed_bytes_be(),
component_id: address,
ord: event.log_ordinal,
@@ -140,31 +138,21 @@ fn event_to_balance_deltas(event: PoolEvent) -> Vec<BalanceDelta> {
delta: BigInt::from_str(&e.amount_0)
.unwrap()
.neg()
.clone()
.to_signed_bytes_be(),
component_id: event
.pool_address
.clone()
.as_bytes()
.to_vec(),
component_id: address.clone(),
ord: event.log_ordinal,
tx: event
.transaction
.clone()
.as_ref()
.map(Into::into),
},
BalanceDelta {
token: hex::decode(event.token1).unwrap(),
delta: BigInt::from_str(&e.amount_1)
.unwrap()
.clone()
.neg()
.to_signed_bytes_be(),
component_id: event
.pool_address
.clone()
.as_bytes()
.to_vec(),
component_id: address,
ord: event.log_ordinal,
tx: event.transaction.map(Into::into),
},

View File

@@ -27,7 +27,7 @@ pub fn map_ticks_changes(events: Events) -> Result<TickDeltas, anyhow::Error> {
#[substreams::handlers::store]
pub fn store_ticks_liquidity(ticks_deltas: TickDeltas, store: StoreAddBigInt) {
let mut deltas = ticks_deltas.deltas.clone();
let mut deltas = ticks_deltas.deltas;
deltas.sort_unstable_by_key(|delta| delta.ordinal);

View File

@@ -170,10 +170,10 @@ fn event_to_attributes_updates(event: PoolEvent) -> Vec<(Transaction, PoolAddres
(
event
.transaction
.clone()
.as_ref()
.unwrap()
.into(),
hex::decode(event.pool_address.clone()).unwrap(),
hex::decode(&event.pool_address).unwrap(),
Attribute {
name: "sqrt_price_x96".to_string(),
value: BigInt::from_str(&initalize.sqrt_price)
@@ -197,10 +197,10 @@ fn event_to_attributes_updates(event: PoolEvent) -> Vec<(Transaction, PoolAddres
(
event
.transaction
.clone()
.as_ref()
.unwrap()
.into(),
hex::decode(event.pool_address.clone()).unwrap(),
hex::decode(&event.pool_address).unwrap(),
Attribute {
name: "sqrt_price_x96".to_string(),
value: BigInt::from_str(&swap.sqrt_price)
@@ -223,10 +223,10 @@ fn event_to_attributes_updates(event: PoolEvent) -> Vec<(Transaction, PoolAddres
(
event
.transaction
.clone()
.as_ref()
.unwrap()
.into(),
hex::decode(event.pool_address.clone()).unwrap(),
hex::decode(&event.pool_address).unwrap(),
Attribute {
name: "protocol_fees/token0".to_string(),
value: BigInt::from(sfp.fee_protocol_0_new).to_signed_bytes_be(),

View File

@@ -32,6 +32,28 @@ impl From<TransactionTrace> for Transaction {
}
}
impl From<&TransactionTrace> for Transaction {
fn from(value: &TransactionTrace) -> Self {
Self {
hash: value.hash.clone(),
from: value.from.clone(),
to: value.to.clone(),
index: value.index.into(),
}
}
}
impl From<&Transaction> for tycho_substreams::prelude::Transaction {
fn from(value: &Transaction) -> Self {
Self {
hash: value.hash.clone(),
from: value.from.clone(),
to: value.to.clone(),
index: value.index,
}
}
}
impl From<Transaction> for tycho_substreams::prelude::Transaction {
fn from(value: Transaction) -> Self {
Self { hash: value.hash, from: value.from, to: value.to, index: value.index }