From 6bb25e5228297388b014a8532369fb845a4c9c0c Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 1 Aug 2022 18:45:31 +0200 Subject: [PATCH] fix(abigen): resolve output struct types correctly (#1546) * fix(abigen): resolve output struct types correctly * chore(clippy): make clippy happy --- .../src/contract/methods.rs | 25 ++++--- ethers-contract/tests/it/abigen.rs | 7 ++ .../tests/solidity-contracts/LargeStruct.json | 68 +++++++++++++++++++ 3 files changed, 87 insertions(+), 13 deletions(-) create mode 100644 ethers-contract/tests/solidity-contracts/LargeStruct.json diff --git a/ethers-contract/ethers-contract-abigen/src/contract/methods.rs b/ethers-contract/ethers-contract-abigen/src/contract/methods.rs index 3894802b..11b1641a 100644 --- a/ethers-contract/ethers-contract-abigen/src/contract/methods.rs +++ b/ethers-contract/ethers-contract-abigen/src/contract/methods.rs @@ -281,13 +281,9 @@ impl Context { util::ident(&format!("{}Calls", self.contract_ident)) } - /// Expands to the `name : type` pairs of the function's parameters - fn expand_params( - &self, - fun: &Function, - params: &[Param], - ) -> Result> { - params + /// Expands to the `name : type` pairs of the function's inputs + fn expand_input_params(&self, fun: &Function) -> Result> { + fun.inputs .iter() .enumerate() .map(|(idx, param)| { @@ -298,14 +294,17 @@ impl Context { .collect() } - /// Expands to the `name : type` pairs of the function's inputs - fn expand_input_params(&self, fun: &Function) -> Result> { - self.expand_params(fun, &fun.inputs) - } - /// Expands to the `name : type` pairs of the function's outputs fn expand_output_params(&self, fun: &Function) -> Result> { - self.expand_params(fun, &fun.outputs) + fun.outputs + .iter() + .enumerate() + .map(|(idx, param)| { + let name = util::expand_input_name(idx, ¶m.name); + let ty = self.expand_output_param_type(fun, param, ¶m.kind)?; + Ok((name, ty)) + }) + .collect() } /// Expands to the return type of a function diff --git a/ethers-contract/tests/it/abigen.rs b/ethers-contract/tests/it/abigen.rs index fd661ad0..9ca7e9a9 100644 --- a/ethers-contract/tests/it/abigen.rs +++ b/ethers-contract/tests/it/abigen.rs @@ -659,3 +659,10 @@ fn can_generate_to_string_overload() { fn can_generate_large_event() { abigen!(NewSale, "ethers-contract/tests/solidity-contracts/sale.json"); } + +#[test] +fn can_generate_large_output_struct() { + abigen!(LargeOutputStruct, "ethers-contract/tests/solidity-contracts/LargeStruct.json"); + + let r = GetByIdReturn(Info::default()); +} diff --git a/ethers-contract/tests/solidity-contracts/LargeStruct.json b/ethers-contract/tests/solidity-contracts/LargeStruct.json new file mode 100644 index 00000000..04e4531b --- /dev/null +++ b/ethers-contract/tests/solidity-contracts/LargeStruct.json @@ -0,0 +1,68 @@ +[ + { + "inputs": [ + { + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + } + ], + "name": "getById", + "outputs": [ + { + "components": [ + { + "internalType": "uint128", + "name": "x", + "type": "uint128" + }, + { + "internalType": "int24", + "name": "y", + "type": "int24" + }, + { + "internalType": "int24", + "name": "z", + "type": "int24" + }, + { + "internalType": "uint256", + "name": "a", + "type": "uint256" + }, + { + "internalType": "int256", + "name": "b", + "type": "int256" + }, + { + "internalType": "int256", + "name": "c", + "type": "int256" + }, + { + "internalType": "int256", + "name": "d", + "type": "int256" + }, + { + "internalType": "uint256", + "name": "e", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "f", + "type": "uint256" + } + ], + "internalType": "struct Many.Info", + "name": "", + "type": "tuple" + } + ], + "stateMutability": "view", + "type": "function" + } +] \ No newline at end of file