[英]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.