From c08f8ba876b7f8c422ee70b25b7ffa4b4c75f840 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Fri, 5 Nov 2021 18:58:03 +0100 Subject: [PATCH] fix: failing test and check --all-targets in ci (#560) * fix: update failing example * ci: check all targets --- .github/workflows/ci.yml | 16 ++++++++ ethers-providers/src/transports/common.rs | 1 + ethers-solc/src/artifacts.rs | 49 ++++++++++++++++++++++- examples/contract_with_abi.rs | 21 +++++----- examples/ipc.rs | 10 ++--- examples/ledger.rs | 2 +- examples/yubi.rs | 2 +- 7 files changed, 82 insertions(+), 19 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ad778f41..0b99238b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,6 +14,22 @@ env: RINKEBY_PRIVATE_KEY: ${{ secrets.RINKEBY_PRIVATE_KEY }} jobs: + check: + name: check all + runs-on: ubuntu-latest + steps: + - name: Install stable toolchain + uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + - uses: Swatinem/rust-cache@v1 + with: + cache-on-failure: true + - name: cargo check + run: cargo check -p ethers --all-targets --all-features + tests: name: ethereum tests runs-on: ubuntu-latest diff --git a/ethers-providers/src/transports/common.rs b/ethers-providers/src/transports/common.rs index 50fe280a..85aec551 100644 --- a/ethers-providers/src/transports/common.rs +++ b/ethers-providers/src/transports/common.rs @@ -84,6 +84,7 @@ impl ResponseData { impl ResponseData { /// Encode the error to json value if it is an error + #[allow(dead_code)] pub fn into_value(self) -> serde_json::Result { match self { ResponseData::Success { result } => Ok(result), diff --git a/ethers-solc/src/artifacts.rs b/ethers-solc/src/artifacts.rs index 64f65d9b..cc922679 100644 --- a/ethers-solc/src/artifacts.rs +++ b/ethers-solc/src/artifacts.rs @@ -393,6 +393,15 @@ impl CompilerOutput { OutputDiagnostics { errors: &self.errors, ignored_error_codes } } + /// Finds the first contract with the given name + pub fn find(&self, contract: impl AsRef) -> Option { + let contract = contract.as_ref(); + self.contracts + .values() + .find_map(|contracts| contracts.get(contract)) + .map(CompactContractRef::from) + } + /// Given the contract file's path and the contract's name, tries to return the contract's /// bytecode, runtime bytecode, and abi pub fn get(&self, path: &str, contract: &str) -> Option { @@ -474,6 +483,24 @@ pub struct CompactContract { pub bin_runtime: Option, } +impl CompactContract { + /// Returns the contents of this type as a single + pub fn into_parts(self) -> (Option, Option, Option) { + (self.abi, self.bin, self.bin_runtime) + } + + /// Returns the individual parts of this contract. + /// + /// If the values are `None`, then `Default` is returned. + pub fn into_parts_or_default(self) -> (Abi, Bytes, Bytes) { + ( + self.abi.unwrap_or_default(), + self.bin.unwrap_or_default(), + self.bin_runtime.unwrap_or_default(), + ) + } +} + impl From for CompactContract { fn from(c: Contract) -> Self { let (bin, bin_runtime) = if let Some(evm) = c.evm { @@ -486,8 +513,14 @@ impl From for CompactContract { } } +impl<'a> From> for CompactContract { + fn from(c: CompactContractRef<'a>) -> Self { + Self { abi: c.abi.cloned(), bin: c.bin.cloned(), bin_runtime: c.bin_runtime.cloned() } + } +} + /// Helper type to serialize while borrowing from `Contract` -#[derive(Clone, Debug, Serialize)] +#[derive(Copy, Clone, Debug, Serialize)] pub struct CompactContractRef<'a> { pub abi: Option<&'a Abi>, #[serde(default, skip_serializing_if = "Option::is_none")] @@ -496,6 +529,20 @@ pub struct CompactContractRef<'a> { pub bin_runtime: Option<&'a Bytes>, } +impl<'a> CompactContractRef<'a> { + /// Clones the referenced values and returns as tuples + pub fn into_parts(self) -> (Option, Option, Option) { + CompactContract::from(self).into_parts() + } + + /// Returns the individual parts of this contract. + /// + /// If the values are `None`, then `Default` is returned. + pub fn into_parts_or_default(self) -> (Abi, Bytes, Bytes) { + CompactContract::from(self).into_parts_or_default() + } +} + impl<'a> From<&'a Contract> for CompactContractRef<'a> { fn from(c: &'a Contract) -> Self { let (bin, bin_runtime) = if let Some(ref evm) = c.evm { diff --git a/examples/contract_with_abi.rs b/examples/contract_with_abi.rs index 074f9f73..83af2d9f 100644 --- a/examples/contract_with_abi.rs +++ b/examples/contract_with_abi.rs @@ -1,9 +1,6 @@ use anyhow::Result; -use ethers::{ - prelude::*, - utils::{compile_and_launch_ganache, Ganache, Solc}, -}; -use std::{convert::TryFrom, sync::Arc, time::Duration}; +use ethers::{prelude::*, utils::Ganache}; +use std::{convert::TryFrom, path::Path, sync::Arc, time::Duration}; // Generate the type-safe contract bindings by providing the ABI // definition in human readable format @@ -17,11 +14,14 @@ abigen!( async fn main() -> Result<()> { // 1. compile the contract (note this requires that you are inside the `examples` directory) and // launch ganache - let (compiled, ganache) = - compile_and_launch_ganache(Solc::new("**/contract.sol"), Ganache::new()).await?; + let ganache = Ganache::new().spawn(); - let contract = compiled.get("SimpleStorage").expect("could not find contract"); - dbg!("OK"); + // set the path to the contract, `CARGO_MANIFEST_DIR` points to the directory containing the + // manifest of `ethers`. which will be `../` relative to this file + let source = Path::new(&env!("CARGO_MANIFEST_DIR")).join("examples/contract.sol"); + let compiled = Solc::default().compile_source(source).expect("Could not compile contracts"); + let (abi, bytecode, _runtime_bytecode) = + compiled.find("SimpleStorage").expect("could not find contract").into_parts_or_default(); // 2. instantiate our wallet let wallet: LocalWallet = ganache.keys()[0].clone().into(); @@ -35,8 +35,7 @@ async fn main() -> Result<()> { let client = Arc::new(client); // 5. create a factory which will be used to deploy instances of the contract - let factory = - ContractFactory::new(contract.abi.clone(), contract.bytecode.clone(), client.clone()); + let factory = ContractFactory::new(abi, bytecode, client.clone()); // 6. deploy it with the constructor arguments let contract = factory.deploy("initial value".to_string())?.legacy().send().await?; diff --git a/examples/ipc.rs b/examples/ipc.rs index 6bcf8e34..9f747635 100644 --- a/examples/ipc.rs +++ b/examples/ipc.rs @@ -1,11 +1,11 @@ -use ethers::prelude::*; -use std::time::Duration; - #[tokio::main] #[cfg(feature = "ipc")] async fn main() -> anyhow::Result<()> { - let provider = - Provider::connect_ipc("~/.ethereum/geth.ipc").await?.interval(Duration::from_millis(2000)); + use ethers::prelude::*; + + let provider = Provider::connect_ipc("~/.ethereum/geth.ipc") + .await? + .interval(std::time::Duration::from_millis(2000)); let block = provider.get_block_number().await?; println!("Current block: {}", block); let mut stream = provider.watch_blocks().await?.stream(); diff --git a/examples/ledger.rs b/examples/ledger.rs index 65af1d5b..f4205792 100644 --- a/examples/ledger.rs +++ b/examples/ledger.rs @@ -1,6 +1,6 @@ #[tokio::main] #[cfg(feature = "ledger")] -async fn main() -> anyhow::Result<()> { +async fn main() -> Result<(), Box> { use ethers::{prelude::*, utils::parse_ether}; // Connect over websockets diff --git a/examples/yubi.rs b/examples/yubi.rs index 3e54709e..4cc8ddac 100644 --- a/examples/yubi.rs +++ b/examples/yubi.rs @@ -1,6 +1,6 @@ #[tokio::main] #[cfg(feature = "yubi")] -async fn main() -> anyhow::Result<()> { +async fn main() -> Result<(), Box> { use ethers::{prelude::*, utils::parse_ether}; use yubihsm::{Connector, Credentials, UsbConfig};