Adding a function that sends tokens to multiple accounts ERC20

:computer: Environment

Truffle, OpenZepelin 2.5.1
:memo:Details

I want to add a function that sends tokens to multiple accounts in one transaction to reduce gas costs. But it is giving me this error:
Thrown: Error: Returned error: VM Exception while processing transaction: revert

So I wrote a test function that simply returns the input. It gives the same error when I call it in truffle.

Could someone help me out pls
:1234: Code to reproduce

Here’s my code:
pragma solidity >= 0.5.5;

//SPDX-License-Identifier: MIT

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Detailed.sol";



contract BPTToken is Context, ERC20, ERC20Detailed {
    
    constructor(
        string memory name,
        string memory symbol,
        uint256 initialSupply
    ) public ERC20Detailed(name, symbol, 18) {
        _mint(_msgSender(), initialSupply);
    }
    event DoneStuff(uint input);

    function test(uint input) external returns(uint){
        return input;
    }
    function multipleTransfer(address[] memory recipients, uint256[] memory amounts, uint256 totalAmount) external returns (uint256) {
        return totalAmount;
        uint recipientsLen = recipients.length;
        uint amountsLen = amounts.length;

        require(recipientsLen == amountsLen, "Recipients and amounts must have the same length.");
        require(balanceOf(_msgSender()) >= totalAmount, "Not enough fund.");


        for (uint index = 0; index < recipientsLen; index++) {
            address recipient = recipients[index];
            uint256 amount = amounts[index];
            bool result = transferFrom(_msgSender(), recipient, amount);
            if (!result) {
                return false;
            }
        }
        
        return true;
    }
}
1 Like

Hey, welcome!

It seems like your function is not correct, I think you should change your function like following:

function multipleTransfer(address[] calldata recipients, uint256[] calldata amounts, uint256 totalAmount) external returns (bool) {}
2 Likes

Hi, it still throws me an error. even when calling the test function. Is there anything I’m doing that’s wrong?

I think you use the dependency of @openzeppelin/contracts is 2.x. and for your function: multipleTransfer, you write to expect to return uint256, but there is some cases to return bool, so I think you should unify the behavior.

function multipleTransfer(address[] calldata recipients, uint256[] calldata amounts, uint256 totalAmount) external returns (uint256) {
        return totalAmount;      <<<<---------here
        uint recipientsLen = recipients.length;
        uint amountsLen = amounts.length;

        require(recipientsLen == amountsLen, "Recipients and amounts must have the same length.");
        require(balanceOf(_msgSender()) >= totalAmount, "Not enough fund.");


        for (uint index = 0; index < recipientsLen; index++) {
            address recipient = recipients[index];
            uint256 amount = amounts[index];
            bool result = transferFrom(_msgSender(), recipient, amount);
            if (!result) {
                return false;   <<<<---------here
            }
        }
        
        return true;        <<<<---------here
    }
2 Likes