Skip to content

Commit

Permalink
[ethers-v5] Fix event and function signatures with tuples and array o…
Browse files Browse the repository at this point in the history
…f tuples (#847)
  • Loading branch information
bernard-wagner committed Jul 17, 2023
1 parent 724a4d5 commit c4720b9
Show file tree
Hide file tree
Showing 21 changed files with 608 additions and 59 deletions.
6 changes: 6 additions & 0 deletions .changeset/three-schools-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@typechain/ethers-v5': minor
'typechain': minor
---

fix tuples in event signatures and also arrays of tuples in functions
4 changes: 4 additions & 0 deletions contracts/v0.6.4/DataTypesInput.sol
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ contract DataTypesInput {
info2.a = address(info1.a);
info2.b = address(info1.b);
}

event event_struct(Struct1 input);

event event_struct_2(uint256 input, Struct1 input2);
}

library StructsLib1 {
Expand Down
6 changes: 6 additions & 0 deletions contracts/v0.6.4/Events.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,11 @@ contract Events {
emit Event4(EventData(2, "test"));
}

event Event5(EventData[2] data);

function emit_event5() public {
emit Event5([EventData(2, "test"), EventData(3, "test2")]);
}

event NoArgsEvent();
}
29 changes: 27 additions & 2 deletions packages/target-ethers-v5-test/test/DataTypesInput.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { FunctionFragment } from '@ethersproject/abi'
import type { EventFragment, FunctionFragment } from '@ethersproject/abi'
import { expect } from 'earljs'
import type { BigNumberish } from 'ethers'
import { BigNumber, ethers } from 'ethers'
Expand Down Expand Up @@ -79,12 +79,37 @@ describe('DataTypesInput', () => {
typedAssert(await chain.contract.input_enum(1), 1)
})

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

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

it('generates correct function signature for tuples with fixed length', () => {
const fragment: FunctionFragment =
chain.contract.interface.functions['input_struct2_tuple((uint256,(uint256,uint256))[3])']
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
expect(fragment !== undefined).toEqual(true)
})

it('generate correct event signature for tuples', () => {
const fragment: EventFragment = chain.contract.interface.events['event_struct((uint256,uint256))']
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
expect(fragment !== undefined).toEqual(true)
})

it('generate correct event signature for mix of arguments and tuples', () => {
const fragment: EventFragment = chain.contract.interface.events['event_struct_2(uint256,(uint256,uint256))']
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
expect(fragment !== undefined).toEqual(true)
})

// tests: https://github.com/ethereum-ts/TypeChain/issues/232
// NOTE: typesAssert is too simple to tests type compatibility here so we can't use it
it('generates correct types for tuples', () => {
Expand Down
16 changes: 16 additions & 0 deletions packages/target-ethers-v5-test/test/Events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ describe('Events', () => {

const filter = contract.filters.Event1(null, null)
const results = await contract.queryFilter(filter)

typedAssert(results.length, 1)
results.map((r) => {
typedAssert(r.args.value1, BigNumber.from(1))
typedAssert(r.args.value2, BigNumber.from(2))
Expand All @@ -41,6 +43,20 @@ describe('Events', () => {
})
})

it('queryFilter with tuples and arrays', async () => {
await contract.emit_event5()

const filter = contract.filters.Event5(null)
const results = await contract.queryFilter(filter)
typedAssert(results.length, 1)
results.map((r) => {
typedAssert(r.args[0][0][0], BigNumber.from(2))
typedAssert(r.args[0][0][1], 'test')
typedAssert(r.args[0][1][0], BigNumber.from(3))
typedAssert(r.args[0][1][1], 'test2')
})
})

it('queryFilter without params', async () => {
await contract.emit_event1()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,62 @@ import type {
} from "../../v0.6.4/DataTypesInput";

const _abi = [
{
anonymous: false,
inputs: [
{
components: [
{
internalType: "uint256",
name: "uint256_0",
type: "uint256",
},
{
internalType: "uint256",
name: "uint256_1",
type: "uint256",
},
],
indexed: false,
internalType: "struct DataTypesInput.Struct1",
name: "input",
type: "tuple",
},
],
name: "event_struct",
type: "event",
},
{
anonymous: false,
inputs: [
{
indexed: false,
internalType: "uint256",
name: "input",
type: "uint256",
},
{
components: [
{
internalType: "uint256",
name: "uint256_0",
type: "uint256",
},
{
internalType: "uint256",
name: "uint256_1",
type: "uint256",
},
],
indexed: false,
internalType: "struct DataTypesInput.Struct1",
name: "input2",
type: "tuple",
},
],
name: "event_struct_2",
type: "event",
},
{
inputs: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,31 @@ const _abi = [
name: "Event4",
type: "event",
},
{
anonymous: false,
inputs: [
{
components: [
{
internalType: "uint256",
name: "index",
type: "uint256",
},
{
internalType: "string",
name: "name",
type: "string",
},
],
indexed: false,
internalType: "struct Events.EventData[2]",
name: "data",
type: "tuple[2]",
},
],
name: "Event5",
type: "event",
},
{
anonymous: false,
inputs: [],
Expand Down Expand Up @@ -176,6 +201,13 @@ const _abi = [
stateMutability: "nonpayable",
type: "function",
},
{
inputs: [],
name: "emit_event5",
outputs: [],
stateMutability: "nonpayable",
type: "function",
},
] as const;

export class Events__factory {
Expand Down
92 changes: 66 additions & 26 deletions packages/target-ethers-v5-test/types/v0.6.4/DataTypesInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import type {
Signer,
utils,
} from "ethers";
import type { FunctionFragment, Result } from "@ethersproject/abi";
import type {
FunctionFragment,
Result,
EventFragment,
} from "@ethersproject/abi";
import type { Listener, Provider } from "@ethersproject/providers";
import type {
TypedEventFilter,
Expand All @@ -20,21 +24,6 @@ import type {
OnEvent,
} from "../common";

export declare namespace StructsLib1 {
export type InfoStruct = { a: BigNumberish; b: BigNumberish };

export type InfoStructOutput = [BigNumber, BigNumber] & {
a: BigNumber;
b: BigNumber;
};
}

export declare namespace StructsLib2 {
export type InfoStruct = { a: string; b: string };

export type InfoStructOutput = [string, string] & { a: string; b: string };
}

export declare namespace DataTypesInput {
export type Struct1Struct = {
uint256_0: BigNumberish;
Expand All @@ -61,6 +50,21 @@ export declare namespace DataTypesInput {
export type Struct3StructOutput = [BigNumber[]] & { input1: BigNumber[] };
}

export declare namespace StructsLib1 {
export type InfoStruct = { a: BigNumberish; b: BigNumberish };

export type InfoStructOutput = [BigNumber, BigNumber] & {
a: BigNumber;
b: BigNumber;
};
}

export declare namespace StructsLib2 {
export type InfoStruct = { a: string; b: string };

export type InfoStructOutput = [string, string] & { a: string; b: string };
}

export interface DataTypesInputInterface extends utils.Interface {
functions: {
"input_address(address)": FunctionFragment;
Expand All @@ -77,16 +81,16 @@ export interface DataTypesInputInterface extends utils.Interface {
"input_struct((uint256,uint256))": FunctionFragment;
"input_struct2((uint256,(uint256,uint256)))": FunctionFragment;
"input_struct2_array((uint256,(uint256,uint256))[])": FunctionFragment;
"input_struct2_tuple(tuple[3])": FunctionFragment;
"input_struct2_tuple((uint256,(uint256,uint256))[3])": FunctionFragment;
"input_struct3_array((uint256[])[])": FunctionFragment;
"input_struct_array((uint256,uint256)[])": FunctionFragment;
"input_struct_array_array(tuple[][])": FunctionFragment;
"input_struct_array_array_array(tuple[][][])": FunctionFragment;
"input_struct_array_fixedarray(tuple[][2])": FunctionFragment;
"input_struct_fixedarray_array(tuple[2][])": FunctionFragment;
"input_struct_fixedarray_array_fixedarray(tuple[2][][3])": FunctionFragment;
"input_struct_fixedarray_array_fixedarray_array_fixedarray(tuple[2][][3][][4])": FunctionFragment;
"input_struct_fixedarray_fixedarray(tuple[2][3])": FunctionFragment;
"input_struct_array_array((uint256,uint256)[][])": FunctionFragment;
"input_struct_array_array_array((uint256,uint256)[][][])": FunctionFragment;
"input_struct_array_fixedarray((uint256,uint256)[][2])": FunctionFragment;
"input_struct_fixedarray_array((uint256,uint256)[2][])": FunctionFragment;
"input_struct_fixedarray_array_fixedarray((uint256,uint256)[2][][3])": FunctionFragment;
"input_struct_fixedarray_array_fixedarray_array_fixedarray((uint256,uint256)[2][][3][][4])": FunctionFragment;
"input_struct_fixedarray_fixedarray((uint256,uint256)[2][3])": FunctionFragment;
"input_tuple(uint256,uint256)": FunctionFragment;
"input_uint256(uint256)": FunctionFragment;
"input_uint8(uint8)": FunctionFragment;
Expand Down Expand Up @@ -387,8 +391,35 @@ export interface DataTypesInputInterface extends utils.Interface {
data: BytesLike
): Result;

events: {};
events: {
"event_struct((uint256,uint256))": EventFragment;
"event_struct_2(uint256,(uint256,uint256))": EventFragment;
};

getEvent(nameOrSignatureOrTopic: "event_struct"): EventFragment;
getEvent(nameOrSignatureOrTopic: "event_struct_2"): EventFragment;
}

export interface event_structEventObject {
input: DataTypesInput.Struct1StructOutput;
}
export type event_structEvent = TypedEvent<
[DataTypesInput.Struct1StructOutput],
event_structEventObject
>;

export type event_structEventFilter = TypedEventFilter<event_structEvent>;

export interface event_struct_2EventObject {
input: BigNumber;
input2: DataTypesInput.Struct1StructOutput;
}
export type event_struct_2Event = TypedEvent<
[BigNumber, DataTypesInput.Struct1StructOutput],
event_struct_2EventObject
>;

export type event_struct_2EventFilter = TypedEventFilter<event_struct_2Event>;

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

filters: {};
filters: {
"event_struct((uint256,uint256))"(input?: null): event_structEventFilter;
event_struct(input?: null): event_structEventFilter;

"event_struct_2(uint256,(uint256,uint256))"(
input?: null,
input2?: null
): event_struct_2EventFilter;
event_struct_2(input?: null, input2?: null): event_struct_2EventFilter;
};

estimateGas: {
input_address(
Expand Down

0 comments on commit c4720b9

Please sign in to comment.