0

Here is my mulsend contract code and Here I want to send token to multiple address from another deployed contract this is my deployed contract:

https://rinkeby.etherscan.io/token/0x8fd22db75d8bc9a95c08bb4907f45e411a7b6251

Advance Thanks.

pragma solidity ^0.5.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.4.0/contracts/token/ERC20/IERC20.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.4.0/contracts/token/ERC20/SafeERC20.sol";
contract BatchSendERC20 {
    using SafeMath for uint256;
    using SafeERC20 for IERC20;
    
    address public owner;
    
    modifier onlyOwner(){
        require(msg.sender == owner);
        _;
    }
    
    constructor() public{
        owner = msg.sender;
    }
   
    //getowner
    function getOwner() public view returns (address) {
        return owner;
    }
    
    //get token balance
    function getTokenBalance(IERC20 token) public view returns (uint256) {
        return token.balanceOf(address(this));
    }
    
    //batch send different token amount from contract
    function multiSendDiffTokenFromContract(IERC20 token, address[] memory recipients, uint256[] memory amounts) public onlyOwner {
        
        require(recipients.length > 0);
        require(recipients.length == amounts.length);
        
        uint256 length = recipients.length;
        uint256 currentSum = 0;
        uint256 currentTokenBalance = token.balanceOf(address(this));
        
        for (uint256 i = 0; i < length; i++) {
            uint256 amount = amounts[i];
            require(amount > 0);
            currentSum = currentSum.add(amount);
            require(currentSum <= currentTokenBalance);
            
            token.safeTransfer(recipients[i], amount);
        }
    }
    
}

When i call multiSendDiffTokenFromContract after deploy contract and give function parameter value showing this error message "Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending? execution reverted".

2
  • Do you have enough tokens that you are trying to send on your BatchSendERC20 contract?
    – dHour
    Aug 24 at 7:33
  • No,,But i have on this rinkeby.etherscan.io/token/… contract ,How can i send token from this contract using BatchSendERC20 contract,,,Is it possible??? Aug 24 at 9:28

2 Answers 2

0

Hey i codied your code it works fine in locally on remix environment, are trying to deploy then calling it ??, please send screenshot of error will really help

9
  • yes, this code is work fine. but my problem is, i want to send token to multiple address from this rinkeby.etherscan.io/token/… contract, that's why i am developed BatchSendERC20 contract that interact with other contract and send token from rinkeby.etherscan.io/token/… contract ....How can i do that??,,,is there any way to do multisend token without using 3rd party app...Thanks for reply @Derek Aug 24 at 9:34
  • idk what is going wrong with you but i tried deploying it and it work fine look link
    – DereK
    Aug 24 at 9:47
  • do you have enough funds, in your multitrasnfercontract address
    – DereK
    Aug 24 at 9:49
  • no ...multisender contract address have not anough fund..how can i get fund Aug 24 at 10:24
  • transfer some token from other address or mint some tokens from the same address of erc20 contract
    – DereK
    Aug 24 at 10:35
0

You must calculate has enough token in your contract and then send tokens to recipients, therefor I added totalSend function for this, please check this contract.

contract BatchSendERC20 {
    using SafeMath for uint256;
    using SafeERC20 for IERC20;

    address public owner;

    modifier onlyOwner(){
       require(msg.sender == owner);
       _;
    }

    constructor() public{
       owner = msg.sender;
    }

    //getowner
    function getOwner() public view returns (address) {
        return owner;
    }

    //get token balance
    function getTokenBalance(IERC20 token) public view returns (uint256) {
        return token.balanceOf(address(this));
    }

    function totalSend(uint256[] memory amounts) internal pure returns(uint256) {
          uint256 length = amounts.lebngth;
          uint256 totalAmount;
      for(uint256 i=0; i < length; i++) {
        totalAmount += amounts[i];
      }
      return totalAmount
    }
    //batch send different token amount from contract
    function multiSendDiffTokenFromContract(IERC20 token, address[]     memory recipients, uint256[] memory amounts) public onlyOwner {
    
        require(recipients.length > 0);
        require(recipients.length == amounts.length);
    
        uint256 length = recipients.length;
        uint256 totalToSend = totalSend(amounts);
        uint256 currentTokenBalance = token.balanceOf(address(this));
        require(currentTokenBalance >= totalToSend)
        for (uint256 i = 0; i < length; i++) {
            uint256 amount = amounts[i];
            require(amount > 0);
            token.safeTransfer(recipients[i], amount);
        }
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.