From 56d22c03609b2c9b9ce50215c26eed8a1245b513 Mon Sep 17 00:00:00 2001 From: Georgios Konstantopoulos Date: Tue, 2 Jun 2020 13:58:48 +0300 Subject: [PATCH] fix examples --- ethers-providers/src/lib.rs | 3 --- ethers-providers/src/provider.rs | 17 +++++++---------- ethers-signers/src/client.rs | 7 ++++++- ethers/examples/contract.rs | 10 +++++----- ethers/examples/ens.rs | 9 +++++---- ethers/examples/get_logs.rs | 4 ++-- ethers/examples/local_signer.rs | 7 ++++--- ethers/examples/sign.rs | 2 +- ethers/examples/transfer_eth.rs | 4 ++-- 9 files changed, 32 insertions(+), 31 deletions(-) diff --git a/ethers-providers/src/lib.rs b/ethers-providers/src/lib.rs index fef9164c..1e0ee5c9 100644 --- a/ethers-providers/src/lib.rs +++ b/ethers-providers/src/lib.rs @@ -11,9 +11,6 @@ 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; - #[async_trait] /// Trait which must be implemented by data transports to be used with the Ethereum /// JSON-RPC provider. diff --git a/ethers-providers/src/provider.rs b/ethers-providers/src/provider.rs index 83c00d32..2f50973e 100644 --- a/ethers-providers/src/provider.rs +++ b/ethers-providers/src/provider.rs @@ -25,9 +25,10 @@ pub struct Provider

(P, Option

); #[derive(Debug, Error)] /// An error thrown when making a call to the provider pub enum ProviderError { - #[error(transparent)] // dyn dispatch the error to avoid generic return types - JsonRpcClientError(#[from] Box), + #[error(transparent)] + JsonRpcClientError(#[from] Box), + #[error("ens name not found: {0}")] EnsError(String), } @@ -357,13 +358,12 @@ impl TryFrom<&str> for Provider { mod ens_tests { use super::*; + const INFURA: &str = "https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27"; + #[tokio::test] // Test vector from: https://docs.ethers.io/ethers.js/v5-beta/api-providers.html#id2 async fn mainnet_resolve_name() { - let provider = Provider::::try_from( - "https://mainnet.infura.io/v3/9408f47dedf04716a03ef994182cf150", - ) - .unwrap(); + let provider = Provider::::try_from(INFURA).unwrap(); let addr = provider .resolve_name("registrar.firefly.eth") @@ -389,10 +389,7 @@ mod ens_tests { #[tokio::test] // Test vector from: https://docs.ethers.io/ethers.js/v5-beta/api-providers.html#id2 async fn mainnet_lookup_address() { - let provider = Provider::::try_from( - "https://mainnet.infura.io/v3/9408f47dedf04716a03ef994182cf150", - ) - .unwrap(); + let provider = Provider::::try_from(INFURA).unwrap(); let name = provider .lookup_address("6fC21092DA55B392b045eD78F4732bff3C580e2c".parse().unwrap()) diff --git a/ethers-signers/src/client.rs b/ethers-signers/src/client.rs index 93ef8ef9..3306b0fd 100644 --- a/ethers-signers/src/client.rs +++ b/ethers-signers/src/client.rs @@ -30,7 +30,7 @@ pub enum ClientError { ProviderError(#[from] ProviderError), #[error(transparent)] - SignerError(#[from] Box), + SignerError(#[from] Box), #[error("ens name not found: {0}")] EnsError(String), @@ -123,6 +123,11 @@ where pub fn signer_unchecked(&self) -> &S { self.signer.as_ref().expect("no signer is configured") } + + pub fn with_signer(mut self, signer: S) -> Self { + self.signer = Some(signer); + self + } } // Abuse Deref to use the Provider's methods without re-writing everything. diff --git a/ethers/examples/contract.rs b/ethers/examples/contract.rs index 2e97c39e..9595f472 100644 --- a/ethers/examples/contract.rs +++ b/ethers/examples/contract.rs @@ -5,7 +5,7 @@ use ethers::{ }; use std::convert::TryFrom; -// Generate the contract bindings by providing the ABI +// Generate the type-safe contract bindings by providing the ABI abigen!( SimpleContract, r#"[{"inputs":[{"internalType":"string","name":"value","type":"string"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"author","type":"address"},{"indexed":false,"internalType":"string","name":"oldValue","type":"string"},{"indexed":false,"internalType":"string","name":"newValue","type":"string"}],"name":"ValueChanged","type":"event"},{"inputs":[],"name":"getValue","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","constant": true, "type":"function"},{"inputs":[{"internalType":"string","name":"value","type":"string"}],"name":"setValue","outputs":[],"stateMutability":"nonpayable","type":"function"}]"#, @@ -28,14 +28,14 @@ async fn main() -> Result<()> { .spawn(); // 3. instantiate our wallet - let wallet = "380eb0f3d505f087e438eca80bc4df9a7faa24f868e69fc0440261a0fc0567dc" - .parse::()?; + let wallet = + "380eb0f3d505f087e438eca80bc4df9a7faa24f868e69fc0440261a0fc0567dc".parse::()?; // 4. connect to the network - let provider = HttpProvider::try_from(url.as_str())?; + let provider = Provider::::try_from(url.as_str())?; // 5. instantiate the client with the wallet - let client = wallet.connect(&provider); + let client = wallet.connect(provider); // 6. create a factory which will be used to deploy instances of the contract let factory = ContractFactory::new(&client, &contract.abi, &contract.bytecode); diff --git a/ethers/examples/ens.rs b/ethers/examples/ens.rs index 6aa24886..0a342944 100644 --- a/ethers/examples/ens.rs +++ b/ethers/examples/ens.rs @@ -5,13 +5,14 @@ use std::convert::TryFrom; #[tokio::main] async fn main() -> Result<()> { // connect to the network - let provider = - HttpProvider::try_from("https://mainnet.infura.io/v3/9408f47dedf04716a03ef994182cf150")?; + let provider = Provider::::try_from( + "https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27", + )?; // create a wallet and connect it to the provider let client = "dcf2cbdd171a21c480aa7f53d77f31bb102282b3ff099c78e3118b37348c72f7" - .parse::()? - .connect(&provider); + .parse::()? + .connect(provider); // craft the transaction let tx = TransactionRequest::new().to("vitalik.eth").value(100_000); diff --git a/ethers/examples/get_logs.rs b/ethers/examples/get_logs.rs index 142ef659..dd9d5108 100644 --- a/ethers/examples/get_logs.rs +++ b/ethers/examples/get_logs.rs @@ -1,11 +1,11 @@ use anyhow::Result; -use ethers::{networks::Any, prelude::*}; +use ethers::prelude::*; use std::convert::TryFrom; #[tokio::main] async fn main() -> Result<()> { // connect to the network - let provider = HttpProvider::::try_from("http://localhost:8545")?; + let provider = Provider::::try_from("http://localhost:8545")?; let filter = Filter::new() .address_str("f817796F60D268A36a57b8D2dF1B97B14C0D0E1d")? diff --git a/ethers/examples/local_signer.rs b/ethers/examples/local_signer.rs index 09a8bb26..ad4a6ae1 100644 --- a/ethers/examples/local_signer.rs +++ b/ethers/examples/local_signer.rs @@ -10,15 +10,16 @@ async fn main() -> Result<()> { .port(port) .mnemonic("abstract vacuum mammal awkward pudding scene penalty purchase dinner depart evoke puzzle") .spawn(); + // this private key belongs to the above mnemonic - let wallet: MainnetWallet = + let wallet: Wallet = "380eb0f3d505f087e438eca80bc4df9a7faa24f868e69fc0440261a0fc0567dc".parse()?; // connect to the network - let provider = HttpProvider::try_from(url.as_str())?; + let provider = Provider::::try_from(url.as_str())?; // connect the wallet to the provider - let client = wallet.connect(&provider); + let client = wallet.connect(provider); // craft the transaction let tx = TransactionRequest::new() diff --git a/ethers/examples/sign.rs b/ethers/examples/sign.rs index 3df2d55f..5897bf70 100644 --- a/ethers/examples/sign.rs +++ b/ethers/examples/sign.rs @@ -2,7 +2,7 @@ use ethers::prelude::*; fn main() { let message = "Some data"; - let wallet = MainnetWallet::new(&mut rand::thread_rng()); + let wallet = Wallet::new(&mut rand::thread_rng()); // sign a message let signature = wallet.sign_message(message); diff --git a/ethers/examples/transfer_eth.rs b/ethers/examples/transfer_eth.rs index eac06309..c195be23 100644 --- a/ethers/examples/transfer_eth.rs +++ b/ethers/examples/transfer_eth.rs @@ -1,5 +1,5 @@ use anyhow::Result; -use ethers::{networks::Any, prelude::*, utils::GanacheBuilder}; +use ethers::{prelude::*, utils::GanacheBuilder}; use std::convert::TryFrom; #[tokio::main] @@ -9,7 +9,7 @@ async fn main() -> Result<()> { let _ganache = GanacheBuilder::new().port(port).spawn(); // connect to the network - let provider = HttpProvider::::try_from(url.as_str())?; + let provider = Provider::::try_from(url.as_str())?; let accounts = provider.get_accounts().await?; let from = accounts[0];