UNPKG

elink-cli

Version:

A Swiss Army knife CLI for Etherlink developers - manage wallets, tokens, NFTs, and more on Etherlink blockchain.

55 lines (47 loc) 2.14 kB
// /commands/call.js const chalk = require('chalk'); const { kit } = require('../lib/kit'); function callCommand(program) { program .command('call') .description('Execute a state-changing function on any smart contract.') .argument('<contractAddress>', 'The address of the smart contract.') .argument('<functionName>', 'The name of the function to call.') .option('-p, --params <params...>', 'Comma-separated list of parameters for the function.') .option('-a, --abi <abiPath>', 'Path to the contract\'s ABI JSON file. (Optional)') .action(async (contractAddress, functionName, options) => { let args = []; if (options.params) { // Simple parser for comma-separated params. // It handles numbers, addresses, and strings. args = options.params.split(',').map(param => { const p = param.trim(); // Check if it's a number (and not a long address-like hex) if (!isNaN(p) && !p.startsWith('0x')) { return Number(p); } return p; }); } console.log(chalk.yellow(`Executing function '${functionName}' on contract ${contractAddress}...`)); console.log(chalk.blue('With parameters:'), args); try { // NOTE: Your SDK's `executeContract` will need the contract's ABI. // A robust CLI might require a path to the ABI file or fetch it from a block explorer. // We assume for this example the SDK might handle simple cases or you'd load the ABI. // TODO: Use the actual 'executeContract' tool from your etherlink-agent-kit. const receipt = await kit.executeContract({ contractAddress, functionName, args, // abi: (optional, depending on your SDK's implementation) }); console.log(chalk.green('\nTransaction successful!')); console.log(chalk.blue('Transaction Hash:'), receipt.transactionHash); } catch (error) { console.error(chalk.red('\nError executing contract function:')); console.error(error); } }); } module.exports = { callCommand };