-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvancednft.sol
188 lines (160 loc) · 5.67 KB
/
advancednft.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
// SPDX-License-Identifier: MIT
// commit-reveal, multicall can only be used public sale.
// mintwithbitmap and mintwithmapping can only be used during pre-sale
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/cryptography/MerkleProof.sol";
import "@openzeppelin/contracts/utils/structs/BitMaps.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
contract AdvancedNFT is ERC721, Ownable(msg.sender), ReentrancyGuard {
using BitMaps for BitMaps.BitMap;
// Merkle tree root
bytes32 public merkleRoot;
// Mapping and Bitmap to track minted addresses
mapping(address => bool) public hasMintedMapping;
BitMaps.BitMap private hasMintedBitmap;
// Commit-Reveal Structure
struct Commit {
bytes32 commitHash;
uint256 blockNumber;
}
mapping(address => Commit) public commits;
uint256 public revealDelay = 10;
// State machine
enum SaleState {
Paused,
Presale,
PublicSale,
SoldOut
}
SaleState public saleState;
// Multicall functionality
uint256 public totalMinted;
uint256 public maxSupply;
// Balances for contributors (for pull pattern)
mapping(address => uint256) public balances;
// Whitelist of allowed function selectors
mapping(bytes4 => bool) public allowedFunctions;
constructor() ERC721("Advanced NFT", "ANFT") {
merkleRoot = 0x063d63976891a949e32286ac8746c5af374caeea523fa4959c45f063bcc9db06;
maxSupply = 20;
saleState = SaleState.Paused;
allowedFunctions[ERC721.transferFrom.selector] = true;
}
// === State Machine for Minting Phases ===
modifier validState(SaleState requiredState) {
require(saleState == requiredState, "Invalid state for this action");
_;
}
function setSaleState(SaleState _newState) external onlyOwner {
saleState = _newState;
}
function mintWithMapping(bytes32[] calldata _merkleProof, uint256 index)
public
nonReentrant
validState(SaleState.Presale)
{
bytes32 leaf = keccak256(
bytes.concat(keccak256(abi.encode(msg.sender, index)))
);
require(
MerkleProof.verify(_merkleProof, merkleRoot, leaf),
"Invalid proof"
);
require(totalMinted < maxSupply, "Max supply reached");
require(!hasMintedMapping[msg.sender], "Already minted");
hasMintedMapping[msg.sender] = true;
_mintInternal();
}
function mintWithBitmap(bytes32[] calldata _merkleProof, uint256 index)
public
nonReentrant
validState(SaleState.Presale)
{
bytes32 leaf = keccak256(
bytes.concat(keccak256(abi.encode(msg.sender, index)))
);
require(
MerkleProof.verify(_merkleProof, merkleRoot, leaf),
"Invalid proof"
);
require(totalMinted < maxSupply, "Max supply reached");
require(!hasMintedBitmap.get(index), "Already minted");
hasMintedBitmap.set(index);
_mintInternal();
}
function _mintInternal() private {
_safeMint(msg.sender, totalMinted);
totalMinted++;
if (totalMinted == maxSupply) {
saleState = SaleState.SoldOut;
}
}
// === Commit-Reveal for Random NFT ID Allocation ===
// web3.utils.keccak256(web3.eth.abi.encodeParameters(['uint256', 'uint256'], [replace_with_nftId, replace_with_secret])) - use this in console to generate hash
function commit(bytes32 _commitHash)
external
validState(SaleState.PublicSale)
{
commits[msg.sender] = Commit(_commitHash, block.number);
}
function reveal(uint256 _nftId, uint256 _secret)
external
nonReentrant
validState(SaleState.PublicSale)
{
Commit memory userCommit = commits[msg.sender];
require(
block.number > userCommit.blockNumber + revealDelay,
"Reveal too soon"
);
require(
keccak256(abi.encodePacked(_nftId, _secret)) ==
userCommit.commitHash,
"Invalid reveal"
);
_safeMint(msg.sender, _nftId);
totalMinted++;
require(totalMinted <= maxSupply, "Exceeds supply");
}
// === Multicall for Transferring NFTs ===
function generateTransferFromData(
address from,
address to,
uint256 tokenId
) public pure returns (bytes memory) {
return
abi.encodeWithSelector(
ERC721.transferFrom.selector,
from,
to,
tokenId
);
}
function multicall(bytes[] calldata data)
external
nonReentrant
validState(SaleState.PublicSale)
{
for (uint256 i = 0; i < data.length; i++) {
require(data[i].length >= 4, "Invalid call data");
bytes4 selector = bytes4(data[i][:4]);
require(allowedFunctions[selector], "Function not allowed");
(bool success, ) = address(this).call(data[i]);
require(success, "Transaction failed");
}
}
// === Pull Pattern for Fund Withdrawals ===
function withdraw() external nonReentrant {
uint256 amount = balances[msg.sender];
require(amount > 0, "No funds to withdraw");
balances[msg.sender] = 0;
payable(msg.sender).transfer(amount);
}
// Receive function to accept funds (for pull payments)
function recievefunds() external payable {
balances[msg.sender] += msg.value;
}
}