1

I want to know what is the gas effective manner to code to transfer many (e.g 1000) ERC721 tokens to a single address. I have tried the way below but I want something more efficient.

    function bulkTransfer(uint256[] tokenIds, address _to) public onlyOwner {
        uint256 length = tokenIds.length;
        for (uint256 i = 0; i < length; i++) {
            _transfer(owner, _to, tokenIds[i]);
        }
    }

Assume that the owner holds all these tokens in that array.

3 Answers 3

2

You can add these gas saving methods:

function bulkTransfer(uint256[] tokenIds, address _to) external onlyOwner {
        for (uint256 i; i < tokenIds.length;) {
            _transfer(msg.sender, _to, tokenIds[I]);
            unchecked { i++ }
        }
}

Here is what did I do:

  • change the visibility to external
  • delete the length variable. Because reading from calldata is not that expensive.
  • change i=0 to i. Because the default value for uint is already 0.
  • delete i++ and add unchecked { i++ }. This means, "solidity, believe me, there will be no over/underflows".
  • change owner to msg.sender. Because we don't want to read every time from the storage. Since the function is onlyOwner, msg.sender should be the owner.
0

I made a small (free) tool that helps when transferring multiple NFTs from an ERC721: https://theforever.io/erc721-batch-transfer-tool

This assumes that you've already transferred the tokens to your own wallet. Using the tool, you can enter a list of addresses and token_ids to batch transfer the ERC721 NFTs to whoever you like. It's more gas-efficient than doing it in multiple transactions.

2
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    May 17, 2023 at 8:57
  • If the tool is open source, please include that information.
    – eth
    May 19, 2023 at 5:15
-1

IF THE CONTRACT IS ALREADY DEPLOYED, I have implemented already and published it here https://nft.life.

You can see quite a few people have used it. It's free and open source.

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.