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
| Name | Required | Description | Default |
|---|---|---|---|
| token_in | Yes | Input token: symbol (e.g., "CSPR"), name, or contract hash | |
| token_out | Yes | Output token: symbol (e.g., "USDT"), name, or contract hash | |
| amount | Yes | Human-readable amount (e.g., "100") | |
| type | Yes | "exact_in" or "exact_out" | |
| slippage_bps | No | Slippage tolerance in basis points (default 300 = 3%) | |
| deadline_minutes | No | Transaction deadline in minutes (default 20) | |
| sender_public_key | Yes | Sender hex public key (e.g., "01abc...") | |
| token_in_balance | No | Raw input token balance for one-time approval (e.g., from wallet). If omitted, approves exact swap amount only. |
Implementation Reference
- packages/mcp/src/tools/trading.ts:7-61 (handler)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') }] }; }, );