ethers-addressbook crate (#769)
* feat: macro and token (simplest form) * fix: better structure to fetch token by string symbol * chore: add tokenlist to prelude * fix: from current dir * fix: dir refactoring * fix: clippy * chore: refactor tokenlist to addressbook
This commit is contained in:
parent
eeb2a84df4
commit
2d05ea6234
|
@ -1088,6 +1088,7 @@ version = "0.6.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"bytes",
|
"bytes",
|
||||||
|
"ethers-addressbook",
|
||||||
"ethers-contract",
|
"ethers-contract",
|
||||||
"ethers-core",
|
"ethers-core",
|
||||||
"ethers-etherscan",
|
"ethers-etherscan",
|
||||||
|
@ -1102,6 +1103,16 @@ dependencies = [
|
||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ethers-addressbook"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"ethers-core",
|
||||||
|
"once_cell",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ethers-contract"
|
name = "ethers-contract"
|
||||||
version = "0.6.0"
|
version = "0.6.0"
|
||||||
|
|
|
@ -14,6 +14,7 @@ Complete Ethereum library and wallet implementation in Rust.
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
members = [
|
members = [
|
||||||
|
"ethers-addressbook",
|
||||||
"ethers-contract",
|
"ethers-contract",
|
||||||
"ethers-providers",
|
"ethers-providers",
|
||||||
"ethers-signers",
|
"ethers-signers",
|
||||||
|
@ -25,6 +26,7 @@ members = [
|
||||||
]
|
]
|
||||||
|
|
||||||
default-members = [
|
default-members = [
|
||||||
|
"ethers-addressbook",
|
||||||
"ethers-contract",
|
"ethers-contract",
|
||||||
"ethers-providers",
|
"ethers-providers",
|
||||||
"ethers-signers",
|
"ethers-signers",
|
||||||
|
@ -80,6 +82,7 @@ solc-tests = ["ethers-solc/tests"]
|
||||||
solc-sha2-asm = ["ethers-solc/asm"]
|
solc-sha2-asm = ["ethers-solc/asm"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
ethers-addressbook = { version = "^0.1.0", default-features = false, path = "./ethers-addressbook" }
|
||||||
ethers-contract = { version = "^0.6.0", default-features = false, path = "./ethers-contract" }
|
ethers-contract = { version = "^0.6.0", default-features = false, path = "./ethers-contract" }
|
||||||
ethers-core = { version = "^0.6.0", default-features = false, path = "./ethers-core" }
|
ethers-core = { version = "^0.6.0", default-features = false, path = "./ethers-core" }
|
||||||
ethers-providers = { version = "^0.6.0", default-features = false, path = "./ethers-providers" }
|
ethers-providers = { version = "^0.6.0", default-features = false, path = "./ethers-providers" }
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
[package]
|
||||||
|
name = "ethers-addressbook"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
once_cell = "1.9.0"
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
serde_json = "1.0"
|
||||||
|
|
||||||
|
ethers-core = { path = "../ethers-core" }
|
|
@ -0,0 +1,13 @@
|
||||||
|
{
|
||||||
|
"dai": {
|
||||||
|
"addresses": {
|
||||||
|
"mainnet": "0x6b175474e89094c44da98b954eedeac495271d0f",
|
||||||
|
"rinkeby": "0x8ad3aa5d5ff084307d28c8f514d7a193b2bfe725"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"usdc": {
|
||||||
|
"addresses": {
|
||||||
|
"mainnet": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
use ethers_core::types::{Address, Chain};
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
const CONTRACTS_JSON: &str = include_str!("./contracts/contracts.json");
|
||||||
|
|
||||||
|
static ADDRESSBOOK: Lazy<HashMap<String, Contract>> =
|
||||||
|
Lazy::new(|| serde_json::from_str(CONTRACTS_JSON).unwrap());
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Deserialize)]
|
||||||
|
pub struct Contract {
|
||||||
|
addresses: HashMap<Chain, Address>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Contract {
|
||||||
|
pub fn address(&self, chain: Chain) -> Option<Address> {
|
||||||
|
self.addresses.get(&chain).cloned()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn contract<S: Into<String>>(name: S) -> Option<Contract> {
|
||||||
|
ADDRESSBOOK.get(&name.into()).cloned()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tokens() {
|
||||||
|
assert!(contract("dai").is_some());
|
||||||
|
assert!(contract("usdc").is_some());
|
||||||
|
assert!(contract("rand").is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_addrs() {
|
||||||
|
assert!(contract("dai").unwrap().address(Chain::Mainnet).is_some());
|
||||||
|
assert!(contract("dai").unwrap().address(Chain::MoonbeamDev).is_none());
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,14 +1,16 @@
|
||||||
use std::fmt;
|
use serde::Deserialize;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
|
use std::{fmt, str::FromStr};
|
||||||
|
|
||||||
use crate::types::U256;
|
use crate::types::U256;
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Error)]
|
#[derive(Debug, Clone, Error)]
|
||||||
#[error("Failed to parse chain: {0}")]
|
#[error("Failed to parse chain: {0}")]
|
||||||
pub struct ParseChainError(String);
|
pub struct ParseChainError(String);
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Deserialize)]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
pub enum Chain {
|
pub enum Chain {
|
||||||
Mainnet,
|
Mainnet,
|
||||||
Ropsten,
|
Ropsten,
|
||||||
|
|
|
@ -84,6 +84,11 @@
|
||||||
//! [`abi`]: core::abi
|
//! [`abi`]: core::abi
|
||||||
//! [`types`]: core::types
|
//! [`types`]: core::types
|
||||||
|
|
||||||
|
/// Address book consisting of frequently used contracts
|
||||||
|
pub mod addressbook {
|
||||||
|
pub use ethers_addressbook::*;
|
||||||
|
}
|
||||||
|
|
||||||
#[doc = include_str!("../assets/CONTRACT_README.md")]
|
#[doc = include_str!("../assets/CONTRACT_README.md")]
|
||||||
pub mod contract {
|
pub mod contract {
|
||||||
pub use ethers_contract::*;
|
pub use ethers_contract::*;
|
||||||
|
@ -124,6 +129,8 @@ pub use crate::core::{abi, types, utils};
|
||||||
/// Easy imports of frequently used type definitions and traits
|
/// Easy imports of frequently used type definitions and traits
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub mod prelude {
|
pub mod prelude {
|
||||||
|
pub use super::addressbook::*;
|
||||||
|
|
||||||
pub use super::contract::*;
|
pub use super::contract::*;
|
||||||
|
|
||||||
pub use super::core::{types::*, *};
|
pub use super::core::{types::*, *};
|
||||||
|
|
Loading…
Reference in New Issue