2 releases
0.1.1 | Jun 19, 2024 |
---|---|
0.1.0 | Jun 17, 2024 |
#1858 in Magic Beans
316 downloads per month
23KB
520 lines
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
.
alloy-erc20 = { git = "https://github.com/leruaa/alloy-erc20" }
Features
- A basic
Token
struct and associated extensions methods on Alloy'sProvider
, allowing to retrieve token decimals, and compute balances asBigDecimal
fromU256
. - A
TokenStore
trait, and aBasicTokenStore
impl, allowing to cacheToken
s in memory. - A
LazyToken
struct, acting as a wrapper around Alloy contract instance, lazily retrievingname
,symbol
,decimals
andtotalSupply
from the blockchain.
Getting started
Basic usage
let provider = ProviderBuilder::new().on_http("https://...".parse()?);
// Just retrieve a token from its address
let dai = provider
.retrieve_token(address!("6B175474E89094C44Da98b954EedeAC495271d0F"))
.await?;
// Compute a balance as a BigDecimal from a U256
let balance = dai.get_balance(U256::from(1000000000000_u64));
Store
let provider = ProviderBuilder::new().on_http("https://...".parse()?);
let store = BasicTokenStore::new();
// Just retrieve a token from its address, and add it to the store
let dai = provider
.get_token(address!("6B175474E89094C44Da98b954EedeAC495271d0F").into(), &store)
.await?;
// ...
// Now the token can be retrieved from its symbol or address from the store
let dai = store.get(1, TokenId::Symbol("DAI".to_string())).unwrap();
// Compute a balance as a BigDecimal from a U256
let balance = dai.get_balance(U256::from(1000000000000_u64));
Lazy
let provider = ProviderBuilder::new().on_http("https://...".parse()?);
let dai = LazyToken::new(address!("6B175474E89094C44Da98b954EedeAC495271d0F"), provider);
// lazily query the network for the decimals and cache the result
let total_supply = dai.decimals()?;
Dependencies
~28–42MB
~815K SLoC