From b3740c693651e3e73dfd257d7cddba4529eb97fa Mon Sep 17 00:00:00 2001 From: Unic Ape Date: Tue, 23 Jan 2024 12:08:50 +0800 Subject: [PATCH] feat: add useEnsText & usePrepareTransactionRequest & useTransactionConfirmations --- .changeset/olive-terms-obey.md | 6 ++ packages/nuxt/src/functions.ts | 3 + packages/vue/package.json | 4 +- packages/vue/src/exports/index.ts | 18 ++++ packages/vue/src/hooks/useEnsText.ts | 74 ++++++++++++++ .../src/hooks/usePrepareTransactionRequest.ts | 98 +++++++++++++++++++ .../src/hooks/useTransactionConfirmations.ts | 81 +++++++++++++++ pnpm-lock.yaml | 23 ++--- 8 files changed, 294 insertions(+), 13 deletions(-) create mode 100644 .changeset/olive-terms-obey.md create mode 100644 packages/vue/src/hooks/useEnsText.ts create mode 100644 packages/vue/src/hooks/usePrepareTransactionRequest.ts create mode 100644 packages/vue/src/hooks/useTransactionConfirmations.ts diff --git a/.changeset/olive-terms-obey.md b/.changeset/olive-terms-obey.md new file mode 100644 index 0000000..6979d55 --- /dev/null +++ b/.changeset/olive-terms-obey.md @@ -0,0 +1,6 @@ +--- +"@use-wagmi/nuxt": minor +"use-wagmi": minor +--- + +add useEnsText & usePrepareTransactionRequest & useTransactionConfirmations diff --git a/packages/nuxt/src/functions.ts b/packages/nuxt/src/functions.ts index 120ae3f..760b05e 100644 --- a/packages/nuxt/src/functions.ts +++ b/packages/nuxt/src/functions.ts @@ -23,6 +23,7 @@ export const functions = [ 'useEnsAvatar', 'useEnsName', 'useEnsResolver', + 'useEnsText', 'useEstimateFeesPerGas', 'useFeeData', 'useEstimateGas', @@ -30,6 +31,7 @@ export const functions = [ 'useFeeHistory', 'useGasPrice', 'useInfiniteReadContracts', + 'usePrepareTransactionRequest', 'useContractInfiniteReads', 'useProof', 'usePublicClient', @@ -47,6 +49,7 @@ export const functions = [ 'useSwitchChain', 'useToken', 'useTransaction', + 'useTransactionConfirmations', 'useTransactionCount', 'useTransactionReceipt', 'useVerifyMessage', diff --git a/packages/vue/package.json b/packages/vue/package.json index d289a2e..1edd30b 100644 --- a/packages/vue/package.json +++ b/packages/vue/package.json @@ -78,8 +78,8 @@ } }, "dependencies": { - "@wagmi/connectors": "4.1.4", - "@wagmi/core": "2.2.1", + "@wagmi/connectors": "4.1.8", + "@wagmi/core": "2.5.0", "vue": "3.3.8", "vue-demi": "^0.14.6" }, diff --git a/packages/vue/src/exports/index.ts b/packages/vue/src/exports/index.ts index 307cab5..4fdd25b 100644 --- a/packages/vue/src/exports/index.ts +++ b/packages/vue/src/exports/index.ts @@ -142,6 +142,12 @@ export { useEnsResolver, } from '../hooks/useEnsResolver.js' +export { + type UseEnsTextParameters, + type UseEnsTextReturnType, + useEnsText, +} from '../hooks/useEnsText.js' + export { type UseEstimateFeesPerGasParameters, type UseEstimateFeesPerGasReturnType, @@ -182,6 +188,12 @@ export { useInfiniteReadContracts as useContractInfiniteReads, } from '../hooks/useInfiniteReadContracts.js' +export { + type UsePrepareTransactionRequestParameters, + type UsePrepareTransactionRequestReturnType, + usePrepareTransactionRequest, +} from '../hooks/usePrepareTransactionRequest.js' + export { type UseProofParameters, type UseProofReturnType, @@ -271,6 +283,12 @@ export { useTransaction, } from '../hooks/useTransaction.js' +export { + type UseTransactionConfirmationsParameters, + type UseTransactionConfirmationsReturnType, + useTransactionConfirmations, +} from '../hooks/useTransactionConfirmations.js' + export { type UseTransactionCountParameters, type UseTransactionCountReturnType, diff --git a/packages/vue/src/hooks/useEnsText.ts b/packages/vue/src/hooks/useEnsText.ts new file mode 100644 index 0000000..68aeadb --- /dev/null +++ b/packages/vue/src/hooks/useEnsText.ts @@ -0,0 +1,74 @@ +'use client' + +import type { Config, GetEnsTextErrorType, ResolvedRegister } from '@wagmi/core' +import { type Evaluate } from '@wagmi/core/internal' +import { + type GetEnsTextData, + type GetEnsTextOptions, + type GetEnsTextQueryFnData, + type GetEnsTextQueryKey, + getEnsTextQueryOptions, +} from '@wagmi/core/query' + +import { computed } from 'vue-demi' +import type { + ConfigParameter, + DeepUnwrapRef, + MaybeRefDeep, + QueryParameter, +} from '../types.js' +import { cloneDeepUnref } from '../utils/cloneDeepUnref.js' +import { type UseQueryReturnType, useQuery } from '../utils/query.js' +import { useChainId } from './useChainId.js' +import { useConfig } from './useConfig.js' + +export type UseEnsTextParameters< + config extends Config = Config, + selectData = GetEnsTextData, +> = MaybeRefDeep< + Evaluate< + GetEnsTextOptions & + ConfigParameter & + QueryParameter< + GetEnsTextQueryFnData, + GetEnsTextErrorType, + selectData, + GetEnsTextQueryKey + > + > +> + +export type UseEnsTextReturnType = + UseQueryReturnType + +/** https://wagmi.sh/react/api/hooks/useEnsText */ +export function useEnsText< + config extends Config = ResolvedRegister['config'], + selectData = GetEnsTextData, +>( + parameters: UseEnsTextParameters = {}, +): UseEnsTextReturnType { + const config = useConfig(parameters) + const chainId = useChainId() + + const queryOptions = computed(() => { + const _parameters = cloneDeepUnref< + DeepUnwrapRef> + >(parameters as any) + + const { key, name, query = {} } = _parameters + const options = getEnsTextQueryOptions(config, { + ..._parameters, + chainId: _parameters.chainId ?? chainId.value, + }) + const enabled = Boolean(key && name && (query.enabled ?? true)) + + return { + ...query, + ...options, + enabled, + } + }) + + return useQuery(queryOptions as any) as UseEnsTextReturnType +} diff --git a/packages/vue/src/hooks/usePrepareTransactionRequest.ts b/packages/vue/src/hooks/usePrepareTransactionRequest.ts new file mode 100644 index 0000000..2fdaaac --- /dev/null +++ b/packages/vue/src/hooks/usePrepareTransactionRequest.ts @@ -0,0 +1,98 @@ +'use client' + +import { + type Config, + type PrepareTransactionRequestErrorType, + type ResolvedRegister, +} from '@wagmi/core' +import { + type PrepareTransactionRequestData, + type PrepareTransactionRequestOptions, + type PrepareTransactionRequestQueryKey, + prepareTransactionRequestQueryOptions, +} from '@wagmi/core/query' +import type { PrepareTransactionRequestQueryFnData } from '@wagmi/core/query' +import { type PrepareTransactionRequestParameterType as viem_PrepareTransactionRequestParameterType } from 'viem' + +import { computed } from 'vue-demi' +import { + type ConfigParameter, + type MaybeRefDeep, + type QueryParameter, +} from '../types.js' +import { cloneDeepUnref } from '../utils/cloneDeepUnref.js' +import { type UseQueryReturnType, useQuery } from '../utils/query.js' +import { useChainId } from './useChainId.js' +import { useConfig } from './useConfig.js' + +export type UsePrepareTransactionRequestParameters< + parameterType extends viem_PrepareTransactionRequestParameterType = viem_PrepareTransactionRequestParameterType, + config extends Config = Config, + chainId extends config['chains'][number]['id'] | undefined = undefined, + selectData = PrepareTransactionRequestData, +> = MaybeRefDeep< + PrepareTransactionRequestOptions & + ConfigParameter & + QueryParameter< + PrepareTransactionRequestQueryFnData, + PrepareTransactionRequestErrorType, + selectData, + PrepareTransactionRequestQueryKey + > +> + +export type UsePrepareTransactionRequestReturnType< + parameterType extends viem_PrepareTransactionRequestParameterType = viem_PrepareTransactionRequestParameterType, + config extends Config = Config, + chainId extends config['chains'][number]['id'] | undefined = undefined, + selectData = PrepareTransactionRequestData, +> = UseQueryReturnType + +/** https://wagmi.sh/react/api/hooks/usePrepareTransactionRequest */ +export function usePrepareTransactionRequest< + parameterType extends viem_PrepareTransactionRequestParameterType, + config extends Config = ResolvedRegister['config'], + chainId extends config['chains'][number]['id'] | undefined = undefined, + selectData = PrepareTransactionRequestData, +>( + parameters: UsePrepareTransactionRequestParameters< + parameterType, + config, + chainId, + selectData + > = {} as any, +): UsePrepareTransactionRequestReturnType< + parameterType, + config, + chainId, + selectData +> { + const config = useConfig(parameters) + const chainId = useChainId() + + const queryOptions = computed(() => { + const _parameters = cloneDeepUnref(parameters) + + const { to, query = {} } = _parameters + const options = prepareTransactionRequestQueryOptions(config, { + ..._parameters, + chainId: _parameters.chainId ?? chainId.value, + }) + const enabled = Boolean(to && (query.enabled ?? true)) + + return { + ...query, + ...options, + enabled, + } + }) + + return useQuery( + queryOptions as any, + ) as UsePrepareTransactionRequestReturnType< + parameterType, + config, + chainId, + selectData + > +} diff --git a/packages/vue/src/hooks/useTransactionConfirmations.ts b/packages/vue/src/hooks/useTransactionConfirmations.ts new file mode 100644 index 0000000..ac6afbd --- /dev/null +++ b/packages/vue/src/hooks/useTransactionConfirmations.ts @@ -0,0 +1,81 @@ +'use client' + +import { + type Config, + type GetTransactionConfirmationsErrorType, + type ResolvedRegister, +} from '@wagmi/core' +import { + type GetTransactionConfirmationsData, + type GetTransactionConfirmationsOptions, + type GetTransactionConfirmationsQueryFnData, + type GetTransactionConfirmationsQueryKey, + getTransactionConfirmationsQueryOptions, +} from '@wagmi/core/query' + +import { computed } from 'vue-demi' +import { + type ConfigParameter, + type MaybeRefDeep, + type QueryParameter, +} from '../types.js' +import { cloneDeepUnref } from '../utils/cloneDeepUnref.js' +import { type UseQueryReturnType, useQuery } from '../utils/query.js' +import { useChainId } from './useChainId.js' +import { useConfig } from './useConfig.js' + +export type UseTransactionConfirmationsParameters< + config extends Config = Config, + chainId extends config['chains'][number]['id'] | undefined = undefined, + selectData = GetTransactionConfirmationsData, +> = MaybeRefDeep< + GetTransactionConfirmationsOptions & + ConfigParameter & + QueryParameter< + GetTransactionConfirmationsQueryFnData, + GetTransactionConfirmationsErrorType, + selectData, + GetTransactionConfirmationsQueryKey + > +> + +export type UseTransactionConfirmationsReturnType< + selectData = GetTransactionConfirmationsData, +> = UseQueryReturnType + +/** https://wagmi.sh/react/api/hooks/useTransactionConfirmations */ +export function useTransactionConfirmations< + config extends Config = ResolvedRegister['config'], + chainId extends config['chains'][number]['id'] | undefined = undefined, + selectData = GetTransactionConfirmationsData, +>( + parameters: UseTransactionConfirmationsParameters< + config, + chainId, + selectData + > = {} as any, +): UseTransactionConfirmationsReturnType { + const config = useConfig(parameters) + const chainId = useChainId() + + const queryOptions = computed(() => { + const _parameters = cloneDeepUnref(parameters) + + const { hash, transactionReceipt, query = {} } = _parameters + const options = getTransactionConfirmationsQueryOptions(config, { + ..._parameters, + chainId: _parameters.chainId ?? chainId.value, + }) + const enabled = Boolean( + !(hash && transactionReceipt) && + (hash || transactionReceipt) && + (query.enabled ?? true), + ) + + return { ...query, ...options, enabled } + }) + + return useQuery( + queryOptions as any, + ) as UseTransactionConfirmationsReturnType +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4c16c3e..2cfdfb5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -76,11 +76,11 @@ importers: packages/vue: dependencies: '@wagmi/connectors': - specifier: 4.1.4 - version: 4.1.4(@wagmi/core@2.2.1)(typescript@5.1.6)(viem@2.1.0) + specifier: 4.1.8 + version: 4.1.8(@wagmi/core@2.5.0)(typescript@5.1.6)(viem@2.1.0) '@wagmi/core': - specifier: 2.2.1 - version: 2.2.1(typescript@5.1.6)(viem@2.1.0) + specifier: 2.5.0 + version: 2.5.0(typescript@5.1.6)(viem@2.1.0) vue: specifier: 3.3.8 version: 3.3.8(typescript@5.1.6) @@ -1721,6 +1721,7 @@ packages: /@motionone/vue@10.16.4: resolution: {integrity: sha512-z10PF9JV6SbjFq+/rYabM+8CVlMokgl8RFGvieSGNTmrkQanfHn+15XBrhG3BgUfvmTeSeyShfOHpG0i9zEdcg==} + deprecated: Motion One for Vue is deprecated. Use Oku Motion instead https://oku-ui.com/motion dependencies: '@motionone/dom': 10.17.0 tslib: 2.6.2 @@ -2307,7 +2308,7 @@ packages: resolution: {integrity: sha512-osvveYtyzdEVbt3OfwwXFr4P2iVBL5u1Q3q4ONBfDY/UpOuXmOlbgwc1xECEboY8wIays8Yt6onaWMUdUbfl0A==} dependencies: '@noble/curves': 1.1.0 - '@noble/hashes': 1.3.1 + '@noble/hashes': 1.3.3 '@scure/base': 1.1.5 dev: false @@ -2921,10 +2922,10 @@ packages: /@vue/shared@3.4.14: resolution: {integrity: sha512-nmi3BtLpvqXAWoRZ6HQ+pFJOHBU4UnH3vD3opgmwXac7vhaHKA9nj1VeGjMggdB9eLtW83eHyPCmOU1qzdsC7Q==} - /@wagmi/connectors@4.1.4(@wagmi/core@2.2.1)(typescript@5.1.6)(viem@2.1.0): - resolution: {integrity: sha512-Mk/JnVs9pPd9cN/b5F+kHZMRU13yVf1zJwXNzSKpt7BOJQBqiFxJtmuZdRsNboEGEiPDFoEnzOzeAFi2xk3UGw==} + /@wagmi/connectors@4.1.8(@wagmi/core@2.5.0)(typescript@5.1.6)(viem@2.1.0): + resolution: {integrity: sha512-ODQUB1YGn0rofOcg7T5UCzHZNKSgai4Nri7harI4wao13J2gPcjIsPewiGwKWhCd8nePICUvZ2IL0b6gWq3cqA==} peerDependencies: - '@wagmi/core': 2.2.1 + '@wagmi/core': 2.5.0 typescript: '>=5.0.4' viem: 2.x peerDependenciesMeta: @@ -2935,7 +2936,7 @@ packages: '@metamask/sdk': 0.14.1 '@safe-global/safe-apps-provider': 0.18.1(typescript@5.1.6) '@safe-global/safe-apps-sdk': 8.1.0(typescript@5.1.6) - '@wagmi/core': 2.2.1(typescript@5.1.6)(viem@2.1.0) + '@wagmi/core': 2.5.0(typescript@5.1.6)(viem@2.1.0) '@walletconnect/ethereum-provider': 2.11.0 '@walletconnect/modal': 2.6.2 typescript: 5.1.6 @@ -2965,8 +2966,8 @@ packages: - zod dev: false - /@wagmi/core@2.2.1(typescript@5.1.6)(viem@2.1.0): - resolution: {integrity: sha512-eoTHG600lFpQyeofuo1nlAuTlI7rjduJK/yU4F8sLwjucROSE5RmD2OpQOkeEC3JGoXjrYxVPbY77PUD6ajzqw==} + /@wagmi/core@2.5.0(typescript@5.1.6)(viem@2.1.0): + resolution: {integrity: sha512-aWWxIctf76NXBDzbX+FPsBLB3lkGwf1wqbQZJqb+pyuzwwN2dR1NDwsYlPVMeP6SKliKsfxlYWFcKCkPqGuJqg==} peerDependencies: '@tanstack/query-core': '>=5.0.0' typescript: '>=5.0.4'