简体   繁体   中英

Ethers.js - "ERC721A: transfer from incorrect owner" MetaMask Error

My goal is to setApprovalForAll for a token contract before executing the safeTransferFrom function for each tokenId in the NFT collection. This way I will be able to transfer NFTs to another address without MetaMask asking for several approvals.

However I am getting an error upon executing the safeTransferFrom function, the following error is triggered:看图

This happens even after I have called the setApprovalForAll function. The setApprovalForAll transaction seems to have also went through successfully:

查看 MetaMask 活动

but calling isApprovedForAll says otherwise (view comment on line 16 in code).

I believe it is possible the error is raised because of me not calling the setApprovalForAll function properly, because why else would isApprovedForAll return false ?

document.querySelector('.click-me').onclick = async () => {
    const provider = new ethers.providers.Web3Provider(window.ethereum);
    const signer = await provider.getSigner();

    const CONTRACT_ADDRESS = '0x...';  // token contract address
    const RECEIVER_ADDRESS = '0x...';  // this address expected to get approval for all
    const ABI = [
        'function setApprovalForAll(address operator, bool _approved)',
        'function safeTransferFrom(address from, address to, uint256 tokenId)',
        'function isApprovedForAll(address owner, address operator) view returns (bool)'
    ];

    const contract = new ethers.Contract(CONTRACT_ADDRESS, ABI, signer);
    try {
        const isApproved = await contract.isApprovedForAll(CONTRACT_ADDRESS, RECEIVER_ADDRESS);
        console.log(isApproved);  // returns false even after several attempts

        await contract.setApprovalForAll(RECEIVER_ADDRESS, true);  // seems to work fine, even shows in MetaMask activity

        // ERROR SEEMS TO OCCUR HERE
        const test = await contract.safeTransferFrom(CONTRACT_ADDRESS, RECEIVER_ADDRESS, 749);  // 749 is my NFT tokenId
        console.log(test);
    } catch (error) {
        console.log(error.message)
    }    
};

You need to change contract address to the owner of the NFT see below


document.querySelector('.click-me').onclick = async () => {
    const provider = new ethers.providers.Web3Provider(window.ethereum);
    const signer = await provider.getSigner();

    const CONTRACT_ADDRESS = '0x...';  // token contract address
    const RECEIVER_ADDRESS = '0x...';  // this address expected to get approval for all
    const ABI = [
        'function setApprovalForAll(address operator, bool _approved)',
        'function safeTransferFrom(address from, address to, uint256 tokenId)',
        'function isApprovedForAll(address owner, address operator) view returns (bool)'
    ];

    const contract = new ethers.Contract(CONTRACT_ADDRESS, ABI, signer);
    try {
        const isApproved = await contract.isApprovedForAll(CONTRACT_ADDRESS, RECEIVER_ADDRESS);
        console.log(isApproved);  // returns false even after several attempts

        await contract.setApprovalForAll(RECEIVER_ADDRESS, true);  // seems to work fine, even shows in MetaMask activity

        // FIXED
        const test = await contract.safeTransferFrom(signer.address, RECEIVER_ADDRESS, 749);  // 749 is my NFT tokenId
        console.log(test);
    } catch (error) {
        console.log(error.message)
    }    
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM