viem 2.57.5 → 2.57.7

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 (185) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/_cjs/actions/wallet/prepareTransactionRequest.js +10 -0
  3. package/_cjs/actions/wallet/prepareTransactionRequest.js.map +1 -1
  4. package/_cjs/errors/version.js +1 -1
  5. package/_cjs/tempo/Abis.js +297 -1
  6. package/_cjs/tempo/Abis.js.map +1 -1
  7. package/_cjs/tempo/Addresses.js +2 -1
  8. package/_cjs/tempo/Addresses.js.map +1 -1
  9. package/_cjs/tempo/Decorator.js.map +1 -1
  10. package/_cjs/tempo/ExecutionError.js +94 -3
  11. package/_cjs/tempo/ExecutionError.js.map +1 -1
  12. package/_cjs/tempo/actions/accessKey.js +382 -10
  13. package/_cjs/tempo/actions/accessKey.js.map +1 -1
  14. package/_cjs/tempo/actions/accounts.js +3 -2
  15. package/_cjs/tempo/actions/accounts.js.map +1 -1
  16. package/_cjs/tempo/actions/amm.js +90 -4
  17. package/_cjs/tempo/actions/amm.js.map +1 -1
  18. package/_cjs/tempo/actions/channel.js +183 -7
  19. package/_cjs/tempo/actions/channel.js.map +1 -1
  20. package/_cjs/tempo/actions/dex.js +507 -14
  21. package/_cjs/tempo/actions/dex.js.map +1 -1
  22. package/_cjs/tempo/actions/earn/deployment.js +4 -3
  23. package/_cjs/tempo/actions/earn/deployment.js.map +1 -1
  24. package/_cjs/tempo/actions/earn.js +22 -21
  25. package/_cjs/tempo/actions/earn.js.map +1 -1
  26. package/_cjs/tempo/actions/fee.js +85 -5
  27. package/_cjs/tempo/actions/fee.js.map +1 -1
  28. package/_cjs/tempo/actions/nonce.js +54 -1
  29. package/_cjs/tempo/actions/nonce.js.map +1 -1
  30. package/_cjs/tempo/actions/policy.js +35 -6
  31. package/_cjs/tempo/actions/policy.js.map +1 -1
  32. package/_cjs/tempo/actions/propAmm.js +18 -8
  33. package/_cjs/tempo/actions/propAmm.js.map +1 -1
  34. package/_cjs/tempo/actions/receivePolicy.js +7 -6
  35. package/_cjs/tempo/actions/receivePolicy.js.map +1 -1
  36. package/_cjs/tempo/actions/reward.js +7 -6
  37. package/_cjs/tempo/actions/reward.js.map +1 -1
  38. package/_cjs/tempo/actions/token.js +433 -22
  39. package/_cjs/tempo/actions/token.js.map +1 -1
  40. package/_cjs/tempo/actions/validator.js +12 -11
  41. package/_cjs/tempo/actions/validator.js.map +1 -1
  42. package/_cjs/tempo/actions/virtualAddress.js +3 -2
  43. package/_cjs/tempo/actions/virtualAddress.js.map +1 -1
  44. package/_cjs/tempo/actions/zone.js +12 -11
  45. package/_cjs/tempo/actions/zone.js.map +1 -1
  46. package/_cjs/tempo/errors.js +57 -1
  47. package/_cjs/tempo/errors.js.map +1 -1
  48. package/_cjs/tempo/internal/errors.js +46 -0
  49. package/_cjs/tempo/internal/errors.js.map +1 -0
  50. package/_cjs/tempo/internal/resolveError.js +128 -0
  51. package/_cjs/tempo/internal/resolveError.js.map +1 -0
  52. package/_cjs/tempo/internal/utils.js +1 -0
  53. package/_cjs/tempo/internal/utils.js.map +1 -1
  54. package/_esm/actions/wallet/prepareTransactionRequest.js +11 -0
  55. package/_esm/actions/wallet/prepareTransactionRequest.js.map +1 -1
  56. package/_esm/errors/version.js +1 -1
  57. package/_esm/tempo/Abis.js +297 -0
  58. package/_esm/tempo/Abis.js.map +1 -1
  59. package/_esm/tempo/Addresses.js +1 -0
  60. package/_esm/tempo/Addresses.js.map +1 -1
  61. package/_esm/tempo/Decorator.js.map +1 -1
  62. package/_esm/tempo/ExecutionError.js +95 -2
  63. package/_esm/tempo/ExecutionError.js.map +1 -1
  64. package/_esm/tempo/actions/accessKey.js +406 -10
  65. package/_esm/tempo/actions/accessKey.js.map +1 -1
  66. package/_esm/tempo/actions/accounts.js +3 -2
  67. package/_esm/tempo/actions/accounts.js.map +1 -1
  68. package/_esm/tempo/actions/amm.js +95 -4
  69. package/_esm/tempo/actions/amm.js.map +1 -1
  70. package/_esm/tempo/actions/channel.js +191 -7
  71. package/_esm/tempo/actions/channel.js.map +1 -1
  72. package/_esm/tempo/actions/dex.js +537 -14
  73. package/_esm/tempo/actions/dex.js.map +1 -1
  74. package/_esm/tempo/actions/earn/deployment.js +4 -3
  75. package/_esm/tempo/actions/earn/deployment.js.map +1 -1
  76. package/_esm/tempo/actions/earn.js +22 -21
  77. package/_esm/tempo/actions/earn.js.map +1 -1
  78. package/_esm/tempo/actions/fee.js +90 -6
  79. package/_esm/tempo/actions/fee.js.map +1 -1
  80. package/_esm/tempo/actions/nonce.js +57 -1
  81. package/_esm/tempo/actions/nonce.js.map +1 -1
  82. package/_esm/tempo/actions/policy.js +36 -6
  83. package/_esm/tempo/actions/policy.js.map +1 -1
  84. package/_esm/tempo/actions/propAmm.js +18 -8
  85. package/_esm/tempo/actions/propAmm.js.map +1 -1
  86. package/_esm/tempo/actions/receivePolicy.js +7 -6
  87. package/_esm/tempo/actions/receivePolicy.js.map +1 -1
  88. package/_esm/tempo/actions/reward.js +7 -6
  89. package/_esm/tempo/actions/reward.js.map +1 -1
  90. package/_esm/tempo/actions/token.js +461 -22
  91. package/_esm/tempo/actions/token.js.map +1 -1
  92. package/_esm/tempo/actions/validator.js +12 -11
  93. package/_esm/tempo/actions/validator.js.map +1 -1
  94. package/_esm/tempo/actions/virtualAddress.js +3 -2
  95. package/_esm/tempo/actions/virtualAddress.js.map +1 -1
  96. package/_esm/tempo/actions/zone.js +12 -11
  97. package/_esm/tempo/actions/zone.js.map +1 -1
  98. package/_esm/tempo/errors.js +63 -0
  99. package/_esm/tempo/errors.js.map +1 -1
  100. package/_esm/tempo/internal/errors.js +51 -0
  101. package/_esm/tempo/internal/errors.js.map +1 -0
  102. package/_esm/tempo/internal/resolveError.js +130 -0
  103. package/_esm/tempo/internal/resolveError.js.map +1 -0
  104. package/_esm/tempo/internal/utils.js +2 -0
  105. package/_esm/tempo/internal/utils.js.map +1 -1
  106. package/_types/actions/wallet/prepareTransactionRequest.d.ts.map +1 -1
  107. package/_types/errors/version.d.ts +1 -1
  108. package/_types/tempo/Abis.d.ts +807 -0
  109. package/_types/tempo/Abis.d.ts.map +1 -1
  110. package/_types/tempo/Addresses.d.ts +1 -0
  111. package/_types/tempo/Addresses.d.ts.map +1 -1
  112. package/_types/tempo/Decorator.d.ts +1 -1
  113. package/_types/tempo/Decorator.d.ts.map +1 -1
  114. package/_types/tempo/ExecutionError.d.ts +3 -1
  115. package/_types/tempo/ExecutionError.d.ts.map +1 -1
  116. package/_types/tempo/actions/accessKey.d.ts +155 -8
  117. package/_types/tempo/actions/accessKey.d.ts.map +1 -1
  118. package/_types/tempo/actions/accounts.d.ts +3 -1
  119. package/_types/tempo/actions/accounts.d.ts.map +1 -1
  120. package/_types/tempo/actions/amm.d.ts +39 -0
  121. package/_types/tempo/actions/amm.d.ts.map +1 -1
  122. package/_types/tempo/actions/channel.d.ts +77 -7
  123. package/_types/tempo/actions/channel.d.ts.map +1 -1
  124. package/_types/tempo/actions/dex.d.ts +222 -16
  125. package/_types/tempo/actions/dex.d.ts.map +1 -1
  126. package/_types/tempo/actions/earn/deployment.d.ts +7 -6
  127. package/_types/tempo/actions/earn/deployment.d.ts.map +1 -1
  128. package/_types/tempo/actions/earn.d.ts +15 -14
  129. package/_types/tempo/actions/earn.d.ts.map +1 -1
  130. package/_types/tempo/actions/fee.d.ts +32 -6
  131. package/_types/tempo/actions/fee.d.ts.map +1 -1
  132. package/_types/tempo/actions/nonce.d.ts +35 -0
  133. package/_types/tempo/actions/nonce.d.ts.map +1 -1
  134. package/_types/tempo/actions/policy.d.ts +29 -8
  135. package/_types/tempo/actions/policy.d.ts.map +1 -1
  136. package/_types/tempo/actions/propAmm.d.ts +9 -1
  137. package/_types/tempo/actions/propAmm.d.ts.map +1 -1
  138. package/_types/tempo/actions/receivePolicy.d.ts +10 -6
  139. package/_types/tempo/actions/receivePolicy.d.ts.map +1 -1
  140. package/_types/tempo/actions/reward.d.ts +7 -3
  141. package/_types/tempo/actions/reward.d.ts.map +1 -1
  142. package/_types/tempo/actions/token.d.ts +200 -32
  143. package/_types/tempo/actions/token.d.ts.map +1 -1
  144. package/_types/tempo/actions/validator.d.ts +17 -5
  145. package/_types/tempo/actions/validator.d.ts.map +1 -1
  146. package/_types/tempo/actions/virtualAddress.d.ts +4 -3
  147. package/_types/tempo/actions/virtualAddress.d.ts.map +1 -1
  148. package/_types/tempo/actions/zone.d.ts +10 -9
  149. package/_types/tempo/actions/zone.d.ts.map +1 -1
  150. package/_types/tempo/errors.d.ts +63 -1
  151. package/_types/tempo/errors.d.ts.map +1 -1
  152. package/_types/tempo/internal/errors.d.ts +59 -0
  153. package/_types/tempo/internal/errors.d.ts.map +1 -0
  154. package/_types/tempo/internal/relay/utils.d.ts +16 -16
  155. package/_types/tempo/internal/resolveError.d.ts +8 -0
  156. package/_types/tempo/internal/resolveError.d.ts.map +1 -0
  157. package/_types/tempo/internal/utils.d.ts.map +1 -1
  158. package/actions/wallet/prepareTransactionRequest.ts +11 -0
  159. package/errors/version.ts +1 -1
  160. package/package.json +1 -1
  161. package/tempo/Abis.ts +298 -0
  162. package/tempo/Addresses.ts +1 -0
  163. package/tempo/Decorator.ts +5 -3
  164. package/tempo/ExecutionError.ts +101 -3
  165. package/tempo/actions/accessKey.ts +496 -25
  166. package/tempo/actions/accounts.ts +7 -3
  167. package/tempo/actions/amm.ts +126 -4
  168. package/tempo/actions/channel.ts +248 -14
  169. package/tempo/actions/dex.ts +599 -33
  170. package/tempo/actions/earn/deployment.ts +11 -9
  171. package/tempo/actions/earn.ts +40 -35
  172. package/tempo/actions/fee.ts +111 -9
  173. package/tempo/actions/nonce.ts +86 -1
  174. package/tempo/actions/policy.ts +66 -14
  175. package/tempo/actions/propAmm.ts +36 -9
  176. package/tempo/actions/receivePolicy.ts +26 -15
  177. package/tempo/actions/reward.ts +17 -9
  178. package/tempo/actions/token.ts +519 -54
  179. package/tempo/actions/validator.ts +40 -16
  180. package/tempo/actions/virtualAddress.ts +7 -5
  181. package/tempo/actions/zone.ts +25 -20
  182. package/tempo/errors.ts +114 -1
  183. package/tempo/internal/errors.ts +110 -0
  184. package/tempo/internal/resolveError.ts +169 -0
  185. package/tempo/internal/utils.ts +2 -0
@@ -0,0 +1,110 @@
1
+ import type { Address } from 'abitype'
2
+ import { TokenId } from 'ox/tempo'
3
+ import type {
4
+ MulticallParameters,
5
+ MulticallReturnType,
6
+ } from '../../actions/public/multicall.js'
7
+ import type { Client } from '../../clients/createClient.js'
8
+ import { formatUnits } from '../../utils/unit/formatUnits.js'
9
+ import type * as ExecutionError from '../ExecutionError.js'
10
+ import type { TempoExecutionError } from '../errors.js'
11
+
12
+ /** Action-specific steps to resolve errors, keyed by error name. Listed before the default steps. */
13
+ export type Hints = {
14
+ [name in TempoExecutionError.ErrorName]?: readonly string[]
15
+ }
16
+
17
+ /** Builds a specific error for a decoded Tempo error, or returns `undefined` when it does not apply. */
18
+ type Resolver = (
19
+ context: Context,
20
+ ) => Promise<TempoExecutionError | undefined> | TempoExecutionError | undefined
21
+
22
+ /** Resolvers keyed by error signature. */
23
+ export type Resolvers = {
24
+ [signature in keyof typeof ExecutionError.messages]?: Resolver
25
+ }
26
+
27
+ export type Context = {
28
+ /** Parameters for the base `TempoExecutionError`. */
29
+ base: TempoExecutionError.Parameters
30
+ client: Client
31
+ /** Signer, with its access key when signing with one. */
32
+ signer: { address: Address; accessKey: Address | undefined } | undefined
33
+ /** Contract call that failed, when the action made one. */
34
+ call:
35
+ | {
36
+ address: Address
37
+ args: readonly unknown[]
38
+ functionName: string
39
+ /** Reads an argument by its ABI input name. */
40
+ arg: <type = unknown>(name: string) => type | undefined
41
+ }
42
+ | undefined
43
+ /** Whether the error came from transaction validation or fee payment rather than a revert. */
44
+ validation: boolean
45
+ /** Raw node message. */
46
+ details: string
47
+ parameters: Record<string, any>
48
+ /** Multicall with `allowFailure: true`. Resolves an empty array when the request fails. */
49
+ read: <const contracts extends readonly unknown[]>(
50
+ contracts: MulticallParameters<contracts>['contracts'],
51
+ ) => Promise<Partial<MulticallReturnType<contracts, true>>>
52
+ }
53
+
54
+ /**
55
+ * Wraps an action so failures with a decoded Tempo error throw a {@link TempoExecutionError}.
56
+ * Other errors are rethrown unchanged.
57
+ *
58
+ * @internal
59
+ */
60
+ export function withExecutionError<
61
+ action extends (client: any, parameters: any) => Promise<any>,
62
+ >(action: action, options: { hints?: Hints | undefined } = {}): action {
63
+ return (async (client: Client, parameters: Record<string, any>) => {
64
+ try {
65
+ return await action(client, parameters)
66
+ } catch (error) {
67
+ // Loaded on failure only, so resolvers and error tables stay out of the success path.
68
+ const { toExecutionError } = await import('./resolveError.js')
69
+ throw await toExecutionError(error, { ...options, client, parameters })
70
+ }
71
+ }) as action
72
+ }
73
+
74
+ /** Fee token from the action parameters or chain, when set. */
75
+ export function getFeeToken(context: Context) {
76
+ const feeToken =
77
+ context.parameters.feeToken ??
78
+ (
79
+ context.client.chain as
80
+ | { feeToken?: TokenId.TokenIdOrAddress }
81
+ | undefined
82
+ )?.feeToken
83
+ return feeToken === undefined || feeToken === null
84
+ ? undefined
85
+ : TokenId.toAddress(feeToken)
86
+ }
87
+
88
+ /** Whether `address` is a TIP-20 token. */
89
+ export function isTip20(address: Address | undefined): address is Address {
90
+ return !!address?.toLowerCase().startsWith('0x20c0')
91
+ }
92
+
93
+ /** Formats an amount as `formatted (raw)` when decimals are known. */
94
+ export function formatAmount(
95
+ value: bigint | undefined,
96
+ decimals: number | undefined,
97
+ ) {
98
+ if (value === undefined) return undefined
99
+ if (decimals === undefined) return value.toString()
100
+ return `${formatUnits(value, decimals)} (${value})`
101
+ }
102
+
103
+ /** Formats Unix seconds as an ISO 8601 string. */
104
+ export function formatTime(seconds: bigint | number | undefined) {
105
+ if (!seconds) return undefined
106
+ const milliseconds = Number(seconds) * 1000
107
+ // `Date` cannot represent times past ±8.64e15 milliseconds.
108
+ if (!(Math.abs(milliseconds) <= 8.64e15)) return seconds.toString()
109
+ return new Date(milliseconds).toISOString()
110
+ }
@@ -0,0 +1,169 @@
1
+ import type { Abi, Address } from 'abitype'
2
+ import { parseAccount } from '../../accounts/utils/parseAccount.js'
3
+ import { call } from '../../actions/public/call.js'
4
+ import { multicall } from '../../actions/public/multicall.js'
5
+ import type { Client } from '../../clients/createClient.js'
6
+ import { BaseError } from '../../errors/base.js'
7
+ import { TransactionReceiptRevertedError } from '../../errors/transaction.js'
8
+ import type { ContractFunctionParameters } from '../../types/contract.js'
9
+ import { encodeFunctionData } from '../../utils/abi/encodeFunctionData.js'
10
+ import { formatAbiItem } from '../../utils/abi/formatAbiItem.js'
11
+ import { toFunctionSelector } from '../../utils/hash/toFunctionSelector.js'
12
+ import { errorResolvers as accessKeyResolvers } from '../actions/accessKey.js'
13
+ import { errorResolvers as ammResolvers } from '../actions/amm.js'
14
+ import { errorResolvers as channelResolvers } from '../actions/channel.js'
15
+ import { errorResolvers as dexResolvers } from '../actions/dex.js'
16
+ import { errorResolvers as feeResolvers } from '../actions/fee.js'
17
+ import { errorResolvers as nonceResolvers } from '../actions/nonce.js'
18
+ import { errorResolvers as policyResolvers } from '../actions/policy.js'
19
+ import { errorResolvers as tokenResolvers } from '../actions/token.js'
20
+ import * as ExecutionError from '../ExecutionError.js'
21
+ import { TempoExecutionError } from '../errors.js'
22
+ import type { Context, Hints, Resolvers } from './errors.js'
23
+
24
+ /** Node messages that carry no revert data, mapped to the error they describe. */
25
+ const messages = [
26
+ [/Access key expired: expiry \d+ <= min allowed \d+/, 'ExpiryInPast()'],
27
+ ] as const
28
+
29
+ export async function toExecutionError(
30
+ cause: unknown,
31
+ options: {
32
+ client: Client
33
+ hints?: Hints | undefined
34
+ parameters: Record<string, any>
35
+ },
36
+ ) {
37
+ if (!(cause instanceof BaseError)) return cause
38
+ const { client, parameters } = options
39
+ const source = (await replay(cause, client, parameters)) ?? cause
40
+ const details = source.details ?? ''
41
+ let error: ExecutionError.ExecutionError = ExecutionError.from(source)
42
+ if (error.errorName === 'unknown') {
43
+ const signature = messages.find(([pattern]) => pattern.test(details))?.[1]
44
+ if (!signature) return cause
45
+ error = ExecutionError.from(toFunctionSelector(signature))
46
+ }
47
+ if (error.errorName === 'unknown') return cause
48
+
49
+ const signature = formatAbiItem(
50
+ error.abiItem,
51
+ ) as keyof typeof ExecutionError.messages
52
+ // Solidity `Error(string)` and `Panic(uint256)` are not Tempo errors.
53
+ if (!(signature in ExecutionError.messages)) return cause
54
+ const hints = [
55
+ ...(options.hints?.[error.errorName] ?? []),
56
+ ...(ExecutionError.hints[signature] ?? []),
57
+ ]
58
+ const base = { cause, error, hints } as TempoExecutionError.Parameters
59
+
60
+ const account_ = parameters.account ?? client.account
61
+ const account = account_ ? parseAccount(account_) : undefined
62
+ const context: Context = {
63
+ base,
64
+ call: getCall(parameters),
65
+ client,
66
+ details,
67
+ parameters,
68
+ read: (contracts) =>
69
+ multicall(client, {
70
+ allowFailure: true,
71
+ contracts: contracts as readonly ContractFunctionParameters[],
72
+ deployless: true,
73
+ }).catch(() => []) as never,
74
+ signer: account && {
75
+ address: account.address,
76
+ accessKey:
77
+ account.source === 'accessKey' && 'accessKeyAddress' in account
78
+ ? (account.accessKeyAddress as Address)
79
+ : undefined,
80
+ },
81
+ validation: !hasRevertData(source),
82
+ }
83
+
84
+ for (const resolvers of [
85
+ accessKeyResolvers,
86
+ ammResolvers,
87
+ channelResolvers,
88
+ dexResolvers,
89
+ feeResolvers,
90
+ nonceResolvers,
91
+ policyResolvers,
92
+ tokenResolvers,
93
+ ] as readonly Resolvers[]) {
94
+ const result = await resolvers[signature]?.(context)
95
+ if (result) return result
96
+ }
97
+ return new TempoExecutionError(base)
98
+ }
99
+
100
+ /** Replays a call whose receipt reverted, to recover its revert data. */
101
+ async function replay(
102
+ cause: BaseError,
103
+ client: Client,
104
+ parameters: Record<string, any>,
105
+ ) {
106
+ const reverted = cause.walk(
107
+ (error) => error instanceof TransactionReceiptRevertedError,
108
+ ) as TransactionReceiptRevertedError | null
109
+ const to = parameters.to ?? parameters.address
110
+ const data =
111
+ parameters.data ??
112
+ (parameters.abi && parameters.functionName
113
+ ? encodeFunctionData({
114
+ abi: parameters.abi,
115
+ args: parameters.args,
116
+ functionName: parameters.functionName,
117
+ })
118
+ : undefined)
119
+ if (!reverted || !to || !data) return undefined
120
+ // ponytail: replays on the parent block, so earlier transactions in the same block are not applied.
121
+ return call(client, {
122
+ account: parameters.account ?? client.account,
123
+ blockNumber: reverted.receipt.blockNumber - 1n,
124
+ data,
125
+ to,
126
+ value: parameters.value,
127
+ }).then(
128
+ () => undefined,
129
+ (error) => (error instanceof BaseError ? error : undefined),
130
+ )
131
+ }
132
+
133
+ /** Whether an error carries revert data, which only execution reverts have. */
134
+ function hasRevertData(error: BaseError) {
135
+ return !!error.walk((error) => {
136
+ const { data, raw } = error as { data?: unknown; raw?: unknown }
137
+ const hex =
138
+ typeof data === 'object' && data !== null
139
+ ? (data as { data?: unknown }).data
140
+ : (data ?? raw)
141
+ return typeof hex === 'string' && hex.length >= 10
142
+ })
143
+ }
144
+
145
+ function getCall(parameters: Record<string, any>): Context['call'] {
146
+ const {
147
+ abi,
148
+ address,
149
+ args = [],
150
+ functionName,
151
+ } = parameters as {
152
+ abi?: Abi
153
+ address?: Address
154
+ args?: readonly unknown[]
155
+ functionName?: string
156
+ }
157
+ if (!address || !functionName) return undefined
158
+ const item = abi?.find(
159
+ (item) => item.type === 'function' && item.name === functionName,
160
+ )
161
+ const inputs = item?.type === 'function' ? item.inputs : []
162
+ return {
163
+ address,
164
+ args,
165
+ functionName,
166
+ arg: (name) =>
167
+ args[inputs.findIndex((input) => input.name === name)] as never,
168
+ }
169
+ }
@@ -279,6 +279,8 @@ export function defineCall<
279
279
  } {
280
280
  return {
281
281
  ...(call as any),
282
+ // Precompile errors bubble up across contracts (e.g. `AccountKeychain` errors from a TIP-20 `transfer`).
283
+ abi: [...call.abi, ...Abis.coreErrors],
282
284
  data: encodeFunctionData(call as never),
283
285
  to: call.address,
284
286
  } as const