alloy-erc20
ERC20 is a Rust libary relying on Alloy allowing to interact with ERC-20 contracts.
Installation
Add alloy-erc20 to your Cargo.toml.
= { = "https://github.com/leruaa/alloy-erc20" }
Features
- A basic
Tokenstruct and associated extensions methods on Alloy'sProvider, allowing to retrieve token decimals, and compute balances asBigDecimalfromU256. - A
TokenStoretrait, and aBasicTokenStoreimpl, allowing to cacheTokens in memory. - A
LazyTokenstruct, acting as a wrapper around Alloy contract instance, lazily retrievingname,symbol,decimalsandtotalSupplyfrom the blockchain.
Getting started
Basic usage
let provider = new.on_http;
// Just retrieve a token from its address
let dai = provider
.retrieve_token
.await?;
// Compute a balance as a BigDecimal from a U256
let balance = dai.get_balance;
Store
let provider = new.on_http;
let store = new;
// Just retrieve a token from its address, and add it to the store
let dai = provider
.get_token
.await?;
// ...
// Now the token can be retrieved from its symbol or address from the store
let dai = store.get.unwrap;
// Compute a balance as a BigDecimal from a U256
let balance = dai.get_balance;
Lazy
let provider = new.on_http;
let dai = new;
// lazily query the network for the decimals and cache the result
let total_supply = dai.decimals?;