import { readFileSync } from 'node:fs' import { option } from 'pastel' import { z } from 'zod' import CliAction from '../components/CliAction.js' import { useAction } from '../hooks/useAction.js' // Add command description for help output export const description = 'Read several contracts in one request\nExample: tevm multicall --contracts ./calls.json --rpc https://mainnet.optimism.io --run --json' // Options definitions and descriptions const optionDescriptions = { contracts: 'Array of contract calls in JSON format (env: TEVM_CONTRACTS)', multicallAddress: 'Address of the multicall contract (env: TEVM_MULTICALL_ADDRESS)', allowFailure: 'Whether to allow calls to fail (env: TEVM_ALLOW_FAILURE)', blockTag: 'Block tag (latest, pending, etc.) (env: TEVM_BLOCK_TAG)', blockNumber: 'Block number to execute at (env: TEVM_BLOCK_NUMBER)', rpc: 'RPC endpoint (env: TEVM_RPC)', } // Define a fallback ERC20 ABI const fallbackERC20Abi = [ { inputs: [], name: 'totalSupply', outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], stateMutability: 'view', type: 'function', }, { inputs: [{ internalType: 'address', name: 'account', type: 'address' }], name: 'balanceOf', outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], stateMutability: 'view', type: 'function', }, ] // Empty args tuple export const args = z.tuple([]) export const options = z.object({ // ALL PARAMETERS OPTIONAL contracts: z .string() .optional() .describe( option({ description: optionDescriptions.contracts, }), ), multicallAddress: z .string() .optional() .describe( option({ description: optionDescriptions.multicallAddress, }), ), allowFailure: z .boolean() .optional() .describe( option({ description: optionDescriptions.allowFailure, }), ), blockTag: z .string() .optional() .describe( option({ description: optionDescriptions.blockTag, }), ), blockNumber: z .string() .optional() .describe( option({ description: optionDescriptions.blockNumber, }), ), // Interactive mode flag run: z .boolean() .default(false) .describe( option({ description: 'Run directly without interactive parameter editing (env: TEVM_RUN)', alias: 'r', }), ), // Transport options rpc: z .string() .optional() .describe( option({ description: optionDescriptions.rpc, defaultValueDescription: 'http://localhost:8545', }), ), // Output formatting json: z .boolean() .optional() .describe( option({ description: 'Emit the stable machine-readable JSON envelope (env: TEVM_JSON)', defaultValueDescription: 'false', }), ), }) type Props = { args: z.infer options: z.infer } // COMPREHENSIVE DEFAULTS const defaultValues: Record = { contracts: '[]', // Empty array as default allowFailure: true, blockTag: 'latest', rpc: 'http://localhost:8545', } // Helper function to parse contracts array const parseContracts = (contractsStr?: string): any[] => { if (!contractsStr) return [] try { const source = contractsStr.trim().startsWith('[') ? contractsStr : readFileSync(contractsStr, 'utf8') const contracts = JSON.parse(source) if (!Array.isArray(contracts)) { throw new Error('Contracts must be a JSON array') } return contracts } catch (e) { if (e instanceof SyntaxError) { throw new Error('Contracts must be inline JSON or a readable JSON file') } throw e } } const getContractCount = (contractsStr?: string): number => { try { return parseContracts(contractsStr).length } catch (_e) { return 0 } } // Helper function to safely parse block number const parseBlockNumber = (blockNumber?: string): bigint | undefined => { if (!blockNumber) return undefined try { return BigInt(blockNumber) } catch (_e) { throw new Error(`Invalid block number "${blockNumber}"`) } } export default function Multicall({ options }: Props) { // Use the action hook const actionResult = useAction({ actionName: 'multicall', options, defaultValues, optionDescriptions, // Create params createParams: async (enhancedOptions: Record) => { // Get ERC20 ABI (from module or fallback) for any contracts that need it let abi try { // Instead of trying to import a module that doesn't exist, // use the fallback ABI directly abi = fallbackERC20Abi } catch (_e) { console.warn('Failed to load ERC20 ABI, using fallback') abi = fallbackERC20Abi } // Process contracts array to ensure each has an ABI const contracts = parseContracts(enhancedOptions['contracts'] || defaultValues['contracts']) const processedContracts = contracts.map((contract: Record) => { if (!contract['abi']) { return { ...contract, abi } } return contract }) const params: Record = { contracts: processedContracts, } // Add optional parameters if (enhancedOptions['multicallAddress']) { params['multicallAddress'] = enhancedOptions['multicallAddress'] } if (enhancedOptions['allowFailure'] !== undefined) { params['allowFailure'] = enhancedOptions['allowFailure'] } // Add block identifier - only one should be used if (enhancedOptions['blockNumber']) { params['blockNumber'] = parseBlockNumber(enhancedOptions['blockNumber']) } else if (enhancedOptions['blockTag']) { params['blockTag'] = enhancedOptions['blockTag'] } return params }, // Execute the action executeAction: async (client: any, params: any): Promise => { return await client.multicall({ ...params, deployless: !params.multicallAddress }) }, }) // If editor is active, render nothing if (actionResult.editorActive) { return null } return ( ) }