Skip to content

Commit

Permalink
Merge branch 'main' into feat/param-validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Raiden1411 committed Jun 15, 2023
2 parents c397940 + da06adb commit e90ae65
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 64 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ jobs:
name: Types
runs-on: ubuntu-latest
timeout-minutes: 5
strategy:
matrix:
version: ['5.0.4', '5.1.3', 'latest']

steps:
- name: Clone repository
Expand All @@ -45,6 +48,9 @@ jobs:
- name: Install dependencies
uses: ./.github/actions/install-dependencies

- name: Use `typescript@${{ matrix.version }}`
run: pnpm add -D -w typescript@${{ matrix.version }}

- name: Build
run: pnpm build

Expand All @@ -58,6 +64,9 @@ jobs:
name: Test
runs-on: ubuntu-latest
timeout-minutes: 5
strategy:
matrix:
version: ['5.0.4', '5.1.3', 'latest']

steps:
- name: Clone repository
Expand All @@ -66,6 +75,9 @@ jobs:
- name: Install dependencies
uses: ./.github/actions/install-dependencies

- name: Use `typescript@${{ matrix.version }}`
run: pnpm add -D -w typescript@${{ matrix.version }}

- name: Run tests
run: pnpm test:cov

Expand Down
11 changes: 8 additions & 3 deletions examples/reads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,21 @@ export declare function reads<
functionName extends string,
const args extends readonly unknown[] | undefined,
contracts extends Contract<abi, functionName, args>[],
>(parameters: ReadsParameters<contracts>): ContractsReturnType<contracts>
>(parameters: ReadsParameters<contracts>): ReadsResult<contracts>

export declare function useReads<
const abi extends Abi | readonly unknown[], // `readonly unknown[]` allows for non-const asserted types
functionName extends string,
const args extends readonly unknown[] | undefined,
contracts extends Contract<abi, functionName, args>[],
>(
parameters: DeepPartial<ReadsParameters<contracts>, 3>,
): ContractsReturnType<contracts>
// TODO(@tmm): Broke for `[email protected]` https://github.com/wagmi-dev/abitype/issues/153
// parameters: DeepPartial<ReadsParameters<contracts>, 3>,
parameters: DeepPartial<
{ contracts: readonly [...DeepPartial<ContractsParameters<contracts>, 2>] },
1
>,
): ReadsResult<contracts>

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Expand Down
32 changes: 8 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,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 All @@ -102,27 +96,19 @@
"rimraf": "^5.0.1",
"rome": "^12.1.3",
"simple-git-hooks": "^2.8.1",
"typescript": "^5.0.4",
"typescript": "5.0.4",
"[email protected]": "npm:[email protected]",
"vitest": "^0.30.1",
"zod": "^3.20.6"
},
"contributors": [
"jxom.eth <[email protected]>",
"awkweb.eth <[email protected]>"
],
"contributors": ["jxom.eth <[email protected]>", "awkweb.eth <[email protected]>"],
"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 @@ -133,9 +119,7 @@
"shiki-twoslash>shiki": "^0.14.1"
},
"peerDependencyRules": {
"ignoreMissing": [
"@algolia/client-search"
]
"ignoreMissing": ["@algolia/client-search"]
}
}
}
21 changes: 12 additions & 9 deletions pnpm-lock.yaml

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

49 changes: 21 additions & 28 deletions src/human-readable/parseAbi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Abi } from '../abi.js'
import type { Narrow } from '../narrow.js'
import type { Error, Filter } from '../types.js'
import { isStructSignature } from './runtime/signatures.js'
import { parseStructs } from './runtime/structs.js'
Expand All @@ -23,27 +22,26 @@ import type { ParseSignature } from './types/utils.js'
* ]
* >
*/
export type ParseAbi<
TSignatures extends readonly string[] | readonly unknown[],
> = string[] extends TSignatures
? Abi // If `T` was not able to be inferred (e.g. just `string[]`), return `Abi`
: TSignatures extends readonly string[]
? TSignatures extends Signatures<TSignatures> // Validate signatures
? ParseStructs<TSignatures> extends infer Structs
? {
[K in keyof TSignatures]: TSignatures[K] extends string
? ParseSignature<TSignatures[K], Structs>
export type ParseAbi<TSignatures extends readonly string[]> =
string[] extends TSignatures
? Abi // If `T` was not able to be inferred (e.g. just `string[]`), return `Abi`
: TSignatures extends readonly string[]
? TSignatures extends Signatures<TSignatures> // Validate signatures
? ParseStructs<TSignatures> extends infer Structs
? {
[K in keyof TSignatures]: TSignatures[K] extends string
? ParseSignature<TSignatures[K], Structs>
: never
} extends infer Mapped extends readonly unknown[]
? Filter<Mapped, never> extends infer Result
? Result extends readonly []
? never
: Result
: never
} extends infer Mapped extends readonly unknown[]
? Filter<Mapped, never> extends infer Result
? Result extends readonly []
? never
: Result
: never
: never
: never
: never
: never

/**
* Parses human-readable ABI into JSON {@link Abi}
Expand All @@ -58,17 +56,12 @@ export type ParseAbi<
* 'event Transfer(address indexed from, address indexed to, uint256 amount)',
* ])
*/
export function parseAbi<
TSignatures extends readonly string[] | readonly unknown[],
>(
signatures: Narrow<TSignatures> &
(TSignatures extends readonly string[]
? TSignatures extends readonly [] // empty array
? Error<'At least one signature required.'>
: string[] extends TSignatures
? unknown
: Signatures<TSignatures>
: never),
export function parseAbi<const TSignatures extends readonly string[]>(
signatures: TSignatures['length'] extends 0
? Error<'At least one signature required'>
: Signatures<TSignatures> extends TSignatures
? TSignatures
: Signatures<TSignatures>,
): ParseAbi<TSignatures> {
const structs = parseStructs(signatures as readonly string[])
const abi = []
Expand Down
2 changes: 2 additions & 0 deletions src/human-readable/types/signatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ export type Signature<
T extends string,
K extends string | unknown = unknown,
> = IsSignature<T> extends true
? T
: string extends T // if exactly `string` (not narrowed), then pass through as valid
? T
: Error<`Signature "${T}" is invalid${K extends string
? ` at position ${K}`
Expand Down

0 comments on commit e90ae65

Please sign in to comment.