formatting & lints

This commit is contained in:
kayibal
2025-02-05 18:04:36 -06:00
committed by Alan Höng
parent 5eb08acf82
commit 22f70ca110
10 changed files with 2150 additions and 2541 deletions

View File

@@ -4,7 +4,6 @@ pub mod balances;
pub mod contract; pub mod contract;
mod mock_store; mod mock_store;
pub mod models; pub mod models;
#[allow(clippy::too_long_first_doc_paragraph)]
mod pb; mod pb;
pub mod prelude { pub mod prelude {

View File

@@ -253,7 +253,7 @@ pub fn map_protocol_changes(
let id = components_store let id = components_store
.get_last(format!("pool:0x{}", hex::encode(address))) .get_last(format!("pool:0x{}", hex::encode(address)))
.unwrap(); // Shouldn't happen because we filter by known components in .unwrap(); // Shouldn't happen because we filter by known components in
// `extract_contract_changes_builder` // `extract_contract_changes_builder`
change.mark_component_as_updated(&id); change.mark_component_as_updated(&id);
} }
}) })

File diff suppressed because it is too large Load Diff

View File

@@ -1,4 +1,3 @@
mod abi; mod abi;
mod modules; mod modules;
mod pool_factories; mod pool_factories;

View File

@@ -27,26 +27,22 @@
//! Adjustments to the template may include: //! Adjustments to the template may include:
//! - Handling native ETH balances alongside token balances. //! - Handling native ETH balances alongside token balances.
//! - Customizing indexing logic for specific factory contract behavior. //! - Customizing indexing logic for specific factory contract behavior.
use std::collections::HashMap;
use anyhow::Result;
use substreams::pb::substreams::StoreDeltas;
use substreams::prelude::*;
use substreams_ethereum::Event;
use substreams_ethereum::pb::eth;
use tycho_substreams::balances::aggregate_balances_changes;
use tycho_substreams::contract::extract_contract_changes_builder;
use tycho_substreams::prelude::*;
use itertools::Itertools;
use crate::pool_factories; use crate::pool_factories;
use anyhow::Result;
use itertools::Itertools;
use std::collections::HashMap;
use substreams::{pb::substreams::StoreDeltas, prelude::*};
use substreams_ethereum::{pb::eth, Event};
use tycho_substreams::{
balances::aggregate_balances_changes, contract::extract_contract_changes_builder, prelude::*,
};
/// Find and create all relevant protocol components /// Find and create all relevant protocol components
/// ///
/// This method maps over blocks and instantiates ProtocolComponents with a unique ids /// This method maps over blocks and instantiates ProtocolComponents with a unique ids
/// as well as all necessary metadata for routing and encoding. /// as well as all necessary metadata for routing and encoding.
#[substreams::handlers::map] #[substreams::handlers::map]
fn map_protocol_components( fn map_protocol_components(block: eth::v2::Block) -> Result<BlockTransactionProtocolComponents> {
block: eth::v2::Block
) -> Result<BlockTransactionProtocolComponents> {
Ok(BlockTransactionProtocolComponents { Ok(BlockTransactionProtocolComponents {
tx_components: block tx_components: block
.transactions() .transactions()
@@ -55,11 +51,7 @@ fn map_protocol_components(
.logs_with_calls() .logs_with_calls()
.filter_map(|(log, call)| { .filter_map(|(log, call)| {
// TODO: ensure this method is implemented correctly // TODO: ensure this method is implemented correctly
pool_factories::maybe_create_component( pool_factories::maybe_create_component(call.call, log, tx)
call.call,
log,
tx,
)
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@@ -79,17 +71,21 @@ fn map_protocol_components(
/// you need to access the whole set of components within your indexing logic. /// you need to access the whole set of components within your indexing logic.
/// ///
/// Popular use cases are: /// Popular use cases are:
/// - Checking if a contract belongs to a component. In this case suggest to use an /// - Checking if a contract belongs to a component. In this case suggest to use an address as the
/// address as the store key so lookup operations are O(1). /// store key so lookup operations are O(1).
/// - Tallying up relative balances changes to calcualte absolute erc20 token balances /// - Tallying up relative balances changes to calcualte absolute erc20 token balances per
/// per component. /// component.
/// ///
/// Usually you can skip this step if: /// Usually you can skip this step if:
/// - You are interested in a static set of components only /// - You are interested in a static set of components only
/// - Your protocol emits balance change events with absolute values /// - Your protocol emits balance change events with absolute values
#[substreams::handlers::store] #[substreams::handlers::store]
fn store_protocol_components(map_protocol_components: BlockTransactionProtocolComponents, store: StoreSetRaw) { fn store_protocol_components(
map_protocol_components.tx_components map_protocol_components: BlockTransactionProtocolComponents,
store: StoreSetRaw,
) {
map_protocol_components
.tx_components
.into_iter() .into_iter()
.for_each(|tx_pc| { .for_each(|tx_pc| {
tx_pc tx_pc
@@ -120,8 +116,12 @@ fn store_protocol_components(map_protocol_components: BlockTransactionProtocolCo
/// You may want to ignore LP tokens if your protocol emits transfer events for these /// You may want to ignore LP tokens if your protocol emits transfer events for these
/// here. /// here.
#[substreams::handlers::map] #[substreams::handlers::map]
fn map_relative_component_balance(block: eth::v2::Block, store: StoreGetRaw) -> Result<BlockBalanceDeltas> { fn map_relative_component_balance(
let res = block.logs() block: eth::v2::Block,
store: StoreGetRaw,
) -> Result<BlockBalanceDeltas> {
let res = block
.logs()
.filter_map(|log| { .filter_map(|log| {
crate::abi::erc20::events::Transfer::match_and_decode(log).map(|transfer| { crate::abi::erc20::events::Transfer::match_and_decode(log).map(|transfer| {
let to_addr = hex::encode(transfer.to.as_slice()); let to_addr = hex::encode(transfer.to.as_slice());
@@ -235,7 +235,6 @@ fn map_protocol_changes(
}); });
}); });
// Extract and insert any storage changes that happened for any of the components. // Extract and insert any storage changes that happened for any of the components.
extract_contract_changes_builder( extract_contract_changes_builder(
&block, &block,
@@ -251,7 +250,6 @@ fn map_protocol_changes(
&mut transaction_changes, &mut transaction_changes,
); );
// Process all `transaction_changes` for final output in the `BlockChanges`, // Process all `transaction_changes` for final output in the `BlockChanges`,
// sorted by transaction index (the key). // sorted by transaction index (the key).
Ok(BlockChanges { Ok(BlockChanges {

View File

@@ -1,7 +1,8 @@
use substreams::hex; use substreams::hex;
use substreams_ethereum::pb::eth::v2::{Call, Log, TransactionTrace}; use substreams_ethereum::pb::eth::v2::{Call, Log, TransactionTrace};
use tycho_substreams::models::{ChangeType, FinancialType, ImplementationType, ProtocolComponent, ProtocolType}; use tycho_substreams::models::{
ChangeType, FinancialType, ImplementationType, ProtocolComponent, ProtocolType,
};
/// Potentially constructs a new ProtocolComponent given a call /// Potentially constructs a new ProtocolComponent given a call
/// ///
@@ -40,4 +41,4 @@ pub fn maybe_create_component(
} }
_ => None, _ => None,
} }
} }

File diff suppressed because it is too large Load Diff

View File

@@ -1,4 +1,3 @@
mod abi; mod abi;
mod pool_factories;
mod modules; mod modules;
mod pool_factories;

View File

@@ -14,21 +14,16 @@
//! likely that you will need to adapt the steps to suit your specific use case. Use the //! likely that you will need to adapt the steps to suit your specific use case. Use the
//! provided code with care and ensure you fully understand each step before proceeding //! provided code with care and ensure you fully understand each step before proceeding
//! with your implementation //! with your implementation
//! use crate::{pool_factories, pool_factories::DeploymentConfig};
use std::collections::HashMap;
use anyhow::Result; use anyhow::Result;
use substreams::pb::substreams::StoreDeltas;
use substreams::prelude::*;
use substreams_ethereum::Event;
use substreams_ethereum::pb::eth;
use tycho_substreams::balances::aggregate_balances_changes;
use tycho_substreams::contract::extract_contract_changes_builder;
use tycho_substreams::prelude::*;
use itertools::Itertools; use itertools::Itertools;
use prost::Message; use prost::Message;
use substreams_ethereum::block_view::CallView; use std::collections::HashMap;
use crate::pool_factories; use substreams::{pb::substreams::StoreDeltas, prelude::*};
use crate::pool_factories::DeploymentConfig; use substreams_ethereum::{block_view::CallView, pb::eth, Event};
use tycho_substreams::{
balances::aggregate_balances_changes, contract::extract_contract_changes_builder, prelude::*,
};
/// Find and create all relevant protocol components /// Find and create all relevant protocol components
/// ///
@@ -48,12 +43,7 @@ fn map_protocol_components(
.logs_with_calls() .logs_with_calls()
.filter_map(|(log, call)| { .filter_map(|(log, call)| {
// TODO: ensure this method is implemented correctly // TODO: ensure this method is implemented correctly
pool_factories::maybe_create_component( pool_factories::maybe_create_component(call.call, log, tx, &config)
call.call,
log,
tx,
&config,
)
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@@ -68,8 +58,12 @@ fn map_protocol_components(
} }
#[substreams::handlers::store] #[substreams::handlers::store]
fn store_protocol_tokens(map_protocol_components: BlockTransactionProtocolComponents, store: StoreSetInt64) { fn store_protocol_tokens(
map_protocol_components.tx_components map_protocol_components: BlockTransactionProtocolComponents,
store: StoreSetInt64,
) {
map_protocol_components
.tx_components
.into_iter() .into_iter()
.for_each(|tx_pc| { .for_each(|tx_pc| {
tx_pc tx_pc
@@ -92,20 +86,22 @@ fn store_protocol_tokens(map_protocol_components: BlockTransactionProtocolCompon
/// is decreased. /// is decreased.
/// ///
/// ## Note: /// ## Note:
/// - If your protocol emits events that let you calculate balance deltas more /// - If your protocol emits events that let you calculate balance deltas more efficiently you may
/// efficiently you may want to use those instead of raw transfers. /// want to use those instead of raw transfers.
/// - Changes are necessary if your protocol uses native ETH or your component burns or /// - Changes are necessary if your protocol uses native ETH or your component burns or mints tokens
/// mints tokens without emitting transfer events. /// without emitting transfer events.
/// - You may want to ignore LP tokens if your protocol emits transfer events for these /// - You may want to ignore LP tokens if your protocol emits transfer events for these here.
/// here.
#[substreams::handlers::map] #[substreams::handlers::map]
fn map_relative_component_balance(params: String, block: eth::v2::Block, store: StoreGetInt64) -> Result<BlockBalanceDeltas> { fn map_relative_component_balance(
params: String,
block: eth::v2::Block,
store: StoreGetInt64,
) -> Result<BlockBalanceDeltas> {
let config: DeploymentConfig = serde_qs::from_str(params.as_str())?; let config: DeploymentConfig = serde_qs::from_str(params.as_str())?;
let res = block let res = block
.transactions() .transactions()
.flat_map(|tx| { .flat_map(|tx| {
tx tx.logs_with_calls()
.logs_with_calls()
.filter_map(|(log, call)| { .filter_map(|(log, call)| {
let token_addr_hex = hex::encode(&log.address); let token_addr_hex = hex::encode(&log.address);
if !store.has_last(&token_addr_hex) { if !store.has_last(&token_addr_hex) {
@@ -222,19 +218,17 @@ fn map_protocol_changes(
balances balances
.values() .values()
.for_each(|token_bc_map| { .for_each(|token_bc_map| {
token_bc_map token_bc_map.values().for_each(|bc| {
.values() // track component balance
.for_each(|bc| { builder.add_balance_change(bc);
// track component balance // track vault contract balance
builder.add_balance_change(bc); contract_changes
// track vault contract balance .upsert_token_balance(bc.token.as_slice(), bc.balance.as_slice())
contract_changes.upsert_token_balance(bc.token.as_slice(), bc.balance.as_slice()) })
})
}); });
builder.add_contract_changes(&contract_changes); builder.add_contract_changes(&contract_changes);
}); });
// Extract and insert any storage changes that happened for any of the components. // Extract and insert any storage changes that happened for any of the components.
extract_contract_changes_builder( extract_contract_changes_builder(
&block, &block,
@@ -258,4 +252,4 @@ fn map_protocol_changes(
.filter_map(|(_, builder)| builder.build()) .filter_map(|(_, builder)| builder.build())
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
}) })
} }

View File

@@ -1,7 +1,8 @@
use serde::Deserialize; use serde::Deserialize;
use substreams_ethereum::pb::eth::v2::{Call, Log, TransactionTrace}; use substreams_ethereum::pb::eth::v2::{Call, Log, TransactionTrace};
use tycho_substreams::models::{ChangeType, FinancialType, ImplementationType, ProtocolComponent, ProtocolType}; use tycho_substreams::models::{
ChangeType, FinancialType, ImplementationType, ProtocolComponent, ProtocolType,
};
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct DeploymentConfig { pub struct DeploymentConfig {
@@ -48,7 +49,6 @@ pub fn maybe_create_component(
} }
} }
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
@@ -58,4 +58,4 @@ mod test {
assert_eq!(config.vault_address, [0u8, 1u8]); assert_eq!(config.vault_address, [0u8, 1u8]);
} }
} }