wagmi 1.4.1 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/context.js +16 -0
- package/dist/esm/context.js.map +1 -0
- package/dist/esm/errors/base.js +20 -0
- package/dist/esm/errors/base.js.map +1 -0
- package/dist/esm/errors/context.js +13 -0
- package/dist/esm/errors/context.js.map +1 -0
- package/dist/esm/exports/actions.js +4 -0
- package/dist/esm/exports/actions.js.map +1 -0
- package/dist/esm/exports/chains.js +4 -0
- package/dist/esm/exports/chains.js.map +1 -0
- package/dist/esm/exports/connectors.js +4 -0
- package/dist/esm/exports/connectors.js.map +1 -0
- package/dist/esm/exports/index.js +63 -0
- package/dist/esm/exports/index.js.map +1 -0
- package/dist/esm/exports/query.js +4 -0
- package/dist/esm/exports/query.js.map +1 -0
- package/dist/esm/hooks/useAccount.js +10 -0
- package/dist/esm/hooks/useAccount.js.map +1 -0
- package/dist/esm/hooks/useAccountEffect.js +29 -0
- package/dist/esm/hooks/useAccountEffect.js.map +1 -0
- package/dist/esm/hooks/useBalance.js +19 -0
- package/dist/esm/hooks/useBalance.js.map +1 -0
- package/dist/esm/hooks/useBlockNumber.js +37 -0
- package/dist/esm/hooks/useBlockNumber.js.map +1 -0
- package/dist/esm/hooks/useChainId.js +10 -0
- package/dist/esm/hooks/useChainId.js.map +1 -0
- package/dist/esm/hooks/useClient.js +10 -0
- package/dist/esm/hooks/useClient.js.map +1 -0
- package/dist/esm/hooks/useConfig.js +13 -0
- package/dist/esm/hooks/useConfig.js.map +1 -0
- package/dist/esm/hooks/useConnect.js +22 -0
- package/dist/esm/hooks/useConnect.js.map +1 -0
- package/dist/esm/hooks/useConnections.js +10 -0
- package/dist/esm/hooks/useConnections.js.map +1 -0
- package/dist/esm/hooks/useConnectorClient.js +40 -0
- package/dist/esm/hooks/useConnectorClient.js.map +1 -0
- package/dist/esm/hooks/useContractRead.js +26 -0
- package/dist/esm/hooks/useContractRead.js.map +1 -0
- package/dist/esm/hooks/useContractReads.js +38 -0
- package/dist/esm/hooks/useContractReads.js.map +1 -0
- package/dist/esm/hooks/useContractSimulate.js +24 -0
- package/dist/esm/hooks/useContractSimulate.js.map +1 -0
- package/dist/esm/hooks/useContractWrite.js +20 -0
- package/dist/esm/hooks/useContractWrite.js.map +1 -0
- package/dist/esm/hooks/useDisconnect.js +23 -0
- package/dist/esm/hooks/useDisconnect.js.map +1 -0
- package/dist/esm/hooks/useEnsAddress.js +19 -0
- package/dist/esm/hooks/useEnsAddress.js.map +1 -0
- package/dist/esm/hooks/useEnsAvatar.js +19 -0
- package/dist/esm/hooks/useEnsAvatar.js.map +1 -0
- package/dist/esm/hooks/useEnsName.js +19 -0
- package/dist/esm/hooks/useEnsName.js.map +1 -0
- package/dist/esm/hooks/useEnsResolver.js +19 -0
- package/dist/esm/hooks/useEnsResolver.js.map +1 -0
- package/dist/esm/hooks/useEstimateFeesPerGas.js +19 -0
- package/dist/esm/hooks/useEstimateFeesPerGas.js.map +1 -0
- package/dist/esm/hooks/useEstimateGas.js +25 -0
- package/dist/esm/hooks/useEstimateGas.js.map +1 -0
- package/dist/esm/hooks/usePublicClient.js +10 -0
- package/dist/esm/hooks/usePublicClient.js.map +1 -0
- package/dist/esm/hooks/useReconnect.js +22 -0
- package/dist/esm/hooks/useReconnect.js.map +1 -0
- package/dist/esm/hooks/useSendTransaction.js +20 -0
- package/dist/esm/hooks/useSendTransaction.js.map +1 -0
- package/dist/esm/hooks/useSignMessage.js +21 -0
- package/dist/esm/hooks/useSignMessage.js.map +1 -0
- package/dist/esm/hooks/useSignTypedData.js +20 -0
- package/dist/esm/hooks/useSignTypedData.js.map +1 -0
- package/dist/esm/hooks/useSwitchAccount.js +22 -0
- package/dist/esm/hooks/useSwitchAccount.js.map +1 -0
- package/dist/esm/hooks/useSwitchChain.js +21 -0
- package/dist/esm/hooks/useSwitchChain.js.map +1 -0
- package/dist/esm/hooks/useSyncExternalStoreWithTracked.js +40 -0
- package/dist/esm/hooks/useSyncExternalStoreWithTracked.js.map +1 -0
- package/dist/esm/hooks/useToken.js +19 -0
- package/dist/esm/hooks/useToken.js.map +1 -0
- package/dist/esm/hooks/useTransaction.js +19 -0
- package/dist/esm/hooks/useTransaction.js.map +1 -0
- package/dist/esm/hooks/useWaitForTransactionReceipt.js +19 -0
- package/dist/esm/hooks/useWaitForTransactionReceipt.js.map +1 -0
- package/dist/esm/hooks/useWalletClient.js +42 -0
- package/dist/esm/hooks/useWalletClient.js.map +1 -0
- package/dist/esm/hooks/useWatchBlockNumber.js +25 -0
- package/dist/esm/hooks/useWatchBlockNumber.js.map +1 -0
- package/dist/esm/hooks/useWatchContractEvent.js +25 -0
- package/dist/esm/hooks/useWatchContractEvent.js.map +1 -0
- package/dist/esm/hooks/useWatchPendingTransactions.js +25 -0
- package/dist/esm/hooks/useWatchPendingTransactions.js.map +1 -0
- package/dist/esm/tsconfig.build.tsbuildinfo +1 -0
- package/dist/esm/types/properties.js +2 -0
- package/dist/esm/types/properties.js.map +1 -0
- package/dist/esm/utils/getVersion.js +3 -0
- package/dist/esm/utils/getVersion.js.map +1 -0
- package/dist/esm/utils/query.js +21 -0
- package/dist/esm/utils/query.js.map +1 -0
- package/dist/esm/version.js +2 -0
- package/dist/esm/version.js.map +1 -0
- package/dist/types/context.d.ts +11 -0
- package/dist/types/context.d.ts.map +1 -0
- package/dist/types/errors/base.d.ts +7 -0
- package/dist/types/errors/base.d.ts.map +1 -0
- package/dist/types/errors/context.d.ts +6 -0
- package/dist/types/errors/context.d.ts.map +1 -0
- package/dist/types/exports/actions.d.ts +2 -0
- package/dist/types/exports/actions.d.ts.map +1 -0
- package/dist/types/exports/chains.d.ts +2 -0
- package/dist/types/exports/chains.d.ts.map +1 -0
- package/dist/types/exports/connectors.d.ts +2 -0
- package/dist/types/exports/connectors.d.ts.map +1 -0
- package/dist/types/exports/index.d.ts +46 -0
- package/dist/types/exports/index.d.ts.map +1 -0
- package/dist/types/exports/query.d.ts +2 -0
- package/dist/types/exports/query.d.ts.map +1 -0
- package/dist/types/hooks/useAccount.d.ts +7 -0
- package/dist/types/hooks/useAccount.d.ts.map +1 -0
- package/dist/types/hooks/useAccountEffect.d.ts +14 -0
- package/dist/types/hooks/useAccountEffect.d.ts.map +1 -0
- package/dist/types/hooks/useBalance.d.ts +13 -0
- package/dist/types/hooks/useBalance.d.ts.map +1 -0
- package/dist/types/hooks/useBlockNumber.d.ts +14 -0
- package/dist/types/hooks/useBlockNumber.d.ts.map +1 -0
- package/dist/types/hooks/useChainId.d.ts +7 -0
- package/dist/types/hooks/useChainId.d.ts.map +1 -0
- package/dist/types/hooks/useClient.d.ts +8 -0
- package/dist/types/hooks/useClient.d.ts.map +1 -0
- package/dist/types/hooks/useConfig.d.ts +7 -0
- package/dist/types/hooks/useConfig.d.ts.map +1 -0
- package/dist/types/hooks/useConnect.d.ts +16 -0
- package/dist/types/hooks/useConnect.d.ts.map +1 -0
- package/dist/types/hooks/useConnections.d.ts +7 -0
- package/dist/types/hooks/useConnections.d.ts.map +1 -0
- package/dist/types/hooks/useConnectorClient.d.ts +12 -0
- package/dist/types/hooks/useConnectorClient.d.ts.map +1 -0
- package/dist/types/hooks/useContractRead.d.ts +13 -0
- package/dist/types/hooks/useContractRead.d.ts.map +1 -0
- package/dist/types/hooks/useContractReads.d.ts +13 -0
- package/dist/types/hooks/useContractReads.d.ts.map +1 -0
- package/dist/types/hooks/useContractSimulate.d.ts +12 -0
- package/dist/types/hooks/useContractSimulate.d.ts.map +1 -0
- package/dist/types/hooks/useContractWrite.d.ts +15 -0
- package/dist/types/hooks/useContractWrite.d.ts.map +1 -0
- package/dist/types/hooks/useDisconnect.d.ts +16 -0
- package/dist/types/hooks/useDisconnect.d.ts.map +1 -0
- package/dist/types/hooks/useEnsAddress.d.ts +12 -0
- package/dist/types/hooks/useEnsAddress.d.ts.map +1 -0
- package/dist/types/hooks/useEnsAvatar.d.ts +12 -0
- package/dist/types/hooks/useEnsAvatar.d.ts.map +1 -0
- package/dist/types/hooks/useEnsName.d.ts +12 -0
- package/dist/types/hooks/useEnsName.d.ts.map +1 -0
- package/dist/types/hooks/useEnsResolver.d.ts +12 -0
- package/dist/types/hooks/useEnsResolver.d.ts.map +1 -0
- package/dist/types/hooks/useEstimateFeesPerGas.d.ts +13 -0
- package/dist/types/hooks/useEstimateFeesPerGas.d.ts.map +1 -0
- package/dist/types/hooks/useEstimateGas.d.ts +11 -0
- package/dist/types/hooks/useEstimateGas.d.ts.map +1 -0
- package/dist/types/hooks/usePublicClient.d.ts +8 -0
- package/dist/types/hooks/usePublicClient.d.ts.map +1 -0
- package/dist/types/hooks/useReconnect.d.ts +16 -0
- package/dist/types/hooks/useReconnect.d.ts.map +1 -0
- package/dist/types/hooks/useSendTransaction.d.ts +15 -0
- package/dist/types/hooks/useSendTransaction.d.ts.map +1 -0
- package/dist/types/hooks/useSignMessage.d.ts +15 -0
- package/dist/types/hooks/useSignMessage.d.ts.map +1 -0
- package/dist/types/hooks/useSignTypedData.d.ts +15 -0
- package/dist/types/hooks/useSignTypedData.d.ts.map +1 -0
- package/dist/types/hooks/useSwitchAccount.d.ts +16 -0
- package/dist/types/hooks/useSwitchAccount.d.ts.map +1 -0
- package/dist/types/hooks/useSwitchChain.d.ts +16 -0
- package/dist/types/hooks/useSwitchChain.d.ts.map +1 -0
- package/dist/types/hooks/useSyncExternalStoreWithTracked.d.ts +2 -0
- package/dist/types/hooks/useSyncExternalStoreWithTracked.d.ts.map +1 -0
- package/dist/types/hooks/useToken.d.ts +12 -0
- package/dist/types/hooks/useToken.d.ts.map +1 -0
- package/dist/types/hooks/useTransaction.d.ts +12 -0
- package/dist/types/hooks/useTransaction.d.ts.map +1 -0
- package/dist/types/hooks/useWaitForTransactionReceipt.d.ts +12 -0
- package/dist/types/hooks/useWaitForTransactionReceipt.d.ts.map +1 -0
- package/dist/types/hooks/useWalletClient.d.ts +12 -0
- package/dist/types/hooks/useWalletClient.d.ts.map +1 -0
- package/dist/types/hooks/useWatchBlockNumber.d.ts +8 -0
- package/dist/types/hooks/useWatchBlockNumber.d.ts.map +1 -0
- package/dist/types/hooks/useWatchContractEvent.d.ts +9 -0
- package/dist/types/hooks/useWatchContractEvent.d.ts.map +1 -0
- package/dist/types/hooks/useWatchPendingTransactions.d.ts +8 -0
- package/dist/types/hooks/useWatchPendingTransactions.d.ts.map +1 -0
- package/dist/types/types/properties.d.ts +8 -0
- package/dist/types/types/properties.d.ts.map +1 -0
- package/dist/types/utils/getVersion.d.ts +2 -0
- package/dist/types/utils/getVersion.d.ts.map +1 -0
- package/dist/types/utils/query.d.ts +15 -0
- package/dist/types/utils/query.d.ts.map +1 -0
- package/dist/types/version.d.ts +2 -0
- package/dist/types/version.d.ts.map +1 -0
- package/package.json +65 -103
- package/src/context.ts +27 -0
- package/src/errors/base.ts +13 -0
- package/src/errors/context.ts +8 -0
- package/src/exports/actions.ts +4 -0
- package/src/exports/chains.ts +4 -0
- package/src/exports/connectors.ts +4 -0
- package/src/exports/index.ts +284 -0
- package/src/exports/query.ts +4 -0
- package/src/hooks/useAccount.ts +27 -0
- package/src/hooks/useAccountEffect.ts +55 -0
- package/src/hooks/useBalance.ts +65 -0
- package/src/hooks/useBlockNumber.ts +97 -0
- package/src/hooks/useChainId.ts +31 -0
- package/src/hooks/useClient.ts +43 -0
- package/src/hooks/useConfig.ts +22 -0
- package/src/hooks/useConnect.ts +80 -0
- package/src/hooks/useConnections.ts +27 -0
- package/src/hooks/useConnectorClient.ts +95 -0
- package/src/hooks/useContractRead.ts +107 -0
- package/src/hooks/useContractReads.ts +98 -0
- package/src/hooks/useContractSimulate.ts +117 -0
- package/src/hooks/useContractWrite.ts +83 -0
- package/src/hooks/useDisconnect.ts +68 -0
- package/src/hooks/useEnsAddress.ts +61 -0
- package/src/hooks/useEnsAvatar.ts +59 -0
- package/src/hooks/useEnsName.ts +61 -0
- package/src/hooks/useEnsResolver.ts +61 -0
- package/src/hooks/useEstimateFeesPerGas.ts +69 -0
- package/src/hooks/useEstimateGas.ts +72 -0
- package/src/hooks/usePublicClient.ts +45 -0
- package/src/hooks/useReconnect.ts +67 -0
- package/src/hooks/useSendTransaction.ts +79 -0
- package/src/hooks/useSignMessage.ts +65 -0
- package/src/hooks/useSignTypedData.ts +66 -0
- package/src/hooks/useSwitchAccount.ts +82 -0
- package/src/hooks/useSwitchChain.ts +77 -0
- package/src/hooks/useSyncExternalStoreWithTracked.ts +67 -0
- package/src/hooks/useToken.ts +63 -0
- package/src/hooks/useTransaction.ts +68 -0
- package/src/hooks/useWaitForTransactionReceipt.ts +74 -0
- package/src/hooks/useWalletClient.ts +96 -0
- package/src/hooks/useWatchBlockNumber.ts +49 -0
- package/src/hooks/useWatchContractEvent.ts +64 -0
- package/src/hooks/useWatchPendingTransactions.ts +52 -0
- package/src/types/properties.ts +9 -0
- package/src/utils/getVersion.ts +3 -0
- package/src/utils/query.ts +102 -0
- package/src/version.ts +1 -0
- package/README.md +0 -30
- package/actions/package.json +0 -4
- package/chains/package.json +0 -4
- package/connectors/coinbaseWallet/package.json +0 -4
- package/connectors/injected/package.json +0 -4
- package/connectors/ledger/package.json +0 -4
- package/connectors/metaMask/package.json +0 -4
- package/connectors/mock/package.json +0 -4
- package/connectors/package.json +0 -4
- package/connectors/safe/package.json +0 -4
- package/connectors/walletConnect/package.json +0 -4
- package/connectors/walletConnectLegacy/package.json +0 -4
- package/dist/actions.d.ts +0 -1
- package/dist/actions.js +0 -85
- package/dist/chains.d.ts +0 -2
- package/dist/chains.js +0 -4
- package/dist/connectors/coinbaseWallet.d.ts +0 -1
- package/dist/connectors/coinbaseWallet.js +0 -7
- package/dist/connectors/index.d.ts +0 -1
- package/dist/connectors/index.js +0 -7
- package/dist/connectors/injected.d.ts +0 -1
- package/dist/connectors/injected.js +0 -7
- package/dist/connectors/ledger.d.ts +0 -1
- package/dist/connectors/ledger.js +0 -7
- package/dist/connectors/metaMask.d.ts +0 -1
- package/dist/connectors/metaMask.js +0 -7
- package/dist/connectors/mock.d.ts +0 -1
- package/dist/connectors/mock.js +0 -7
- package/dist/connectors/safe.d.ts +0 -1
- package/dist/connectors/safe.js +0 -7
- package/dist/connectors/walletConnect.d.ts +0 -1
- package/dist/connectors/walletConnect.js +0 -7
- package/dist/connectors/walletConnectLegacy.d.ts +0 -1
- package/dist/connectors/walletConnectLegacy.js +0 -7
- package/dist/index.d.ts +0 -958
- package/dist/index.js +0 -2564
- package/dist/providers/alchemy.d.ts +0 -1
- package/dist/providers/alchemy.js +0 -7
- package/dist/providers/infura.d.ts +0 -1
- package/dist/providers/infura.js +0 -7
- package/dist/providers/jsonRpc.d.ts +0 -1
- package/dist/providers/jsonRpc.js +0 -7
- package/dist/providers/public.d.ts +0 -1
- package/dist/providers/public.js +0 -7
- package/dist/window.d.ts +0 -76
- package/dist/window.js +0 -1
- package/providers/alchemy/package.json +0 -4
- package/providers/infura/package.json +0 -4
- package/providers/jsonRpc/package.json +0 -4
- package/providers/public/package.json +0 -4
- package/window/package.json +0 -4
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type Config,
|
|
5
|
+
type GetBalanceError,
|
|
6
|
+
type ResolvedRegister,
|
|
7
|
+
} from '@wagmi/core'
|
|
8
|
+
import type { Evaluate } from '@wagmi/core/internal'
|
|
9
|
+
import {
|
|
10
|
+
type GetBalanceData,
|
|
11
|
+
type GetBalanceOptions,
|
|
12
|
+
type GetBalanceQueryKey,
|
|
13
|
+
getBalanceQueryOptions,
|
|
14
|
+
} from '@wagmi/core/query'
|
|
15
|
+
import type { GetBalanceQueryFnData } from '@wagmi/core/query'
|
|
16
|
+
|
|
17
|
+
import type { ConfigParameter } from '../types/properties.js'
|
|
18
|
+
import {
|
|
19
|
+
type UseQueryParameters,
|
|
20
|
+
type UseQueryReturnType,
|
|
21
|
+
useQuery,
|
|
22
|
+
} from '../utils/query.js'
|
|
23
|
+
import { useChainId } from './useChainId.js'
|
|
24
|
+
import { useConfig } from './useConfig.js'
|
|
25
|
+
|
|
26
|
+
export type UseBalanceParameters<
|
|
27
|
+
config extends Config = Config,
|
|
28
|
+
selectData = GetBalanceData,
|
|
29
|
+
> = Evaluate<
|
|
30
|
+
GetBalanceOptions<config> &
|
|
31
|
+
ConfigParameter<config> & {
|
|
32
|
+
query?:
|
|
33
|
+
| UseQueryParameters<
|
|
34
|
+
GetBalanceQueryFnData,
|
|
35
|
+
GetBalanceError,
|
|
36
|
+
selectData,
|
|
37
|
+
GetBalanceQueryKey<config>
|
|
38
|
+
>
|
|
39
|
+
| undefined
|
|
40
|
+
}
|
|
41
|
+
>
|
|
42
|
+
|
|
43
|
+
export type UseBalanceReturnType<selectData = GetBalanceData> =
|
|
44
|
+
UseQueryReturnType<selectData, GetBalanceError>
|
|
45
|
+
|
|
46
|
+
/** https://alpha.wagmi.sh/react/api/hooks/useBalance */
|
|
47
|
+
export function useBalance<
|
|
48
|
+
config extends Config = ResolvedRegister['config'],
|
|
49
|
+
selectData = GetBalanceData,
|
|
50
|
+
>(
|
|
51
|
+
parameters: UseBalanceParameters<config, selectData> = {},
|
|
52
|
+
): UseBalanceReturnType<selectData> {
|
|
53
|
+
const { address, query = {} } = parameters
|
|
54
|
+
|
|
55
|
+
const config = useConfig(parameters)
|
|
56
|
+
const chainId = useChainId()
|
|
57
|
+
|
|
58
|
+
const queryOptions = getBalanceQueryOptions(config, {
|
|
59
|
+
...parameters,
|
|
60
|
+
chainId: parameters.chainId ?? chainId,
|
|
61
|
+
})
|
|
62
|
+
const enabled = Boolean(address && (query.enabled ?? true))
|
|
63
|
+
|
|
64
|
+
return useQuery({ ...queryOptions, ...query, enabled })
|
|
65
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useQueryClient } from '@tanstack/react-query'
|
|
4
|
+
import {
|
|
5
|
+
type Config,
|
|
6
|
+
type GetBlockNumberError,
|
|
7
|
+
type ResolvedRegister,
|
|
8
|
+
} from '@wagmi/core'
|
|
9
|
+
import { type Evaluate } from '@wagmi/core/internal'
|
|
10
|
+
import {
|
|
11
|
+
type GetBlockNumberData,
|
|
12
|
+
type GetBlockNumberOptions,
|
|
13
|
+
type GetBlockNumberQueryFnData,
|
|
14
|
+
type GetBlockNumberQueryKey,
|
|
15
|
+
getBlockNumberQueryOptions,
|
|
16
|
+
} from '@wagmi/core/query'
|
|
17
|
+
|
|
18
|
+
import type { ConfigParameter } from '../types/properties.js'
|
|
19
|
+
import {
|
|
20
|
+
type UseQueryParameters,
|
|
21
|
+
type UseQueryReturnType,
|
|
22
|
+
useQuery,
|
|
23
|
+
} from '../utils/query.js'
|
|
24
|
+
import { useChainId } from './useChainId.js'
|
|
25
|
+
import { useConfig } from './useConfig.js'
|
|
26
|
+
import {
|
|
27
|
+
type UseWatchBlockNumberParameters,
|
|
28
|
+
useWatchBlockNumber,
|
|
29
|
+
} from './useWatchBlockNumber.js'
|
|
30
|
+
|
|
31
|
+
export type UseBlockNumberParameters<
|
|
32
|
+
config extends Config = Config,
|
|
33
|
+
chainId extends config['chains'][number]['id'] = config['chains'][number]['id'],
|
|
34
|
+
selectData = GetBlockNumberData,
|
|
35
|
+
> = Evaluate<
|
|
36
|
+
GetBlockNumberOptions<config> &
|
|
37
|
+
ConfigParameter<config> & {
|
|
38
|
+
query?:
|
|
39
|
+
| UseQueryParameters<
|
|
40
|
+
GetBlockNumberQueryFnData,
|
|
41
|
+
GetBlockNumberError,
|
|
42
|
+
selectData,
|
|
43
|
+
GetBlockNumberQueryKey<config>
|
|
44
|
+
>
|
|
45
|
+
| undefined
|
|
46
|
+
watch?:
|
|
47
|
+
| boolean
|
|
48
|
+
| Omit<
|
|
49
|
+
UseWatchBlockNumberParameters<config, chainId>,
|
|
50
|
+
'chainId' | 'config' | 'onBlockNumber' | 'onError'
|
|
51
|
+
>
|
|
52
|
+
| undefined
|
|
53
|
+
}
|
|
54
|
+
>
|
|
55
|
+
|
|
56
|
+
export type UseBlockNumberReturnType<selectData = GetBlockNumberData> =
|
|
57
|
+
UseQueryReturnType<selectData, GetBlockNumberError>
|
|
58
|
+
|
|
59
|
+
/** https://alpha.wagmi.sh/react/api/hooks/useBlockNumber */
|
|
60
|
+
export function useBlockNumber<
|
|
61
|
+
config extends Config = ResolvedRegister['config'],
|
|
62
|
+
chainId extends config['chains'][number]['id'] = config['chains'][number]['id'],
|
|
63
|
+
selectData = GetBlockNumberData,
|
|
64
|
+
>(
|
|
65
|
+
parameters: UseBlockNumberParameters<config, chainId, selectData> = {},
|
|
66
|
+
): UseBlockNumberReturnType<selectData> {
|
|
67
|
+
const { query = {}, watch } = parameters
|
|
68
|
+
|
|
69
|
+
const config = useConfig(parameters)
|
|
70
|
+
const queryClient = useQueryClient()
|
|
71
|
+
const configChainId = useChainId()
|
|
72
|
+
const chainId = parameters.chainId ?? configChainId
|
|
73
|
+
|
|
74
|
+
const queryOptions = getBlockNumberQueryOptions(config, {
|
|
75
|
+
...parameters,
|
|
76
|
+
chainId,
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
useWatchBlockNumber({
|
|
80
|
+
...{
|
|
81
|
+
config: parameters.config,
|
|
82
|
+
chainId: parameters.chainId as number,
|
|
83
|
+
},
|
|
84
|
+
...(typeof watch === 'object'
|
|
85
|
+
? (watch as UseWatchBlockNumberParameters)
|
|
86
|
+
: {}),
|
|
87
|
+
enabled: Boolean(
|
|
88
|
+
(query.enabled ?? true) &&
|
|
89
|
+
(typeof watch === 'object' ? watch.enabled : watch),
|
|
90
|
+
),
|
|
91
|
+
onBlockNumber(blockNumber) {
|
|
92
|
+
queryClient.setQueryData(queryOptions.queryKey, blockNumber)
|
|
93
|
+
},
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
return useQuery({ ...queryOptions, ...query })
|
|
97
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type Config,
|
|
5
|
+
type GetChainIdReturnType,
|
|
6
|
+
type ResolvedRegister,
|
|
7
|
+
getChainId,
|
|
8
|
+
watchChainId,
|
|
9
|
+
} from '@wagmi/core'
|
|
10
|
+
import { useSyncExternalStore } from 'react'
|
|
11
|
+
|
|
12
|
+
import type { ConfigParameter } from '../types/properties.js'
|
|
13
|
+
import { useConfig } from './useConfig.js'
|
|
14
|
+
|
|
15
|
+
export type UseChainIdParameters<config extends Config = Config> =
|
|
16
|
+
ConfigParameter<config>
|
|
17
|
+
|
|
18
|
+
export type UseChainIdReturnType<config extends Config = Config> =
|
|
19
|
+
GetChainIdReturnType<config>
|
|
20
|
+
|
|
21
|
+
/** https://alpha.wagmi.sh/react/api/hooks/useChainId */
|
|
22
|
+
export function useChainId<config extends Config = ResolvedRegister['config']>(
|
|
23
|
+
parameters: UseChainIdParameters<config> = {},
|
|
24
|
+
): UseChainIdReturnType<config> {
|
|
25
|
+
const config = useConfig(parameters)
|
|
26
|
+
|
|
27
|
+
return useSyncExternalStore(
|
|
28
|
+
(onChange) => watchChainId(config, { onChange }),
|
|
29
|
+
() => getChainId(config),
|
|
30
|
+
)
|
|
31
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type Config,
|
|
5
|
+
type GetClientParameters,
|
|
6
|
+
type GetClientReturnType,
|
|
7
|
+
type ResolvedRegister,
|
|
8
|
+
getClient,
|
|
9
|
+
watchClient,
|
|
10
|
+
} from '@wagmi/core'
|
|
11
|
+
import type { Evaluate } from '@wagmi/core/internal'
|
|
12
|
+
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector.js'
|
|
13
|
+
|
|
14
|
+
import type { ConfigParameter } from '../types/properties.js'
|
|
15
|
+
import { useConfig } from './useConfig.js'
|
|
16
|
+
|
|
17
|
+
export type UseClientParameters<
|
|
18
|
+
config extends Config = Config,
|
|
19
|
+
chainId extends config['chains'][number]['id'] = config['chains'][number]['id'],
|
|
20
|
+
> = Evaluate<GetClientParameters<config, chainId> & ConfigParameter<config>>
|
|
21
|
+
|
|
22
|
+
export type UseClientReturnType<
|
|
23
|
+
config extends Config = Config,
|
|
24
|
+
chainId extends config['chains'][number]['id'] = config['chains'][number]['id'],
|
|
25
|
+
> = GetClientReturnType<config, chainId>
|
|
26
|
+
|
|
27
|
+
/** https://alpha.wagmi.sh/react/api/hooks/useClient */
|
|
28
|
+
export function useClient<
|
|
29
|
+
config extends Config = ResolvedRegister['config'],
|
|
30
|
+
chainId extends config['chains'][number]['id'] = config['chains'][number]['id'],
|
|
31
|
+
>(
|
|
32
|
+
parameters: UseClientParameters<config, chainId> = {},
|
|
33
|
+
): UseClientReturnType<config, chainId> {
|
|
34
|
+
const config = useConfig(parameters)
|
|
35
|
+
|
|
36
|
+
return useSyncExternalStoreWithSelector(
|
|
37
|
+
(onChange) => watchClient(config, { onChange }),
|
|
38
|
+
() => getClient(config, parameters),
|
|
39
|
+
() => getClient(config, parameters),
|
|
40
|
+
(x) => x,
|
|
41
|
+
(a, b) => a.uid === b.uid,
|
|
42
|
+
) as any
|
|
43
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { type Config, type ResolvedRegister } from '@wagmi/core'
|
|
4
|
+
import { useContext } from 'react'
|
|
5
|
+
|
|
6
|
+
import { WagmiContext } from '../context.js'
|
|
7
|
+
import { WagmiProviderNotFoundError } from '../errors/context.js'
|
|
8
|
+
import type { ConfigParameter } from '../types/properties.js'
|
|
9
|
+
|
|
10
|
+
export type UseConfigParameters<config extends Config = Config> =
|
|
11
|
+
ConfigParameter<config>
|
|
12
|
+
|
|
13
|
+
export type UseConfigReturnType<config extends Config = Config> = config
|
|
14
|
+
|
|
15
|
+
/** https://alpha.wagmi.sh/react/api/hooks/useConfig */
|
|
16
|
+
export function useConfig<config extends Config = ResolvedRegister['config']>(
|
|
17
|
+
parameters: UseConfigParameters<config> = {},
|
|
18
|
+
): UseConfigReturnType<config> {
|
|
19
|
+
const config = parameters.config ?? useContext(WagmiContext)
|
|
20
|
+
if (!config) throw new WagmiProviderNotFoundError()
|
|
21
|
+
return config as UseConfigReturnType<config>
|
|
22
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useMutation } from '@tanstack/react-query'
|
|
4
|
+
import {
|
|
5
|
+
type Config,
|
|
6
|
+
type ConnectError,
|
|
7
|
+
type ResolvedRegister,
|
|
8
|
+
} from '@wagmi/core'
|
|
9
|
+
import type { Evaluate } from '@wagmi/core/internal'
|
|
10
|
+
import {
|
|
11
|
+
type ConnectData,
|
|
12
|
+
type ConnectMutate,
|
|
13
|
+
type ConnectMutateAsync,
|
|
14
|
+
type ConnectVariables,
|
|
15
|
+
connectMutationOptions,
|
|
16
|
+
} from '@wagmi/core/query'
|
|
17
|
+
|
|
18
|
+
import type { ConfigParameter } from '../types/properties.js'
|
|
19
|
+
import type {
|
|
20
|
+
UseMutationParameters,
|
|
21
|
+
UseMutationReturnType,
|
|
22
|
+
} from '../utils/query.js'
|
|
23
|
+
import { useConfig } from './useConfig.js'
|
|
24
|
+
|
|
25
|
+
export type UseConnectParameters<
|
|
26
|
+
config extends Config = Config,
|
|
27
|
+
context = unknown,
|
|
28
|
+
> = Evaluate<
|
|
29
|
+
ConfigParameter<config> & {
|
|
30
|
+
mutation?:
|
|
31
|
+
| UseMutationParameters<
|
|
32
|
+
ConnectData<config>,
|
|
33
|
+
ConnectError,
|
|
34
|
+
ConnectVariables<config>,
|
|
35
|
+
context
|
|
36
|
+
>
|
|
37
|
+
| undefined
|
|
38
|
+
}
|
|
39
|
+
>
|
|
40
|
+
|
|
41
|
+
export type UseConnectReturnType<
|
|
42
|
+
config extends Config = Config,
|
|
43
|
+
context = unknown,
|
|
44
|
+
> = Evaluate<
|
|
45
|
+
UseMutationReturnType<
|
|
46
|
+
ConnectData<config>,
|
|
47
|
+
ConnectError,
|
|
48
|
+
ConnectVariables<config>,
|
|
49
|
+
context
|
|
50
|
+
> & {
|
|
51
|
+
connect: ConnectMutate<config, context>
|
|
52
|
+
connectAsync: ConnectMutateAsync<config, context>
|
|
53
|
+
connectors: config['connectors']
|
|
54
|
+
}
|
|
55
|
+
>
|
|
56
|
+
|
|
57
|
+
/** https://alpha.wagmi.sh/react/api/hooks/useConnect */
|
|
58
|
+
export function useConnect<
|
|
59
|
+
config extends Config = ResolvedRegister['config'],
|
|
60
|
+
context = unknown,
|
|
61
|
+
>(
|
|
62
|
+
parameters: UseConnectParameters<config, context> = {},
|
|
63
|
+
): UseConnectReturnType<config, context> {
|
|
64
|
+
const { mutation } = parameters
|
|
65
|
+
|
|
66
|
+
const config = useConfig(parameters)
|
|
67
|
+
|
|
68
|
+
const mutationOptions = connectMutationOptions(config)
|
|
69
|
+
const { mutate, mutateAsync, ...result } = useMutation({
|
|
70
|
+
...mutation,
|
|
71
|
+
...mutationOptions,
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
...result,
|
|
76
|
+
connect: mutate,
|
|
77
|
+
connectAsync: mutateAsync,
|
|
78
|
+
connectors: config.connectors,
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type GetConnectionsReturnType,
|
|
5
|
+
getConnections,
|
|
6
|
+
watchConnections,
|
|
7
|
+
} from '@wagmi/core'
|
|
8
|
+
import { useSyncExternalStore } from 'react'
|
|
9
|
+
|
|
10
|
+
import type { ConfigParameter } from '../types/properties.js'
|
|
11
|
+
import { useConfig } from './useConfig.js'
|
|
12
|
+
|
|
13
|
+
export type UseConnectionsParameters = ConfigParameter
|
|
14
|
+
|
|
15
|
+
export type UseConnectionsReturnType = GetConnectionsReturnType
|
|
16
|
+
|
|
17
|
+
/** https://alpha.wagmi.sh/react/api/hooks/useConnections */
|
|
18
|
+
export function useConnections(
|
|
19
|
+
parameters: UseConnectionsParameters = {},
|
|
20
|
+
): UseConnectionsReturnType {
|
|
21
|
+
const config = useConfig(parameters)
|
|
22
|
+
|
|
23
|
+
return useSyncExternalStore(
|
|
24
|
+
(onChange) => watchConnections(config, { onChange }),
|
|
25
|
+
() => getConnections(config),
|
|
26
|
+
)
|
|
27
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useQueryClient } from '@tanstack/react-query'
|
|
4
|
+
import type {
|
|
5
|
+
Config,
|
|
6
|
+
GetConnectorClientError,
|
|
7
|
+
ResolvedRegister,
|
|
8
|
+
} from '@wagmi/core'
|
|
9
|
+
import { type Evaluate, type Omit } from '@wagmi/core/internal'
|
|
10
|
+
import {
|
|
11
|
+
type GetConnectorClientData,
|
|
12
|
+
type GetConnectorClientOptions,
|
|
13
|
+
type GetConnectorClientQueryFnData,
|
|
14
|
+
type GetConnectorClientQueryKey,
|
|
15
|
+
getConnectorClientQueryOptions,
|
|
16
|
+
} from '@wagmi/core/query'
|
|
17
|
+
import { useEffect } from 'react'
|
|
18
|
+
|
|
19
|
+
import type { ConfigParameter } from '../types/properties.js'
|
|
20
|
+
import {
|
|
21
|
+
type UseQueryParameters,
|
|
22
|
+
type UseQueryReturnType,
|
|
23
|
+
useQuery,
|
|
24
|
+
} from '../utils/query.js'
|
|
25
|
+
import { useAccount } from './useAccount.js'
|
|
26
|
+
import { useChainId } from './useChainId.js'
|
|
27
|
+
import { useConfig } from './useConfig.js'
|
|
28
|
+
|
|
29
|
+
export type UseConnectorClientParameters<
|
|
30
|
+
config extends Config = Config,
|
|
31
|
+
chainId extends config['chains'][number]['id'] = config['chains'][number]['id'],
|
|
32
|
+
selectData = GetConnectorClientData<config, chainId>,
|
|
33
|
+
> = Evaluate<
|
|
34
|
+
GetConnectorClientOptions<config, chainId> &
|
|
35
|
+
ConfigParameter<config> & {
|
|
36
|
+
query?:
|
|
37
|
+
| Evaluate<
|
|
38
|
+
Omit<
|
|
39
|
+
UseQueryParameters<
|
|
40
|
+
GetConnectorClientQueryFnData<config, chainId>,
|
|
41
|
+
GetConnectorClientError,
|
|
42
|
+
selectData,
|
|
43
|
+
GetConnectorClientQueryKey<config, chainId>
|
|
44
|
+
>,
|
|
45
|
+
'gcTime' | 'staleTime'
|
|
46
|
+
>
|
|
47
|
+
>
|
|
48
|
+
| undefined
|
|
49
|
+
}
|
|
50
|
+
>
|
|
51
|
+
|
|
52
|
+
export type UseConnectorClientReturnType<
|
|
53
|
+
config extends Config = Config,
|
|
54
|
+
chainId extends config['chains'][number]['id'] = config['chains'][number]['id'],
|
|
55
|
+
selectData = GetConnectorClientData<config, chainId>,
|
|
56
|
+
> = UseQueryReturnType<selectData, GetConnectorClientError>
|
|
57
|
+
|
|
58
|
+
/** https://alpha.wagmi.sh/react/api/hooks/useConnectorClient */
|
|
59
|
+
export function useConnectorClient<
|
|
60
|
+
config extends Config = ResolvedRegister['config'],
|
|
61
|
+
chainId extends config['chains'][number]['id'] = config['chains'][number]['id'],
|
|
62
|
+
selectData = GetConnectorClientData<config, chainId>,
|
|
63
|
+
>(
|
|
64
|
+
parameters: UseConnectorClientParameters<config, chainId, selectData> = {},
|
|
65
|
+
): UseConnectorClientReturnType<config, chainId, selectData> {
|
|
66
|
+
const { query = {} } = parameters
|
|
67
|
+
|
|
68
|
+
const config = useConfig(parameters)
|
|
69
|
+
const queryClient = useQueryClient()
|
|
70
|
+
const { address, connector, status } = useAccount()
|
|
71
|
+
const chainId = useChainId()
|
|
72
|
+
|
|
73
|
+
const { queryKey, ...options } = getConnectorClientQueryOptions(config, {
|
|
74
|
+
...parameters,
|
|
75
|
+
chainId: parameters.chainId ?? chainId,
|
|
76
|
+
connector: parameters.connector ?? connector,
|
|
77
|
+
})
|
|
78
|
+
const enabled = Boolean(status !== 'disconnected' && (query.enabled ?? true))
|
|
79
|
+
|
|
80
|
+
// biome-ignore lint/nursery/useExhaustiveDependencies: `queryKey` not required
|
|
81
|
+
useEffect(() => {
|
|
82
|
+
// invalidate when address changes
|
|
83
|
+
if (address) queryClient.invalidateQueries({ queryKey })
|
|
84
|
+
else queryClient.removeQueries({ queryKey }) // remove when account is disconnected
|
|
85
|
+
}, [address, queryClient])
|
|
86
|
+
|
|
87
|
+
return useQuery({
|
|
88
|
+
...options,
|
|
89
|
+
...query,
|
|
90
|
+
queryKey,
|
|
91
|
+
enabled,
|
|
92
|
+
gcTime: 0,
|
|
93
|
+
staleTime: Infinity,
|
|
94
|
+
})
|
|
95
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type Config,
|
|
5
|
+
type ReadContractError,
|
|
6
|
+
type ResolvedRegister,
|
|
7
|
+
} from '@wagmi/core'
|
|
8
|
+
import { type UnionEvaluate } from '@wagmi/core/internal'
|
|
9
|
+
import {
|
|
10
|
+
type ReadContractData,
|
|
11
|
+
type ReadContractOptions,
|
|
12
|
+
type ReadContractQueryFnData,
|
|
13
|
+
type ReadContractQueryKey,
|
|
14
|
+
readContractQueryOptions,
|
|
15
|
+
} from '@wagmi/core/query'
|
|
16
|
+
import {
|
|
17
|
+
type Abi,
|
|
18
|
+
type ContractFunctionArgs,
|
|
19
|
+
type ContractFunctionName,
|
|
20
|
+
} from 'viem'
|
|
21
|
+
|
|
22
|
+
import type { ConfigParameter } from '../types/properties.js'
|
|
23
|
+
import {
|
|
24
|
+
type UseQueryParameters,
|
|
25
|
+
type UseQueryReturnType,
|
|
26
|
+
structuralSharing,
|
|
27
|
+
useQuery,
|
|
28
|
+
} from '../utils/query.js'
|
|
29
|
+
import { useChainId } from './useChainId.js'
|
|
30
|
+
import { useConfig } from './useConfig.js'
|
|
31
|
+
|
|
32
|
+
export type UseContractReadParameters<
|
|
33
|
+
abi extends Abi | readonly unknown[] = Abi,
|
|
34
|
+
functionName extends ContractFunctionName<
|
|
35
|
+
abi,
|
|
36
|
+
'pure' | 'view'
|
|
37
|
+
> = ContractFunctionName<abi, 'pure' | 'view'>,
|
|
38
|
+
args extends ContractFunctionArgs<
|
|
39
|
+
abi,
|
|
40
|
+
'pure' | 'view',
|
|
41
|
+
functionName
|
|
42
|
+
> = ContractFunctionArgs<abi, 'pure' | 'view', functionName>,
|
|
43
|
+
config extends Config = Config,
|
|
44
|
+
selectData = ReadContractData<abi, functionName, args>,
|
|
45
|
+
> = UnionEvaluate<
|
|
46
|
+
ReadContractOptions<abi, functionName, args, config> &
|
|
47
|
+
ConfigParameter<config> & {
|
|
48
|
+
query?:
|
|
49
|
+
| UseQueryParameters<
|
|
50
|
+
ReadContractQueryFnData<abi, functionName, args>,
|
|
51
|
+
ReadContractError,
|
|
52
|
+
selectData,
|
|
53
|
+
ReadContractQueryKey<config, abi, functionName, args>
|
|
54
|
+
>
|
|
55
|
+
| undefined
|
|
56
|
+
}
|
|
57
|
+
>
|
|
58
|
+
|
|
59
|
+
export type UseContractReadReturnType<
|
|
60
|
+
abi extends Abi | readonly unknown[] = Abi,
|
|
61
|
+
functionName extends ContractFunctionName<
|
|
62
|
+
abi,
|
|
63
|
+
'pure' | 'view'
|
|
64
|
+
> = ContractFunctionName<abi, 'pure' | 'view'>,
|
|
65
|
+
args extends ContractFunctionArgs<
|
|
66
|
+
abi,
|
|
67
|
+
'pure' | 'view',
|
|
68
|
+
functionName
|
|
69
|
+
> = ContractFunctionArgs<abi, 'pure' | 'view', functionName>,
|
|
70
|
+
selectData = ReadContractData<abi, functionName, args>,
|
|
71
|
+
> = UseQueryReturnType<selectData, ReadContractError>
|
|
72
|
+
|
|
73
|
+
/** https://alpha.wagmi.sh/react/api/hooks/useContractRead */
|
|
74
|
+
export function useContractRead<
|
|
75
|
+
const abi extends Abi | readonly unknown[],
|
|
76
|
+
functionName extends ContractFunctionName<abi, 'pure' | 'view'>,
|
|
77
|
+
args extends ContractFunctionArgs<abi, 'pure' | 'view', functionName>,
|
|
78
|
+
config extends Config = ResolvedRegister['config'],
|
|
79
|
+
selectData = ReadContractData<abi, functionName, args>,
|
|
80
|
+
>(
|
|
81
|
+
parameters: UseContractReadParameters<
|
|
82
|
+
abi,
|
|
83
|
+
functionName,
|
|
84
|
+
args,
|
|
85
|
+
config,
|
|
86
|
+
selectData
|
|
87
|
+
> = {} as any,
|
|
88
|
+
): UseContractReadReturnType<abi, functionName, args, selectData> {
|
|
89
|
+
const { abi, address, functionName, query = {} } = parameters
|
|
90
|
+
const config = useConfig(parameters)
|
|
91
|
+
const chainId = useChainId()
|
|
92
|
+
|
|
93
|
+
const queryOptions = readContractQueryOptions(config, {
|
|
94
|
+
...(parameters as any),
|
|
95
|
+
chainId: parameters.chainId ?? chainId,
|
|
96
|
+
})
|
|
97
|
+
const enabled = Boolean(
|
|
98
|
+
address && abi && functionName && (query.enabled ?? true),
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
return useQuery({
|
|
102
|
+
...queryOptions,
|
|
103
|
+
...query,
|
|
104
|
+
enabled,
|
|
105
|
+
structuralSharing: query.structuralSharing ?? structuralSharing,
|
|
106
|
+
})
|
|
107
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type Config,
|
|
5
|
+
type ReadContractError,
|
|
6
|
+
type ResolvedRegister,
|
|
7
|
+
} from '@wagmi/core'
|
|
8
|
+
import { type Evaluate } from '@wagmi/core/internal'
|
|
9
|
+
import {
|
|
10
|
+
type ReadContractsData,
|
|
11
|
+
type ReadContractsOptions,
|
|
12
|
+
type ReadContractsQueryFnData,
|
|
13
|
+
type ReadContractsQueryKey,
|
|
14
|
+
readContractsQueryOptions,
|
|
15
|
+
} from '@wagmi/core/query'
|
|
16
|
+
import { useMemo } from 'react'
|
|
17
|
+
import type { ContractFunctionParameters } from 'viem'
|
|
18
|
+
|
|
19
|
+
import type { ConfigParameter } from '../types/properties.js'
|
|
20
|
+
import {
|
|
21
|
+
type UseQueryParameters,
|
|
22
|
+
type UseQueryReturnType,
|
|
23
|
+
structuralSharing,
|
|
24
|
+
useQuery,
|
|
25
|
+
} from '../utils/query.js'
|
|
26
|
+
import { useChainId } from './useChainId.js'
|
|
27
|
+
import { useConfig } from './useConfig.js'
|
|
28
|
+
|
|
29
|
+
export type UseContractReadsParameters<
|
|
30
|
+
contracts extends readonly unknown[] = readonly ContractFunctionParameters[],
|
|
31
|
+
allowFailure extends boolean = true,
|
|
32
|
+
config extends Config = Config,
|
|
33
|
+
selectData = ReadContractsData<contracts, allowFailure>,
|
|
34
|
+
> = Evaluate<
|
|
35
|
+
ReadContractsOptions<config, contracts, allowFailure> &
|
|
36
|
+
ConfigParameter<config> & {
|
|
37
|
+
query?:
|
|
38
|
+
| UseQueryParameters<
|
|
39
|
+
ReadContractsQueryFnData<contracts, allowFailure>,
|
|
40
|
+
ReadContractError,
|
|
41
|
+
selectData,
|
|
42
|
+
ReadContractsQueryKey<config, contracts, allowFailure>
|
|
43
|
+
>
|
|
44
|
+
| undefined
|
|
45
|
+
}
|
|
46
|
+
>
|
|
47
|
+
|
|
48
|
+
export type UseContractReadsReturnType<
|
|
49
|
+
contracts extends readonly unknown[] = readonly ContractFunctionParameters[],
|
|
50
|
+
allowFailure extends boolean = true,
|
|
51
|
+
selectData = ReadContractsData<contracts, allowFailure>,
|
|
52
|
+
> = UseQueryReturnType<selectData, ReadContractError>
|
|
53
|
+
|
|
54
|
+
/** https://alpha.wagmi.sh/react/api/hooks/useContractReads */
|
|
55
|
+
export function useContractReads<
|
|
56
|
+
const contracts extends readonly unknown[],
|
|
57
|
+
allowFailure extends boolean = true,
|
|
58
|
+
config extends Config = ResolvedRegister['config'],
|
|
59
|
+
selectData = ReadContractsData<contracts, allowFailure>,
|
|
60
|
+
>(
|
|
61
|
+
parameters: UseContractReadsParameters<
|
|
62
|
+
contracts,
|
|
63
|
+
allowFailure,
|
|
64
|
+
config,
|
|
65
|
+
selectData
|
|
66
|
+
> = {},
|
|
67
|
+
): UseContractReadsReturnType<contracts, allowFailure, selectData> {
|
|
68
|
+
const { contracts = [], query = {} } = parameters
|
|
69
|
+
|
|
70
|
+
const config = useConfig(parameters)
|
|
71
|
+
const chainId = useChainId()
|
|
72
|
+
|
|
73
|
+
const queryOptions = readContractsQueryOptions(config, {
|
|
74
|
+
...parameters,
|
|
75
|
+
chainId,
|
|
76
|
+
contracts,
|
|
77
|
+
})
|
|
78
|
+
const enabled = useMemo(() => {
|
|
79
|
+
let isContractsValid = false
|
|
80
|
+
for (const contract of contracts) {
|
|
81
|
+
const { abi, address, functionName } =
|
|
82
|
+
contract as ContractFunctionParameters
|
|
83
|
+
if (!abi || !address || !functionName) {
|
|
84
|
+
isContractsValid = false
|
|
85
|
+
break
|
|
86
|
+
}
|
|
87
|
+
isContractsValid = true
|
|
88
|
+
}
|
|
89
|
+
return Boolean(isContractsValid && (query.enabled ?? true))
|
|
90
|
+
}, [contracts, query.enabled])
|
|
91
|
+
|
|
92
|
+
return useQuery({
|
|
93
|
+
...queryOptions,
|
|
94
|
+
...query,
|
|
95
|
+
enabled,
|
|
96
|
+
structuralSharing: query.structuralSharing ?? structuralSharing,
|
|
97
|
+
})
|
|
98
|
+
}
|