From c184bc7fe35cf66b608f399f968cd289c3a519a7 Mon Sep 17 00:00:00 2001 From: Akosh Farkash Date: Tue, 10 Oct 2023 11:45:41 +0100 Subject: [PATCH 1/2] BUG: Constructor parameters not generated --- .../ethers-contract-abigen/src/lib.rs | 9 ++++++++ .../solidity-contracts/StructConstructor.json | 1 + .../solidity-contracts/StructConstructor.sol | 22 +++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 ethers-contract/tests/solidity-contracts/StructConstructor.json create mode 100644 ethers-contract/tests/solidity-contracts/StructConstructor.sol diff --git a/ethers-contract/ethers-contract-abigen/src/lib.rs b/ethers-contract/ethers-contract-abigen/src/lib.rs index 7f2b5ea85..932ae1671 100644 --- a/ethers-contract/ethers-contract-abigen/src/lib.rs +++ b/ethers-contract/ethers-contract-abigen/src/lib.rs @@ -420,4 +420,13 @@ mod tests { let out = gen.tokens.to_string(); assert!(out.contains("pub struct Stuff")); } + + #[test] + fn can_generate_constructor_params() { + let contract = include_str!("../../tests/solidity-contracts/StructConstructor.json"); + let abigen = Abigen::new("MyContract", contract).unwrap(); + let gen = abigen.generate().unwrap(); + let out = gen.tokens.to_string(); + assert!(out.contains("pub struct ConstructorParams")); + } } diff --git a/ethers-contract/tests/solidity-contracts/StructConstructor.json b/ethers-contract/tests/solidity-contracts/StructConstructor.json new file mode 100644 index 000000000..8a6bf1004 --- /dev/null +++ b/ethers-contract/tests/solidity-contracts/StructConstructor.json @@ -0,0 +1 @@ +{"abi": [{"inputs":[{"components":[{"internalType":"uint256","name":"x","type":"uint256"},{"internalType":"uint256","name":"y","type":"uint256"}],"internalType":"struct MyContract.ConstructorParams","name":"params","type":"tuple"}],"stateMutability":"nonpayable","type":"constructor"}], "bin": "608060405234801561001057600080fd5b506040516101df3803806101df833981810160405281019061003291906100b2565b8060008082015181600001556020820151816001015590505050610192565b60006040828403121561006357600080fd5b61006d60406100db565b9050600061007d8482850161009d565b60008301525060206100918482850161009d565b60208301525092915050565b6000815190506100ac8161017b565b92915050565b6000604082840312156100c457600080fd5b60006100d284828501610051565b91505092915050565b60006100e56100f6565b90506100f1828261010a565b919050565b6000604051905090565b6000819050919050565b6101138261016a565b810181811067ffffffffffffffff821117156101325761013161013b565b5b80604052505050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6000601f19601f8301169050919050565b61018481610100565b811461018f57600080fd5b50565b603f806101a06000396000f3fe6080604052600080fdfea2646970667358221220bbd84df82374c1aa309661efb1096e8b1edc2b6fd57eaa1d43c53908b04321b864736f6c63430008020033"} diff --git a/ethers-contract/tests/solidity-contracts/StructConstructor.sol b/ethers-contract/tests/solidity-contracts/StructConstructor.sol new file mode 100644 index 000000000..896c306f8 --- /dev/null +++ b/ethers-contract/tests/solidity-contracts/StructConstructor.sol @@ -0,0 +1,22 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity >=0.8; + +// To sync with StructContractor.json run: + +// CONTRACT=ethers-contract/tests/solidity-contracts/StructConstructor +// BIN=$(solc --bin $CONTRACT.sol | sed '4q;d' | tr -d '\n') +// ABI=$(solc --abi $CONTRACT.sol | tail -n 1) +// echo "{\"abi\": $ABI, \"bin\": \"$BIN\"}" > $CONTRACT.json + +contract MyContract { + struct ConstructorParams { + uint256 x; + uint256 y; + } + + ConstructorParams _params; + + constructor(ConstructorParams memory params) { + _params = params; + } +} From 39a88d2e003381e6f7364bb00d82b0e864f2525b Mon Sep 17 00:00:00 2001 From: Akosh Farkash Date: Tue, 10 Oct 2023 11:50:01 +0100 Subject: [PATCH 2/2] FIX: Use the name 'constructor' if there is empty --- .../ethers-contract-abigen/src/contract/structs.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ethers-contract/ethers-contract-abigen/src/contract/structs.rs b/ethers-contract/ethers-contract-abigen/src/contract/structs.rs index a0fd3af2b..d469968e7 100644 --- a/ethers-contract/ethers-contract-abigen/src/contract/structs.rs +++ b/ethers-contract/ethers-contract-abigen/src/contract/structs.rs @@ -234,7 +234,12 @@ impl InternalStructs { { let is_event = item.type_field == "event"; - if let Some(name) = item.name { + let name = match item.name { + None if item.type_field == "constructor" => Some("constructor".to_owned()), + other => other, + }; + + if let Some(name) = name { for (idx, input) in item.inputs.into_iter().enumerate() { if let Some(ty) = input .internal_type