coordinated_sell
Execute synchronized token sales across multiple managed wallets on Base using Uniswap V2. Specify token address, percentage to sell, and slippage tolerance for coordinated portfolio management.
Instructions
Sell a token from all managed wallets via Uniswap V2 on Base.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| token_address | Yes | Token contract address to sell | |
| percent_to_sell | No | Percentage of token balance to sell per wallet (default 100) | |
| slippage_percent | No | Slippage tolerance percentage (default 5) |
Implementation Reference
- src/index.ts:479-597 (handler)The handler function for the 'coordinated_sell' tool, which iterates over managed wallets to sell tokens on Uniswap V2.
async function handleCoordinatedSell( args: z.infer<typeof CoordinatedSellSchema> ): Promise<string> { if (wallets.length === 0) { return JSON.stringify({ success: false, error: "No managed wallets. Create or import wallets first.", }); } const provider = getProvider(); const deadline = Math.floor(Date.now() / 1000) + 1200; const path = [args.token_address, WETH]; const sellPromises = wallets.map(async (w) => { const signer = getSignerForWallet(w); try { const tokenBalance = await getTokenBalance( provider, args.token_address, w.address ); if (tokenBalance === 0n) { throw new Error("No token balance to sell"); } const sellAmount = args.percent_to_sell >= 100 ? tokenBalance : (tokenBalance * BigInt(args.percent_to_sell)) / 100n; if (sellAmount === 0n) { throw new Error("Sell amount rounds to zero"); } // Check and set approval const token = new ethers.Contract(args.token_address, ERC20_ABI, signer); const allowance: bigint = await token.allowance(w.address, UNISWAP_V2_ROUTER); if (allowance < sellAmount) { const approveTx = await token.approve( UNISWAP_V2_ROUTER, ethers.MaxUint256, { gasLimit: 100_000n } ); await approveTx.wait(); } // Get quote const router = new ethers.Contract(UNISWAP_V2_ROUTER, ROUTER_ABI, provider); const amounts = await router.getAmountsOut(sellAmount, path); const expectedEth: bigint = amounts[1]; const slippageBps = BigInt(Math.floor(args.slippage_percent * 100)); const minOut = expectedEth - (expectedEth * slippageBps) / 10000n; // Execute sell const routerSigner = new ethers.Contract( UNISWAP_V2_ROUTER, ROUTER_ABI, signer ); const tx = await routerSigner.swapExactTokensForETH( sellAmount, minOut, path, w.address, deadline, { gasLimit: 300_000n } ); const receipt = await tx.wait(); return { address: w.address, label: w.label, tokens_sold: sellAmount.toString(), expected_eth: formatEth(expectedEth), txHash: tx.hash, success: receipt !== null && receipt.status === 1, }; } catch (err: unknown) { return { address: w.address, label: w.label, tokens_sold: "0", expected_eth: "0", txHash: "", success: false, error: err instanceof Error ? err.message : String(err), }; } }); const results = await Promise.allSettled(sellPromises); const outcomes = results.map((r) => r.status === "fulfilled" ? r.value : { address: "unknown", label: "", success: false, error: "Promise rejected" } ); const succeeded = outcomes.filter((o) => o.success).length; return JSON.stringify( { success: succeeded > 0, token: args.token_address, sold: succeeded, failed: outcomes.length - succeeded, percent_sold: args.percent_to_sell + "%", slippage: args.slippage_percent + "%", results: outcomes, }, null, 2 ); } - src/index.ts:142-152 (schema)Zod schema defining the input arguments for 'coordinated_sell'.
const CoordinatedSellSchema = z.object({ token_address: z.string().describe("Token contract address to sell"), percent_to_sell: z .number() .default(100) .describe("Percentage of token balance to sell per wallet (default 100)"), slippage_percent: z .number() .default(5) .describe("Slippage tolerance percentage (default 5)"), }); - src/index.ts:849-873 (registration)Registration of 'coordinated_sell' in the tool list provided by the MCP server.
name: "coordinated_sell", description: "Sell a token from all managed wallets via Uniswap V2 on Base.", inputSchema: { type: "object" as const, properties: { token_address: { type: "string", description: "Token contract address to sell", }, percent_to_sell: { type: "number", description: "Percentage of token balance to sell per wallet (default 100)", default: 100, }, slippage_percent: { type: "number", description: "Slippage tolerance percentage (default 5)", default: 5, }, }, required: ["token_address"], }, },