From 148fac276cdff712b9edef4e38c04b2c8511675f Mon Sep 17 00:00:00 2001 From: kayibal Date: Mon, 22 Jul 2024 18:57:38 +0100 Subject: [PATCH] feat(balancer): Add rate providers to static attributes. Adds the rate providers and additional attributes to balancers static attributes. These will help in the future to migrate some of the components to use the DCI. Adds a small attributes module to tycho-substreams to make json encoding a bit easier. --- substreams/Cargo.lock | 14 +-- substreams/Cargo.toml | 2 + substreams/crates/tycho-substreams/Cargo.toml | 2 + .../crates/tycho-substreams/src/attributes.rs | 48 +++++++++ substreams/crates/tycho-substreams/src/lib.rs | 1 + .../ethereum-balancer/src/pool_factories.rs | 99 ++++++++++++------- 6 files changed, 127 insertions(+), 39 deletions(-) create mode 100644 substreams/crates/tycho-substreams/src/attributes.rs diff --git a/substreams/Cargo.lock b/substreams/Cargo.lock index 36656f6..1f4d8c6 100644 --- a/substreams/Cargo.lock +++ b/substreams/Cargo.lock @@ -857,18 +857,18 @@ checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1" [[package]] name = "serde" -version = "1.0.197" +version = "1.0.204" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" +checksum = "bc76f558e0cbb2a839d37354c575f1dc3fdc6546b5be373ba43d95f231bf7c12" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.197" +version = "1.0.204" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" +checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222" dependencies = [ "proc-macro2", "quote", @@ -877,9 +877,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.114" +version = "1.0.120" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5f09b1bd632ef549eaa9f60a1f8de742bdbc698e6cee2095fc84dde5f549ae0" +checksum = "4e0d21c9a8cae1235ad58a00c11cb40d4b1e5c784f1ef2c537876ed6ffd8b7c5" dependencies = [ "itoa", "ryu", @@ -1104,6 +1104,8 @@ dependencies = [ "itertools 0.12.1", "num-bigint", "prost 0.11.9", + "serde", + "serde_json", "substreams", "substreams-ethereum", ] diff --git a/substreams/Cargo.toml b/substreams/Cargo.toml index 4a69dfc..904aedb 100644 --- a/substreams/Cargo.toml +++ b/substreams/Cargo.toml @@ -12,6 +12,8 @@ hex-literal = "0.4.1" hex = "0.4.3" ethabi = "18.0.0" tycho-substreams = { path = "crates/tycho-substreams" } +serde = "1.0.204" +serde_json = "1.0.120" [profile.release] lto = true diff --git a/substreams/crates/tycho-substreams/Cargo.toml b/substreams/crates/tycho-substreams/Cargo.toml index a62469d..a63c044 100644 --- a/substreams/crates/tycho-substreams/Cargo.toml +++ b/substreams/crates/tycho-substreams/Cargo.toml @@ -11,3 +11,5 @@ hex.workspace = true itertools = "0.12.0" ethabi.workspace = true num-bigint = "0.4.4" +serde.workspace = true +serde_json.workspace = true diff --git a/substreams/crates/tycho-substreams/src/attributes.rs b/substreams/crates/tycho-substreams/src/attributes.rs new file mode 100644 index 0000000..3a00244 --- /dev/null +++ b/substreams/crates/tycho-substreams/src/attributes.rs @@ -0,0 +1,48 @@ +use std::fmt::Debug; +use substreams::prelude::BigInt; + +/// Encodes a value to bytes using json. +/// +/// ## Panics +/// In case the serialisation to json fails. +pub fn json_serialize_value(v: T) -> Vec { + serde_json::to_value(v) + .unwrap_or_else(|e| panic!("Failed to encode value as json {e}")) + .to_string() + .as_bytes() + .to_vec() +} + + +/// Encodes a list of addresses (in byte representation) into json. +/// +/// Converts each address to a 0x prefixed hex string and then serializes +/// the list of strings as a json. +/// +/// ## Panics +/// In case the serialisation to json fails. +pub fn json_serialize_address_list(addresses: &[Vec]) -> Vec { + json_serialize_value( + addresses + .iter() + .map(|a| format!("0x{}", hex::encode(a))) + .collect::>(), + ) +} + + +/// Encodes a list of BigInt values into json. +/// +/// Converts each integer to a 0x prefixed hex string and then serializes +/// the list of strings as a json. +/// +/// ## Panics +/// In case the serialisation to json fails. +pub fn json_serialize_bigint_list(values: &[BigInt]) -> Vec { + json_serialize_value( + values + .iter() + .map(|v| format!("0x{}", hex::encode(v.to_signed_bytes_be()))) + .collect::>(), + ) +} diff --git a/substreams/crates/tycho-substreams/src/lib.rs b/substreams/crates/tycho-substreams/src/lib.rs index 60f3047..4537305 100644 --- a/substreams/crates/tycho-substreams/src/lib.rs +++ b/substreams/crates/tycho-substreams/src/lib.rs @@ -4,6 +4,7 @@ pub mod contract; mod mock_store; pub mod models; mod pb; +pub mod attributes; pub mod prelude { pub use super::models::*; diff --git a/substreams/ethereum-balancer/src/pool_factories.rs b/substreams/ethereum-balancer/src/pool_factories.rs index 9cb4910..5f58ee4 100644 --- a/substreams/ethereum-balancer/src/pool_factories.rs +++ b/substreams/ethereum-balancer/src/pool_factories.rs @@ -1,33 +1,13 @@ use crate::{abi, modules::VAULT_ADDRESS}; -use substreams::{hex, scalar::BigInt}; +use substreams::hex; use substreams_ethereum::{ pb::eth::v2::{Call, Log, TransactionTrace}, Event, Function, }; -use tycho_substreams::prelude::*; - -/// This trait defines some helpers for serializing and deserializing `Vec Vec; - #[allow(dead_code)] - fn deserialize_bytes(bytes: &[u8]) -> Vec; -} - -impl SerializableVecBigInt for Vec { - fn serialize_bytes(&self) -> Vec { - self.iter() - .flat_map(|big_int| big_int.to_signed_bytes_be()) - .collect() - } - fn deserialize_bytes(bytes: &[u8]) -> Vec { - bytes - .chunks_exact(32) - .map(BigInt::from_signed_bytes_be) - .collect::>() - } -} +use tycho_substreams::{ + attributes::{json_serialize_address_list, json_serialize_bigint_list}, + prelude::*, +}; /// Helper function to get pool_registered event fn get_pool_registered( @@ -76,14 +56,13 @@ pub fn address_map( ("pool_type", "WeightedPoolFactory".as_bytes()), ( "normalized_weights", - &create_call - .normalized_weights - .serialize_bytes(), + &json_serialize_bigint_list(&create_call.normalized_weights), ), ( "pool_id", format!("0x{}", hex::encode(pool_registered.pool_id)).as_bytes(), ), + ("manual_updates", &[1u8]), ]) .as_swap_type("balancer_pool", ImplementationType::Vm), ) @@ -105,6 +84,11 @@ pub fn address_map( "pool_id", format!("0x{}", hex::encode(pool_registered.pool_id)).as_bytes(), ), + ("manual_updates", &[1u8]), + ( + "rate_providers", + &json_serialize_address_list(&create_call.rate_providers), + ), ]) .as_swap_type("balancer_pool", ImplementationType::Vm), ) @@ -119,7 +103,10 @@ pub fn address_map( Some( ProtocolComponent::at_contract(&pool_created.pool, &(tx.into())) .with_contracts(&[pool_created.pool, VAULT_ADDRESS.to_vec()]) - .with_tokens(&[create_call.main_token, create_call.wrapped_token]) + .with_tokens(&[ + create_call.main_token.clone(), + create_call.wrapped_token.clone(), + ]) .with_attributes(&[ ("pool_type", "ERC4626LinearPoolFactory".as_bytes()), ( @@ -132,6 +119,15 @@ pub fn address_map( "pool_id", format!("0x{}", hex::encode(pool_registered.pool_id)).as_bytes(), ), + ("manual_updates", &[1u8]), + ("main_token", &create_call.main_token), + ("wrapped_token", &create_call.wrapped_token), + ( + "fee", + &create_call + .swap_fee_percentage + .to_signed_bytes_be(), + ), ]) .as_swap_type("balancer_pool", ImplementationType::Vm), ) @@ -146,7 +142,10 @@ pub fn address_map( Some( ProtocolComponent::at_contract(&pool_created.pool, &(tx.into())) .with_contracts(&[pool_created.pool, VAULT_ADDRESS.to_vec()]) - .with_tokens(&[create_call.main_token, create_call.wrapped_token]) + .with_tokens(&[ + create_call.main_token.clone(), + create_call.wrapped_token.clone(), + ]) .with_attributes(&[ ("pool_type", "EulerLinearPoolFactory".as_bytes()), ( @@ -159,6 +158,15 @@ pub fn address_map( "pool_id", format!("0x{}", hex::encode(pool_registered.pool_id)).as_bytes(), ), + ("manual_updates", &[1u8]), + ("main_token", &create_call.main_token), + ("wrapped_token", &create_call.wrapped_token), + ( + "fee", + &create_call + .swap_fee_percentage + .to_signed_bytes_be(), + ), ]) .as_swap_type("balancer_pool", ImplementationType::Vm), ) @@ -221,7 +229,10 @@ pub fn address_map( Some( ProtocolComponent::at_contract(&pool_created.pool, &(tx.into())) .with_contracts(&[pool_created.pool, VAULT_ADDRESS.to_vec()]) - .with_tokens(&[create_call.main_token, create_call.wrapped_token]) + .with_tokens(&[ + create_call.main_token.clone(), + create_call.wrapped_token.clone(), + ]) .with_attributes(&[ ("pool_type", "SiloLinearPoolFactory".as_bytes()), ( @@ -234,6 +245,15 @@ pub fn address_map( "pool_id", format!("0x{}", hex::encode(pool_registered.pool_id)).as_bytes(), ), + ("manual_updates", &[1u8]), + ("main_token", &create_call.main_token), + ("wrapped_token", &create_call.wrapped_token), + ( + "fee", + &create_call + .swap_fee_percentage + .to_signed_bytes_be(), + ), ]) .as_swap_type("balancer_pool", ImplementationType::Vm), ) @@ -248,7 +268,10 @@ pub fn address_map( Some( ProtocolComponent::at_contract(&pool_created.pool, &(tx.into())) .with_contracts(&[pool_created.pool, VAULT_ADDRESS.to_vec()]) - .with_tokens(&[create_call.main_token, create_call.wrapped_token]) + .with_tokens(&[ + create_call.main_token.clone(), + create_call.wrapped_token.clone(), + ]) .with_attributes(&[ ("pool_type", "YearnLinearPoolFactory".as_bytes()), ( @@ -261,6 +284,15 @@ pub fn address_map( "pool_id", format!("0x{}", hex::encode(pool_registered.pool_id)).as_bytes(), ), + ("manual_updates", &[1u8]), + ("main_token", &create_call.main_token), + ("wrapped_token", &create_call.wrapped_token), + ( + "fee", + &create_call + .swap_fee_percentage + .to_signed_bytes_be(), + ), ]) .as_swap_type("balancer_pool", ImplementationType::Vm), ) @@ -280,11 +312,12 @@ pub fn address_map( .with_tokens(&create_call.tokens) .with_attributes(&[ ("pool_type", "WeightedPool2TokensFactory".as_bytes()), - ("weights", &create_call.weights.serialize_bytes()), + ("weights", &json_serialize_bigint_list(&create_call.weights)), ( "pool_id", format!("0x{}", hex::encode(pool_registered.pool_id)).as_bytes(), ), + ("manual_updates", &[1u8]), ]) .as_swap_type("balancer_pool", ImplementationType::Vm), )