Skip to content

Commit c4720b9

Browse files
[ethers-v5] Fix event and function signatures with tuples and array of tuples (#847)
1 parent 724a4d5 commit c4720b9

File tree

21 files changed

+608
-59
lines changed

21 files changed

+608
-59
lines changed

.changeset/three-schools-peel.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@typechain/ethers-v5': minor
3+
'typechain': minor
4+
---
5+
6+
fix tuples in event signatures and also arrays of tuples in functions

contracts/v0.6.4/DataTypesInput.sol

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ contract DataTypesInput {
101101
info2.a = address(info1.a);
102102
info2.b = address(info1.b);
103103
}
104+
105+
event event_struct(Struct1 input);
106+
107+
event event_struct_2(uint256 input, Struct1 input2);
104108
}
105109

106110
library StructsLib1 {

contracts/v0.6.4/Events.sol

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,11 @@ contract Events {
4545
emit Event4(EventData(2, "test"));
4646
}
4747

48+
event Event5(EventData[2] data);
49+
50+
function emit_event5() public {
51+
emit Event5([EventData(2, "test"), EventData(3, "test2")]);
52+
}
53+
4854
event NoArgsEvent();
4955
}

packages/target-ethers-v5-test/test/DataTypesInput.test.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { FunctionFragment } from '@ethersproject/abi'
1+
import type { EventFragment, FunctionFragment } from '@ethersproject/abi'
22
import { expect } from 'earljs'
33
import type { BigNumberish } from 'ethers'
44
import { BigNumber, ethers } from 'ethers'
@@ -79,12 +79,37 @@ describe('DataTypesInput', () => {
7979
typedAssert(await chain.contract.input_enum(1), 1)
8080
})
8181

82-
it('generates correct signature for tuples', () => {
82+
it('generates correct function signature for tuples', () => {
8383
const fragment: FunctionFragment = chain.contract.interface.functions['input_struct((uint256,uint256))']
8484
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
8585
expect(fragment !== undefined).toEqual(true)
8686
})
8787

88+
it('generates correct function signature for tuples arrays', () => {
89+
const fragment: FunctionFragment = chain.contract.interface.functions['input_struct3_array((uint256[])[])']
90+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
91+
expect(fragment !== undefined).toEqual(true)
92+
})
93+
94+
it('generates correct function signature for tuples with fixed length', () => {
95+
const fragment: FunctionFragment =
96+
chain.contract.interface.functions['input_struct2_tuple((uint256,(uint256,uint256))[3])']
97+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
98+
expect(fragment !== undefined).toEqual(true)
99+
})
100+
101+
it('generate correct event signature for tuples', () => {
102+
const fragment: EventFragment = chain.contract.interface.events['event_struct((uint256,uint256))']
103+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
104+
expect(fragment !== undefined).toEqual(true)
105+
})
106+
107+
it('generate correct event signature for mix of arguments and tuples', () => {
108+
const fragment: EventFragment = chain.contract.interface.events['event_struct_2(uint256,(uint256,uint256))']
109+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
110+
expect(fragment !== undefined).toEqual(true)
111+
})
112+
88113
// tests: https://github.com/ethereum-ts/TypeChain/issues/232
89114
// NOTE: typesAssert is too simple to tests type compatibility here so we can't use it
90115
it('generates correct types for tuples', () => {

packages/target-ethers-v5-test/test/Events.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ describe('Events', () => {
3333

3434
const filter = contract.filters.Event1(null, null)
3535
const results = await contract.queryFilter(filter)
36+
37+
typedAssert(results.length, 1)
3638
results.map((r) => {
3739
typedAssert(r.args.value1, BigNumber.from(1))
3840
typedAssert(r.args.value2, BigNumber.from(2))
@@ -41,6 +43,20 @@ describe('Events', () => {
4143
})
4244
})
4345

46+
it('queryFilter with tuples and arrays', async () => {
47+
await contract.emit_event5()
48+
49+
const filter = contract.filters.Event5(null)
50+
const results = await contract.queryFilter(filter)
51+
typedAssert(results.length, 1)
52+
results.map((r) => {
53+
typedAssert(r.args[0][0][0], BigNumber.from(2))
54+
typedAssert(r.args[0][0][1], 'test')
55+
typedAssert(r.args[0][1][0], BigNumber.from(3))
56+
typedAssert(r.args[0][1][1], 'test2')
57+
})
58+
})
59+
4460
it('queryFilter without params', async () => {
4561
await contract.emit_event1()
4662

packages/target-ethers-v5-test/types/factories/v0.6.4/DataTypesInput__factory.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,62 @@ import type {
1010
} from "../../v0.6.4/DataTypesInput";
1111

1212
const _abi = [
13+
{
14+
anonymous: false,
15+
inputs: [
16+
{
17+
components: [
18+
{
19+
internalType: "uint256",
20+
name: "uint256_0",
21+
type: "uint256",
22+
},
23+
{
24+
internalType: "uint256",
25+
name: "uint256_1",
26+
type: "uint256",
27+
},
28+
],
29+
indexed: false,
30+
internalType: "struct DataTypesInput.Struct1",
31+
name: "input",
32+
type: "tuple",
33+
},
34+
],
35+
name: "event_struct",
36+
type: "event",
37+
},
38+
{
39+
anonymous: false,
40+
inputs: [
41+
{
42+
indexed: false,
43+
internalType: "uint256",
44+
name: "input",
45+
type: "uint256",
46+
},
47+
{
48+
components: [
49+
{
50+
internalType: "uint256",
51+
name: "uint256_0",
52+
type: "uint256",
53+
},
54+
{
55+
internalType: "uint256",
56+
name: "uint256_1",
57+
type: "uint256",
58+
},
59+
],
60+
indexed: false,
61+
internalType: "struct DataTypesInput.Struct1",
62+
name: "input2",
63+
type: "tuple",
64+
},
65+
],
66+
name: "event_struct_2",
67+
type: "event",
68+
},
1369
{
1470
inputs: [
1571
{

packages/target-ethers-v5-test/types/factories/v0.6.4/Events__factory.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,31 @@ const _abi = [
109109
name: "Event4",
110110
type: "event",
111111
},
112+
{
113+
anonymous: false,
114+
inputs: [
115+
{
116+
components: [
117+
{
118+
internalType: "uint256",
119+
name: "index",
120+
type: "uint256",
121+
},
122+
{
123+
internalType: "string",
124+
name: "name",
125+
type: "string",
126+
},
127+
],
128+
indexed: false,
129+
internalType: "struct Events.EventData[2]",
130+
name: "data",
131+
type: "tuple[2]",
132+
},
133+
],
134+
name: "Event5",
135+
type: "event",
136+
},
112137
{
113138
anonymous: false,
114139
inputs: [],
@@ -176,6 +201,13 @@ const _abi = [
176201
stateMutability: "nonpayable",
177202
type: "function",
178203
},
204+
{
205+
inputs: [],
206+
name: "emit_event5",
207+
outputs: [],
208+
stateMutability: "nonpayable",
209+
type: "function",
210+
},
179211
] as const;
180212

181213
export class Events__factory {

packages/target-ethers-v5-test/types/v0.6.4/DataTypesInput.ts

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import type {
1111
Signer,
1212
utils,
1313
} from "ethers";
14-
import type { FunctionFragment, Result } from "@ethersproject/abi";
14+
import type {
15+
FunctionFragment,
16+
Result,
17+
EventFragment,
18+
} from "@ethersproject/abi";
1519
import type { Listener, Provider } from "@ethersproject/providers";
1620
import type {
1721
TypedEventFilter,
@@ -20,21 +24,6 @@ import type {
2024
OnEvent,
2125
} from "../common";
2226

23-
export declare namespace StructsLib1 {
24-
export type InfoStruct = { a: BigNumberish; b: BigNumberish };
25-
26-
export type InfoStructOutput = [BigNumber, BigNumber] & {
27-
a: BigNumber;
28-
b: BigNumber;
29-
};
30-
}
31-
32-
export declare namespace StructsLib2 {
33-
export type InfoStruct = { a: string; b: string };
34-
35-
export type InfoStructOutput = [string, string] & { a: string; b: string };
36-
}
37-
3827
export declare namespace DataTypesInput {
3928
export type Struct1Struct = {
4029
uint256_0: BigNumberish;
@@ -61,6 +50,21 @@ export declare namespace DataTypesInput {
6150
export type Struct3StructOutput = [BigNumber[]] & { input1: BigNumber[] };
6251
}
6352

53+
export declare namespace StructsLib1 {
54+
export type InfoStruct = { a: BigNumberish; b: BigNumberish };
55+
56+
export type InfoStructOutput = [BigNumber, BigNumber] & {
57+
a: BigNumber;
58+
b: BigNumber;
59+
};
60+
}
61+
62+
export declare namespace StructsLib2 {
63+
export type InfoStruct = { a: string; b: string };
64+
65+
export type InfoStructOutput = [string, string] & { a: string; b: string };
66+
}
67+
6468
export interface DataTypesInputInterface extends utils.Interface {
6569
functions: {
6670
"input_address(address)": FunctionFragment;
@@ -77,16 +81,16 @@ export interface DataTypesInputInterface extends utils.Interface {
7781
"input_struct((uint256,uint256))": FunctionFragment;
7882
"input_struct2((uint256,(uint256,uint256)))": FunctionFragment;
7983
"input_struct2_array((uint256,(uint256,uint256))[])": FunctionFragment;
80-
"input_struct2_tuple(tuple[3])": FunctionFragment;
84+
"input_struct2_tuple((uint256,(uint256,uint256))[3])": FunctionFragment;
8185
"input_struct3_array((uint256[])[])": FunctionFragment;
8286
"input_struct_array((uint256,uint256)[])": FunctionFragment;
83-
"input_struct_array_array(tuple[][])": FunctionFragment;
84-
"input_struct_array_array_array(tuple[][][])": FunctionFragment;
85-
"input_struct_array_fixedarray(tuple[][2])": FunctionFragment;
86-
"input_struct_fixedarray_array(tuple[2][])": FunctionFragment;
87-
"input_struct_fixedarray_array_fixedarray(tuple[2][][3])": FunctionFragment;
88-
"input_struct_fixedarray_array_fixedarray_array_fixedarray(tuple[2][][3][][4])": FunctionFragment;
89-
"input_struct_fixedarray_fixedarray(tuple[2][3])": FunctionFragment;
87+
"input_struct_array_array((uint256,uint256)[][])": FunctionFragment;
88+
"input_struct_array_array_array((uint256,uint256)[][][])": FunctionFragment;
89+
"input_struct_array_fixedarray((uint256,uint256)[][2])": FunctionFragment;
90+
"input_struct_fixedarray_array((uint256,uint256)[2][])": FunctionFragment;
91+
"input_struct_fixedarray_array_fixedarray((uint256,uint256)[2][][3])": FunctionFragment;
92+
"input_struct_fixedarray_array_fixedarray_array_fixedarray((uint256,uint256)[2][][3][][4])": FunctionFragment;
93+
"input_struct_fixedarray_fixedarray((uint256,uint256)[2][3])": FunctionFragment;
9094
"input_tuple(uint256,uint256)": FunctionFragment;
9195
"input_uint256(uint256)": FunctionFragment;
9296
"input_uint8(uint8)": FunctionFragment;
@@ -387,8 +391,35 @@ export interface DataTypesInputInterface extends utils.Interface {
387391
data: BytesLike
388392
): Result;
389393

390-
events: {};
394+
events: {
395+
"event_struct((uint256,uint256))": EventFragment;
396+
"event_struct_2(uint256,(uint256,uint256))": EventFragment;
397+
};
398+
399+
getEvent(nameOrSignatureOrTopic: "event_struct"): EventFragment;
400+
getEvent(nameOrSignatureOrTopic: "event_struct_2"): EventFragment;
401+
}
402+
403+
export interface event_structEventObject {
404+
input: DataTypesInput.Struct1StructOutput;
405+
}
406+
export type event_structEvent = TypedEvent<
407+
[DataTypesInput.Struct1StructOutput],
408+
event_structEventObject
409+
>;
410+
411+
export type event_structEventFilter = TypedEventFilter<event_structEvent>;
412+
413+
export interface event_struct_2EventObject {
414+
input: BigNumber;
415+
input2: DataTypesInput.Struct1StructOutput;
391416
}
417+
export type event_struct_2Event = TypedEvent<
418+
[BigNumber, DataTypesInput.Struct1StructOutput],
419+
event_struct_2EventObject
420+
>;
421+
422+
export type event_struct_2EventFilter = TypedEventFilter<event_struct_2Event>;
392423

393424
export interface DataTypesInput extends BaseContract {
394425
connect(signerOrProvider: Signer | Provider | string): this;
@@ -1237,7 +1268,16 @@ export interface DataTypesInput extends BaseContract {
12371268
): Promise<BigNumber[]>;
12381269
};
12391270

1240-
filters: {};
1271+
filters: {
1272+
"event_struct((uint256,uint256))"(input?: null): event_structEventFilter;
1273+
event_struct(input?: null): event_structEventFilter;
1274+
1275+
"event_struct_2(uint256,(uint256,uint256))"(
1276+
input?: null,
1277+
input2?: null
1278+
): event_struct_2EventFilter;
1279+
event_struct_2(input?: null, input2?: null): event_struct_2EventFilter;
1280+
};
12411281

12421282
estimateGas: {
12431283
input_address(

0 commit comments

Comments
 (0)