-
Notifications
You must be signed in to change notification settings - Fork 11
/
ERC721Azuki.sol
262 lines (233 loc) · 7.92 KB
/
ERC721Azuki.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "./../libraries/Administrated.sol";
import "./ERC721A.sol";
import "./../marketplace/ISplitterContract.sol";
/// @dev we will bring in the openzeppelin ERC721 NFT functionality
contract ERC721Azuki is Administrated, ERC721A {
using Strings for uint256;
string public baseUri;
string public baseExtension = ".json";
/// @dev Object with royalty info
struct RoyaltyInfo {
address receiver;
uint96 royaltyFraction;
}
/// @dev Fallback royalty information
RoyaltyInfo private _defaultRoyaltyInfo;
/// @dev Royalty information
mapping(uint256 => RoyaltyInfo) private _tokenRoyaltyInfo;
/**
* @notice Runs once when the contract is deployed
* @param _name - NFT token name
* @param _symbol - NFT token symbol
* @param _admin - address of the admin
* @param _splitter - address of the Splitter contract
* @param _artist - Arrdess of artist
* @param _primaryDistRecipients - List of primary addresses for distribution
* @param _primaryDistShares - List of primary percentages for distribution
* @param _secondaryDistRecipients - List of secondary addresses for distribution
* @param _secondaryDistShares - List of secondary percentages for distribution
*/
constructor(
string memory _name,
string memory _symbol,
address _admin,
address _owner,
address _splitter,
address _artist,
address[] memory _primaryDistRecipients,
uint256[] memory _primaryDistShares,
address[] memory _secondaryDistRecipients,
uint256[] memory _secondaryDistShares
) ERC721A(_name, _symbol) {
require(_admin != address(0), "zero_addr");
require(_splitter != address(0), "zero_addr");
require(_artist != address(0), "zero_addr");
require(
_primaryDistRecipients.length == _primaryDistShares.length,
"diff_length"
);
require(
_secondaryDistRecipients.length == _secondaryDistShares.length,
"diff_length"
);
changeAdmin(_admin);
_transferOwnership(_owner);
ISplitterContract(_splitter).setPrimaryDistribution(
_artist,
_primaryDistRecipients,
_primaryDistShares
);
ISplitterContract(_splitter).setSecondaryDistribution(
_artist,
_secondaryDistRecipients,
_secondaryDistShares
);
_setDefaultRoyalty(
_secondaryDistRecipients[0],
uint96(_secondaryDistShares[0])
);
}
/**
* @dev Mint tokens to admin wallet
*/
function mint(uint256 _quantity) external onlyAdmin {
require(_quantity > 0, "zero_amount");
_safeMint(admin(), _quantity);
}
/**
* @dev Mint tokens to external address
*/
function mintTo(address _receiver, uint256 _quantity) external onlyAdmin {
require(_quantity > 0, "zero_amount");
_safeMint(_receiver, _quantity);
}
/**
* @dev Returns how much royalty is owed and to whom, based on a sale price that may be denominated in any unit of
* exchange. The royalty amount is denominated and should be payed in that same unit of exchange.
*/
function royaltyInfo(uint256 _tokenId, uint256 _salePrice)
external
view
returns (address, uint256)
{
RoyaltyInfo memory royalty = _tokenRoyaltyInfo[_tokenId];
if (royalty.receiver == address(0)) {
royalty = _defaultRoyaltyInfo;
}
uint256 royaltyAmount = (_salePrice * royalty.royaltyFraction) /
_feeDenominator();
return (royalty.receiver, royaltyAmount);
}
function setTokenRoyalty(
uint256 tokenId,
address receiver,
uint96 feeNumerator
) external onlyAdmin {
_setTokenRoyalty(tokenId, receiver, feeNumerator);
}
function setDefaultRoyalty(address receiver, uint96 feeNumerator)
external
onlyAdmin
{
_setDefaultRoyalty(receiver, feeNumerator);
}
function deleteDefaultRoyalty() external onlyAdmin {
_deleteDefaultRoyalty();
}
function resetTokenRoyalty(uint256 tokenId) external onlyAdmin {
_resetTokenRoyalty(tokenId);
}
/**
* @dev The denominator with which to interpret the fee set in {_setTokenRoyalty} and {_setDefaultRoyalty} as a
* fraction of the sale price. Defaults to 10000 so fees are expressed in basis points, but may be customized by an
* override.
*/
function _feeDenominator() internal pure returns (uint96) {
return 10000;
}
/**
* @dev Sets the royalty information that all ids in this contract will default to.
*
* Requirements:
*
* - `receiver` cannot be the zero address.
* - `feeNumerator` cannot be greater than the fee denominator.
*/
function _setDefaultRoyalty(address receiver, uint96 feeNumerator)
internal
{
require(feeNumerator <= _feeDenominator(), "fee exceed salePrice");
require(receiver != address(0), "invalid receiver");
_defaultRoyaltyInfo = RoyaltyInfo(receiver, feeNumerator);
}
/**
* @dev Removes default royalty information.
*/
function _deleteDefaultRoyalty() internal {
delete _defaultRoyaltyInfo;
}
/**
* @dev Sets the royalty information for a specific token id, overriding the global default.
*
* Requirements:
*
* - `tokenId` must be already minted.
* - `receiver` cannot be the zero address.
* - `feeNumerator` cannot be greater than the fee denominator.
*/
function _setTokenRoyalty(
uint256 tokenId,
address receiver,
uint96 feeNumerator
) internal {
require(feeNumerator <= _feeDenominator(), "fee exceed salePrice");
require(receiver != address(0), "invalid parameters");
_tokenRoyaltyInfo[tokenId] = RoyaltyInfo(receiver, feeNumerator);
}
/**
* @dev Resets royalty information for the token id back to the global default.
*/
function _resetTokenRoyalty(uint256 tokenId) internal {
delete _tokenRoyaltyInfo[tokenId];
}
/**
* @dev See {ERC721-_burn}. This override additionally clears the royalty information for the token.
*/
function _burn(uint256 tokenId) internal virtual override {
super._burn(tokenId);
_resetTokenRoyalty(tokenId);
}
function tokensOfOwner(address _owner)
external
view
returns (uint256[] memory)
{
uint256 tokenCount = balanceOf(_owner);
if (tokenCount == 0) {
return new uint256[](0);
} else {
uint256[] memory result = new uint256[](tokenCount);
uint256 index;
for (index = 0; index < tokenCount; index++) {
result[index] = tokenOfOwnerByIndex(_owner, index);
}
return result;
}
}
function setBaseURI(string memory newBaseURI) public onlyAdmin {
baseUri = newBaseURI;
}
function setBaseExtension(string memory _newBaseExtension)
public
onlyAdmin
{
baseExtension = _newBaseExtension;
}
function _baseURI() internal view virtual override returns (string memory) {
return baseUri;
}
function tokenURI(uint256 tokenId)
public
view
override(ERC721A)
returns (string memory)
{
require(
_exists(tokenId),
"ERC721Metadata: URI query for nonexistent token"
);
string memory currentBaseURI = _baseURI();
return
bytes(currentBaseURI).length > 0
? string(
abi.encodePacked(
currentBaseURI,
tokenId.toString(),
baseExtension
)
)
: "";
}
}