abi encode
abi-encodeEncode Ethereum smart contract function parameters using ABI encoding. Input parameter types and values to generate encoded data for blockchain transactions.
Instructions
encode function parameters with abi
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| types | Yes | parameter types array, e.g. ['uint256', 'address', 'bool'] | |
| values | Yes | parameter values array, corresponding to the types array |
Implementation Reference
- src/cast-commands/abi-encoder.ts:17-52 (handler)Executes the ABI encoding logic: validates input lengths, processes values, encodes using ethers.AbiCoder, formats output with types, values, encoded hex, and byte length.
async ({ types, values }) => { try { if (types.length !== values.length) { return { content: [{ type: "text", text: "error: parameter types and values length mismatch" }], isError: true }; } const processedValues = this.processValues(types, values); const encoded = ethers.AbiCoder.defaultAbiCoder().encode(types, processedValues); const typeSignature = `(${types.join(',')})`; const readableParams = types.map((type, index) => ` ${type}: ${this.formatValue(values[index])}` ).join('\n'); return { content: [{ type: "text", text: `abi encode result:\n\nš parameter types: ${typeSignature}\nš parameter values:\n${readableParams}\n\nš¢ encoded result: ${encoded}\n\nš length: ${(encoded.length - 2) / 2} bytes` }] }; } catch (error) { return { content: [{ type: "text", text: `error: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } } - src/cast-commands/abi-encoder.ts:7-53 (registration)Registers the 'abi-encode' tool on the MCP server with title, description, Zod input schema for types and values arrays, and references the handler function.
server.registerTool( "abi-encode", { title: "abi encode", description: "encode function parameters with abi", inputSchema: { types: z.array(z.string()).describe("parameter types array, e.g. ['uint256', 'address', 'bool']"), values: z.array(z.union([z.string(), z.number(), z.boolean()])).describe("parameter values array, corresponding to the types array") } }, async ({ types, values }) => { try { if (types.length !== values.length) { return { content: [{ type: "text", text: "error: parameter types and values length mismatch" }], isError: true }; } const processedValues = this.processValues(types, values); const encoded = ethers.AbiCoder.defaultAbiCoder().encode(types, processedValues); const typeSignature = `(${types.join(',')})`; const readableParams = types.map((type, index) => ` ${type}: ${this.formatValue(values[index])}` ).join('\n'); return { content: [{ type: "text", text: `abi encode result:\n\nš parameter types: ${typeSignature}\nš parameter values:\n${readableParams}\n\nš¢ encoded result: ${encoded}\n\nš length: ${(encoded.length - 2) / 2} bytes` }] }; } catch (error) { return { content: [{ type: "text", text: `error: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } } ); - Helper method to process and convert input values based on their ABI types (e.g., BigInt for integers, address validation, bool parsing, bytes hex prefixing). Called by the handler.
private processValues(types: string[], values: (string | number | boolean)[]): any[] { return values.map((value, index) => { const type = types[index]; if (type.includes('uint') || type.includes('int')) { return ethers.getBigInt(value.toString()); } if (type === 'address') { if (typeof value !== 'string') { throw new Error(`address type parameter must be a string: ${value}`); } if (!ethers.isAddress(value)) { throw new Error(`invalid ethereum address: ${value}`); } return value; } if (type === 'bool') { if (typeof value === 'boolean') return value; if (typeof value === 'string') { const lowercaseValue = value.toLowerCase(); if (lowercaseValue === 'true') return true; if (lowercaseValue === 'false') return false; throw new Error(`boolean value must be true or false: ${value}`); } throw new Error(`invalid boolean value: ${value}`); } if (type.startsWith('bytes')) { if (typeof value !== 'string') { throw new Error(`bytes type parameter must be a string: ${value}`); } return value.startsWith('0x') ? value : `0x${value}`; } return value; }); } - Helper method to format parameter values for readable display in the tool output.
private formatValue(value: string | number | boolean): string { if (typeof value === 'string') { return `"${value}"`; } return String(value); } - src/cast-commands/index.ts:17-20 (registration)CastCommands class registers its AbiEncoder instance (which registers 'abi-encode') with the MCP server.
registerWithServer(server: McpServer) { this.fourByteService.registerWithServer(server); this.abiEncoder.registerWithServer(server); this.utilsService.registerWithServer(server);