diff --git a/substreams/ethereum-curve/README.md b/substreams/ethereum-curve/README.md new file mode 100644 index 0000000..908e8d0 --- /dev/null +++ b/substreams/ethereum-curve/README.md @@ -0,0 +1,5 @@ +# Instructions + +```bash +$ substreams run -e mainnet.eth.streamingfast.io:443 substreams.yaml map_protocol_changes --start-block 11507454 --stop-block +100 -p map_components=`python params.py` +``` diff --git a/substreams/ethereum-curve/params.json b/substreams/ethereum-curve/params.json index eafb489..fcdd5d5 100644 --- a/substreams/ethereum-curve/params.json +++ b/substreams/ethereum-curve/params.json @@ -1,11 +1,12 @@ [ { - "address": "0x5F890841f657d90E081bAbdB532A05996Af79Fe6", - "tx_hash": "0xb71a66c1d93c525a2dd19a8db0da19e65be04f36e733af7f03e3c9dff41aa16a", + "name": "3pool", + "address": "bEbc44782C7dB0a1A60Cb6fe97d0b483032FF1C7", + "tx_hash": "20793bbf260912aae189d5d261ff003c9b9166da8191d8f9d63ff1c7722f3ac6", "tokens": [ - "0x6b175474e89094c44da98b954eedeac495271d0f", - "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", - "0xdac17f958d2ee523a2206206994597c13d831ec7" + "6b175474e89094c44da98b954eedeac495271d0f", + "a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", + "dac17f958d2ee523a2206206994597c13d831ec7" ], "attributes": {} } diff --git a/substreams/ethereum-curve/params.py b/substreams/ethereum-curve/params.py index 8f3480a..fd6b9b3 100644 --- a/substreams/ethereum-curve/params.py +++ b/substreams/ethereum-curve/params.py @@ -47,7 +47,7 @@ def encode_json_to_query_params(params: list[dict[str, Any]]): def main(): with open(PARAMETERS, "r") as f: params = json.load(f) - print('"', encode_json_to_query_params(params), '"', sep="") + print(encode_json_to_query_params(params)) if __name__ == "__main__": diff --git a/substreams/ethereum-curve/src/modules.rs b/substreams/ethereum-curve/src/modules.rs index 3f45bcc..e67677c 100644 --- a/substreams/ethereum-curve/src/modules.rs +++ b/substreams/ethereum-curve/src/modules.rs @@ -58,9 +58,11 @@ pub fn map_components( }) .collect::>(); - components.extend(emit_specific_pools(¶ms, &block).expect( + if let Some(component) = emit_specific_pools(¶ms, &tx).expect( "An unexpected error occured when parsing params for emitting specific pools", - )); + ) { + components.push(component) + } if !components.is_empty() { Some(TransactionProtocolComponents { diff --git a/substreams/ethereum-curve/src/pools.rs b/substreams/ethereum-curve/src/pools.rs index db55f8b..8839298 100644 --- a/substreams/ethereum-curve/src/pools.rs +++ b/substreams/ethereum-curve/src/pools.rs @@ -1,7 +1,7 @@ use anyhow::{Context, Result}; use serde::Deserialize; use std::{collections::HashMap, iter::zip}; -use substreams_ethereum::pb::eth; +use substreams_ethereum::pb::eth::v2::TransactionTrace; use tycho_substreams::prelude::*; const PARAMS_SEPERATOR: &str = ","; @@ -11,8 +11,8 @@ struct PoolQueryParams { address: String, tx_hash: String, tokens: Vec, - attribute_keys: Vec, - attribute_vals: Vec, + attribute_keys: Option>, + attribute_vals: Option>, } /// This function parses the `params` string and extracts the pool query parameters. `params` are @@ -30,60 +30,63 @@ struct PoolQueryParams { /// if various addresses are not formatted properly. pub fn emit_specific_pools( params: &String, - block: ð::v2::Block, -) -> Result> { + tx: &TransactionTrace, +) -> Result> { let pools = parse_params(params)?; - create_components(block, pools) + create_component(tx, pools) } -fn create_components( - block: ð::v2::Block, +fn create_component( + tx: &TransactionTrace, pools: HashMap, -) -> Result, anyhow::Error> { - let mut components: Vec = vec![]; - for tx in block.transactions() { - let encoded_hash = hex::encode(tx.hash.clone()); - if let Some(pool) = pools.get(&encoded_hash) { - let component = ProtocolComponent { - id: pool.address.clone(), - tx: Some(Transaction { - to: tx.to.clone(), - from: tx.from.clone(), - hash: tx.hash.clone(), - index: tx.index.into(), - }), - tokens: pool - .tokens - .clone() - .into_iter() - .map(|token| Result::Ok(hex::decode(token)?)) - .collect::>>() - .with_context(|| "Token addresses were not formatted properly")?, - static_att: zip( - pool.attribute_keys.clone().into_iter(), - pool.attribute_vals.clone().into_iter(), - ) +) -> Result> { + let encoded_hash = hex::encode(tx.hash.clone()); + if let Some(pool) = pools.get(&encoded_hash) { + Ok(Some(ProtocolComponent { + id: pool.address.clone(), + tx: Some(Transaction { + to: tx.to.clone(), + from: tx.from.clone(), + hash: tx.hash.clone(), + index: tx.index.into(), + }), + tokens: pool + .tokens .clone() - .map(|(key, value)| Attribute { - name: key, - value: value.into(), - change: ChangeType::Creation.into(), - }) - .collect::>(), - contracts: vec![hex::decode(pool.address.clone()) - .with_context(|| "Pool address was not formatted properly")?], + .into_iter() + .map(|token| Result::Ok(hex::decode(token)?)) + .collect::>>() + .with_context(|| "Token addresses were not formatted properly")?, + static_att: zip( + pool.attribute_keys + .clone() + .unwrap_or(vec![]) + .into_iter(), + pool.attribute_vals + .clone() + .unwrap_or(vec![]) + .into_iter(), + ) + .clone() + .map(|(key, value)| Attribute { + name: key, + value: value.into(), change: ChangeType::Creation.into(), - protocol_type: Some(ProtocolType { - name: "curve_pool".into(), - financial_type: FinancialType::Swap.into(), - attribute_schema: Vec::new(), - implementation_type: ImplementationType::Vm.into(), - }), - }; - components.push(component); - } + }) + .collect::>(), + contracts: vec![hex::decode(pool.address.clone()) + .with_context(|| "Pool address was not formatted properly")?], + change: ChangeType::Creation.into(), + protocol_type: Some(ProtocolType { + name: "curve_pool".into(), + financial_type: FinancialType::Swap.into(), + attribute_schema: Vec::new(), + implementation_type: ImplementationType::Vm.into(), + }), + })) + } else { + Ok(None) } - Ok(components) } fn parse_params(params: &String) -> Result, anyhow::Error> { @@ -119,8 +122,8 @@ mod tests { "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48".to_string(), "0xdac17f958d2ee523a2206206994597c13d831ec7".to_string(), ], - attribute_keys: vec!["key1".to_string()], - attribute_vals: vec!["val1".to_string()], + attribute_keys: Some(vec!["key1".to_string()]), + attribute_vals: Some(vec!["val1".to_string()]), }, ); map diff --git a/substreams/ethereum-curve/substreams.yaml b/substreams/ethereum-curve/substreams.yaml index 0119eb8..bedbdba 100644 --- a/substreams/ethereum-curve/substreams.yaml +++ b/substreams/ethereum-curve/substreams.yaml @@ -19,7 +19,7 @@ binaries: modules: - name: map_components kind: map - initialBlock: 11942410 + initialBlock: 10809473 inputs: - params: string - source: sf.ethereum.type.v2.Block @@ -28,7 +28,7 @@ modules: - name: store_components kind: store - initialBlock: 11942410 + initialBlock: 10809473 updatePolicy: add valueType: int64 inputs: @@ -36,7 +36,7 @@ modules: - name: store_component_tokens kind: store - initialBlock: 11942410 + initialBlock: 10809473 updatePolicy: set valueType: string inputs: @@ -44,7 +44,7 @@ modules: - name: map_relative_balances kind: map - initialBlock: 11942410 # An arbitrary block that should change based on your requirements + initialBlock: 10809473 # An arbitrary block that should change based on your requirements inputs: - source: sf.ethereum.type.v2.Block - store: store_components @@ -54,7 +54,7 @@ modules: - name: store_balances kind: store - initialBlock: 11942410 + initialBlock: 10809473 updatePolicy: add valueType: bigint inputs: @@ -62,7 +62,7 @@ modules: - name: map_protocol_changes kind: map - initialBlock: 11942410 + initialBlock: 10809473 inputs: - source: sf.ethereum.type.v2.Block - map: map_components