-
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathcontract.tsx
More file actions
414 lines (378 loc) · 10.5 KB
/
Copy pathcontract.tsx
File metadata and controls
414 lines (378 loc) · 10.5 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import type { ContractParams, ContractResult } from '@tevm/actions'
import { option } from 'pastel'
import { z } from 'zod'
import CliAction from '../components/CliAction.js'
import { envVar, useAction } from '../hooks/useAction.js'
// Add command description for help output
export const description = 'Call a contract function with ABI and arguments'
// Options definitions and descriptions
const optionDescriptions = {
to: 'Contract address to call (env: TEVM_TO)',
rpc: 'RPC endpoint (env: TEVM_RPC)',
abi: 'Contract ABI (JSON string or path to file) (env: TEVM_ABI)',
functionName: 'Function name to call (env: TEVM_FUNCTION_NAME)',
args: 'Arguments for the function call (JSON string array) (env: TEVM_ARGS)',
from: 'Address to send the transaction from (env: TEVM_FROM)',
value: 'ETH value to send in wei (env: TEVM_VALUE)',
deployedBytecode: 'Deployed bytecode to put in state before call (env: TEVM_DEPLOYED_BYTECODE)',
code: 'Alias for deployedBytecode (env: TEVM_CODE)',
gas: 'Gas limit for the transaction (env: TEVM_GAS)',
gasPrice: 'Gas price in wei (env: TEVM_GAS_PRICE)',
maxFeePerGas: 'Maximum fee per gas (EIP-1559) (env: TEVM_MAX_FEE_PER_GAS)',
maxPriorityFeePerGas: 'Maximum priority fee per gas (EIP-1559) (env: TEVM_MAX_PRIORITY_FEE_PER_GAS)',
gasRefund: 'Gas refund counter (env: TEVM_GAS_REFUND)',
blockTag: 'Block tag (latest, pending, etc.) or number (env: TEVM_BLOCK_TAG)',
caller: 'Address that ran this code (msg.sender) (env: TEVM_CALLER)',
origin: 'Address where the call originated from (env: TEVM_ORIGIN)',
depth: 'Depth of EVM call stack (env: TEVM_DEPTH)',
skipBalance: 'Skip balance check (env: TEVM_SKIP_BALANCE)',
createTrace: 'Return a complete trace with the call (env: TEVM_CREATE_TRACE)',
createAccessList: 'Return an access list mapping of addresses to storage keys (env: TEVM_CREATE_ACCESS_LIST)',
createTransaction: 'Whether to update state (on-success, always, never) (env: TEVM_CREATE_TRANSACTION)',
}
// Empty args tuple
export const args = z.tuple([])
export const options = z.object({
// Contract address
to: z
.string()
.optional()
.describe(
option({
description: optionDescriptions.to,
}),
),
// Interactive mode flag (run directly without interactive editor)
run: z
.boolean()
.default(false)
.describe(
option({
description: 'Run directly without interactive parameter editing (env: TEVM_RUN)',
alias: 'r',
}),
),
// Transport options
rpc: z
.string()
.default(envVar('rpc') || 'http://localhost:8545')
.describe(
option({
description: optionDescriptions.rpc,
defaultValueDescription: 'http://localhost:8545',
}),
),
// Contract parameters
abi: z.string().describe(
option({
description: optionDescriptions.abi,
}),
),
functionName: z.string().describe(
option({
description: optionDescriptions.functionName,
}),
),
args: z
.string()
.optional()
.describe(
option({
description: optionDescriptions.args,
}),
),
// Basic call parameters
from: z
.string()
.default(envVar('from') || '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266')
.describe(
option({
description: optionDescriptions.from,
defaultValueDescription: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
}),
),
value: z
.string()
.optional()
.describe(
option({
description: optionDescriptions.value,
}),
),
// Contract code options
deployedBytecode: z
.string()
.optional()
.describe(
option({
description: optionDescriptions.deployedBytecode,
}),
),
code: z
.string()
.optional()
.describe(
option({
description: optionDescriptions.code,
}),
),
// Gas parameters
gas: z
.string()
.optional()
.describe(
option({
description: optionDescriptions.gas,
}),
),
gasPrice: z
.string()
.optional()
.describe(
option({
description: optionDescriptions.gasPrice,
}),
),
maxFeePerGas: z
.string()
.optional()
.describe(
option({
description: optionDescriptions.maxFeePerGas,
}),
),
maxPriorityFeePerGas: z
.string()
.optional()
.describe(
option({
description: optionDescriptions.maxPriorityFeePerGas,
}),
),
gasRefund: z
.string()
.optional()
.describe(
option({
description: optionDescriptions.gasRefund,
}),
),
// Block options
blockTag: z
.string()
.optional()
.describe(
option({
description: optionDescriptions.blockTag,
}),
),
// Advanced options
caller: z
.string()
.optional()
.describe(
option({
description: optionDescriptions.caller,
}),
),
origin: z
.string()
.optional()
.describe(
option({
description: optionDescriptions.origin,
}),
),
depth: z
.number()
.optional()
.describe(
option({
description: optionDescriptions.depth,
}),
),
skipBalance: z
.boolean()
.optional()
.describe(
option({
description: optionDescriptions.skipBalance,
}),
),
// Instrumentation options
createTrace: z
.boolean()
.default(false)
.describe(
option({
description: optionDescriptions.createTrace,
}),
),
createAccessList: z
.boolean()
.default(false)
.describe(
option({
description: optionDescriptions.createAccessList,
}),
),
createTransaction: z
.enum(['on-success', 'always', 'never'])
.default('never')
.describe(
option({
description: optionDescriptions.createTransaction,
}),
),
// Output formatting
formatJson: z
.boolean()
.default(envVar('format_json') !== 'false')
.describe(
option({
description: 'Format output as JSON (env: TEVM_FORMAT_JSON)',
defaultValueDescription: 'true',
}),
),
})
type Props = {
args: z.infer<typeof args>
options: z.infer<typeof options>
}
// Default values for all parameters
const defaultValues: Record<string, any> = {
from: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
gas: BigInt(10000000), // 10 million gas
gasPrice: BigInt(1000000000), // 1 gwei
maxFeePerGas: BigInt(2000000000), // 2 gwei
maxPriorityFeePerGas: BigInt(1000000000), // 1 gwei
gasRefund: BigInt(0),
createTrace: false,
createAccessList: false,
skipBalance: false,
createTransaction: 'never',
rpc: 'http://localhost:8545',
value: BigInt(0),
blockTag: 'latest',
depth: 0,
}
// Helper function to parse ABI from string (either JSON or file path)
const parseAbi = (abiString: string) => {
try {
// Try parsing as JSON first
return JSON.parse(abiString)
} catch (_e) {
// If JSON parsing fails, try reading from file (would require fs module in Node.js)
// For browser CLI, we'd need to handle this differently
throw new Error('ABI must be a valid JSON string. File reading not supported in this context.')
}
}
// Helper function to parse function arguments
const parseArgs = (argsString?: string) => {
if (!argsString) return []
try {
return JSON.parse(argsString)
} catch (_e) {
throw new Error('Arguments must be a valid JSON array string')
}
}
export default function Contract({ options }: Props) {
// Use the action hook with inlined createParams and executeAction
const actionResult = useAction<ContractParams, ContractResult>({
actionName: 'contract',
options,
defaultValues,
optionDescriptions,
// Inlined createParams function
createParams: (enhancedOptions: Record<string, any>): ContractParams => {
const abi = parseAbi(enhancedOptions['abi'])
const functionName = enhancedOptions['functionName']
const args = parseArgs(enhancedOptions['args'])
if (!abi) {
throw new Error('ABI is required')
}
if (!functionName) {
throw new Error('Function name is required')
}
// Handle contract address and code options
const to = enhancedOptions['to'] as `0x${string}` | undefined
const code = enhancedOptions['code'] as `0x${string}` | undefined
const deployedBytecode = enhancedOptions['deployedBytecode'] as `0x${string}` | undefined
if (!to && !code && !deployedBytecode) {
throw new Error('Either to address, code, or deployedBytecode must be provided')
}
// Basic parameters that are the same for all variants
const baseParams = {
// Contract parameters
abi,
functionName,
args,
// Basic parameters
from: enhancedOptions['from'] ?? undefined,
value: enhancedOptions['value'] ? BigInt(enhancedOptions['value']) : undefined,
// Gas parameters
gas: enhancedOptions['gas'] ? BigInt(enhancedOptions['gas']) : undefined,
gasPrice: enhancedOptions['gasPrice'] ? BigInt(enhancedOptions['gasPrice']) : undefined,
maxFeePerGas: enhancedOptions['maxFeePerGas'] ? BigInt(enhancedOptions['maxFeePerGas']) : undefined,
maxPriorityFeePerGas: enhancedOptions['maxPriorityFeePerGas']
? BigInt(enhancedOptions['maxPriorityFeePerGas'])
: undefined,
gasRefund: enhancedOptions['gasRefund'] ? BigInt(enhancedOptions['gasRefund']) : undefined,
// Block options
blockTag: enhancedOptions['blockTag'] ?? undefined,
// Advanced options
caller: enhancedOptions['caller'] ?? undefined,
origin: enhancedOptions['origin'] ?? undefined,
depth:
typeof enhancedOptions['depth'] === 'bigint' ? Number(enhancedOptions['depth']) : enhancedOptions['depth'],
skipBalance: enhancedOptions['skipBalance'] ?? undefined,
// Instrumentation options
createTrace: enhancedOptions['createTrace'] ?? undefined,
createAccessList: enhancedOptions['createAccessList'] ?? undefined,
createTransaction: enhancedOptions['createTransaction'] ?? undefined,
}
// Handle the union types according to ContractParams definition
// Case 1: to is required, deployedBytecode and code are optional
if (to) {
return {
...baseParams,
to,
deployedBytecode,
code,
} as ContractParams
}
// Case 2: code is required, to and deployedBytecode are optional
if (code) {
return {
...baseParams,
to,
code,
deployedBytecode,
} as ContractParams
}
// Case 3: deployedBytecode is required, to and code are optional
if (deployedBytecode) {
return {
...baseParams,
to,
deployedBytecode,
code,
} as ContractParams
}
// This should never happen due to the earlier check
throw new Error('Either to address, code, or deployedBytecode must be provided')
},
// Inlined executeAction function
executeAction: async (client: any, params: ContractParams): Promise<ContractResult> => {
return await client.tevmContract(params)
},
})
// Render the action UI
return (
<CliAction
{...actionResult}
targetName={`contract ${actionResult.options['to'] || 'code'} function ${actionResult.options['functionName']}`}
successMessage="Contract function called successfully!"
/>
)
}