Skip to main content
Glama
edkdev

DeFi Trading Agent MCP Server

by edkdev

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
NameRequiredDescriptionDefault
amountYesAmount in wei (as string to handle large numbers)
decimalsYesNumber of decimal places for the token (e.g., 18 for ETH, 6 for USDC)

Implementation Reference

  • 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"],
      },
    },
  • 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"],
    },
  • 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;

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