viem 2.46.1 → 2.46.3

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 (135) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/_cjs/actions/index.js +5 -3
  3. package/_cjs/actions/index.js.map +1 -1
  4. package/_cjs/actions/public/getDelegation.js +21 -0
  5. package/_cjs/actions/public/getDelegation.js.map +1 -0
  6. package/_cjs/actions/wallet/sendRawTransactionSync.js +1 -2
  7. package/_cjs/actions/wallet/sendRawTransactionSync.js.map +1 -1
  8. package/_cjs/chains/definitions/krown.js +26 -0
  9. package/_cjs/chains/definitions/krown.js.map +1 -0
  10. package/_cjs/chains/definitions/polygon.js +1 -1
  11. package/_cjs/chains/definitions/polygon.js.map +1 -1
  12. package/_cjs/chains/definitions/saigon.js +3 -3
  13. package/_cjs/chains/definitions/saigon.js.map +1 -1
  14. package/_cjs/chains/definitions/sei.js +3 -3
  15. package/_cjs/chains/definitions/sei.js.map +1 -1
  16. package/_cjs/chains/definitions/seiTestnet.js +3 -2
  17. package/_cjs/chains/definitions/seiTestnet.js.map +1 -1
  18. package/_cjs/chains/index.js +7 -7
  19. package/_cjs/chains/index.js.map +1 -1
  20. package/_cjs/clients/decorators/public.js +2 -0
  21. package/_cjs/clients/decorators/public.js.map +1 -1
  22. package/_cjs/errors/version.js +1 -1
  23. package/_cjs/index.js.map +1 -1
  24. package/_cjs/tempo/Account.js +7 -6
  25. package/_cjs/tempo/Account.js.map +1 -1
  26. package/_cjs/tempo/Addresses.js +2 -1
  27. package/_cjs/tempo/Addresses.js.map +1 -1
  28. package/_cjs/tempo/Decorator.js +11 -0
  29. package/_cjs/tempo/Decorator.js.map +1 -1
  30. package/_cjs/tempo/Transaction.js +9 -14
  31. package/_cjs/tempo/Transaction.js.map +1 -1
  32. package/_cjs/tempo/actions/accessKey.js +238 -0
  33. package/_cjs/tempo/actions/accessKey.js.map +1 -0
  34. package/_cjs/tempo/actions/index.js +2 -1
  35. package/_cjs/tempo/actions/index.js.map +1 -1
  36. package/_cjs/utils/abi/parseEventLogs.js +8 -6
  37. package/_cjs/utils/abi/parseEventLogs.js.map +1 -1
  38. package/_esm/actions/index.js +1 -0
  39. package/_esm/actions/index.js.map +1 -1
  40. package/_esm/actions/public/getDelegation.js +43 -0
  41. package/_esm/actions/public/getDelegation.js.map +1 -0
  42. package/_esm/actions/wallet/sendRawTransactionSync.js +1 -2
  43. package/_esm/actions/wallet/sendRawTransactionSync.js.map +1 -1
  44. package/_esm/chains/definitions/krown.js +23 -0
  45. package/_esm/chains/definitions/krown.js.map +1 -0
  46. package/_esm/chains/definitions/polygon.js +1 -1
  47. package/_esm/chains/definitions/polygon.js.map +1 -1
  48. package/_esm/chains/definitions/saigon.js +3 -3
  49. package/_esm/chains/definitions/saigon.js.map +1 -1
  50. package/_esm/chains/definitions/sei.js +3 -3
  51. package/_esm/chains/definitions/sei.js.map +1 -1
  52. package/_esm/chains/definitions/seiTestnet.js +3 -2
  53. package/_esm/chains/definitions/seiTestnet.js.map +1 -1
  54. package/_esm/chains/index.js +1 -1
  55. package/_esm/chains/index.js.map +1 -1
  56. package/_esm/clients/decorators/public.js +2 -0
  57. package/_esm/clients/decorators/public.js.map +1 -1
  58. package/_esm/errors/version.js +1 -1
  59. package/_esm/index.js.map +1 -1
  60. package/_esm/tempo/Account.js +7 -7
  61. package/_esm/tempo/Account.js.map +1 -1
  62. package/_esm/tempo/Addresses.js +1 -0
  63. package/_esm/tempo/Addresses.js.map +1 -1
  64. package/_esm/tempo/Decorator.js +11 -0
  65. package/_esm/tempo/Decorator.js.map +1 -1
  66. package/_esm/tempo/Transaction.js +9 -17
  67. package/_esm/tempo/Transaction.js.map +1 -1
  68. package/_esm/tempo/actions/accessKey.js +548 -0
  69. package/_esm/tempo/actions/accessKey.js.map +1 -0
  70. package/_esm/tempo/actions/index.js +1 -0
  71. package/_esm/tempo/actions/index.js.map +1 -1
  72. package/_esm/utils/abi/parseEventLogs.js +11 -6
  73. package/_esm/utils/abi/parseEventLogs.js.map +1 -1
  74. package/_types/actions/index.d.ts +1 -0
  75. package/_types/actions/index.d.ts.map +1 -1
  76. package/_types/actions/public/getDelegation.d.ts +46 -0
  77. package/_types/actions/public/getDelegation.d.ts.map +1 -0
  78. package/_types/actions/wallet/sendRawTransactionSync.d.ts +1 -1
  79. package/_types/actions/wallet/sendRawTransactionSync.d.ts.map +1 -1
  80. package/_types/chains/definitions/{seiDevnet.d.ts → krown.d.ts} +10 -10
  81. package/_types/chains/definitions/krown.d.ts.map +1 -0
  82. package/_types/chains/definitions/polygon.d.ts +1 -1
  83. package/_types/chains/definitions/saigon.d.ts +3 -3
  84. package/_types/chains/definitions/sei.d.ts +3 -3
  85. package/_types/chains/definitions/seiTestnet.d.ts +3 -2
  86. package/_types/chains/definitions/seiTestnet.d.ts.map +1 -1
  87. package/_types/chains/index.d.ts +1 -1
  88. package/_types/chains/index.d.ts.map +1 -1
  89. package/_types/clients/decorators/public.d.ts +22 -0
  90. package/_types/clients/decorators/public.d.ts.map +1 -1
  91. package/_types/errors/version.d.ts +1 -1
  92. package/_types/index.d.ts +1 -0
  93. package/_types/index.d.ts.map +1 -1
  94. package/_types/tempo/Account.d.ts +12 -11
  95. package/_types/tempo/Account.d.ts.map +1 -1
  96. package/_types/tempo/Addresses.d.ts +1 -0
  97. package/_types/tempo/Addresses.d.ts.map +1 -1
  98. package/_types/tempo/Decorator.d.ts +340 -122
  99. package/_types/tempo/Decorator.d.ts.map +1 -1
  100. package/_types/tempo/Transaction.d.ts +3 -1
  101. package/_types/tempo/Transaction.d.ts.map +1 -1
  102. package/_types/tempo/actions/accessKey.d.ts +1169 -0
  103. package/_types/tempo/actions/accessKey.d.ts.map +1 -0
  104. package/_types/tempo/actions/index.d.ts +1 -0
  105. package/_types/tempo/actions/index.d.ts.map +1 -1
  106. package/_types/types/eip1193.d.ts +2 -2
  107. package/_types/types/eip1193.d.ts.map +1 -1
  108. package/_types/utils/abi/parseEventLogs.d.ts.map +1 -1
  109. package/actions/index.ts +6 -0
  110. package/actions/public/getDelegation.ts +80 -0
  111. package/actions/wallet/sendRawTransactionSync.ts +2 -5
  112. package/chains/definitions/krown.ts +23 -0
  113. package/chains/definitions/polygon.ts +1 -1
  114. package/chains/definitions/saigon.ts +3 -3
  115. package/chains/definitions/sei.ts +3 -3
  116. package/chains/definitions/seiTestnet.ts +3 -2
  117. package/chains/index.ts +1 -1
  118. package/clients/decorators/public.ts +29 -0
  119. package/errors/version.ts +1 -1
  120. package/index.ts +5 -0
  121. package/package.json +2 -2
  122. package/tempo/Account.ts +25 -25
  123. package/tempo/Addresses.ts +1 -0
  124. package/tempo/Decorator.ts +373 -122
  125. package/tempo/Transaction.ts +14 -19
  126. package/tempo/actions/accessKey.ts +831 -0
  127. package/tempo/actions/index.ts +1 -0
  128. package/types/eip1193.ts +2 -2
  129. package/utils/abi/parseEventLogs.ts +13 -6
  130. package/_cjs/chains/definitions/seiDevnet.js +0 -22
  131. package/_cjs/chains/definitions/seiDevnet.js.map +0 -1
  132. package/_esm/chains/definitions/seiDevnet.js +0 -19
  133. package/_esm/chains/definitions/seiDevnet.js.map +0 -1
  134. package/_types/chains/definitions/seiDevnet.d.ts.map +0 -1
  135. package/chains/definitions/seiDevnet.ts +0 -19
@@ -0,0 +1,831 @@
1
+ import type { Address } from 'abitype'
2
+ import type { Account } from '../../accounts/types.js'
3
+ import { parseAccount } from '../../accounts/utils/parseAccount.js'
4
+ import type { ReadContractReturnType } from '../../actions/public/readContract.js'
5
+ import { readContract } from '../../actions/public/readContract.js'
6
+ import { sendTransaction } from '../../actions/wallet/sendTransaction.js'
7
+ import { sendTransactionSync } from '../../actions/wallet/sendTransactionSync.js'
8
+ import type { WriteContractReturnType } from '../../actions/wallet/writeContract.js'
9
+ import { writeContract } from '../../actions/wallet/writeContract.js'
10
+ import { writeContractSync } from '../../actions/wallet/writeContractSync.js'
11
+ import type { Client } from '../../clients/createClient.js'
12
+ import type { Transport } from '../../clients/transports/createTransport.js'
13
+ import type { BaseErrorType } from '../../errors/base.js'
14
+ import type { Chain } from '../../types/chain.js'
15
+ import type { GetEventArgs } from '../../types/contract.js'
16
+ import type { Log } from '../../types/log.js'
17
+ import type { Compute } from '../../types/utils.js'
18
+ import { parseEventLogs } from '../../utils/abi/parseEventLogs.js'
19
+ import * as Abis from '../Abis.js'
20
+ import type { AccessKeyAccount, RootAccount } from '../Account.js'
21
+ import { signKeyAuthorization } from '../Account.js'
22
+ import * as Addresses from '../Addresses.js'
23
+ import type {
24
+ GetAccountParameter,
25
+ ReadParameters,
26
+ WriteParameters,
27
+ } from '../internal/types.js'
28
+ import { defineCall } from '../internal/utils.js'
29
+ import type { TransactionReceipt } from '../Transaction.js'
30
+
31
+ /** @internal */
32
+ const signatureTypes = {
33
+ 0: 'secp256k1',
34
+ 1: 'p256',
35
+ 2: 'webAuthn',
36
+ } as const satisfies Record<number, string>
37
+
38
+ /** @internal */
39
+ const spendPolicies = {
40
+ true: 'limited',
41
+ false: 'unlimited',
42
+ } as const
43
+
44
+ /**
45
+ * Authorizes an access key by signing a key authorization and sending a transaction.
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * import { createClient, http } from 'viem'
50
+ * import { tempo } from 'viem/chains'
51
+ * import { Actions, Account } from 'viem/tempo'
52
+ * import { generatePrivateKey } from 'viem/accounts'
53
+ *
54
+ * const account = Account.from({ privateKey: '0x...' })
55
+ * const client = createClient({
56
+ * account,
57
+ * chain: tempo({ feeToken: '0x20c0000000000000000000000000000000000001' }),
58
+ * transport: http(),
59
+ * })
60
+ *
61
+ * const accessKey = Account.fromP256(generatePrivateKey(), {
62
+ * access: account,
63
+ * })
64
+ *
65
+ * const hash = await Actions.accessKey.authorize(client, {
66
+ * accessKey,
67
+ * expiry: Math.floor((Date.now() + 30_000) / 1000),
68
+ * })
69
+ * ```
70
+ *
71
+ * @param client - Client.
72
+ * @param parameters - Parameters.
73
+ * @returns The transaction hash.
74
+ */
75
+ export async function authorize<
76
+ chain extends Chain | undefined,
77
+ account extends Account | undefined,
78
+ >(
79
+ client: Client<Transport, chain, account>,
80
+ parameters: authorize.Parameters<chain, account>,
81
+ ): Promise<authorize.ReturnValue> {
82
+ return authorize.inner(sendTransaction, client, parameters)
83
+ }
84
+
85
+ export namespace authorize {
86
+ export type Parameters<
87
+ chain extends Chain | undefined = Chain | undefined,
88
+ account extends Account | undefined = Account | undefined,
89
+ > = WriteParameters<chain, account> & Args
90
+
91
+ export type Args = {
92
+ /** The access key to authorize. */
93
+ accessKey: Pick<AccessKeyAccount, 'accessKeyAddress' | 'keyType'>
94
+ /** Unix timestamp when the key expires. */
95
+ expiry?: number | undefined
96
+ /** Spending limits per token. */
97
+ limits?: { token: Address; limit: bigint }[] | undefined
98
+ }
99
+
100
+ export type ReturnValue = WriteContractReturnType
101
+
102
+ // TODO: exhaustive error type
103
+ export type ErrorType = BaseErrorType
104
+
105
+ /** @internal */
106
+ export async function inner<
107
+ action extends typeof sendTransaction | typeof sendTransactionSync,
108
+ chain extends Chain | undefined,
109
+ account extends Account | undefined,
110
+ >(
111
+ action: action,
112
+ client: Client<Transport, chain, account>,
113
+ parameters: authorize.Parameters<chain, account>,
114
+ ): Promise<ReturnType<action>> {
115
+ const { accessKey, expiry, limits, ...rest } = parameters
116
+ const account_ = rest.account ?? client.account
117
+ if (!account_) throw new Error('account is required.')
118
+ const parsed = parseAccount(account_)
119
+ const keyAuthorization = await signKeyAuthorization(parsed as never, {
120
+ key: accessKey,
121
+ expiry,
122
+ limits,
123
+ })
124
+ return (await action(client, {
125
+ ...rest,
126
+ keyAuthorization,
127
+ } as never)) as never
128
+ }
129
+
130
+ export function extractEvent(logs: Log[]) {
131
+ const [log] = parseEventLogs({
132
+ abi: Abis.accountKeychain,
133
+ logs,
134
+ eventName: 'KeyAuthorized',
135
+ strict: true,
136
+ })
137
+ if (!log) throw new Error('`KeyAuthorized` event not found.')
138
+ return log
139
+ }
140
+ }
141
+
142
+ /**
143
+ * Authorizes an access key and waits for the transaction receipt.
144
+ *
145
+ * @example
146
+ * ```ts
147
+ * import { createClient, http } from 'viem'
148
+ * import { tempo } from 'viem/chains'
149
+ * import { Actions, Account } from 'viem/tempo'
150
+ * import { generatePrivateKey } from 'viem/accounts'
151
+ *
152
+ * const account = Account.from({ privateKey: '0x...' })
153
+ * const client = createClient({
154
+ * account,
155
+ * chain: tempo({ feeToken: '0x20c0000000000000000000000000000000000001' }),
156
+ * transport: http(),
157
+ * })
158
+ *
159
+ * const accessKey = Account.fromP256(generatePrivateKey(), {
160
+ * access: account,
161
+ * })
162
+ *
163
+ * const { receipt, ...result } = await Actions.accessKey.authorizeSync(client, {
164
+ * accessKey,
165
+ * expiry: Math.floor((Date.now() + 30_000) / 1000),
166
+ * })
167
+ * ```
168
+ *
169
+ * @param client - Client.
170
+ * @param parameters - Parameters.
171
+ * @returns The transaction receipt and event data.
172
+ */
173
+ export async function authorizeSync<
174
+ chain extends Chain | undefined,
175
+ account extends Account | undefined,
176
+ >(
177
+ client: Client<Transport, chain, account>,
178
+ parameters: authorizeSync.Parameters<chain, account>,
179
+ ): Promise<authorizeSync.ReturnValue> {
180
+ const { throwOnReceiptRevert = true, ...rest } = parameters
181
+ const receipt = await authorize.inner(sendTransactionSync, client, {
182
+ ...rest,
183
+ throwOnReceiptRevert,
184
+ } as never)
185
+ const { args } = authorize.extractEvent(receipt.logs)
186
+ return {
187
+ ...args,
188
+ receipt,
189
+ } as never
190
+ }
191
+
192
+ export namespace authorizeSync {
193
+ export type Parameters<
194
+ chain extends Chain | undefined = Chain | undefined,
195
+ account extends Account | undefined = Account | undefined,
196
+ > = authorize.Parameters<chain, account>
197
+
198
+ export type Args = authorize.Args
199
+
200
+ export type ReturnValue = Compute<
201
+ GetEventArgs<
202
+ typeof Abis.accountKeychain,
203
+ 'KeyAuthorized',
204
+ { IndexedOnly: false; Required: true }
205
+ > & {
206
+ receipt: TransactionReceipt
207
+ }
208
+ >
209
+
210
+ // TODO: exhaustive error type
211
+ export type ErrorType = BaseErrorType
212
+ }
213
+
214
+ /**
215
+ * Revokes an authorized access key.
216
+ *
217
+ * @example
218
+ * ```ts
219
+ * import { createClient, http } from 'viem'
220
+ * import { tempo } from 'viem/chains'
221
+ * import { Actions } from 'viem/tempo'
222
+ * import { privateKeyToAccount } from 'viem/accounts'
223
+ *
224
+ * const client = createClient({
225
+ * account: privateKeyToAccount('0x...'),
226
+ * chain: tempo({ feeToken: '0x20c0000000000000000000000000000000000001' }),
227
+ * transport: http(),
228
+ * })
229
+ *
230
+ * const hash = await Actions.accessKey.revoke(client, {
231
+ * accessKey: '0x...',
232
+ * })
233
+ * ```
234
+ *
235
+ * @param client - Client.
236
+ * @param parameters - Parameters.
237
+ * @returns The transaction hash.
238
+ */
239
+ export async function revoke<
240
+ chain extends Chain | undefined,
241
+ account extends Account | undefined,
242
+ >(
243
+ client: Client<Transport, chain, account>,
244
+ parameters: revoke.Parameters<chain, account>,
245
+ ): Promise<revoke.ReturnValue> {
246
+ return revoke.inner(writeContract, client, parameters)
247
+ }
248
+
249
+ export namespace revoke {
250
+ export type Parameters<
251
+ chain extends Chain | undefined = Chain | undefined,
252
+ account extends Account | undefined = Account | undefined,
253
+ > = WriteParameters<chain, account> & Args
254
+
255
+ export type Args = {
256
+ /** The access key to revoke. */
257
+ accessKey: Address | AccessKeyAccount
258
+ }
259
+
260
+ export type ReturnValue = WriteContractReturnType
261
+
262
+ // TODO: exhaustive error type
263
+ export type ErrorType = BaseErrorType
264
+
265
+ /** @internal */
266
+ export async function inner<
267
+ action extends typeof writeContract | typeof writeContractSync,
268
+ chain extends Chain | undefined,
269
+ account extends Account | undefined,
270
+ >(
271
+ action: action,
272
+ client: Client<Transport, chain, account>,
273
+ parameters: revoke.Parameters<chain, account>,
274
+ ): Promise<ReturnType<action>> {
275
+ const { accessKey, ...rest } = parameters
276
+ const call = revoke.call({ accessKey })
277
+ return (await action(client, {
278
+ ...rest,
279
+ ...call,
280
+ } as never)) as never
281
+ }
282
+
283
+ /**
284
+ * Defines a call to the `revokeKey` function.
285
+ *
286
+ * Can be passed as a parameter to:
287
+ * - [`estimateContractGas`](https://viem.sh/docs/contract/estimateContractGas): estimate the gas cost of the call
288
+ * - [`simulateContract`](https://viem.sh/docs/contract/simulateContract): simulate the call
289
+ * - [`sendCalls`](https://viem.sh/docs/actions/wallet/sendCalls): send multiple calls
290
+ *
291
+ * @example
292
+ * ```ts
293
+ * import { createClient, http, walletActions } from 'viem'
294
+ * import { tempo } from 'viem/chains'
295
+ * import { Actions } from 'viem/tempo'
296
+ *
297
+ * const client = createClient({
298
+ * chain: tempo({ feeToken: '0x20c0000000000000000000000000000000000001' }),
299
+ * transport: http(),
300
+ * }).extend(walletActions)
301
+ *
302
+ * const hash = await client.sendTransaction({
303
+ * calls: [
304
+ * Actions.accessKey.revoke.call({ accessKey: '0x...' }),
305
+ * ],
306
+ * })
307
+ * ```
308
+ *
309
+ * @param args - Arguments.
310
+ * @returns The call.
311
+ */
312
+ export function call(args: Args) {
313
+ const { accessKey } = args
314
+ return defineCall({
315
+ address: Addresses.accountKeychain,
316
+ abi: Abis.accountKeychain,
317
+ functionName: 'revokeKey',
318
+ args: [resolveAccessKey(accessKey)],
319
+ })
320
+ }
321
+
322
+ export function extractEvent(logs: Log[]) {
323
+ const [log] = parseEventLogs({
324
+ abi: Abis.accountKeychain,
325
+ logs,
326
+ eventName: 'KeyRevoked',
327
+ strict: true,
328
+ })
329
+ if (!log) throw new Error('`KeyRevoked` event not found.')
330
+ return log
331
+ }
332
+ }
333
+
334
+ /**
335
+ * Revokes an authorized access key and waits for the transaction receipt.
336
+ *
337
+ * @example
338
+ * ```ts
339
+ * import { createClient, http } from 'viem'
340
+ * import { tempo } from 'viem/chains'
341
+ * import { Actions } from 'viem/tempo'
342
+ * import { privateKeyToAccount } from 'viem/accounts'
343
+ *
344
+ * const client = createClient({
345
+ * account: privateKeyToAccount('0x...'),
346
+ * chain: tempo({ feeToken: '0x20c0000000000000000000000000000000000001' }),
347
+ * transport: http(),
348
+ * })
349
+ *
350
+ * const result = await Actions.accessKey.revokeSync(client, {
351
+ * accessKey: '0x...',
352
+ * })
353
+ * ```
354
+ *
355
+ * @param client - Client.
356
+ * @param parameters - Parameters.
357
+ * @returns The transaction receipt and event data.
358
+ */
359
+ export async function revokeSync<
360
+ chain extends Chain | undefined,
361
+ account extends Account | undefined,
362
+ >(
363
+ client: Client<Transport, chain, account>,
364
+ parameters: revokeSync.Parameters<chain, account>,
365
+ ): Promise<revokeSync.ReturnValue> {
366
+ const { throwOnReceiptRevert = true, ...rest } = parameters
367
+ const receipt = await revoke.inner(writeContractSync, client, {
368
+ ...rest,
369
+ throwOnReceiptRevert,
370
+ } as never)
371
+ const { args } = revoke.extractEvent(receipt.logs)
372
+ return {
373
+ ...args,
374
+ receipt,
375
+ } as never
376
+ }
377
+
378
+ export namespace revokeSync {
379
+ export type Parameters<
380
+ chain extends Chain | undefined = Chain | undefined,
381
+ account extends Account | undefined = Account | undefined,
382
+ > = revoke.Parameters<chain, account>
383
+
384
+ export type Args = revoke.Args
385
+
386
+ export type ReturnValue = Compute<
387
+ GetEventArgs<
388
+ typeof Abis.accountKeychain,
389
+ 'KeyRevoked',
390
+ { IndexedOnly: false; Required: true }
391
+ > & {
392
+ receipt: TransactionReceipt
393
+ }
394
+ >
395
+
396
+ // TODO: exhaustive error type
397
+ export type ErrorType = BaseErrorType
398
+ }
399
+
400
+ /**
401
+ * Updates the spending limit for a specific token on an authorized access key.
402
+ *
403
+ * @example
404
+ * ```ts
405
+ * import { createClient, http } from 'viem'
406
+ * import { tempo } from 'viem/chains'
407
+ * import { Actions } from 'viem/tempo'
408
+ * import { privateKeyToAccount } from 'viem/accounts'
409
+ *
410
+ * const client = createClient({
411
+ * account: privateKeyToAccount('0x...'),
412
+ * chain: tempo({ feeToken: '0x20c0000000000000000000000000000000000001' }),
413
+ * transport: http(),
414
+ * })
415
+ *
416
+ * const hash = await Actions.accessKey.updateLimit(client, {
417
+ * accessKey: '0x...',
418
+ * token: '0x...',
419
+ * limit: 1000000000000000000n,
420
+ * })
421
+ * ```
422
+ *
423
+ * @param client - Client.
424
+ * @param parameters - Parameters.
425
+ * @returns The transaction hash.
426
+ */
427
+ export async function updateLimit<
428
+ chain extends Chain | undefined,
429
+ account extends Account | undefined,
430
+ >(
431
+ client: Client<Transport, chain, account>,
432
+ parameters: updateLimit.Parameters<chain, account>,
433
+ ): Promise<updateLimit.ReturnValue> {
434
+ return updateLimit.inner(writeContract, client, parameters)
435
+ }
436
+
437
+ export namespace updateLimit {
438
+ export type Parameters<
439
+ chain extends Chain | undefined = Chain | undefined,
440
+ account extends Account | undefined = Account | undefined,
441
+ > = WriteParameters<chain, account> & Args
442
+
443
+ export type Args = {
444
+ /** The access key to update. */
445
+ accessKey: Address | AccessKeyAccount
446
+ /** The token address. */
447
+ token: Address
448
+ /** The new spending limit. */
449
+ limit: bigint
450
+ }
451
+
452
+ export type ReturnValue = WriteContractReturnType
453
+
454
+ // TODO: exhaustive error type
455
+ export type ErrorType = BaseErrorType
456
+
457
+ /** @internal */
458
+ export async function inner<
459
+ action extends typeof writeContract | typeof writeContractSync,
460
+ chain extends Chain | undefined,
461
+ account extends Account | undefined,
462
+ >(
463
+ action: action,
464
+ client: Client<Transport, chain, account>,
465
+ parameters: updateLimit.Parameters<chain, account>,
466
+ ): Promise<ReturnType<action>> {
467
+ const { accessKey, token, limit, ...rest } = parameters
468
+ const call = updateLimit.call({ accessKey, token, limit })
469
+ return (await action(client, {
470
+ ...rest,
471
+ ...call,
472
+ } as never)) as never
473
+ }
474
+
475
+ /**
476
+ * Defines a call to the `updateSpendingLimit` function.
477
+ *
478
+ * Can be passed as a parameter to:
479
+ * - [`estimateContractGas`](https://viem.sh/docs/contract/estimateContractGas): estimate the gas cost of the call
480
+ * - [`simulateContract`](https://viem.sh/docs/contract/simulateContract): simulate the call
481
+ * - [`sendCalls`](https://viem.sh/docs/actions/wallet/sendCalls): send multiple calls
482
+ *
483
+ * @example
484
+ * ```ts
485
+ * import { createClient, http, walletActions } from 'viem'
486
+ * import { tempo } from 'viem/chains'
487
+ * import { Actions } from 'viem/tempo'
488
+ *
489
+ * const client = createClient({
490
+ * chain: tempo({ feeToken: '0x20c0000000000000000000000000000000000001' }),
491
+ * transport: http(),
492
+ * }).extend(walletActions)
493
+ *
494
+ * const hash = await client.sendTransaction({
495
+ * calls: [
496
+ * Actions.accessKey.updateLimit.call({
497
+ * accessKey: '0x...',
498
+ * token: '0x...',
499
+ * limit: 1000000000000000000n,
500
+ * }),
501
+ * ],
502
+ * })
503
+ * ```
504
+ *
505
+ * @param args - Arguments.
506
+ * @returns The call.
507
+ */
508
+ export function call(args: Args) {
509
+ const { accessKey, token, limit } = args
510
+ return defineCall({
511
+ address: Addresses.accountKeychain,
512
+ abi: Abis.accountKeychain,
513
+ functionName: 'updateSpendingLimit',
514
+ args: [resolveAccessKey(accessKey), token, limit],
515
+ })
516
+ }
517
+
518
+ export function extractEvent(logs: Log[]) {
519
+ const [log] = parseEventLogs({
520
+ abi: Abis.accountKeychain,
521
+ logs,
522
+ eventName: 'SpendingLimitUpdated',
523
+ strict: true,
524
+ })
525
+ if (!log) throw new Error('`SpendingLimitUpdated` event not found.')
526
+ return log
527
+ }
528
+ }
529
+
530
+ /**
531
+ * Updates the spending limit and waits for the transaction receipt.
532
+ *
533
+ * @example
534
+ * ```ts
535
+ * import { createClient, http } from 'viem'
536
+ * import { tempo } from 'viem/chains'
537
+ * import { Actions } from 'viem/tempo'
538
+ * import { privateKeyToAccount } from 'viem/accounts'
539
+ *
540
+ * const client = createClient({
541
+ * account: privateKeyToAccount('0x...'),
542
+ * chain: tempo({ feeToken: '0x20c0000000000000000000000000000000000001' }),
543
+ * transport: http(),
544
+ * })
545
+ *
546
+ * const result = await Actions.accessKey.updateLimitSync(client, {
547
+ * accessKey: '0x...',
548
+ * token: '0x...',
549
+ * limit: 1000000000000000000n,
550
+ * })
551
+ * ```
552
+ *
553
+ * @param client - Client.
554
+ * @param parameters - Parameters.
555
+ * @returns The transaction receipt and event data.
556
+ */
557
+ export async function updateLimitSync<
558
+ chain extends Chain | undefined,
559
+ account extends Account | undefined,
560
+ >(
561
+ client: Client<Transport, chain, account>,
562
+ parameters: updateLimitSync.Parameters<chain, account>,
563
+ ): Promise<updateLimitSync.ReturnValue> {
564
+ const { throwOnReceiptRevert = true, ...rest } = parameters
565
+ const receipt = await updateLimit.inner(writeContractSync, client, {
566
+ ...rest,
567
+ throwOnReceiptRevert,
568
+ } as never)
569
+ const { args } = updateLimit.extractEvent(receipt.logs)
570
+ return {
571
+ account: args.account,
572
+ publicKey: args.publicKey,
573
+ token: args.token,
574
+ limit: args.newLimit,
575
+ receipt,
576
+ }
577
+ }
578
+
579
+ export namespace updateLimitSync {
580
+ export type Parameters<
581
+ chain extends Chain | undefined = Chain | undefined,
582
+ account extends Account | undefined = Account | undefined,
583
+ > = updateLimit.Parameters<chain, account>
584
+
585
+ export type Args = updateLimit.Args
586
+
587
+ export type ReturnValue = {
588
+ /** The account that owns the key. */
589
+ account: Address
590
+ /** The access key address. */
591
+ publicKey: Address
592
+ /** The token address. */
593
+ token: Address
594
+ /** The new spending limit. */
595
+ limit: bigint
596
+ /** The transaction receipt. */
597
+ receipt: TransactionReceipt
598
+ }
599
+
600
+ // TODO: exhaustive error type
601
+ export type ErrorType = BaseErrorType
602
+ }
603
+
604
+ /**
605
+ * Gets access key information.
606
+ *
607
+ * @example
608
+ * ```ts
609
+ * import { createClient, http } from 'viem'
610
+ * import { tempo } from 'viem/chains'
611
+ * import { Actions } from 'viem/tempo'
612
+ *
613
+ * const client = createClient({
614
+ * chain: tempo({ feeToken: '0x20c0000000000000000000000000000000000001' }),
615
+ * transport: http(),
616
+ * })
617
+ *
618
+ * const key = await Actions.accessKey.getMetadata(client, {
619
+ * account: '0x...',
620
+ * accessKey: '0x...',
621
+ * })
622
+ * ```
623
+ *
624
+ * @param client - Client.
625
+ * @param parameters - Parameters.
626
+ * @returns The key information.
627
+ */
628
+ export async function getMetadata<
629
+ chain extends Chain | undefined,
630
+ account extends Account | undefined,
631
+ >(
632
+ client: Client<Transport, chain, account>,
633
+ parameters: getMetadata.Parameters<account>,
634
+ ): Promise<getMetadata.ReturnValue> {
635
+ const { account: account_ = client.account, accessKey, ...rest } = parameters
636
+ if (!account_) throw new Error('account is required.')
637
+ const account = parseAccount(account_)
638
+ const result = await readContract(client, {
639
+ ...rest,
640
+ ...getMetadata.call({ account: account.address, accessKey }),
641
+ })
642
+ return {
643
+ address: result.keyId,
644
+ keyType:
645
+ signatureTypes[result.signatureType as keyof typeof signatureTypes] ??
646
+ 'secp256k1',
647
+ expiry: result.expiry,
648
+ spendPolicy: spendPolicies[`${result.enforceLimits}`],
649
+ isRevoked: result.isRevoked,
650
+ }
651
+ }
652
+
653
+ export namespace getMetadata {
654
+ export type Parameters<
655
+ account extends Account | undefined = Account | undefined,
656
+ > = ReadParameters & GetAccountParameter<account> & Pick<Args, 'accessKey'>
657
+
658
+ export type Args = {
659
+ /** Account address. */
660
+ account: Address
661
+ /** The access key. */
662
+ accessKey: Address | AccessKeyAccount
663
+ }
664
+
665
+ export type ReturnValue = {
666
+ /** The access key address. */
667
+ address: Address
668
+ /** The key type. */
669
+ keyType: 'secp256k1' | 'p256' | 'webAuthn'
670
+ /** The expiry timestamp. */
671
+ expiry: bigint
672
+ /** The spending policy. */
673
+ spendPolicy: 'limited' | 'unlimited'
674
+ /** Whether the key is revoked. */
675
+ isRevoked: boolean
676
+ }
677
+
678
+ /**
679
+ * Defines a call to the `getKey` function.
680
+ *
681
+ * @param args - Arguments.
682
+ * @returns The call.
683
+ */
684
+ export function call(args: Args) {
685
+ const { account, accessKey } = args
686
+ return defineCall({
687
+ address: Addresses.accountKeychain,
688
+ abi: Abis.accountKeychain,
689
+ functionName: 'getKey',
690
+ args: [account, resolveAccessKey(accessKey)],
691
+ })
692
+ }
693
+ }
694
+
695
+ /**
696
+ * Gets the remaining spending limit for a key-token pair.
697
+ *
698
+ * @example
699
+ * ```ts
700
+ * import { createClient, http } from 'viem'
701
+ * import { tempo } from 'viem/chains'
702
+ * import { Actions } from 'viem/tempo'
703
+ *
704
+ * const client = createClient({
705
+ * chain: tempo({ feeToken: '0x20c0000000000000000000000000000000000001' }),
706
+ * transport: http(),
707
+ * })
708
+ *
709
+ * const remaining = await Actions.accessKey.getRemainingLimit(client, {
710
+ * account: '0x...',
711
+ * accessKey: '0x...',
712
+ * token: '0x...',
713
+ * })
714
+ * ```
715
+ *
716
+ * @param client - Client.
717
+ * @param parameters - Parameters.
718
+ * @returns The remaining spending amount.
719
+ */
720
+ export async function getRemainingLimit<
721
+ chain extends Chain | undefined,
722
+ account extends Account | undefined,
723
+ >(
724
+ client: Client<Transport, chain, account>,
725
+ parameters: getRemainingLimit.Parameters<account>,
726
+ ): Promise<getRemainingLimit.ReturnValue> {
727
+ const {
728
+ account: account_ = client.account,
729
+ accessKey,
730
+ token,
731
+ ...rest
732
+ } = parameters
733
+ if (!account_) throw new Error('account is required.')
734
+ const account = parseAccount(account_)
735
+ return readContract(client, {
736
+ ...rest,
737
+ ...getRemainingLimit.call({ account: account.address, accessKey, token }),
738
+ })
739
+ }
740
+
741
+ export namespace getRemainingLimit {
742
+ export type Parameters<
743
+ account extends Account | undefined = Account | undefined,
744
+ > = ReadParameters &
745
+ GetAccountParameter<account> &
746
+ Pick<Args, 'accessKey' | 'token'>
747
+
748
+ export type Args = {
749
+ /** Account address. */
750
+ account: Address
751
+ /** The access key. */
752
+ accessKey: Address | AccessKeyAccount
753
+ /** The token address. */
754
+ token: Address
755
+ }
756
+
757
+ export type ReturnValue = ReadContractReturnType<
758
+ typeof Abis.accountKeychain,
759
+ 'getRemainingLimit',
760
+ never
761
+ >
762
+
763
+ /**
764
+ * Defines a call to the `getRemainingLimit` function.
765
+ *
766
+ * @param args - Arguments.
767
+ * @returns The call.
768
+ */
769
+ export function call(args: Args) {
770
+ const { account, accessKey, token } = args
771
+ return defineCall({
772
+ address: Addresses.accountKeychain,
773
+ abi: Abis.accountKeychain,
774
+ functionName: 'getRemainingLimit',
775
+ args: [account, resolveAccessKey(accessKey), token],
776
+ })
777
+ }
778
+ }
779
+
780
+ /**
781
+ * Signs a key authorization for an access key.
782
+ *
783
+ * @example
784
+ * ```ts
785
+ * import { generatePrivateKey } from 'viem/accounts'
786
+ * import { Account, Actions } from 'viem/tempo'
787
+ *
788
+ * const account = Account.from({ privateKey: '0x...' })
789
+ * const accessKey = Account.fromP256(generatePrivateKey(), {
790
+ * access: account,
791
+ * })
792
+ *
793
+ * const keyAuthorization = await Actions.accessKey.signAuthorization(
794
+ * account,
795
+ * {
796
+ * accessKey,
797
+ * expiry: Math.floor((Date.now() + 30_000) / 1000),
798
+ * },
799
+ * )
800
+ * ```
801
+ *
802
+ * @param account - The root account signing the authorization.
803
+ * @param parameters - Parameters.
804
+ * @returns The signed key authorization.
805
+ */
806
+ export async function signAuthorization(
807
+ account: RootAccount,
808
+ parameters: signAuthorization.Parameters,
809
+ ): Promise<signAuthorization.ReturnValue> {
810
+ const { accessKey, ...rest } = parameters
811
+ return signKeyAuthorization(account, { key: accessKey, ...rest })
812
+ }
813
+
814
+ export namespace signAuthorization {
815
+ export type Parameters = {
816
+ /** The access key to authorize. */
817
+ accessKey: Pick<AccessKeyAccount, 'accessKeyAddress' | 'keyType'>
818
+ /** Unix timestamp when the key expires. */
819
+ expiry?: number | undefined
820
+ /** Spending limits per token. */
821
+ limits?: { token: Address; limit: bigint }[] | undefined
822
+ }
823
+
824
+ export type ReturnValue = Awaited<ReturnType<typeof signKeyAuthorization>>
825
+ }
826
+
827
+ /** @internal */
828
+ function resolveAccessKey(accessKey: Address | AccessKeyAccount): Address {
829
+ if (typeof accessKey === 'string') return accessKey
830
+ return accessKey.accessKeyAddress
831
+ }