Skip to content

Commit

Permalink
fix: filter out empty bytecode (gakonst#1248)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored May 11, 2022
1 parent 0a03141 commit 847110a
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 49 deletions.
92 changes: 46 additions & 46 deletions ethers-contract/ethers-contract-abigen/src/contract/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,57 +44,57 @@ impl Context {

/// Returns all deploy (constructor) implementations
pub(crate) fn deployment_methods(&self) -> TokenStream {
if self.contract_bytecode.is_some() {
let ethers_core = ethers_core_crate();
let ethers_contract = ethers_contract_crate();

let abi_name = self.inline_abi_ident();
let get_abi = quote! {
#abi_name.clone()
};
if self.contract_bytecode.is_none() {
// don't generate deploy if no bytecode
return quote! {}
}
let ethers_core = ethers_core_crate();
let ethers_contract = ethers_contract_crate();

let bytecode_name = self.inline_bytecode_ident();
let get_bytecode = quote! {
#bytecode_name.clone().into()
};
let abi_name = self.inline_abi_ident();
let get_abi = quote! {
#abi_name.clone()
};

let deploy = quote! {
/// Constructs the general purpose `Deployer` instance based on the provided constructor arguments and sends it.
/// Returns a new instance of a deployer that returns an instance of this contract after sending the transaction
///
/// Notes:
/// 1. If there are no constructor arguments, you should pass `()` as the argument.
/// 1. The default poll duration is 7 seconds.
/// 1. The default number of confirmations is 1 block.
///
///
/// # Example
///
/// Generate contract bindings with `abigen!` and deploy a new contract instance.
///
/// *Note*: this requires a `bytecode` and `abi` object in the `greeter.json` artifact.
///
/// ```ignore
/// # async fn deploy<M: ethers::providers::Middleware>(client: ::std::sync::Arc<M>) {
/// abigen!(Greeter,"../greeter.json");
///
/// let greeter_contract = Greeter::deploy(client, "Hello world!".to_string()).unwrap().send().await.unwrap();
/// let msg = greeter_contract.greet().call().await.unwrap();
/// # }
/// ```
pub fn deploy<T: #ethers_core::abi::Tokenize >(client: ::std::sync::Arc<M>, constructor_args: T) -> Result<#ethers_contract::builders::ContractDeployer<M, Self>, #ethers_contract::ContractError<M>> {
let factory = #ethers_contract::ContractFactory::new(#get_abi, #get_bytecode, client);
let deployer = factory.deploy(constructor_args)?;
let deployer = #ethers_contract::ContractDeployer::new(deployer);
Ok(deployer)
}
let bytecode_name = self.inline_bytecode_ident();
let get_bytecode = quote! {
#bytecode_name.clone().into()
};

};
let deploy = quote! {
/// Constructs the general purpose `Deployer` instance based on the provided constructor arguments and sends it.
/// Returns a new instance of a deployer that returns an instance of this contract after sending the transaction
///
/// Notes:
/// 1. If there are no constructor arguments, you should pass `()` as the argument.
/// 1. The default poll duration is 7 seconds.
/// 1. The default number of confirmations is 1 block.
///
///
/// # Example
///
/// Generate contract bindings with `abigen!` and deploy a new contract instance.
///
/// *Note*: this requires a `bytecode` and `abi` object in the `greeter.json` artifact.
///
/// ```ignore
/// # async fn deploy<M: ethers::providers::Middleware>(client: ::std::sync::Arc<M>) {
/// abigen!(Greeter,"../greeter.json");
///
/// let greeter_contract = Greeter::deploy(client, "Hello world!".to_string()).unwrap().send().await.unwrap();
/// let msg = greeter_contract.greet().call().await.unwrap();
/// # }
/// ```
pub fn deploy<T: #ethers_core::abi::Tokenize >(client: ::std::sync::Arc<M>, constructor_args: T) -> Result<#ethers_contract::builders::ContractDeployer<M, Self>, #ethers_contract::ContractError<M>> {
let factory = #ethers_contract::ContractFactory::new(#get_abi, #get_bytecode, client);
let deployer = factory.deploy(constructor_args)?;
let deployer = #ethers_contract::ContractDeployer::new(deployer);
Ok(deployer)
}

return deploy
}
};

quote! {}
deploy
}

/// Expands to the corresponding struct type based on the inputs of the given function
Expand Down
40 changes: 37 additions & 3 deletions ethers-contract/ethers-contract-abigen/src/rawabi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,18 @@ impl<'de> Visitor<'de> for AbiObjectVisitor {
abi = Some(RawAbi(map.next_value::<Vec<Item>>()?));
}
"bytecode" | "byteCode" => {
bytecode = map.next_value::<BytecodeObject>().ok().map(|obj| obj.object);
bytecode = map
.next_value::<BytecodeObject>()
.ok()
.map(|obj| obj.object)
.filter(|bytecode| !bytecode.0.is_empty());
}
"bin" => {
bytecode = map.next_value::<DeserializeBytes>().ok().map(|b| b.0);
bytecode = map
.next_value::<DeserializeBytes>()
.ok()
.map(|b| b.0)
.filter(|b| !b.0.is_empty());
}
_ => {
map.next_value::<serde::de::IgnoredAny>()?;
Expand All @@ -185,7 +193,7 @@ impl<'de> Visitor<'de> for AbiObjectVisitor {
}

impl<'de> Deserialize<'de> for AbiObject {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
Expand Down Expand Up @@ -269,4 +277,30 @@ mod tests {
let artifact = include_str!("../../tests/solidity-contracts/greeter.json");
assert_has_bytecode(artifact);
}

#[test]
fn ignores_empty_bytecode() {
let abi_str = r#"[{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint64","name":"number","type":"uint64"}],"name":"MyEvent","type":"event"},{"inputs":[],"name":"greet","outputs":[],"stateMutability":"nonpayable","type":"function"}]"#;
let s = format!(r#"{{"abi": {}, "bin" : "0x" }}"#, abi_str);

match serde_json::from_str::<JsonAbi>(&s).unwrap() {
JsonAbi::Object(abi) => {
assert!(abi.bytecode.is_none());
}
_ => {
panic!("expected abi object")
}
}

let s = format!(r#"{{"abi": {}, "bytecode" : {{ "object": "0x" }} }}"#, abi_str);

match serde_json::from_str::<JsonAbi>(&s).unwrap() {
JsonAbi::Object(abi) => {
assert!(abi.bytecode.is_none());
}
_ => {
panic!("expected abi object")
}
}
}
}

0 comments on commit 847110a

Please sign in to comment.