Skip to content

Commit

Permalink
test: types
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Aug 18, 2023
1 parent fe3a928 commit d9dbe9f
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 25 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"rome": "^12.1.3",
"simple-git-hooks": "^2.8.1",
"typescript": "5.0.4",
"viem": "0.0.0-w-20230818210302",
"viem": "0.0.0-w-20230818230619",
"vite": "^4.3.9",
"vitest": "^0.34.1"
},
Expand All @@ -60,7 +60,7 @@
"overrides": {
"@wagmi/connectors": "workspace:*",
"@wagmi/core": "workspace:*",
"viem": "0.0.0-w-20230818210302"
"viem": "0.0.0-w-20230818230619"
},
"patchedDependencies": {
"vitepress@1.0.0-rc.4": "patches/vitepress@1.0.0-rc.4.patch"
Expand Down
33 changes: 33 additions & 0 deletions packages/core/src/actions/multicall.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { abi, config } from '@wagmi/test'
import { type Address, parseAbi } from 'viem'
import { expectTypeOf, test } from 'vitest'

import { multicall } from './multicall.js'
Expand Down Expand Up @@ -55,3 +56,35 @@ test('allowFailure', async () => {
})
expectTypeOf(result).toEqualTypeOf<[bigint, string]>()
})

test('MulticallParameters', async () => {
const abi = parseAbi([
'function foo() view returns (int8)',
'function foo(address) view returns (string)',
'function foo(address, address) view returns ((address foo, address bar))',
'function bar() view returns (int8)',
])

type Result = Parameters<
typeof multicall<
typeof config,
[
{
address: '0x'
abi: typeof abi
functionName: 'foo'
},
]
>
>[1]['contracts'][0]
expectTypeOf<Result>().toEqualTypeOf<{
address: Address
abi: typeof abi
functionName: 'foo' | 'bar'
args?:
| readonly []
| readonly [Address]
| readonly [Address, Address]
| undefined
}>()
})
2 changes: 1 addition & 1 deletion packages/core/src/actions/switchChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import type { BaseError } from '../errors/base.js'
import { ChainNotConfiguredError } from '../errors/config.js'
import { SwitchChainNotSupportedError } from '../errors/connector.js'
import { type ProviderNotFoundError } from '../errors/connector.js'
import type { Evaluate } from '../internal.js'
import type { ConnectorParameter } from '../types/properties.js'
import type { Evaluate } from '../types/utils.js'

export type SwitchChainParameters<
config extends Config = Config,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/query/readContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
readContracts,
} from '../actions/readContracts.js'
import type { Config } from '../config.js'
import type { ExactPartial } from '../internal.js'
import type { ChainIdParameter } from '../types/properties.js'
import type { ExactPartial } from '../types/utils.js'
import type { ScopeKeyParameter } from './types.js'
import { filterQueryOptions } from './utils.js'

Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/useContractRead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export type UseContractReadReturnType<
selectData = ReadContractData<abi, functionName>,
> = UseQueryResult<selectData, ReadContractError>

// /** https://wagmi.sh/react/hooks/useContractRead */
/** https://wagmi.sh/react/hooks/useContractRead */
export function useContractRead<
const abi extends Abi | readonly unknown[],
functionName extends string,
Expand Down
12 changes: 6 additions & 6 deletions packages/react/src/hooks/useContractReads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
readContractsQueryOptions,
} from '@wagmi/core/query'
import { useMemo } from 'react'
import type { ContractParameters, MulticallContract } from 'viem'
import type { MulticallContract } from 'viem'

import {
type UseQueryParameters,
Expand Down Expand Up @@ -39,7 +39,7 @@ export type UseContractReadsReturnType<
selectData = ReadContractsData<contracts, allowFailure>,
> = UseQueryResult<selectData, ReadContractError>

// /** https://wagmi.sh/react/hooks/useContractReads */
/** https://wagmi.sh/react/hooks/useContractReads */
export function useContractReads<
const contracts extends readonly unknown[],
allowFailure extends boolean = true,
Expand All @@ -58,11 +58,11 @@ export function useContractReads<
const queryOptions = readContractsQueryOptions(config, {
...parameters,
chainId,
contracts: contracts as ContractParameters[],
contracts,
})
const enabled = useMemo(() => {
let isContractsValid = false
for (const contract of contracts as ContractParameters[]) {
for (const contract of contracts as MulticallContract[]) {
const { abi, address, functionName } = contract
if (!abi || !address || !functionName) {
isContractsValid = false
Expand All @@ -75,8 +75,8 @@ export function useContractReads<

return useQuery({
...queryOptions,
...(query as any),
...query,
enabled,
structuralSharing: query.structuralSharing ?? structuralSharing,
}) as UseContractReadsReturnType<contracts, allowFailure, selectData>
})
}
38 changes: 38 additions & 0 deletions packages/register-tests/react/src/useContractReads.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { abi } from '@wagmi/test'
import type { Address } from 'viem'
import { expectTypeOf, test } from 'vitest'
import { type UseContractReadsParameters } from 'wagmi'

import type { ChainId } from './config.js'

test('UseContractReadsParameters', () => {
type Result = UseContractReadsParameters<
[
{
abi: typeof abi.erc20
functionName: 'balanceOf'
address: Address
args: readonly [Address]
},
]
>['contracts']
expectTypeOf<Result>().toMatchTypeOf<
| readonly [
{
abi?: typeof abi.erc20 | undefined
functionName?:
| 'symbol'
| 'name'
| 'allowance'
| 'balanceOf'
| 'decimals'
| 'totalSupply'
| undefined
address?: Address | undefined
args?: readonly [Address] | undefined
chainId?: ChainId | undefined
},
]
| undefined
>()
})
28 changes: 14 additions & 14 deletions pnpm-lock.yaml

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

1 comment on commit d9dbe9f

@vercel
Copy link

@vercel vercel bot commented on d9dbe9f Aug 18, 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:

wagmi-v2 – ./docs

wagmi-v2-git-alpha-wagmi-dev.vercel.app
wagmi-v2-wagmi-dev.vercel.app
wagmi-v2.vercel.app
alpha.wagmi.sh

Please sign in to comment.