0
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "./interfaces/IERC20.sol";

contract Airdrop {
    function airdropToken(
        address _token,
        address[] calldata _receivers,
        uint256[] calldata _amounts
    ) external {
        require(_receivers.length == _amounts.length, "_receivers.length not the same as _amounts.length");        
        for (uint256 i; i < _receivers.length; i++) {
            require(IERC20(_token).transferFrom(tx.origin, _receivers[i], _amounts[i]), "Token send failed");
        }
    }
}

I have this Airdrop contract code and I would like to know if it's considered best practice to limit the size of the input array to avoid really long transactions, and/or gas errors. How important would it be to add the following require statement for checking the array input sizes?

require(_receivers.length <= 200, "Too many transfers");
1
  • Usually the block gas limit works as the limiting factor for on-chain transactions. I won't hardcode an explicit limit, unless there's a valid reason (for example you need at most 199 investors).
    – Ismael
    Aug 16, 2022 at 4:14

1 Answer 1

0

There are no limits in specification, arrays may go up to 2^256-1 elements. But the maximum allocation size for dynamic memory arrays is 2^64-1

2
  • Thank you very much for your answer but I don't think it's quite the answer I need. I can't push 2^256-1 elements in a single transaction, so I'd like to know if there's a "best-practice" allowing a maximum number of items in an input array for a single transaction. Is 2^64-1 the number for it? Aug 15, 2022 at 20:05
  • Sorry for that, best practice samples while using a dyamic arary is given here check these, i think it will help you: blog.soliditylang.org/2020/04/06/memory-creation-overflow-bug
    – R.V
    Aug 16, 2022 at 12:26

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.