convert_formatted_to_wei
Convert human-readable cryptocurrency amounts to wei units for blockchain transactions. Specify token decimals to ensure accurate conversions in DeFi trading operations.
Instructions
Convert formatted amounts to wei using ethers.js
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| amount | Yes | Formatted amount (e.g., '1.5' for 1.5 ETH) | |
| decimals | Yes | Number of decimal places for the token (e.g., 18 for ETH, 6 for USDC) |
Implementation Reference
- src/toolService.js:779-807 (handler)The main handler function that implements the convert_formatted_to_wei tool logic using ethers.parseUnits to convert formatted amounts to wei.async convertFormattedToWei(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 formatted amount to wei (BigNumber) const weiAmount = ethers.parseUnits(amount.toString(), decimals); return { message: "Formatted to wei conversion completed successfully", data: { originalAmount: amount.toString(), decimals: decimals, weiAmount: weiAmount.toString(), unit: decimals === 18 ? "ETH" : `${decimals} decimals`, }, summary: `Converted ${amount} (${decimals} decimals) to ${weiAmount.toString()} wei`, }; } catch (error) { throw new Error(`Formatted to wei conversion failed: ${error.message}`); } }
- src/index.js:954-971 (schema)MCP tool schema definition including inputSchema, properties for amount and decimals, and required fields.name: TOOL_NAMES.CONVERT_FORMATTED_TO_WEI, description: "Convert formatted amounts to wei using ethers.js", inputSchema: { type: "object", properties: { amount: { type: "string", description: "Formatted amount (e.g., '1.5' for 1.5 ETH)", }, 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:1178-1180 (registration)Tool registration and dispatch in the CallToolRequestSchema handler switch statement, calling toolService.convertFormattedToWei.case TOOL_NAMES.CONVERT_FORMATTED_TO_WEI: result = await toolService.convertFormattedToWei(args); break;
- src/constants.js:45-45 (helper)Constant definition for the tool name used in registration and dispatch.CONVERT_FORMATTED_TO_WEI: "convert_formatted_to_wei",