ERC721 Smart contract revoke approval

770 Views Asked by At

I've some question about ERC721 processing. I'd like to make an user to give aproval to an external smart contract for transfering one of its NFT. To do it I'm using 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)

At this step, I got the following error: 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)? What about others NFT from the same collection owned by the user ? 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 ?

1

There are 1 best solutions below

0
On

It sounds like you have 2 contracts and an EOA involved in this process. You want the NFT contract to give another contract permission to transfer tokens, as well as remove that permission. 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

You would indeed get this error if Non-NFT contract has not been given approval permission for tokenID. Even though the token owner is calling that function, the context of the call reads the Non-NFT contract address as msg.sender. You could avoid this actually if the function inside the Non-NFT contract made a delegatecall instead of a regular call.

You do not need to use setApprovalForAll if you are only working with one NFT. You can give the contract approval by calling approve() and then revoke it with the method you stated above.