-
Notifications
You must be signed in to change notification settings - Fork 1
/
XT-Domain-NFT.sol
727 lines (575 loc) · 27.9 KB
/
XT-Domain-NFT.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
//Contract based on [https://docs.openzeppelin.com/contracts/3.x/erc721](https://docs.openzeppelin.com/contracts/3.x/erc721)
// SPDX-License-Identifier: MIT
pragma solidity 0.8.0;
//pragma experimental ABIEncoderV2;
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
contract XTNFT is ERC721URIStorage, Ownable {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
struct NFTSaleStatsStruct {
mapping(address => uint256) _totalSalePerPaymentToken;
}
NFTSaleStatsStruct private nftSaleStats;
struct NFTSaleStruct {
address _ownerAddress;
address _payerAddress;
uint256 _paidTime;
uint256 _salePrice;
address _tokenForPayment;
uint256 _qtyYear;
}
struct NFTRegisterStruct {
address _ownerAddress;
address _payerAddress;
uint256 _tokenId;
uint256 _beginTime;
uint256 _expiryTime;
bool _forSale;
uint256 _salePrice;
string _tokenURI;
string _nftName;
string _nameXTExt;
uint256 _totalPaidForSubscription;
NFTSaleStruct[] _NFTSaleHistory;
}
mapping(string => NFTRegisterStruct) private nftNameMap;
mapping(uint256 => string) private nftIdNameMap;
//mapping with wallet address for a better performance
struct UserNFTRegisterStruct {
uint256[] _tokenIds;
mapping(uint256 => uint256) _tokenIdArrIndexMap;
}
mapping(address => UserNFTRegisterStruct) private nftUserTokenMap;
//mapping with ext name for a better performance
struct ExtNFTRegisterStruct {
uint256[] _tokenIds;
}
mapping(string => ExtNFTRegisterStruct) private nftExtTokenMap;
using SafeERC20 for IERC20;
uint256 private _activeTime;
// ERC20 basic payment token contracts being held
IERC20 private _tokenForRegisterNFT;
IERC20 private _tokenForMarketPlace;
// beneficiary of payment
address private _newBeneficiary;
address private _beneficiary;
uint256 private _beneficiaryActiveTime;
address private _worker;
string[] private _nameXTExt= [".alt", ".bsc", ".int", ".xt"];
//string[] private _nameIANAExt= [".com"];
uint256 private mintPrice = 30e18;//30 * 10^18 tokens
uint256 private _marketplaceFee = 2e18; // 2 = 2 * 10^18 %
constructor() ERC721("XT-Domain-NFT", "XT-Domain-NFT") {
_activeTime = block.timestamp + 24 hours;
_beneficiary = address(this);
_newBeneficiary = address(this);
_beneficiaryActiveTime = block.timestamp;
_worker = msg.sender;
}
function getWorker() external onlyOwner view returns (address) {
return _worker;
}
//Declare an Event
event UpdateWorker(
address indexed caller,
address indexed worker
);
function updateWorker(address newWorker_) external onlyOwner {
require(
newWorker_ != address(0),
"New worker is the zero address"
);
_worker = newWorker_;
emit UpdateWorker(msg.sender, newWorker_);
}
function getActiveTime() public view returns (uint256)
{
return _activeTime;
}
//Declare an Event
event SetMarketPlaceFee(
address indexed caller,
uint256 indexed marketplaceFee_
);
function setMarketPlaceFee(uint256 marketplaceFee_) external onlyOwner {
_marketplaceFee = marketplaceFee_;
emit SetMarketPlaceFee(msg.sender, marketplaceFee_);
}
//Declare an Event
event AddXTExt(
address indexed caller,
string indexed nameExt_
);
function addXTExt( string memory nameExt_) external onlyOwner {
uint loopCnt = _nameXTExt.length;
for(uint i=0; i< _nameXTExt.length; i++){
if (keccak256(abi.encodePacked(nameExt_)) == keccak256(abi.encodePacked(_nameXTExt[i]))){
loopCnt = i;
break;
}
}
require(
loopCnt == _nameXTExt.length,
"The name extension is already existed."
);
_nameXTExt.push(nameExt_);
emit AddXTExt(msg.sender, nameExt_);
}
//Declare an Event
event RemoveXTExt(
address indexed caller,
string indexed nameExt_
);
function removeXTExt( string memory nameExt_) external onlyOwner {
uint loopCnt = _nameXTExt.length;
for(uint i=0; i< _nameXTExt.length; i++){
if (keccak256(abi.encodePacked(nameExt_)) == keccak256(abi.encodePacked(_nameXTExt[i]))){
loopCnt = i;
break;
}
}
require(
loopCnt < _nameXTExt.length,
"The name extension is not existed."
);
_nameXTExt[loopCnt] = _nameXTExt[_nameXTExt.length - 1];
_nameXTExt.pop();
emit RemoveXTExt(msg.sender, nameExt_);
}
function getNameExt() external view returns (string[] memory) {
return _nameXTExt;
}
//Declare an Event
event SetTokenForPayment(
address indexed caller,
IERC20 indexed token_
);
//Declare an Event
event SetMintPrice(
address indexed caller,
uint indexed newMintPrice
);
function setMintPrice(uint256 newMintPrice) external onlyOwner {
mintPrice = newMintPrice;
emit SetMintPrice(msg.sender, newMintPrice);
}
function getMintPrice() public view virtual returns (uint256) {
return mintPrice;
}
function getCurrentTokenId() external view virtual returns (uint256) {
return _tokenIds.current();
}
/**
* @return the token being held.
*/
function paymentToken(uint paymentId) public view virtual returns (IERC20) {
require(
paymentId <= 2,
"Unknown payment token"
);
if(paymentId == 1) return _tokenForRegisterNFT;
return _tokenForMarketPlace;
}
function setTokenForRegisterNFTPayment(IERC20 token_) external onlyOwner {
_tokenForRegisterNFT = token_;
emit SetTokenForPayment(msg.sender, token_);
}
function setTokenForMarketPlacePayment(IERC20 token_) external onlyOwner {
_tokenForMarketPlace = token_;
emit SetTokenForPayment(msg.sender, token_);
}
function balanceTokenForPayment(uint paymentId) external view virtual returns (uint256) {
require(
paymentId <= 2,
"Unknown payment token"
);
return paymentToken(paymentId).balanceOf(address(this));
}
//Declare an Event
event WithdrawTokenForPayment(
address indexed caller,
address indexed beneficiary_,
uint256 indexed balance_
);
function withdrawTokenForPayment(uint paymentId) external {
require(
paymentId <= 2,
"Unknown payment token"
);
if(_beneficiary != _newBeneficiary && block.timestamp > _beneficiaryActiveTime) _beneficiary = _newBeneficiary;
require(msg.sender == _worker || msg.sender == owner(), "Invalid caller!");
uint256 currentBalance = paymentToken(paymentId).balanceOf(address(this));
// paymentToken(paymentId).safeTransferFrom(address(this), beneficiary(), currentBalance);
paymentToken(paymentId).safeTransfer(beneficiary(), currentBalance);
emit WithdrawTokenForPayment(address(this), beneficiary(), currentBalance);
}
/**
* @return the beneficiary of the tokens.
*/
function beneficiary() public view virtual returns (address) {
return _beneficiary;
}
//Declare an Event
event SetBeneficiary(
address indexed caller,
address indexed beneficiary_,
uint256 indexed activeTime
);
function setBeneficiary( address beneficiary_) external onlyOwner {
require(
beneficiary_ != address(0),
"New beneficiary is the zero address"
);
_newBeneficiary = beneficiary_;
_beneficiaryActiveTime = block.timestamp + 24 hours;
emit SetBeneficiary(msg.sender, beneficiary_, _beneficiaryActiveTime);
}
//Declare an Event
event RegisteredNewNFT(
address indexed caller,
string indexed newNFT
);
function registerNFT(string memory newNFT, uint nameXTExtId_, string memory tokenURI_, uint numOfYear)
external
returns (uint256)
{
require(
block.timestamp > getActiveTime(),
"ActiveTime: You can't register NFT before the active time."
);
if(_beneficiary != _newBeneficiary && block.timestamp > _beneficiaryActiveTime) _beneficiary = _newBeneficiary;
require(paymentToken(1).balanceOf(msg.sender) >= getMintPrice(), "Can't pay nft fee!");
require(bytes(newNFT).length > 0, "Can't be blank!");
require(numOfYear >= 1 && numOfYear <= 10, "Can't be less than 1 year or greater than 10 years!");
require(nameXTExtId_ < _nameXTExt.length , "Out of array.");
paymentToken(1).safeTransferFrom(msg.sender, address(this), numOfYear * getMintPrice());
nftSaleStats._totalSalePerPaymentToken[address(_tokenForRegisterNFT)] += numOfYear * getMintPrice();
//string memory NFTName_ = bytes(newNFT).length > 0 ? string(abi.encodePacked(newNFT, _nameXTExt[nameXTExtId_])) : "";
string memory NFTName_ = string(abi.encodePacked(newNFT, _nameXTExt[nameXTExtId_]));
require(nftNameMap[NFTName_]._tokenId == 0, "The NFT Name has been taken!");
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(address(this), newItemId);
//_mint(msg.sender, newItemId);
nftNameMap[NFTName_]._ownerAddress = msg.sender;//recipient;
nftNameMap[NFTName_]._payerAddress = msg.sender;
nftNameMap[NFTName_]._tokenId = newItemId;
nftNameMap[NFTName_]._beginTime = block.timestamp;
nftNameMap[NFTName_]._expiryTime = block.timestamp + numOfYear * 365 * 86400;
nftNameMap[NFTName_]._forSale = false;
nftNameMap[NFTName_]._salePrice = 0;
nftNameMap[NFTName_]._tokenURI = tokenURI_;
nftNameMap[NFTName_]._nftName = newNFT;
nftNameMap[NFTName_]._nameXTExt = _nameXTExt[nameXTExtId_];
nftNameMap[NFTName_]._totalPaidForSubscription += numOfYear * getMintPrice();
nftNameMap[NFTName_]._NFTSaleHistory.push(NFTSaleStruct(
nftNameMap[NFTName_]._ownerAddress,
msg.sender,
block.timestamp,
getMintPrice(),
address(paymentToken(1)),
numOfYear
));
//use the map of address with tokenIds for a better performance when get the list of tokenIds by an address
nftUserTokenMap[msg.sender]._tokenIds.push(newItemId);
nftUserTokenMap[msg.sender]._tokenIdArrIndexMap[newItemId] = nftUserTokenMap[msg.sender]._tokenIds.length-1;
nftExtTokenMap[_nameXTExt[nameXTExtId_]]._tokenIds.push(newItemId);
nftIdNameMap[newItemId] = NFTName_;
_setTokenURI(nftNameMap[NFTName_]._tokenId, tokenURI_);
//Emit an event
emit RegisteredNewNFT(msg.sender, NFTName_);
return newItemId;
}
//Declare an Event
event ExtendNFTSubscription(
address indexed caller,
string indexed NFTName_
);
function extendNFTSubscription(string memory NFTName_, uint numOfYear)
external
{
require(
block.timestamp > getActiveTime(),
"ActiveTime: You can't register NFT before the active time."
);
if(_beneficiary != _newBeneficiary && block.timestamp > _beneficiaryActiveTime) _beneficiary = _newBeneficiary;
require(paymentToken(1).balanceOf(msg.sender) >= numOfYear * getMintPrice(), "Can't pay nft fee!");
require(bytes(NFTName_).length > 0, "Can't be blank!");
require(numOfYear >= 1 && numOfYear <= 10, "Can't be less than 1 year or greater than 10 years!");
//require(nameXTExtId_ < _nameXTExt.length , "Out of array.");
paymentToken(1).safeTransferFrom(msg.sender, beneficiary(), numOfYear * getMintPrice());
nftSaleStats._totalSalePerPaymentToken[address(_tokenForRegisterNFT)] += numOfYear * getMintPrice();
//string memory NFTName_ = bytes(newNFT).length > 0 ? string(abi.encodePacked(newNFT, _nameXTExt[nameXTExtId_])) : "";
if(nftNameMap[NFTName_]._expiryTime < block.timestamp){
nftNameMap[NFTName_]._expiryTime = block.timestamp + numOfYear * 365 * 86400;
}else{
nftNameMap[NFTName_]._expiryTime = nftNameMap[NFTName_]._expiryTime + numOfYear * 365 * 86400;
}
//nftNameMap[NFTName_]._expiryTime = block.timestamp + numOfYear * 365 * 86400;
nftNameMap[NFTName_]._totalPaidForSubscription += numOfYear * getMintPrice();
nftNameMap[NFTName_]._NFTSaleHistory.push(NFTSaleStruct(
nftNameMap[NFTName_]._ownerAddress,
msg.sender,
block.timestamp,
getMintPrice(),
address(paymentToken(1)),
numOfYear
));
//Emit an event
emit ExtendNFTSubscription(msg.sender, NFTName_);
}
//Declare an Event
event ImportNewNFT(
address indexed caller,
string indexed newNFT
);
function importNFT(address recipient, string memory newNFT, string memory nameXTExt, string memory tokenURI_, uint numOfYear)
external onlyOwner
returns (uint256)
{
require(
block.timestamp > getActiveTime(),
"ActiveTime: You can't register NFT before the active time."
);
if(_beneficiary != _newBeneficiary && block.timestamp > _beneficiaryActiveTime) _beneficiary = _newBeneficiary;
require(
recipient != address(0),
"recipient is the zero address"
);
require(paymentToken(1).balanceOf(msg.sender) >= numOfYear * getMintPrice(), "Can't pay nft fee!");
require(bytes(newNFT).length > 0, "newNFT: Can't be blank!");
require(bytes(tokenURI_).length > 0, "tokenURI: Can't be blank!");
require(numOfYear >= 1 && numOfYear <= 10, "Can't be less than 1 year or greater than 10 years!");
paymentToken(1).safeTransferFrom(msg.sender, beneficiary(), numOfYear * getMintPrice());
nftSaleStats._totalSalePerPaymentToken[address(_tokenForRegisterNFT)] += numOfYear * getMintPrice();
//string memory NFTName_ = bytes(newNFT).length > 0 ? string(abi.encodePacked(newNFT, nameXTExt)) : "";
string memory NFTName_ = string(abi.encodePacked(newNFT, nameXTExt));
require(nftNameMap[NFTName_]._tokenId == 0, "The NFT Name has been taken!");
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(address(this), newItemId);
//_mint(recipient, newItemId);
nftNameMap[NFTName_]._ownerAddress = recipient;
nftNameMap[NFTName_]._payerAddress = msg.sender;
nftNameMap[NFTName_]._tokenId = newItemId;
nftNameMap[NFTName_]._beginTime = block.timestamp;
nftNameMap[NFTName_]._expiryTime = block.timestamp + numOfYear * 365 * 86400;
nftNameMap[NFTName_]._forSale = false;
nftNameMap[NFTName_]._salePrice = 0;
nftNameMap[NFTName_]._tokenURI = tokenURI_;
nftNameMap[NFTName_]._nftName = newNFT;
nftNameMap[NFTName_]._nameXTExt = nameXTExt;
nftNameMap[NFTName_]._totalPaidForSubscription += numOfYear * getMintPrice();
nftNameMap[NFTName_]._NFTSaleHistory.push(NFTSaleStruct(
nftNameMap[NFTName_]._ownerAddress,
msg.sender,
block.timestamp,
getMintPrice(),
address(paymentToken(1)),
numOfYear
));
//use the map of address with tokenIds for a better performance when get the list of tokenIds by an address
nftUserTokenMap[recipient]._tokenIds.push(newItemId);
nftUserTokenMap[recipient]._tokenIdArrIndexMap[newItemId] = nftUserTokenMap[recipient]._tokenIds.length-1;
nftExtTokenMap[nameXTExt]._tokenIds.push(newItemId);
nftIdNameMap[newItemId] = NFTName_;
_setTokenURI(nftNameMap[NFTName_]._tokenId, tokenURI_);
//Emit an event
emit ImportNewNFT(recipient, NFTName_);
return newItemId;
}
//Declare an Event
event ExtendImportedNFTSubscription(
address indexed caller,
string indexed NFTName_,
uint indexed numOfYear
);
function extendImportedNFTSubscription(string memory NFTName_, uint numOfYear)
external onlyOwner
{
require(
block.timestamp > getActiveTime(),
"ActiveTime: You can't register NFT before the active time."
);
if(_beneficiary != _newBeneficiary && block.timestamp > _beneficiaryActiveTime) _beneficiary = _newBeneficiary;
require(paymentToken(1).balanceOf(msg.sender) >= numOfYear * getMintPrice(), "Can't pay nft fee!");
require(bytes(NFTName_).length > 0, "NFTName_: Can't be blank!");
require(numOfYear >= 1 && numOfYear <= 10, "Can't be less than 1 year or greater than 10 years!");
paymentToken(1).safeTransferFrom(msg.sender, beneficiary(), numOfYear * getMintPrice());
nftSaleStats._totalSalePerPaymentToken[address(_tokenForRegisterNFT)] += numOfYear * getMintPrice();
//string memory NFTName_ = bytes(newNFT).length > 0 ? string(abi.encodePacked(newNFT, _nameXTExt[nameXTExtId_])) : "";
if(nftNameMap[NFTName_]._expiryTime < block.timestamp){
nftNameMap[NFTName_]._expiryTime = block.timestamp + numOfYear * 365 * 86400;
}else{
nftNameMap[NFTName_]._expiryTime = nftNameMap[NFTName_]._expiryTime + numOfYear * 365 * 86400;
}
nftNameMap[NFTName_]._totalPaidForSubscription += numOfYear * getMintPrice();
//nftNameMap[NFTName_]._expiryTime = block.timestamp + numOfYear * 365 * 86400;
nftNameMap[NFTName_]._NFTSaleHistory.push(NFTSaleStruct(
nftNameMap[NFTName_]._ownerAddress,
msg.sender,
block.timestamp,
getMintPrice(),
address(paymentToken(1)),
numOfYear
));
//Emit an event
emit ExtendImportedNFTSubscription(msg.sender, NFTName_, numOfYear);
}
//Declare an Event
event BuyNFTFromMarketPlace(
address indexed caller,
string indexed NFTName_
);
function buyNFTFromMarketPlace(string memory NFTName_)
external
{
require(
block.timestamp > getActiveTime(),
"ActiveTime: You can't buy NFT before the current active time."
);
require(nftNameMap[NFTName_]._forSale, "Not for sale for the time being!");
require(bytes(NFTName_).length > 0, "NFTName_: Can't be blank!");
require(paymentToken(2).balanceOf(msg.sender) >= nftNameMap[NFTName_]._salePrice, "Can't pay nft fee!");
//Need to approve this contract before this transaction
//safeTransferFrom( nftNameMap[NFTName_]._ownerAddress, address(this), nftNameMap[NFTName_]._tokenId);
//paymentToken(2).safeTransferFrom(msg.sender, address(this), _marketplaceFee * nftNameMap[NFTName_]._salePrice / 100);
//paymentToken(2).safeTransferFrom(msg.sender, nftNameMap[NFTName_]._ownerAddress, (100 - _marketplaceFee ) * nftNameMap[NFTName_]._salePrice / 100);
paymentToken(2).safeTransferFrom(msg.sender, address(this), _marketplaceFee * nftNameMap[NFTName_]._salePrice/ 1e20);
paymentToken(2).safeTransferFrom(msg.sender, nftNameMap[NFTName_]._ownerAddress, (1e20 - _marketplaceFee ) *nftNameMap[NFTName_]._salePrice /1e20);
nftSaleStats._totalSalePerPaymentToken[address(_tokenForMarketPlace)] += nftNameMap[NFTName_]._salePrice;
//Need to approve this contract before this transaction
//safeTransferFrom( address(this), msg.sender, nftNameMap[NFTName_]._tokenId);
//safeTransferFrom( nftNameMap[NFTName_]._ownerAddress, msg.sender, nftNameMap[NFTName_]._tokenId);
uint arrayLength = nftUserTokenMap[nftNameMap[NFTName_]._ownerAddress]._tokenIds.length;
/*
uint foundId = arrayLength;
for(uint i = 0; i < nftUserTokenMap[nftNameMap[NFTName_]._ownerAddress]._tokenIds.length; i++){
if((nftNameMap[NFTName_]._tokenId == nftUserTokenMap[nftNameMap[NFTName_]._ownerAddress]._tokenIds[i])){
foundId = i;
break;
}
}
*/
//if(foundId < arrayLength){
nftUserTokenMap[nftNameMap[NFTName_]._ownerAddress]._tokenIds[nftUserTokenMap[nftNameMap[NFTName_]._ownerAddress]._tokenIdArrIndexMap[nftNameMap[NFTName_]._tokenId]] = nftUserTokenMap[nftNameMap[NFTName_]._ownerAddress]._tokenIds[arrayLength - 1];
//nftUserTokenMap[nftNameMap[NFTName_]._ownerAddress]._tokenIdArrIndexMap[nftNameMap[NFTName_]._tokenId] = nftUserTokenMap[nftNameMap[NFTName_]._ownerAddress]._tokenIds.length;
nftUserTokenMap[nftNameMap[NFTName_]._ownerAddress]._tokenIds.pop();
/*
nftUserTokenMap[nftNameMap[NFTName_]._ownerAddress]._tokenIds[foundId] = nftUserTokenMap[nftNameMap[NFTName_]._ownerAddress]._tokenIds[arrayLength - 1];
nftUserTokenMap[nftNameMap[NFTName_]._ownerAddress]._tokenIds.pop();
*/
//}
nftNameMap[NFTName_]._ownerAddress = msg.sender;//recipient;
nftNameMap[NFTName_]._forSale = false;
nftNameMap[NFTName_]._salePrice = 0;
nftNameMap[NFTName_]._NFTSaleHistory.push(NFTSaleStruct(
nftNameMap[NFTName_]._ownerAddress,
msg.sender,
block.timestamp,
nftNameMap[NFTName_]._salePrice,
address(paymentToken(2)),
0
));
//use the map of address with tokenIds for a better performance when get the list of tokenIds by an address
nftUserTokenMap[msg.sender]._tokenIds.push(nftNameMap[NFTName_]._tokenId);
nftUserTokenMap[msg.sender]._tokenIdArrIndexMap[nftNameMap[NFTName_]._tokenId] = nftUserTokenMap[msg.sender]._tokenIds.length-1;
//Emit an event
emit BuyNFTFromMarketPlace(msg.sender, NFTName_);
}
//Declare an Event
event SetNFTSalePrice(
address indexed caller,
string indexed NFTName_,
uint256 salePrice_
);
function setNFTSalePrice(string memory NFTName_, uint256 salePrice_) external {
address nftOwner = nftNameMap[NFTName_]._ownerAddress;
require(
nftOwner == msg.sender,
"Ownable: caller is not the current owner"
);
nftNameMap[NFTName_]._forSale = true;
nftNameMap[NFTName_]._salePrice = salePrice_;
emit SetNFTSalePrice(msg.sender, NFTName_, salePrice_);
}
//get tokenIds by address
function getTokenIdsByAddress(address walletAddress, uint256 start, uint256 end) external view returns (uint256[] memory tokenIds)
{
require(start <= end, "Invalid counters");
require(nftUserTokenMap[walletAddress]._tokenIds.length > 0 && nftUserTokenMap[walletAddress]._tokenIds.length > end, "Exceeded array length");
uint256[] memory tokenIdsInterim = new uint256[](100);
uint256 arrayLen = end - start + 1;
for(uint256 i = 0; i < arrayLen; i++){
tokenIdsInterim[i]= nftUserTokenMap[walletAddress]._tokenIds[i + start];
}
return tokenIdsInterim;//nftUserTokenMap[walletAddress]._tokenIds;
}
function getTotalTokenIdsByAddress(address walletAddress) external view returns (uint256 totalTokenIds)
{
return nftUserTokenMap[walletAddress]._tokenIds.length;
}
//get tokenIds by ext name
function getTokenIdsByExt(string memory extName_, uint256 start, uint256 end) external view returns (uint256[] memory tokenIds)
{
require(start <= end, "Invalid counters");
require(nftExtTokenMap[extName_]._tokenIds.length > 0 && nftExtTokenMap[extName_]._tokenIds.length > end, "Exceeded array length");
uint256[] memory tokenIdsInterim = new uint256[](100);
uint256 arrayLen = end - start + 1;
for(uint256 i = 0; i < arrayLen; i++){
tokenIdsInterim[i]= nftExtTokenMap[extName_]._tokenIds[i + start];
}
return tokenIdsInterim;//nftExtTokenMap[extName_]._tokenIds;
}
function getTotalTokenIdsByExt(string memory extName_) external view returns (uint256 totalTokenIds)
{
return nftExtTokenMap[extName_]._tokenIds.length;
}
function getNFTURI(string memory NFTName) external view returns (string memory)
{
require(nftNameMap[NFTName]._expiryTime > block.timestamp, "This NFT has been expired. Owner need to extend its subscription time.");
return tokenURI(nftNameMap[NFTName]._tokenId);
}
function getNFTDataByName(string memory NFTName) external view returns (NFTRegisterStruct memory)
{
return nftNameMap[NFTName];
}
function getNFTDataById(uint256 tokenId_) external view returns (NFTRegisterStruct memory)
{
return nftNameMap[nftIdNameMap[tokenId_]];
}
function getNFTNameById(uint256 tokenId_) external view returns (string memory)
{
return nftIdNameMap[tokenId_];
}
function getNFTSaleStatsPerToken(IERC20 tokenPaymentAddress) external view returns (uint256)
{
return nftSaleStats._totalSalePerPaymentToken[address(tokenPaymentAddress)];
}
//Declare an Event
event SetNFTURI(
address indexed caller,
string indexed NFTName_,
string indexed tokenURI_
);
function setNFTURI(string memory NFTName_, string memory tokenURI_) external
{
//require(paymentToken(1).balanceOf(msg.sender) >= getMintPrice(), "Can't pay nft fee!");
//if(_beneficiary != _newBeneficiary && block.timestamp > _beneficiaryActiveTime) _beneficiary = _newBeneficiary;
address nftOwner = nftNameMap[NFTName_]._ownerAddress;
require(
nftOwner == msg.sender,
"Ownable: caller is not the current owner"
);
//paymentToken(1).safeTransferFrom(msg.sender, beneficiary(), getMintPrice());
require(bytes(NFTName_).length > 0, "NFTName_: Can't be blank!");
require(bytes(tokenURI_).length > 0, "tokenURI: Can't be blank!");
_setTokenURI(nftNameMap[NFTName_]._tokenId, tokenURI_);
nftNameMap[NFTName_]._tokenURI = tokenURI_;
emit SetNFTURI(msg.sender, NFTName_, tokenURI_);
}
}