Skip to content

Commit

Permalink
add map_valid_balance_changes map which outputs only valid balance ch…
Browse files Browse the repository at this point in the history
…anges
  • Loading branch information
colindickson committed Aug 28, 2023
1 parent a36a3ab commit 70862e8
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 19 deletions.
31 changes: 23 additions & 8 deletions proto/v1/erc20.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,35 @@ enum BalanceChangeType {
}

message BalanceChange {
string contract = 1; // 0xmytokencontract
string owner = 2; // 0xmyaddress
string old_balance = 3; // 0x1234
string new_balance = 4; // 0x1234
string contract = 1;
string owner = 2;
string old_balance = 3;
string new_balance = 4;

string transaction = 5; // 0xmytransaction
string storage_key = 6; // 0xmykey
uint32 call_index = 7; // 0xmycallindex
string transaction = 5;
string storage_key = 6;
uint32 call_index = 7;

string transfer_value = 8; // 0x1234
string transfer_value = 8;

BalanceChangeType change_type = 9;
}

message ValidBalanceChanges {
repeated ValidBalanceChange valid_balance_changes = 1;
}

message ValidBalanceChange {
string contract = 1;
string owner = 2;
string old_balance = 3;
string new_balance = 4;

string transaction = 5;
}



message BalanceChangeStats {
uint64 type0_count = 1;
uint64 type1_count = 2;
Expand Down
24 changes: 22 additions & 2 deletions src/maps.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::abi::{self};
use crate::pb::erc20::types::v1::{BalanceChange, BalanceChangeType, BalanceChanges, BalanceChangeStats};
use crate::pb::erc20::types::v1::{BalanceChange, BalanceChangeType, BalanceChanges, BalanceChangeStats, ValidBalanceChange, ValidBalanceChanges};
use abi::erc20::events::Transfer;
use hex_literal::hex;
use std::collections::HashMap;
Expand All @@ -23,9 +23,29 @@ pub fn map_balance_changes(block: Block) -> Result<BalanceChanges, Error> {
}

#[substreams::handlers::map]
pub fn balance_change_stats(clock: Clock, store: StoreGetBigInt) -> Result<BalanceChangeStats, Error> {
pub fn map_valid_balance_changes(balance_changes: BalanceChanges) -> Result<ValidBalanceChanges, Error> {
let mut out = Vec::new();
for change in balance_changes.balance_changes {
if change.change_type == BalanceChangeType::TypeUnknown as i32 {
continue;
}

out.push(ValidBalanceChange{
contract: change.contract,
owner: change.owner,
old_balance: change.old_balance,
new_balance: change.new_balance,
transaction: change.transaction,
});
}

Ok(ValidBalanceChanges {
valid_balance_changes: out,
})
}

#[substreams::handlers::map]
pub fn balance_change_stats(clock: Clock, store: StoreGetBigInt) -> Result<BalanceChangeStats, Error> {
let type_1 = store.get_last("type1").unwrap_or(BigInt::from(0));
let type_2 = store.get_last("type2").unwrap_or(BigInt::from(0));
let total = store.get_last("total").unwrap_or(BigInt::from(0));
Expand Down
28 changes: 20 additions & 8 deletions src/pb/erc20.types.v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,47 @@ pub struct BalanceChanges {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct BalanceChange {
/// 0xmytokencontract
#[prost(string, tag="1")]
pub contract: ::prost::alloc::string::String,
/// 0xmyaddress
#[prost(string, tag="2")]
pub owner: ::prost::alloc::string::String,
/// 0x1234
#[prost(string, tag="3")]
pub old_balance: ::prost::alloc::string::String,
/// 0x1234
#[prost(string, tag="4")]
pub new_balance: ::prost::alloc::string::String,
/// 0xmytransaction
#[prost(string, tag="5")]
pub transaction: ::prost::alloc::string::String,
/// 0xmykey
#[prost(string, tag="6")]
pub storage_key: ::prost::alloc::string::String,
/// 0xmycallindex
#[prost(uint32, tag="7")]
pub call_index: u32,
/// 0x1234
#[prost(string, tag="8")]
pub transfer_value: ::prost::alloc::string::String,
#[prost(enumeration="BalanceChangeType", tag="9")]
pub change_type: i32,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct ValidBalanceChanges {
#[prost(message, repeated, tag="1")]
pub valid_balance_changes: ::prost::alloc::vec::Vec<ValidBalanceChange>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct ValidBalanceChange {
#[prost(string, tag="1")]
pub contract: ::prost::alloc::string::String,
#[prost(string, tag="2")]
pub owner: ::prost::alloc::string::String,
#[prost(string, tag="3")]
pub old_balance: ::prost::alloc::string::String,
#[prost(string, tag="4")]
pub new_balance: ::prost::alloc::string::String,
#[prost(string, tag="5")]
pub transaction: ::prost::alloc::string::String,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct BalanceChangeStats {
#[prost(uint64, tag="1")]
pub type0_count: u64,
Expand Down
11 changes: 10 additions & 1 deletion substreams.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
specVersion: v0.1.0
package:
name: erc20_balance_changes
version: v0.0.3
version: v0.0.4
url: https://github.com/streamingfast/erc20-balance-changes
doc: ERC-20

Expand Down Expand Up @@ -30,6 +30,15 @@ modules:
output:
type: proto:erc20.types.v1.BalanceChanges

- name: map_valid_balance_changes
kind: map
doc: Extracts ERC20 balance changes
initialBlock: 1397553
inputs:
- map: map_balance_changes
output:
type: proto:erc20.types.v1.ValidBalanceChanges

- name: db_out
kind: map
inputs:
Expand Down

0 comments on commit 70862e8

Please sign in to comment.