feat: add AccountBalanceChange proto message (#137)

This commit is contained in:
Louise Poole
2025-01-23 11:00:12 +02:00
committed by GitHub
parent 53f764972d
commit 28dd2fc858
3 changed files with 102 additions and 79 deletions

View File

@@ -55,7 +55,6 @@ enum FinancialType{
PSM = 3; PSM = 3;
} }
enum ImplementationType { enum ImplementationType {
VM = 0; VM = 0;
CUSTOM = 1; CUSTOM = 1;
@@ -128,6 +127,14 @@ message ContractSlot {
bytes value = 3; bytes value = 3;
} }
// A struct for following the token balance changes for a contract.
message AccountBalanceChange {
// The address of the ERC20 token whose balance changed.
bytes token = 1;
// The new balance of the token. Note: it must be a big endian encoded int.
bytes balance = 2;
}
// Changes made to a single contract's state. // Changes made to a single contract's state.
message ContractChange { message ContractChange {
// The contract's address // The contract's address
@@ -140,6 +147,8 @@ message ContractChange {
repeated ContractSlot slots = 4; repeated ContractSlot slots = 4;
// Whether this is an update, a creation or a deletion. // Whether this is an update, a creation or a deletion.
ChangeType change = 5; ChangeType change = 5;
// The new ERC20 balances of the contract.
repeated AccountBalanceChange token_balances = 6;
} }
// Aggregate entities // Aggregate entities

View File

@@ -467,6 +467,7 @@ pub struct InterimContractChange {
code: Vec<u8>, code: Vec<u8>,
slots: HashMap<Vec<u8>, SlotValue>, slots: HashMap<Vec<u8>, SlotValue>,
change: ChangeType, change: ChangeType,
token_balances: HashMap<Vec<u8>, Vec<u8>>,
} }
impl InterimContractChange { impl InterimContractChange {
@@ -477,6 +478,7 @@ impl InterimContractChange {
code: vec![], code: vec![],
slots: Default::default(), slots: Default::default(),
change: if creation { ChangeType::Creation } else { ChangeType::Update }, change: if creation { ChangeType::Creation } else { ChangeType::Update },
token_balances: Default::default(),
} }
} }
@@ -530,6 +532,11 @@ impl From<InterimContractChange> for Option<ContractChange> {
.map(|(slot, value)| ContractSlot { slot, value: value.new_value }) .map(|(slot, value)| ContractSlot { slot, value: value.new_value })
.collect(), .collect(),
change: value.change.into(), change: value.change.into(),
token_balances: value
.token_balances
.into_iter()
.map(|(k, v)| AccountBalanceChange { token: k, balance: v })
.collect(),
}; };
if contract_change.is_empty() { if contract_change.is_empty() {
None None

View File

@@ -38,8 +38,7 @@ pub struct Transaction {
pub index: u64, pub index: u64,
} }
/// A custom struct representing an arbitrary attribute of a protocol component. /// A custom struct representing an arbitrary attribute of a protocol component.
/// This is mainly used by the native integration to track the necessary information about the /// This is mainly used by the native integration to track the necessary information about the protocol.
/// protocol.
#[allow(clippy::derive_partial_eq_without_eq)] #[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)] #[derive(Clone, PartialEq, ::prost::Message)]
pub struct Attribute { pub struct Attribute {
@@ -66,13 +65,11 @@ pub struct ProtocolType {
pub implementation_type: i32, pub implementation_type: i32,
} }
/// A struct describing a part of the protocol. /// A struct describing a part of the protocol.
/// /// Note: For example this can be a UniswapV2 pair, that tracks the two ERC20 tokens used by the pair,
/// Note: For example this can be a UniswapV2 pair, that tracks the two ERC20 tokens used by the /// the component would represent a single contract. In case of VM integration, such component would
/// pair, the component would represent a single contract. In case of VM integration, such component /// not need any attributes, because all the relevant info would be tracked via storage slots and balance changes.
/// would not need any attributes, because all the relevant info would be tracked via storage slots /// It can also be a wrapping contract, like WETH, that has a constant price, but it allows swapping tokens.
/// and balance changes. It can also be a wrapping contract, like WETH, that has a constant price, /// This is why the name ProtocolComponent is used instead of "Pool" or "Pair".
/// but it allows swapping tokens. This is why the name ProtocolComponent is used instead of "Pool"
/// or "Pair".
#[allow(clippy::derive_partial_eq_without_eq)] #[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)] #[derive(Clone, PartialEq, ::prost::Message)]
pub struct ProtocolComponent { pub struct ProtocolComponent {
@@ -103,10 +100,8 @@ pub struct ProtocolComponent {
pub tx: ::core::option::Option<Transaction>, pub tx: ::core::option::Option<Transaction>,
} }
/// A struct for following the changes of Total Value Locked (TVL) of a protocol component. /// A struct for following the changes of Total Value Locked (TVL) of a protocol component.
/// /// Note that if a ProtocolComponent contains multiple contracts, the TVL is tracked for the component as a whole.
/// Note that if a ProtocolComponent contains multiple contracts, the TVL is tracked for the /// E.g. for UniswapV2 pair WETH/USDC, this tracks the USDC and WETH balance of the pair contract.
/// component as a whole. E.g. for UniswapV2 pair WETH/USDC, this tracks the USDC and WETH balance
/// of the pair contract.
#[allow(clippy::derive_partial_eq_without_eq)] #[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)] #[derive(Clone, PartialEq, ::prost::Message)]
pub struct BalanceChange { pub struct BalanceChange {
@@ -117,8 +112,7 @@ pub struct BalanceChange {
#[prost(bytes="vec", tag="2")] #[prost(bytes="vec", tag="2")]
pub balance: ::prost::alloc::vec::Vec<u8>, pub balance: ::prost::alloc::vec::Vec<u8>,
/// The id of the component whose TVL is tracked. Note: This MUST be utf8 encoded. /// The id of the component whose TVL is tracked. Note: This MUST be utf8 encoded.
/// If the protocol component includes multiple contracts, the balance change must be /// If the protocol component includes multiple contracts, the balance change must be aggregated to reflect how much tokens can be traded.
/// aggregated to reflect how much tokens can be traded.
#[prost(bytes="vec", tag="3")] #[prost(bytes="vec", tag="3")]
pub component_id: ::prost::alloc::vec::Vec<u8>, pub component_id: ::prost::alloc::vec::Vec<u8>,
} }
@@ -148,6 +142,17 @@ pub struct ContractSlot {
#[prost(bytes="vec", tag="3")] #[prost(bytes="vec", tag="3")]
pub value: ::prost::alloc::vec::Vec<u8>, pub value: ::prost::alloc::vec::Vec<u8>,
} }
/// A struct for following the token balance changes for a contract.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct AccountBalanceChange {
/// The address of the ERC20 token whose balance changed.
#[prost(bytes="vec", tag="1")]
pub token: ::prost::alloc::vec::Vec<u8>,
/// The new balance of the token. Note: it must be a big endian encoded int.
#[prost(bytes="vec", tag="2")]
pub balance: ::prost::alloc::vec::Vec<u8>,
}
/// Changes made to a single contract's state. /// Changes made to a single contract's state.
#[allow(clippy::derive_partial_eq_without_eq)] #[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)] #[derive(Clone, PartialEq, ::prost::Message)]
@@ -167,6 +172,9 @@ pub struct ContractChange {
/// Whether this is an update, a creation or a deletion. /// Whether this is an update, a creation or a deletion.
#[prost(enumeration="ChangeType", tag="5")] #[prost(enumeration="ChangeType", tag="5")]
pub change: i32, pub change: i32,
/// The new ERC20 balances of the contract.
#[prost(message, repeated, tag="6")]
pub token_balances: ::prost::alloc::vec::Vec<AccountBalanceChange>,
} }
// Aggregate entities // Aggregate entities
@@ -178,8 +186,7 @@ pub struct TransactionChanges {
#[prost(message, optional, tag="1")] #[prost(message, optional, tag="1")]
pub tx: ::core::option::Option<Transaction>, pub tx: ::core::option::Option<Transaction>,
/// Contains the changes induced by the above transaction, aggregated on a per-contract basis. /// Contains the changes induced by the above transaction, aggregated on a per-contract basis.
/// Contains the contract changes induced by the above transaction, usually for tracking VM /// Contains the contract changes induced by the above transaction, usually for tracking VM components.
/// components.
#[prost(message, repeated, tag="2")] #[prost(message, repeated, tag="2")]
pub contract_changes: ::prost::alloc::vec::Vec<ContractChange>, pub contract_changes: ::prost::alloc::vec::Vec<ContractChange>,
/// Contains the entity changes induced by the above transaction. /// Contains the entity changes induced by the above transaction.