This repository has been archived by the owner on Oct 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 795
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(abigen): correctly parse params in human readable abi (#194)
* fix(abigen): correctly parse params in human readable abi * chore: make clippy happy * test: remove unwrap * chore: make clippy happy again * chore: fix contract.rs example * chore: rename to contract using human readable format * examples: add abigen example with path to abi * fix: pin funty version to fix bitvec error * chore: remove unused import * chore: fix deps
- Loading branch information
Showing
12 changed files
with
110 additions
and
41 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[{"inputs":[{"internalType":"string","name":"value","type":"string"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"author","type":"address"},{"indexed":false,"internalType":"string","name":"oldValue","type":"string"},{"indexed":false,"internalType":"string","name":"newValue","type":"string"}],"name":"ValueChanged","type":"event"},{"inputs":[],"name":"getValue","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"value","type":"string"}],"name":"setValue","outputs":[],"stateMutability":"nonpayable","type":"function"}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use anyhow::Result; | ||
use ethers::{ | ||
prelude::*, | ||
utils::{Ganache, Solc}, | ||
}; | ||
use std::{convert::TryFrom, sync::Arc, time::Duration}; | ||
|
||
// Generate the type-safe contract bindings by providing the ABI | ||
// definition in human readable format | ||
abigen!( | ||
SimpleContract, | ||
"./ethers/examples/contract_abi.json", | ||
event_derives(serde::Deserialize, serde::Serialize) | ||
); | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
// 1. compile the contract (note this requires that you are inside the `ethers/examples` directory) | ||
let compiled = Solc::new("**/contract.sol").build()?; | ||
let contract = compiled | ||
.get("SimpleStorage") | ||
.expect("could not find contract"); | ||
dbg!("OK"); | ||
|
||
// 2. launch ganache | ||
let ganache = Ganache::new().spawn(); | ||
|
||
// 3. instantiate our wallet | ||
let wallet: LocalWallet = ganache.keys()[0].clone().into(); | ||
|
||
// 4. connect to the network | ||
let provider = | ||
Provider::<Http>::try_from(ganache.endpoint())?.interval(Duration::from_millis(10u64)); | ||
|
||
// 5. instantiate the client with the wallet | ||
let client = SignerMiddleware::new(provider, wallet); | ||
let client = Arc::new(client); | ||
|
||
// 6. create a factory which will be used to deploy instances of the contract | ||
let factory = ContractFactory::new( | ||
contract.abi.clone(), | ||
contract.bytecode.clone(), | ||
client.clone(), | ||
); | ||
|
||
// 7. deploy it with the constructor arguments | ||
let contract = factory.deploy("initial value".to_string())?.send().await?; | ||
|
||
// 8. get the contract's address | ||
let addr = contract.address(); | ||
|
||
// 9. instantiate the contract | ||
let contract = SimpleContract::new(addr, client.clone()); | ||
|
||
// 10. 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?; | ||
|
||
// 11. get all events | ||
let logs = contract | ||
.value_changed_filter() | ||
.from_block(0u64) | ||
.query() | ||
.await?; | ||
|
||
// 12. get the new value | ||
let value = contract.get_value().call().await?; | ||
|
||
println!("Value: {}. Logs: {}", value, serde_json::to_string(&logs)?); | ||
|
||
Ok(()) | ||
} |