Skip to main content
Glama
edkdev

DeFi Trading Agent MCP Server

by edkdev

execute_swap

Execute cryptocurrency swap transactions using pre-quoted data to complete trades across multiple blockchains.

Instructions

Execute a swap transaction (requires quote data)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
quoteDataYesQuote data from get_swap_quote

Implementation Reference

  • The primary handler function for the 'execute_swap' tool. It validates the quoteData and user private key, extracts the chainId, signs the transaction, broadcasts it via BlockchainService, and returns success details with transaction hash.
    async executeSwap(quoteData) {
      if (!quoteData) {
        throw new Error("Quote data is required for swap execution");
      }
    
      if (!this.userPrivateKey) {
        throw new Error("User private key is required for swap execution");
      }
    
      try {
        // Extract chain ID from quote data
        const chainId = quoteData.chainId || quoteData.transaction?.chainId;
        if (!chainId) {
          throw new Error("Chain ID not found in quote data");
        }
    
        console.log("🚀 Executing swap transaction...");
    
        // Sign and broadcast the transaction using blockchain service
        const result = await this.blockchain.signAndBroadcastTransaction(
          chainId,
          quoteData
        );
    
        return {
          message: "Swap executed successfully",
          data: result,
          nextSteps: [
            "1. Transaction has been broadcasted to the blockchain",
            "2. Wait for confirmation (usually 1-3 minutes)",
            "3. Check transaction status on block explorer",
            `4. Transaction hash: ${result.hash}`,
          ],
        };
      } catch (error) {
        throw new Error(`Swap execution failed: ${error.message}`);
      }
    }
  • The input schema definition for the 'execute_swap' tool, specifying that it requires an object 'quoteData' from the prior get_swap_quote tool.
    {
      name: TOOL_NAMES.EXECUTE_SWAP,
      description: "Execute a swap transaction (requires quote data)",
      inputSchema: {
        type: "object",
        properties: {
          quoteData: {
            type: "object",
            description: "Quote data from get_swap_quote",
          },
        },
        required: ["quoteData"],
      },
    },
  • src/index.js:992-994 (registration)
    The dispatch/registration case in the main tool handler switch statement that routes calls to 'execute_swap' to the ToolService.executeSwap method.
    case TOOL_NAMES.EXECUTE_SWAP:
      result = await toolService.executeSwap(args.quoteData);
      break;
  • src/constants.js:6-6 (registration)
    Constant definition mapping EXECUTE_SWAP to the tool name string 'execute_swap' used throughout the codebase.
    EXECUTE_SWAP: "execute_swap",

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