ethers-rs/examples/contract_with_abi.rs

63 lines
2.3 KiB
Rust
Raw Permalink Normal View History

use ethers::{prelude::*, utils::Anvil};
use eyre::Result;
use std::{convert::TryFrom, path::Path, sync::Arc, time::Duration};
2020-05-25 10:05:00 +00:00
2020-06-02 10:58:48 +00:00
// Generate the type-safe contract bindings by providing the ABI
// definition
2020-05-26 18:57:59 +00:00
abigen!(
SimpleContract,
"./examples/contract_abi.json",
2020-05-26 18:57:59 +00:00
event_derives(serde::Deserialize, serde::Serialize)
);
2020-05-25 10:05:00 +00:00
#[tokio::main]
2020-05-26 11:00:56 +00:00
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();
// 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();
2020-05-31 14:50:00 +00:00
// 2. instantiate our wallet
let wallet: LocalWallet = anvil.keys()[0].clone().into();
2020-05-31 14:50:00 +00:00
// 3. connect to the network
let provider =
Provider::<Http>::try_from(anvil.endpoint())?.interval(Duration::from_millis(10u64));
2020-05-31 14:50:00 +00:00
// 4. instantiate the client with the wallet
2022-08-30 16:45:36 +00:00
let client = SignerMiddleware::new(provider, wallet.with_chain_id(anvil.chain_id()));
let client = Arc::new(client);
2020-05-25 10:05:00 +00:00
// 5. create a factory which will be used to deploy instances of the contract
let factory = ContractFactory::new(abi, bytecode, client.clone());
2020-05-25 10:05:00 +00:00
// 6. deploy it with the constructor arguments
let contract = factory.deploy("initial value".to_string())?.send().await?;
2020-05-25 10:05:00 +00:00
// 7. get the contract's address
2020-05-31 14:50:00 +00:00
let addr = contract.address();
2020-05-25 10:05:00 +00:00
// 8. instantiate the contract
let contract = SimpleContract::new(addr, client.clone());
2020-05-25 15:35:38 +00:00
// 9. call the `setValue` method
// (first `await` returns a PendingTransaction, second one waits for it to be mined)
let _receipt = contract.set_value("hi".to_owned()).send().await?.await?;
// 10. get all events
let logs = contract.value_changed_filter().from_block(0u64).query().await?;
// 11. get the new value
let value = contract.get_value().call().await?;
2020-05-25 15:35:38 +00:00
println!("Value: {value}. Logs: {}", serde_json::to_string(&logs)?);
2020-05-25 15:35:38 +00:00
2020-05-25 10:05:00 +00:00
Ok(())
}