coordinated_buy
Execute simultaneous token purchases across multiple managed wallets using Uniswap V2 on Base blockchain. Specify token address, ETH amount per wallet, and optional slippage tolerance.
Instructions
Buy a token from all managed wallets simultaneously via Uniswap V2 on Base.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| token_address | Yes | Token contract address to buy | |
| eth_per_wallet | Yes | ETH each wallet spends on the buy (e.g. '0.001') | |
| slippage_percent | No | Slippage tolerance percentage (default 5) |
Implementation Reference
- src/index.ts:375-463 (handler)The `handleCoordinatedBuy` function performs the swap logic for all managed wallets using Uniswap V2. It calculates expected output based on slippage, performs balance checks, and executes the swap transactions in parallel.
async function handleCoordinatedBuy( args: z.infer<typeof CoordinatedBuySchema> ): Promise<string> { if (wallets.length === 0) { return JSON.stringify({ success: false, error: "No managed wallets. Create or import wallets first.", }); } const provider = getProvider(); const ethPerWallet = ethers.parseEther(args.eth_per_wallet); const deadline = Math.floor(Date.now() / 1000) + 1200; // 20 min const path = [WETH, args.token_address]; // Get quote for slippage calc const router = new ethers.Contract(UNISWAP_V2_ROUTER, ROUTER_ABI, provider); let expectedOut: bigint; try { const amounts = await router.getAmountsOut(ethPerWallet, path); expectedOut = amounts[1]; } catch { return JSON.stringify({ success: false, error: "Failed to get quote. Token may not have a V2 liquidity pool on Base.", }); } const slippageBps = BigInt(Math.floor(args.slippage_percent * 100)); const minOut = expectedOut - (expectedOut * slippageBps) / 10000n; // Execute buys from all wallets in parallel const buyPromises = wallets.map(async (w) => { const signer = getSignerForWallet(w); try { const balance = await provider.getBalance(w.address); if (balance < ethPerWallet) { throw new Error( `Insufficient ETH: has ${formatEth(balance)}, needs ${formatEth(ethPerWallet)}` ); } const routerContract = new ethers.Contract( UNISWAP_V2_ROUTER, ROUTER_ABI, signer ); const tx = await routerContract.swapExactETHForTokens( minOut, path, w.address, deadline, { value: ethPerWallet, gasLimit: 300_000n } ); const receipt = await tx.wait(); return { address: w.address, label: w.label, eth_spent: formatEth(ethPerWallet), txHash: tx.hash, success: receipt !== null && receipt.status === 1, }; } catch (err: unknown) { return { address: w.address, label: w.label, eth_spent: "0", txHash: "", success: false, error: err instanceof Error ? err.message : String(err), }; } }); const results = await Promise.allSettled(buyPromises); 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; - src/index.ts:131-140 (schema)The `CoordinatedBuySchema` defines the input parameters for the coordinated_buy tool, including token address, ETH amount per wallet, and slippage tolerance.
const CoordinatedBuySchema = z.object({ token_address: z.string().describe("Token contract address to buy"), eth_per_wallet: z .string() .describe("ETH each wallet spends on the buy (e.g. '0.001')"), slippage_percent: z .number() .default(5) .describe("Slippage tolerance percentage (default 5)"), }); - src/index.ts:824-843 (registration)The tool `coordinated_buy` is registered with a description and its input schema in the server's tool definition list.
{ name: "coordinated_buy", description: "Buy a token from all managed wallets simultaneously via Uniswap V2 on Base.", inputSchema: { type: "object" as const, properties: { token_address: { type: "string", description: "Token contract address to buy", }, eth_per_wallet: { type: "string", description: "ETH each wallet spends on the buy (e.g. '0.001')", }, slippage_percent: { type: "number", description: "Slippage tolerance percentage (default 5)", default: 5, },