diff --git a/CHANGELOG.md b/CHANGELOG.md index c5a0d996..608599c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Unreleased +- Add abigen support for hardhat generated bytecode json format [#2012](https://github.com/gakonst/ethers-rs/pull/2012) - Fix typo in `RwClient` docs for `write_client` method. - Add support for Geth `debug_traceCall` [#1949](https://github.com/gakonst/ethers-rs/pull/1949) - Graceful handling of WebSocket transport errors [#1889](https://github.com/gakonst/ethers-rs/issues/1889) [#1815](https://github.com/gakonst/ethers-rs/issues/1815) diff --git a/ethers-core/src/abi/raw.rs b/ethers-core/src/abi/raw.rs index 7aaa915a..ddc73e34 100644 --- a/ethers-core/src/abi/raw.rs +++ b/ethers-core/src/abi/raw.rs @@ -147,8 +147,10 @@ impl<'de> Visitor<'de> for AbiObjectVisitor { let mut bytecode = None; #[derive(Deserialize)] - struct BytecodeObject { - object: Bytes, + #[serde(untagged)] + enum Bytecode { + Object { object: Bytes }, + Bytes(Bytes), } struct DeserializeBytes(Bytes); @@ -169,9 +171,12 @@ impl<'de> Visitor<'de> for AbiObjectVisitor { } "bytecode" | "byteCode" => { bytecode = map - .next_value::() + .next_value::() .ok() - .map(|obj| obj.object) + .map(|obj| match obj { + Bytecode::Object { object } => object, + Bytecode::Bytes(bytes) => bytes, + }) .filter(|bytecode| !bytecode.0.is_empty()); } "bin" => { @@ -263,6 +268,9 @@ mod tests { let s = format!(r#"{{"abi": {abi_str}, "bytecode" : {{ "object": "{code}" }} }}"#); assert_has_bytecode(&s); + let s = format!(r#"{{"abi": {abi_str}, "bytecode" : "{code}" }}"#); + assert_has_bytecode(&s); + let hh_artifact = include_str!( "../../../ethers-contract/tests/solidity-contracts/verifier_abi_hardhat.json" ); diff --git a/examples/contracts/examples/deploy_from_abi_and_bytecode.rs b/examples/contracts/examples/deploy_from_abi_and_bytecode.rs index 1dd91f37..f1f33f64 100644 --- a/examples/contracts/examples/deploy_from_abi_and_bytecode.rs +++ b/examples/contracts/examples/deploy_from_abi_and_bytecode.rs @@ -10,8 +10,8 @@ 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` +// `{"abi": [..], "bin": "..."}` , `{"abi": [..], "bytecode": {"object": "..."}}` or +// `{"abi": [..], "bytecode": "..."}` this will embedd the bytecode in a variable `GREETER_BYTECODE` abigen!(Greeter, "ethers-contract/tests/solidity-contracts/greeter.json",); #[tokio::main]