@wagmi/core 2.0.0-beta.5 → 2.0.0-beta.6

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.
Files changed (96) hide show
  1. package/actions/package.json.tmp +5 -0
  2. package/chains/package.json.tmp +5 -0
  3. package/dist/esm/actions/disconnect.js +7 -11
  4. package/dist/esm/actions/disconnect.js.map +1 -1
  5. package/dist/esm/actions/getBlock.js.map +1 -1
  6. package/dist/esm/actions/getTransaction.js.map +1 -1
  7. package/dist/esm/actions/getTransactionCount.js +11 -0
  8. package/dist/esm/actions/getTransactionCount.js.map +1 -0
  9. package/dist/esm/actions/waitForTransactionReceipt.js.map +1 -1
  10. package/dist/esm/actions/writeContract.js.map +1 -1
  11. package/dist/esm/connectors/createConnector.js.map +1 -1
  12. package/dist/esm/connectors/injected.js +10 -4
  13. package/dist/esm/connectors/injected.js.map +1 -1
  14. package/dist/esm/connectors/mock.js +2 -0
  15. package/dist/esm/connectors/mock.js.map +1 -1
  16. package/dist/esm/createConfig.js +4 -1
  17. package/dist/esm/createConfig.js.map +1 -1
  18. package/dist/esm/exports/actions.js +1 -0
  19. package/dist/esm/exports/actions.js.map +1 -1
  20. package/dist/esm/exports/index.js +6 -4
  21. package/dist/esm/exports/index.js.map +1 -1
  22. package/dist/esm/exports/query.js +1 -0
  23. package/dist/esm/exports/query.js.map +1 -1
  24. package/dist/esm/query/getBlock.js +1 -1
  25. package/dist/esm/query/getBlock.js.map +1 -1
  26. package/dist/esm/query/getTransactionCount.js +23 -0
  27. package/dist/esm/query/getTransactionCount.js.map +1 -0
  28. package/dist/esm/transports/connector.js +34 -0
  29. package/dist/esm/transports/connector.js.map +1 -0
  30. package/dist/esm/transports/fallback.js +5 -0
  31. package/dist/esm/transports/fallback.js.map +1 -0
  32. package/dist/esm/tsconfig.build.tsbuildinfo +1 -1
  33. package/dist/esm/version.js +1 -1
  34. package/dist/types/actions/disconnect.d.ts +2 -2
  35. package/dist/types/actions/disconnect.d.ts.map +1 -1
  36. package/dist/types/actions/estimateGas.d.ts +2 -2
  37. package/dist/types/actions/estimateGas.d.ts.map +1 -1
  38. package/dist/types/actions/getBlock.d.ts +2 -2
  39. package/dist/types/actions/getBlock.d.ts.map +1 -1
  40. package/dist/types/actions/getTransaction.d.ts +2 -2
  41. package/dist/types/actions/getTransaction.d.ts.map +1 -1
  42. package/dist/types/actions/getTransactionCount.d.ts +10 -0
  43. package/dist/types/actions/getTransactionCount.d.ts.map +1 -0
  44. package/dist/types/actions/waitForTransactionReceipt.d.ts +2 -2
  45. package/dist/types/actions/waitForTransactionReceipt.d.ts.map +1 -1
  46. package/dist/types/actions/watchBlocks.d.ts +2 -2
  47. package/dist/types/actions/watchBlocks.d.ts.map +1 -1
  48. package/dist/types/actions/writeContract.d.ts.map +1 -1
  49. package/dist/types/connectors/createConnector.d.ts +1 -0
  50. package/dist/types/connectors/createConnector.d.ts.map +1 -1
  51. package/dist/types/connectors/injected.d.ts +3 -0
  52. package/dist/types/connectors/injected.d.ts.map +1 -1
  53. package/dist/types/connectors/mock.d.ts +3 -0
  54. package/dist/types/connectors/mock.d.ts.map +1 -1
  55. package/dist/types/createConfig.d.ts +4 -1
  56. package/dist/types/createConfig.d.ts.map +1 -1
  57. package/dist/types/exports/actions.d.ts +1 -0
  58. package/dist/types/exports/actions.d.ts.map +1 -1
  59. package/dist/types/exports/index.d.ts +4 -1
  60. package/dist/types/exports/index.d.ts.map +1 -1
  61. package/dist/types/exports/query.d.ts +1 -0
  62. package/dist/types/exports/query.d.ts.map +1 -1
  63. package/dist/types/query/getBlock.d.ts +1 -1
  64. package/dist/types/query/getTransaction.d.ts +1 -1
  65. package/dist/types/query/getTransactionCount.d.ts +36 -0
  66. package/dist/types/query/getTransactionCount.d.ts.map +1 -0
  67. package/dist/types/query/getWalletClient.d.ts +2 -2
  68. package/dist/types/query/waitForTransactionReceipt.d.ts +1 -1
  69. package/dist/types/transports/connector.d.ts +15 -0
  70. package/dist/types/transports/connector.d.ts.map +1 -0
  71. package/dist/types/transports/fallback.d.ts +4 -0
  72. package/dist/types/transports/fallback.d.ts.map +1 -0
  73. package/dist/types/version.d.ts +1 -1
  74. package/internal/package.json.tmp +5 -0
  75. package/package.json +7 -17
  76. package/query/package.json.tmp +5 -0
  77. package/src/actions/disconnect.ts +11 -17
  78. package/src/actions/estimateGas.ts +1 -1
  79. package/src/actions/getBlock.ts +2 -2
  80. package/src/actions/getTransaction.ts +4 -2
  81. package/src/actions/getTransactionCount.ts +31 -0
  82. package/src/actions/waitForTransactionReceipt.ts +4 -2
  83. package/src/actions/watchBlocks.ts +2 -2
  84. package/src/actions/writeContract.ts +1 -2
  85. package/src/connectors/createConnector.ts +1 -0
  86. package/src/connectors/injected.ts +13 -3
  87. package/src/connectors/mock.ts +2 -0
  88. package/src/createConfig.ts +12 -2
  89. package/src/exports/actions.ts +7 -0
  90. package/src/exports/index.ts +14 -11
  91. package/src/exports/query.ts +9 -0
  92. package/src/query/getBlock.ts +1 -1
  93. package/src/query/getTransactionCount.ts +55 -0
  94. package/src/transports/connector.ts +83 -0
  95. package/src/transports/fallback.ts +9 -0
  96. package/src/version.ts +1 -1
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@wagmi/core",
3
3
  "description": "VanillaJS library for Ethereum",
4
- "version": "2.0.0-beta.5",
4
+ "version": "2.0.0-beta.6",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
8
- "url": "https://github.com/wagmi-dev/wagmi.git",
8
+ "url": "https://github.com/wevm/wagmi.git",
9
9
  "directory": "packages/core"
10
10
  },
11
11
  "files": [
@@ -66,7 +66,7 @@
66
66
  "peerDependencies": {
67
67
  "@tanstack/query-core": ">=5.0.0",
68
68
  "typescript": ">=5.0.4",
69
- "viem": "2.0.0-beta.6"
69
+ "viem": "2.0.0-beta.15"
70
70
  },
71
71
  "peerDependenciesMeta": {
72
72
  "@tanstack/query-core": {
@@ -81,14 +81,11 @@
81
81
  "mipd": "^0.0.5",
82
82
  "zustand": "^4.4.1"
83
83
  },
84
- "devDependencies": {
85
- "@tanstack/query-core": "5.0.5"
86
- },
87
84
  "contributors": [
88
- "awkweb.eth <t@wagmi.sh>",
89
- "jxom.eth <j@wagmi.sh>"
85
+ "awkweb.eth <t@wevm.dev>",
86
+ "jxom.eth <j@wevm.dev>"
90
87
  ],
91
- "funding": "https://github.com/sponsors/wagmi-dev",
88
+ "funding": "https://github.com/sponsors/wevm",
92
89
  "keywords": [
93
90
  "wagmi",
94
91
  "eth",
@@ -96,12 +93,5 @@
96
93
  "dapps",
97
94
  "wallet",
98
95
  "web3"
99
- ],
100
- "scripts": {
101
- "build": "pnpm run clean && pnpm run build:esm+types",
102
- "build:esm+types": "tsc --project tsconfig.build.json --module es2020 --outDir ./dist/esm --declaration --declarationMap --declarationDir ./dist/types",
103
- "clean": "rm -rf dist tsconfig.tsbuildinfo actions chains internal query",
104
- "test:build": "publint --strict",
105
- "typecheck": "tsc --noEmit"
106
- }
96
+ ]
107
97
  }
@@ -0,0 +1,5 @@
1
+ {
2
+ "type": "module",
3
+ "types": "../dist/types/exports/query.d.ts",
4
+ "main": "../dist/esm/exports/query.js"
5
+ }
@@ -1,14 +1,8 @@
1
- import {
2
- type Config,
3
- type Connection,
4
- type Connector,
5
- } from '../createConfig.js'
1
+ import type { Config, Connection, Connector } from '../createConfig.js'
6
2
  import type { BaseErrorType, ErrorType } from '../errors/base.js'
7
- import {
8
- ConnectorNotConnectedError,
9
- type ConnectorNotConnectedErrorType,
10
- ConnectorNotFoundError,
11
- type ConnectorNotFoundErrorType,
3
+ import type {
4
+ ConnectorNotConnectedErrorType,
5
+ ConnectorNotFoundErrorType,
12
6
  } from '../errors/config.js'
13
7
  import type { ConnectorParameter } from '../types/properties.js'
14
8
 
@@ -36,16 +30,16 @@ export async function disconnect(
36
30
  connector = connection?.connector
37
31
  }
38
32
 
39
- if (!connector) throw new ConnectorNotFoundError()
40
33
  const connections = config.state.connections
41
- if (!connections.has(connector.uid)) throw new ConnectorNotConnectedError()
42
34
 
43
- await connector.disconnect()
44
- connector.emitter.off('change', config._internal.events.change)
45
- connector.emitter.off('disconnect', config._internal.events.disconnect)
46
- connector.emitter.on('connect', config._internal.events.connect)
35
+ if (connector) {
36
+ await connector.disconnect()
37
+ connector.emitter.off('change', config._internal.events.change)
38
+ connector.emitter.off('disconnect', config._internal.events.disconnect)
39
+ connector.emitter.on('connect', config._internal.events.connect)
47
40
 
48
- connections.delete(connector.uid)
41
+ connections.delete(connector.uid)
42
+ }
49
43
 
50
44
  config.setState((x) => {
51
45
  // if no connections exist, move to disconnected state
@@ -28,7 +28,7 @@ export type EstimateGasParameters<
28
28
  chains extends readonly Chain[] = SelectChains<config, chainId>,
29
29
  > = {
30
30
  [key in keyof chains]: UnionEvaluate<
31
- UnionLooseOmit<viem_EstimateGasParameters<chains[key], Account>, 'chain'> &
31
+ UnionLooseOmit<viem_EstimateGasParameters<chains[key]>, 'chain'> &
32
32
  ChainIdParameter<config, chainId> &
33
33
  ConnectorParameter
34
34
  >
@@ -9,7 +9,7 @@ import {
9
9
  import { type Config } from '../createConfig.js'
10
10
  import type { SelectChains } from '../types/chain.js'
11
11
  import { type ChainIdParameter } from '../types/properties.js'
12
- import { type Evaluate } from '../types/utils.js'
12
+ import { type Evaluate, type IsNarrowable } from '../types/utils.js'
13
13
 
14
14
  export type GetBlockParameters<
15
15
  includeTransactions extends boolean = false,
@@ -32,7 +32,7 @@ export type GetBlockReturnType<
32
32
  > = Evaluate<
33
33
  {
34
34
  [key in keyof chains]: viem_GetBlockReturnType<
35
- chains[key],
35
+ IsNarrowable<chains[key], Chain> extends true ? chains[key] : undefined,
36
36
  includeTransactions,
37
37
  blockTag
38
38
  >
@@ -9,7 +9,7 @@ import {
9
9
  import { type Config } from '../createConfig.js'
10
10
  import type { SelectChains } from '../types/chain.js'
11
11
  import { type ChainIdParameter } from '../types/properties.js'
12
- import { type Evaluate } from '../types/utils.js'
12
+ import { type Evaluate, type IsNarrowable } from '../types/utils.js'
13
13
 
14
14
  export type GetTransactionParameters<
15
15
  config extends Config = Config,
@@ -23,7 +23,9 @@ export type GetTransactionReturnType<
23
23
  chains extends readonly Chain[] = SelectChains<config, chainId>,
24
24
  > = Evaluate<
25
25
  {
26
- [key in keyof chains]: viem_GetTransactionReturnType<chains[key]>
26
+ [key in keyof chains]: viem_GetTransactionReturnType<
27
+ IsNarrowable<chains[key], Chain> extends true ? chains[key] : undefined
28
+ >
27
29
  }[number]
28
30
  >
29
31
 
@@ -0,0 +1,31 @@
1
+ import {
2
+ type GetTransactionCountErrorType as viem_GetTransactionCountErrorType,
3
+ type GetTransactionCountParameters as viem_GetTransactionCountParameters,
4
+ type GetTransactionCountReturnType as viem_GetTransactionCountReturnType,
5
+ getTransactionCount as viem_getTransactionCount,
6
+ } from 'viem/actions'
7
+
8
+ import { type Config } from '../createConfig.js'
9
+ import { type ChainIdParameter } from '../types/properties.js'
10
+ import { type Evaluate } from '../types/utils.js'
11
+
12
+ export type GetTransactionCountParameters<config extends Config = Config> =
13
+ Evaluate<ChainIdParameter<config> & viem_GetTransactionCountParameters>
14
+
15
+ export type GetTransactionCountReturnType = viem_GetTransactionCountReturnType
16
+
17
+ export type GetTransactionCountErrorType = viem_GetTransactionCountErrorType
18
+
19
+ /** https://beta.wagmi.sh/core/api/actions/getTransactionCount */
20
+ export async function getTransactionCount<config extends Config>(
21
+ config: config,
22
+ parameters: GetTransactionCountParameters<config>,
23
+ ): Promise<GetTransactionCountReturnType> {
24
+ const { address, blockNumber, blockTag, chainId } = parameters
25
+
26
+ const client = config.getClient({ chainId })
27
+ return await viem_getTransactionCount(
28
+ client,
29
+ blockNumber ? { address, blockNumber } : { address, blockTag },
30
+ )
31
+ }
@@ -12,7 +12,7 @@ import {
12
12
  import { type Config } from '../createConfig.js'
13
13
  import type { SelectChains } from '../types/chain.js'
14
14
  import { type ChainIdParameter } from '../types/properties.js'
15
- import { type Evaluate } from '../types/utils.js'
15
+ import { type Evaluate, type IsNarrowable } from '../types/utils.js'
16
16
 
17
17
  export type WaitForTransactionReceiptParameters<
18
18
  config extends Config = Config,
@@ -28,7 +28,9 @@ export type WaitForTransactionReceiptReturnType<
28
28
  chains extends readonly Chain[] = SelectChains<config, chainId>,
29
29
  > = Evaluate<
30
30
  {
31
- [key in keyof chains]: viem_WaitForTransactionReceiptReturnType<chains[key]>
31
+ [key in keyof chains]: viem_WaitForTransactionReceiptReturnType<
32
+ IsNarrowable<chains[key], Chain> extends true ? chains[key] : undefined
33
+ >
32
34
  }[number]
33
35
  >
34
36
 
@@ -11,7 +11,7 @@ import type {
11
11
  ChainIdParameter,
12
12
  SyncConnectedChainParameter,
13
13
  } from '../types/properties.js'
14
- import type { UnionEvaluate } from '../types/utils.js'
14
+ import type { IsNarrowable, UnionEvaluate } from '../types/utils.js'
15
15
 
16
16
  export type WatchBlocksParameters<
17
17
  includeTransactions extends boolean = false,
@@ -28,7 +28,7 @@ export type WatchBlocksParameters<
28
28
  ? WebSocketTransport
29
29
  : transport
30
30
  : WebSocketTransport,
31
- chains[key],
31
+ IsNarrowable<chains[key], Chain> extends true ? chains[key] : undefined,
32
32
  includeTransactions,
33
33
  blockTag
34
34
  > &
@@ -26,7 +26,6 @@ import {
26
26
  } from './getConnectorClient.js'
27
27
  import {
28
28
  type SimulateContractErrorType,
29
- type SimulateContractParameters,
30
29
  simulateContract,
31
30
  } from './simulateContract.js'
32
31
 
@@ -106,7 +105,7 @@ export async function writeContract<
106
105
  else {
107
106
  const { request: simulateRequest } = await simulateContract(
108
107
  config,
109
- rest as SimulateContractParameters,
108
+ rest as any,
110
109
  )
111
110
  request = simulateRequest
112
111
  }
@@ -34,6 +34,7 @@ export type CreateConnectorFn<
34
34
  readonly icon?: string | undefined
35
35
  readonly id: string
36
36
  readonly name: string
37
+ readonly type: string
37
38
 
38
39
  setup?(): Promise<void>
39
40
  connect(
@@ -9,6 +9,8 @@ import {
9
9
  UserRejectedRequestError,
10
10
  getAddress,
11
11
  numberToHex,
12
+ withRetry,
13
+ withTimeout,
12
14
  } from 'viem'
13
15
 
14
16
  import { ChainNotConfiguredError } from '../errors/config.js'
@@ -84,6 +86,7 @@ const targetMap = {
84
86
  },
85
87
  } as const satisfies TargetMap
86
88
 
89
+ injected.type = 'injected' as const
87
90
  export function injected(parameters: InjectedParameters = {}) {
88
91
  const { shimDisconnect = true, unstable_shimAsyncInject } = parameters
89
92
 
@@ -132,6 +135,7 @@ export function injected(parameters: InjectedParameters = {}) {
132
135
  get name() {
133
136
  return getTarget().name
134
137
  },
138
+ type: injected.type,
135
139
  async setup() {
136
140
  const provider = await this.getProvider()
137
141
  // Only start listening for events if `target` is set, otherwise `injected()` will also receive events
@@ -268,7 +272,7 @@ export function injected(parameters: InjectedParameters = {}) {
268
272
  unstable_shimAsyncInject !== false
269
273
  ) {
270
274
  // If no provider is found, check for async injection
271
- // https://github.com/wagmi-dev/references/issues/167
275
+ // https://github.com/wevm/references/issues/167
272
276
  // https://github.com/MetaMask/detect-provider
273
277
  const handleEthereum = async () => {
274
278
  if (typeof window !== 'undefined')
@@ -305,7 +309,13 @@ export function injected(parameters: InjectedParameters = {}) {
305
309
  throw new ProviderNotFoundError()
306
310
  }
307
311
 
308
- const accounts = await this.getAccounts()
312
+ // We are applying a retry & timeout strategy here as some injected wallets (ie. MetaMask) fail to
313
+ // immediately resolve a JSON-RPC request on page load.
314
+ const accounts = await withRetry(() =>
315
+ withTimeout(() => this.getAccounts(), {
316
+ timeout: 100,
317
+ }),
318
+ )
309
319
  return !!accounts.length
310
320
  } catch {
311
321
  return false
@@ -359,7 +369,7 @@ export function injected(parameters: InjectedParameters = {}) {
359
369
  chainId: numberToHex(chainId),
360
370
  chainName: chain.name,
361
371
  nativeCurrency: chain.nativeCurrency,
362
- rpcUrls: [chain.rpcUrls.public?.http[0] ?? ''],
372
+ rpcUrls: [chain.rpcUrls.default?.http[0] ?? ''],
363
373
  blockExplorerUrls,
364
374
  },
365
375
  ],
@@ -34,6 +34,7 @@ export type MockParameters = {
34
34
  | undefined
35
35
  }
36
36
 
37
+ mock.type = 'mock' as const
37
38
  export function mock(parameters: MockParameters) {
38
39
  const features = parameters.features ?? {}
39
40
 
@@ -46,6 +47,7 @@ export function mock(parameters: MockParameters) {
46
47
  return createConnector<Provider>((config) => ({
47
48
  id: 'mock',
48
49
  name: 'Mock Connector',
50
+ type: mock.type,
49
51
  async setup() {
50
52
  connectedChainId = config.chains[0].id
51
53
  },
@@ -8,7 +8,7 @@ import {
8
8
  type Chain,
9
9
  type Client,
10
10
  type ClientConfig as viem_ClientConfig,
11
- type Transport,
11
+ type Transport as viem_Transport,
12
12
  createClient,
13
13
  } from 'viem'
14
14
  import { persist, subscribeWithSelector } from 'zustand/middleware'
@@ -155,7 +155,11 @@ export function createConfig<
155
155
  ...properties,
156
156
  chain,
157
157
  batch: properties.batch ?? { multicall: true },
158
- transport: rest.transports[chainId],
158
+ transport: (parameters) =>
159
+ rest.transports[chainId]({
160
+ ...parameters,
161
+ connectors,
162
+ }),
159
163
  })
160
164
  }
161
165
 
@@ -443,6 +447,12 @@ export type Connector = ReturnType<CreateConnectorFn> & {
443
447
  uid: string
444
448
  }
445
449
 
450
+ export type Transport = (
451
+ params: Parameters<viem_Transport>[0] & {
452
+ connectors?: StoreApi<Connector[]>
453
+ },
454
+ ) => ReturnType<viem_Transport>
455
+
446
456
  type ClientConfig = LooseOmit<
447
457
  viem_ClientConfig,
448
458
  'account' | 'chain' | 'key' | 'name' | 'transport' | 'type'
@@ -147,6 +147,13 @@ export {
147
147
  getTransaction as fetchTransaction,
148
148
  } from '../actions/getTransaction.js'
149
149
 
150
+ export {
151
+ type GetTransactionCountErrorType,
152
+ type GetTransactionCountParameters,
153
+ type GetTransactionCountReturnType,
154
+ getTransactionCount,
155
+ } from '../actions/getTransactionCount.js'
156
+
150
157
  export {
151
158
  type GetWalletClientErrorType,
152
159
  type GetWalletClientParameters,
@@ -1,14 +1,3 @@
1
- ////////////////////////////////////////////////////////////////////////////////
2
- // Viem
3
-
4
- // extremely commonly used viem exports
5
- export {
6
- custom,
7
- fallback,
8
- http,
9
- webSocket,
10
- } from 'viem'
11
-
12
1
  ////////////////////////////////////////////////////////////////////////////////
13
2
  // Actions
14
3
 
@@ -158,6 +147,13 @@ export {
158
147
  getTransaction as fetchTransaction,
159
148
  } from '../actions/getTransaction.js'
160
149
 
150
+ export {
151
+ type GetTransactionCountErrorType,
152
+ type GetTransactionCountParameters,
153
+ type GetTransactionCountReturnType,
154
+ getTransactionCount,
155
+ } from '../actions/getTransactionCount.js'
156
+
161
157
  export {
162
158
  type GetWalletClientErrorType,
163
159
  type GetWalletClientParameters,
@@ -374,6 +370,13 @@ export {
374
370
  SwitchChainNotSupportedError,
375
371
  } from '../errors/connector.js'
376
372
 
373
+ ////////////////////////////////////////////////////////////////////////////////
374
+ // Transports
375
+
376
+ export { custom, http, webSocket } from 'viem'
377
+ export { unstable_connector } from '../transports/connector.js'
378
+ export { fallback } from '../transports/fallback.js'
379
+
377
380
  ////////////////////////////////////////////////////////////////////////////////
378
381
  // Types
379
382
 
@@ -125,6 +125,15 @@ export {
125
125
  getTransactionQueryOptions,
126
126
  } from '../query/getTransaction.js'
127
127
 
128
+ export {
129
+ type GetTransactionCountData,
130
+ type GetTransactionCountOptions,
131
+ type GetTransactionCountQueryFnData,
132
+ type GetTransactionCountQueryKey,
133
+ getTransactionCountQueryKey,
134
+ getTransactionCountQueryOptions,
135
+ } from '../query/getTransactionCount.js'
136
+
128
137
  export {
129
138
  type GetWalletClientData,
130
139
  type GetWalletClientOptions,
@@ -38,7 +38,7 @@ export function getBlockQueryOptions<
38
38
  async queryFn({ queryKey }) {
39
39
  const { scopeKey: _, ...parameters } = queryKey[1]
40
40
  const block = await getBlock(config, parameters)
41
- return block ?? null
41
+ return (block ?? null) as any
42
42
  },
43
43
  queryKey: getBlockQueryKey(options),
44
44
  } as const satisfies QueryOptions<
@@ -0,0 +1,55 @@
1
+ import { type QueryOptions } from '@tanstack/query-core'
2
+
3
+ import {
4
+ type GetTransactionCountErrorType,
5
+ type GetTransactionCountParameters,
6
+ type GetTransactionCountReturnType,
7
+ getTransactionCount,
8
+ } from '../actions/getTransactionCount.js'
9
+ import { type Config } from '../createConfig.js'
10
+ import type { Evaluate, PartialBy } from '../types/utils.js'
11
+ import type { ScopeKeyParameter } from './types.js'
12
+ import { filterQueryOptions } from './utils.js'
13
+
14
+ export type GetTransactionCountOptions<config extends Config> = Evaluate<
15
+ PartialBy<GetTransactionCountParameters<config>, 'address'> &
16
+ ScopeKeyParameter
17
+ >
18
+
19
+ export function getTransactionCountQueryOptions<config extends Config>(
20
+ config: config,
21
+ options: GetTransactionCountOptions<config> = {},
22
+ ) {
23
+ return {
24
+ async queryFn({ queryKey }) {
25
+ const { address, scopeKey: _, ...parameters } = queryKey[1]
26
+ if (!address) throw new Error('address is required')
27
+ const transactionCount = await getTransactionCount(config, {
28
+ ...(parameters as GetTransactionCountParameters),
29
+ address,
30
+ })
31
+ return transactionCount ?? null
32
+ },
33
+ queryKey: getTransactionCountQueryKey(options),
34
+ } as const satisfies QueryOptions<
35
+ GetTransactionCountQueryFnData,
36
+ GetTransactionCountErrorType,
37
+ GetTransactionCountData,
38
+ GetTransactionCountQueryKey<config>
39
+ >
40
+ }
41
+
42
+ export type GetTransactionCountQueryFnData =
43
+ Evaluate<GetTransactionCountReturnType>
44
+
45
+ export type GetTransactionCountData = GetTransactionCountQueryFnData
46
+
47
+ export function getTransactionCountQueryKey<config extends Config>(
48
+ options: GetTransactionCountOptions<config> = {},
49
+ ) {
50
+ return ['transactionCount', filterQueryOptions(options)] as const
51
+ }
52
+
53
+ export type GetTransactionCountQueryKey<config extends Config> = ReturnType<
54
+ typeof getTransactionCountQueryKey<config>
55
+ >
@@ -0,0 +1,83 @@
1
+ import {
2
+ ChainDisconnectedError,
3
+ type EIP1193Provider,
4
+ ProviderDisconnectedError,
5
+ type TransportConfig,
6
+ createTransport,
7
+ hexToNumber,
8
+ withRetry,
9
+ withTimeout,
10
+ } from 'viem'
11
+ import type { Connector, Transport } from '../createConfig.js'
12
+
13
+ export type ConnectorTransportConfig = {
14
+ /** The key of the transport. */
15
+ key?: TransportConfig['key']
16
+ /** The name of the transport. */
17
+ name?: TransportConfig['name']
18
+ /** The max number of times to retry. */
19
+ retryCount?: TransportConfig['retryCount']
20
+ /** The base delay (in ms) between retries. */
21
+ retryDelay?: TransportConfig['retryDelay']
22
+ }
23
+
24
+ export type ConnectorTransport = Transport
25
+
26
+ export function unstable_connector(
27
+ connector: Pick<Connector, 'type'>,
28
+ config: ConnectorTransportConfig = {},
29
+ ): Transport {
30
+ const { type } = connector
31
+ const {
32
+ key = 'connector',
33
+ name = 'Connector',
34
+ retryCount,
35
+ retryDelay,
36
+ } = config
37
+ return ({ chain, connectors, retryCount: defaultRetryCount }) =>
38
+ createTransport({
39
+ key,
40
+ name,
41
+ async request({ method, params }) {
42
+ const connector = connectors?.getState().find((c) => c.type === type)
43
+ if (!connector)
44
+ throw new ProviderDisconnectedError(
45
+ new Error(
46
+ `Could not find connector of type "${type}" in \`connectors\` passed to \`createConfig\`.`,
47
+ ),
48
+ )
49
+
50
+ const provider = (await connector.getProvider({
51
+ chainId: chain?.id,
52
+ })) as EIP1193Provider | undefined
53
+ if (!provider)
54
+ throw new ProviderDisconnectedError(
55
+ new Error('Provider is disconnected.'),
56
+ )
57
+
58
+ // We are applying a retry & timeout strategy here as some injected wallets (ie. MetaMask) fail to
59
+ // immediately resolve a JSON-RPC request on page load.
60
+ const chainId = hexToNumber(
61
+ await withRetry(() =>
62
+ withTimeout(() => provider.request({ method: 'eth_chainId' }), {
63
+ timeout: 100,
64
+ }),
65
+ ),
66
+ )
67
+ if (chain && chainId !== chain.id)
68
+ throw new ChainDisconnectedError(
69
+ new Error(
70
+ `The current chain of the connector (id: ${chainId}) does not match the target chain for the request (id: ${chain.id} – ${chain.name}).`,
71
+ ),
72
+ )
73
+
74
+ return (await provider.request({
75
+ method,
76
+ params,
77
+ } as any)) as any
78
+ },
79
+ retryDelay,
80
+ retryCount: retryCount ?? defaultRetryCount,
81
+ type: 'connector',
82
+ })
83
+ }
@@ -0,0 +1,9 @@
1
+ import { fallback as viem_fallback } from 'viem'
2
+ import type { Transport } from '../createConfig.js'
3
+
4
+ export function fallback(
5
+ transports: Transport[],
6
+ config?: Parameters<typeof viem_fallback>[1],
7
+ ) {
8
+ return viem_fallback(transports, config)
9
+ }
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '2.0.0-beta.5'
1
+ export const version = '2.0.0-beta.6'