From 6181943485909932a2dd12cebee0b21c6d158946 Mon Sep 17 00:00:00 2001 From: Georgios Konstantopoulos Date: Tue, 2 Jun 2020 00:58:21 +0300 Subject: [PATCH] remove type-safe networks nobody really wants this --- ethers-providers/src/ens.rs | 8 ++++++- ethers-providers/src/lib.rs | 5 ++-- ethers-providers/src/networks.rs | 41 -------------------------------- ethers-providers/src/provider.rs | 19 ++++----------- 4 files changed, 13 insertions(+), 60 deletions(-) delete mode 100644 ethers-providers/src/networks.rs diff --git a/ethers-providers/src/ens.rs b/ethers-providers/src/ens.rs index 3fddc944..387192aa 100644 --- a/ethers-providers/src/ens.rs +++ b/ethers-providers/src/ens.rs @@ -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"; diff --git a/ethers-providers/src/lib.rs b/ethers-providers/src/lib.rs index fba5124d..43d725a4 100644 --- a/ethers-providers/src/lib.rs +++ b/ethers-providers/src/lib.rs @@ -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; +pub type HttpProvider = Provider; #[async_trait] /// Trait which must be implemented by data transports to be used with the Ethereum diff --git a/ethers-providers/src/networks.rs b/ethers-providers/src/networks.rs deleted file mode 100644 index 36e055bf..00000000 --- a/ethers-providers/src/networks.rs +++ /dev/null @@ -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; - - /// The network's ENS address. - const ENS_ADDRESS: Option
; - - // 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 = Some(U64([1])); - - // 0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e - const ENS_ADDRESS: Option
= 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 = None; - const ENS_ADDRESS: Option
= None; -} diff --git a/ethers-providers/src/provider.rs b/ethers-providers/src/provider.rs index 4f97c360..0bced446 100644 --- a/ethers-providers/src/provider.rs +++ b/ethers-providers/src/provider.rs @@ -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

Provider

where P: JsonRpcClient, - ProviderError: From<

::Error> + 'static, + ProviderError: From<

::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, 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