Skip to main content
Glama
lordbasilaiassistant-sudo

base-flash-arb-mcp

estimate_flash_profit

Calculate potential profit from arbitrage opportunities using flash loans on Base network. Estimates net earnings after accounting for gas fees when exploiting price differences between two liquidity pools.

Instructions

Given two pools with a price difference, estimate flash loan profit after gas.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
token_addressYesToken contract address
pool_a_addressYesAddress of pool A (buy cheap here)
pool_b_addressYesAddress of pool B (sell expensive here)
loan_amount_ethNoFlash loan amount in ETH (default 1.0)1.0

Implementation Reference

  • The tool "estimate_flash_profit" is defined and implemented here. It calculates the price difference between two liquidity pools and estimates potential profit from a flash loan, accounting for flash loan fees and gas costs.
    server.tool(
      "estimate_flash_profit",
      "Given two pools with a price difference, estimate flash loan profit after gas.",
      {
        token_address: z.string().describe("Token contract address"),
        pool_a_address: z.string().describe("Address of pool A (buy cheap here)"),
        pool_b_address: z.string().describe("Address of pool B (sell expensive here)"),
        loan_amount_eth: z
          .string()
          .default("1.0")
          .describe("Flash loan amount in ETH (default 1.0)"),
      },
      async ({ token_address, pool_a_address, pool_b_address, loan_amount_eth }) => {
        try {
          const symbol = await getSymbol(token_address);
          const loanWei = ethers.parseEther(loan_amount_eth);
          const gasCost = await estimateGasCost();
    
          // Identify pool types by checking factories
          const v2Factory = new ethers.Contract(
            UNIV2_FACTORY,
            UNIV2_FACTORY_ABI,
            provider
          );
          const aeroFactory = new ethers.Contract(
            AERO_FACTORY,
            AERO_FACTORY_ABI,
            provider
          );
    
          // Try to get reserves from both pools
          const pairAbi = [
            ...UNIV2_PAIR_ABI,
            "function stable() view returns (bool)",
          ];
    
          async function getPoolPrice(
            poolAddr: string
          ): Promise<{ priceInWeth: number; type: string } | null> {
            try {
              const pair = new ethers.Contract(poolAddr, pairAbi, provider);
              const [r0, r1] = await pair.getReserves();
              const t0: string = await pair.token0();
              const isWeth0 = t0.toLowerCase() === WETH.toLowerCase();
              const wethRes = isWeth0 ? r0 : r1;
              const tokenRes = isWeth0 ? r1 : r0;
    
              if (tokenRes === 0n) return null;
    
              const price =
                Number(ethers.formatEther(wethRes)) /
                Number(ethers.formatUnits(tokenRes, 18));
    
              // Detect type
              let poolType = "unknown";
              try {
                const v2Pair = await v2Factory.getPair(WETH, token_address);
                if (v2Pair.toLowerCase() === poolAddr.toLowerCase())
                  poolType = "Uniswap V2";
              } catch { /* ignore */ }
              try {
                for (const stable of [false, true]) {
                  const aeroPool = await aeroFactory.getPool(
                    WETH,
                    token_address,
                    stable
                  );
                  if (aeroPool.toLowerCase() === poolAddr.toLowerCase())
                    poolType = `Aerodrome (${stable ? "stable" : "volatile"})`;
                }
              } catch { /* ignore */ }
    
              return { priceInWeth: price, type: poolType };
            } catch {
              return null;
            }
          }
    
          const [poolAInfo, poolBInfo] = await Promise.all([
            getPoolPrice(pool_a_address),
            getPoolPrice(pool_b_address),
          ]);
    
          if (!poolAInfo || !poolBInfo) {
            return {
              content: [
                {
                  type: "text" as const,
                  text: JSON.stringify({
                    error: "Could not read reserves from one or both pools",
                    poolA: poolAInfo ? "OK" : "FAILED",
                    poolB: poolBInfo ? "OK" : "FAILED",
                  }),
                },
              ],
              isError: true,
            };
          }
    
          // Calculate arb: buy cheap on A, sell expensive on B
          const priceDiffBps = Math.abs(
            ((poolAInfo.priceInWeth - poolBInfo.priceInWeth) /
              Math.min(poolAInfo.priceInWeth, poolBInfo.priceInWeth)) *
              10000
          );
    
          // Estimate tokens from loan
          const cheaperPool =
            poolAInfo.priceInWeth < poolBInfo.priceInWeth ? "A" : "B";
          const cheapPrice = Math.min(poolAInfo.priceInWeth, poolBInfo.priceInWeth);
          const expPrice = Math.max(poolAInfo.priceInWeth, poolBInfo.priceInWeth);
    
          const tokensFromLoan =
            cheapPrice > 0
              ? Number(ethers.formatEther(loanWei)) / cheapPrice
              : 0;
          const ethFromSell = tokensFromLoan * expPrice;
          const grossProfitEth =
            ethFromSell - Number(ethers.formatEther(loanWei));
          const flashLoanFee = Number(ethers.formatEther(loanWei)) * 0.0009; // Aave 0.09%
          const netProfitEth =
            grossProfitEth -
            flashLoanFee -
            Number(ethers.formatEther(gasCost));
    
          return {
            content: [
              {
                type: "text" as const,
                text: JSON.stringify(
                  {
                    token: token_address,
                    symbol,
                    loanAmountEth: loan_amount_eth,
                    poolA: {
                      address: pool_a_address,
                      type: poolAInfo.type,
                      priceInWeth: poolAInfo.priceInWeth.toFixed(18),
                    },
                    poolB: {
                      address: pool_b_address,
                      type: poolBInfo.type,
                      priceInWeth: poolBInfo.priceInWeth.toFixed(18),
                    },
                    priceDiffBps: Math.round(priceDiffBps),
                    buyOn: cheaperPool === "A" ? "Pool A" : "Pool B",
                    sellOn: cheaperPool === "A" ? "Pool B" : "Pool A",
                    estimatedTokens: tokensFromLoan.toFixed(4),
                    ethFromSell: ethFromSell.toFixed(6),
                    grossProfitEth: grossProfitEth.toFixed(6),
                    flashLoanFeeEth: flashLoanFee.toFixed(6),
                    gasCostEth: ethers.formatEther(gasCost),
                    netProfitEth: netProfitEth.toFixed(6),
                    profitable: netProfitEth > 0,
                    warning:
                      "Estimates assume no slippage. Real execution will have slippage proportional to trade size vs pool liquidity.",
                  },
                  null,
                  2
Behavior2/5

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

No annotations are provided, so the description carries the full burden of behavioral disclosure. It mentions 'estimate' and 'after gas', which implies a calculation without execution, but it doesn't clarify if this is a simulation, requires network access, has rate limits, or what the output format might be. For a tool with financial implications and no annotations, this leaves significant gaps in understanding its behavior and constraints.

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

Conciseness5/5

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

The description is a single, efficient sentence that front-loads the core purpose ('estimate flash loan profit after gas') and adds the key condition ('Given two pools with a price difference'). Every word earns its place with zero waste, making it highly concise and well-structured for quick understanding.

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

Completeness2/5

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

Given the complexity of flash loan profit estimation (involving gas costs, price differences, and financial calculations), no annotations, and no output schema, the description is incomplete. It lacks details on how the estimation is performed, what the output includes (e.g., profit amount, gas cost breakdown), and any assumptions or limitations. This makes it inadequate for a tool with such operational and financial implications.

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

Parameters3/5

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

Schema description coverage is 100%, so the schema already documents all parameters thoroughly (e.g., 'buy cheap here' for pool_a_address). The description adds no additional parameter semantics beyond what's in the schema. According to guidelines, with high schema coverage (>80%), the baseline is 3 even with no param info in the description, which applies here.

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

Purpose4/5

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

The description clearly states the tool's purpose: 'estimate flash loan profit after gas' given 'two pools with a price difference'. It specifies the verb ('estimate'), resource ('flash loan profit'), and key condition ('after gas'). However, it doesn't explicitly differentiate from siblings like 'detect_arb_opportunity' or 'check_sandwich_risk', which might also involve profit estimation or arbitrage scenarios.

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

Usage Guidelines2/5

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

The description provides minimal guidance: it implies usage when there's a price difference between two pools for flash loans. However, it doesn't specify when to use this tool versus alternatives (e.g., 'detect_arb_opportunity' for opportunity detection or 'get_price_across_dexes' for price checks), nor does it mention prerequisites like needing pool addresses or token details. No explicit exclusions or context for when-not-to-use are provided.

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/lordbasilaiassistant-sudo/base-flash-arb-mcp'

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