check_token_approval
Verify token approval status for Uniswap swaps. Determines if approval is required and provides the approval transaction when needed.
Instructions
Check if a token is approved for swapping on Uniswap. Returns whether approval is needed and the approval transaction if so. Requires UNISWAP_API_KEY env var.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| token | Yes | Token symbol (USDC, USDT, DAI, WETH) or contract address | |
| amount | Yes | Amount to approve (in human-readable units) | |
| walletAddress | No | Wallet address. Uses W3SHIP_PUBLIC_KEY if not provided. | |
| chainId | No | Chain ID (default: 8453 for Base) |
Implementation Reference
- src/index.ts:838-916 (handler)The handler implementation for the check_token_approval tool in src/index.ts.
case 'check_token_approval': { if (!UNISWAP_API_KEY) { return { content: [{ type: 'text', text: 'Error: UNISWAP_API_KEY environment variable is not set. Get your API key at https://developers.uniswap.org' }], isError: true, }; } const { token: tokenArg, amount: approveAmount, walletAddress: approveWallet, chainId: approveChain } = args as any; const appChainId = approveChain || 8453; const appWallet = approveWallet || CONFIGURED_KEY; if (!appWallet) { return { content: [{ type: 'text', text: 'Error: Wallet address required. Set W3SHIP_PUBLIC_KEY or provide walletAddress.' }], isError: true, }; } const resolveTokenAddr = (t: string) => { const upper = t.toUpperCase(); return TOKENS[upper]?.address || t; }; const resolveTokenDec = (t: string) => { const upper = t.toUpperCase(); return TOKENS[upper]?.decimals || 18; }; const tokenAddr = resolveTokenAddr(tokenArg); const tokenDec = resolveTokenDec(tokenArg); const approveAmountRaw = BigInt(Math.floor(parseFloat(approveAmount) * (10 ** tokenDec))).toString(); try { const approvalRes = await fetch(`${UNISWAP_API}/check_approval`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'x-api-key': UNISWAP_API_KEY, }, body: JSON.stringify({ token: tokenAddr, amount: approveAmountRaw, walletAddress: appWallet, chainId: appChainId, }), }); const approvalData = await approvalRes.json() as any; if (!approvalRes.ok) { return { content: [{ type: 'text', text: `Uniswap API error: ${approvalData.errorCode || JSON.stringify(approvalData)}` }], isError: true, }; } return { content: [{ type: 'text', text: JSON.stringify({ token: tokenArg.toUpperCase(), amount: approveAmount, chainId: appChainId, approved: approvalData.approved || false, approvalNeeded: !approvalData.approved, approvalTransaction: approvalData.approvalTransaction || null, message: approvalData.approved ? `${tokenArg.toUpperCase()} is already approved for swapping` : `${tokenArg.toUpperCase()} needs approval before swapping. Transaction data provided.`, }, null, 2) }] }; } catch (e: any) { return { content: [{ type: 'text', text: `Error checking approval: ${e.message}` }], isError: true, }; } } - src/index.ts:236-248 (registration)Tool registration for check_token_approval in src/index.ts.
name: 'check_token_approval', description: 'Check if a token is approved for swapping on Uniswap. Returns whether approval is needed and the approval transaction if so. Requires UNISWAP_API_KEY env var.', inputSchema: { type: 'object', properties: { token: { type: 'string', description: 'Token symbol (USDC, USDT, DAI, WETH) or contract address' }, amount: { type: 'string', description: 'Amount to approve (in human-readable units)' }, walletAddress: { type: 'string', description: 'Wallet address. Uses W3SHIP_PUBLIC_KEY if not provided.' }, chainId: { type: 'number', description: 'Chain ID (default: 8453 for Base)' }, }, required: ['token', 'amount'], }, },