mapping(uint => uint) public starsForSale;
function _make_payable(address account_) internal pure returns(address payable) {
return payable(account_);
}
function buyStar(uint256 tokenId_) public payable {
require(starsForSale[tokenId_] > 0, "The star should be up for sale");
uint256 starCost = starsForSale[tokenId_];
address ownerAddress = ownerOf(tokenId_);
require(msg.value >= starCost, "Your ETH balance must be >= the price of star");
safeTransferFrom(ownerAddress, msg.sender, tokenId_);
address payable payableOwnerAddress = _make_payable(ownerAddress);
address payable payableMsgSender = _make_payable(msg.sender);
payableOwnerAddress.transfer(starCost);
if(msg.value > starCost) {
payableMsgSender.transfer(msg.value - starCost);
}
emit Transfer(ownerAddress, msg.sender, tokenId_);
}
}
Just started learning solidity and I intend to create an NFT marketplace. Each time I attempt to execute buyStar()
using a different address, I get the following error:
Error: Returned error: VM Exception while processing transaction: revert ERC721: transfer caller is not owner nor approved -- Reason given: ERC721: transfer caller is not owner nor approved.