ethers-rs/examples/contract_with_abi_and_bytec...

37 lines
1.4 KiB
Rust
Raw Permalink Normal View History

use ethers::{prelude::*, utils::Anvil};
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 anvil
let anvil = Anvil::new().spawn();
// 2. instantiate our wallet
let wallet: LocalWallet = anvil.keys()[0].clone().into();
// 3. connect to the network
let provider =
Provider::<Http>::try_from(anvil.endpoint())?.interval(Duration::from_millis(10u64));
// 4. instantiate the client with the wallet
2022-08-30 16:45:36 +00:00
let client = Arc::new(SignerMiddleware::new(provider, wallet.with_chain_id(anvil.chain_id())));
// 5. deploy contract
let greeter_contract =
Greeter::deploy(client, "Hello World!".to_string()).unwrap().send().await.unwrap();
// 6. call contract function
let greeting = greeter_contract.greet().call().await.unwrap();
assert_eq!("Hello World!", greeting);
Ok(())
}