get-contract-abi
Retrieve the Application Binary Interface (ABI) for a smart contract by providing its address in 0x format. This enables interaction with contracts on the specified blockchain.
Instructions
Get the ABI for a smart contract
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| address | Yes | Contract address (0x format) |
Implementation Reference
- src/server.ts:233-246 (handler)Handler logic for the 'get-contract-abi' tool: parses input using ContractSchema, calls etherscanService.getContractABI, and formats the response.if (name === "get-contract-abi") { try { const { address } = ContractSchema.parse(args); const abi = await etherscanService.getContractABI(address); return { content: [{ type: "text", text: `Contract ABI for ${address}:\n\n${abi}` }], }; } catch (error) { if (error instanceof z.ZodError) { throw new Error(`Invalid input: ${error.errors.map(e => e.message).join(", ")}`); } throw error; } }
- src/server.ts:47-49 (schema)Zod schema for validating the input parameters (address) of the get-contract-abi tool.const ContractSchema = z.object({ address: z.string().regex(/^0x[a-fA-F0-9]{40}$/, 'Invalid Ethereum address format'), });
- src/server.ts:112-126 (registration)Tool registration in the ListTools response, including name, description, and input schema.{ name: "get-contract-abi", description: "Get the ABI for a smart contract", inputSchema: { type: "object", properties: { address: { type: "string", description: "Contract address (0x format)", pattern: "^0x[a-fA-F0-9]{40}$" }, }, required: ["address"], }, },
- Core implementation of fetching the contract ABI from Etherscan API using the getabi endpoint.async getContractABI(address: string): Promise<string> { try { const validAddress = ethers.getAddress(address); // Get contract ABI const result = await fetch( `https://api.etherscan.io/api?module=contract&action=getabi&address=${validAddress}&apikey=${this.provider.apiKey}` ); const data = await result.json(); if (data.status !== "1" || !data.result) { throw new Error(data.message || "Failed to fetch contract ABI"); } return data.result; } catch (error) { if (error instanceof Error) { throw new Error(`Failed to get contract ABI: ${error.message}`); } throw error; } }