ethers-rs/crates/ethers-providers/src/networks.rs

42 lines
1.5 KiB
Rust
Raw Normal View History

2020-05-28 16:34:06 +00:00
//! Networks are used inside wallets and providers to ensure replay protection across networks,
//! as well as to allow functions to be called with ENS names instead of Addresses.
2020-05-27 15:43:43 +00:00
use ethers_types::{Address, H160, U64};
2020-05-24 14:41:12 +00:00
2020-05-28 16:34:06 +00:00
/// Trait for specifying network specific metadata, such as the Chain Id or the ENS
/// address.
2020-05-24 14:41:12 +00:00
pub trait Network {
2020-05-28 16:34:06 +00:00
/// The network's Chain Id. If None, then EIP-155 is not used and as a result
/// transactions **will not have replay protection**
2020-05-24 14:41:12 +00:00
const CHAIN_ID: Option<U64>;
2020-05-28 16:34:06 +00:00
/// The network's ENS address.
2020-05-27 15:43:43 +00:00
const ENS_ADDRESS: Option<Address>;
2020-05-24 14:41:12 +00:00
// TODO: Default providers? e.g. `mainnet.infura.io/XXX`?
}
2020-05-28 16:34:06 +00:00
/// Ethereum Mainnet, pre-specified ENS address and ChainID = 1 (for EIP-155)
2020-05-24 14:41:12 +00:00
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Mainnet;
impl Network for Mainnet {
const CHAIN_ID: Option<U64> = Some(U64([1]));
2020-05-27 15:43:43 +00:00
// 0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e
const ENS_ADDRESS: Option<Address> = Some(H160([
// cannot set type aliases as constructors
0, 0, 0, 0, 0, 12, 46, 7, 78, 198, 154, 13, 251, 41, 151, 186, 108, 125, 46, 30,
]));
2020-05-24 14:41:12 +00:00
}
2020-05-28 16:34:06 +00:00
/// Any other network, ChainID is not specified so **there is no replay protection when
/// using this network type**. ENS is also not specified, so any calls to the provider's
/// `lookup_address` and `resolve_name` _will fail_.
2020-05-24 14:41:12 +00:00
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
2020-05-27 15:43:43 +00:00
pub struct Any;
2020-05-24 14:41:12 +00:00
2020-05-27 15:43:43 +00:00
impl Network for Any {
2020-05-24 14:41:12 +00:00
const CHAIN_ID: Option<U64> = None;
2020-05-27 15:43:43 +00:00
const ENS_ADDRESS: Option<Address> = None;
2020-05-24 14:41:12 +00:00
}