Skip to content

Commit

Permalink
fix: changes zod schemas inputs/outputs to readonly to match abit…
Browse files Browse the repository at this point in the history
…ype (#194)

* fix: change zod AbiEvent.inputs to readonly to match AbiType.inputs

* feat: extends readonly to all AbiParameter

* docs: document that abitype must be built before testing types

* chore(dependencies): rollback tpo pnpm v8.3.1 and typescript v5.0.4

* docs: explicitly specify pnpm and TypeScript versions in CONTRIBUTING.md

* docs: fix TypeScript typo

* docs: revert explicit pnpm and TS versions, add suggested note
  • Loading branch information
Mathieu Bour authored Oct 9, 2023
1 parent 529da5c commit 380c9d9
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 48 deletions.
10 changes: 10 additions & 0 deletions .changeset/seven-planes-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"abitype": patch
---

Changed the following types to readonly in zod package:

- `AbiContructor.inputs`
- `AbiError.inputs`
- `AbiEvent.inputs`
- `AbiFunction.inputs` / `AbiFunction.outputs`
4 changes: 4 additions & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ pnpm install

After the install completes, pnpm links packages across the project for development and [git hooks](https://github.com/toplenboren/simple-git-hooks) are set up.

> **Note:** In case you have to install new packages or upgrade packages make sure to use **pnpm@8.3.1** and **typescript@5.0.4**
<div align="right">
<a href="#basic-guide">&uarr; back to top</a></b>
</div>
Expand All @@ -95,6 +97,8 @@ pnpm test
pnpm test:typecheck
```

> **Note** Ensure to build the package (`pnpm build`) before running the `test:typecheck` suite.
<div align="right">
<a href="#basic-guide">&uarr; back to top</a></b>
</div>
Expand Down
33 changes: 8 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,14 @@
},
"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": {
"typescript": ">=5.0.4",
"zod": "^3 >=3.19.1"
"zod": "^3 >=3.22.0"
},
"peerDependenciesMeta": {
"typescript": {
Expand All @@ -109,25 +103,16 @@
"typescript": "5.0.4",
"typescript@5.1.3": "npm:typescript@5.1.3",
"vitest": "^0.30.1",
"zod": "^3.20.6"
"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"]
}
}
}
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 99 additions & 10 deletions src/zod.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,106 @@
import type { Abi } from './abi.js'
import { Abi as AbiSchema } 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>()
})
})

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

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>()
})
})
})
18 changes: 9 additions & 9 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 @@ -182,13 +182,13 @@ export const AbiReceive = z.object({
export const AbiEvent = z.object({
type: z.literal('event'),
anonymous: z.boolean().optional(),
inputs: z.array(AbiEventParameter),
inputs: z.array(AbiEventParameter).readonly(),
name: Identifier,
})

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

1 comment on commit 380c9d9

@vercel
Copy link

@vercel vercel bot commented on 380c9d9 Oct 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

abitype – ./

abitype-wagmi-dev.vercel.app
abitype.dev
abitype-git-main-wagmi-dev.vercel.app
abitype.vercel.app

Please sign in to comment.