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:
2
substreams/Cargo.lock
generated
2
substreams/Cargo.lock
generated
@@ -384,7 +384,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ethereum-uniswap-v3-logs-only"
|
name = "ethereum-uniswap-v3-logs-only"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"ethabi 18.0.0",
|
"ethabi 18.0.0",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "ethereum-uniswap-v3-logs-only"
|
name = "ethereum-uniswap-v3-logs-only"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
specVersion: v0.1.0
|
specVersion: v0.1.0
|
||||||
package:
|
package:
|
||||||
name: "base_uniswap_v3_logs_only"
|
name: "base_uniswap_v3_logs_only"
|
||||||
version: v0.1.0
|
version: v0.1.1
|
||||||
|
|
||||||
protobuf:
|
protobuf:
|
||||||
files:
|
files:
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
specVersion: v0.1.0
|
specVersion: v0.1.0
|
||||||
package:
|
package:
|
||||||
name: "ethereum_uniswap_v3_logs_only"
|
name: "ethereum_uniswap_v3_logs_only"
|
||||||
version: v0.1.0
|
version: v0.1.1
|
||||||
|
|
||||||
protobuf:
|
protobuf:
|
||||||
files:
|
files:
|
||||||
|
|||||||
@@ -32,15 +32,19 @@ 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");
|
||||||
|
|
||||||
|
receipt
|
||||||
|
.logs
|
||||||
|
.iter()
|
||||||
.filter_map(|log| {
|
.filter_map(|log| {
|
||||||
let key = format!("{}:{}", "Pool", log.address.to_hex());
|
let key = format!("{}:{}", "Pool", log.address.to_hex());
|
||||||
// Skip if the log is not from a known uniswapV3 pool.
|
// Skip if the log is not from a known uniswapV3 pool.
|
||||||
if let Some(pool) = pools_store.get_last(key) {
|
if let Some(pool) = pools_store.get_last(key) {
|
||||||
log_to_event(&log, pool, tx.clone())
|
log_to_event(log, pool, &tx)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
@@ -54,7 +58,7 @@ pub fn map_events(
|
|||||||
Ok(Events { pool_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) {
|
if let Some(init) = Initialize::match_and_decode(event) {
|
||||||
Some(PoolEvent {
|
Some(PoolEvent {
|
||||||
log_ordinal: event.ordinal,
|
log_ordinal: event.ordinal,
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ fn event_to_balance_deltas(event: PoolEvent) -> Vec<BalanceDelta> {
|
|||||||
match event.r#type.unwrap() {
|
match event.r#type.unwrap() {
|
||||||
pool_event::Type::Mint(e) => vec![
|
pool_event::Type::Mint(e) => vec![
|
||||||
BalanceDelta {
|
BalanceDelta {
|
||||||
token: hex::decode(event.token0.clone()).unwrap(),
|
token: hex::decode(event.token0).unwrap(),
|
||||||
delta: BigInt::from_str(&e.amount_0)
|
delta: BigInt::from_str(&e.amount_0)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.to_signed_bytes_be(),
|
.to_signed_bytes_be(),
|
||||||
@@ -43,11 +43,11 @@ fn event_to_balance_deltas(event: PoolEvent) -> Vec<BalanceDelta> {
|
|||||||
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(event.token1.clone()).unwrap(),
|
token: hex::decode(event.token1).unwrap(),
|
||||||
delta: BigInt::from_str(&e.amount_1)
|
delta: BigInt::from_str(&e.amount_1)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.to_signed_bytes_be(),
|
.to_signed_bytes_be(),
|
||||||
@@ -58,7 +58,7 @@ fn event_to_balance_deltas(event: PoolEvent) -> Vec<BalanceDelta> {
|
|||||||
],
|
],
|
||||||
pool_event::Type::Collect(e) => vec![
|
pool_event::Type::Collect(e) => vec![
|
||||||
BalanceDelta {
|
BalanceDelta {
|
||||||
token: hex::decode(event.token0.clone()).unwrap(),
|
token: hex::decode(event.token0).unwrap(),
|
||||||
delta: BigInt::from_str(&e.amount_0)
|
delta: BigInt::from_str(&e.amount_0)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.neg()
|
.neg()
|
||||||
@@ -67,11 +67,11 @@ fn event_to_balance_deltas(event: PoolEvent) -> Vec<BalanceDelta> {
|
|||||||
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(event.token1.clone()).unwrap(),
|
token: hex::decode(event.token1).unwrap(),
|
||||||
delta: BigInt::from_str(&e.amount_1)
|
delta: BigInt::from_str(&e.amount_1)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.neg()
|
.neg()
|
||||||
@@ -86,7 +86,7 @@ fn event_to_balance_deltas(event: PoolEvent) -> Vec<BalanceDelta> {
|
|||||||
pool_event::Type::Swap(e) => {
|
pool_event::Type::Swap(e) => {
|
||||||
vec![
|
vec![
|
||||||
BalanceDelta {
|
BalanceDelta {
|
||||||
token: hex::decode(event.token0.clone()).unwrap(),
|
token: hex::decode(event.token0).unwrap(),
|
||||||
delta: BigInt::from_str(&e.amount_0)
|
delta: BigInt::from_str(&e.amount_0)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.to_signed_bytes_be(),
|
.to_signed_bytes_be(),
|
||||||
@@ -94,15 +94,15 @@ fn event_to_balance_deltas(event: PoolEvent) -> Vec<BalanceDelta> {
|
|||||||
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(event.token1.clone()).unwrap(),
|
token: hex::decode(event.token1).unwrap(),
|
||||||
delta: BigInt::from_str(&e.amount_1)
|
delta: BigInt::from_str(&e.amount_1)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.to_signed_bytes_be(),
|
.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.map(Into::into),
|
||||||
},
|
},
|
||||||
@@ -113,20 +113,18 @@ fn event_to_balance_deltas(event: PoolEvent) -> Vec<BalanceDelta> {
|
|||||||
token: hex::decode(event.token0).unwrap(),
|
token: hex::decode(event.token0).unwrap(),
|
||||||
delta: BigInt::from_str(&e.paid_0)
|
delta: BigInt::from_str(&e.paid_0)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.clone()
|
|
||||||
.to_signed_bytes_be(),
|
.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(event.token1).unwrap(),
|
token: hex::decode(event.token1).unwrap(),
|
||||||
delta: BigInt::from_str(&e.paid_1)
|
delta: BigInt::from_str(&e.paid_1)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.clone()
|
|
||||||
.to_signed_bytes_be(),
|
.to_signed_bytes_be(),
|
||||||
component_id: address,
|
component_id: address,
|
||||||
ord: event.log_ordinal,
|
ord: event.log_ordinal,
|
||||||
@@ -140,31 +138,21 @@ fn event_to_balance_deltas(event: PoolEvent) -> Vec<BalanceDelta> {
|
|||||||
delta: BigInt::from_str(&e.amount_0)
|
delta: BigInt::from_str(&e.amount_0)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.neg()
|
.neg()
|
||||||
.clone()
|
|
||||||
.to_signed_bytes_be(),
|
.to_signed_bytes_be(),
|
||||||
component_id: event
|
component_id: address.clone(),
|
||||||
.pool_address
|
|
||||||
.clone()
|
|
||||||
.as_bytes()
|
|
||||||
.to_vec(),
|
|
||||||
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(event.token1).unwrap(),
|
token: hex::decode(event.token1).unwrap(),
|
||||||
delta: BigInt::from_str(&e.amount_1)
|
delta: BigInt::from_str(&e.amount_1)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.clone()
|
|
||||||
.neg()
|
.neg()
|
||||||
.to_signed_bytes_be(),
|
.to_signed_bytes_be(),
|
||||||
component_id: event
|
component_id: address,
|
||||||
.pool_address
|
|
||||||
.clone()
|
|
||||||
.as_bytes()
|
|
||||||
.to_vec(),
|
|
||||||
ord: event.log_ordinal,
|
ord: event.log_ordinal,
|
||||||
tx: event.transaction.map(Into::into),
|
tx: event.transaction.map(Into::into),
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -27,7 +27,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);
|
||||||
|
|
||||||
|
|||||||
@@ -170,10 +170,10 @@ fn event_to_attributes_updates(event: PoolEvent) -> Vec<(Transaction, PoolAddres
|
|||||||
(
|
(
|
||||||
event
|
event
|
||||||
.transaction
|
.transaction
|
||||||
.clone()
|
.as_ref()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.into(),
|
.into(),
|
||||||
hex::decode(event.pool_address.clone()).unwrap(),
|
hex::decode(&event.pool_address).unwrap(),
|
||||||
Attribute {
|
Attribute {
|
||||||
name: "sqrt_price_x96".to_string(),
|
name: "sqrt_price_x96".to_string(),
|
||||||
value: BigInt::from_str(&initalize.sqrt_price)
|
value: BigInt::from_str(&initalize.sqrt_price)
|
||||||
@@ -197,10 +197,10 @@ fn event_to_attributes_updates(event: PoolEvent) -> Vec<(Transaction, PoolAddres
|
|||||||
(
|
(
|
||||||
event
|
event
|
||||||
.transaction
|
.transaction
|
||||||
.clone()
|
.as_ref()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.into(),
|
.into(),
|
||||||
hex::decode(event.pool_address.clone()).unwrap(),
|
hex::decode(&event.pool_address).unwrap(),
|
||||||
Attribute {
|
Attribute {
|
||||||
name: "sqrt_price_x96".to_string(),
|
name: "sqrt_price_x96".to_string(),
|
||||||
value: BigInt::from_str(&swap.sqrt_price)
|
value: BigInt::from_str(&swap.sqrt_price)
|
||||||
@@ -223,10 +223,10 @@ fn event_to_attributes_updates(event: PoolEvent) -> Vec<(Transaction, PoolAddres
|
|||||||
(
|
(
|
||||||
event
|
event
|
||||||
.transaction
|
.transaction
|
||||||
.clone()
|
.as_ref()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.into(),
|
.into(),
|
||||||
hex::decode(event.pool_address.clone()).unwrap(),
|
hex::decode(&event.pool_address).unwrap(),
|
||||||
Attribute {
|
Attribute {
|
||||||
name: "protocol_fees/token0".to_string(),
|
name: "protocol_fees/token0".to_string(),
|
||||||
value: BigInt::from(sfp.fee_protocol_0_new).to_signed_bytes_be(),
|
value: BigInt::from(sfp.fee_protocol_0_new).to_signed_bytes_be(),
|
||||||
|
|||||||
@@ -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 {
|
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 }
|
||||||
|
|||||||
Reference in New Issue
Block a user