-
Notifications
You must be signed in to change notification settings - Fork 1
/
COTNFT.sol
222 lines (194 loc) · 7.68 KB
/
COTNFT.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Base64.sol";
pragma solidity ^0.8.0;
contract COTNFT is ERC721{
using Counters for Counters.Counter;
struct NFTInfo{
string imageUrl;
string background;
string wing;
string body;
string hat;
string eye;
string sdg;
uint256 cot;
bool neutralized;
}
struct ProjectInfo{
string projectName;
string projectTitle;
string location;
string registry;
string methodology;
uint256 totalCOT;
}
ProjectInfo public projectInfo;
Counters.Counter private _childTokenIdCounter;
uint256 private _totalSupply = 0;
uint256 public maxCOT = 800000;
uint256 public totalCOTMinted = 0;
uint256[] private _mintedTokenIds;
mapping(uint256 => NFTInfo) private _tokenInfo;
mapping(address => uint256[]) private _tokensOfAddress;
constructor(ProjectInfo memory _projectInfo) ERC721("DCCMdNFT", "DCCM") {
projectInfo = _projectInfo;
_childTokenIdCounter._value = 101;
}
function uint2str(uint256 _i)
internal
pure
returns (string memory _uintAsString)
{
if (_i == 0) {
return "0";
}
uint j = _i;
uint len;
while (j != 0) {
len++;
j /= 10;
}
bytes memory bstr = new bytes(len);
uint k = len;
while (_i != 0) {
k = k - 1;
uint8 temp = (48 + uint8(_i - (_i / 10) * 10));
bytes1 b1 = bytes1(temp);
bstr[k] = b1;
_i /= 10;
}
return string(bstr);
}
//for opensea
function contractURI() public view returns (string memory) {
return string(
abi.encodePacked(
"data:application/json;base64,",
Base64.encode(
bytes(
abi.encodePacked(
'{"name":"',
projectInfo.projectName,
'", "description":"',projectInfo.projectTitle,'"}'
)
)
)
)
);
}
function totalSupply() public view returns(uint256){
return _totalSupply;
}
function mintedTokenIds() public view returns(uint256[] memory) {
return _mintedTokenIds;
}
function split(uint256 tokenId, NFTInfo[] memory nftArray) public {
require(_exists(tokenId), "nft of the tokenId does not exist");
require(_isApprovedOrOwner(_msgSender(), tokenId), "split caller is not owner nor approved");
require(nftArray.length != 0, "nftArray length zero");
NFTInfo storage originalNFT = _tokenInfo[tokenId];
require(originalNFT.neutralized == false, "token has neutralized");
uint256 cotTotal = 0;
for (uint256 i = 0; i < nftArray.length; i++) {
cotTotal += nftArray[i].cot;
}
require(cotTotal <= originalNFT.cot, "cot amount error");
for (uint256 i = 0; i < nftArray.length; i++) {
NFTInfo memory info = nftArray[i];
if (i==0) {
originalNFT.wing = info.wing;
originalNFT.background = info.background;
originalNFT.body = info.body;
originalNFT.hat = info.body;
originalNFT.eye = info.eye;
originalNFT.sdg = info.sdg;
originalNFT.imageUrl = info.imageUrl;
originalNFT.cot = info.cot;
}else {
uint256 newTokenId = _childTokenIdCounter.current();
_childTokenIdCounter.increment();
_totalSupply+=1;
_tokenInfo[newTokenId] = info;
_tokensOfAddress[_msgSender()].push(newTokenId);
_mintedTokenIds.push(newTokenId);
_safeMint(_msgSender(), newTokenId);
}
}
}
function neutralize(uint256 tokenId) public {
require(_exists(tokenId), "nft of the tokenId does not exist");
require(_isApprovedOrOwner(_msgSender(), tokenId), "neutralize caller is not owner nor approved");
NFTInfo storage nft = _tokenInfo[tokenId];
require(!nft.neutralized, "nft already neutralized");
nft.neutralized = true;
}
function mint(uint256 tokenId, address to, NFTInfo memory nftInfo) public {
require(!_exists(tokenId), "nft is already minted");
require(totalCOTMinted + nftInfo.cot <= maxCOT, "all nft has been minted");
totalCOTMinted += nftInfo.cot;
_totalSupply += 1;
_tokenInfo[tokenId] = nftInfo;
_tokensOfAddress[_msgSender()].push(tokenId);
_mintedTokenIds.push(tokenId);
_safeMint(to, tokenId);
}
function burn(uint256 tokenId) public {
require(_isApprovedOrOwner(_msgSender(), tokenId), "burn caller is not owner nor approved");
_burn(tokenId);
}
function _assembleMetadata(uint256 tokenId) private view returns (string memory) {
NFTInfo memory nft = _tokenInfo[tokenId];
string memory cot = uint2str(nft.cot);
string memory totalCOT = uint2str(projectInfo.totalCOT);
string memory neutralized = nft.neutralized ? "true" : "false";
return string(
abi.encodePacked(
"data:application/json;base64,",
Base64.encode(
bytes(
abi.encodePacked(
'{"image":"',
nft.imageUrl,
'","attributes":[{"trait_type":"Project title","value":"',
projectInfo.projectTitle,
'"},{"trait_type":"Total COT","value":"',
totalCOT,
'"},{"trait_type":"Registry","value":"',
projectInfo.registry,
'"},{"trait_type":"methodology","value":"',
projectInfo.methodology,
'"},{"trait_type":"wing","value":"',
nft.wing,
'"},{"trait_type":"body","value":"',
nft.body,
'"},{"trait_type":"hat","value":"',
nft.hat,
'"},{"trait_type":"eye","value":"',
nft.eye,
'"},{"trait_type":"sdg","value":"',
nft.sdg,
'"},{"trait_type":"neutralized","value":"',
neutralized,
'"},{"display_type":"number","trait_type":"COT(t)","value":"',
cot,
'"}]}'
)
)
)
)
);
}
function tokenURI(uint256 tokenId) public view override returns (string memory) {
return _assembleMetadata(tokenId);
}
function getNFTInfo(uint256 tokenId) public view returns (NFTInfo memory nftInfo) {
nftInfo = _tokenInfo[tokenId];
}
function getTokensOfAddress(address _sender) public view returns (uint256[] memory) {
return _tokensOfAddress[_sender];
}
}