From 94433216ffcbec07c9ec9c231d696065bd4cc29b Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Fri, 18 Mar 2022 14:22:57 +0100 Subject: [PATCH] feat: add deploy example --- examples/contract_with_abi.rs | 2 +- examples/contract_with_abi_and_bytecode.rs | 36 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 examples/contract_with_abi_and_bytecode.rs diff --git a/examples/contract_with_abi.rs b/examples/contract_with_abi.rs index c151649ee..743b3504b 100644 --- a/examples/contract_with_abi.rs +++ b/examples/contract_with_abi.rs @@ -3,7 +3,7 @@ use eyre::Result; 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 +// definition abigen!( SimpleContract, "./examples/contract_abi.json", diff --git a/examples/contract_with_abi_and_bytecode.rs b/examples/contract_with_abi_and_bytecode.rs new file mode 100644 index 000000000..deda6c41e --- /dev/null +++ b/examples/contract_with_abi_and_bytecode.rs @@ -0,0 +1,36 @@ +use ethers::{prelude::*, utils::Ganache}; +use eyre::Result; +use std::{convert::TryFrom, sync::Arc, time::Duration}; + +// Generate the type-safe contract bindings by providing the json artifact +// *Note*: this requires a `bytecode` and `abi` object in the `greeter.json` artifact: +// `{"abi": [..], "bin": "..."}` or `{"abi": [..], "bytecode": {"object": "..."}}` +// this will embedd the bytecode in a variable `GREETER_BYTECODE` +abigen!(Greeter, "ethers-contract/tests/solidity-contracts/greeter.json",); + +#[tokio::main] +async fn main() -> Result<()> { + // 1. compile the contract (note this requires that you are inside the `examples` directory) and + // launch ganache + let ganache = Ganache::new().spawn(); + + // 2. instantiate our wallet + let wallet: LocalWallet = ganache.keys()[0].clone().into(); + + // 3. connect to the network + let provider = + Provider::::try_from(ganache.endpoint())?.interval(Duration::from_millis(10u64)); + + // 4. instantiate the client with the wallet + let client = Arc::new(SignerMiddleware::new(provider, wallet)); + + // 5. deploy contract, note the `legacy` call required for non EIP-1559 + let greeter_contract = + Greeter::deploy(client, "Hello World!".to_string()).unwrap().legacy().send().await.unwrap(); + + // 6. call contract function + let greeting = greeter_contract.greet().call().await.unwrap(); + assert_eq!("Hello World!", greeting); + + Ok(()) +}