Skip to main content
Glama

DeFi Trading Agent MCP Server

by edkdev

convert_formatted_to_wei

Convert formatted token amounts (e.g., '1.5 ETH') to wei using specified decimals, enabling precise blockchain transactions. Ideal for DeFi trading and token operations.

Instructions

Convert formatted amounts to wei using ethers.js

Input Schema

NameRequiredDescriptionDefault
amountYesFormatted amount (e.g., '1.5' for 1.5 ETH)
decimalsYesNumber of decimal places for the token (e.g., 18 for ETH, 6 for USDC)

Input Schema (JSON Schema)

{ "properties": { "amount": { "description": "Formatted amount (e.g., '1.5' for 1.5 ETH)", "type": "string" }, "decimals": { "description": "Number of decimal places for the token (e.g., 18 for ETH, 6 for USDC)", "type": "integer" } }, "required": [ "amount", "decimals" ], "type": "object" }

Implementation Reference

  • The core handler function that implements the tool logic: validates input, uses ethers.parseUnits to convert formatted amount to wei, and returns formatted result with metadata.
    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}`); } }
  • MCP tool schema definition including input validation schema (amount: string, decimals: integer) and description.
    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)
    Dispatcher switch case that routes tool calls to the specific handler method in ToolService.
    case TOOL_NAMES.CONVERT_FORMATTED_TO_WEI: result = await toolService.convertFormattedToWei(args); break;
  • Constant definition mapping the tool name constant to its string identifier used throughout the codebase.
    CONVERT_FORMATTED_TO_WEI: "convert_formatted_to_wei",

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/edkdev/defi-trading-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server