Skip to content

Commit 92939ea

Browse files
zemsehasparuskrzkaczor
authoredJan 8, 2022
Multiple struct inputs with same name (#576)
Co-authored-by: hasparus <hasparus@gmail.com> Co-authored-by: Krzysztof Kaczor <krzkaczor@gmail.com>
1 parent d244e41 commit 92939ea

File tree

26 files changed

+647
-201
lines changed

26 files changed

+647
-201
lines changed
 

Diff for: ‎.changeset/pretty-waves-leave.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@typechain/ethers-v5': major
3+
'typechain': major
4+
---
5+
6+
Structs will be namespaced using contract name when available

Diff for: ‎contracts/v0.6.4/DataTypesInput.sol

+19
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,23 @@ contract DataTypesInput {
7070
function input_struct2_tuple(Struct2[3] memory input) public pure returns (Struct2[3] memory) {
7171
return input;
7272
}
73+
74+
function input_multiple_structs_with_same_name(StructsLib1.Info memory info1) public pure returns (StructsLib2.Info memory info2) {
75+
info2.a = address(info1.a);
76+
info2.b = address(info1.b);
77+
}
7378
}
79+
80+
library StructsLib1 {
81+
struct Info {
82+
uint160 a;
83+
uint160 b;
84+
}
85+
}
86+
87+
library StructsLib2 {
88+
struct Info {
89+
address a;
90+
address b;
91+
}
92+
}

Diff for: ‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"@types/mocha": "^8.2.1",
3838
"@typescript-eslint/eslint-plugin": "4.15.1",
3939
"@typescript-eslint/parser": "4.15.1",
40-
"earljs": "^0.1.10",
40+
"earljs": "^0.1.12",
4141
"eslint": "^7.29.0",
4242
"eslint-config-typestrict": "^1.0.2",
4343
"eslint-plugin-import": "^2.23.4",

Diff for: ‎packages/hardhat-test/typechain-types/Demo.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@ import { BaseContract, BigNumber, BigNumberish, Signer, utils } from "ethers";
66
import { Listener, Provider } from "@ethersproject/providers";
77
import { TypedEventFilter, TypedEvent, TypedListener, OnEvent } from "./common";
88

9-
export type Struct1Struct = { a: BigNumberish; b: BigNumberish };
9+
export declare namespace Demo {
10+
export type Struct1Struct = { a: BigNumberish; b: BigNumberish };
1011

11-
export type Struct1StructOutput = [BigNumber, BigNumber] & {
12-
a: BigNumber;
13-
b: BigNumber;
14-
};
12+
export type Struct1StructOutput = [BigNumber, BigNumber] & {
13+
a: BigNumber;
14+
b: BigNumber;
15+
};
1516

16-
export type Struct2Struct = { a: BigNumberish; b: BigNumberish };
17+
export type Struct2Struct = { a: BigNumberish; b: BigNumberish };
1718

18-
export type Struct2StructOutput = [BigNumber, BigNumber] & {
19-
a: BigNumber;
20-
b: BigNumber;
21-
};
19+
export type Struct2StructOutput = [BigNumber, BigNumber] & {
20+
a: BigNumber;
21+
b: BigNumber;
22+
};
23+
}
2224

2325
export interface DemoInterface extends utils.Interface {
2426
contractName: "Demo";

Diff for: ‎packages/hardhat-test/typechain-types/factories/Demo__factory.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@
33
/* eslint-disable */
44
import { Signer, utils, Contract, ContractFactory, Overrides } from "ethers";
55
import { Provider, TransactionRequest } from "@ethersproject/providers";
6-
import type {
7-
Demo,
8-
DemoInterface,
9-
Struct1Struct,
10-
Struct2Struct,
11-
} from "../Demo";
6+
import type { Demo, DemoInterface } from "../Demo";
127

138
const _abi = [
149
{
@@ -75,15 +70,15 @@ export class Demo__factory extends ContractFactory {
7570
}
7671

7772
deploy(
78-
input1: Struct1Struct,
79-
input2: Struct2Struct[],
73+
input1: Demo.Struct1Struct,
74+
input2: Demo.Struct2Struct[],
8075
overrides?: Overrides & { from?: string | Promise<string> }
8176
): Promise<Demo> {
8277
return super.deploy(input1, input2, overrides || {}) as Promise<Demo>;
8378
}
8479
getDeployTransaction(
85-
input1: Struct1Struct,
86-
input2: Struct2Struct[],
80+
input1: Demo.Struct1Struct,
81+
input2: Demo.Struct2Struct[],
8782
overrides?: Overrides & { from?: string | Promise<string> }
8883
): TransactionRequest {
8984
return super.getDeployTransaction(input1, input2, overrides || {});

Diff for: ‎packages/target-ethers-v4-test/types/DataTypesInput.d.ts

+34
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ interface DataTypesInputInterface extends Interface {
3838
encode([input1]: [BigNumberish]): string;
3939
}>;
4040

41+
input_multiple_structs_with_same_name: TypedFunctionDescription<{
42+
encode([info1]: [{ a: BigNumberish; b: BigNumberish }]): string;
43+
}>;
44+
4145
input_stat_array: TypedFunctionDescription<{
4246
encode([input1]: [BigNumberish[]]): string;
4347
}>;
@@ -190,6 +194,16 @@ export class DataTypesInput extends Contract {
190194
overrides?: UnsignedTransaction
191195
): Promise<number>;
192196

197+
input_multiple_structs_with_same_name(
198+
info1: { a: BigNumberish; b: BigNumberish },
199+
overrides?: UnsignedTransaction
200+
): Promise<[string, string] & { a: string; b: string }>;
201+
202+
"input_multiple_structs_with_same_name((uint160,uint160))"(
203+
info1: { a: BigNumberish; b: BigNumberish },
204+
overrides?: UnsignedTransaction
205+
): Promise<[string, string] & { a: string; b: string }>;
206+
193207
input_stat_array(
194208
input1: BigNumberish[],
195209
overrides?: UnsignedTransaction
@@ -461,6 +475,16 @@ export class DataTypesInput extends Contract {
461475
overrides?: UnsignedTransaction
462476
): Promise<number>;
463477

478+
input_multiple_structs_with_same_name(
479+
info1: { a: BigNumberish; b: BigNumberish },
480+
overrides?: UnsignedTransaction
481+
): Promise<[string, string] & { a: string; b: string }>;
482+
483+
"input_multiple_structs_with_same_name((uint160,uint160))"(
484+
info1: { a: BigNumberish; b: BigNumberish },
485+
overrides?: UnsignedTransaction
486+
): Promise<[string, string] & { a: string; b: string }>;
487+
464488
input_stat_array(
465489
input1: BigNumberish[],
466490
overrides?: UnsignedTransaction
@@ -734,6 +758,16 @@ export class DataTypesInput extends Contract {
734758
overrides?: UnsignedTransaction
735759
): Promise<BigNumber>;
736760

761+
input_multiple_structs_with_same_name(
762+
info1: { a: BigNumberish; b: BigNumberish },
763+
overrides?: UnsignedTransaction
764+
): Promise<BigNumber>;
765+
766+
"input_multiple_structs_with_same_name((uint160,uint160))"(
767+
info1: { a: BigNumberish; b: BigNumberish },
768+
overrides?: UnsignedTransaction
769+
): Promise<BigNumber>;
770+
737771
input_stat_array(
738772
input1: BigNumberish[],
739773
overrides?: UnsignedTransaction

Diff for: ‎packages/target-ethers-v4-test/types/factories/DataTypesInput__factory.ts

+43
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,49 @@ const _abi = [
150150
stateMutability: "pure",
151151
type: "function",
152152
},
153+
{
154+
inputs: [
155+
{
156+
components: [
157+
{
158+
internalType: "uint160",
159+
name: "a",
160+
type: "uint160",
161+
},
162+
{
163+
internalType: "uint160",
164+
name: "b",
165+
type: "uint160",
166+
},
167+
],
168+
internalType: "struct StructsLib1.Info",
169+
name: "info1",
170+
type: "tuple",
171+
},
172+
],
173+
name: "input_multiple_structs_with_same_name",
174+
outputs: [
175+
{
176+
components: [
177+
{
178+
internalType: "address",
179+
name: "a",
180+
type: "address",
181+
},
182+
{
183+
internalType: "address",
184+
name: "b",
185+
type: "address",
186+
},
187+
],
188+
internalType: "struct StructsLib2.Info",
189+
name: "info2",
190+
type: "tuple",
191+
},
192+
],
193+
stateMutability: "pure",
194+
type: "function",
195+
},
153196
{
154197
inputs: [
155198
{

Diff for: ‎packages/target-ethers-v5-test/test/DataTypesInput.test.ts

+30-9
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ import { Awaited } from 'earljs/dist/mocks/types'
44
import { BigNumber, BigNumberish, ethers } from 'ethers'
55
import { AssertTrue, IsExact, q18, typedAssert } from 'test-utils'
66

7-
import {
8-
DataTypesInput,
9-
Struct1Struct,
10-
Struct1StructOutput,
11-
Struct2Struct,
12-
Struct2StructOutput,
13-
Struct3Struct,
14-
Struct3StructOutput,
15-
} from '../types/DataTypesInput'
7+
import { DataTypesInput } from '../types/DataTypesInput'
168
import { createNewBlockchain, deployContract } from './common'
179

10+
type Struct1Struct = DataTypesInput.Struct1Struct
11+
type Struct1StructOutput = DataTypesInput.Struct1StructOutput
12+
type Struct2Struct = DataTypesInput.Struct2Struct
13+
type Struct2StructOutput = DataTypesInput.Struct2StructOutput
14+
type Struct3Struct = DataTypesInput.Struct3Struct
15+
type Struct3StructOutput = DataTypesInput.Struct3StructOutput
16+
1817
describe('DataTypesInput', () => {
1918
let contract!: DataTypesInput
2019
let ganache: any
@@ -257,6 +256,28 @@ describe('DataTypesInput', () => {
257256
})
258257
})
259258

259+
it('structs with same name in different contract/library: input test', async () => {
260+
type ViewFunctionInputType = Parameters<typeof contract.input_multiple_structs_with_same_name>
261+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
262+
type _t1 = AssertTrue<
263+
IsExact<
264+
ViewFunctionInputType,
265+
[info1: { a: BigNumberish; b: BigNumberish }, overrides?: ethers.CallOverrides | undefined]
266+
>
267+
>
268+
269+
type ViewFunctionOutputType = Awaited<ReturnType<typeof contract.input_multiple_structs_with_same_name>>
270+
type _t2 = AssertTrue<
271+
IsExact<
272+
ViewFunctionOutputType,
273+
[string, string] & {
274+
a: string
275+
b: string
276+
}
277+
>
278+
>
279+
})
280+
260281
// we skip this test as ts only about types
261282
it.skip('prevents from using not existing methods', () => {
262283
// @ts-expect-error

Diff for: ‎packages/target-ethers-v5-test/test/Issue552Reproduction.test.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
import { AssertTrue, IsExact } from 'test-utils'
22

3-
import * as types from '../types/Issue552Reproduction'
3+
import { Issue552Observer, Issue552Reproduction } from '../types/Issue552Reproduction'
44
import { createNewBlockchain, deployContract } from './common'
55

66
describe('Issue552Reproduction', () => {
77
it('does not emit overly long tuples', () => {
88
type _ = [
9-
AssertTrue<IsExact<types.ObservationParamsStruct['observations'], types.ObservationStruct[]>>,
10-
AssertTrue<IsExact<types.ObservationParamsStructOutput['observations'], types.ObservationStructOutput[]>>,
9+
AssertTrue<
10+
IsExact<Issue552Reproduction.ObservationParamsStruct['observations'], Issue552Observer.ObservationStruct[]>
11+
>,
12+
AssertTrue<
13+
IsExact<
14+
Issue552Reproduction.ObservationParamsStructOutput['observations'],
15+
Issue552Observer.ObservationStructOutput[]
16+
>
17+
>,
1118
]
1219
})
1320

1421
it('accepts array of numbers', async () => {
1522
const { signer, ganache } = await createNewBlockchain()
1623

1724
try {
18-
const contract = await deployContract<types.Issue552Reproduction>(signer, 'Issue552_Reproduction')
25+
const contract = await deployContract<Issue552Reproduction>(signer, 'Issue552_Reproduction')
1926

2027
await contract.input([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
2128
} finally {

0 commit comments

Comments
 (0)
Please sign in to comment.