简体   繁体   English

ERC721 智能合约撤销批准

[英]ERC721 Smart contract revoke approval

I've some question about ERC721 processing.我对 ERC721 处理有一些疑问。 I'd like to make an user to give aproval to an external smart contract for transfering one of its NFT.我想让用户批准外部智能合约以转移其 NFT 之一。 To do it I'm using approve(to, tokenId) .为此,我使用approve(to, tokenId)

Then, in some situation, the contract should be able to revoke its own autorisation by calling approve(address(0), tokenId) (The NFT owner calls a cancel method from the smart contract that perform some operations and revokes its own permission on the token)然后,在某些情况下,合约应该能够通过调用approve(address(0), tokenId)来撤销自己的授权(NFT 所有者从执行某些操作的智能合约中调用取消方法,并撤销自己对令牌)

At this step, I got the following error: ERC721: approve caller is not owner nor approved在这一步,我收到以下错误:ERC721: approve caller is not owner nor approved

My understanding is that in order to make the contract able to call approve, the NFT owner should have approved it with setApprovalForAll(operator, _approved) ?我的理解是,为了让合约能够调用 approve,NFT 所有者应该已经通过setApprovalForAll(operator, _approved)批准了它? What about others NFT from the same collection owned by the user?用户拥有的同一收藏中的其他 NFT 呢? Would the contract be able to manage them too?合约也能管理它们吗? I'd like to limit as much as possible the smart contract's permissions and stick to a very specific NFT transfer (with its token_id) Can someone enlighten me on the right way to do it?我想尽可能地限制智能合约的权限并坚持非常具体的 NFT 传输(及其 token_id)有人可以用正确的方式启发我吗?

It sounds like you have 2 contracts and an EOA involved in this process.听起来您在此过程中涉及 2 份合同和一份 EOA。 You want the NFT contract to give another contract permission to transfer tokens, as well as remove that permission.您希望 NFT 合约授予另一个合约转移代币的权限,以及删除该权限。 You should be following this order:您应该遵循以下顺序:

1.) EOA calls approve(to, tokenID) on the NFT contract 2.) NFT contract has a function that calls approve(address(0), tokenID) 3.) Non-NFT contract calls the function in step 2 --> rever error 1.) EOA 在 NFT 合约上调用 approve(to, tokenID) 2.) NFT 合约有一个调用 approve(address(0), tokenID) 的 function 3.) 非 NFT 合约在步骤 2 中调用 function -->反向错误

You would indeed get this error if Non-NFT contract has not been given approval permission for tokenID.如果非 NFT 合约未获得 tokenID 的批准权限,您确实会收到此错误。 Even though the token owner is calling that function, the context of the call reads the Non-NFT contract address as msg.sender.即使代币所有者正在调用 function,调用的上下文将非 NFT 合约地址读取为 msg.sender。 You could avoid this actually if the function inside the Non-NFT contract made a delegatecall instead of a regular call.如果非 NFT 合约中的 function 进行委托调用而不是常规调用,则实际上可以避免这种情况。

You do not need to use setApprovalForAll if you are only working with one NFT.如果您只使用一个 NFT,则无需使用 setApprovalForAll。 You can give the contract approval by calling approve() and then revoke it with the method you stated above.您可以通过调用 approve() 来批准合同,然后使用上述方法将其撤销。

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

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