distributeToken
Send ERC20 tokens to multiple addresses on Binance Smart Chain (BSC) in a single transaction using the C98MultiSend contract, streamlining bulk token distributions.
Instructions
Send ERC20 tokens to multiple addresses on BSC using the C98MultiSend contract.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Input Schema (JSON Schema)
{
"type": "object"
}
Implementation Reference
- index.js:164-232 (handler)The handler function for the 'distributeToken' tool. It handles ERC20 token distribution to multiple addresses: validates inputs, fetches token decimals, approves spending if needed, and calls transferMultiToken on the contract.async ({ tokenAddress, receivers, amounts }) => { try { // Validate inputs if (receivers.length !== amounts.length) { return { content: [{ type: 'text', text: 'Receivers and amounts arrays must have the same length' }], isError: true }; } // Initialize token contract const tokenContract = new ethers.Contract(tokenAddress, ERC20_ABI, wallet); // Query token decimals let decimals; try { decimals = await tokenContract.decimals(); } catch (error) { return { content: [{ type: 'text', text: `Invalid ERC20 token: Unable to fetch decimals (${error.message})` }], isError: true }; } // Convert amounts to token units const amountsUnits = amounts.map(amount => ethers.parseUnits(amount.toString(), decimals)); // Calculate total amount needed const totalAmount = amountsUnits.reduce((sum, amount) => sum + BigInt(amount), BigInt(0)); // Check allowance const allowance = await tokenContract.allowance(WALLET_ADDRESS, CONTRACT_ADDRESS); if (BigInt(allowance) < totalAmount) { // Approve the contract to spend tokens try { const approveTx = await tokenContract.approve(CONTRACT_ADDRESS, totalAmount, { gasLimit: await tokenContract.approve.estimateGas(CONTRACT_ADDRESS, totalAmount) }); await approveTx.wait(); } catch (error) { return { content: [{ type: 'text', text: `Failed to approve token spending: ${error.message}` }], isError: true }; } } // Prepare and send transaction const tx = await contract.transferMultiToken(tokenAddress, receivers, amountsUnits, { gasLimit: await contract.transferMultiToken.estimateGas(tokenAddress, receivers, amountsUnits) }); // Wait for transaction confirmation const receipt = await tx.wait(); return { content: [{ type: 'text', text: `Token distribution successful. Tx Hash: ${receipt.hash}` }], isError: false }; } catch (error) { return { content: [{ type: 'text', text: `Error executing token distribution: ${error.message}` }], isError: true }; } }
- index.js:155-163 (schema)Zod input schema for the 'distributeToken' tool, validating tokenAddress, receivers (array of addresses), and amounts (array of positive numbers).z.object({ tokenAddress: z.string().refine((val) => ethers.isAddress(val), { message: 'Invalid token address' }).describe('Address of the ERC20 token contract to distribute.'), receivers: z.array(z.string().refine((val) => ethers.isAddress(val), { message: 'Invalid BSC address' })).describe('Array of BSC addresses to receive tokens.'), amounts: z.array(z.number().positive('Amount must be positive')).describe('Array of token amounts (in tokens, e.g., 100.5) to send to each receiver.') }),
- index.js:152-233 (registration)Registration of the 'distributeToken' MCP tool on the McpServer, specifying name, description, input schema, and handler function.server.tool( 'distributeToken', 'Send ERC20 tokens to multiple addresses on BSC using the C98MultiSend contract.', z.object({ tokenAddress: z.string().refine((val) => ethers.isAddress(val), { message: 'Invalid token address' }).describe('Address of the ERC20 token contract to distribute.'), receivers: z.array(z.string().refine((val) => ethers.isAddress(val), { message: 'Invalid BSC address' })).describe('Array of BSC addresses to receive tokens.'), amounts: z.array(z.number().positive('Amount must be positive')).describe('Array of token amounts (in tokens, e.g., 100.5) to send to each receiver.') }), async ({ tokenAddress, receivers, amounts }) => { try { // Validate inputs if (receivers.length !== amounts.length) { return { content: [{ type: 'text', text: 'Receivers and amounts arrays must have the same length' }], isError: true }; } // Initialize token contract const tokenContract = new ethers.Contract(tokenAddress, ERC20_ABI, wallet); // Query token decimals let decimals; try { decimals = await tokenContract.decimals(); } catch (error) { return { content: [{ type: 'text', text: `Invalid ERC20 token: Unable to fetch decimals (${error.message})` }], isError: true }; } // Convert amounts to token units const amountsUnits = amounts.map(amount => ethers.parseUnits(amount.toString(), decimals)); // Calculate total amount needed const totalAmount = amountsUnits.reduce((sum, amount) => sum + BigInt(amount), BigInt(0)); // Check allowance const allowance = await tokenContract.allowance(WALLET_ADDRESS, CONTRACT_ADDRESS); if (BigInt(allowance) < totalAmount) { // Approve the contract to spend tokens try { const approveTx = await tokenContract.approve(CONTRACT_ADDRESS, totalAmount, { gasLimit: await tokenContract.approve.estimateGas(CONTRACT_ADDRESS, totalAmount) }); await approveTx.wait(); } catch (error) { return { content: [{ type: 'text', text: `Failed to approve token spending: ${error.message}` }], isError: true }; } } // Prepare and send transaction const tx = await contract.transferMultiToken(tokenAddress, receivers, amountsUnits, { gasLimit: await contract.transferMultiToken.estimateGas(tokenAddress, receivers, amountsUnits) }); // Wait for transaction confirmation const receipt = await tx.wait(); return { content: [{ type: 'text', text: `Token distribution successful. Tx Hash: ${receipt.hash}` }], isError: false }; } catch (error) { return { content: [{ type: 'text', text: `Error executing token distribution: ${error.message}` }], isError: true }; } } );