convert_wei_to_formatted
Convert wei amounts to human-readable format using token decimals for DeFi trading operations.
Instructions
Convert wei amounts to human-readable format using ethers.js
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| amount | Yes | Amount in wei (as string to handle large numbers) | |
| decimals | Yes | Number of decimal places for the token (e.g., 18 for ETH, 6 for USDC) |
Implementation Reference
- src/toolService.js:749-777 (handler)The main handler function that executes the wei to formatted conversion logic using ethers.formatUnits, including input validation and formatted response.async convertWeiToFormatted(params) { const { amount, decimals } = params; if (!amount) { throw new Error("amount is required"); } if (decimals === undefined || decimals === null) { throw new Error("decimals is required"); } try { // Convert the amount to a BigNumber and format it const formattedAmount = ethers.formatUnits(amount.toString(), decimals); return { message: "Wei to formatted conversion completed successfully", data: { originalAmount: amount.toString(), decimals: decimals, formattedAmount: formattedAmount, unit: decimals === 18 ? "ETH" : `${decimals} decimals`, }, summary: `Converted ${amount} wei to ${formattedAmount} (${decimals} decimals)`, }; } catch (error) { throw new Error(`Wei to formatted conversion failed: ${error.message}`); } }
- src/index.js:934-952 (registration)MCP tool registration including name, description, and input schema definition.name: TOOL_NAMES.CONVERT_WEI_TO_FORMATTED, description: "Convert wei amounts to human-readable format using ethers.js", inputSchema: { type: "object", properties: { amount: { type: "string", description: "Amount in wei (as string to handle large numbers)", }, decimals: { type: "integer", description: "Number of decimal places for the token (e.g., 18 for ETH, 6 for USDC)", }, }, required: ["amount", "decimals"], }, },
- src/index.js:937-951 (schema)Input schema for the tool defining parameters amount (string) and decimals (integer).inputSchema: { type: "object", properties: { amount: { type: "string", description: "Amount in wei (as string to handle large numbers)", }, decimals: { type: "integer", description: "Number of decimal places for the token (e.g., 18 for ETH, 6 for USDC)", }, }, required: ["amount", "decimals"], },
- src/constants.js:44-44 (helper)Constant defining the tool name string for use in registration and dispatch.CONVERT_WEI_TO_FORMATTED: "convert_wei_to_formatted",
- src/index.js:1174-1176 (registration)Dispatch case in the main tool request handler that routes execution to the toolService handler.case TOOL_NAMES.CONVERT_WEI_TO_FORMATTED: result = await toolService.convertWeiToFormatted(args); break;