From d5dadf7135838c998d35099adb7f31670fca4470 Mon Sep 17 00:00:00 2001 From: Mathieu Bour Date: Sat, 14 Oct 2023 10:16:21 +0200 Subject: [PATCH] fix: normalize AbiFallback.inputs type and zod --- .changeset/angry-taxis-enjoy.md | 5 +++ packages/abitype/src/abi.ts | 2 +- packages/abitype/src/zod.test-d.ts | 53 ++++++++++++++++++++++++++++++ packages/abitype/src/zod.ts | 2 +- 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 .changeset/angry-taxis-enjoy.md diff --git a/.changeset/angry-taxis-enjoy.md b/.changeset/angry-taxis-enjoy.md new file mode 100644 index 00000000..71df33b9 --- /dev/null +++ b/.changeset/angry-taxis-enjoy.md @@ -0,0 +1,5 @@ +--- +"abitype": patch +--- + +normalize AbiFallback.inputs type and zod diff --git a/packages/abitype/src/abi.ts b/packages/abitype/src/abi.ts index 3ae275d8..669fe5bf 100644 --- a/packages/abitype/src/abi.ts +++ b/packages/abitype/src/abi.ts @@ -163,7 +163,7 @@ export type AbiConstructor = { /** ABI ["fallback"](https://docs.soliditylang.org/en/latest/abi-spec.html#json) type */ export type AbiFallback = { type: 'fallback' - inputs?: readonly [] | undefined + inputs?: readonly never[] | undefined /** * @deprecated use `payable` or `nonpayable` from {@link AbiStateMutability} instead * @see https://github.com/ethereum/solidity/issues/992 diff --git a/packages/abitype/src/zod.test-d.ts b/packages/abitype/src/zod.test-d.ts index c13f607b..5c861ff3 100644 --- a/packages/abitype/src/zod.test-d.ts +++ b/packages/abitype/src/zod.test-d.ts @@ -5,18 +5,23 @@ import type { AbiConstructor, AbiError, AbiEvent, + AbiFallback, + AbiFunction, AbiParameter, } from './abi.js' import { customSolidityErrorsAbi, ensRegistryWithFallbackAbi, erc20Abi, + wethAbi, } from './abis/json.js' import { Abi as AbiSchema, AbiConstructor as AbiConstructorSchema, AbiError as AbiErrorSchema, AbiEvent as AbiEventSchema, + AbiFallback as AbiFallbackSchema, + AbiFunction as AbiFunctionSchema, AbiParameter as AbiParameterSchema, } from './zod.js' @@ -87,6 +92,54 @@ describe('Zod Types', () => { }) }) + describe('AbiFunction', () => { + const approveFunction = erc20Abi[3] + + test('assignable to AbiFunction', () => { + const parsed: AbiFunction = AbiFunctionSchema.parse(approveFunction) + type Result = typeof parsed extends AbiFunction ? true : false + expectTypeOf().toEqualTypeOf() + }) + + test('extends AbiFunction', () => { + const parsed = AbiFunctionSchema.parse(approveFunction) + type Result = typeof parsed extends AbiFunction ? true : false + expectTypeOf().toEqualTypeOf() + }) + }) + + describe('AbiFallback', () => { + const approveFunction = wethAbi[11] + + test('assignable to AbiFallback', () => { + const parsed: AbiFallback = AbiFallbackSchema.parse(approveFunction) + type Result = typeof parsed extends AbiFallback ? true : false + expectTypeOf().toEqualTypeOf() + }) + + test('extends AbiFallback', () => { + const parsed = AbiFallbackSchema.parse(approveFunction) + type Result = typeof parsed extends AbiFallback ? true : false + expectTypeOf().toEqualTypeOf() + }) + }) + + describe('AbiFunction', () => { + const approveFunction = erc20Abi[3] + + test('assignable to AbiFunction', () => { + const parsed: AbiFunction = AbiFunctionSchema.parse(approveFunction) + type Result = typeof parsed extends AbiFunction ? true : false + expectTypeOf().toEqualTypeOf() + }) + + test('extends AbiFunction', () => { + const parsed = AbiFunctionSchema.parse(approveFunction) + type Result = typeof parsed extends AbiFunction ? true : false + expectTypeOf().toEqualTypeOf() + }) + }) + describe('AbiParameter', () => { const approvalOwnerParameter = erc20Abi[0].inputs[0] diff --git a/packages/abitype/src/zod.ts b/packages/abitype/src/zod.ts index b074f9f5..75a224e3 100644 --- a/packages/abitype/src/zod.ts +++ b/packages/abitype/src/zod.ts @@ -164,7 +164,7 @@ export const AbiFallback = z.preprocess( * @deprecated use `pure` or `view` from {@link AbiStateMutability} instead * https://github.com/ethereum/solidity/issues/992 */ - inputs: z.tuple([]).optional(), + inputs: z.tuple([]).readonly().optional(), /** * @deprecated use `payable` or `nonpayable` from {@link AbiStateMutability} instead * https://github.com/ethereum/solidity/issues/992