Skip to main content
Glama

build_swap

Create unsigned swap transactions for token exchanges on the Casper Network. Specify input/output tokens, amounts, and slippage to generate deploy JSON for external signing.

Instructions

Build an unsigned swap transaction. Returns the deploy JSON for external signing, plus a human-readable summary.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
token_inYesInput token: symbol (e.g., "CSPR"), name, or contract hash
token_outYesOutput token: symbol (e.g., "USDT"), name, or contract hash
amountYesHuman-readable amount (e.g., "100")
typeYes"exact_in" or "exact_out"
slippage_bpsNoSlippage tolerance in basis points (default 300 = 3%)
deadline_minutesNoTransaction deadline in minutes (default 20)
sender_public_keyYesSender hex public key (e.g., "01abc...")
token_in_balanceNoRaw input token balance for one-time approval (e.g., from wallet). If omitted, approves exact swap amount only.

Implementation Reference

  • The tool `build_swap` is registered here, defining its input schema (zod) and the handler function that calls `client.buildSwap`.
    server.tool(
      'build_swap',
      'Build an unsigned swap transaction. Returns the deploy JSON for external signing, plus a human-readable summary.',
      {
        token_in: z.string().describe('Input token: symbol (e.g., "CSPR"), name, or contract hash'),
        token_out: z.string().describe('Output token: symbol (e.g., "USDT"), name, or contract hash'),
        amount: z.string().describe('Human-readable amount (e.g., "100")'),
        type: z.enum(['exact_in', 'exact_out']).describe('"exact_in" or "exact_out"'),
        slippage_bps: z.number().optional().describe('Slippage tolerance in basis points (default 300 = 3%)'),
        deadline_minutes: z.number().optional().describe('Transaction deadline in minutes (default 20)'),
        sender_public_key: z.string().describe('Sender hex public key (e.g., "01abc...")'),
        token_in_balance: z.string().optional().describe('Raw input token balance for one-time approval (e.g., from wallet). If omitted, approves exact swap amount only.'),
      },
      async (args) => {
        const bundle = await client.buildSwap({
          tokenIn: args.token_in,
          tokenOut: args.token_out,
          amount: args.amount,
          type: args.type,
          slippageBps: args.slippage_bps,
          deadlineMinutes: args.deadline_minutes,
          senderPublicKey: args.sender_public_key,
          tokenInBalance: args.token_in_balance,
        });
    
        const parts = [bundle.summary];
        if (bundle.warnings.length > 0) {
          parts.push('\nWARNINGS:\n' + bundle.warnings.join('\n'));
        }
    
        // Write approval transactions first, then the main swap
        if (bundle.approvalsRequired?.length) {
          parts.push('\n--- APPROVAL REQUIRED ---');
          for (let i = 0; i < bundle.approvalsRequired.length; i++) {
            const approval = bundle.approvalsRequired[i];
            const approvalPath = await writeDeployFile(approval.transactionJson);
            parts.push(`\nStep ${i + 1}: ${approval.summary}`);
            parts.push(`Approval transaction saved to: ${approvalPath}`);
            parts.push(`Gas: ${approval.estimatedGasCost}`);
          }
          parts.push('\n--- SWAP TRANSACTION ---');
        }
    
        const deployPath = await writeDeployFile(bundle.transactionJson);
        parts.push(`\nSwap transaction saved to: ${deployPath}`);
    
        if (bundle.approvalsRequired?.length) {
          parts.push('\nWorkflow: Sign and submit each approval with submit_transaction, then sign and submit the swap with submit_transaction.');
        } else {
          parts.push('Pass this path to sign_deploy, then use submit_transaction.');
        }
    
        return { content: [{ type: 'text' as const, text: parts.join('\n') }] };
      },
    );

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/make-software/cspr-trade-mcp'

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