Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
FIX: Generate structs for constructor arguments (#2631)
Browse files Browse the repository at this point in the history
* BUG: Constructor parameters not generated

* FIX: Use the name 'constructor' if there is empty
  • Loading branch information
aakoshh authored Oct 18, 2023
1 parent 976f38c commit 23f5766
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions ethers-contract/ethers-contract-abigen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
Original file line number Diff line number Diff line change
@@ -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"}
22 changes: 22 additions & 0 deletions ethers-contract/tests/solidity-contracts/StructConstructor.sol
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 23f5766

Please sign in to comment.