From f64fcdf2208b0c1bb96dfec992f302d30f905a5a Mon Sep 17 00:00:00 2001 From: 0xMochan Date: Wed, 29 May 2024 14:39:32 -0400 Subject: [PATCH] feat: new pool->token function, renamed ERC20->erc20 --- substreams/ethereum-curve/run.sh | 2 +- substreams/ethereum-curve/src/abi/mod.rs | 2 +- substreams/ethereum-curve/src/pool_changes.rs | 2 +- .../ethereum-curve/src/pool_factories.rs | 42 +++++++++++++++---- substreams/ethereum-curve/substreams.yaml | 10 ++--- 5 files changed, 42 insertions(+), 16 deletions(-) diff --git a/substreams/ethereum-curve/run.sh b/substreams/ethereum-curve/run.sh index f9ea7ab..58c7c54 100644 --- a/substreams/ethereum-curve/run.sh +++ b/substreams/ethereum-curve/run.sh @@ -1 +1 @@ -substreams gui -e mainnet.eth.streamingfast.io:443 substreams.yaml map_protocol_changes --start-block 19933736 --stop-block +50 -p map_components=`python params.py` \ No newline at end of file +substreams gui -e mainnet.eth.streamingfast.io:443 substreams.yaml map_protocol_changes --start-block 19216042 --stop-block +50 -p map_components=`python params.py` \ No newline at end of file diff --git a/substreams/ethereum-curve/src/abi/mod.rs b/substreams/ethereum-curve/src/abi/mod.rs index e73d47c..593ee55 100644 --- a/substreams/ethereum-curve/src/abi/mod.rs +++ b/substreams/ethereum-curve/src/abi/mod.rs @@ -12,6 +12,6 @@ pub mod pool; pub mod tricrypto_factory; pub mod main_registry; pub mod pool_3pool; -pub mod ERC20; +pub mod erc20; pub mod meta_pool_factory; pub mod crypto_swap_registry; diff --git a/substreams/ethereum-curve/src/pool_changes.rs b/substreams/ethereum-curve/src/pool_changes.rs index 42f2146..7ccf405 100644 --- a/substreams/ethereum-curve/src/pool_changes.rs +++ b/substreams/ethereum-curve/src/pool_changes.rs @@ -26,7 +26,7 @@ fn get_pool_tokens(pool_address: &Vec, tokens_store: &StoreGetString) -> Opt /// TODO rewrite pub fn emit_deltas(log: LogView, tokens_store: &StoreGetString) -> Option { - let transfer = abi::ERC20::events::Transfer::match_and_decode(log)?; + let transfer = abi::erc20::events::Transfer::match_and_decode(log)?; let (component_id, pool_tokens, is_incoming) = if let Some(pool_tokens) = get_pool_tokens(&transfer.to, tokens_store) { diff --git a/substreams/ethereum-curve/src/pool_factories.rs b/substreams/ethereum-curve/src/pool_factories.rs index ab56ca0..61c7103 100644 --- a/substreams/ethereum-curve/src/pool_factories.rs +++ b/substreams/ethereum-curve/src/pool_factories.rs @@ -311,10 +311,7 @@ pub fn address_map( })?; let component_id = &call.return_data[12..]; - - let get_lp_token = - abi::meta_registry::functions::GetLpToken1 { pool: add_pool.base_pool.clone() }; - let lp_token = get_lp_token.call(META_REGISTRY.to_vec())?; + let lp_token = get_token_from_pool(&pool_added.base_pool); Some(ProtocolComponent { id: hex::encode(component_id), @@ -415,10 +412,7 @@ pub fn address_map( let add_pool = abi::crypto_swap_ng_factory::functions::DeployMetapool::match_and_decode(call)?; let component_id = &call.return_data[12..]; - - let get_lp_token = - abi::meta_registry::functions::GetLpToken1 { pool: add_pool.base_pool.clone() }; - let lp_token = get_lp_token.call(META_REGISTRY.to_vec())?; + let lp_token = get_token_from_pool(&pool_added.base_pool); Some(ProtocolComponent { id: hex::encode(component_id), @@ -665,3 +659,35 @@ pub fn address_map( _ => None, } } + +/// This function makes 3 attempts to confirm / get the LP token address from a pool address. +/// +/// 1. We attempt to see if the pool address is a token address itself by calling an ERC 20 func. +/// - Some pools may not be the token themselves +/// 2. Then, we try to ping the `META_REGISTRY` address to see if it has a record of the pool. +/// - Older pools might have been created before the `META_REGISTRY` was created and therefore +/// would have registered much later +/// 3. Finally, we have a hardcoded map of pool address -> token address for some pools. +/// +/// If all else fails, we force an `unwrap` to trigger a `panic` so that we can resolve this by +/// adding onto our map of `pool` -> `token` addresses. +fn get_token_from_pool(pool: &Vec) -> Vec { + abi::erc20::functions::Name {} + .call(pool.clone()) + .and(Some(pool.clone())) + .or_else(|| { + abi::meta_registry::functions::GetLpToken1 { pool: pool.clone() } + .call(META_REGISTRY.to_vec()) + }) + .or_else(|| { + substreams::log::info!(format!("Using pool tree with pool {}", hex::encode(&pool))); + match hex::encode(&pool).as_str() { + // Curve.fi DAI/USDC/USDT (3Crv) + "bebc44782c7db0a1a60cb6fe97d0b483032ff1c7" => { + hex::decode("6c3F90f043a72FA612cbac8115EE7e52BDe6E490").ok() + } + _ => None, + } + }) + .unwrap() +} diff --git a/substreams/ethereum-curve/substreams.yaml b/substreams/ethereum-curve/substreams.yaml index 974688d..81cbe70 100644 --- a/substreams/ethereum-curve/substreams.yaml +++ b/substreams/ethereum-curve/substreams.yaml @@ -20,7 +20,7 @@ binaries: modules: - name: map_components kind: map - initialBlock: 19933736 + initialBlock: 19216042 inputs: - params: string - source: sf.ethereum.type.v2.Block @@ -29,7 +29,7 @@ modules: - name: store_component_tokens kind: store - initialBlock: 19933736 + initialBlock: 19216042 updatePolicy: set valueType: string inputs: @@ -37,7 +37,7 @@ modules: - name: map_relative_balances kind: map - initialBlock: 19933736 # An arbitrary block that should change based on your requirements + initialBlock: 19216042 # An arbitrary block that should change based on your requirements inputs: - source: sf.ethereum.type.v2.Block - store: store_component_tokens @@ -46,7 +46,7 @@ modules: - name: store_balances kind: store - initialBlock: 19933736 + initialBlock: 19216042 updatePolicy: add valueType: bigint inputs: @@ -54,7 +54,7 @@ modules: - name: map_protocol_changes kind: map - initialBlock: 19933736 + initialBlock: 19216042 inputs: - source: sf.ethereum.type.v2.Block - map: map_components