0

I am new at Solidity and Blockchain. I Have written this solidity code to send all NFTs in wallet.

contract MultiERC721Transfer {
    // Mapping to keep track of approved contracts
    mapping(address => bool) public approvedContracts;

    event ApprovedContract(address indexed contractAddress, bool isApproved);

    // Function to approve or revoke an ERC721 contract for transfers
    function approveContract(address _contractAddress, bool _isApproved) public {
        approvedContracts[_contractAddress] = _isApproved;
        emit ApprovedContract(_contractAddress, _isApproved);
    }

    // Function to transfer multiple ERC721 tokens to a recipient
    function safeTransferMultipleERC721Tokens(
        address[] memory _tokenContracts,
        uint256[] memory _tokenIds,
        address _to
    ) external payable{
        require(_tokenContracts.length == _tokenIds.length, "Arrays must have the same length");

        for (uint256 i = 0; i < _tokenContracts.length; i++) {
            address tokenContract = _tokenContracts[i];
            uint256 tokenId = _tokenIds[i];

            require(
                IERC721(tokenContract).ownerOf(tokenId) == msg.sender,
                "Sender does not own the token"
            );

            IERC721(tokenContract).safeTransferFrom(msg.sender, _to, tokenId);
        }
    }
}

I get this error

Error: Error: cannot estimate gas; transaction may fail or may require manual gas limit [ See: https://links.ethers.org/v5-errors-UNPREDICTABLE_GAS_LIMIT ] (error={"reason":"execution reverted","code":"UNPREDICTABLE_GAS_LIMIT","method":"estimateGas","transaction":{"from":"0x37269d114cD1fc0777699B0B20518dbd7D87a7aA","maxPriorityFeePerGas":{"type":"BigNumber","hex":"0x59682f00"},"maxFeePerGas":{"type":"BigNumber","hex":"0x02ed18655c"},"to":"0x11aD4e8426204B868cD5Cfad6D32796B8FA16E53","data":"0x4098da35000000000000000000000000b9373e7d7f54b6d722194c5609d3de85e971ca4e0000000000000000000000000000000000000000000000000000000000000001","type":2,"accessList":null},"error":{"reason":"processing response error","code":"SERVER_ERROR","body":"{\"jsonrpc\":\"2.0\",\"id\":49,\"error\":{\"code\":-32000,\"message\":\"execution reverted\"}}","error":{"code":-32000},"requestBody":"{\"method\":\"eth_estimateGas\",\"params\":[{\"type\":\"0x2\",\"maxFeePerGas\":\"0x2ed18655c\",\"maxPriorityFeePerGas\":\"0x59682f00\",\"from\":\"0x37269d114cd1fc0777699b0b20518dbd7d87a7aa\",\"to\":\"0x11ad4e8426204b868cd5cfad6d32796b8fa16e53\",\"data\":\"0x4098da35000000000000000000000000b9373e7d7f54b6d722194c5609d3de85e971ca4e0000000000000000000000000000000000000000000000000000000000000001\"}],\"id\":49,\"jsonrpc\":\"2.0\"}","requestMethod":"POST","url":"https://mainnet.infura.io/v3/57f5f5f45dcd43f1b40212d6833281ed"}}, tx={"data":"0x4098da35000000000000000000000000b9373e7d7f54b6d722194c5609d3de85e971ca4e0000000000000000000000000000000000000000000000000000000000000001","to":{},"from":"0x37269d114cD1fc0777699B0B20518dbd7D87a7aA","type":2,"maxFeePerGas":{"type":"BigNumber","hex":"0x02ed18655c"},"maxPriorityFeePerGas":{"type":"BigNumber","hex":"0x59682f00"},"nonce":{},"gasLimit":{},"chainId":{}}, code=UNPREDICTABLE_GAS_LIMIT, version=abstract-signer/5.7.0)

When I call the function

    const rw = await contract2.safeTransferMultipleERC721Tokens(
      tokenHashes, 
      tokenIds,
      "0x...."
      );

Anyone know what could be wrong? I think the problem could be from my Solidity script.

3

1 Answer 1

0

I think the reason is that your for has become so large that the necessary gas is more than a block gas limit and the transaction is too large.It is better to segment your For loop and do it in several transactions to avoid this problem.

2
  • What do you mean Segment my For Ring and do it in several transactions? Kindly explain
    – Daniel
    Commented Oct 7, 2023 at 20:45
  • That is, if your For loop transfers 100 NFTs, you divide it by four, and four transactions with the transfer of 25 NFT
    – Mehran Ra
    Commented Oct 8, 2023 at 2:38

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.