remove type-safe networks

nobody really wants this
This commit is contained in:
Georgios Konstantopoulos 2020-06-02 00:58:21 +03:00
parent 1c6b067bb9
commit 6181943485
No known key found for this signature in database
GPG Key ID: FA607837CD26EDBC
4 changed files with 13 additions and 60 deletions

View File

@ -1,10 +1,16 @@
/// [Ethereum Name Service](https://docs.ens.domains/) support
// Adapted from https://github.com/hhatto/rust-ens/blob/master/src/lib.rs
use ethers_core::{
types::{Address, NameOrAddress, Selector, TransactionRequest, H256},
types::{Address, NameOrAddress, Selector, TransactionRequest, H160, H256},
utils::keccak256,
};
// 0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e
pub const ENS_ADDRESS: Address = 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,
]);
// Selectors
const ENS_REVERSE_REGISTRAR_DOMAIN: &str = "addr.reverse";

View File

@ -1,8 +1,7 @@
pub mod http;
pub use http::Provider as Http;
mod provider;
pub mod networks;
// ENS support
mod ens;
@ -13,7 +12,7 @@ use std::{error::Error, fmt::Debug};
pub use provider::{Provider, ProviderError};
/// An HTTP provider for interacting with an Ethereum-compatible blockchain
pub type HttpProvider = Provider<http::Provider>;
pub type HttpProvider = Provider<Http>;
#[async_trait]
/// Trait which must be implemented by data transports to be used with the Ethereum

View File

@ -1,41 +0,0 @@
//! 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.
use ethers_core::types::{Address, H160, U64};
/// Trait for specifying network specific metadata, such as the Chain Id or the ENS
/// address.
pub trait Network {
/// The network's Chain Id. If None, then EIP-155 is not used and as a result
/// transactions **will not have replay protection**
const CHAIN_ID: Option<U64>;
/// The network's ENS address.
const ENS_ADDRESS: Option<Address>;
// TODO: Default providers? e.g. `mainnet.infura.io/XXX`?
}
/// Ethereum Mainnet, pre-specified ENS address and ChainID = 1 (for EIP-155)
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Mainnet;
impl Network for Mainnet {
const CHAIN_ID: Option<U64> = Some(U64([1]));
// 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,
]));
}
/// 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_.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Any;
impl Network for Any {
const CHAIN_ID: Option<U64> = None;
const ENS_ADDRESS: Option<Address> = None;
}

View File

@ -1,9 +1,4 @@
use crate::{
ens,
http::Provider as HttpProvider,
networks::{Mainnet, Network},
JsonRpcClient,
};
use crate::{ens, http::Provider as HttpProvider, JsonRpcClient};
use ethers_core::{
abi::{self, Detokenize, ParamType},
@ -41,7 +36,7 @@ pub enum ProviderError {
impl<P> Provider<P>
where
P: JsonRpcClient,
ProviderError: From<<P as JsonRpcClient>::Error> + 'static,
ProviderError: From<<P as JsonRpcClient>::Error>,
{
////// Blockchain Status
//
@ -207,7 +202,7 @@ where
let addr = self
.resolve_name(&ens_name)
.await?
.ok_or(ProviderError::EnsError(ens_name.to_owned()))?;
.ok_or_else(|| ProviderError::EnsError(ens_name.to_owned()))?;
// set the value
tx.to = Some(addr.into())
@ -269,13 +264,7 @@ where
selector: Selector,
) -> Result<Option<T>, ProviderError> {
// Get the ENS address, prioritize the local override variable
let ens_addr = match self.1 {
Some(ens_addr) => ens_addr,
None => match Mainnet::ENS_ADDRESS {
Some(ens_addr) => ens_addr,
None => return Ok(None),
},
};
let ens_addr = self.1.unwrap_or(ens::ENS_ADDRESS);
// first get the resolver responsible for this name
// the call will return a Bytes array which we convert to an address