Skip to content

Commit

Permalink
feat: extends readonly to all AbiParameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathieu Bour committed Oct 9, 2023
1 parent 477c914 commit a399099
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 68 deletions.
7 changes: 6 additions & 1 deletion .changeset/seven-planes-deliver.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
29 changes: 6 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -111,23 +105,14 @@
"vitest": "^0.30.1",
"zod": "^3.22.4"
},
"contributors": [
"jxom.eth <j@wagmi.sh>",
"awkweb.eth <t@wagmi.sh>"
],
"contributors": ["jxom.eth <j@wagmi.sh>", "awkweb.eth <t@wagmi.sh>"],
"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"
},
Expand All @@ -138,9 +123,7 @@
"shiki-twoslash>shiki": "^0.14.1"
},
"peerDependencyRules": {
"ignoreMissing": [
"@algolia/client-search"
]
"ignoreMissing": ["@algolia/client-search"]
}
}
}
131 changes: 95 additions & 36 deletions src/zod.test-d.ts
Original file line number Diff line number Diff line change
@@ -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<Result>().toEqualTypeOf<true>()
describe('Abi', () => {
test('assignable to Abi', () => {
const parsed: Abi = AbiSchema.parse(erc20Abi)
type Result = typeof parsed extends Abi ? true : false
expectTypeOf<Result>().toEqualTypeOf<true>()
})

test('extends Abi', () => {
const parsed = AbiSchema.parse(erc20Abi)
type Result = typeof parsed extends Abi ? true : false
expectTypeOf<Result>().toEqualTypeOf<true>()
})
})

test('extends Abi', () => {
const parsed = AbiSchema.parse([])
type Result = typeof parsed extends Abi ? true : false
expectTypeOf<Result>().toEqualTypeOf<true>()
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<Result>().toEqualTypeOf<true>()
})

test('extends AbiConstructor', () => {
const parsed = AbiConstructorSchema.parse(ensRegistryConstructor)
type Result = typeof parsed extends AbiConstructor ? true : false
expectTypeOf<Result>().toEqualTypeOf<true>()
})
})

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<Result>().toEqualTypeOf<true>()
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<Result>().toEqualTypeOf<true>()
})

test('extends AbiError', () => {
const parsed = AbiErrorSchema.parse(approvalCallerNotOwnerNorApproved)
type Result = typeof parsed extends AbiError ? true : false
expectTypeOf<Result>().toEqualTypeOf<true>()
})
})

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<Result>().toEqualTypeOf<true>()
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<Result>().toEqualTypeOf<true>()
})

test('extends AbiEvent', () => {
const parsed = AbiEventSchema.parse(approvalEvent)
type Result = typeof parsed extends AbiEvent ? true : false
expectTypeOf<Result>().toEqualTypeOf<true>()
})
})

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<Result>().toEqualTypeOf<true>()
})

test('extends AbiParameter', () => {
const parsed = AbiParameterSchema.parse(approvalOwnerParameter)
type Result = typeof parsed extends AbiParameter ? true : false
expectTypeOf<Result>().toEqualTypeOf<true>()
})
})
})
16 changes: 8 additions & 8 deletions src/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const AbiParameter: z.ZodType<AbiParameterType> = z.lazy(() =>
}),
z.object({
type: z.union([SolidityTuple, SolidityArrayWithTuple]),
components: z.array(AbiParameter),
components: z.array(AbiParameter).readonly(),
}),
]),
),
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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(),
})

Expand Down Expand Up @@ -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'),
Expand Down

0 comments on commit a399099

Please # to comment.