/
StarterPackV2.sol
386 lines (354 loc) · 16.6 KB
/
StarterPackV2.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
//SPDX-License-Identifier: MIT
pragma solidity 0.8.2;
import {PurchaseValidator, Context, AccessControl} from "./PurchaseValidator.sol";
import {GemsCatalystsRegistry, ICatalyst, IGem, IERC20} from "../catalyst/GemsCatalystsRegistry.sol";
import {ERC2771HandlerV2} from "../common/BaseWithStorage/ERC2771/ERC2771HandlerV2.sol";
/// @title StarterPack contract for the purchase of StarterPacks (bundles of Catalysts and Gems) with EIP712
/// @notice This contract enables purchases with SAND when the backend authorizes it via message signing
/// @notice The following privileged roles are used in StarterPackV2: DEFAULT_ADMIN_ROLE, STARTERPACK_ROLE
/// @dev DEFAULT_ADMIN_ROLE is intended for contract setup / emergency, STARTERPACK_ROLE is provided for business purposes
contract StarterPackV2 is AccessControl, PurchaseValidator, ERC2771HandlerV2 {
uint256 internal constant MAX_UINT16 = type(uint16).max;
uint256 private constant DECIMAL_PLACES = 1 ether;
uint256 private constant MAX_WITHDRAWAL = 100;
// The delay between calling setPrices() and when the new prices come into effect
// Minimizes the effect of price changes on pending TXs
uint256 private constant PRICE_CHANGE_DELAY = 1 hours;
// The timestamp of the last price change
uint256 private _priceChangeTimestamp;
// The following role is provided for business-related admin functions
bytes32 public constant STARTERPACK_ROLE = keccak256("STARTERPACK_ROLE");
address internal immutable _sand;
address internal immutable _registry;
address payable internal _wallet;
bool public _sandEnabled;
// Mapping catalyst and gem ids to their prices
mapping(uint16 => uint256) private _catalystPrices;
mapping(uint16 => uint256) private _catalystPreviousPrices;
mapping(uint16 => uint256) private _gemPrices;
mapping(uint16 => uint256) private _gemPreviousPrices;
event ReceivingWallet(address indexed newReceivingWallet);
event Purchase(address indexed buyer, Message message, uint256 amountPaid, address indexed token);
event WithdrawAll(address indexed to, uint16[] catalystIds, uint16[] gemIds);
event SandEnabled(bool enabled);
event SetPrices(
uint16[] catalystIds,
uint256[] catalystPrices,
uint16[] gemIds,
uint256[] gemPrices,
uint256 priceChangeTimestamp
);
struct Message {
address buyer;
uint16[] catalystIds;
uint256[] catalystQuantities;
uint16[] gemIds;
uint256[] gemQuantities;
uint256 nonce;
}
constructor(
address defaultAdmin,
address starterPackAdmin,
address sandContractAddress,
address trustedForwarder,
address payable initialWalletAddress,
address initialSigningWallet,
address registry
) PurchaseValidator(initialSigningWallet, "Sandbox StarterPack", "1.0") ERC2771HandlerV2(trustedForwarder) {
require(defaultAdmin != address(0), "ADMIN_ZERO_ADDRESS");
require(starterPackAdmin != address(0), "STARTERPACK_ADMIN_ZERO_ADDRESS");
require(sandContractAddress != address(0), "SAND_ZERO_ADDRESS");
require(trustedForwarder != address(0), "FORWARDER_ZERO_ADDRESS");
require(initialWalletAddress != address(0), "WALLET_ZERO_ADDRESS");
require(registry != address(0), "REGISTRY_ZERO_ADDRESS");
_grantRole(DEFAULT_ADMIN_ROLE, defaultAdmin);
_grantRole(STARTERPACK_ROLE, starterPackAdmin);
_sand = sandContractAddress;
_wallet = initialWalletAddress;
_registry = registry;
}
/// @notice Set the wallet receiving the proceeds
/// @param newReceivingWallet Address of the new receiving wallet
function setReceivingWallet(address payable newReceivingWallet) external onlyRole(DEFAULT_ADMIN_ROLE) {
require(newReceivingWallet != address(0), "WALLET_ZERO_ADDRESS");
require(newReceivingWallet != _wallet, "WALLET_ALREADY_SET");
_wallet = newReceivingWallet;
emit ReceivingWallet(newReceivingWallet);
}
/// @dev Enable / disable the specific SAND payment for StarterPacks
/// @param enabled Whether to enable or disable
function setSANDEnabled(bool enabled) external onlyRole(STARTERPACK_ROLE) {
_sandEnabled = enabled;
emit SandEnabled(enabled);
}
/// @notice Enables admin to change the prices (in SAND) of the catalysts and gems in the StarterPack bundle
/// @param catalystIds Array of catalyst IDs for which new prices will take effect after a delay period
/// @param catalystPrices Array of new catalyst prices that will take effect after a delay period
/// @param gemIds Array of gem IDs for which new prices will take effect after a delay period
/// @param gemPrices Array of new gems prices that will take effect after a delay period
function setPrices(
uint16[] calldata catalystIds,
uint256[] calldata catalystPrices,
uint16[] calldata gemIds,
uint256[] calldata gemPrices
) external onlyRole(STARTERPACK_ROLE) {
require(block.timestamp > _priceChangeTimestamp + PRICE_CHANGE_DELAY, "DELAY_PERIOD_IN_EFFECT");
require(catalystIds.length == catalystPrices.length, "INVALID_CAT_INPUT");
require(gemIds.length == gemPrices.length, "INVALID_GEM_INPUT");
require(catalystPrices.length <= MAX_UINT16, "TOO_MANY_CATALYST_PRICES");
require(gemPrices.length <= MAX_UINT16, "TOO_MANY_GEM_PRICES");
for (uint256 i = 0; i < catalystIds.length; i++) {
uint16 id = catalystIds[i];
require(_isValidCatalyst(id), "INVALID_CAT_ID");
_catalystPreviousPrices[id] = _catalystPrices[id];
_catalystPrices[id] = catalystPrices[i];
}
for (uint256 i = 0; i < gemIds.length; i++) {
uint16 id = gemIds[i];
require(_isValidGem(id), "INVALID_GEM_ID");
_gemPreviousPrices[id] = _gemPrices[id];
_gemPrices[id] = gemPrices[i];
}
_priceChangeTimestamp = block.timestamp;
emit SetPrices(catalystIds, catalystPrices, gemIds, gemPrices, _priceChangeTimestamp);
}
/// @notice Enables admin to withdraw any remaining tokens
/// @param to The destination address for the purchased Catalysts and Gems
/// @param catalystIds The IDs of the catalysts to be transferred
/// @param gemIds The IDs of the gems to be transferred
/// @dev The sum length of catalystIds + gemIds must be <= MAX_WITHDRAWAL
function withdrawAll(
address to,
uint16[] calldata catalystIds,
uint16[] calldata gemIds
) external onlyRole(DEFAULT_ADMIN_ROLE) {
require(catalystIds.length + gemIds.length <= MAX_WITHDRAWAL, "TOO_MANY_IDS");
require(to != address(0), "ZERO_ADDRESS");
for (uint256 i = 0; i < catalystIds.length; i++) {
uint16 id = catalystIds[i];
require(_isValidCatalyst(id), "INVALID_CATALYST_ID");
ICatalyst catalyst = _getCatalyst(id);
uint256 balance = catalyst.balanceOf(address(this));
_executeRegistryTransferCatalyst(catalyst, address(this), to, balance);
}
for (uint256 i = 0; i < gemIds.length; i++) {
uint16 id = gemIds[i];
require(_isValidGem(id), "INVALID_GEM_ID");
IGem gem = _getGem(id);
uint256 balance = gem.balanceOf(address(this));
_executeRegistryTransferGem(gem, address(this), to, balance);
}
emit WithdrawAll(to, catalystIds, gemIds);
}
/// @notice Purchase StarterPacks with SAND
/// @param message A message containing information about the Catalysts and Gems to be purchased together with the destination (buyer) and a nonce
/// @param signature A signed message specifying tx details
function purchaseWithSAND(Message calldata message, bytes calldata signature) external {
require(message.buyer == _msgSender(), "INVALID_SENDER");
require(_sandEnabled, "SAND_IS_NOT_ENABLED");
require(
_isPurchaseValid(
message.buyer,
message.catalystIds,
message.catalystQuantities,
message.gemIds,
message.gemQuantities,
message.nonce,
signature
),
"INVALID_PURCHASE"
);
uint256 amountInSAND =
_calculateTotalPriceInSAND(
message.catalystIds,
message.catalystQuantities,
message.gemIds,
message.gemQuantities
);
_transferSANDPayment(message.buyer, _wallet, amountInSAND);
_transferCatalysts(message.catalystIds, message.catalystQuantities, message.buyer);
_transferGems(message.gemIds, message.gemQuantities, message.buyer);
emit Purchase(message.buyer, message, amountInSAND, _sand);
}
/// @notice Get current StarterPack prices for catalysts and gems by id
/// @param catalystIds The IDs of the catalysts you want to obtain price information for
/// @param gemIds The IDs of the gems you want to obtain price information for
/// @return catalystPricesBeforeSwitch Catalyst prices before price change
/// @return catalystPricesAfterSwitch Catalyst prices after price change
/// @return gemPricesBeforeSwitch Gem prices before price change
/// @return gemPricesAfterSwitch Gem prices after price change
/// @return switchTime The time the latest price change will take effect, being the time of the price change plus the price change delay
function getPrices(uint16[] calldata catalystIds, uint16[] calldata gemIds)
external
view
returns (
uint256[] memory,
uint256[] memory,
uint256[] memory,
uint256[] memory,
uint256
)
{
uint256 switchTime = 0;
if (_priceChangeTimestamp != 0) {
switchTime = _priceChangeTimestamp + PRICE_CHANGE_DELAY;
}
uint256[] memory catalystPricesBeforeSwitch = new uint256[](catalystIds.length);
uint256[] memory catalystPricesAfterSwitch = new uint256[](catalystIds.length);
uint256[] memory gemPricesBeforeSwitch = new uint256[](gemIds.length);
uint256[] memory gemPricesAfterSwitch = new uint256[](gemIds.length);
for (uint256 i = 0; i < catalystIds.length; i++) {
uint16 id = catalystIds[i];
catalystPricesBeforeSwitch[i] = _catalystPreviousPrices[id];
catalystPricesAfterSwitch[i] = _catalystPrices[id];
}
for (uint256 i = 0; i < gemIds.length; i++) {
uint16 id = gemIds[i];
gemPricesBeforeSwitch[i] = _gemPreviousPrices[id];
gemPricesAfterSwitch[i] = _gemPrices[id];
}
return (
catalystPricesBeforeSwitch,
catalystPricesAfterSwitch,
gemPricesBeforeSwitch,
gemPricesAfterSwitch,
switchTime
);
}
/// @notice Return whether SAND payments are enabled
/// @return Whether SAND payments are enabled
function isSANDEnabled() external view returns (bool) {
return _sandEnabled;
}
/// @notice Get the beneficiary wallet.
/// @return the address of the receiving wallet
function getReceivingWallet() external view returns (address) {
return _wallet;
}
/// @notice Verify the total expected price to pay in SAND
/// @param catalystIds An array of catalyst IDs to be purchased
/// @param catalystQuantities An array of catalyst amounts to be purchased
/// @param gemIds An array of gem IDs to be purchased
/// @param gemQuantities An array of gem amounts to be purchased
/// @return the total price to pay in SAND for the cats and gems in the bundle
function calculateTotalPriceInSAND(
uint16[] memory catalystIds,
uint256[] memory catalystQuantities,
uint16[] memory gemIds,
uint256[] memory gemQuantities
) external returns (uint256) {
return _calculateTotalPriceInSAND(catalystIds, catalystQuantities, gemIds, gemQuantities);
}
function _transferCatalysts(
uint16[] memory catalystIds,
uint256[] memory catalystQuantities,
address buyer
) internal {
for (uint256 i = 0; i < catalystIds.length; i++) {
uint16 id = catalystIds[i];
require(_isValidCatalyst(id), "INVALID_CATALYST_ID");
_executeRegistryTransferCatalyst(_getCatalyst(id), address(this), buyer, catalystQuantities[i]);
}
}
function _transferGems(
uint16[] memory gemIds,
uint256[] memory gemQuantities,
address buyer
) internal {
for (uint256 i = 0; i < gemIds.length; i++) {
uint16 id = gemIds[i];
require(_isValidGem(id), "INVALID_GEM_ID");
_executeRegistryTransferGem(_getGem(id), address(this), buyer, gemQuantities[i]);
}
}
function _executeRegistryTransferCatalyst(
ICatalyst catalyst,
address from,
address to,
uint256 quantity
) private {
require(catalyst.transferFrom(from, to, quantity), "CATALYST_TRANSFER_FAILED");
}
function _executeRegistryTransferGem(
IGem gem,
address from,
address to,
uint256 quantity
) private {
require(gem.transferFrom(from, to, quantity), "GEM_TRANSFER_FAILED");
}
function _getCatalyst(uint16 catalystId) internal view returns (ICatalyst) {
return GemsCatalystsRegistry(_registry).getCatalyst(catalystId);
}
function _isValidCatalyst(uint16 catalystId) internal view returns (bool) {
return GemsCatalystsRegistry(_registry).doesCatalystExist(catalystId) && catalystId > 0;
}
function _getGem(uint16 gemId) internal view returns (IGem) {
return GemsCatalystsRegistry(_registry).getGem(gemId);
}
function _isValidGem(uint16 gemId) internal view returns (bool) {
return GemsCatalystsRegistry(_registry).doesGemExist(gemId) && gemId > 0;
}
/// @dev Function to calculate the total price in SAND of the StarterPacks to be purchased
function _calculateTotalPriceInSAND(
uint16[] memory catalystIds,
uint256[] memory catalystQuantities,
uint16[] memory gemIds,
uint256[] memory gemQuantities
) internal returns (uint256) {
require(catalystIds.length == catalystQuantities.length, "INVALID_CAT_INPUT");
require(gemIds.length == gemQuantities.length, "INVALID_GEM_INPUT");
bool useCurrentPrices = _priceSelector();
uint256 totalPrice;
for (uint256 i = 0; i < catalystIds.length; i++) {
uint16 id = catalystIds[i];
uint256 quantity = catalystQuantities[i];
totalPrice =
totalPrice +
(useCurrentPrices ? _catalystPrices[id] * (quantity) : _catalystPreviousPrices[id] * (quantity));
}
for (uint256 i = 0; i < gemIds.length; i++) {
uint16 id = gemIds[i];
uint256 quantity = gemQuantities[i];
totalPrice =
totalPrice +
(useCurrentPrices ? _gemPrices[id] * (quantity) : _gemPreviousPrices[id] * (quantity));
}
return totalPrice;
}
/// @dev Function to determine whether to use previous or current prices
function _priceSelector() internal returns (bool) {
bool useCurrentPrices;
// No price change
if (_priceChangeTimestamp == 0) {
useCurrentPrices = true;
} else {
// Price change delay has expired: use current prices
if (block.timestamp > _priceChangeTimestamp + PRICE_CHANGE_DELAY) {
_priceChangeTimestamp = 0;
useCurrentPrices = true;
} else {
// Price change has recently occured: use previous prices until price change takes effect
useCurrentPrices = false;
}
}
return (useCurrentPrices);
}
/// @dev Function to handle purchase with SAND
function _transferSANDPayment(
address buyer,
address payable paymentRecipient,
uint256 amount
) internal {
uint256 amountForDestination = amount;
require(IERC20(_sand).transferFrom(buyer, paymentRecipient, amountForDestination), "PAYMENT_TRANSFER_FAILED");
}
/// @dev this override is required; two or more base classes define function
function _msgSender() internal view override(Context, ERC2771HandlerV2) returns (address sender) {
return ERC2771HandlerV2._msgSender();
}
/// @dev this override is required; two or more base classes define function
function _msgData() internal view override(Context, ERC2771HandlerV2) returns (bytes calldata) {
return ERC2771HandlerV2._msgData();
}
}