0

I've written a function to send tokens in a batch, just wondering if this is secure, and if this is the best way to go about it (instead of using something like Token Multisender).

Here is the function:

function sendMultiple(address[] memory _redemptions, uint[] memory _values) public returns (bool) {
    require(_redemptions.length == _values.length);

    uint256 senderBalance = balances[msg.sender];
    uint256 length = _redemptions.length;
    for (uint i = 0; i < length; i++) {
        uint value = _values[i];
        address recipient = _redemptions[i];

        require(senderBalance >= value);
        if (msg.sender != _redemptions[i]) {
            senderBalance -= value;
            balances[recipient] += value;
        }

        emit Transfer(msg.sender, recipient, value);
    }

    balances[msg.sender] = senderBalance;
    return true;
}

Thanks!

1 Answer 1

1

I've been using such a method and it's safe and also saves some gas. Just why don't you use the transfer() method instead of all that logic?

function sendMultiple(address[] memory _redemptions, uint[] memory _values) public returns (bool) {  

    require(_redemptions.length == _values.length);

    uint256 length = _redemptions.length;
    for (uint i = 0; i < length; i++) {
        transfer(msg.sender, _redemptions[i]);
    }

    return true;
}

It will take care of balances, allowance, event.

1
  • That's fair, probably a lot simpler. Thanks!
    – zbys
    Mar 1, 2021 at 0:08

Your Answer

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

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