Skip to main content
Glama
0xGval
by 0xGval

getWalletPnl

Calculate profit and loss for an Ethereum wallet by analyzing transaction history and token balances to track investment performance.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
addressYes
chainNoeth

Implementation Reference

  • Handler function that fetches wallet PnL data from Moralis API using PowerShell, processes token profitability statistics, and returns a formatted summary or error message.
    async ({ address, chain }) => {
      try {
        // Use API key from environment variable
        const apiKey = process.env.MORALIS_API_KEY;
        if (!apiKey) {
          throw new Error("MORALIS_API_KEY environment variable is not set");
        }
        
        // Format the URL for PowerShell request
        const url = `https://deep-index.moralis.io/api/v2.2/wallets/${address}/profitability?chain=${chain}`;
        const psCommand = `powershell -Command "Invoke-RestMethod -Method Get -Uri '${url}' -Headers @{'X-API-Key'='${apiKey}'; 'accept'='application/json'} | ConvertTo-Json -Depth 10"`;
        
        // Execute PowerShell command
        const { stdout } = await execPromise(psCommand);
        const response = JSON.parse(stdout);
        
        // Get profit/loss data from the response
        const tokens = response.result || [];
        
        // Process data - calculate some summary statistics
        let totalRealizedProfit = 0;
        let totalInvested = 0;
        let profitableTokens = 0;
        let unprofitableTokens = 0;
        
        tokens.forEach(token => {
          if (token.realized_profit_usd) {
            totalRealizedProfit += parseFloat(token.realized_profit_usd);
          }
          if (token.total_usd_invested) {
            totalInvested += parseFloat(token.total_usd_invested);
          }
          
          if (parseFloat(token.realized_profit_percentage || 0) > 0) {
            profitableTokens++;
          } else if (parseFloat(token.realized_profit_percentage || 0) < 0) {
            unprofitableTokens++;
          }
        });
        
        return {
          content: [{ 
            type: "text", 
            text: `Wallet PnL for ${address} on chain ${chain}:\n` +
                  `Summary: ${tokens.length} tokens analyzed\n` +
                  `Total invested: $${totalInvested.toFixed(2)}\n` +
                  `Total realized profit/loss: $${totalRealizedProfit.toFixed(2)}\n` +
                  `Profitable tokens: ${profitableTokens}, Unprofitable tokens: ${unprofitableTokens}\n\n` +
                  `Detailed results: ${JSON.stringify(tokens, null, 2)}`
          }]
        };
      } catch (error) {
        return {
          content: [{ 
            type: "text", 
            text: `Error fetching wallet profitability: ${error.message}`
          }]
        };
      }
    }
  • Zod input schema defining 'address' as a valid Ethereum address and 'chain' as optional string defaulting to 'eth'.
    { 
      address: z.string().regex(/^0x[a-fA-F0-9]{40}$/, "Invalid Ethereum address"),
      chain: z.string().optional().default("eth")
    },
  • Registration of the 'getWalletPnl' tool using server.tool with schema and handler function.
    server.tool("getWalletPnl",
      { 
        address: z.string().regex(/^0x[a-fA-F0-9]{40}$/, "Invalid Ethereum address"),
        chain: z.string().optional().default("eth")
      },
      async ({ address, chain }) => {
        try {
          // Use API key from environment variable
          const apiKey = process.env.MORALIS_API_KEY;
          if (!apiKey) {
            throw new Error("MORALIS_API_KEY environment variable is not set");
          }
          
          // Format the URL for PowerShell request
          const url = `https://deep-index.moralis.io/api/v2.2/wallets/${address}/profitability?chain=${chain}`;
          const psCommand = `powershell -Command "Invoke-RestMethod -Method Get -Uri '${url}' -Headers @{'X-API-Key'='${apiKey}'; 'accept'='application/json'} | ConvertTo-Json -Depth 10"`;
          
          // Execute PowerShell command
          const { stdout } = await execPromise(psCommand);
          const response = JSON.parse(stdout);
          
          // Get profit/loss data from the response
          const tokens = response.result || [];
          
          // Process data - calculate some summary statistics
          let totalRealizedProfit = 0;
          let totalInvested = 0;
          let profitableTokens = 0;
          let unprofitableTokens = 0;
          
          tokens.forEach(token => {
            if (token.realized_profit_usd) {
              totalRealizedProfit += parseFloat(token.realized_profit_usd);
            }
            if (token.total_usd_invested) {
              totalInvested += parseFloat(token.total_usd_invested);
            }
            
            if (parseFloat(token.realized_profit_percentage || 0) > 0) {
              profitableTokens++;
            } else if (parseFloat(token.realized_profit_percentage || 0) < 0) {
              unprofitableTokens++;
            }
          });
          
          return {
            content: [{ 
              type: "text", 
              text: `Wallet PnL for ${address} on chain ${chain}:\n` +
                    `Summary: ${tokens.length} tokens analyzed\n` +
                    `Total invested: $${totalInvested.toFixed(2)}\n` +
                    `Total realized profit/loss: $${totalRealizedProfit.toFixed(2)}\n` +
                    `Profitable tokens: ${profitableTokens}, Unprofitable tokens: ${unprofitableTokens}\n\n` +
                    `Detailed results: ${JSON.stringify(tokens, null, 2)}`
            }]
          };
        } catch (error) {
          return {
            content: [{ 
              type: "text", 
              text: `Error fetching wallet profitability: ${error.message}`
            }]
          };
        }
      }
    );
  • main.js:62-62 (registration)
    Invocation of registerProfitabilityTools which includes the getWalletPnl tool registration on the main MCP server.
    registerProfitabilityTools(server);
Behavior1/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Tool has no description.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness1/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Tool has no description.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness1/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Tool has no description.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters1/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Tool has no description.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose1/5

Does the description clearly state what the tool does and how it differs from similar tools?

Tool has no description.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines1/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Tool has no description.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/0xGval/evm-mcp-tools'

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