-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTartist.sol
286 lines (242 loc) · 10.3 KB
/
Tartist.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/security/PullPayment.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "./Tarti.sol";
contract Tartist is ERC721URIStorage, ERC721Enumerable, PullPayment, Ownable {
using Counters for Counters.Counter;
uint256 public constant MINT_TARTIST_PRICE = 0.17 ether;
uint256 public constant MINT_TARTI_PRICE = 0.047 ether;
uint256[] public allTraits;
mapping(uint256 => uint256[]) public botTraits;
mapping(uint256 => uint256[]) public botTraitDominances;
mapping(uint256 => string) public availableTraits;
mapping(uint256 => string[]) public botTraitValues;
mapping(bytes32 => bool) private _usedTraitComboHashes;
//Tartist owner can set to greater than zero, allowing other to use their Tartist to make art
mapping(uint8 => uint256) public tartiRoyaltyRate;
address private _tartiAddr;
Counters.Counter private _currentTokenId;
mapping(string => uint256) private _allTraitsByName;
bytes private constant _newMetadataCid =
"QmYKXrpFmABTH7VwfMWvXXRtMJWDcTTmB3vQXSSd2rEkYR";
bytes private constant _newMetadataUri =
"ipfs://QmYKXrpFmABTH7VwfMWvXXRtMJWDcTTmB3vQXSSd2rEkYR";
bytes private constant _inProcessMetadataCid =
"QmXUHvDvfFkk6f4GeiX5tpENh2aAyHYwp5BuzEhCTo79kF";
bytes private constant _inProcessMetadataUri =
"ipfs://QmXUHvDvfFkk6f4GeiX5tpENh2aAyHYwp5BuzEhCTo79kF";
event PermanentURI(string _value, uint256 indexed _id);
constructor() ERC721("Tarti Artist", "TARTIST") {}
/**
@dev
`traitName` will either be:
- The name of the Trait class (ie MusicProducer)
- The name of the Trait with dot then the name of the trait prop appended (ie DynMusicProductionStyle.FavoriteKeys)
-- In this case the TraitAI trait being added to the bot is DynMusicProductionStyle,
-- and the prop the value wll map to is FavoriteKeys.
-- This flat structure will be nice for the NFT metadata to be standard and easy.
-- When it gets sent into the TraitHttpIO we will need to pull out the props and put them all beneath the same single trait.
*/
function addTraits(
uint256[] calldata traitCode,
string[] calldata traitName
) public onlyOwner {
for (uint i = 0; i < traitCode.length; i++) {
require(bytes(availableTraits[traitCode[i]]).length == 0, "traitiddup");
require(_allTraitsByName[traitName[i]] == 0, "traitdup");
availableTraits[traitCode[i]] = traitName[i];
_allTraitsByName[traitName[i]] = traitCode[i];
allTraits.push(traitCode[i]);
}
}
function cancelTrait(uint256 traitCode) public onlyOwner {
_allTraitsByName[availableTraits[traitCode]] = 0;
availableTraits[traitCode] = "";
}
function getAllTraits() external view returns (uint256[] memory) {
return allTraits;
}
/**
Mint a TARTIST token
This function `giveBrith` should be used in place of the traditional `mintTo`.
*/
function giveBirth(
address recipient,
uint256[] memory traits,
string[] memory dynamicTraitValues,
uint8[] memory traitDominance
) public payable returns (uint256) {
require(msg.value == MINT_TARTIST_PRICE, "missingcommission");
require(traits.length < 100, "toomanytraits");
require(dynamicTraitValues.length < 100, "toomanytraitvals");
require(traitDominance.length < 100, "toomanytraitdoms");
for (uint256 i = 0; i < traits.length; i++) {
require(
bytes(availableTraits[traits[i]]).length > 0,
"invalidtrait"
);
}
bytes memory traitBytes;
for (uint256 i = 0; i < traits.length; i++) {
traitBytes = abi.encodePacked(traitBytes, traits[i]);
}
bytes memory dynTraitValuesBytes;
for (uint256 i = 0; i < dynamicTraitValues.length; i++) {
dynTraitValuesBytes = abi.encodePacked(
dynTraitValuesBytes,
dynamicTraitValues[i]
);
}
bytes32 botTraitsHash = keccak256(
bytes.concat(traitBytes, dynTraitValuesBytes)
);
require(_usedTraitComboHashes[botTraitsHash] != true, "geneticcopy");
_usedTraitComboHashes[botTraitsHash] = true;
_currentTokenId.increment();
uint256 newItemId = _currentTokenId.current();
botTraits[newItemId] = traits;
botTraitValues[newItemId] = dynamicTraitValues;
botTraitDominances[newItemId] = traitDominance;
_safeMint(recipient, newItemId);
_setTokenURI(newItemId, string(abi.encodePacked(_newMetadataCid)));
_asyncTransfer(owner(), MINT_TARTIST_PRICE);
return newItemId;
}
function setRoyaltyRate(uint8 artistId, uint256 ratePerTarti) public {
require(msg.sender == ownerOf(artistId), "norights");
tartiRoyaltyRate[artistId] = ratePerTarti;
}
/**
Mint a TARTI token.
You cannot mint a TARTI token directly on the TARTI contract.
You must do it through this (TARTIST) contract using the `newArt` function.
Minter must send MINT_TARTI_PRICE + whatever royalty the TARTIST owner has set.
*/
function newArt(uint8 artistId) public payable returns (uint256) {
//address canot be blank
require(_tartiAddr != address(0), "tarticontractnotset");
//call newArt on the Tarti contract
//Tarti only allows this contract to call that method
//First check that the caller is allowed to use this tartist, either by owning it or paying royalties
//norights = specified artist is not contractually obligated, nor even allowed, to make any art for you.
if (tartiRoyaltyRate[artistId] == 0) {
//if royalty rate is not set, then only the owner can make art with this Tartist
require(msg.sender == ownerOf(artistId), "norights");
require(msg.value == MINT_TARTI_PRICE, "missingcommission"); //.018 eth
}
if (tartiRoyaltyRate[artistId] > 0) {
//if royalty rate is not set, then anyone can make art with this Tartist but they must pay the royalty.
require(
msg.value == MINT_TARTI_PRICE + tartiRoyaltyRate[artistId],
"missingcommissionroyals"
); //.018 eth + royalty
//mark the royalty as payable to Tartist owner (can be withdrawn using pull payment)
_asyncTransfer(ownerOf(artistId), tartiRoyaltyRate[artistId]);
}
//use the Tarti contract to create a new Tarti token
//Trait engine will see the new Tarti on the blockchain and create the art/music
Tarti tarti = Tarti(_tartiAddr);
//mint the tarti token
uint256 newTartiToken = tarti.newArt(msg.sender, artistId);
//mark payment payable to contract owner
_asyncTransfer(owner(), MINT_TARTI_PRICE);
return newTartiToken;
}
/// @dev Returns an URI for a given token ID
function _baseURI() internal pure override returns (string memory) {
return "ipfs://";
}
function contractURI() public pure returns (string memory) {
return "http://tartipublicfiles.tartiart.com/Tartist.metadata.json";
}
function setTartiAddr(address tartiAddr) public onlyOwner {
_tartiAddr = tartiAddr;
}
function setCreationStarted(
uint256 tokenId,
bool onTarti
) public onlyOwner {
if (onTarti) {
require(_tartiAddr != address(0), "tarticontractnotset");
Tarti tarti = Tarti(_tartiAddr);
return tarti.setCreationStarted(tokenId);
}
bytes32 tokenUriBytesHash = keccak256(bytes(tokenURI(tokenId)));
require(
tokenUriBytesHash == keccak256(abi.encodePacked(_newMetadataUri)),
"tartistnotnew"
);
_setTokenURI(tokenId, string(abi.encodePacked(_inProcessMetadataCid)));
}
function setCreated(
uint256 tokenId,
bytes calldata cid,
bool onTarti
) public onlyOwner {
if (onTarti) {
require(_tartiAddr != address(0), "tarticontractnotset");
Tarti tarti = Tarti(_tartiAddr);
return tarti.setCreated(tokenId, cid);
}
bytes32 tokenUriBytesHash = keccak256(bytes(tokenURI(tokenId)));
require(
tokenUriBytesHash ==
keccak256(abi.encodePacked(_inProcessMetadataUri)),
"tartistnotstarted"
);
string memory newUri = string(abi.encodePacked(cid));
_setTokenURI(tokenId, newUri);
emit PermanentURI(newUri, tokenId);
}
function getTraits(
uint256 tokenId
) external view returns (uint256[] memory) {
return botTraits[tokenId];
}
function getTraitValues(
uint256 tokenId
) external view returns (string[] memory) {
return botTraitValues[tokenId];
}
function getTraitDominances(
uint256 tokenId
) external view returns (uint256[] memory) {
return botTraitDominances[tokenId];
}
/// @dev Overridden in order to retrict caller to payee or owner
function withdrawPayments(address payable payee) public virtual override {
//Only allow payee to request withdrawl.
//Help protect against gas forwarding attack.
require(
msg.sender == payee || msg.sender == owner(),
"unauthwithdrawl"
);
super.withdrawPayments(payee);
}
function _burn(
uint256 tokenId
) internal override(ERC721, ERC721URIStorage) {
super._burn(tokenId);
}
function _beforeTokenTransfer(
address from,
address to,
uint256 firstTokenId,
uint256 batchSize
) internal override(ERC721, ERC721Enumerable) {
super._beforeTokenTransfer(from, to, firstTokenId, batchSize);
}
function supportsInterface(
bytes4 interfaceId
) public view override(ERC721Enumerable, ERC721URIStorage) returns (bool) {
return super.supportsInterface(interfaceId);
}
function tokenURI(
uint256 tokenId
) public view override(ERC721, ERC721URIStorage) returns (string memory) {
return ERC721URIStorage.tokenURI(tokenId);
}
}