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.
This commit is contained in:
kayibal
2024-07-22 18:57:38 +01:00
parent 2eb41d82f0
commit 148fac276c
6 changed files with 127 additions and 39 deletions

14
substreams/Cargo.lock generated
View File

@@ -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",
]

View File

@@ -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

View File

@@ -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

View File

@@ -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<T: serde::Serialize + Debug>(v: T) -> Vec<u8> {
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<u8>]) -> Vec<u8> {
json_serialize_value(
addresses
.iter()
.map(|a| format!("0x{}", hex::encode(a)))
.collect::<Vec<_>>(),
)
}
/// 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<u8> {
json_serialize_value(
values
.iter()
.map(|v| format!("0x{}", hex::encode(v.to_signed_bytes_be())))
.collect::<Vec<_>>(),
)
}

View File

@@ -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::*;

View File

@@ -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<BigInt` which is needed
/// to be able to encode the `normalized_weights` and `weights` `Attribute`s. This should also be
/// handled by any downstream application.
trait SerializableVecBigInt {
fn serialize_bytes(&self) -> Vec<u8>;
#[allow(dead_code)]
fn deserialize_bytes(bytes: &[u8]) -> Vec<BigInt>;
}
impl SerializableVecBigInt for Vec<BigInt> {
fn serialize_bytes(&self) -> Vec<u8> {
self.iter()
.flat_map(|big_int| big_int.to_signed_bytes_be())
.collect()
}
fn deserialize_bytes(bytes: &[u8]) -> Vec<BigInt> {
bytes
.chunks_exact(32)
.map(BigInt::from_signed_bytes_be)
.collect::<Vec<BigInt>>()
}
}
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),
)