0

I was checking the implementation of Openzeppelin's implementation of ERC-1155 standard and I'm curious about the use of _safeBatchTransferFrom function to transfer multiple tokens at once. The concept is very clear for me but I think that the operation could lead to "out of gas" error. If not, I would like to know why not, because I always heard that you should avoid loops on an undetermined size list.

function _safeBatchTransferFrom(
    address from,
    address to,
    uint256[] memory ids,
    uint256[] memory amounts,
    bytes memory data
) internal virtual {
    require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch");

    (...)

    for (uint256 i = 0; i < ids.length; ++i) {
        uint256 id = ids[i];
        uint256 amount = amounts[i];

        uint256 fromBalance = _balances[id][from];
        require(fromBalance >= amount, "ERC1155: insufficient balance for transfer");
        unchecked {
            _balances[id][from] = fromBalance - amount;
        }
        _balances[id][to] += amount;
    }

    (...)

}

1 Answer 1

0

The undetermined size of the list is only from the smart contract's perspective.

The caller is going to provide gas for the transaction, and the caller also knows the size of the list. So, any rationale caller will consider the size of the list while providing gas for the transaction and hence never run "out of gas"

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.