簡體   English   中英

ERC721:轉接呼叫者既不是所有者也不是批准的

[英]ERC721: transfer caller is not owner nor approved

我有一個 nftToken 合約,它將代幣鑄造到 msg.sender,然后我在市場合約中有一個 function,將 nft 從所有者轉移到市場合約。 但是,我收到一條錯誤消息:ERC721: transfer caller is not owner or approved。

這是我的 nftContract (nft) function 片段:

function createToken(string memory tokenURI) public returns (uint) {
  _tokenIds.increment();
  uint256 newItemId = _tokenIds.current();

  _mint(msg.sender, newItemId);
  _setTokenURI(newItemId, tokenURI);
  setApprovalForAll(contractAddress, true);
  return newItemId;
}

這是我的市場代碼(stripeMarket Contract)function 片段:

function createItem(
    address nftContract,
    uint256 tokenId
    ) public payable{
     address _owner = IERC721(nftContract).ownerOf(tokenId);
     IERC721(nftContract).transferFrom(_owner, address(this),tokenId);
      IERC721(nftContract).approve(address(this),tokenId);    
}

在這里,我嘗試使用 web3 從前端調用它:

const getItems=async()=>{
      await contracts.nft.methods.createToken("https://i.ytimg.com/vi/nYxGhQYi0s4/maxresdefault.jpg").send({from: accounts[0]});
      const owners = await contracts.nft.methods.ownerOf(1).call({from:accounts[0]});
      await contracts.stripeMarket.methods.createItem(contracts.nft._address,1).send({from: {owners}}); 
}

但我收到錯誤:

ERC721:轉接呼叫者既不是所有者也不是批准的。

nftContract執行setApprovalForAll(contractAddress, true)時,它允許contractAddress (市場合約)操作所有nftContract的代幣

但是新鑄造的代幣歸msg.sender擁有,而不是nftContract 因此,批准不適用於此令牌。


根據您的用例,您可以

  1. 將新令牌鑄造到nftContract (而不是msg.sender ),以便允許市場合約操作它。 或者直接將其鑄造到市場合約中。

     // the owner is the `nftContract` _mint(address(this), newItemId); // the Market contract is allowed to operate the `nftContract`'s tokens setApprovalForAll(contractAddress, true);
  2. 在執行createItem()之前,讓msg.sender (令牌所有者)在nftContract上執行approve(marketAddress, tokenId) ) 。

    這將使市場合約批准操作msg.sender擁有的這個特定令牌。 (假設它與_owner的地址相同 - 否則它將失敗。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM