Recently I am working on a NFT marketplace and I had difficulties in implementing safeTransferFrom function. At this stage I use _transfer function to allow the buyer calling the function directly so that the token can be transferred from the owner to the buyer. I read the documentation the better way to do so is using SetApproval and safeTransferFrom. In that case, how do I implement these functions so that the buyer can still call transfer from function without getting "caller is not the owner/not approved" error. Below is my code:
function buyingWithoutApproval (uint _tokenId) external payable {
require (ownerOf(_tokenId) != msg.sender);
Item storage _item = items[_tokenId];
require(_item.forSale == true);
address _from = ownerOf(_tokenId);
_transfer(_from, msg.sender, _tokenId);
itemToOwner[_tokenId] = msg.sender;
_item.owner = msg.sender;
_item.price = 0;
_item.forSale = false;
from here the buyer can call this function to 'buy' the NFT token. What is the standard way to do so with Approval and safeTransferFrom? Much appreciated with example code is shared! Thanks!