-
-
Notifications
You must be signed in to change notification settings - Fork 968
/
Copy pathcontract.ts
318 lines (297 loc) · 9.29 KB
/
contract.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import type { Abi, Address } from 'abitype'
import { parseAccount } from '../accounts/utils/parseAccount.js'
import type { CallParameters } from '../actions/public/call.js'
import { panicReasons } from '../constants/solidity.js'
import type { Chain } from '../types/chain.js'
import type { Hex } from '../types/misc.js'
import {
type DecodeErrorResultReturnType,
decodeErrorResult,
} from '../utils/abi/decodeErrorResult.js'
import { formatAbiItem } from '../utils/abi/formatAbiItem.js'
import { formatAbiItemWithArgs } from '../utils/abi/formatAbiItemWithArgs.js'
import { getAbiItem } from '../utils/abi/getAbiItem.js'
import { formatEther } from '../utils/unit/formatEther.js'
import { formatGwei } from '../utils/unit/formatGwei.js'
import { AbiErrorSignatureNotFoundError } from './abi.js'
import { BaseError } from './base.js'
import { prettyStateOverride } from './stateOverride.js'
import { prettyPrint } from './transaction.js'
import { getContractAddress } from './utils.js'
export type CallExecutionErrorType = CallExecutionError & {
name: 'CallExecutionError'
}
export class CallExecutionError extends BaseError {
override cause: BaseError
constructor(
cause: BaseError,
{
account: account_,
docsPath,
chain,
data,
gas,
gasPrice,
maxFeePerGas,
maxPriorityFeePerGas,
nonce,
to,
value,
stateOverride,
}: CallParameters & {
chain?: Chain | undefined
docsPath?: string | undefined
},
) {
const account = account_ ? parseAccount(account_) : undefined
let prettyArgs = prettyPrint({
from: account?.address,
to,
value:
typeof value !== 'undefined' &&
`${formatEther(value)} ${chain?.nativeCurrency?.symbol || 'ETH'}`,
data,
gas,
gasPrice:
typeof gasPrice !== 'undefined' && `${formatGwei(gasPrice)} gwei`,
maxFeePerGas:
typeof maxFeePerGas !== 'undefined' &&
`${formatGwei(maxFeePerGas)} gwei`,
maxPriorityFeePerGas:
typeof maxPriorityFeePerGas !== 'undefined' &&
`${formatGwei(maxPriorityFeePerGas)} gwei`,
nonce,
})
if (stateOverride) {
prettyArgs += `\n${prettyStateOverride(stateOverride)}`
}
super(cause.shortMessage, {
cause,
docsPath,
metaMessages: [
...(cause.metaMessages ? [...cause.metaMessages, ' '] : []),
'Raw Call Arguments:',
prettyArgs,
].filter(Boolean) as string[],
name: 'CallExecutionError',
})
this.cause = cause
}
}
export type ContractFunctionExecutionErrorType =
ContractFunctionExecutionError & {
name: 'ContractFunctionExecutionError'
}
export class ContractFunctionExecutionError extends BaseError {
abi: Abi
args?: unknown[] | undefined
override cause: BaseError
contractAddress?: Address | undefined
formattedArgs?: string | undefined
functionName: string
sender?: Address | undefined
constructor(
cause: BaseError,
{
abi,
args,
contractAddress,
docsPath,
functionName,
sender,
}: {
abi: Abi
args?: any | undefined
contractAddress?: Address | undefined
docsPath?: string | undefined
functionName: string
sender?: Address | undefined
},
) {
const abiItem = getAbiItem({ abi, args, name: functionName })
const formattedArgs = abiItem
? formatAbiItemWithArgs({
abiItem,
args,
includeFunctionName: false,
includeName: false,
})
: undefined
const functionWithParams = abiItem
? formatAbiItem(abiItem, { includeName: true })
: undefined
const prettyArgs = prettyPrint({
address: contractAddress && getContractAddress(contractAddress),
function: functionWithParams,
args:
formattedArgs &&
formattedArgs !== '()' &&
`${[...Array(functionName?.length ?? 0).keys()]
.map(() => ' ')
.join('')}${formattedArgs}`,
sender,
})
super(
cause.shortMessage ||
`An unknown error occurred while executing the contract function "${functionName}".`,
{
cause,
docsPath,
metaMessages: [
...(cause.metaMessages ? [...cause.metaMessages, ' '] : []),
prettyArgs && 'Contract Call:',
prettyArgs,
].filter(Boolean) as string[],
name: 'ContractFunctionExecutionError',
},
)
this.abi = abi
this.args = args
this.cause = cause
this.contractAddress = contractAddress
this.functionName = functionName
this.sender = sender
}
}
export type ContractFunctionRevertedErrorType =
ContractFunctionRevertedError & {
name: 'ContractFunctionRevertedError'
}
export class ContractFunctionRevertedError extends BaseError {
data?: DecodeErrorResultReturnType | undefined
reason?: string | undefined
signature?: Hex | undefined
constructor({
abi,
data,
functionName,
message,
}: {
abi: Abi
data?: Hex | undefined
functionName: string
message?: string | undefined
}) {
let cause: Error | undefined
let decodedData: DecodeErrorResultReturnType | undefined = undefined
let metaMessages: string[] | undefined
let reason: string | undefined
if (data && data !== '0x') {
try {
decodedData = decodeErrorResult({ abi, data })
const { abiItem, errorName, args: errorArgs } = decodedData
if (errorName === 'Error') {
reason = (errorArgs as [string])[0]
} else if (errorName === 'Panic') {
const [firstArg] = errorArgs as [number]
reason = panicReasons[firstArg as keyof typeof panicReasons]
} else {
const errorWithParams = abiItem
? formatAbiItem(abiItem, { includeName: true })
: undefined
const formattedArgs =
abiItem && errorArgs
? formatAbiItemWithArgs({
abiItem,
args: errorArgs,
includeFunctionName: false,
includeName: false,
})
: undefined
metaMessages = [
errorWithParams ? `Error: ${errorWithParams}` : '',
formattedArgs && formattedArgs !== '()'
? ` ${[...Array(errorName?.length ?? 0).keys()]
.map(() => ' ')
.join('')}${formattedArgs}`
: '',
]
}
} catch (err) {
cause = err as Error
}
} else if (message) reason = message
let signature: Hex | undefined
if (cause instanceof AbiErrorSignatureNotFoundError) {
signature = cause.signature
metaMessages = [
`Unable to decode signature "${signature}" as it was not found on the provided ABI.`,
'Make sure you are using the correct ABI and that the error exists on it.',
`You can look up the decoded signature here: https://openchain.xyz/signatures?query=${signature}.`,
]
}
super(
(reason && reason !== 'execution reverted') || signature
? [
`The contract function "${functionName}" reverted with the following ${
signature ? 'signature' : 'reason'
}:`,
reason || signature,
].join('\n')
: `The contract function "${functionName}" reverted.`,
{
cause,
metaMessages,
name: 'ContractFunctionRevertedError',
},
)
this.data = decodedData
this.reason = reason
this.signature = signature
}
}
export type ContractFunctionZeroDataErrorType =
ContractFunctionZeroDataError & {
name: 'ContractFunctionZeroDataError'
}
export class ContractFunctionZeroDataError extends BaseError {
constructor({ functionName }: { functionName: string }) {
super(`The contract function "${functionName}" returned no data ("0x").`, {
metaMessages: [
'This could be due to any of the following:',
` - The contract does not have the function "${functionName}",`,
' - The parameters passed to the contract function may be invalid, or',
' - The address is not a contract.',
],
name: 'ContractFunctionZeroDataError',
})
}
}
export type CounterfactualDeploymentFailedErrorType =
CounterfactualDeploymentFailedError & {
name: 'CounterfactualDeploymentFailedError'
}
export class CounterfactualDeploymentFailedError extends BaseError {
constructor({ factory }: { factory?: Address | undefined }) {
super(
`Deployment for counterfactual contract call failed${
factory ? ` for factory "${factory}".` : ''
}`,
{
metaMessages: [
'Please ensure:',
'- The `factory` is a valid contract deployment factory (ie. Create2 Factory, ERC-4337 Factory, etc).',
'- The `factoryData` is a valid encoded function call for contract deployment function on the factory.',
],
name: 'CounterfactualDeploymentFailedError',
},
)
}
}
export type RawContractErrorType = RawContractError & {
name: 'RawContractError'
}
export class RawContractError extends BaseError {
code = 3
data?: Hex | { data?: Hex | undefined } | undefined
constructor({
data,
message,
}: {
data?: Hex | { data?: Hex | undefined } | undefined
message?: string | undefined
}) {
super(message || '', { name: 'RawContractError' })
this.data = data
}
}