abi-decode
Decode ABI-encoded data by specifying parameter types and input hexadecimal data, simplifying Ethereum blockchain interactions for developers and users.
Instructions
decode abi encoded data
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| data | Yes | hexadecimal data to decode | |
| types | Yes | parameter types array, e.g. ['uint256', 'address', 'bool'] |
Implementation Reference
- src/cast-commands/abi-encoder.ts:106-131 (handler)Handler function that cleans input data, decodes using ethers.AbiCoder.decode(types, data), formats decoded values, and returns formatted text response or error.async ({ types, data }) => { try { const cleanData = data.startsWith('0x') ? data : `0x${data}`; const decoded = ethers.AbiCoder.defaultAbiCoder().decode(types, cleanData); const decodedValues = types.map((type, index) => { const value = decoded[index]; return ` ${type}: ${this.formatDecodedValue(value, type)}`; }).join('\n'); return { content: [{ type: "text", text: `abi decode result:\n\n📊 original data: ${cleanData}\n📋 parameter types: (${types.join(',')})\n\n📝 decoded values:\n${decodedValues}` }] }; } catch (error) { return { content: [{ type: "text", text: `error: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } }
- Zod schema for tool inputs: array of ABI types and hex data string.inputSchema: { types: z.array(z.string()).describe("parameter types array, e.g. ['uint256', 'address', 'bool']"), data: z.string().describe("hexadecimal data to decode") }
- src/cast-commands/abi-encoder.ts:96-132 (registration)Direct registration of the 'abi-decode' tool on the MCP server with schema and handler.server.registerTool( "abi-decode", { title: "abi decode", description: "decode abi encoded data", inputSchema: { types: z.array(z.string()).describe("parameter types array, e.g. ['uint256', 'address', 'bool']"), data: z.string().describe("hexadecimal data to decode") } }, async ({ types, data }) => { try { const cleanData = data.startsWith('0x') ? data : `0x${data}`; const decoded = ethers.AbiCoder.defaultAbiCoder().decode(types, cleanData); const decodedValues = types.map((type, index) => { const value = decoded[index]; return ` ${type}: ${this.formatDecodedValue(value, type)}`; }).join('\n'); return { content: [{ type: "text", text: `abi decode result:\n\n📊 original data: ${cleanData}\n📋 parameter types: (${types.join(',')})\n\n📝 decoded values:\n${decodedValues}` }] }; } catch (error) { return { content: [{ type: "text", text: `error: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } } );
- Helper method used by the handler to format decoded values for display based on ABI type.private formatDecodedValue(value: any, type: string): string { if (typeof value === 'bigint') { return value.toString(); } if (type === 'address') { return value.toString(); } if (type === 'bool') { return value ? 'true' : 'false'; } if (type.startsWith('bytes')) { return value.toString(); } if (typeof value === 'string') { return `"${value}"`; } return String(value); }
- src/cast-commands/index.ts:19-19 (registration)Registration call in CastCommands that triggers abi-encoder tool registrations including abi-decode.this.abiEncoder.registerWithServer(server);