// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/interfaces/IERC2981.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Burnable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
contract MAGNETNFT is
ERC721,
ERC721URIStorage,
ERC721Burnable,
Ownable,
IERC2981
{
using Counters for Counters.Counter;
Counters.Counter private _tokenIdCounter;
constructor() ERC721("MAGNETNFT", "MNFT") {}
function safeMint(
address to,
string memory uri,
uint256 royaltyPercentage
) public onlyOwner {
uint256 tokenId = _tokenIdCounter.current();
_tokenIdCounter.increment();
_safeMint(to, tokenId);
_setTokenURI(tokenId, uri);
_setRoyaltyPercentage(tokenId, royaltyPercentage);
_registerInterface(_INTERFACE_ID_ERC721);
_registerInterface(_INTERFACE_ID_ERC721_METADATA);
_registerInterface(_INTERFACE_ID_ERC721_ENUMERABLE);
// Royalties interface
_registerInterface(_INTERFACE_ID_ERC2981);
}
bytes4 private constant _INTERFACE_ID_ERC2981 = 0x2a55205a;
function checkRoyalties(address _contract) internal returns (bool) {
(bool success) = IERC165(_contract).supportsInterface(_INTERFACE_ID_ERC2981);
return success;
}
function _setRoyaltyPercentage(uint256 tokenId, uint256 royaltyPercentage)
internal
{}
//supporting IERC2981 interface
function supportsInterface(bytes4 interfaceId) public view override(ERC721, ERC2981) returns (bool)
{
return super.supportsInterface(interfaceId);
}
// The following functions are overrides required by Solidity.
function _burn(uint256 tokenId)
internal
override(ERC721, ERC721URIStorage)
{
super._burn(tokenId);
}
//onlyOwner callable self destruction function impl
function selfDestroy() public onlyOwner {
selfdestruct(payable(msg.sender));
}
function tokenURI(uint256 tokenId)
public
view
override(ERC721, ERC721URIStorage)
returns (string memory)
{
return super.tokenURI(tokenId);
}
function royaltyInfo(uint256 tokenId, uint256 value)
public
view
returns (address receiver, uint256 royaltyAmount)
{
// Get the creator of the token
address creator = ownerOf(tokenId);
// Calculate the royalty amount as 10% of the value
uint256 _royaltyAmount = value / 10;
return (creator, _royaltyAmount);
}
function updateRoyaltyFee(
uint256 tokenId,
uint256 value,
uint256 rate
) public view returns (uint256 royaltyAmount) {
require(
_msgSender() == ownerOf(tokenId),
"Only the owner can update royalty fee"
);
// Get the creator of the token
address creator = ownerOf(tokenId);
uint256 _royaltyAmount = value / rate;
return (creator, _royaltyAmount);
}
}