Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #150 from unicape/1.x
Browse files Browse the repository at this point in the history
feat: add useEnsText & usePrepareTransactionRequest & useTransactionC…
  • Loading branch information
unicape authored Jan 23, 2024
2 parents 61c79e3 + b3740c6 commit 7fb949e
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 13 deletions.
6 changes: 6 additions & 0 deletions .changeset/olive-terms-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@use-wagmi/nuxt": minor
"use-wagmi": minor
---

add useEnsText & usePrepareTransactionRequest & useTransactionConfirmations
3 changes: 3 additions & 0 deletions packages/nuxt/src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ export const functions = [
'useEnsAvatar',
'useEnsName',
'useEnsResolver',
'useEnsText',
'useEstimateFeesPerGas',
'useFeeData',
'useEstimateGas',
'useEstimateMaxPriorityFeePerGas',
'useFeeHistory',
'useGasPrice',
'useInfiniteReadContracts',
'usePrepareTransactionRequest',
'useContractInfiniteReads',
'useProof',
'usePublicClient',
Expand All @@ -47,6 +49,7 @@ export const functions = [
'useSwitchChain',
'useToken',
'useTransaction',
'useTransactionConfirmations',
'useTransactionCount',
'useTransactionReceipt',
'useVerifyMessage',
Expand Down
4 changes: 2 additions & 2 deletions packages/vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,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"
},
Expand Down
18 changes: 18 additions & 0 deletions packages/vue/src/exports/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
74 changes: 74 additions & 0 deletions packages/vue/src/hooks/useEnsText.ts
Original file line number Diff line number Diff line change
@@ -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<config> &
ConfigParameter<config> &
QueryParameter<
GetEnsTextQueryFnData,
GetEnsTextErrorType,
selectData,
GetEnsTextQueryKey<config>
>
>
>

export type UseEnsTextReturnType<selectData = GetEnsTextData> =
UseQueryReturnType<selectData, GetEnsTextErrorType>

/** https://wagmi.sh/react/api/hooks/useEnsText */
export function useEnsText<
config extends Config = ResolvedRegister['config'],
selectData = GetEnsTextData,
>(
parameters: UseEnsTextParameters<config, selectData> = {},
): UseEnsTextReturnType<selectData> {
const config = useConfig(parameters)
const chainId = useChainId()

const queryOptions = computed(() => {
const _parameters = cloneDeepUnref<
DeepUnwrapRef<UseEnsTextParameters<config, selectData>>
>(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<selectData>
}
98 changes: 98 additions & 0 deletions packages/vue/src/hooks/usePrepareTransactionRequest.ts
Original file line number Diff line number Diff line change
@@ -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<parameterType, config, chainId>,
> = MaybeRefDeep<
PrepareTransactionRequestOptions<parameterType, config, chainId> &
ConfigParameter<config> &
QueryParameter<
PrepareTransactionRequestQueryFnData<parameterType, config, chainId>,
PrepareTransactionRequestErrorType,
selectData,
PrepareTransactionRequestQueryKey<parameterType, config, chainId>
>
>

export type UsePrepareTransactionRequestReturnType<
parameterType extends viem_PrepareTransactionRequestParameterType = viem_PrepareTransactionRequestParameterType,
config extends Config = Config,
chainId extends config['chains'][number]['id'] | undefined = undefined,
selectData = PrepareTransactionRequestData<parameterType, config, chainId>,
> = UseQueryReturnType<selectData, PrepareTransactionRequestErrorType>

/** 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<parameterType, config, chainId>,
>(
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
>
}
81 changes: 81 additions & 0 deletions packages/vue/src/hooks/useTransactionConfirmations.ts
Original file line number Diff line number Diff line change
@@ -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<config, chainId> &
ConfigParameter<config> &
QueryParameter<
GetTransactionConfirmationsQueryFnData,
GetTransactionConfirmationsErrorType,
selectData,
GetTransactionConfirmationsQueryKey<config, chainId>
>
>

export type UseTransactionConfirmationsReturnType<
selectData = GetTransactionConfirmationsData,
> = UseQueryReturnType<selectData, GetTransactionConfirmationsErrorType>

/** 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<selectData> {
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<selectData>
}
Loading

0 comments on commit 7fb949e

Please sign in to comment.