feat(substreams): add substreams for Uniswap v2 and v3
This commit is contained in:
1
substreams/crates/substreams-helper/.gitignore
vendored
Normal file
1
substreams/crates/substreams-helper/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
Cargo.lock
|
||||
29
substreams/crates/substreams-helper/Cargo.toml
Normal file
29
substreams/crates/substreams-helper/Cargo.toml
Normal file
@@ -0,0 +1,29 @@
|
||||
[package]
|
||||
name = "substreams-helper"
|
||||
description = "Substreams Helper Crate - by Messari"
|
||||
version = "0.0.2"
|
||||
edition = "2021"
|
||||
license = "MIT"
|
||||
homepage = "https://messari.io/"
|
||||
repository = "https://github.com/messari/substreams/substreams-helper"
|
||||
|
||||
[dependencies]
|
||||
ethabi.workspace = true
|
||||
hex.workspace = true
|
||||
hex-literal.workspace = true
|
||||
prost.workspace = true
|
||||
prost-types.workspace = true
|
||||
num-bigint = "0.4"
|
||||
bigdecimal = "0.3"
|
||||
pad = "0.1"
|
||||
tiny-keccak = { version = "2.0", features = ["keccak"] }
|
||||
substreams = { workspace = true }
|
||||
substreams-ethereum = { workspace = true }
|
||||
thiserror = "1.0.37"
|
||||
downcast-rs = "1.2.0"
|
||||
substreams-entity-change = "1.1.0"
|
||||
base64 = "0.13.0"
|
||||
|
||||
|
||||
[build-dependencies]
|
||||
anyhow.workspace = true
|
||||
7
substreams/crates/substreams-helper/Makefile
Normal file
7
substreams/crates/substreams-helper/Makefile
Normal file
@@ -0,0 +1,7 @@
|
||||
.PHONY: build
|
||||
build:
|
||||
cargo build --target wasm32-unknown-unknown --release
|
||||
|
||||
.PHONY: publish
|
||||
publish:
|
||||
cargo publish --target wasm32-unknown-unknown
|
||||
4
substreams/crates/substreams-helper/rust-toolchain.toml
Normal file
4
substreams/crates/substreams-helper/rust-toolchain.toml
Normal file
@@ -0,0 +1,4 @@
|
||||
[toolchain]
|
||||
channel = "1.75.0"
|
||||
components = [ "rustfmt" ]
|
||||
targets = [ "wasm32-unknown-unknown" ]
|
||||
69
substreams/crates/substreams-helper/src/common.rs
Normal file
69
substreams/crates/substreams-helper/src/common.rs
Normal file
@@ -0,0 +1,69 @@
|
||||
use ethabi::ethereum_types::Address;
|
||||
|
||||
use substreams::store::{
|
||||
StoreGet, StoreGetBigDecimal, StoreGetBigInt, StoreGetInt64, StoreGetProto, StoreGetRaw,
|
||||
StoreGetString,
|
||||
};
|
||||
|
||||
use crate::hex::Hexable;
|
||||
|
||||
/// HasAddresser is a trait that a few functionalities in this crate depend on.
|
||||
/// Every time we need to filter something by address (events emmited by a set of addresses,
|
||||
/// storage changes occurring on certain contracts, etc) you'll likely need
|
||||
/// to provide a HasAddresser.
|
||||
///
|
||||
/// HasAddresser has been implemented already for all substreams::store's for convenience.
|
||||
/// So if you know a given store module contains the list of addresses you want to filter by
|
||||
/// you can pass it directly as a HasAddresser. In this case, the addresses need to be the store key
|
||||
/// hex encoded as a string including the leading 0x. The value of the store is ignored.
|
||||
pub trait HasAddresser {
|
||||
fn has_address(&self, key: Address) -> bool;
|
||||
}
|
||||
|
||||
impl HasAddresser for Vec<Address> {
|
||||
fn has_address(&self, key: Address) -> bool {
|
||||
self.contains(&key)
|
||||
}
|
||||
}
|
||||
|
||||
impl HasAddresser for StoreGetString {
|
||||
fn has_address(&self, key: Address) -> bool {
|
||||
self.get_last(key.to_hex()).is_some()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Default + prost::Message> HasAddresser for StoreGetProto<T> {
|
||||
fn has_address(&self, key: Address) -> bool {
|
||||
self.get_last(key.to_hex()).is_some()
|
||||
}
|
||||
}
|
||||
|
||||
impl HasAddresser for StoreGetRaw {
|
||||
fn has_address(&self, key: Address) -> bool {
|
||||
self.get_last(key.to_hex()).is_some()
|
||||
}
|
||||
}
|
||||
|
||||
impl HasAddresser for StoreGetBigInt {
|
||||
fn has_address(&self, key: Address) -> bool {
|
||||
self.get_last(key.to_hex()).is_some()
|
||||
}
|
||||
}
|
||||
|
||||
impl HasAddresser for StoreGetBigDecimal {
|
||||
fn has_address(&self, key: Address) -> bool {
|
||||
self.get_last(key.to_hex()).is_some()
|
||||
}
|
||||
}
|
||||
|
||||
impl HasAddresser for StoreGetInt64 {
|
||||
fn has_address(&self, key: Address) -> bool {
|
||||
self.get_last(key.to_hex()).is_some()
|
||||
}
|
||||
}
|
||||
|
||||
impl HasAddresser for Address {
|
||||
fn has_address(&self, key: Address) -> bool {
|
||||
key == *self
|
||||
}
|
||||
}
|
||||
103
substreams/crates/substreams-helper/src/event_handler.rs
Normal file
103
substreams/crates/substreams-helper/src/event_handler.rs
Normal file
@@ -0,0 +1,103 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use ethabi::ethereum_types::Address;
|
||||
use substreams_ethereum::{
|
||||
pb::eth::v2::{self as eth},
|
||||
Event,
|
||||
};
|
||||
|
||||
use crate::common::HasAddresser;
|
||||
|
||||
/// Utility struct to easily filter events and assign them handlers.
|
||||
///
|
||||
/// Usage:
|
||||
/// ```
|
||||
/// let eh = EventHandler::new(&block);
|
||||
/// eh.filter_by_address(store); // This is optional, if omitted it will handle all events that match the type, independently of the emitting contract.
|
||||
/// eh.on::<Transfer, _>(&mut on_transfer);
|
||||
/// eh.on::<Approval, _>(&mut on_approval);
|
||||
/// eh.on::<Mint, _>(&mut on_mint);
|
||||
/// eh.on::<Burn, _>(&mut on_burn);
|
||||
/// eh.handle_events(); // this will run all handlers
|
||||
/// ```
|
||||
///
|
||||
/// You'll likely want to mutate some value from the handlers that is in the current scope.
|
||||
/// For that, make your handlers be closures, that close over the variable you want to mutate, and
|
||||
/// have the whole EventHandler block of code in its own scope (either by wrapping it in an aux
|
||||
/// function or by wrapping it in {...})
|
||||
///
|
||||
/// Like so:
|
||||
/// ```
|
||||
/// let mut balances : Vec<Balance> = vec![];
|
||||
/// {
|
||||
/// let mut on_transfer = |/*...*/| {
|
||||
/// // this handler modifies `balances`
|
||||
/// balances.push(some_balance);
|
||||
/// };
|
||||
/// let eh = EventHandler::new(&block);
|
||||
/// eh.on::<Transfer, _>(&mut on_transfer);
|
||||
/// eh.handle_events();
|
||||
/// }
|
||||
///
|
||||
/// // do whatever else with `balances` here.
|
||||
/// ```
|
||||
pub struct EventHandler<'a> {
|
||||
block: &'a eth::Block,
|
||||
#[allow(clippy::type_complexity)]
|
||||
handlers: HashMap<&'static str, Box<dyn FnMut(ð::Log, ð::TransactionTrace) + 'a>>,
|
||||
addresses: Option<Box<dyn HasAddresser + 'a>>,
|
||||
}
|
||||
|
||||
impl<'a> EventHandler<'a> {
|
||||
pub fn new(block: &'a eth::Block) -> Self {
|
||||
Self { block, handlers: HashMap::new(), addresses: None }
|
||||
}
|
||||
|
||||
/// Sets the HasAddresser as a filter for which events to handle.
|
||||
/// Only one at a time can be set. Setting it twice will remove the first one.
|
||||
/// Addresses found in the `HasAddresser` will be the ones we'll handle events from.
|
||||
pub fn filter_by_address(&mut self, addresser: impl HasAddresser + 'a) {
|
||||
self.addresses = Some(Box::new(addresser));
|
||||
}
|
||||
|
||||
/// Registers a handler to be run on a given event. The handler should have the signature:
|
||||
/// `|ev: SomeEvent, tx: &pbeth::v2::TransactionTrace, log: &pbeth::v2::Log|`.
|
||||
/// You can only assign one handler to each Event type.
|
||||
/// Handlers are keyed by the name of the event they are handling, so be careful to not assign
|
||||
/// handlers for 2 different events named equal.
|
||||
pub fn on<E: Event, F>(&mut self, mut handler: F)
|
||||
where
|
||||
F: FnMut(E, ð::TransactionTrace, ð::Log) + 'a,
|
||||
{
|
||||
self.handlers.insert(
|
||||
E::NAME,
|
||||
Box::new(move |log: ð::Log, tx: ð::TransactionTrace| {
|
||||
if let Some(event) = E::match_and_decode(log) {
|
||||
handler(event, tx, log);
|
||||
}
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
/// Will run all registered handlers for all events present on the block that match the given
|
||||
/// filters. You'll likely want to run this just once.
|
||||
pub fn handle_events(&mut self) {
|
||||
// Here we don't need to filter out failed transactions because logs only exist for
|
||||
// successful ones.
|
||||
for log in self.block.logs() {
|
||||
if self.addresses.is_some() &&
|
||||
!&self
|
||||
.addresses
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.has_address(Address::from_slice(log.log.address.as_slice()))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
for handler in self.handlers.values_mut() {
|
||||
handler(log.log, log.receipt.transaction);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
20
substreams/crates/substreams-helper/src/hex.rs
Normal file
20
substreams/crates/substreams-helper/src/hex.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
use ethabi::ethereum_types::Address;
|
||||
use substreams::Hex;
|
||||
|
||||
pub trait Hexable {
|
||||
fn to_hex(&self) -> String;
|
||||
}
|
||||
|
||||
impl Hexable for Vec<u8> {
|
||||
fn to_hex(&self) -> String {
|
||||
let mut str = Hex::encode(self);
|
||||
str.insert_str(0, "0x");
|
||||
str
|
||||
}
|
||||
}
|
||||
|
||||
impl Hexable for Address {
|
||||
fn to_hex(&self) -> String {
|
||||
self.as_bytes().to_vec().to_hex()
|
||||
}
|
||||
}
|
||||
5
substreams/crates/substreams-helper/src/lib.rs
Normal file
5
substreams/crates/substreams-helper/src/lib.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
pub mod common;
|
||||
|
||||
pub mod event_handler;
|
||||
pub mod hex;
|
||||
pub mod storage_change;
|
||||
14
substreams/crates/substreams-helper/src/storage_change.rs
Normal file
14
substreams/crates/substreams-helper/src/storage_change.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
use substreams_ethereum::pb::eth::v2::StorageChange;
|
||||
|
||||
pub trait StorageChangesFilter {
|
||||
fn filter_by_address(&self, contract_addr: &[u8; 20]) -> Vec<&StorageChange>;
|
||||
}
|
||||
|
||||
impl StorageChangesFilter for Vec<StorageChange> {
|
||||
fn filter_by_address(&self, contract_addr: &[u8; 20]) -> Vec<&StorageChange> {
|
||||
return self
|
||||
.iter()
|
||||
.filter(|change| change.address == contract_addr)
|
||||
.collect();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user