0

I am trying to estimate gasLimit for function that transfers funds to multiple addresses:

const gasLimit = await multiTransferContract.methods
  .transferToMany(targetAddresses, amounts, tokenAddress)
  .estimateGas();

However I receive an error:

{\"code\":-32000,\"message\":\"gas required exceeds allowance or always failing transaction\"}"

Solidity function :

  modifier validOwner() {
    require(msg.sender == _owner);
    _;
  }

  function transferToMany(address[] memory receivers, uint[] memory amounts, address token)
    validOwner
    public
      {
    require(receivers.length == amounts.length);
    uint totalAmount = 0;

    for(uint i = 0; i < receivers.length; i++){
         totalAmount += amounts[i];
     }
     require(address(this).balance >= totalAmount);

    for(uint i = 0; i < receivers.length; i++) {
      if(token != 0x0000000000000000000000000000000000000000) {
        erc20iface =  ERC20(token);
        erc20iface.transfer(receivers[i], amounts[i]);
     } else {
        (bool result, ) = receivers[i].call.value(amounts[i])("");
        if(!result) revert();
     }
    }
  }

In remix it works fine. How to pass the array of addresses and amount with web3? (['0x123',...] and ['0','1','2'] doesn't work)

2
  • What is the implementation of function transferToMany? Does it possibly revert on specific input? If yes, is it possible that you are using different input in your Remix test and in your Web3.js test? Or is it possible that you are running on different networks in your Remix test and in your Web3.js test? How do you expect us to guess the reason for the transaction revert of this function, when we have no knowledge on how it is implemented??? Jun 28, 2019 at 10:15
  • @goodvibration sorry, added implementation
    – newbie
    Jun 28, 2019 at 10:46

1 Answer 1

1

The solution was easy, I had to specify from value in

const gasLimit = await multiTransferContract.methods
  .transferToMany(targetAddresses, amounts, tokenAddress)
  .estimateGas({from: initiatorAddress });

I tried this before, and I didn't work, propably I did something wrong, cuz of lack of attention.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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