-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path420.sol
197 lines (159 loc) · 7.86 KB
/
420.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v5.2/contracts/token/ERC1155/ERC1155.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v5.2/contracts/access/Ownable.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v5.2/contracts/token/ERC20/utils/SafeERC20.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v5.2/contracts/utils/ReentrancyGuard.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v5.2/contracts/utils/Strings.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v5.2/contracts/utils/Address.sol";
contract NFTCollection is ERC1155, Ownable, ReentrancyGuard {
using SafeERC20 for IERC20;
using Address for address payable;
string public constant contractName = "420";
string public collectionName;
string public collectionURI;
struct MintData {
uint256 mintID;
address[] tokenAddresses;
mapping(address => bool) hasToken;
}
uint256 public constant MAX_MINT_PER_WALLET = 3;
uint256 public constant VESTING_PERIOD = 90 days;
uint256 public constant VESTING_INTERVAL = 30 days;
uint256 public constant VESTING_PERCENTAGE = 10;
bool public mintActive;
uint256 public mintPrice = 0.1 ether;
uint256 public maxMintSupply = 1000;
uint256 public currentMintID;
uint256 public currentMintSupply;
mapping(address => uint256) public allocatedContractTokens;
mapping(address => uint256) public userMintedCount;
mapping(uint256 => MintData) public mintTokens;
mapping(uint256 => mapping(address => uint256)) public mintTokenInitialBalance;
mapping(uint256 => mapping(address => uint256)) public mintTokenWithdrawn;
mapping(uint256 => mapping(address => uint256)) public mintTokenVestDate;
event TokensDeposited(uint256 indexed mintID, address indexed token, uint256 amount);
event MintCreated(address indexed recipient, uint256 mintID);
event TokensWithdrawn(address indexed user, uint256 mintID, address token, uint256 amount);
event CollectionURIUpdated(string newURI);
event MintStateChanged(bool active);
event ETHCleared(uint256 amount);
event ERC20Cleared(address token, uint256 amount);
constructor(
string memory _collectionName,
string memory _collectionURI
) ERC1155(_collectionURI) Ownable(msg.sender) {
collectionName = _collectionName;
collectionURI = _collectionURI;
}
modifier validMint(uint256 mintID) {
require(mintID <= currentMintID && mintID > 0, "Invalid mint ID");
_;
}
function clearETH() external onlyOwner nonReentrant {
uint256 balance = address(this).balance;
require(balance > 0, "No ETH to clear");
payable(owner()).sendValue(balance);
emit ETHCleared(balance);
}
function clearERC20(address token) external onlyOwner nonReentrant {
uint256 availableBalance = IERC20(token).balanceOf(address(this)) - allocatedContractTokens[token];
require(availableBalance > 0, "No available tokens");
IERC20(token).safeTransfer(owner(), availableBalance);
emit ERC20Cleared(token, availableBalance);
}
function setMintActive(bool active) external onlyOwner {
mintActive = active;
emit MintStateChanged(active);
}
function setCollectionURI(string memory newURI) external onlyOwner {
collectionURI = newURI;
emit CollectionURIUpdated(newURI);
}
function setCollectionName(string memory newName) external onlyOwner {
collectionName = newName;
}
function depositTokens(uint256 mintID, address token, uint256 amount) external onlyOwner {
require(token != address(0), "Invalid token address");
require(amount > 0, "Invalid amount");
MintData storage data = mintTokens[mintID];
if (!data.hasToken[token]) {
data.tokenAddresses.push(token);
data.hasToken[token] = true;
mintTokenVestDate[mintID][token] = block.timestamp + VESTING_PERIOD;
}
IERC20(token).safeTransferFrom(msg.sender, address(this), amount);
mintTokenInitialBalance[mintID][token] += amount;
allocatedContractTokens[token] += amount;
emit TokensDeposited(mintID, token, amount);
}
function adminMint(address recipient, uint256 quantity) external onlyOwner {
require(recipient != address(0), "Invalid recipient");
require(quantity <= 50 && quantity > 0, "Invalid quantity");
require(currentMintSupply + quantity <= maxMintSupply, "Exceeds max supply");
for (uint256 i = 0; i < quantity; i++) {
_mint(recipient, ++currentMintID, 1, "");
currentMintSupply++;
emit MintCreated(recipient, currentMintID);
}
}
function mint() external payable nonReentrant {
require(mintActive, "Minting inactive");
require(msg.value >= mintPrice, "Insufficient ETH");
require(currentMintSupply < maxMintSupply, "Max supply reached");
require(userMintedCount[msg.sender] < MAX_MINT_PER_WALLET, "Max 3 mints per wallet");
_mint(msg.sender, ++currentMintID, 1, "");
currentMintSupply++;
userMintedCount[msg.sender]++;
emit MintCreated(msg.sender, currentMintID);
if (msg.value > mintPrice) {
payable(msg.sender).sendValue(msg.value - mintPrice);
}
}
function burn(uint256 mintID) external validMint(mintID) {
require(balanceOf(msg.sender, mintID) == 1, "Not owner");
MintData storage data = mintTokens[mintID];
for (uint256 i = 0; i < data.tokenAddresses.length; i++) {
address token = data.tokenAddresses[i];
uint256 remaining = mintTokenInitialBalance[mintID][token] - mintTokenWithdrawn[mintID][token];
allocatedContractTokens[token] -= remaining;
delete mintTokenInitialBalance[mintID][token];
delete mintTokenWithdrawn[mintID][token];
delete mintTokenVestDate[mintID][token];
}
delete mintTokens[mintID];
_burn(msg.sender, mintID, 1);
currentMintSupply--;
}
function withdraw(uint256 mintID) external validMint(mintID) nonReentrant {
require(balanceOf(msg.sender, mintID) == 1, "Not owner");
MintData storage data = mintTokens[mintID];
for (uint256 i = 0; i < data.tokenAddresses.length; i++) {
address token = data.tokenAddresses[i];
_processWithdrawal(mintID, token);
}
}
function uri(uint256) public view override returns (string memory) {
return collectionURI;
}
function getMintTokens(uint256 mintID) external view returns (address[] memory) {
return mintTokens[mintID].tokenAddresses;
}
function _processWithdrawal(uint256 mintID, address token) private {
require(block.timestamp >= mintTokenVestDate[mintID][token], "Vesting not started");
uint256 timePassed = block.timestamp - mintTokenVestDate[mintID][token];
uint256 intervals = timePassed / VESTING_INTERVAL;
intervals = intervals > 10 ? 10 : intervals;
uint256 totalWithdrawable = (mintTokenInitialBalance[mintID][token] * VESTING_PERCENTAGE * intervals) / 100;
uint256 available = totalWithdrawable - mintTokenWithdrawn[mintID][token];
if (available > 0) {
mintTokenWithdrawn[mintID][token] += available;
allocatedContractTokens[token] -= available;
IERC20(token).safeTransfer(msg.sender, available);
emit TokensWithdrawn(msg.sender, mintID, token, available);
}
}
function supportsInterface(bytes4 interfaceId) public view override(ERC1155) returns (bool) {
return super.supportsInterface(interfaceId);
}
}