2022-12-29 12:53:11 +00:00
|
|
|
use ethers::{prelude::Abigen, solc::Solc};
|
|
|
|
use eyre::Result;
|
2020-06-16 12:08:42 +00:00
|
|
|
|
2022-12-29 12:53:11 +00:00
|
|
|
fn main() -> Result<()> {
|
2020-06-16 12:08:42 +00:00
|
|
|
let mut args = std::env::args();
|
|
|
|
args.next().unwrap(); // skip program name
|
|
|
|
|
2022-10-11 17:49:05 +00:00
|
|
|
let contract_name = args.next().unwrap_or_else(|| "SimpleStorage".to_owned());
|
2022-12-29 12:53:11 +00:00
|
|
|
let contract: String = args
|
|
|
|
.next()
|
|
|
|
.unwrap_or_else(|| "examples/contracts/examples/contracts/contract.sol".to_owned());
|
2020-06-16 12:08:42 +00:00
|
|
|
|
2022-11-07 23:43:11 +00:00
|
|
|
println!("Generating bindings for {contract}\n");
|
2020-06-16 12:08:42 +00:00
|
|
|
|
2021-10-31 11:34:51 +00:00
|
|
|
// compile it
|
2020-06-16 12:08:42 +00:00
|
|
|
let abi = if contract.ends_with(".sol") {
|
2021-10-31 11:34:51 +00:00
|
|
|
let contracts = Solc::default().compile_source(&contract)?;
|
|
|
|
let abi = contracts.get(&contract, &contract_name).unwrap().abi.unwrap();
|
|
|
|
serde_json::to_string(abi).unwrap()
|
2020-06-16 12:08:42 +00:00
|
|
|
} else {
|
|
|
|
contract
|
|
|
|
};
|
|
|
|
|
|
|
|
let bindings = Abigen::new(&contract_name, abi)?.generate()?;
|
|
|
|
|
|
|
|
// print to stdout if no output arg is given
|
|
|
|
if let Some(output_path) = args.next() {
|
2022-11-07 23:43:11 +00:00
|
|
|
bindings.write_to_file(output_path)?;
|
2020-06-16 12:08:42 +00:00
|
|
|
} else {
|
2023-01-13 18:23:59 +00:00
|
|
|
bindings.write(&mut std::io::stdout())?;
|
2020-06-16 12:08:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|