I have two contracts, one that issues a token and gives the approval for the Marketplace contract. And the other, Marketplace, allows you to put a token for sale and sell the token.
My problem happens in the following scenario: User A issues the token and gives permission to the Marketplace User A puts the token up for sale through the Marketplace User B buys the token through the Marketplace User B puts the token for sale -> Error because the Marketplace doesn't have the approval
That's why I want to give the approval for the Marketplace in the same sales function, however, it's giving an error.
function buyItem(uint256 id)
ItemExists(id)
IsForSale(id)
HasTransferApproval(itemsForSale[id].tokenId)
payable
external {
require(msg.value >= itemsForSale[id].price, "Not enough funds sent");
require(msg.sender != itemsForSale[id].seller);
itemsForSale[id].isSold = true;
activeItems[itemsForSale[id].tokenId] = false;
token.safeTransferFrom(itemsForSale[id].seller, msg.sender, itemsForSale[id].tokenId);
itemsForSale[id].seller.transfer(msg.value);
// token.approve(address(this), itemsForSale[id].tokenId, {from: msg.sender}); // I want this line to work
emit itemSold(id, msg.sender, itemsForSale[id].price);
}