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