简体   繁体   English

此函数以原因字符串“ERC721:转移调用者不是所有者也不是批准”恢复

[英]This function reverted with reason string 'ERC721: transfer caller is not owner nor approved'

Code:代码:

function f(address nftContract, uint256 itemId, uint256 price) public payable nonReentrant 
{
  uint tokenId = idToMarketItem[itemId].tokenId;
  IERC721(nftContract).approve(address(this), tokenId);
  IERC721(nftContract).transferFrom(msg.sender, address(this), tokenId);
}

I really dont know whats wrong.我真的不知道怎么了。 Please help me请帮我

The fix is not to do the approve in your function:解决方法不是在您的函数中执行批准:

function f(address nftContract, uint256 itemId, uint256 price) public payable nonReentrant 
{
  uint tokenId = idToMarketItem[itemId].tokenId;
  IERC721(nftContract).transferFrom(msg.sender, address(this), tokenId);
}

Because you are trying to have your contract approve itself to spend funds belonging to the caller (see the answer I linked in the comment, that explains why msg.sender is not what you think inside the IERC721(nftContract).approve call) this will not work.因为您正试图让您的合同批准自己花费属于调用者的资金(请参阅我在评论中链接的答案,这解释了为什么 msg.sender 不是您在 IERC721(nftContract).approve 调用中所想的那样)这将不行。 The user must directly call approve on the nftContract.用户必须直接在 nftContract 上调用批准。

This is the user's nft, only it should have the right to approve their spending by a third party, no one else.这是用户的nft,只有它有权批准第三方的支出,其他人没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:处理事务时出现 VM 异常:已恢复,原因字符串为“ERC721:转移呼叫者不是所有者也未获批准” - Error: VM Exception while processing transaction: reverted with reason string 'ERC721: transfer caller is not owner nor approved' ERC721:转接呼叫者既不是所有者也不是批准的 - ERC721: transfer caller is not owner nor approved 检查所有者是 msg.sender,但仍然给出 ERC721:transfer caller is not owner nor approved - Check owner is msg.sender, but still gives ERC721: transfer caller is not owner nor approved 多边形区块链上 MetaMask 的内部 JSON-RPC 错误。 `ERC721:转接呼叫者既不是所有者也不是批准的。` - Internal JSON-RPC error with MetaMask on Polygon Blockchain. `ERC721: transfer caller is not owner nor approved.` 将 ERC721 从所有者转让给买家 - Transfer ERC721 from owner to buyer ERC721 应付账款 function - ERC721 with payable function erc721 NFT的锁转移 - Lock transfer of erc721 NFT 如何转移 ERC721 代币 - How to transfer an ERC721 token 无法在 ERC721 智能合约中设置批准或转让所有权 - Unable to set approval nor transfer ownership in ERC721 smart contract ERC721 NFT 创建一个 function 来购买/出售由合约所有者预制的 NFT,安全问题 - ERC721 NFT creating a function to buy/sell NFTs that have been preminted by the contract owner, security question
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM