From 2ae43ea0491d44d3abc712d5ef80c24db0b87e54 Mon Sep 17 00:00:00 2001 From: Tom Meagher Date: Wed, 23 Aug 2023 11:28:46 -0400 Subject: [PATCH] feat: hook config prop --- package.json | 2 +- packages/core/src/actions/getTransaction.ts | 12 ++--- packages/core/src/actions/multicall.ts | 4 +- .../src/actions/prepareSendTransaction.ts | 14 ++--- packages/core/src/actions/readContracts.ts | 4 +- packages/core/src/actions/sendTransaction.ts | 8 ++- packages/core/src/actions/simulateContract.ts | 14 +++-- .../src/actions/waitForTransactionReceipt.ts | 12 ++--- .../core/src/actions/writeContract.test-d.ts | 21 +++++--- packages/core/src/actions/writeContract.ts | 15 +++--- packages/core/src/query/getTransaction.ts | 12 ++--- .../core/src/query/prepareSendTransaction.ts | 12 ++--- packages/core/src/query/sendTransaction.ts | 6 +-- .../src/query/waitForTransactionReceipt.ts | 12 ++--- packages/core/src/query/writeContract.ts | 52 +++++++------------ packages/core/src/types/chain.test-d.ts | 21 ++++---- packages/core/src/types/chain.ts | 26 +++++----- packages/react/src/hooks/useContractWrite.ts | 47 ++++++++++------- .../src/hooks/usePrepareSendTransaction.ts | 42 +++++++++------ .../react/src/hooks/useSendTransaction.ts | 47 ++++++++++------- packages/react/src/hooks/useTransaction.ts | 6 +-- .../src/hooks/useWaitForTransactionReceipt.ts | 12 +++-- .../react/src/useContractWrite.test-d.ts | 4 +- .../react/src/useSendTransaction.test-d.ts | 20 ++++--- 24 files changed, 223 insertions(+), 202 deletions(-) diff --git a/package.json b/package.json index 908bfbacb6..f8ea3e6983 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "postinstall": "pnpm preconstruct", "preconstruct": "bun scripts/preconstruct.ts", "preinstall": "npx only-allow@1.1.1 pnpm", - "prepare": "npx simple-git-hooks", + "prepare": "pnpm simple-git-hooks", "prepublishOnly": "bun scripts/formatPackageJson.ts", "test": "vitest", "test:build": "pnpm run --r --parallel test:build", diff --git a/packages/core/src/actions/getTransaction.ts b/packages/core/src/actions/getTransaction.ts index 420c7ddbbe..117275728a 100644 --- a/packages/core/src/actions/getTransaction.ts +++ b/packages/core/src/actions/getTransaction.ts @@ -12,18 +12,14 @@ import { type Evaluate } from '../types/utils.js' export type GetTransactionParameters< config extends Config = Config, - chainId extends - | config['chains'][number]['id'] - | undefined = config['chains'][number]['id'], + chainId extends config['chains'][number]['id'] = config['chains'][number]['id'], > = Evaluate> export type GetTransactionReturnType< config extends Config = Config, - chainId extends - | config['chains'][number]['id'] - | undefined = config['chains'][number]['id'], + chainId extends config['chains'][number]['id'] = config['chains'][number]['id'], /// - chains extends readonly Chain[] = SelectChains, + chains extends readonly Chain[] = SelectChains, > = Evaluate< { [key in keyof chains]: viem_GetTransactionReturnType @@ -35,7 +31,7 @@ export type GetTransactionError = Error /** https://wagmi.sh/core/actions/getTransaction */ export function getTransaction< config extends Config, - chainId extends config['chains'][number]['id'] | undefined, + chainId extends config['chains'][number]['id'], >( config: config, parameters: GetTransactionParameters, diff --git a/packages/core/src/actions/multicall.ts b/packages/core/src/actions/multicall.ts index 8f7601c7a2..d1cd723327 100644 --- a/packages/core/src/actions/multicall.ts +++ b/packages/core/src/actions/multicall.ts @@ -9,9 +9,9 @@ import type { Config } from '../config.js' import type { ChainIdParameter } from '../types/properties.js' export type MulticallParameters< - config extends Config = Config, contracts extends readonly unknown[] = readonly ContractFunctionParameters[], allowFailure extends boolean = true, + config extends Config = Config, > = viem_MulticallParameters & ChainIdParameter export type MulticallReturnType< @@ -25,7 +25,7 @@ export async function multicall< allowFailure extends boolean = true, >( config: config, - parameters: MulticallParameters, + parameters: MulticallParameters, ): Promise> { const { allowFailure = true, chainId, contracts, ...rest } = parameters const client = config.getClient({ chainId }) diff --git a/packages/core/src/actions/prepareSendTransaction.ts b/packages/core/src/actions/prepareSendTransaction.ts index ed46f81128..e829147052 100644 --- a/packages/core/src/actions/prepareSendTransaction.ts +++ b/packages/core/src/actions/prepareSendTransaction.ts @@ -13,11 +13,9 @@ import { getConnectorClient } from './getConnectorClient.js' export type PrepareSendTransactionParameters< config extends Config = Config, - chainId extends - | config['chains'][number]['id'] - | undefined = config['chains'][number]['id'], + chainId extends config['chains'][number]['id'] = config['chains'][number]['id'], /// - chains extends readonly Chain[] = SelectChains, + chains extends readonly Chain[] = SelectChains, > = Evaluate< { [key in keyof chains]: Omit< @@ -33,11 +31,9 @@ export type PrepareSendTransactionParameters< export type PrepareSendTransactionReturnType< config extends Config = Config, - chainId extends - | config['chains'][number]['id'] - | undefined = config['chains'][number]['id'], + chainId extends config['chains'][number]['id'] = config['chains'][number]['id'], /// - chains extends readonly Chain[] = SelectChains, + chains extends readonly Chain[] = SelectChains, > = Evaluate< { [key in keyof chains]: Omit< @@ -56,7 +52,7 @@ export type PrepareSendTransactionError = Error /** https://wagmi.sh/core/actions/prepareSendTransaction */ export async function prepareSendTransaction< config extends Config, - chainId extends config['chains'][number]['id'] | undefined, + chainId extends config['chains'][number]['id'], >( config: config, parameters: PrepareSendTransactionParameters, diff --git a/packages/core/src/actions/readContracts.ts b/packages/core/src/actions/readContracts.ts index bb63881117..63c9d6d455 100644 --- a/packages/core/src/actions/readContracts.ts +++ b/packages/core/src/actions/readContracts.ts @@ -11,9 +11,9 @@ import { multicall } from './multicall.js' import { readContract } from './readContract.js' export type ReadContractsParameters< - config extends Config = Config, contracts extends readonly unknown[] = readonly ContractFunctionParameters[], allowFailure extends boolean = true, + config extends Config = Config, > = viem_MulticallParameters< contracts, allowFailure, @@ -33,7 +33,7 @@ export async function readContracts< allowFailure extends boolean = true, >( config: config, - parameters: ReadContractsParameters, + parameters: ReadContractsParameters, ): Promise> { const { allowFailure = true, blockNumber, blockTag, ...rest } = parameters const contracts = parameters.contracts as (ContractFunctionParameters & { diff --git a/packages/core/src/actions/sendTransaction.ts b/packages/core/src/actions/sendTransaction.ts index 687194e125..51e91741b9 100644 --- a/packages/core/src/actions/sendTransaction.ts +++ b/packages/core/src/actions/sendTransaction.ts @@ -19,11 +19,9 @@ import { getConnectorClient } from './getConnectorClient.js' export type SendTransactionParameters< config extends Config = Config, - chainId extends - | config['chains'][number]['id'] - | undefined = config['chains'][number]['id'], + chainId extends config['chains'][number]['id'] = config['chains'][number]['id'], /// - chains extends readonly Chain[] = SelectChains, + chains extends readonly Chain[] = SelectChains, > = { [key in keyof chains]: Evaluate< Omit< @@ -48,7 +46,7 @@ export type SendTransactionError = Error /** https://wagmi.sh/core/actions/sendTransaction */ export async function sendTransaction< config extends Config, - chainId extends config['chains'][number]['id'] | undefined, + chainId extends config['chains'][number]['id'], >( config: config, parameters: SendTransactionParameters, diff --git a/packages/core/src/actions/simulateContract.ts b/packages/core/src/actions/simulateContract.ts index a0aa2bb329..194afe5bbe 100644 --- a/packages/core/src/actions/simulateContract.ts +++ b/packages/core/src/actions/simulateContract.ts @@ -35,9 +35,11 @@ export type SimulateContractParameters< functionName > = ContractFunctionArgs, config extends Config = Config, - chainId extends config['chains'][number]['id'] | undefined = undefined, + chainId extends + | config['chains'][number]['id'] + | undefined = config['chains'][number]['id'], /// - chains extends readonly Chain[] = SelectChains, + chains extends readonly Chain[] = SelectChains, > = { [key in keyof chains]: UnionEvaluate< UnionOmit< @@ -67,9 +69,11 @@ export type SimulateContractReturnType< functionName > = ContractFunctionArgs, config extends Config = Config, - chainId extends config['chains'][number]['id'] | undefined = undefined, + chainId extends + | config['chains'][number]['id'] + | undefined = config['chains'][number]['id'], /// - chains extends readonly Chain[] = SelectChains, + chains extends readonly Chain[] = SelectChains, > = { [key in keyof chains]: viem_SimulateContractReturnType< readonly [ExtractAbiFunction], @@ -97,6 +101,7 @@ export type SimulateContractError = Error /** https://wagmi.sh/core/actions/simulateContract */ export async function simulateContract< + config extends Config, const abi extends Abi | readonly unknown[], functionName extends ContractFunctionName, args extends ContractFunctionArgs< @@ -104,7 +109,6 @@ export async function simulateContract< 'nonpayable' | 'payable', functionName >, - config extends Config, chainId extends config['chains'][number]['id'] | undefined = undefined, >( config: config, diff --git a/packages/core/src/actions/waitForTransactionReceipt.ts b/packages/core/src/actions/waitForTransactionReceipt.ts index 81233a82aa..410dd9887b 100644 --- a/packages/core/src/actions/waitForTransactionReceipt.ts +++ b/packages/core/src/actions/waitForTransactionReceipt.ts @@ -18,20 +18,16 @@ import type { Evaluate } from '../types/utils.js' export type WaitForTransactionReceiptParameters< config extends Config = Config, - chainId extends - | config['chains'][number]['id'] - | undefined = config['chains'][number]['id'], + chainId extends config['chains'][number]['id'] = config['chains'][number]['id'], > = Evaluate< viem_WaitForTransactionReceiptParameters & ChainIdParameter > export type WaitForTransactionReceiptReturnType< config extends Config = Config, - chainId extends - | config['chains'][number]['id'] - | undefined = config['chains'][number]['id'], + chainId extends config['chains'][number]['id'] = config['chains'][number]['id'], /// - chains extends readonly Chain[] = SelectChains, + chains extends readonly Chain[] = SelectChains, > = Evaluate< { [key in keyof chains]: viem_WaitForTransactionReceiptReturnType @@ -42,7 +38,7 @@ export type WaitForTransactionReceiptError = Error export async function waitForTransactionReceipt< config extends Config, - chainId extends config['chains'][number]['id'] | undefined, + chainId extends config['chains'][number]['id'], >( config: config, parameters: WaitForTransactionReceiptParameters, diff --git a/packages/core/src/actions/writeContract.test-d.ts b/packages/core/src/actions/writeContract.test-d.ts index a56b6e4246..2cbd043465 100644 --- a/packages/core/src/actions/writeContract.test-d.ts +++ b/packages/core/src/actions/writeContract.test-d.ts @@ -40,7 +40,12 @@ test('chain formatters', () => { transports: { [celo.id]: http(), [mainnet.id]: http() }, }) - type Result = WriteContractParameters + type Result = WriteContractParameters< + typeof abi.erc20, + 'transferFrom', + [Address, Address, bigint], + typeof config + > expectTypeOf().toMatchTypeOf<{ chainId?: typeof celo.id | typeof mainnet.id | undefined feeCurrency?: `0x${string}` | undefined @@ -58,10 +63,11 @@ test('chain formatters', () => { }) type Result2 = WriteContractParameters< - typeof config, - typeof celo.id, typeof abi.erc20, - 'transferFrom' + 'transferFrom', + [Address, Address, bigint], + typeof config, + typeof celo.id > expectTypeOf().toMatchTypeOf<{ functionName: 'approve' | 'transfer' | 'transferFrom' @@ -82,10 +88,11 @@ test('chain formatters', () => { }) type Result3 = WriteContractParameters< - typeof config, - typeof mainnet.id, typeof abi.erc20, - 'transferFrom' + 'transferFrom', + [Address, Address, bigint], + typeof config, + typeof mainnet.id > expectTypeOf().toMatchTypeOf<{ functionName: 'approve' | 'transfer' | 'transferFrom' diff --git a/packages/core/src/actions/writeContract.ts b/packages/core/src/actions/writeContract.ts index df4df621ac..523a58138d 100644 --- a/packages/core/src/actions/writeContract.ts +++ b/packages/core/src/actions/writeContract.ts @@ -10,6 +10,7 @@ import type { import { writeContract as viem_writeContract } from 'viem/actions' import type { Config } from '../config.js' +import type { SelectChains } from '../types/chain.js' import type { ChainIdParameter, ConnectorParameter, @@ -23,10 +24,6 @@ import { } from './simulateContract.js' export type WriteContractParameters< - config extends Config = Config, - chainId extends - | config['chains'][number]['id'] - | undefined = config['chains'][number]['id'], abi extends Abi | readonly unknown[] = Abi, functionName extends ContractFunctionName< abi, @@ -37,10 +34,10 @@ export type WriteContractParameters< 'nonpayable' | 'payable', functionName > = ContractFunctionArgs, + config extends Config = Config, + chainId extends config['chains'][number]['id'] = config['chains'][number]['id'], /// - chains extends readonly Chain[] = chainId extends config['chains'][number]['id'] - ? [Extract] - : config['chains'], + chains extends readonly Chain[] = SelectChains, > = { [key in keyof chains]: UnionOmit< viem_WriteContractParameters< @@ -69,7 +66,6 @@ export type WriteContractError = Error /** https://wagmi.sh/core/actions/writeContract */ export async function writeContract< config extends Config, - chainId extends config['chains'][number]['id'] | undefined, const abi extends Abi | readonly unknown[], functionName extends ContractFunctionName, args extends ContractFunctionArgs< @@ -77,9 +73,10 @@ export async function writeContract< 'nonpayable' | 'payable', functionName >, + chainId extends config['chains'][number]['id'], >( config: config, - parameters: WriteContractParameters, + parameters: WriteContractParameters, ): Promise { const { chainId, connector, __mode, ...rest } = parameters diff --git a/packages/core/src/query/getTransaction.ts b/packages/core/src/query/getTransaction.ts index 0a36e10fbe..a020234fe2 100644 --- a/packages/core/src/query/getTransaction.ts +++ b/packages/core/src/query/getTransaction.ts @@ -13,14 +13,14 @@ import { filterQueryOptions } from './utils.js' export type GetTransactionOptions< config extends Config, - chainId extends config['chains'][number]['id'] | undefined, + chainId extends config['chains'][number]['id'], > = Evaluate< ExactPartial> & ScopeKeyParameter > export function getTransactionQueryOptions< config extends Config, - chainId extends config['chains'][number]['id'] | undefined, + chainId extends config['chains'][number]['id'], >(config: config, options: GetTransactionOptions = {}) { return { async queryFn({ queryKey }) { @@ -54,22 +54,22 @@ export function getTransactionQueryOptions< export type GetTransactionQueryFnData< config extends Config, - chainId extends config['chains'][number]['id'] | undefined, + chainId extends config['chains'][number]['id'], > = GetTransactionReturnType export type GetTransactionData< config extends Config, - chainId extends config['chains'][number]['id'] | undefined, + chainId extends config['chains'][number]['id'], > = GetTransactionQueryFnData export function getTransactionQueryKey< config extends Config, - chainId extends config['chains'][number]['id'] | undefined, + chainId extends config['chains'][number]['id'], >(options: GetTransactionOptions = {}) { return ['transaction', filterQueryOptions(options)] as const } export type GetTransactionQueryKey< config extends Config, - chainId extends config['chains'][number]['id'] | undefined, + chainId extends config['chains'][number]['id'], > = ReturnType> diff --git a/packages/core/src/query/prepareSendTransaction.ts b/packages/core/src/query/prepareSendTransaction.ts index 7871ddd663..977537c060 100644 --- a/packages/core/src/query/prepareSendTransaction.ts +++ b/packages/core/src/query/prepareSendTransaction.ts @@ -13,7 +13,7 @@ import { filterQueryOptions } from './utils.js' export type PrepareSendTransactionOptions< config extends Config, - chainId extends config['chains'][number]['id'] | undefined, + chainId extends config['chains'][number]['id'], > = UnionPartialBy< PrepareSendTransactionParameters, keyof PrepareSendTransactionParameters @@ -22,7 +22,7 @@ export type PrepareSendTransactionOptions< export function prepareSendTransactionQueryOptions< config extends Config, - chainId extends config['chains'][number]['id'] | undefined, + chainId extends config['chains'][number]['id'], >( config: config, options: PrepareSendTransactionOptions< @@ -51,17 +51,17 @@ export function prepareSendTransactionQueryOptions< export type PrepareSendTransactionQueryFnData< config extends Config, - chainId extends config['chains'][number]['id'] | undefined, + chainId extends config['chains'][number]['id'], > = PrepareSendTransactionReturnType export type PrepareSendTransactionData< config extends Config, - chainId extends config['chains'][number]['id'] | undefined, + chainId extends config['chains'][number]['id'], > = PrepareSendTransactionQueryFnData export function prepareSendTransactionQueryKey< config extends Config, - chainId extends config['chains'][number]['id'] | undefined, + chainId extends config['chains'][number]['id'], >( options: PrepareSendTransactionOptions< config, @@ -74,5 +74,5 @@ export function prepareSendTransactionQueryKey< export type PrepareSendTransactionQueryKey< config extends Config, - chainId extends config['chains'][number]['id'] | undefined, + chainId extends config['chains'][number]['id'], > = ReturnType> diff --git a/packages/core/src/query/sendTransaction.ts b/packages/core/src/query/sendTransaction.ts index 188701ca84..3c37c5375e 100644 --- a/packages/core/src/query/sendTransaction.ts +++ b/packages/core/src/query/sendTransaction.ts @@ -28,11 +28,11 @@ export type SendTransactionData = Evaluate export type SendTransactionVariables< config extends Config, - chainId extends config['chains'][number]['id'] | undefined, + chainId extends config['chains'][number]['id'], > = SendTransactionParameters export type SendTransactionMutate = < - chainId extends config['chains'][number]['id'] | undefined, + chainId extends config['chains'][number]['id'], >( variables: SendTransactionVariables, options?: @@ -50,7 +50,7 @@ export type SendTransactionMutate = < export type SendTransactionMutateAsync< config extends Config, context = unknown, -> = ( +> = ( variables: SendTransactionVariables, options?: | Evaluate< diff --git a/packages/core/src/query/waitForTransactionReceipt.ts b/packages/core/src/query/waitForTransactionReceipt.ts index 8286a4fa33..390649ecb2 100644 --- a/packages/core/src/query/waitForTransactionReceipt.ts +++ b/packages/core/src/query/waitForTransactionReceipt.ts @@ -13,7 +13,7 @@ import { filterQueryOptions } from './utils.js' export type WaitForTransactionReceiptOptions< config extends Config, - chainId extends config['chains'][number]['id'] | undefined, + chainId extends config['chains'][number]['id'], > = Evaluate< ExactPartial> & ScopeKeyParameter @@ -21,7 +21,7 @@ export type WaitForTransactionReceiptOptions< export function waitForTransactionReceiptQueryOptions< config extends Config, - chainId extends config['chains'][number]['id'] | undefined, + chainId extends config['chains'][number]['id'], >( config: Config, options: WaitForTransactionReceiptOptions = {}, @@ -49,17 +49,17 @@ export function waitForTransactionReceiptQueryOptions< export type WaitForTransactionReceiptQueryFnData< config extends Config, - chainId extends config['chains'][number]['id'] | undefined, + chainId extends config['chains'][number]['id'], > = WaitForTransactionReceiptReturnType export type WaitForTransactionReceiptData< config extends Config, - chainId extends config['chains'][number]['id'] | undefined, + chainId extends config['chains'][number]['id'], > = WaitForTransactionReceiptQueryFnData export function waitForTransactionReceiptQueryKey< config extends Config, - chainId extends config['chains'][number]['id'] | undefined, + chainId extends config['chains'][number]['id'], >(options: WaitForTransactionReceiptOptions = {}) { const { onReplaced: _, ...rest } = options return ['waitForTransactionReceipt', filterQueryOptions(rest)] as const @@ -67,5 +67,5 @@ export function waitForTransactionReceiptQueryKey< export type WaitForTransactionReceiptQueryKey< config extends Config, - chainId extends config['chains'][number]['id'] | undefined, + chainId extends config['chains'][number]['id'], > = ReturnType> diff --git a/packages/core/src/query/writeContract.ts b/packages/core/src/query/writeContract.ts index 12064658b0..36b651ea11 100644 --- a/packages/core/src/query/writeContract.ts +++ b/packages/core/src/query/writeContract.ts @@ -22,11 +22,11 @@ export function writeContractMutationOptions( WriteContractData, WriteContractError, WriteContractVariables< - config, - config['chains'][number]['id'], Abi, string, - readonly unknown[] + readonly unknown[], + config, + config['chains'][number]['id'] > > } @@ -34,41 +34,33 @@ export function writeContractMutationOptions( export type WriteContractData = Evaluate export type WriteContractVariables< - config extends Config, - chainId extends config['chains'][number]['id'] | undefined, abi extends Abi | readonly unknown[], - functionName extends ContractFunctionName, + functionName extends ContractFunctionName, args extends ContractFunctionArgs< abi, - 'payable' | 'nonpayable', + 'nonpayable' | 'payable', functionName >, -> = WriteContractParameters + config extends Config, + chainId extends config['chains'][number]['id'], +> = WriteContractParameters export type WriteContractMutate = < - chainId extends config['chains'][number]['id'] | undefined, const abi extends Abi | readonly unknown[], - functionName extends ContractFunctionName, + functionName extends ContractFunctionName, args extends ContractFunctionArgs< abi, - 'payable' | 'nonpayable', + 'nonpayable' | 'payable', functionName >, + chainId extends config['chains'][number]['id'], >( - variables: WriteContractVariables< - config, - number extends config['chains'][number]['id'] - ? config['chains'][number]['id'] - : chainId, - abi, - functionName, - args - >, + variables: WriteContractVariables, options?: | MutateOptions< WriteContractData, WriteContractError, - WriteContractVariables, + WriteContractVariables, context > | undefined, @@ -78,29 +70,21 @@ export type WriteContractMutateAsync< config extends Config, context = unknown, > = < - chainId extends config['chains'][number]['id'] | undefined, const abi extends Abi | readonly unknown[], - functionName extends ContractFunctionName, + functionName extends ContractFunctionName, args extends ContractFunctionArgs< abi, - 'payable' | 'nonpayable', + 'nonpayable' | 'payable', functionName >, + chainId extends config['chains'][number]['id'], >( - variables: WriteContractVariables< - config, - number extends config['chains'][number]['id'] - ? config['chains'][number]['id'] - : chainId, - abi, - functionName, - args - >, + variables: WriteContractVariables, options?: | MutateOptions< WriteContractData, WriteContractError, - WriteContractVariables, + WriteContractVariables, context > | undefined, diff --git a/packages/core/src/types/chain.test-d.ts b/packages/core/src/types/chain.test-d.ts index df4653e203..76e4ee588f 100644 --- a/packages/core/src/types/chain.test-d.ts +++ b/packages/core/src/types/chain.test-d.ts @@ -1,30 +1,33 @@ -import type { SelectChains } from './chain.js' -import type { Merge } from './utils.js' import type { Chain, mainnet, optimism, sepolia } from 'viem/chains' import { expectTypeOf, test } from 'vitest' +import type { Config } from '../config.js' +import type { SelectChains } from './chain.js' +import type { Merge } from './utils.js' + test('not narrowable', () => { - type Result = SelectChains + type Result = SelectChains expectTypeOf().toEqualTypeOf() }) test('chainId', () => { - type Result = SelectChains + type Result = SelectChains< + Config, + 1 + > expectTypeOf().toEqualTypeOf() }) test('at least one chain has formatters', () => { - type Result = SelectChains + type Result = SelectChains> expectTypeOf().toEqualTypeOf< readonly [typeof mainnet, typeof optimism] >() }) test('no formatters', () => { - type Result = SelectChains + type Result = SelectChains> expectTypeOf().toEqualTypeOf< - readonly [ - Merge>, - ] + readonly [Merge] >() }) diff --git a/packages/core/src/types/chain.ts b/packages/core/src/types/chain.ts index 2727b62bc9..f2bde99d29 100644 --- a/packages/core/src/types/chain.ts +++ b/packages/core/src/types/chain.ts @@ -1,23 +1,25 @@ +import type { Chain, ChainFormatters } from 'viem' + +import type { Config } from '../config.js' import type { IsNarrowable, Merge } from './utils.js' -import type { Chain, Formatters } from 'viem' -/** Filters {@link chains} by {@link chainId} or simplifies if no `Formatters` are present. */ +/** Filters {@link Config} chains by {@link chainId} or simplifies if no `ChainFormatters` are present. */ export type SelectChains< - chains extends readonly Chain[], - chainId extends chains[number]['id'] | undefined = undefined, -> = number extends chains[number]['id'] - ? readonly [Chain] // chains not narrowable - : chainId extends chains[number]['id'] - ? readonly [Extract] // select chain - : HasFormatter extends true - ? chains - : readonly [Merge>] + config extends Config, + chainId extends config['chains'][number]['id'] | undefined = undefined, +> = Config extends config + ? readonly [Chain] // chains not inferrable, return default + : IsNarrowable extends true + ? readonly [Extract] // select specific chain + : HasFormatter extends true + ? config['chains'] // return all chains since one has formatter + : readonly [Merge] // return default chain with ID set to union (allows for more simple type since the only thing that is different is the chain ID for each chain) type HasFormatter = chains extends readonly [ infer head extends Chain, ...infer tail extends readonly Chain[], ] - ? IsNarrowable extends true + ? IsNarrowable extends true ? true : HasFormatter : false diff --git a/packages/react/src/hooks/useContractWrite.ts b/packages/react/src/hooks/useContractWrite.ts index 87fc68dc41..ff6ab96883 100644 --- a/packages/react/src/hooks/useContractWrite.ts +++ b/packages/react/src/hooks/useContractWrite.ts @@ -1,5 +1,5 @@ import { useMutation } from '@tanstack/react-query' -import type { ResolvedRegister, WriteContractError } from '@wagmi/core' +import type { Config, ResolvedRegister, WriteContractError } from '@wagmi/core' import { type WriteContractData, type WriteContractMutate, @@ -9,51 +9,60 @@ import { } from '@wagmi/core/query' import type { Abi } from 'viem' +import type { ConfigParameter } from '../types/properties.js' import type { UseMutationOptions, UseMutationResult } from '../utils/query.js' import { useConfig } from './useConfig.js' -type ChainId = ResolvedRegister['config']['chains'][number]['id'] - -export type UseContractWriteParameters = UseMutationOptions< +export type UseContractWriteParameters< + config extends Config = Config, + context = unknown, +> = UseMutationOptions< WriteContractData, WriteContractError, WriteContractVariables< - ResolvedRegister['config'], - ChainId, Abi, string, - readonly unknown[] + readonly unknown[], + config, + config['chains'][number]['id'] >, context -> +> & + ConfigParameter -export type UseContractWriteReturnType = UseMutationResult< +export type UseContractWriteReturnType< + config extends Config = Config, + context = unknown, +> = UseMutationResult< WriteContractData, WriteContractError, WriteContractVariables< - ResolvedRegister['config'], - ChainId, Abi, string, - readonly unknown[] + readonly unknown[], + config, + config['chains'][number]['id'] >, context > & { - write: WriteContractMutate - writeAsync: WriteContractMutateAsync + write: WriteContractMutate + writeAsync: WriteContractMutateAsync } /** https://wagmi.sh/react/hooks/useContractWrite */ -export function useContractWrite( - parameters: UseContractWriteParameters = {}, -): UseContractWriteReturnType { - const config = useConfig() +export function useContractWrite< + config extends Config = ResolvedRegister['config'], + context = unknown, +>( + parameters: UseContractWriteParameters = {}, +): UseContractWriteReturnType { + const config = useConfig(parameters) const mutationOptions = writeContractMutationOptions(config) const { mutate, mutateAsync, ...result } = useMutation({ ...parameters, ...mutationOptions, }) - type Return = UseContractWriteReturnType + type Return = UseContractWriteReturnType return { ...result, write: mutate as Return['write'], diff --git a/packages/react/src/hooks/usePrepareSendTransaction.ts b/packages/react/src/hooks/usePrepareSendTransaction.ts index e8addef898..f38eee922d 100644 --- a/packages/react/src/hooks/usePrepareSendTransaction.ts +++ b/packages/react/src/hooks/usePrepareSendTransaction.ts @@ -1,4 +1,8 @@ -import type { PrepareSendTransactionError, ResolvedRegister } from '@wagmi/core' +import type { + Config, + PrepareSendTransactionError, + ResolvedRegister, +} from '@wagmi/core' import { type PrepareSendTransactionData, type PrepareSendTransactionOptions, @@ -7,6 +11,7 @@ import { prepareSendTransactionQueryOptions, } from '@wagmi/core/query' +import type { ConfigParameter } from '../types/properties.js' import { type UseQueryParameters, type UseQueryResult, @@ -16,36 +21,39 @@ import { useChainId } from './useChainId.js' import { useConfig } from './useConfig.js' import { useConnectorClient } from './useConnectorClient.js' -type ChainId = ResolvedRegister['config']['chains'][number]['id'] - export type UsePrepareSendTransactionParameters< - chainId extends ChainId | undefined = undefined, - selectData = PrepareSendTransactionData, -> = PrepareSendTransactionOptions & + config extends Config = Config, + chainId extends config['chains'][number]['id'] = config['chains'][number]['id'], + selectData = PrepareSendTransactionData, +> = PrepareSendTransactionOptions & UseQueryParameters< - PrepareSendTransactionQueryFnData, + PrepareSendTransactionQueryFnData, PrepareSendTransactionError, selectData, - PrepareSendTransactionQueryKey - > + PrepareSendTransactionQueryKey + > & + ConfigParameter export type UsePrepareSendTransactionReturnType< - chainId extends ChainId | undefined = undefined, - selectData = PrepareSendTransactionData, + config extends Config = Config, + chainId extends config['chains'][number]['id'] = config['chains'][number]['id'], + selectData = PrepareSendTransactionData, > = UseQueryResult /** https://wagmi.sh/react/hooks/usePrepareSendTransaction */ export function usePrepareSendTransaction< - chainId extends ChainId | undefined = undefined, - selectData = PrepareSendTransactionData, + config extends Config = ResolvedRegister['config'], + chainId extends config['chains'][number]['id'] = config['chains'][number]['id'], + selectData = PrepareSendTransactionData, >( parameters: UsePrepareSendTransactionParameters< + config, chainId, selectData - > = {} as UsePrepareSendTransactionParameters, -): UsePrepareSendTransactionReturnType { + > = {} as UsePrepareSendTransactionParameters, +): UsePrepareSendTransactionReturnType { const { connector, ...query } = parameters - const config = useConfig() + const config = useConfig(parameters) const { data: connectorClient } = useConnectorClient({ connector, enabled: parameters.account === undefined, @@ -64,5 +72,5 @@ export function usePrepareSendTransaction< ...queryOptions, ...(query as any), enabled, - }) as UsePrepareSendTransactionReturnType + }) as UsePrepareSendTransactionReturnType } diff --git a/packages/react/src/hooks/useSendTransaction.ts b/packages/react/src/hooks/useSendTransaction.ts index 1a1210b915..d53de0770f 100644 --- a/packages/react/src/hooks/useSendTransaction.ts +++ b/packages/react/src/hooks/useSendTransaction.ts @@ -1,5 +1,9 @@ import { useMutation } from '@tanstack/react-query' -import type { ResolvedRegister, SendTransactionError } from '@wagmi/core' +import type { + Config, + ResolvedRegister, + SendTransactionError, +} from '@wagmi/core' import type { Evaluate } from '@wagmi/core/internal' import { type SendTransactionData, @@ -9,45 +13,52 @@ import { sendTransactionMutationOptions, } from '@wagmi/core/query' +import type { ConfigParameter } from '../types/properties.js' import type { UseMutationOptions, UseMutationResult } from '../utils/query.js' import { useConfig } from './useConfig.js' -type ChainId = ResolvedRegister['config']['chains'][number]['id'] - -export type UseSendTransactionParameters = +export type UseSendTransactionParameters< + config extends Config = Config, + context = unknown, +> = Evaluate< UseMutationOptions< SendTransactionData, SendTransactionError, - SendTransactionVariables, + SendTransactionVariables, context - > + > & + ConfigParameter +> -export type UseSendTransactionReturnType = Evaluate< +export type UseSendTransactionReturnType< + config extends Config = Config, + context = unknown, +> = Evaluate< UseMutationResult< SendTransactionData, SendTransactionError, - SendTransactionVariables, + SendTransactionVariables, context > & { - sendTransaction: SendTransactionMutate - sendTransactionAsync: SendTransactionMutateAsync< - ResolvedRegister['config'], - context - > + sendTransaction: SendTransactionMutate + sendTransactionAsync: SendTransactionMutateAsync } > /** https://wagmi.sh/react/hooks/useSendTransaction */ -export function useSendTransaction( - parameters: UseSendTransactionParameters = {}, -): UseSendTransactionReturnType { - const config = useConfig() +export function useSendTransaction< + config extends Config = ResolvedRegister['config'], + context = unknown, +>( + parameters: UseSendTransactionParameters = {}, +): UseSendTransactionReturnType { + const config = useConfig(parameters) const mutationOptions = sendTransactionMutationOptions(config) const { mutate, mutateAsync, ...result } = useMutation({ ...parameters, ...mutationOptions, }) - type Return = UseSendTransactionReturnType + type Return = UseSendTransactionReturnType return { ...result, sendTransaction: mutate as Return['sendTransaction'], diff --git a/packages/react/src/hooks/useTransaction.ts b/packages/react/src/hooks/useTransaction.ts index 1aed05108e..cb4f9eb929 100644 --- a/packages/react/src/hooks/useTransaction.ts +++ b/packages/react/src/hooks/useTransaction.ts @@ -19,7 +19,7 @@ import { useConfig } from './useConfig.js' export type UseTransactionParameters< config extends Config = Config, - chainId extends config['chains'][number]['id'] | undefined = undefined, + chainId extends config['chains'][number]['id'] = config['chains'][number]['id'], selectData = GetTransactionData, > = Evaluate< GetTransactionOptions & @@ -34,14 +34,14 @@ export type UseTransactionParameters< export type UseTransactionReturnType< config extends Config = Config, - chainId extends config['chains'][number]['id'] | undefined = undefined, + chainId extends config['chains'][number]['id'] = config['chains'][number]['id'], selectData = GetTransactionData, > = UseQueryResult /** https://wagmi.sh/react/hooks/useTransaction */ export function useTransaction< config extends Config = ResolvedRegister['config'], - chainId extends config['chains'][number]['id'] | undefined = undefined, + chainId extends config['chains'][number]['id'] = config['chains'][number]['id'], selectData = GetTransactionData, >( parameters: UseTransactionParameters = {}, diff --git a/packages/react/src/hooks/useWaitForTransactionReceipt.ts b/packages/react/src/hooks/useWaitForTransactionReceipt.ts index 627b08cb4e..59154e9fcc 100644 --- a/packages/react/src/hooks/useWaitForTransactionReceipt.ts +++ b/packages/react/src/hooks/useWaitForTransactionReceipt.ts @@ -12,6 +12,7 @@ import { waitForTransactionReceiptQueryOptions, } from '@wagmi/core/query' +import type { ConfigParameter } from '../types/properties.js' import { type UseQueryParameters, type UseQueryResult, @@ -22,7 +23,7 @@ import { useConfig } from './useConfig.js' export type UseWaitForTransactionReceiptParameters< config extends Config = Config, - chainId extends config['chains'][number]['id'] | undefined = undefined, + chainId extends config['chains'][number]['id'] = config['chains'][number]['id'], selectData = WaitForTransactionReceiptData, > = Evaluate< WaitForTransactionReceiptOptions & @@ -31,19 +32,20 @@ export type UseWaitForTransactionReceiptParameters< WaitForTransactionReceiptError, selectData, WaitForTransactionReceiptQueryKey - > + > & + ConfigParameter > export type UseWaitForTransactionReceiptReturnType< config extends Config = Config, - chainId extends config['chains'][number]['id'] | undefined = undefined, + chainId extends config['chains'][number]['id'] = config['chains'][number]['id'], selectData = WaitForTransactionReceiptData, > = UseQueryResult /** https://wagmi.sh/react/hooks/useWaitForTransactionReceipt */ export function useWaitForTransactionReceipt< config extends Config = ResolvedRegister['config'], - chainId extends config['chains'][number]['id'] | undefined = undefined, + chainId extends config['chains'][number]['id'] = config['chains'][number]['id'], selectData = WaitForTransactionReceiptData, >( parameters: UseWaitForTransactionReceiptParameters< @@ -53,7 +55,7 @@ export function useWaitForTransactionReceipt< > = {}, ): UseWaitForTransactionReceiptReturnType { const { hash, ...query } = parameters - const config = useConfig() + const config = useConfig(parameters) const chainId = parameters.chainId ?? useChainId() const queryOptions = waitForTransactionReceiptQueryOptions(config, { diff --git a/packages/register-tests/react/src/useContractWrite.test-d.ts b/packages/register-tests/react/src/useContractWrite.test-d.ts index ed08f43436..92ac1583d0 100644 --- a/packages/register-tests/react/src/useContractWrite.test-d.ts +++ b/packages/register-tests/react/src/useContractWrite.test-d.ts @@ -21,10 +21,10 @@ test('chain formatters', () => { type Result = Parameters< typeof write< - typeof celo.id, typeof abi.erc20, 'transferFrom', - [Address, Address, bigint] + [Address, Address, bigint], + typeof celo.id > >[0] expectTypeOf().toEqualTypeOf< diff --git a/packages/register-tests/react/src/useSendTransaction.test-d.ts b/packages/register-tests/react/src/useSendTransaction.test-d.ts index 1af6018db5..1cc0bc2ed6 100644 --- a/packages/register-tests/react/src/useSendTransaction.test-d.ts +++ b/packages/register-tests/react/src/useSendTransaction.test-d.ts @@ -1,3 +1,4 @@ +import type { ChainId } from './config.js' import { expectTypeOf, test } from 'vitest' import { useSendTransaction } from 'wagmi' import { celo, mainnet, optimism } from 'wagmi/chains' @@ -5,12 +6,19 @@ import { celo, mainnet, optimism } from 'wagmi/chains' test('chain formatters', () => { const { sendTransaction } = useSendTransaction() - sendTransaction({ - to: '0x', - feeCurrency: '0x', - gatewayFee: 123n, - gatewayFeeRecipient: '0x', - }) + sendTransaction( + { + to: '0x', + feeCurrency: '0x', + gatewayFee: 123n, + gatewayFeeRecipient: '0x', + }, + { + onSuccess(_data, variables) { + expectTypeOf(variables.chainId).toEqualTypeOf() + }, + }, + ) type Result = Parameters>[0] expectTypeOf().toEqualTypeOf<