integrate remaining abis and clean up / comment some code

This commit is contained in:
0xMochan
2024-05-02 13:34:11 -05:00
parent 692579e930
commit 126bd003a8
2 changed files with 151 additions and 145 deletions

View File

@@ -123,7 +123,15 @@ pub fn map_relative_balances(
pools_store: StoreGetInt64, pools_store: StoreGetInt64,
tokens_store: StoreGetString, tokens_store: StoreGetString,
) -> Result<BlockBalanceDeltas, anyhow::Error> { ) -> Result<BlockBalanceDeltas, anyhow::Error> {
Ok(BlockBalanceDeltas { balance_deltas: emit_deltas(block, pools_store, tokens_store) }) Ok(BlockBalanceDeltas {
balance_deltas: {
block
.logs()
.filter_map(|log| emit_deltas(log, &pools_store, &tokens_store))
.flat_map(|deltas| deltas.into_iter())
.collect::<Vec<_>>()
},
})
} }
/// It's significant to include both the `pool_id` and the `token_id` for each balance delta as the /// It's significant to include both the `pool_id` and the `token_id` for each balance delta as the

View File

@@ -2,11 +2,19 @@ use substreams::{
scalar::BigInt, scalar::BigInt,
store::{StoreGet, StoreGetInt64, StoreGetString}, store::{StoreGet, StoreGetInt64, StoreGetString},
}; };
use substreams_ethereum::{block_view::LogView, pb::eth, Event}; use substreams_ethereum::{block_view::LogView, Event};
use tycho_substreams::prelude::*; use tycho_substreams::prelude::*;
use crate::abi; use crate::abi;
struct GenericAddLiquidity {
token_amounts: Vec<BigInt>,
provider: String,
fees: BigInt,
invariant: BigInt,
token_supply: BigInt,
}
fn tx_from_log(log: &LogView) -> Transaction { fn tx_from_log(log: &LogView) -> Transaction {
Transaction { Transaction {
hash: log.receipt.transaction.hash.clone(), hash: log.receipt.transaction.hash.clone(),
@@ -16,21 +24,89 @@ fn tx_from_log(log: &LogView) -> Transaction {
} }
} }
/// This function emits balance deltas for mints, burns, and exchanges in Curve pools. Since some
/// pools contain differing ABIs, we load in several examples of abis in order to best match the
/// topic ID to the correct event. The repetition in this function is dervived from the fact that
/// most of these events have similar structures, but the specific topic id differs.
pub fn emit_deltas( pub fn emit_deltas(
log: LogView, log: LogView,
pools_store: StoreGetInt64, pools_store: &StoreGetInt64,
tokens_store: StoreGetString, tokens_store: &StoreGetString,
) -> Option<impl Iterator<Item = BalanceDelta>> { ) -> Option<Vec<BalanceDelta>> {
if let Some(event) = abi::pool::events::TokenExchange::match_and_decode(log) { let pool_key = format!("pool:{}", hex::encode(&log.address()));
if pools_store if pools_store.get_last(pool_key).is_none() {
.get_last(format!("pool:{0}", hex::encode(&log.address())))
.is_none()
{
return None; return None;
} }
let tokens = tokens_store
.get_last(format!("pool:{}", hex::encode(log.address())))?
.split(":")
.map(|token| token.to_owned())
.collect::<Vec<_>>();
if let Some(event) = abi::pool::events::TokenExchange::match_and_decode(log) {
return token_change_deltas(event, log);
} else if let Some(event) = abi::pool_3pool::events::TokenExchange::match_and_decode(log) {
return token_change_deltas(
abi::pool::events::TokenExchange {
sold_id: event.sold_id,
bought_id: event.bought_id,
tokens_sold: event.tokens_sold,
tokens_bought: event.tokens_bought,
buyer: event.buyer,
},
log,
);
} else if let Some(event) = abi::pool_steth::events::TokenExchange::match_and_decode(log) {
return token_change_deltas(
abi::pool::events::TokenExchange {
sold_id: event.sold_id,
bought_id: event.bought_id,
tokens_sold: event.tokens_sold,
tokens_bought: event.tokens_bought,
buyer: event.buyer,
},
log,
);
} else if let Some(event) = abi::pool_tricrypto::events::TokenExchange::match_and_decode(log) {
return token_change_deltas(
abi::pool::events::TokenExchange {
sold_id: event.sold_id,
bought_id: event.bought_id,
tokens_sold: event.tokens_sold,
tokens_bought: event.tokens_bought,
buyer: event.buyer,
},
log,
);
} else if let Some(event) = abi::pool::events::AddLiquidity::match_and_decode(log) {
return add_liquidity_deltas(event.token_amounts.into(), &tokens, log);
} else if let Some(event) = abi::pool_3pool::events::AddLiquidity::match_and_decode(log) {
return add_liquidity_deltas(event.token_amounts.into(), &tokens, log)
} else if let Some(event) = abi::pool_steth::events::AddLiquidity::match_and_decode(log) {
return add_liquidity_deltas(event.token_amounts.into(), &tokens, log)
} else if let Some(event) = abi::pool_tricrypto::events::AddLiquidity::match_and_decode(log) {
return add_liquidity_deltas(event.token_amounts.into(), &tokens, log)
} else if let Some(event) = abi::pool::events::RemoveLiquidity::match_and_decode(log) {
return add_liquidity_deltas(event.token_amounts.into(), &tokens, log)
} else if let Some(event) = abi::pool_3pool::events::RemoveLiquidity::match_and_decode(log) {
return add_liquidity_deltas(event.token_amounts.into(), &tokens, log)
} else if let Some(event) = abi::pool_steth::events::RemoveLiquidity::match_and_decode(log) {
return add_liquidity_deltas(event.token_amounts.into(), &tokens, log)
} else if let Some(event) = abi::pool_tricrypto::events::RemoveLiquidity::match_and_decode(log)
{
return add_liquidity_deltas(event.token_amounts.into(), &tokens, log)
} else {
None
}
}
fn token_change_deltas(
event: abi::pool::events::TokenExchange,
log: LogView<'_>,
) -> Option<Vec<BalanceDelta>> {
let tokens_bought_delta: BigInt = event.tokens_bought * -1; let tokens_bought_delta: BigInt = event.tokens_bought * -1;
return Some( Some(vec![
vec![
BalanceDelta { BalanceDelta {
ord: log.log.ordinal, ord: log.log.ordinal,
tx: Some(tx_from_log(&log)), tx: Some(tx_from_log(&log)),
@@ -45,123 +121,45 @@ pub fn emit_deltas(
delta: tokens_bought_delta.to_signed_bytes_be(), delta: tokens_bought_delta.to_signed_bytes_be(),
component_id: log.address().into(), component_id: log.address().into(),
}, },
] ])
.into_iter(), }
)
} else if let Some(event) = abi::pool::events::AddLiquidity::match_and_decode(log) { fn add_liquidity_deltas(
let tokens = tokens_store amounts: Vec<BigInt>,
.get_last(format!("pool:{0}", hex::encode(log.address())))? tokens: &Vec<String>,
.split(":") log: LogView<'_>,
.map(|token| token.to_owned()) // Clone the tokens ) -> Option<Vec<BalanceDelta>> {
.collect::<Vec<_>>(); Some(
amounts
let deltas: Vec<_> = event .iter()
.token_amounts .zip(tokens)
.iter() .map(move |(token_amount, token_id)| BalanceDelta {
.zip(tokens) ord: log.log.ordinal,
.map(move |(token_amount, token_id)| BalanceDelta { tx: Some(tx_from_log(&log)),
ord: log.log.ordinal, token: token_id.as_str().into(),
tx: Some(tx_from_log(&log)), delta: token_amount.to_signed_bytes_be(),
token: token_id.into(), component_id: log.address().into(),
delta: token_amount.to_signed_bytes_be(), })
component_id: log.address().into(), .collect::<Vec<_>>(),
}) )
.collect(); }
return Some(deltas.into_iter()); fn remove_liquidity_deltas(
} else if let Some(event) = abi::pool::events::RemoveLiquidity::match_and_decode(log) { amounts: Vec<BigInt>,
let tokens = tokens_store tokens: &Vec<String>,
.get_last(format!("pool:{0}", hex::encode(log.address())))? log: LogView<'_>,
.split(":") ) -> Option<Vec<BalanceDelta>> {
.map(|token| token.to_owned()) // Clone the tokens Some(
.collect::<Vec<_>>(); amounts
.iter()
let deltas: Vec<_> = event .zip(tokens)
.token_amounts .map(move |(token_amount, token_id)| BalanceDelta {
.iter() ord: log.log.ordinal,
.zip(tokens) tx: Some(tx_from_log(&log)),
.map(move |(token_amount, token_id)| BalanceDelta { token: token_id.as_str().into(),
ord: log.log.ordinal, delta: token_amount.to_signed_bytes_be(),
tx: Some(tx_from_log(&log)), component_id: log.address().into(),
token: token_id.into(), })
delta: token_amount.to_signed_bytes_be(), .collect::<Vec<_>>(),
component_id: log.address().into(), )
})
.collect();
return Some(deltas.into_iter());
}
deltas.extend(
block
.logs()
.filter_map(|log| {
let event = abi::pool::events::AddLiquidity::match_and_decode(log)?;
Some((log, event))
})
.filter_map(|(log, event)| {
let tokens = tokens_store
.get_last(format!("pool:{0}", hex::encode(log.address())))?
.split(":")
.map(|token| token.to_owned()) // Clone the tokens
.collect::<Vec<_>>();
Some((tokens, log, event))
})
.flat_map(|(tokens, log, event)| {
event
.token_amounts
.iter()
.zip(tokens)
.map(move |(token_amount, token_id)| BalanceDelta {
ord: log.log.ordinal,
tx: Some(tx_from_log(&log)),
token: token_id.into(),
delta: token_amount.to_signed_bytes_be(),
component_id: log.address().into(),
})
.collect::<Vec<_>>()
})
.collect::<Vec<_>>(),
);
deltas.extend(
block
.logs()
.filter_map(|log| {
let event = abi::pool::events::RemoveLiquidity::match_and_decode(log)?;
Some((log, event))
})
.filter(|(log, _)| {
pools_store
.get_last(format!("pool:{0}", hex::encode(&log.address())))
.is_none()
})
.flat_map(|(log, event)| {
let tokens = tokens_store
.get_last(format!("pool:{}", hex::encode(log.address())))
.unwrap()
.split(":")
.map(|token| token.to_owned()) // Clone the tokens
.collect::<Vec<_>>();
event
.token_amounts
.iter()
.zip(tokens)
.map(move |(token_amount, token_id)| {
let negative_token_amount: BigInt = token_amount * BigInt::from(-1);
BalanceDelta {
ord: log.log.ordinal,
tx: Some(tx_from_log(&log)),
token: token_id.into(),
delta: negative_token_amount.to_signed_bytes_be(),
component_id: log.address().into(),
}
})
.collect::<Vec<_>>()
})
.collect::<Vec<_>>(),
);
deltas
} }