tempo.ts 0.10.2 → 0.10.4
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/CHANGELOG.md +12 -0
- package/dist/chains.d.ts +3 -0
- package/dist/chains.d.ts.map +1 -1
- package/dist/chains.js +5 -1
- package/dist/chains.js.map +1 -1
- package/dist/viem/Actions/index.d.ts +1 -0
- package/dist/viem/Actions/index.d.ts.map +1 -1
- package/dist/viem/Actions/index.js +1 -0
- package/dist/viem/Actions/index.js.map +1 -1
- package/dist/viem/Actions/nonce.d.ts +257 -0
- package/dist/viem/Actions/nonce.d.ts.map +1 -0
- package/dist/viem/Actions/nonce.js +226 -0
- package/dist/viem/Actions/nonce.js.map +1 -0
- package/dist/viem/Addresses.d.ts +1 -0
- package/dist/viem/Addresses.d.ts.map +1 -1
- package/dist/viem/Addresses.js +1 -0
- package/dist/viem/Addresses.js.map +1 -1
- package/dist/viem/Chain.d.ts +0 -6
- package/dist/viem/Chain.d.ts.map +1 -1
- package/dist/viem/Chain.js +0 -6
- package/dist/viem/Chain.js.map +1 -1
- package/dist/wagmi/Actions/index.d.ts +1 -0
- package/dist/wagmi/Actions/index.d.ts.map +1 -1
- package/dist/wagmi/Actions/index.js +1 -0
- package/dist/wagmi/Actions/index.js.map +1 -1
- package/dist/wagmi/Actions/nonce.d.ts +147 -0
- package/dist/wagmi/Actions/nonce.d.ts.map +1 -0
- package/dist/wagmi/Actions/nonce.js +169 -0
- package/dist/wagmi/Actions/nonce.js.map +1 -0
- package/dist/wagmi/Hooks/index.d.ts +1 -0
- package/dist/wagmi/Hooks/index.d.ts.map +1 -1
- package/dist/wagmi/Hooks/index.js +1 -0
- package/dist/wagmi/Hooks/index.js.map +1 -1
- package/dist/wagmi/Hooks/nonce.d.ts +110 -0
- package/dist/wagmi/Hooks/nonce.d.ts.map +1 -0
- package/dist/wagmi/Hooks/nonce.js +144 -0
- package/dist/wagmi/Hooks/nonce.js.map +1 -0
- package/package.json +1 -1
- package/src/chains.ts +5 -1
- package/src/viem/Actions/index.ts +1 -0
- package/src/viem/Actions/nonce.test.ts +193 -0
- package/src/viem/Actions/nonce.ts +345 -0
- package/src/viem/Addresses.ts +1 -0
- package/src/viem/Chain.test.ts +9 -9
- package/src/viem/Chain.ts +0 -6
- package/src/wagmi/Actions/index.ts +1 -0
- package/src/wagmi/Actions/nonce.test.ts +141 -0
- package/src/wagmi/Actions/nonce.ts +271 -0
- package/src/wagmi/Hooks/index.ts +1 -0
- package/src/wagmi/Hooks/nonce.test.ts +207 -0
- package/src/wagmi/Hooks/nonce.ts +230 -0
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import type * as Query from '@tanstack/query-core'
|
|
2
|
+
import type { Config } from '@wagmi/core'
|
|
3
|
+
import type { ChainIdParameter } from '@wagmi/core/internal'
|
|
4
|
+
import type { PartialBy, RequiredBy } from '../../internal/types.js'
|
|
5
|
+
import * as viem_Actions from '../../viem/Actions/nonce.js'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Gets the nonce for an account and nonce key.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { createConfig, http } from '@wagmi/core'
|
|
13
|
+
* import { tempo } from 'tempo.ts/chains'
|
|
14
|
+
* import { Actions } from 'tempo.ts/wagmi'
|
|
15
|
+
*
|
|
16
|
+
* const config = createConfig({
|
|
17
|
+
* chains: [tempo({ feeToken: '0x20c0000000000000000000000000000000000001' })],
|
|
18
|
+
* transports: {
|
|
19
|
+
* [tempo.id]: http(),
|
|
20
|
+
* },
|
|
21
|
+
* })
|
|
22
|
+
*
|
|
23
|
+
* const nonce = await Actions.nonce.getNonce(config, {
|
|
24
|
+
* account: '0x...',
|
|
25
|
+
* nonceKey: 1n,
|
|
26
|
+
* })
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @param config - Config.
|
|
30
|
+
* @param parameters - Parameters.
|
|
31
|
+
* @returns The nonce value.
|
|
32
|
+
*/
|
|
33
|
+
export function getNonce<config extends Config>(
|
|
34
|
+
config: config,
|
|
35
|
+
parameters: getNonce.Parameters<config>,
|
|
36
|
+
): Promise<getNonce.ReturnValue> {
|
|
37
|
+
const { chainId, ...rest } = parameters
|
|
38
|
+
const client = config.getClient({ chainId })
|
|
39
|
+
return viem_Actions.getNonce(client, rest)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export namespace getNonce {
|
|
43
|
+
export type Parameters<config extends Config> = ChainIdParameter<config> &
|
|
44
|
+
viem_Actions.getNonce.Parameters
|
|
45
|
+
|
|
46
|
+
export type ReturnValue = viem_Actions.getNonce.ReturnValue
|
|
47
|
+
|
|
48
|
+
export function queryKey<config extends Config>(
|
|
49
|
+
parameters: PartialBy<Parameters<config>, 'account' | 'nonceKey'>,
|
|
50
|
+
) {
|
|
51
|
+
return ['getNonce', parameters] as const
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export type QueryKey<config extends Config> = ReturnType<
|
|
55
|
+
typeof queryKey<config>
|
|
56
|
+
>
|
|
57
|
+
|
|
58
|
+
export function queryOptions<config extends Config, selectData = ReturnValue>(
|
|
59
|
+
config: Config,
|
|
60
|
+
parameters: queryOptions.Parameters<config, selectData>,
|
|
61
|
+
): queryOptions.ReturnValue<config, selectData> {
|
|
62
|
+
const { query, ...rest } = parameters
|
|
63
|
+
return {
|
|
64
|
+
...query,
|
|
65
|
+
queryKey: queryKey(rest),
|
|
66
|
+
async queryFn({ queryKey }) {
|
|
67
|
+
const [, { account, nonceKey, ...parameters }] = queryKey
|
|
68
|
+
if (!account) throw new Error('account is required.')
|
|
69
|
+
if (nonceKey === undefined) throw new Error('nonceKey is required.')
|
|
70
|
+
return await getNonce(config, { account, nonceKey, ...parameters })
|
|
71
|
+
},
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export declare namespace queryOptions {
|
|
76
|
+
export type Parameters<
|
|
77
|
+
config extends Config,
|
|
78
|
+
selectData = getNonce.ReturnValue,
|
|
79
|
+
> = PartialBy<getNonce.Parameters<config>, 'account' | 'nonceKey'> & {
|
|
80
|
+
query?:
|
|
81
|
+
| Omit<ReturnValue<config, selectData>, 'queryKey' | 'queryFn'>
|
|
82
|
+
| undefined
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export type ReturnValue<
|
|
86
|
+
config extends Config,
|
|
87
|
+
selectData = getNonce.ReturnValue,
|
|
88
|
+
> = RequiredBy<
|
|
89
|
+
Query.QueryOptions<
|
|
90
|
+
getNonce.ReturnValue,
|
|
91
|
+
Query.DefaultError,
|
|
92
|
+
selectData,
|
|
93
|
+
getNonce.QueryKey<config>
|
|
94
|
+
>,
|
|
95
|
+
'queryKey' | 'queryFn'
|
|
96
|
+
>
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Gets the number of active nonce keys for an account.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```ts
|
|
105
|
+
* import { createConfig, http } from '@wagmi/core'
|
|
106
|
+
* import { tempo } from 'tempo.ts/chains'
|
|
107
|
+
* import { Actions } from 'tempo.ts/wagmi'
|
|
108
|
+
*
|
|
109
|
+
* const config = createConfig({
|
|
110
|
+
* chains: [tempo({ feeToken: '0x20c0000000000000000000000000000000000001' })],
|
|
111
|
+
* transports: {
|
|
112
|
+
* [tempo.id]: http(),
|
|
113
|
+
* },
|
|
114
|
+
* })
|
|
115
|
+
*
|
|
116
|
+
* const count = await Actions.nonce.getNonceKeyCount(config, {
|
|
117
|
+
* account: '0x...',
|
|
118
|
+
* })
|
|
119
|
+
* ```
|
|
120
|
+
*
|
|
121
|
+
* @param config - Config.
|
|
122
|
+
* @param parameters - Parameters.
|
|
123
|
+
* @returns The number of active nonce keys.
|
|
124
|
+
*/
|
|
125
|
+
export function getNonceKeyCount<config extends Config>(
|
|
126
|
+
config: config,
|
|
127
|
+
parameters: getNonceKeyCount.Parameters<config>,
|
|
128
|
+
): Promise<getNonceKeyCount.ReturnValue> {
|
|
129
|
+
const { chainId, ...rest } = parameters
|
|
130
|
+
const client = config.getClient({ chainId })
|
|
131
|
+
return viem_Actions.getNonceKeyCount(client, rest)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export namespace getNonceKeyCount {
|
|
135
|
+
export type Parameters<config extends Config> = ChainIdParameter<config> &
|
|
136
|
+
viem_Actions.getNonceKeyCount.Parameters
|
|
137
|
+
|
|
138
|
+
export type ReturnValue = viem_Actions.getNonceKeyCount.ReturnValue
|
|
139
|
+
|
|
140
|
+
export function queryKey<config extends Config>(
|
|
141
|
+
parameters: PartialBy<Parameters<config>, 'account'>,
|
|
142
|
+
) {
|
|
143
|
+
return ['getNonceKeyCount', parameters] as const
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export type QueryKey<config extends Config> = ReturnType<
|
|
147
|
+
typeof queryKey<config>
|
|
148
|
+
>
|
|
149
|
+
|
|
150
|
+
export function queryOptions<config extends Config, selectData = ReturnValue>(
|
|
151
|
+
config: Config,
|
|
152
|
+
parameters: queryOptions.Parameters<config, selectData>,
|
|
153
|
+
): queryOptions.ReturnValue<config, selectData> {
|
|
154
|
+
const { query, ...rest } = parameters
|
|
155
|
+
return {
|
|
156
|
+
...query,
|
|
157
|
+
queryKey: queryKey(rest),
|
|
158
|
+
async queryFn({ queryKey }) {
|
|
159
|
+
const [, { account, ...parameters }] = queryKey
|
|
160
|
+
if (!account) throw new Error('account is required.')
|
|
161
|
+
return await getNonceKeyCount(config, { account, ...parameters })
|
|
162
|
+
},
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export declare namespace queryOptions {
|
|
167
|
+
export type Parameters<
|
|
168
|
+
config extends Config,
|
|
169
|
+
selectData = getNonceKeyCount.ReturnValue,
|
|
170
|
+
> = PartialBy<getNonceKeyCount.Parameters<config>, 'account'> & {
|
|
171
|
+
query?:
|
|
172
|
+
| Omit<ReturnValue<config, selectData>, 'queryKey' | 'queryFn'>
|
|
173
|
+
| undefined
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export type ReturnValue<
|
|
177
|
+
config extends Config,
|
|
178
|
+
selectData = getNonceKeyCount.ReturnValue,
|
|
179
|
+
> = RequiredBy<
|
|
180
|
+
Query.QueryOptions<
|
|
181
|
+
getNonceKeyCount.ReturnValue,
|
|
182
|
+
Query.DefaultError,
|
|
183
|
+
selectData,
|
|
184
|
+
getNonceKeyCount.QueryKey<config>
|
|
185
|
+
>,
|
|
186
|
+
'queryKey' | 'queryFn'
|
|
187
|
+
>
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Watches for nonce incremented events.
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* ```ts
|
|
196
|
+
* import { createConfig, http } from '@wagmi/core'
|
|
197
|
+
* import { tempo } from 'tempo.ts/chains'
|
|
198
|
+
* import { Actions } from 'tempo.ts/wagmi'
|
|
199
|
+
*
|
|
200
|
+
* const config = createConfig({
|
|
201
|
+
* chains: [tempo({ feeToken: '0x20c0000000000000000000000000000000000001' })],
|
|
202
|
+
* transports: {
|
|
203
|
+
* [tempo.id]: http(),
|
|
204
|
+
* },
|
|
205
|
+
* })
|
|
206
|
+
*
|
|
207
|
+
* const unwatch = Actions.nonce.watchNonceIncremented(config, {
|
|
208
|
+
* onNonceIncremented: (args, log) => {
|
|
209
|
+
* console.log('Nonce incremented:', args)
|
|
210
|
+
* },
|
|
211
|
+
* })
|
|
212
|
+
* ```
|
|
213
|
+
*
|
|
214
|
+
* @param config - Config.
|
|
215
|
+
* @param parameters - Parameters.
|
|
216
|
+
* @returns A function to unsubscribe from the event.
|
|
217
|
+
*/
|
|
218
|
+
export function watchNonceIncremented<config extends Config>(
|
|
219
|
+
config: config,
|
|
220
|
+
parameters: watchNonceIncremented.Parameters<config>,
|
|
221
|
+
): () => void {
|
|
222
|
+
const { chainId, ...rest } = parameters
|
|
223
|
+
const client = config.getClient({ chainId })
|
|
224
|
+
return viem_Actions.watchNonceIncremented(client, rest)
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export declare namespace watchNonceIncremented {
|
|
228
|
+
export type Parameters<config extends Config> = ChainIdParameter<config> &
|
|
229
|
+
viem_Actions.watchNonceIncremented.Parameters
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Watches for active key count changed events.
|
|
234
|
+
*
|
|
235
|
+
* @example
|
|
236
|
+
* ```ts
|
|
237
|
+
* import { createConfig, http } from '@wagmi/core'
|
|
238
|
+
* import { tempo } from 'tempo.ts/chains'
|
|
239
|
+
* import { Actions } from 'tempo.ts/wagmi'
|
|
240
|
+
*
|
|
241
|
+
* const config = createConfig({
|
|
242
|
+
* chains: [tempo({ feeToken: '0x20c0000000000000000000000000000000000001' })],
|
|
243
|
+
* transports: {
|
|
244
|
+
* [tempo.id]: http(),
|
|
245
|
+
* },
|
|
246
|
+
* })
|
|
247
|
+
*
|
|
248
|
+
* const unwatch = Actions.nonce.watchActiveKeyCountChanged(config, {
|
|
249
|
+
* onActiveKeyCountChanged: (args, log) => {
|
|
250
|
+
* console.log('Active key count changed:', args)
|
|
251
|
+
* },
|
|
252
|
+
* })
|
|
253
|
+
* ```
|
|
254
|
+
*
|
|
255
|
+
* @param config - Config.
|
|
256
|
+
* @param parameters - Parameters.
|
|
257
|
+
* @returns A function to unsubscribe from the event.
|
|
258
|
+
*/
|
|
259
|
+
export function watchActiveKeyCountChanged<config extends Config>(
|
|
260
|
+
config: config,
|
|
261
|
+
parameters: watchActiveKeyCountChanged.Parameters<config>,
|
|
262
|
+
): () => void {
|
|
263
|
+
const { chainId, ...rest } = parameters
|
|
264
|
+
const client = config.getClient({ chainId })
|
|
265
|
+
return viem_Actions.watchActiveKeyCountChanged(client, rest)
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
export declare namespace watchActiveKeyCountChanged {
|
|
269
|
+
export type Parameters<config extends Config> = ChainIdParameter<config> &
|
|
270
|
+
viem_Actions.watchActiveKeyCountChanged.Parameters
|
|
271
|
+
}
|
package/src/wagmi/Hooks/index.ts
CHANGED
|
@@ -2,6 +2,7 @@ export * as amm from './amm.js'
|
|
|
2
2
|
export * as dex from './dex.js'
|
|
3
3
|
export * as faucet from './faucet.js'
|
|
4
4
|
export * as fee from './fee.js'
|
|
5
|
+
export * as nonce from './nonce.js'
|
|
5
6
|
export * as policy from './policy.js'
|
|
6
7
|
export * as reward from './reward.js'
|
|
7
8
|
export * as token from './token.js'
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import type { Address } from 'viem'
|
|
2
|
+
import { describe, expect, test, vi } from 'vitest'
|
|
3
|
+
import { useConnect } from 'wagmi'
|
|
4
|
+
import { accounts } from '../../../test/viem/config.js'
|
|
5
|
+
import { config, renderHook } from '../../../test/wagmi/config.js'
|
|
6
|
+
import * as hooks from './nonce.js'
|
|
7
|
+
import * as tokenHooks from './token.js'
|
|
8
|
+
|
|
9
|
+
const { useNonceKeyCount, useNonce } = hooks
|
|
10
|
+
|
|
11
|
+
const account = accounts[0]
|
|
12
|
+
const account2 = accounts[1]
|
|
13
|
+
|
|
14
|
+
describe('useNonce', () => {
|
|
15
|
+
test('default', async () => {
|
|
16
|
+
let testAccount: Address | undefined
|
|
17
|
+
let testNonceKey: bigint | undefined
|
|
18
|
+
|
|
19
|
+
const { result, rerender } = await renderHook(() =>
|
|
20
|
+
useNonce({ account: testAccount, nonceKey: testNonceKey }),
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
await vi.waitFor(() => result.current.fetchStatus === 'fetching')
|
|
24
|
+
|
|
25
|
+
// Should be disabled when account is undefined
|
|
26
|
+
expect(result.current.data).toBeUndefined()
|
|
27
|
+
expect(result.current.isPending).toBe(true)
|
|
28
|
+
expect(result.current.isEnabled).toBe(false)
|
|
29
|
+
|
|
30
|
+
// Set account and nonceKey
|
|
31
|
+
testAccount = account.address
|
|
32
|
+
testNonceKey = 1n
|
|
33
|
+
rerender()
|
|
34
|
+
|
|
35
|
+
await vi.waitFor(() => expect(result.current.isSuccess).toBeTruthy(), {
|
|
36
|
+
timeout: 5_000,
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
// Should now be enabled and have data
|
|
40
|
+
expect(result.current.isEnabled).toBe(true)
|
|
41
|
+
expect(result.current.data).toBe(1n)
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
test('reactivity: account parameter', async () => {
|
|
45
|
+
let testAccount: Address | undefined
|
|
46
|
+
|
|
47
|
+
const { result, rerender } = await renderHook(() =>
|
|
48
|
+
useNonce({ account: testAccount, nonceKey: 1n }),
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
await vi.waitFor(() => result.current.fetchStatus === 'fetching')
|
|
52
|
+
|
|
53
|
+
// Should be disabled when account is undefined
|
|
54
|
+
expect(result.current.isEnabled).toBe(false)
|
|
55
|
+
|
|
56
|
+
// Set account
|
|
57
|
+
testAccount = account.address
|
|
58
|
+
rerender()
|
|
59
|
+
|
|
60
|
+
await vi.waitFor(() => expect(result.current.isSuccess).toBeTruthy(), {
|
|
61
|
+
timeout: 5_000,
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
expect(result.current.isEnabled).toBe(true)
|
|
65
|
+
expect(result.current.data).toBe(1n)
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
test('reactivity: nonceKey parameter', async () => {
|
|
69
|
+
let testNonceKey: bigint | undefined
|
|
70
|
+
|
|
71
|
+
const { result, rerender } = await renderHook(() =>
|
|
72
|
+
useNonce({ account: account.address, nonceKey: testNonceKey }),
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
await vi.waitFor(() => result.current.fetchStatus === 'fetching')
|
|
76
|
+
|
|
77
|
+
// Should be disabled when nonceKey is undefined
|
|
78
|
+
expect(result.current.isEnabled).toBe(false)
|
|
79
|
+
|
|
80
|
+
// Set nonceKey
|
|
81
|
+
testNonceKey = 1n
|
|
82
|
+
rerender()
|
|
83
|
+
|
|
84
|
+
await vi.waitFor(() => expect(result.current.isSuccess).toBeTruthy(), {
|
|
85
|
+
timeout: 5_000,
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
expect(result.current.isEnabled).toBe(true)
|
|
89
|
+
expect(result.current.data).toBe(1n)
|
|
90
|
+
})
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
describe('useNonceKeyCount', () => {
|
|
94
|
+
test('default', async () => {
|
|
95
|
+
let testAccount: Address | undefined
|
|
96
|
+
|
|
97
|
+
const { result, rerender } = await renderHook(() =>
|
|
98
|
+
useNonceKeyCount({ account: testAccount }),
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
await vi.waitFor(() => result.current.fetchStatus === 'fetching')
|
|
102
|
+
|
|
103
|
+
// Should be disabled when account is undefined
|
|
104
|
+
expect(result.current.data).toBeUndefined()
|
|
105
|
+
expect(result.current.isPending).toBe(true)
|
|
106
|
+
expect(result.current.isEnabled).toBe(false)
|
|
107
|
+
|
|
108
|
+
// Set account
|
|
109
|
+
testAccount = account.address
|
|
110
|
+
rerender()
|
|
111
|
+
|
|
112
|
+
await vi.waitFor(() => expect(result.current.isSuccess).toBeTruthy(), {
|
|
113
|
+
timeout: 5_000,
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
// Should now be enabled and have data
|
|
117
|
+
expect(result.current.isEnabled).toBe(true)
|
|
118
|
+
expect(result.current.data).toBe(0n)
|
|
119
|
+
})
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
describe('useWatchNonceIncremented', () => {
|
|
123
|
+
test('default', async () => {
|
|
124
|
+
const { result: connectResult } = await renderHook(() => ({
|
|
125
|
+
connect: useConnect(),
|
|
126
|
+
transferSync: tokenHooks.useTransferSync(),
|
|
127
|
+
}))
|
|
128
|
+
|
|
129
|
+
await connectResult.current.connect.connectAsync({
|
|
130
|
+
connector: config.connectors[0]!,
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
const events: any[] = []
|
|
134
|
+
await renderHook(() =>
|
|
135
|
+
hooks.useWatchNonceIncremented({
|
|
136
|
+
onNonceIncremented(args) {
|
|
137
|
+
events.push(args)
|
|
138
|
+
},
|
|
139
|
+
args: {
|
|
140
|
+
account: account.address,
|
|
141
|
+
nonceKey: 5n,
|
|
142
|
+
},
|
|
143
|
+
}),
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
// Have to manually set nonce because eth_FillTransaction does not support nonce keys
|
|
147
|
+
await connectResult.current.transferSync.mutateAsync({
|
|
148
|
+
to: account2.address,
|
|
149
|
+
amount: 1n,
|
|
150
|
+
token: 1n,
|
|
151
|
+
nonceKey: 5n,
|
|
152
|
+
nonce: 0,
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
await connectResult.current.transferSync.mutateAsync({
|
|
156
|
+
to: account2.address,
|
|
157
|
+
amount: 1n,
|
|
158
|
+
token: 1n,
|
|
159
|
+
nonceKey: 5n,
|
|
160
|
+
nonce: 1,
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
await vi.waitUntil(() => events.length >= 2)
|
|
164
|
+
|
|
165
|
+
expect(events).toHaveLength(2)
|
|
166
|
+
expect(events[0]?.account).toBe(account.address)
|
|
167
|
+
expect(events[0]?.nonceKey).toBe(5n)
|
|
168
|
+
expect(events[0]?.newNonce).toBe(1n)
|
|
169
|
+
expect(events[1]?.newNonce).toBe(2n)
|
|
170
|
+
})
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
describe('useWatchActiveKeyCountChanged', () => {
|
|
174
|
+
test('default', async () => {
|
|
175
|
+
const { result: connectResult } = await renderHook(() => ({
|
|
176
|
+
connect: useConnect(),
|
|
177
|
+
transferSync: tokenHooks.useTransferSync(),
|
|
178
|
+
}))
|
|
179
|
+
|
|
180
|
+
await connectResult.current.connect.connectAsync({
|
|
181
|
+
connector: config.connectors[0]!,
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
const events: any[] = []
|
|
185
|
+
await renderHook(() =>
|
|
186
|
+
hooks.useWatchActiveKeyCountChanged({
|
|
187
|
+
onActiveKeyCountChanged(args) {
|
|
188
|
+
events.push(args)
|
|
189
|
+
},
|
|
190
|
+
}),
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
await connectResult.current.transferSync.mutateAsync({
|
|
194
|
+
to: account2.address,
|
|
195
|
+
amount: 1n,
|
|
196
|
+
token: 1n,
|
|
197
|
+
nonceKey: 10n,
|
|
198
|
+
nonce: 0,
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
await vi.waitUntil(() => events.length >= 1)
|
|
202
|
+
|
|
203
|
+
expect(events).toHaveLength(1)
|
|
204
|
+
expect(events[0]?.account).toBe(account.address)
|
|
205
|
+
expect(events[0]?.newCount).toBe(2n)
|
|
206
|
+
})
|
|
207
|
+
})
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import type { DefaultError } from '@tanstack/query-core'
|
|
2
|
+
import type { Config, ResolvedRegister } from '@wagmi/core'
|
|
3
|
+
import { useEffect } from 'react'
|
|
4
|
+
import { useChainId, useConfig } from 'wagmi'
|
|
5
|
+
import type { ConfigParameter, QueryParameter } from 'wagmi/internal'
|
|
6
|
+
import { type UseQueryReturnType, useQuery } from 'wagmi/query'
|
|
7
|
+
|
|
8
|
+
import type { ExactPartial, UnionCompute } from '../../internal/types.js'
|
|
9
|
+
import {
|
|
10
|
+
getNonce,
|
|
11
|
+
getNonceKeyCount,
|
|
12
|
+
watchActiveKeyCountChanged,
|
|
13
|
+
watchNonceIncremented,
|
|
14
|
+
} from '../Actions/nonce.js'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Hook for getting the nonce for an account and nonce key.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```tsx
|
|
21
|
+
* import { Hooks } from 'tempo.ts/wagmi'
|
|
22
|
+
*
|
|
23
|
+
* function App() {
|
|
24
|
+
* const { data, isLoading } = Hooks.nonce.useNonce({
|
|
25
|
+
* account: '0x...',
|
|
26
|
+
* nonceKey: 1n,
|
|
27
|
+
* })
|
|
28
|
+
*
|
|
29
|
+
* if (isLoading) return <div>Loading...</div>
|
|
30
|
+
* return <div>Nonce: {data?.toString()}</div>
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @param parameters - Parameters.
|
|
35
|
+
* @returns Query result with nonce value.
|
|
36
|
+
*/
|
|
37
|
+
export function useNonce<
|
|
38
|
+
config extends Config = ResolvedRegister['config'],
|
|
39
|
+
selectData = getNonce.ReturnValue,
|
|
40
|
+
>(parameters: useNonce.Parameters<config, selectData> = {}) {
|
|
41
|
+
const { account, nonceKey, query = {} } = parameters
|
|
42
|
+
|
|
43
|
+
const config = useConfig(parameters)
|
|
44
|
+
const chainId = useChainId({ config })
|
|
45
|
+
|
|
46
|
+
const options = getNonce.queryOptions(config, {
|
|
47
|
+
...parameters,
|
|
48
|
+
chainId: parameters.chainId ?? chainId,
|
|
49
|
+
query: undefined,
|
|
50
|
+
})
|
|
51
|
+
const enabled = Boolean(
|
|
52
|
+
account && nonceKey !== undefined && (query.enabled ?? true),
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
return useQuery({ ...query, ...options, enabled })
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export declare namespace useNonce {
|
|
59
|
+
export type Parameters<
|
|
60
|
+
config extends Config = ResolvedRegister['config'],
|
|
61
|
+
selectData = getNonce.ReturnValue,
|
|
62
|
+
> = ConfigParameter<config> &
|
|
63
|
+
QueryParameter<
|
|
64
|
+
getNonce.ReturnValue,
|
|
65
|
+
DefaultError,
|
|
66
|
+
selectData,
|
|
67
|
+
getNonce.QueryKey<config>
|
|
68
|
+
> &
|
|
69
|
+
ExactPartial<
|
|
70
|
+
Omit<getNonce.queryOptions.Parameters<config, selectData>, 'query'>
|
|
71
|
+
>
|
|
72
|
+
|
|
73
|
+
export type ReturnValue<selectData = getNonce.ReturnValue> =
|
|
74
|
+
UseQueryReturnType<selectData, Error>
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Hook for getting the number of active nonce keys for an account.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```tsx
|
|
82
|
+
* import { Hooks } from 'tempo.ts/wagmi'
|
|
83
|
+
*
|
|
84
|
+
* function App() {
|
|
85
|
+
* const { data, isLoading } = Hooks.nonce.useNonceKeyCount({
|
|
86
|
+
* account: '0x...',
|
|
87
|
+
* })
|
|
88
|
+
*
|
|
89
|
+
* if (isLoading) return <div>Loading...</div>
|
|
90
|
+
* return <div>Active keys: {data?.toString()}</div>
|
|
91
|
+
* }
|
|
92
|
+
* ```
|
|
93
|
+
*
|
|
94
|
+
* @param parameters - Parameters.
|
|
95
|
+
* @returns Query result with active nonce key count.
|
|
96
|
+
*/
|
|
97
|
+
export function useNonceKeyCount<
|
|
98
|
+
config extends Config = ResolvedRegister['config'],
|
|
99
|
+
selectData = getNonceKeyCount.ReturnValue,
|
|
100
|
+
>(parameters: useNonceKeyCount.Parameters<config, selectData> = {}) {
|
|
101
|
+
const { account, query = {} } = parameters
|
|
102
|
+
|
|
103
|
+
const config = useConfig(parameters)
|
|
104
|
+
const chainId = useChainId({ config })
|
|
105
|
+
|
|
106
|
+
const options = getNonceKeyCount.queryOptions(config, {
|
|
107
|
+
...parameters,
|
|
108
|
+
chainId: parameters.chainId ?? chainId,
|
|
109
|
+
query: undefined,
|
|
110
|
+
})
|
|
111
|
+
const enabled = Boolean(account && (query.enabled ?? true))
|
|
112
|
+
|
|
113
|
+
return useQuery({ ...query, ...options, enabled })
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export declare namespace useNonceKeyCount {
|
|
117
|
+
export type Parameters<
|
|
118
|
+
config extends Config = ResolvedRegister['config'],
|
|
119
|
+
selectData = getNonceKeyCount.ReturnValue,
|
|
120
|
+
> = ConfigParameter<config> &
|
|
121
|
+
QueryParameter<
|
|
122
|
+
getNonceKeyCount.ReturnValue,
|
|
123
|
+
DefaultError,
|
|
124
|
+
selectData,
|
|
125
|
+
getNonceKeyCount.QueryKey<config>
|
|
126
|
+
> &
|
|
127
|
+
ExactPartial<
|
|
128
|
+
Omit<
|
|
129
|
+
getNonceKeyCount.queryOptions.Parameters<config, selectData>,
|
|
130
|
+
'query'
|
|
131
|
+
>
|
|
132
|
+
>
|
|
133
|
+
|
|
134
|
+
export type ReturnValue<selectData = getNonceKeyCount.ReturnValue> =
|
|
135
|
+
UseQueryReturnType<selectData, Error>
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Hook for watching nonce incremented events.
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```tsx
|
|
143
|
+
* import { Hooks } from 'tempo.ts/wagmi'
|
|
144
|
+
*
|
|
145
|
+
* function App() {
|
|
146
|
+
* Hooks.nonce.useWatchNonceIncremented({
|
|
147
|
+
* onNonceIncremented(args, log) {
|
|
148
|
+
* console.log('Nonce incremented:', args)
|
|
149
|
+
* },
|
|
150
|
+
* })
|
|
151
|
+
*
|
|
152
|
+
* return <div>Watching for nonce increments...</div>
|
|
153
|
+
* }
|
|
154
|
+
* ```
|
|
155
|
+
*
|
|
156
|
+
* @param parameters - Parameters.
|
|
157
|
+
*/
|
|
158
|
+
export function useWatchNonceIncremented<
|
|
159
|
+
config extends Config = ResolvedRegister['config'],
|
|
160
|
+
>(parameters: useWatchNonceIncremented.Parameters<config> = {}) {
|
|
161
|
+
const { enabled = true, onNonceIncremented, ...rest } = parameters
|
|
162
|
+
|
|
163
|
+
const config = useConfig({ config: parameters.config })
|
|
164
|
+
const configChainId = useChainId({ config })
|
|
165
|
+
const chainId = parameters.chainId ?? configChainId
|
|
166
|
+
|
|
167
|
+
useEffect(() => {
|
|
168
|
+
if (!enabled) return
|
|
169
|
+
if (!onNonceIncremented) return
|
|
170
|
+
return watchNonceIncremented(config, {
|
|
171
|
+
...rest,
|
|
172
|
+
chainId,
|
|
173
|
+
onNonceIncremented,
|
|
174
|
+
})
|
|
175
|
+
}, [config, enabled, onNonceIncremented, chainId, rest])
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export declare namespace useWatchNonceIncremented {
|
|
179
|
+
type Parameters<config extends Config = Config> = UnionCompute<
|
|
180
|
+
ExactPartial<watchNonceIncremented.Parameters<config>> &
|
|
181
|
+
ConfigParameter<config> & { enabled?: boolean | undefined }
|
|
182
|
+
>
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Hook for watching active key count changed events.
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* ```tsx
|
|
190
|
+
* import { Hooks } from 'tempo.ts/wagmi'
|
|
191
|
+
*
|
|
192
|
+
* function App() {
|
|
193
|
+
* Hooks.nonce.useWatchActiveKeyCountChanged({
|
|
194
|
+
* onActiveKeyCountChanged(args, log) {
|
|
195
|
+
* console.log('Active key count changed:', args)
|
|
196
|
+
* },
|
|
197
|
+
* })
|
|
198
|
+
*
|
|
199
|
+
* return <div>Watching for active key count changes...</div>
|
|
200
|
+
* }
|
|
201
|
+
* ```
|
|
202
|
+
*
|
|
203
|
+
* @param parameters - Parameters.
|
|
204
|
+
*/
|
|
205
|
+
export function useWatchActiveKeyCountChanged<
|
|
206
|
+
config extends Config = ResolvedRegister['config'],
|
|
207
|
+
>(parameters: useWatchActiveKeyCountChanged.Parameters<config> = {}) {
|
|
208
|
+
const { enabled = true, onActiveKeyCountChanged, ...rest } = parameters
|
|
209
|
+
|
|
210
|
+
const config = useConfig({ config: parameters.config })
|
|
211
|
+
const configChainId = useChainId({ config })
|
|
212
|
+
const chainId = parameters.chainId ?? configChainId
|
|
213
|
+
|
|
214
|
+
useEffect(() => {
|
|
215
|
+
if (!enabled) return
|
|
216
|
+
if (!onActiveKeyCountChanged) return
|
|
217
|
+
return watchActiveKeyCountChanged(config, {
|
|
218
|
+
...rest,
|
|
219
|
+
chainId,
|
|
220
|
+
onActiveKeyCountChanged,
|
|
221
|
+
})
|
|
222
|
+
}, [config, enabled, onActiveKeyCountChanged, chainId, rest])
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export declare namespace useWatchActiveKeyCountChanged {
|
|
226
|
+
type Parameters<config extends Config = Config> = UnionCompute<
|
|
227
|
+
ExactPartial<watchActiveKeyCountChanged.Parameters<config>> &
|
|
228
|
+
ConfigParameter<config> & { enabled?: boolean | undefined }
|
|
229
|
+
>
|
|
230
|
+
}
|