From f13c1c263b72cbc8a44af76bb2f771f5145b8af2 Mon Sep 17 00:00:00 2001 From: Louise Poole Date: Tue, 4 Mar 2025 16:51:13 +0200 Subject: [PATCH] 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 --- .../ethereum-uniswap-v4/base-uniswap-v4.yaml | 2 +- .../ethereum-uniswap-v4.yaml | 2 +- .../sepolia-uniswap-v4.yaml | 2 +- .../src/modules/3_map_events.rs | 14 ++++-- .../modules/5_map_store_balance_changes.rs | 48 ++++++------------- .../src/modules/5_map_store_ticks.rs | 2 +- .../src/modules/6_map_protocol_changes.rs | 20 ++------ .../ethereum-uniswap-v4/src/modules/mod.rs | 22 +++++++++ .../src/modules/uni_math.rs | 1 + 9 files changed, 55 insertions(+), 58 deletions(-) diff --git a/substreams/ethereum-uniswap-v4/base-uniswap-v4.yaml b/substreams/ethereum-uniswap-v4/base-uniswap-v4.yaml index 00436c8..b3dea69 100644 --- a/substreams/ethereum-uniswap-v4/base-uniswap-v4.yaml +++ b/substreams/ethereum-uniswap-v4/base-uniswap-v4.yaml @@ -1,7 +1,7 @@ specVersion: v0.1.0 package: name: "base_uniswap_v4" - version: v0.2.0 + version: v0.2.1 protobuf: files: diff --git a/substreams/ethereum-uniswap-v4/ethereum-uniswap-v4.yaml b/substreams/ethereum-uniswap-v4/ethereum-uniswap-v4.yaml index b13982e..1b12917 100644 --- a/substreams/ethereum-uniswap-v4/ethereum-uniswap-v4.yaml +++ b/substreams/ethereum-uniswap-v4/ethereum-uniswap-v4.yaml @@ -1,7 +1,7 @@ specVersion: v0.1.0 package: name: "ethereum_uniswap_v4" - version: v0.2.0 + version: v0.2.1 protobuf: files: diff --git a/substreams/ethereum-uniswap-v4/sepolia-uniswap-v4.yaml b/substreams/ethereum-uniswap-v4/sepolia-uniswap-v4.yaml index 29dd0c4..8ca1e37 100644 --- a/substreams/ethereum-uniswap-v4/sepolia-uniswap-v4.yaml +++ b/substreams/ethereum-uniswap-v4/sepolia-uniswap-v4.yaml @@ -1,7 +1,7 @@ specVersion: v0.1.0 package: name: "ethereum_uniswap_v4" - version: v0.1.1 + version: v0.2.1 protobuf: files: diff --git a/substreams/ethereum-uniswap-v4/src/modules/3_map_events.rs b/substreams/ethereum-uniswap-v4/src/modules/3_map_events.rs index 6a7e0ad..63c5b02 100644 --- a/substreams/ethereum-uniswap-v4/src/modules/3_map_events.rs +++ b/substreams/ethereum-uniswap-v4/src/modules/3_map_events.rs @@ -23,11 +23,15 @@ 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) - .filter_map(|log| log_to_event(&log, tx.clone(), &pools_store)) + .as_ref() + .expect("all transaction traces have a receipt"); + + receipt + .logs + .iter() + .filter_map(|log| log_to_event(log, &tx, &pools_store)) .collect::>() }) .collect::>(); @@ -39,7 +43,7 @@ pub fn map_events( fn log_to_event( event: &Log, - tx: TransactionTrace, + tx: &TransactionTrace, pools_store: &StoreGetProto, ) -> Option { if let Some(init) = Initialize::match_and_decode(event) { diff --git a/substreams/ethereum-uniswap-v4/src/modules/5_map_store_balance_changes.rs b/substreams/ethereum-uniswap-v4/src/modules/5_map_store_balance_changes.rs index 1e2e497..dee15a3 100644 --- a/substreams/ethereum-uniswap-v4/src/modules/5_map_store_balance_changes.rs +++ b/substreams/ethereum-uniswap-v4/src/modules/5_map_store_balance_changes.rs @@ -58,33 +58,24 @@ fn event_to_balance_deltas( get_amount_delta(current_sqrt_price, e.tick_lower, e.tick_upper, e.liquidity_delta); Some(vec![ BalanceDelta { - token: hex::decode( - event - .currency0 - .clone() - .trim_start_matches("0x"), - ) - .unwrap(), + token: hex::decode(event.currency0.trim_start_matches("0x")).unwrap(), delta: delta0.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 - .currency1 - .clone() - .trim_start_matches("0x"), - ) - .unwrap(), + token: hex::decode(event.currency1.trim_start_matches("0x")).unwrap(), delta: delta1.to_signed_bytes_be(), component_id: address, 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![ BalanceDelta { - token: hex::decode( - event - .currency0 - .clone() - .trim_start_matches("0x"), - ) - .unwrap(), + token: hex::decode(event.currency0.trim_start_matches("0x")).unwrap(), delta: delta0.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 - .currency1 - .clone() - .trim_start_matches("0x"), - ) - .unwrap(), + token: hex::decode(event.currency1.trim_start_matches("0x")).unwrap(), delta: delta1.to_signed_bytes_be(), - component_id: address.clone(), + component_id: address, ord: event.log_ordinal, - tx: event.transaction.map(Into::into), + tx: event + .transaction + .as_ref() + .map(Into::into), }, ]) } diff --git a/substreams/ethereum-uniswap-v4/src/modules/5_map_store_ticks.rs b/substreams/ethereum-uniswap-v4/src/modules/5_map_store_ticks.rs index f60a09e..7dda47b 100644 --- a/substreams/ethereum-uniswap-v4/src/modules/5_map_store_ticks.rs +++ b/substreams/ethereum-uniswap-v4/src/modules/5_map_store_ticks.rs @@ -28,7 +28,7 @@ pub fn map_ticks_changes(events: Events) -> Result { #[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); diff --git a/substreams/ethereum-uniswap-v4/src/modules/6_map_protocol_changes.rs b/substreams/ethereum-uniswap-v4/src/modules/6_map_protocol_changes.rs index b0b9c2c..eccc1b6 100644 --- a/substreams/ethereum-uniswap-v4/src/modules/6_map_protocol_changes.rs +++ b/substreams/ethereum-uniswap-v4/src/modules/6_map_protocol_changes.rs @@ -169,16 +169,10 @@ fn event_to_attributes_updates(event: PoolEvent) -> Vec<(Transaction, PoolAddres ( event .transaction - .clone() + .as_ref() .unwrap() .into(), - hex::decode( - event - .pool_id - .clone() - .trim_start_matches("0x"), - ) - .unwrap(), + hex::decode(event.pool_id.trim_start_matches("0x")).unwrap(), Attribute { name: "sqrt_price_x96".to_string(), value: BigInt::from_str(&swap.sqrt_price_x96) @@ -208,16 +202,10 @@ fn event_to_attributes_updates(event: PoolEvent) -> Vec<(Transaction, PoolAddres ( event .transaction - .clone() + .as_ref() .unwrap() .into(), - hex::decode( - event - .pool_id - .clone() - .trim_start_matches("0x"), - ) - .unwrap(), + hex::decode(event.pool_id.trim_start_matches("0x")).unwrap(), Attribute { name: "protocol_fees/zero2one".to_string(), value: BigInt::from(lower_12_bits).to_signed_bytes_be(), diff --git a/substreams/ethereum-uniswap-v4/src/modules/mod.rs b/substreams/ethereum-uniswap-v4/src/modules/mod.rs index cd855e9..4dd323e 100644 --- a/substreams/ethereum-uniswap-v4/src/modules/mod.rs +++ b/substreams/ethereum-uniswap-v4/src/modules/mod.rs @@ -36,8 +36,30 @@ impl From 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 for tycho_substreams::prelude::Transaction { fn from(value: Transaction) -> Self { 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, + } + } +} diff --git a/substreams/ethereum-uniswap-v4/src/modules/uni_math.rs b/substreams/ethereum-uniswap-v4/src/modules/uni_math.rs index 2fcdede..f225309 100644 --- a/substreams/ethereum-uniswap-v4/src/modules/uni_math.rs +++ b/substreams/ethereum-uniswap-v4/src/modules/uni_math.rs @@ -1,5 +1,6 @@ use num_bigint::BigInt; use std::ops::Shr; + /// Calculates the amounts of token0 and token1 for a given position /// /// Source: https://github.com/Uniswap/v4-core/blob/main/src/libraries/Pool.sol