diff --git a/.changeset/seven-planes-deliver.md b/.changeset/seven-planes-deliver.md index 644e9e3c..b11df3bb 100644 --- a/.changeset/seven-planes-deliver.md +++ b/.changeset/seven-planes-deliver.md @@ -2,4 +2,9 @@ "abitype": patch --- -Changed AbiEvent.inputs to readonly to match AbiEvent type +Changed the following types to readonly in zod package: + +- `AbiContructor.inputs` +- `AbiError.inputs` +- `AbiEvent.inputs` +- `AbiFunction.inputs` / `AbiFunction.outputs` \ No newline at end of file diff --git a/package.json b/package.json index 7d254f0c..ab4c0d10 100644 --- a/package.json +++ b/package.json @@ -70,15 +70,9 @@ }, "typesVersions": { "*": { - "config": [ - "./dist/types/config.d.ts" - ], - "test": [ - "./dist/types/test.d.ts" - ], - "zod": [ - "./dist/types/zod.d.ts" - ] + "config": ["./dist/types/config.d.ts"], + "test": ["./dist/types/test.d.ts"], + "zod": ["./dist/types/zod.d.ts"] } }, "peerDependencies": { @@ -111,23 +105,14 @@ "vitest": "^0.30.1", "zod": "^3.22.4" }, - "contributors": [ - "jxom.eth ", - "awkweb.eth " - ], + "contributors": ["jxom.eth ", "awkweb.eth "], "funding": [ { "type": "github", "url": "https://github.com/sponsors/wagmi-dev" } ], - "keywords": [ - "abi", - "eth", - "ethereum", - "typescript", - "web3" - ], + "keywords": ["abi", "eth", "ethereum", "typescript", "web3"], "simple-git-hooks": { "pre-commit": "pnpm format && pnpm lint:fix" }, @@ -138,9 +123,7 @@ "shiki-twoslash>shiki": "^0.14.1" }, "peerDependencyRules": { - "ignoreMissing": [ - "@algolia/client-search" - ] + "ignoreMissing": ["@algolia/client-search"] } } } diff --git a/src/zod.test-d.ts b/src/zod.test-d.ts index 6a84d13c..e6072296 100644 --- a/src/zod.test-d.ts +++ b/src/zod.test-d.ts @@ -1,47 +1,106 @@ -import type { Abi, AbiEvent } from './abi.js' -import { Abi as AbiSchema, AbiEvent as AbiEventSchema } from './zod.js' +import type { + Abi, + AbiConstructor, + AbiError, + AbiEvent, + AbiParameter, +} from './abi.js' +import { + customSolidityErrorsAbi, + ensRegistryWithFallbackAbi, + erc20Abi, +} from './test/abis.js' +import { + Abi as AbiSchema, + AbiConstructor as AbiConstructorSchema, + AbiError as AbiErrorSchema, + AbiEvent as AbiEventSchema, + AbiParameter as AbiParameterSchema, +} from './zod.js' import { describe, expectTypeOf, test } from 'vitest' describe('Zod Types', () => { - test('assignable to Abi', () => { - const parsed: Abi = AbiSchema.parse([]) - type Result = typeof parsed extends Abi ? true : false - expectTypeOf().toEqualTypeOf() + describe('Abi', () => { + test('assignable to Abi', () => { + const parsed: Abi = AbiSchema.parse(erc20Abi) + type Result = typeof parsed extends Abi ? true : false + expectTypeOf().toEqualTypeOf() + }) + + test('extends Abi', () => { + const parsed = AbiSchema.parse(erc20Abi) + type Result = typeof parsed extends Abi ? true : false + expectTypeOf().toEqualTypeOf() + }) }) - test('extends Abi', () => { - const parsed = AbiSchema.parse([]) - type Result = typeof parsed extends Abi ? true : false - expectTypeOf().toEqualTypeOf() + describe('AbiConstructor', () => { + const ensRegistryConstructor = ensRegistryWithFallbackAbi[0] + + test('assignable to AbiConstructor', () => { + const parsed: AbiConstructor = AbiConstructorSchema.parse( + ensRegistryConstructor, + ) + type Result = typeof parsed extends AbiConstructor ? true : false + expectTypeOf().toEqualTypeOf() + }) + + test('extends AbiConstructor', () => { + const parsed = AbiConstructorSchema.parse(ensRegistryConstructor) + type Result = typeof parsed extends AbiConstructor ? true : false + expectTypeOf().toEqualTypeOf() + }) }) - test('assignable to AbiEvent', () => { - const parsed: AbiEvent = AbiEventSchema.parse({ - anonymous: false, - inputs: [ - { indexed: true, name: 'owner', type: 'address' }, - { indexed: true, name: 'spender', type: 'address' }, - { indexed: false, name: 'value', type: 'uint256' }, - ], - name: 'Approval', - type: 'event', - }) - type Result = typeof parsed extends AbiEvent ? true : false - expectTypeOf().toEqualTypeOf() + describe('AbiError', () => { + const approvalCallerNotOwnerNorApproved = customSolidityErrorsAbi[1] + + test('assignable to AbiError', () => { + const parsed: AbiError = AbiErrorSchema.parse( + approvalCallerNotOwnerNorApproved, + ) + type Result = typeof parsed extends AbiError ? true : false + expectTypeOf().toEqualTypeOf() + }) + + test('extends AbiError', () => { + const parsed = AbiErrorSchema.parse(approvalCallerNotOwnerNorApproved) + type Result = typeof parsed extends AbiError ? true : false + expectTypeOf().toEqualTypeOf() + }) }) - test('extends Abi', () => { - const parsed = AbiEventSchema.parse({ - anonymous: false, - inputs: [ - { indexed: true, name: 'owner', type: 'address' }, - { indexed: true, name: 'spender', type: 'address' }, - { indexed: false, name: 'value', type: 'uint256' }, - ], - name: 'Approval', - type: 'event', - }) - type Result = typeof parsed extends AbiEvent ? true : false - expectTypeOf().toEqualTypeOf() + describe('AbiEvent', () => { + const approvalEvent = erc20Abi[0] + + test('assignable to AbiEvent', () => { + const parsed: AbiEvent = AbiEventSchema.parse(approvalEvent) + type Result = typeof parsed extends AbiEvent ? true : false + expectTypeOf().toEqualTypeOf() + }) + + test('extends AbiEvent', () => { + const parsed = AbiEventSchema.parse(approvalEvent) + type Result = typeof parsed extends AbiEvent ? true : false + expectTypeOf().toEqualTypeOf() + }) + }) + + describe('AbiParameter', () => { + const approvalOwnerParameter = erc20Abi[0].inputs[0] + + test('assignable to AbiParameter', () => { + const parsed: AbiParameter = AbiParameterSchema.parse( + approvalOwnerParameter, + ) + type Result = typeof parsed extends AbiParameter ? true : false + expectTypeOf().toEqualTypeOf() + }) + + test('extends AbiParameter', () => { + const parsed = AbiParameterSchema.parse(approvalOwnerParameter) + type Result = typeof parsed extends AbiParameter ? true : false + expectTypeOf().toEqualTypeOf() + }) }) }) diff --git a/src/zod.ts b/src/zod.ts index 5aa5dfe0..f5f6c01f 100644 --- a/src/zod.ts +++ b/src/zod.ts @@ -71,7 +71,7 @@ export const AbiParameter: z.ZodType = z.lazy(() => }), z.object({ type: z.union([SolidityTuple, SolidityArrayWithTuple]), - components: z.array(AbiParameter), + components: z.array(AbiParameter).readonly(), }), ]), ), @@ -110,9 +110,9 @@ export const AbiFunction = z.preprocess( * https://github.com/vyperlang/vyper/issues/2151 */ gas: z.number().optional(), - inputs: z.array(AbiParameter), + inputs: z.array(AbiParameter).readonly(), name: Identifier, - outputs: z.array(AbiParameter), + outputs: z.array(AbiParameter).readonly(), /** * @deprecated use `payable` or `nonpayable` from {@link AbiStateMutability} instead * https://github.com/ethereum/solidity/issues/992 @@ -138,7 +138,7 @@ export const AbiConstructor = z.preprocess( * @deprecated use `pure` or `view` from {@link AbiStateMutability} instead * https://github.com/ethereum/solidity/issues/992 */ - inputs: z.array(AbiParameter), + inputs: z.array(AbiParameter).readonly(), /** * @deprecated use `payable` or `nonpayable` from {@link AbiStateMutability} instead * https://github.com/ethereum/solidity/issues/992 @@ -188,7 +188,7 @@ export const AbiEvent = z.object({ export const AbiError = z.object({ type: z.literal('error'), - inputs: z.array(AbiParameter), + inputs: z.array(AbiParameter).readonly(), name: z.string(), }) @@ -263,14 +263,14 @@ export const Abi = z.array( z.discriminatedUnion('type', [ z.object({ type: z.literal('function'), - inputs: z.array(AbiParameter), + inputs: z.array(AbiParameter).readonly(), name: z.string().regex(/[a-zA-Z$_][a-zA-Z0-9$_]*/), - outputs: z.array(AbiParameter), + outputs: z.array(AbiParameter).readonly(), stateMutability: AbiStateMutability, }), z.object({ type: z.literal('constructor'), - inputs: z.array(AbiParameter), + inputs: z.array(AbiParameter).readonly(), stateMutability: z.union([ z.literal('payable'), z.literal('nonpayable'),