fix: BalanceChange encoding

This commit is contained in:
Florian Pellissier
2024-03-12 12:51:26 +01:00
parent 1e546230de
commit 38dcd9d843
2 changed files with 8 additions and 2 deletions

View File

@@ -100,7 +100,7 @@ message ProtocolComponent {
message BalanceChange {
// The address of the ERC20 token whose balance changed.
bytes token = 1;
// The new balance of the token.
// The new balance of the token. Note: it must be a big endian encoded int.
bytes balance = 2;
// 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 aggregated to reflect how much tokens can be traded.

View File

@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::str::FromStr;
use anyhow::Result;
use substreams::hex;
@@ -200,12 +201,17 @@ pub fn map_changes(
.map(|(store_delta, balance_delta)| {
let pool_id = key::segment_at(&store_delta.key, 1);
let token_id = key::segment_at(&store_delta.key, 3);
// store_delta.new_value is an ASCII string representing an integer
let ascii_string =
String::from_utf8(store_delta.new_value.clone()).expect("Invalid UTF-8 sequence");
let balance = BigInt::from_str(&ascii_string).expect("Failed to parse integer");
let big_endian_bytes_balance = balance.to_bytes_be().1;
(
balance_delta.tx.unwrap(),
tycho::BalanceChange {
token: hex::decode(token_id).expect("Token ID not valid hex"),
balance: store_delta.new_value,
balance: big_endian_bytes_balance,
component_id: hex::decode(pool_id).expect("Token ID not valid hex"),
},
)