-
Notifications
You must be signed in to change notification settings - Fork 0
/
NFRoyalT.sol
90 lines (72 loc) · 3.25 KB
/
NFRoyalT.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
contract NFRoyalT2 is ERC721URIStorage, Ownable {
using Strings for uint256;
uint256 public tokenCounter;
address payable public immutable creator;
uint256 public immutable fixedRoyalty;
uint256 public royaltyPercentage;
event RoyaltyPaid(address indexed recipient, uint256 amount);
event NFTTransferred(address indexed from, address indexed to, uint256 tokenId, uint256 salePrice);
constructor(
address payable _creator,
uint256 _fixedRoyalty,
uint256 _royaltyPercentage,
address initialOwner
) ERC721("NFRoyalT2", "NFR2") Ownable(initialOwner) {
require(_creator != address(0), "Invalid creator address");
require(_fixedRoyalty >= 0, "Fixed royalty must be non-negative");
require(_royaltyPercentage >= 0 && _royaltyPercentage <= 100, "Invalid royalty percentage");
tokenCounter = 0;
creator = _creator;
fixedRoyalty = _fixedRoyalty;
royaltyPercentage = _royaltyPercentage;
}
function createNFT(string memory _tokenURI) public onlyOwner returns (uint256) {
uint256 newItemId = tokenCounter;
_safeMint(msg.sender, newItemId);
_setTokenURI(newItemId, _tokenURI);
tokenCounter++;
return newItemId;
}
function brokerTransfer(
address from,
address to,
uint256 tokenId,
uint256 salePrice
) public payable {
require(msg.value >= salePrice, "Insufficient payment");
require(ownerOf(tokenId) == from, "Transfer not authorized by owner");
uint256 royaltyAmountPercentage = (salePrice * royaltyPercentage) / 100;
uint256 royaltyAmount = royaltyAmountPercentage > fixedRoyalty ? royaltyAmountPercentage : fixedRoyalty;
require(royaltyAmount <= salePrice, "Royalty exceeds sale price");
if (royaltyAmount > 0) {
(bool successRoyalty, ) = creator.call{value: royaltyAmount}("");
require(successRoyalty, "Royalty transfer failed");
emit RoyaltyPaid(creator, royaltyAmount);
}
uint256 sellerAmount = salePrice - royaltyAmount;
(bool successSeller, ) = from.call{value: sellerAmount}("");
require(successSeller, "Transfer to seller failed");
_transfer(from, to, tokenId);
emit NFTTransferred(from, to, tokenId, salePrice);
}
function setRoyaltyPercentage(uint256 _royaltyPercentage) external onlyOwner {
require(_royaltyPercentage >= 0 && _royaltyPercentage <= 100, "Invalid royalty percentage");
royaltyPercentage = _royaltyPercentage;
}
function tokenURI(uint256 tokenId) public view override returns (string memory) {
_requireOwned(tokenId);
string memory baseURI = _baseURI();
return bytes(baseURI).length > 0 ? string(abi.encodePacked(baseURI, tokenId.toString())) : "";
}
receive() external payable {
// Function to receive Ether. msg.data must be empty
}
fallback() external payable {
// Function to receive Ether. msg.data is not empty
}
}