refactor(uniswap_v4): remove costly clone calls (#169)

* Removed costly `TransactionTrace` clones calls and `.clone()` at a few other places

* style: formatting

* chore: bump uniswap v4 versions to 0.2.1

* fix: fix double referencing

---------

Co-authored-by: Matthieu Vachon <matt@streamingfast.io>
This commit is contained in:
Louise Poole
2025-03-04 16:51:13 +02:00
committed by GitHub
parent 04d14db719
commit f13c1c263b
9 changed files with 55 additions and 58 deletions

View File

@@ -1,7 +1,7 @@
specVersion: v0.1.0 specVersion: v0.1.0
package: package:
name: "base_uniswap_v4" name: "base_uniswap_v4"
version: v0.2.0 version: v0.2.1
protobuf: protobuf:
files: files:

View File

@@ -1,7 +1,7 @@
specVersion: v0.1.0 specVersion: v0.1.0
package: package:
name: "ethereum_uniswap_v4" name: "ethereum_uniswap_v4"
version: v0.2.0 version: v0.2.1
protobuf: protobuf:
files: files:

View File

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

View File

@@ -23,11 +23,15 @@ pub fn map_events(
.into_iter() .into_iter()
.filter(|tx| tx.status == 1) .filter(|tx| tx.status == 1)
.flat_map(|tx| { .flat_map(|tx| {
tx.clone() let receipt = tx
.receipt .receipt
.into_iter() .as_ref()
.flat_map(|receipt| receipt.logs) .expect("all transaction traces have a receipt");
.filter_map(|log| log_to_event(&log, tx.clone(), &pools_store))
receipt
.logs
.iter()
.filter_map(|log| log_to_event(log, &tx, &pools_store))
.collect::<Vec<_>>() .collect::<Vec<_>>()
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@@ -39,7 +43,7 @@ pub fn map_events(
fn log_to_event( fn log_to_event(
event: &Log, event: &Log,
tx: TransactionTrace, tx: &TransactionTrace,
pools_store: &StoreGetProto<Pool>, pools_store: &StoreGetProto<Pool>,
) -> Option<PoolEvent> { ) -> Option<PoolEvent> {
if let Some(init) = Initialize::match_and_decode(event) { if let Some(init) = Initialize::match_and_decode(event) {

View File

@@ -58,33 +58,24 @@ fn event_to_balance_deltas(
get_amount_delta(current_sqrt_price, e.tick_lower, e.tick_upper, e.liquidity_delta); get_amount_delta(current_sqrt_price, e.tick_lower, e.tick_upper, e.liquidity_delta);
Some(vec![ Some(vec![
BalanceDelta { BalanceDelta {
token: hex::decode( token: hex::decode(event.currency0.trim_start_matches("0x")).unwrap(),
event
.currency0
.clone()
.trim_start_matches("0x"),
)
.unwrap(),
delta: delta0.to_signed_bytes_be(), delta: delta0.to_signed_bytes_be(),
component_id: address.clone(), component_id: address.clone(),
ord: event.log_ordinal, ord: event.log_ordinal,
tx: event tx: event
.transaction .transaction
.clone() .as_ref()
.map(Into::into), .map(Into::into),
}, },
BalanceDelta { BalanceDelta {
token: hex::decode( token: hex::decode(event.currency1.trim_start_matches("0x")).unwrap(),
event
.currency1
.clone()
.trim_start_matches("0x"),
)
.unwrap(),
delta: delta1.to_signed_bytes_be(), delta: delta1.to_signed_bytes_be(),
component_id: address, component_id: address,
ord: event.log_ordinal, ord: event.log_ordinal,
tx: event.transaction.map(Into::into), tx: event
.transaction
.as_ref()
.map(Into::into),
}, },
]) ])
} }
@@ -123,33 +114,24 @@ fn event_to_balance_deltas(
Some(vec![ Some(vec![
BalanceDelta { BalanceDelta {
token: hex::decode( token: hex::decode(event.currency0.trim_start_matches("0x")).unwrap(),
event
.currency0
.clone()
.trim_start_matches("0x"),
)
.unwrap(),
delta: delta0.to_signed_bytes_be(), delta: delta0.to_signed_bytes_be(),
component_id: address.clone(), component_id: address.clone(),
ord: event.log_ordinal, ord: event.log_ordinal,
tx: event tx: event
.transaction .transaction
.clone() .as_ref()
.map(Into::into), .map(Into::into),
}, },
BalanceDelta { BalanceDelta {
token: hex::decode( token: hex::decode(event.currency1.trim_start_matches("0x")).unwrap(),
event
.currency1
.clone()
.trim_start_matches("0x"),
)
.unwrap(),
delta: delta1.to_signed_bytes_be(), delta: delta1.to_signed_bytes_be(),
component_id: address.clone(), component_id: address,
ord: event.log_ordinal, ord: event.log_ordinal,
tx: event.transaction.map(Into::into), tx: event
.transaction
.as_ref()
.map(Into::into),
}, },
]) ])
} }

View File

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

View File

@@ -169,16 +169,10 @@ fn event_to_attributes_updates(event: PoolEvent) -> Vec<(Transaction, PoolAddres
( (
event event
.transaction .transaction
.clone() .as_ref()
.unwrap() .unwrap()
.into(), .into(),
hex::decode( hex::decode(event.pool_id.trim_start_matches("0x")).unwrap(),
event
.pool_id
.clone()
.trim_start_matches("0x"),
)
.unwrap(),
Attribute { Attribute {
name: "sqrt_price_x96".to_string(), name: "sqrt_price_x96".to_string(),
value: BigInt::from_str(&swap.sqrt_price_x96) value: BigInt::from_str(&swap.sqrt_price_x96)
@@ -208,16 +202,10 @@ fn event_to_attributes_updates(event: PoolEvent) -> Vec<(Transaction, PoolAddres
( (
event event
.transaction .transaction
.clone() .as_ref()
.unwrap() .unwrap()
.into(), .into(),
hex::decode( hex::decode(event.pool_id.trim_start_matches("0x")).unwrap(),
event
.pool_id
.clone()
.trim_start_matches("0x"),
)
.unwrap(),
Attribute { Attribute {
name: "protocol_fees/zero2one".to_string(), name: "protocol_fees/zero2one".to_string(),
value: BigInt::from(lower_12_bits).to_signed_bytes_be(), value: BigInt::from(lower_12_bits).to_signed_bytes_be(),

View File

@@ -36,8 +36,30 @@ 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 { impl From<Transaction> for tycho_substreams::prelude::Transaction {
fn from(value: Transaction) -> Self { fn from(value: Transaction) -> Self {
Self { hash: value.hash, from: value.from, to: value.to, index: value.index } Self { hash: value.hash, from: value.from, to: value.to, index: value.index }
} }
} }
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,
}
}
}

View File

@@ -1,5 +1,6 @@
use num_bigint::BigInt; use num_bigint::BigInt;
use std::ops::Shr; use std::ops::Shr;
/// Calculates the amounts of token0 and token1 for a given position /// Calculates the amounts of token0 and token1 for a given position
/// ///
/// Source: https://github.com/Uniswap/v4-core/blob/main/src/libraries/Pool.sol /// Source: https://github.com/Uniswap/v4-core/blob/main/src/libraries/Pool.sol