Skip to main content
Glama
kukapay

bsc-multisend-mcp

distributeToken

Send ERC20 tokens to multiple BSC addresses simultaneously using the C98MultiSend contract for efficient bulk distribution.

Instructions

Send ERC20 tokens to multiple addresses on BSC using the C98MultiSend contract.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • The handler function that executes the distributeToken tool. It handles ERC20 token distribution to multiple receivers by validating inputs, fetching token decimals, converting amounts, managing approvals, calling the contract's transferMultiToken method, and returning the transaction result.
    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
        };
      }
    }
  • Zod schema for validating inputs to the distributeToken tool: tokenAddress (ERC20 contract), receivers (array of addresses), 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 tool on the MCP server, 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
          };
        }
      }
    );
Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/kukapay/bsc-multisend-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server