-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpaceRats.sol
195 lines (159 loc) · 6.55 KB
/
SpaceRats.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "./ERC721A/ERC721A.sol";
import "openzeppelin-contracts/access/Ownable.sol";
import "openzeppelin-contracts/security/ReentrancyGuard.sol";
import "openzeppelin-contracts/utils/Strings.sol";
contract SpaceRats is Ownable, ERC721A, ReentrancyGuard {
/// -----------------------------------------------------------------------
/// Structs
/// -----------------------------------------------------------------------
struct SaleConfig {
uint32 publicSaleStartTime;
uint64 whitelistPrice;
uint64 publicPrice;
uint32 publicSaleKey;
}
/// -----------------------------------------------------------------------
/// Errors
/// -----------------------------------------------------------------------
error SpaceRats__LargerCollectionSizeNeeded();
error SpaceRats__PublicMintHasNotBegun();
error SpaceRats__WhitelistMintHasNotBegun();
error SpaceRats__IncorrectPublicSaleKey();
error SpaceRats__CannotMintThisMany();
error SpaceRats__ReachedMaxSupply();
error SpaceRats__SendMoreEth();
error SpaceRats__NotEligibleForWhitelistMint();
error SpaceRats__ArrayLengthsDoNotMatch();
error SpaceRats__TransferFailed();
/// -----------------------------------------------------------------------
/// Immutables
/// -----------------------------------------------------------------------
uint256 public immutable maxPerAddressDuringMint;
uint256 public immutable amountForWhitelist;
uint256 public immutable amountForPublicAndWhitelist;
/// -----------------------------------------------------------------------
/// Storage variables
/// -----------------------------------------------------------------------
mapping(address => uint256) public allowlist;
SaleConfig public saleConfig;
/// -----------------------------------------------------------------------
/// Constructor
/// -----------------------------------------------------------------------
constructor(
uint256 maxBatchSize_,
uint256 collectionSize_,
uint256 amountForPublicAndWhitelist_,
uint256 amountForWhitelist_
) ERC721A("Space Rats", "SPACERATS", maxBatchSize_, collectionSize_) {
maxPerAddressDuringMint = maxBatchSize_;
amountForPublicAndWhitelist = amountForPublicAndWhitelist_;
amountForWhitelist = amountForWhitelist_;
if (amountForPublicAndWhitelist_ > collectionSize_)
revert SpaceRats__LargerCollectionSizeNeeded();
}
modifier callerIsUser() {
require(tx.origin == msg.sender);
_;
}
/// -----------------------------------------------------------------------
/// User actions: mint
/// -----------------------------------------------------------------------
function publicSaleMint(uint256 quantity, uint256 callerPublicSaleKey)
external
payable
callerIsUser
{
SaleConfig memory config = saleConfig;
uint256 publicSaleKey = uint256(config.publicSaleKey);
uint256 publicPrice = uint256(config.publicPrice);
uint256 publicSaleStartTime = uint256(config.publicSaleStartTime);
if (publicSaleKey != callerPublicSaleKey)
revert SpaceRats__IncorrectPublicSaleKey();
if (!isPublicSaleOn(publicPrice, publicSaleKey, publicSaleStartTime))
revert SpaceRats__PublicMintHasNotBegun();
if (totalSupply() + quantity > collectionSize)
revert SpaceRats__ReachedMaxSupply();
if (numberMinted(msg.sender) + quantity > maxPerAddressDuringMint)
revert SpaceRats__CannotMintThisMany();
_safeMint(msg.sender, quantity);
refundIfOver(publicPrice * quantity);
}
function whitelistMint() external payable callerIsUser {
uint256 price = uint256(saleConfig.whitelistPrice);
if (price == 0) revert SpaceRats__WhitelistMintHasNotBegun();
if (allowlist[msg.sender] == 0)
revert SpaceRats__NotEligibleForWhitelistMint();
if (totalSupply() + 1 > collectionSize)
revert SpaceRats__ReachedMaxSupply();
allowlist[msg.sender]--;
_safeMint(msg.sender, 1);
refundIfOver(price);
}
/// -----------------------------------------------------------------------
/// Owner actions
/// -----------------------------------------------------------------------
function setupSaleInfo(
uint32 publicSaleStartTime,
uint64 whitelistPrice,
uint64 publicPrice,
uint32 publicSaleKey
) external onlyOwner {
saleConfig = SaleConfig({
publicSaleStartTime: publicSaleStartTime,
whitelistPrice: whitelistPrice,
publicPrice: publicPrice,
publicSaleKey: publicSaleKey
});
}
function setPublicSaleKey(uint32 key) external onlyOwner {
saleConfig.publicSaleKey = key;
}
function seedWhitelist(
address[] memory addresses,
uint256[] memory numSlots
) external onlyOwner {
if (addresses.length != numSlots.length)
revert SpaceRats__ArrayLengthsDoNotMatch();
for (uint256 i; i < addresses.length; ++i) {
allowlist[addresses[i]] = numSlots[i];
}
}
function addToWhitelist(address addr, uint256 numSlots) external onlyOwner {
allowlist[addr] = numSlots;
}
function refundIfOver(uint256 price) private {
if (msg.value < price) revert SpaceRats__SendMoreEth();
if (msg.value > price) {
payable(msg.sender).transfer(msg.value - price);
}
}
function withdrawFunds() external onlyOwner nonReentrant {
(bool success, ) = msg.sender.call{value: address(this).balance}("");
if (!success) revert SpaceRats__TransferFailed();
}
/// -----------------------------------------------------------------------
/// View functions
/// -----------------------------------------------------------------------
function isPublicSaleOn(
uint256 publicPriceWei,
uint256 publicSaleKey,
uint256 publicSaleStartTime
) public view returns (bool) {
return
publicPriceWei != 0 &&
publicSaleKey != 0 &&
block.timestamp >= publicSaleStartTime;
}
function numberMinted(address owner) public view returns (uint256) {
return _numberMinted(owner);
}
function getOwnershipData(uint256 tokenId)
external
view
returns (TokenOwnership memory)
{
return ownershipOf(tokenId);
}
}