Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

ethers-addressbook crate #769

Merged
merged 7 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Complete Ethereum library and wallet implementation in Rust.

[workspace]
members = [
"ethers-addressbook",
"ethers-contract",
"ethers-providers",
"ethers-signers",
Expand All @@ -25,6 +26,7 @@ members = [
]

default-members = [
"ethers-addressbook",
"ethers-contract",
"ethers-providers",
"ethers-signers",
Expand Down Expand Up @@ -80,6 +82,7 @@ solc-tests = ["ethers-solc/tests"]
solc-sha2-asm = ["ethers-solc/asm"]

[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-core = { version = "^0.6.0", default-features = false, path = "./ethers-core" }
ethers-providers = { version = "^0.6.0", default-features = false, path = "./ethers-providers" }
Expand Down
11 changes: 11 additions & 0 deletions ethers-addressbook/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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" }
13 changes: 13 additions & 0 deletions ethers-addressbook/src/contracts/contracts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"dai": {
"addresses": {
"mainnet": "0x6b175474e89094c44da98b954eedeac495271d0f",
"rinkeby": "0x8ad3aa5d5ff084307d28c8f514d7a193b2bfe725"
}
},
"usdc": {
"addresses": {
"mainnet": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"
}
}
}
43 changes: 43 additions & 0 deletions ethers-addressbook/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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());
}
}
8 changes: 5 additions & 3 deletions ethers-core/src/types/chain.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use std::fmt;
use serde::Deserialize;
use thiserror::Error;

use std::{fmt, str::FromStr};

use crate::types::U256;
use std::str::FromStr;

#[derive(Debug, Clone, Error)]
#[error("Failed to parse chain: {0}")]
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 {
Mainnet,
Ropsten,
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@
//! [`abi`]: core::abi
//! [`types`]: core::types

/// Address book consisting of frequently used contracts
pub mod addressbook {
pub use ethers_addressbook::*;
}

#[doc = include_str!("../assets/CONTRACT_README.md")]
pub mod contract {
pub use ethers_contract::*;
Expand Down Expand Up @@ -124,6 +129,8 @@ pub use crate::core::{abi, types, utils};
/// Easy imports of frequently used type definitions and traits
#[doc(hidden)]
pub mod prelude {
pub use super::addressbook::*;

pub use super::contract::*;

pub use super::core::{types::*, *};
Expand Down