fix: filter out empty bytecode (#1248)

This commit is contained in:
Matthias Seitz 2022-05-11 16:34:41 +02:00 committed by GitHub
parent 0a031417d8
commit 847110a3fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 86 additions and 52 deletions

View File

@ -44,7 +44,10 @@ impl Context {
/// Returns all deploy (constructor) implementations
pub(crate) fn deployment_methods(&self) -> TokenStream {
if self.contract_bytecode.is_some() {
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();
@ -91,10 +94,7 @@ impl Context {
};
return deploy
}
quote! {}
deploy
}
/// Expands to the corresponding struct type based on the inputs of the given function

View File

@ -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>()?;
@ -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>,
{
@ -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")
}
}
}
}