-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSalemay23.sol
406 lines (352 loc) · 15.4 KB
/
Salemay23.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
contract Plots_MultiToken_Presale {
// Token Addresses
address public VLND = address(0);
address public USDT = 0xdAC17F958D2ee523a2206206994597C13D831ec7;
address public USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
// Chainlink Price Feeds
address public USDTPriceFeed = 0xEe9F2375b4bdF6387aa8265dD4FB8F16512A1d46;
// Admin Address
address public Admin;
// Merkle Root
bytes32 public MerkleRoot = 0x0;
// Params
uint256 public SaleStart;
uint256 public SaleEnd;
uint256 public PhaseOnePrice;
uint256 public PhaseTwoPrice;
uint256 public TotalRaised;
uint256 public PhaseOneCap;
enum SalePhase { AwaitingStart, PhaseOne, PhaseTwo, Over }
modifier OnlyAdmin() {
require(msg.sender == Admin, "Only Admin");
_;
}
event TokensPurchased(address indexed buyer, uint256 amount, address token);
event ProceedsSentToTreasury(uint256 usdtAmount, uint256 usdcAmount, uint256 ethAmount);
event SaleParamsSet(uint256 saleStart, uint256 saleEnd, uint256 phaseOnePrice, uint256 phaseTwoPrice, uint256 phaseOneCap);
constructor(
uint256 saleStart,
uint256 saleEnd,
uint256 phaseOnePrice,
uint256 phaseTwoPrice,
uint256 phaseOneCap,
address admin
) {
SaleStart = saleStart;
SaleEnd = saleEnd;
PhaseOnePrice = phaseOnePrice;
PhaseTwoPrice = phaseTwoPrice;
PhaseOneCap = phaseOneCap;
Admin = admin;
emit SaleParamsSet(saleStart, saleEnd, phaseOnePrice, phaseTwoPrice, phaseOneCap);
}
// Admin Functions
function SendProceedsToTreasury() public OnlyAdmin {
uint256 usdtBalance = ERC20(USDT).balanceOf(address(this));
uint256 usdcBalance = ERC20(USDC).balanceOf(address(this));
uint256 ethBalance = address(this).balance;
ERC20(USDT).transfer(Admin, usdtBalance);
ERC20(USDC).transfer(Admin, usdcBalance);
payable(Admin).transfer(ethBalance);
emit ProceedsSentToTreasury(usdtBalance, usdcBalance, ethBalance);
}
// Getter Functions
function GetSaleStatus() public view returns (SalePhase) {
if (block.timestamp < SaleStart) {
return SalePhase.AwaitingStart;
} else if (block.timestamp > SaleEnd) {
return SalePhase.Over;
} else if (TotalRaised < PhaseOneCap) {
return SalePhase.PhaseOne;
} else if (TotalRaised >= PhaseOneCap) {
return SalePhase.PhaseTwo;
}
return SalePhase.Over;
}
function GetVLNDPrice() public view returns (uint256) {
if (GetSaleStatus() == SalePhase.PhaseOne) {
return PhaseOnePrice;
} else if (GetSaleStatus() == SalePhase.PhaseTwo) {
return PhaseTwoPrice;
}
return 0;
}
function ConvertEthToPlots(uint256 amountIn) public view returns (uint256) {
// AggregatorV3Interface priceFeed = AggregatorV3Interface(USDTPriceFeed);
// (, int256 priceusdt, , , ) = priceFeed.latestRoundData();
// uint256 USDTEquivalent = (amountIn * uint256(priceusdt)) / 1e8;
//set it to 3400$ temporarily for testing
uint256 USDTEquivalent = amountIn * 3400;
return ConvertStableToPlots(USDTEquivalent);
}
function ConvertStableToPlots(uint256 amountIn) public view returns (uint256) {
return amountIn / GetVLNDPrice();
}
// Purchase Functions
function PurchaseWithEth() public payable {
require(GetSaleStatus() != SalePhase.Over, "Sale is over");
uint256 plotsToReceive = ConvertEthToPlots(msg.value);
require(plotsToReceive > 0, "Invalid amount");
TotalRaised += msg.value;
ERC20(VLND).transfer(msg.sender, plotsToReceive);
emit TokensPurchased(msg.sender, plotsToReceive, address(0));
}
function PurchaseWithUSDT(uint256 amount) public {
require(GetSaleStatus() != SalePhase.Over, "Sale is over");
uint256 plotsToReceive = ConvertStableToPlots(amount);
require(plotsToReceive > 0, "Invalid amount");
ERC20(USDT).transferFrom(msg.sender, address(this), amount);
TotalRaised += amount;
ERC20(VLND).transfer(msg.sender, plotsToReceive);
emit TokensPurchased(msg.sender, plotsToReceive, USDT);
}
function PurchaseWithUSDC(uint256 amount) public {
require(GetSaleStatus() != SalePhase.Over, "Sale is over");
uint256 plotsToReceive = ConvertStableToPlots(amount);
require(plotsToReceive > 0, "Invalid amount");
ERC20(USDC).transferFrom(msg.sender, address(this), amount);
TotalRaised += amount;
ERC20(VLND).transfer(msg.sender, plotsToReceive);
emit TokensPurchased(msg.sender, plotsToReceive, USDC);
}
// Utility Functions
function VerifyWhitelist(bytes32[] memory proof, bytes32 leaf) public view returns (bool) {
return verify(proof, MerkleRoot, leaf);
}
function verify(
bytes32[] memory proof,
bytes32 root,
bytes32 leaf
) internal pure returns (bool) {
bytes32 computedHash = leaf;
for (uint256 i = 0; i < proof.length; i++) {
bytes32 proofElement = proof[i];
if (computedHash <= proofElement) {
computedHash = keccak256(abi.encodePacked(computedHash, proofElement));
} else {
computedHash = keccak256(abi.encodePacked(proofElement, computedHash));
}
}
return computedHash == root;
}
}
interface ERC20 {
function balanceOf(address owner) external view returns (uint256);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint value) external returns (bool);
function Mint(address _MintTo, uint256 _MintAmount) external;
function transfer(address to, uint value) external returns (bool);
function transferFrom(address from, address to, uint256 value) external returns (bool);
function totalSupply() external view returns (uint);
function CheckMinter(address AddytoCheck) external view returns(uint);
}
interface AggregatorV3Interface {
function latestRoundData()
external
view
returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound);
}
//Merkle Verification Libraries
library Hashes {
/**
* @dev Commutative Keccak256 hash of a sorted pair of bytes32. Frequently used when working with merkle proofs.
*
* NOTE: Equivalent to the `standardNodeHash` in our https://github.com/OpenZeppelin/merkle-tree[JavaScript library].
*/
function commutativeKeccak256(bytes32 a, bytes32 b) internal pure returns (bytes32) {
return a < b ? _efficientKeccak256(a, b) : _efficientKeccak256(b, a);
}
/**
* @dev Implementation of keccak256(abi.encode(a, b)) that doesn't allocate or expand memory.
*/
function _efficientKeccak256(bytes32 a, bytes32 b) private pure returns (bytes32 value) {
/// @solidity memory-safe-assembly
assembly {
mstore(0x00, a)
mstore(0x20, b)
value := keccak256(0x00, 0x40)
}
}
}
library MerkleProof {
/**
*@dev The multiproof provided is not valid.
*/
error MerkleProofInvalidMultiproof();
/**
* @dev Returns true if a `leaf` can be proved to be a part of a Merkle tree
* defined by `root`. For this, a `proof` must be provided, containing
* sibling hashes on the branch from the leaf to the root of the tree. Each
* pair of leaves and each pair of pre-images are assumed to be sorted.
*/
function verify(bytes32[] memory proof, bytes32 root, bytes32 leaf) internal pure returns (bool) {
return processProof(proof, leaf) == root;
}
/**
* @dev Calldata version of {verify}
*/
function verifyCalldata(bytes32[] calldata proof, bytes32 root, bytes32 leaf) internal pure returns (bool) {
return processProofCalldata(proof, leaf) == root;
}
/**
* @dev Returns the rebuilt hash obtained by traversing a Merkle tree up
* from `leaf` using `proof`. A `proof` is valid if and only if the rebuilt
* hash matches the root of the tree. When processing the proof, the pairs
* of leafs & pre-images are assumed to be sorted.
*/
function processProof(bytes32[] memory proof, bytes32 leaf) internal pure returns (bytes32) {
bytes32 computedHash = leaf;
for (uint256 i = 0; i < proof.length; i++) {
computedHash = Hashes.commutativeKeccak256(computedHash, proof[i]);
}
return computedHash;
}
/**
* @dev Calldata version of {processProof}
*/
function processProofCalldata(bytes32[] calldata proof, bytes32 leaf) internal pure returns (bytes32) {
bytes32 computedHash = leaf;
for (uint256 i = 0; i < proof.length; i++) {
computedHash = Hashes.commutativeKeccak256(computedHash, proof[i]);
}
return computedHash;
}
/**
* @dev Returns true if the `leaves` can be simultaneously proven to be a part of a Merkle tree defined by
* `root`, according to `proof` and `proofFlags` as described in {processMultiProof}.
*
* CAUTION: Not all Merkle trees admit multiproofs. See {processMultiProof} for details.
*/
function multiProofVerify(
bytes32[] memory proof,
bool[] memory proofFlags,
bytes32 root,
bytes32[] memory leaves
) internal pure returns (bool) {
return processMultiProof(proof, proofFlags, leaves) == root;
}
/**
* @dev Calldata version of {multiProofVerify}
*
* CAUTION: Not all Merkle trees admit multiproofs. See {processMultiProof} for details.
*/
function multiProofVerifyCalldata(
bytes32[] calldata proof,
bool[] calldata proofFlags,
bytes32 root,
bytes32[] memory leaves
) internal pure returns (bool) {
return processMultiProofCalldata(proof, proofFlags, leaves) == root;
}
/**
* @dev Returns the root of a tree reconstructed from `leaves` and sibling nodes in `proof`. The reconstruction
* proceeds by incrementally reconstructing all inner nodes by combining a leaf/inner node with either another
* leaf/inner node or a proof sibling node, depending on whether each `proofFlags` item is true or false
* respectively.
*
* CAUTION: Not all Merkle trees admit multiproofs. To use multiproofs, it is sufficient to ensure that: 1) the tree
* is complete (but not necessarily perfect), 2) the leaves to be proven are in the opposite order they are in the
* tree (i.e., as seen from right to left starting at the deepest layer and continuing at the next layer).
*/
function processMultiProof(
bytes32[] memory proof,
bool[] memory proofFlags,
bytes32[] memory leaves
) internal pure returns (bytes32 merkleRoot) {
// This function rebuilds the root hash by traversing the tree up from the leaves. The root is rebuilt by
// consuming and producing values on a queue. The queue starts with the `leaves` array, then goes onto the
// `hashes` array. At the end of the process, the last hash in the `hashes` array should contain the root of
// the Merkle tree.
uint256 leavesLen = leaves.length;
uint256 proofLen = proof.length;
uint256 totalHashes = proofFlags.length;
// Check proof validity.
if (leavesLen + proofLen != totalHashes + 1) {
revert MerkleProofInvalidMultiproof();
}
// The xxxPos values are "pointers" to the next value to consume in each array. All accesses are done using
// `xxx[xxxPos++]`, which return the current value and increment the pointer, thus mimicking a queue's "pop".
bytes32[] memory hashes = new bytes32[](totalHashes);
uint256 leafPos = 0;
uint256 hashPos = 0;
uint256 proofPos = 0;
// At each step, we compute the next hash using two values:
// - a value from the "main queue". If not all leaves have been consumed, we get the next leaf, otherwise we
// get the next hash.
// - depending on the flag, either another value from the "main queue" (merging branches) or an element from the
// `proof` array.
for (uint256 i = 0; i < totalHashes; i++) {
bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++];
bytes32 b = proofFlags[i]
? (leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++])
: proof[proofPos++];
hashes[i] = Hashes.commutativeKeccak256(a, b);
}
if (totalHashes > 0) {
if (proofPos != proofLen) {
revert MerkleProofInvalidMultiproof();
}
unchecked {
return hashes[totalHashes - 1];
}
} else if (leavesLen > 0) {
return leaves[0];
} else {
return proof[0];
}
}
/**
* @dev Calldata version of {processMultiProof}.
*
* CAUTION: Not all Merkle trees admit multiproofs. See {processMultiProof} for details.
*/
function processMultiProofCalldata(
bytes32[] calldata proof,
bool[] calldata proofFlags,
bytes32[] memory leaves
) internal pure returns (bytes32 merkleRoot) {
// This function rebuilds the root hash by traversing the tree up from the leaves. The root is rebuilt by
// consuming and producing values on a queue. The queue starts with the `leaves` array, then goes onto the
// `hashes` array. At the end of the process, the last hash in the `hashes` array should contain the root of
// the Merkle tree.
uint256 leavesLen = leaves.length;
uint256 proofLen = proof.length;
uint256 totalHashes = proofFlags.length;
// Check proof validity.
if (leavesLen + proofLen != totalHashes + 1) {
revert MerkleProofInvalidMultiproof();
}
// The xxxPos values are "pointers" to the next value to consume in each array. All accesses are done using
// `xxx[xxxPos++]`, which return the current value and increment the pointer, thus mimicking a queue's "pop".
bytes32[] memory hashes = new bytes32[](totalHashes);
uint256 leafPos = 0;
uint256 hashPos = 0;
uint256 proofPos = 0;
// At each step, we compute the next hash using two values:
// - a value from the "main queue". If not all leaves have been consumed, we get the next leaf, otherwise we
// get the next hash.
// - depending on the flag, either another value from the "main queue" (merging branches) or an element from the
// `proof` array.
for (uint256 i = 0; i < totalHashes; i++) {
bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++];
bytes32 b = proofFlags[i]
? (leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++])
: proof[proofPos++];
hashes[i] = Hashes.commutativeKeccak256(a, b);
}
if (totalHashes > 0) {
if (proofPos != proofLen) {
revert MerkleProofInvalidMultiproof();
}
unchecked {
return hashes[totalHashes - 1];
}
} else if (leavesLen > 0) {
return leaves[0];
} else {
return proof[0];
}
}
}