Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
feat: add deploy example
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse committed Mar 18, 2022
1 parent 8edf096 commit 9443321
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/contract_with_abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
36 changes: 36 additions & 0 deletions examples/contract_with_abi_and_bytecode.rs
Original file line number Diff line number Diff line change
@@ -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::<Http>::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(())
}

0 comments on commit 9443321

Please sign in to comment.