2022-01-04 09:32:17 +00:00
|
|
|
use ethers::{
|
|
|
|
prelude::*,
|
|
|
|
solc::{Project, ProjectPathsConfig},
|
|
|
|
utils::Ganache,
|
|
|
|
};
|
2022-02-03 20:36:05 +00:00
|
|
|
use eyre::Result;
|
2021-10-31 11:34:51 +00:00
|
|
|
use std::{convert::TryFrom, path::PathBuf, 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
|
2020-10-29 07:48:24 +00:00
|
|
|
// definition in human readable format
|
2020-05-26 18:57:59 +00:00
|
|
|
abigen!(
|
|
|
|
SimpleContract,
|
2020-10-29 07:48:24 +00:00
|
|
|
r#"[
|
|
|
|
function setValue(string)
|
2021-03-12 14:57:46 +00:00
|
|
|
function getValue() external view returns (string)
|
2021-02-16 17:10:26 +00:00
|
|
|
event ValueChanged(address indexed author, string oldValue, string newValue)
|
2020-10-29 07:48:24 +00:00
|
|
|
]"#,
|
2020-05-26 18:57:59 +00:00
|
|
|
event_derives(serde::Deserialize, serde::Serialize)
|
|
|
|
);
|
2020-05-26 14:43:51 +00:00
|
|
|
|
2020-05-25 10:05:00 +00:00
|
|
|
#[tokio::main]
|
2020-05-26 11:00:56 +00:00
|
|
|
async fn main() -> Result<()> {
|
2021-10-31 11:34:51 +00:00
|
|
|
// the directory we use is root-dir/examples
|
|
|
|
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("examples");
|
|
|
|
// we use `root` for both the project root and for where to search for contracts since
|
|
|
|
// everything is in the same directory
|
|
|
|
let paths = ProjectPathsConfig::builder().root(&root).sources(&root).build().unwrap();
|
2022-01-04 09:32:17 +00:00
|
|
|
|
2021-10-31 11:34:51 +00:00
|
|
|
// get the solc project instance using the paths above
|
2022-01-04 09:32:17 +00:00
|
|
|
let project = Project::builder().paths(paths).ephemeral().no_artifacts().build().unwrap();
|
2021-10-31 11:34:51 +00:00
|
|
|
// compile the project and get the artifacts
|
2022-01-04 09:32:17 +00:00
|
|
|
let output = project.compile().unwrap();
|
2022-03-13 23:44:01 +00:00
|
|
|
let contract = output.find("SimpleStorage").expect("could not find contract").clone();
|
2022-01-04 09:32:17 +00:00
|
|
|
let (abi, bytecode, _) = contract.into_parts_or_default();
|
2020-05-31 14:50:00 +00:00
|
|
|
|
2021-10-31 11:34:51 +00:00
|
|
|
// 2. instantiate our wallet & ganache
|
|
|
|
let ganache = Ganache::new().spawn();
|
2020-10-02 08:41:16 +00:00
|
|
|
let wallet: LocalWallet = ganache.keys()[0].clone().into();
|
2020-05-31 14:50:00 +00:00
|
|
|
|
2021-03-22 11:09:12 +00:00
|
|
|
// 3. connect to the network
|
2020-06-22 13:42:34 +00:00
|
|
|
let provider =
|
|
|
|
Provider::<Http>::try_from(ganache.endpoint())?.interval(Duration::from_millis(10u64));
|
2020-05-31 14:50:00 +00:00
|
|
|
|
2021-03-22 11:09:12 +00:00
|
|
|
// 4. instantiate the client with the wallet
|
2020-10-08 15:56:36 +00:00
|
|
|
let client = SignerMiddleware::new(provider, wallet);
|
2020-06-22 08:44:08 +00:00
|
|
|
let client = Arc::new(client);
|
2020-05-25 10:05:00 +00:00
|
|
|
|
2021-03-22 11:09:12 +00:00
|
|
|
// 5. create a factory which will be used to deploy instances of the contract
|
2022-01-04 09:32:17 +00:00
|
|
|
let factory = ContractFactory::new(abi, bytecode, client.clone());
|
2020-05-25 10:05:00 +00:00
|
|
|
|
2021-03-22 11:09:12 +00:00
|
|
|
// 6. deploy it with the constructor arguments
|
2021-10-29 12:29:35 +00:00
|
|
|
let contract = factory.deploy("initial value".to_string())?.legacy().send().await?;
|
2020-05-25 10:05:00 +00:00
|
|
|
|
2021-03-22 11:09:12 +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
|
|
|
|
2021-03-22 11:09:12 +00:00
|
|
|
// 8. instantiate the contract
|
2020-06-22 08:44:08 +00:00
|
|
|
let contract = SimpleContract::new(addr, client.clone());
|
2020-05-25 15:35:38 +00:00
|
|
|
|
2021-03-22 11:09:12 +00:00
|
|
|
// 9. call the `setValue` method
|
2020-12-17 11:26:01 +00:00
|
|
|
// (first `await` returns a PendingTransaction, second one waits for it to be mined)
|
2021-10-29 12:29:35 +00:00
|
|
|
let _receipt = contract.set_value("hi".to_owned()).legacy().send().await?.await?;
|
2020-05-26 14:43:51 +00:00
|
|
|
|
2021-03-22 11:09:12 +00:00
|
|
|
// 10. get all events
|
2021-10-29 12:29:35 +00:00
|
|
|
let logs = contract.value_changed_filter().from_block(0u64).query().await?;
|
2020-05-26 14:43:51 +00:00
|
|
|
|
2021-03-22 11:09:12 +00:00
|
|
|
// 11. get the new value
|
2020-05-26 14:43:51 +00:00
|
|
|
let value = contract.get_value().call().await?;
|
2020-05-25 15:35:38 +00:00
|
|
|
|
2020-05-26 14:43:51 +00:00
|
|
|
println!("Value: {}. Logs: {}", value, serde_json::to_string(&logs)?);
|
2020-05-25 15:35:38 +00:00
|
|
|
|
2020-05-25 10:05:00 +00:00
|
|
|
Ok(())
|
|
|
|
}
|