Skip to content

Commit

Permalink
feat: hook config prop
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Aug 23, 2023
1 parent 4aedd6a commit 2ae43ea
Show file tree
Hide file tree
Showing 24 changed files with 223 additions and 202 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 4 additions & 8 deletions packages/core/src/actions/getTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<viem_GetTransactionParameters & ChainIdParameter<config, chainId>>

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<config['chains'], chainId>,
chains extends readonly Chain[] = SelectChains<config, chainId>,
> = Evaluate<
{
[key in keyof chains]: viem_GetTransactionReturnType<chains[key]>
Expand All @@ -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<config, chainId>,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/actions/multicall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<contracts, allowFailure> & ChainIdParameter<config>

export type MulticallReturnType<
Expand All @@ -25,7 +25,7 @@ export async function multicall<
allowFailure extends boolean = true,
>(
config: config,
parameters: MulticallParameters<config, contracts, allowFailure>,
parameters: MulticallParameters<contracts, allowFailure, config>,
): Promise<MulticallReturnType<contracts, allowFailure>> {
const { allowFailure = true, chainId, contracts, ...rest } = parameters
const client = config.getClient({ chainId })
Expand Down
14 changes: 5 additions & 9 deletions packages/core/src/actions/prepareSendTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<config['chains'], chainId>,
chains extends readonly Chain[] = SelectChains<config, chainId>,
> = Evaluate<
{
[key in keyof chains]: Omit<
Expand All @@ -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<config['chains'], chainId>,
chains extends readonly Chain[] = SelectChains<config, chainId>,
> = Evaluate<
{
[key in keyof chains]: Omit<
Expand All @@ -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<config, chainId>,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/actions/readContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -33,7 +33,7 @@ export async function readContracts<
allowFailure extends boolean = true,
>(
config: config,
parameters: ReadContractsParameters<config, contracts, allowFailure>,
parameters: ReadContractsParameters<contracts, allowFailure, config>,
): Promise<ReadContractsReturnType<contracts, allowFailure>> {
const { allowFailure = true, blockNumber, blockTag, ...rest } = parameters
const contracts = parameters.contracts as (ContractFunctionParameters & {
Expand Down
8 changes: 3 additions & 5 deletions packages/core/src/actions/sendTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<config['chains'], chainId>,
chains extends readonly Chain[] = SelectChains<config, chainId>,
> = {
[key in keyof chains]: Evaluate<
Omit<
Expand All @@ -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<config, chainId>,
Expand Down
14 changes: 9 additions & 5 deletions packages/core/src/actions/simulateContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ export type SimulateContractParameters<
functionName
> = ContractFunctionArgs<abi, 'nonpayable' | 'payable', functionName>,
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<config['chains'], chainId>,
chains extends readonly Chain[] = SelectChains<config, chainId>,
> = {
[key in keyof chains]: UnionEvaluate<
UnionOmit<
Expand Down Expand Up @@ -67,9 +69,11 @@ export type SimulateContractReturnType<
functionName
> = ContractFunctionArgs<abi, 'nonpayable' | 'payable', functionName>,
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<config['chains'], chainId>,
chains extends readonly Chain[] = SelectChains<config, chainId>,
> = {
[key in keyof chains]: viem_SimulateContractReturnType<
readonly [ExtractAbiFunction<abi extends Abi ? abi : Abi, functionName>],
Expand Down Expand Up @@ -97,14 +101,14 @@ 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<abi, 'nonpayable' | 'payable'>,
args extends ContractFunctionArgs<
abi,
'nonpayable' | 'payable',
functionName
>,
config extends Config,
chainId extends config['chains'][number]['id'] | undefined = undefined,
>(
config: config,
Expand Down
12 changes: 4 additions & 8 deletions packages/core/src/actions/waitForTransactionReceipt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<config, chainId>
>

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<config['chains'], chainId>,
chains extends readonly Chain[] = SelectChains<config, chainId>,
> = Evaluate<
{
[key in keyof chains]: viem_WaitForTransactionReceiptReturnType<chains[key]>
Expand All @@ -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<config, chainId>,
Expand Down
21 changes: 14 additions & 7 deletions packages/core/src/actions/writeContract.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ test('chain formatters', () => {
transports: { [celo.id]: http(), [mainnet.id]: http() },
})

type Result = WriteContractParameters<typeof config>
type Result = WriteContractParameters<
typeof abi.erc20,
'transferFrom',
[Address, Address, bigint],
typeof config
>
expectTypeOf<Result>().toMatchTypeOf<{
chainId?: typeof celo.id | typeof mainnet.id | undefined
feeCurrency?: `0x${string}` | undefined
Expand All @@ -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<Result2>().toMatchTypeOf<{
functionName: 'approve' | 'transfer' | 'transferFrom'
Expand All @@ -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<Result3>().toMatchTypeOf<{
functionName: 'approve' | 'transfer' | 'transferFrom'
Expand Down
15 changes: 6 additions & 9 deletions packages/core/src/actions/writeContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -37,10 +34,10 @@ export type WriteContractParameters<
'nonpayable' | 'payable',
functionName
> = ContractFunctionArgs<abi, 'nonpayable' | 'payable', functionName>,
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'][number], { id: chainId }>]
: config['chains'],
chains extends readonly Chain[] = SelectChains<config, chainId>,
> = {
[key in keyof chains]: UnionOmit<
viem_WriteContractParameters<
Expand Down Expand Up @@ -69,17 +66,17 @@ 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<abi, 'nonpayable' | 'payable'>,
args extends ContractFunctionArgs<
abi,
'nonpayable' | 'payable',
functionName
>,
chainId extends config['chains'][number]['id'],
>(
config: config,
parameters: WriteContractParameters<config, chainId, abi, functionName, args>,
parameters: WriteContractParameters<abi, functionName, args, config, chainId>,
): Promise<WriteContractReturnType> {
const { chainId, connector, __mode, ...rest } = parameters

Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/query/getTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<GetTransactionParameters<config, chainId>> & ScopeKeyParameter
>

export function getTransactionQueryOptions<
config extends Config,
chainId extends config['chains'][number]['id'] | undefined,
chainId extends config['chains'][number]['id'],
>(config: config, options: GetTransactionOptions<config, chainId> = {}) {
return {
async queryFn({ queryKey }) {
Expand Down Expand Up @@ -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<config, chainId>

export type GetTransactionData<
config extends Config,
chainId extends config['chains'][number]['id'] | undefined,
chainId extends config['chains'][number]['id'],
> = GetTransactionQueryFnData<config, chainId>

export function getTransactionQueryKey<
config extends Config,
chainId extends config['chains'][number]['id'] | undefined,
chainId extends config['chains'][number]['id'],
>(options: GetTransactionOptions<config, chainId> = {}) {
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<typeof getTransactionQueryKey<config, chainId>>
Loading

1 comment on commit 2ae43ea

@vercel
Copy link

@vercel vercel bot commented on 2ae43ea Aug 23, 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.vercel.app
wagmi-v2-wagmi-dev.vercel.app
wagmi-v2-git-alpha-wagmi-dev.vercel.app
alpha.wagmi.sh

Please sign in to comment.