-
Notifications
You must be signed in to change notification settings - Fork 1
/
RCFactory.sol
633 lines (565 loc) · 25.2 KB
/
RCFactory.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
// SPDX-License-Identifier: AGPL-3.0
pragma solidity 0.8.4;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/proxy/Clones.sol";
import "hardhat/console.sol";
import "./interfaces/IRCFactory.sol";
import "./interfaces/IRCTreasury.sol";
import "./interfaces/IRCMarket.sol";
import "./interfaces/IRCNftHubL2.sol";
import "./interfaces/IRCOrderbook.sol";
import "./lib/NativeMetaTransaction.sol";
import "./interfaces/IRealitio.sol";
/// @title Reality Cards Factory
/// @author Andrew Stanger & Daniel Chilvers
/// @notice If you have found a bug, please contact andrew@realitycards.io- no hack pls!!
contract RCFactory is Ownable, NativeMetaTransaction, IRCFactory {
/*╔═════════════════════════════════╗
║ VARIABLES ║
╚═════════════════════════════════╝*/
//≡≡≡≡≡≡≡ CONTRACT VARIABLES ≡≡≡≡≡≡≡//
IRCTreasury public override treasury;
IRCNftHubL2 public override nfthub;
IRCOrderbook public override orderbook;
IRealitio public realitio;
///// CONTRACT ADDRESSES /////
/// @dev reference contract
address public referenceContractAddress;
/// @dev increments each time a new reference contract is added
uint256 public referenceContractVersion;
/// @dev market addresses, mode // address
/// @dev these are not used for anything, just an easy way to get markets
mapping(uint256 => address[]) public marketAddresses;
mapping(address => bool) public mappingOfMarkets;
///// GOVERNANCE VARIABLES- OWNER /////
/// @dev artist / winner / market creator / affiliate / card affiliate
uint256[5] public potDistribution;
/// @dev minimum tokens that must be sent when creating market which forms iniital pot
uint256 public sponsorshipRequired;
/// @dev adjust required price increase (in %)
uint256 public override minimumPriceIncreasePercent;
/// @dev market opening time must be at least this many seconds in the future
uint32 public advancedWarning;
/// @dev market closing time must be no more than this many seconds in the future
uint32 public maximumDuration;
/// @dev list of governors
mapping(address => bool) public governors;
/// @dev if false, anyone can create markets
bool public marketCreationGovernorsOnly = true;
/// @dev if false, anyone can be an affiliate
bool public approvedAffilliatesOnly = true;
/// @dev if false, anyone can be an artist
bool public approvedArtistsOnly = true;
/// @dev if true, cards are burnt at the end of events for hidden markets to enforce scarcity
bool public override trapIfUnapproved = true;
/// @dev high level owner who can change the factory address
address public uberOwner;
/// @dev the maximum number of rent collections to perform in a single transaction
uint256 public override maxRentIterations;
/// @dev the address of the arbitrator
address public arbitrator;
/// @dev the time allowed to dispute the oracle answer
uint32 public timeout;
///// GOVERNANCE VARIABLES- GOVERNORS /////
/// @dev unapproved markets hidden from the interface
mapping(address => bool) public override isMarketApproved;
/// @dev allows artist to receive cut of total rent
mapping(address => bool) public isArtistApproved;
/// @dev allows affiliate to receive cut of total rent
mapping(address => bool) public isAffiliateApproved;
/// @dev allows card affiliate to receive cut of total rent
mapping(address => bool) public isCardAffiliateApproved;
/// @dev a limit to the number of NFTs to mint per market
uint256 public nftMintingLimit;
///// OTHER /////
/// @dev counts the total NFTs minted across all events
/// @dev ... so the appropriate token id is used when upgrading to mainnet
uint256 public totalNftMintCount;
/*╔═════════════════════════════════╗
║ EVENTS ║
╚═════════════════════════════════╝*/
event LogMarketCreated1(
address contractAddress,
address treasuryAddress,
address nftHubAddress,
uint256 referenceContractVersion
);
event LogMarketCreated2(
address contractAddress,
uint32 mode,
string[] tokenURIs,
string ipfsHash,
uint32[] timestamps,
uint256 totalNftMintCount
);
event LogMarketApproved(address market, bool hidden);
event LogAdvancedWarning(uint256 _newAdvancedWarning);
event LogMaximumDuration(uint256 _newMaximumDuration);
/*╔═════════════════════════════════╗
║ CONSTRUCTOR ║
╚═════════════════════════════════╝*/
/// @dev Treasury must be deployed before Factory
constructor(
IRCTreasury _treasuryAddress,
address _realitioAddress,
address _arbitratorAddress
) {
require(address(_treasuryAddress) != address(0));
// initialise MetaTransactions
_initializeEIP712("RealityCardsFactory", "1");
// at initiation, uberOwner and owner will be the same
uberOwner = msgSender();
// initialise contract variable
treasury = _treasuryAddress;
// initialise adjustable parameters
// artist // winner // creator // affiliate // card affiliates
setPotDistribution(20, 0, 0, 20, 100); // 2% artist, 2% affiliate, 10% card affiliate
setminimumPriceIncreasePercent(10); // 10%
setNFTMintingLimit(60); // current gas limit (12.5m) allows for 60 NFTs to be minted
setMaxRentIterations(35); // limit appears to be 41, set safe at 35 for now.
// oracle
setArbitrator(_arbitratorAddress);
setRealitioAddress(_realitioAddress);
setTimeout(86400); // 24 hours
}
/*╔═════════════════════════════════╗
║ VIEW FUNCTIONS ║
╚═════════════════════════════════╝*/
/// @notice Fetch the address of the most recently created market
/// @param _mode Filter by market mode, 0=Classic 1=Winner Takes All 2=SafeMode
/// @return the address of the most recent market in the given mode
function getMostRecentMarket(uint256 _mode)
external
view
returns (address)
{
return marketAddresses[_mode][marketAddresses[_mode].length - (1)];
}
/// @notice Fetch all the market addresses for a given mode
/// @param _mode Filter by market mode, 0=Classic 1=Winner Takes All 2=SafeMode
/// @return an array of all markets in a given mode
function getAllMarkets(uint256 _mode)
external
view
returns (address[] memory)
{
return marketAddresses[_mode];
}
/// @notice Returns the currently set pot distribution
/// @return the pot distribution array: artist, winner, creator, affiliate, card affiliates
function getPotDistribution()
external
view
override
returns (uint256[5] memory)
{
return potDistribution;
}
/*╔═════════════════════════════════╗
║ MODIFIERS ║
╚═════════════════════════════════╝*/
/// @dev include the owner as a governor
modifier onlyGovernors() {
require(
governors[msgSender()] || owner() == msgSender(),
"Not approved"
);
_;
}
/*╔═════════════════════════════════╗
║ GOVERNANCE - OWNER (SETUP) ║
╚═════════════════════════════════╝*/
/// @dev all functions should have onlyOwner modifier
/// @notice where the NFTs live
/// @dev nftMintCount will probably need to be reset to zero if new nft contract, but
/// @dev ... keeping flexible in case returning to previous contract
/// @param _newAddress the address to set
/// @param _newNftMintCount the number of NFTs this contract has minted, in order to keep them unique
function setNftHubAddress(IRCNftHubL2 _newAddress, uint256 _newNftMintCount)
external
onlyOwner
{
require(address(_newAddress) != address(0));
nfthub = _newAddress;
totalNftMintCount = _newNftMintCount;
}
/// @notice set the address of the orderbook contract
/// @param _newAddress the address to set
function setOrderbookAddress(IRCOrderbook _newAddress) external onlyOwner {
require(address(_newAddress) != address(0));
orderbook = _newAddress;
}
/*╔═════════════════════════════════╗
║ GOVERNANCE - OWNER ║
╚═════════════════════════════════╝*/
/// @dev all functions should have onlyOwner modifier
// Min price increase & pot distribution emitted by Market.
// Advanced Warning and Maximum Duration events emitted here. Nothing else need be emitted.
/*┌────────────────────────────────────┐
│ CALLED WITHIN CONSTRUTOR - PUBLIC │
└────────────────────────────────────┘*/
/// @notice update stakeholder payouts
/// @dev in basis points (so 1000 = 100%)
/// @param _artistCut The artist that designed the card
/// @param _winnerCut Extra cut for the longest owner
/// @param _creatorCut The creator of the market
/// @param _affiliateCut An affiliate for the market that doesn't fit into the other cuts
/// @param _cardAffiliateCut An affiliate cur for specific cards
function setPotDistribution(
uint256 _artistCut,
uint256 _winnerCut,
uint256 _creatorCut,
uint256 _affiliateCut,
uint256 _cardAffiliateCut
) public onlyOwner {
require(
_artistCut +
_winnerCut +
_creatorCut +
_affiliateCut +
_cardAffiliateCut <=
1000,
"Cuts too big"
);
potDistribution[0] = _artistCut;
potDistribution[1] = _winnerCut;
potDistribution[2] = _creatorCut;
potDistribution[3] = _affiliateCut;
potDistribution[4] = _cardAffiliateCut;
}
/// @notice how much above the current price a user must bid, in %
/// @param _percentIncrease the percentage to set, e.g. 10 = 10%
function setminimumPriceIncreasePercent(uint256 _percentIncrease)
public
override
onlyOwner
{
minimumPriceIncreasePercent = _percentIncrease;
}
/// @notice A limit to the number of NFTs to mint per market
/// @dev to avoid gas limits
/// @param _mintLimit the limit to set
function setNFTMintingLimit(uint256 _mintLimit) public override onlyOwner {
nftMintingLimit = _mintLimit;
}
/// @notice A limit to the number of rent collections per transaction
/// @dev to avoid gas limits
/// @param _rentLimit the limit to set
function setMaxRentIterations(uint256 _rentLimit)
public
override
onlyOwner
{
maxRentIterations = _rentLimit;
}
/// @notice set the address of the reality.eth contracts
/// @param _newAddress the address to set
function setRealitioAddress(address _newAddress) public onlyOwner {
require(_newAddress != address(0), "Must set an address");
realitio = IRealitio(_newAddress);
}
/// @notice address of the arbitrator, in case of continued disputes on reality.eth
/// @param _newAddress the address to set
function setArbitrator(address _newAddress) public onlyOwner {
require(_newAddress != address(0), "Must set an address");
arbitrator = _newAddress;
}
/// @notice set how long reality.eth waits for disputes before finalising
/// @param _newTimeout the timeout to set in seconds, 86400 = 24hrs
function setTimeout(uint32 _newTimeout) public onlyOwner {
timeout = _newTimeout;
}
/*┌──────────────────────────────────────────┐
│ NOT CALLED WITHIN CONSTRUTOR - EXTERNAL │
└──────────────────────────────────────────┘*/
/// @notice whether or not only governors can create the market
function changeMarketCreationGovernorsOnly() external onlyOwner {
marketCreationGovernorsOnly = !marketCreationGovernorsOnly;
}
/// @notice whether or not anyone can be an artist
function changeApprovedArtistsOnly() external onlyOwner {
approvedArtistsOnly = !approvedArtistsOnly;
}
/// @notice whether or not anyone can be an affiliate
function changeApprovedAffilliatesOnly() external onlyOwner {
approvedAffilliatesOnly = !approvedAffilliatesOnly;
}
/// @notice how many tokens must be sent in the createMarket tx which forms the initial pot
/// @param _amount the sponsorship required in wei
function setSponsorshipRequired(uint256 _amount) external onlyOwner {
sponsorshipRequired = _amount;
}
/// @notice if true, Cards in unapproved markets can't be upgraded
function changeTrapCardsIfUnapproved() external onlyOwner {
trapIfUnapproved = !trapIfUnapproved;
}
/// @notice market opening time must be at least this many seconds in the future
/// @param _newAdvancedWarning the warning time to set in seconds
function setAdvancedWarning(uint32 _newAdvancedWarning) external onlyOwner {
advancedWarning = _newAdvancedWarning;
emit LogAdvancedWarning(_newAdvancedWarning);
}
/// @notice market closing time must be no more than this many seconds in the future
/// @param _newMaximumDuration the duration limit to set in seconds
function setMaximumDuration(uint32 _newMaximumDuration) external onlyOwner {
maximumDuration = _newMaximumDuration;
emit LogMaximumDuration(_newMaximumDuration);
}
/// @notice to fetch the owner of the contract
/// @dev used to specifiy the Ownable contract instead of the interface
function owner()
public
view
override(IRCFactory, Ownable)
returns (address)
{
return Ownable.owner();
}
/// @notice check if an address is a governor
/// @param _user the address to query
/// @return boolean return if true or false
function isGovernor(address _user) external view override returns (bool) {
return governors[_user];
}
// EDIT GOVERNORS
/// @notice add or remove an address from market creator whitelist
/// @param _governor the address to change approval for
/// @dev recommended to check isGovernor() afterwards to confirm the desired outcome
function changeGovernorApproval(address _governor) external onlyOwner {
require(_governor != address(0));
governors[_governor] = !governors[_governor];
}
/*╔═════════════════════════════════╗
║ GOVERNANCE - GOVERNORS ║
╚═════════════════════════════════╝*/
/// @dev all functions should have onlyGovernors modifier
/// @notice markets are default hidden from the interface, this reveals them
/// @param _market the market address to change approval for
function changeMarketApproval(address _market) external onlyGovernors {
require(_market != address(0));
// check it's an RC contract
IRCMarket _marketToApprove = IRCMarket(_market);
assert(_marketToApprove.isMarket());
isMarketApproved[_market] = !isMarketApproved[_market];
emit LogMarketApproved(_market, isMarketApproved[_market]);
}
/// @notice artistAddress, passed in createMarket, must be approved
/// @param _artist the artist address to change approval for
function changeArtistApproval(address _artist) external onlyGovernors {
require(_artist != address(0));
isArtistApproved[_artist] = !isArtistApproved[_artist];
}
/// @notice affiliateAddress, passed in createMarket, must be approved
/// @param _affiliate the affiliate address to change approval for
function changeAffiliateApproval(address _affiliate)
external
onlyGovernors
{
require(_affiliate != address(0));
isAffiliateApproved[_affiliate] = !isAffiliateApproved[_affiliate];
}
/// @notice cardAffiliateAddress, passed in createMarket, must be approved
/// @param _affiliate the card affiliate address to change approval for
function changeCardAffiliateApproval(address _affiliate)
external
onlyGovernors
{
require(_affiliate != address(0));
isCardAffiliateApproved[_affiliate] = !isCardAffiliateApproved[
_affiliate
];
}
/*╔═════════════════════════════════╗
║ GOVERNANCE - UBER OWNER ║
╠═════════════════════════════════╣
║ ******** DANGER ZONE ******** ║
╚═════════════════════════════════╝*/
/// @dev uber owner required for upgrades
/// @dev this is seperated so owner so can be set to multisig, or burn address to relinquish upgrade ability
/// @dev ... while maintaining governance over other governanace functions
/// @notice change the reference contract for the contract logic
function setReferenceContractAddress(address _newAddress) external {
require(msgSender() == uberOwner, "Extremely Verboten");
require(_newAddress != address(0));
// check it's an RC contract
IRCMarket newContractVariable = IRCMarket(_newAddress);
assert(newContractVariable.isMarket());
// set
referenceContractAddress = _newAddress;
// increment version
referenceContractVersion += 1;
}
/// @notice to change or renounce ownership of the uberOwner role
function changeUberOwner(address _newUberOwner) external {
require(msgSender() == uberOwner, "Extremely Verboten");
require(_newUberOwner != address(0));
uberOwner = _newUberOwner;
}
/*╔═════════════════════════════════╗
║ MARKET CREATION ║
╚═════════════════════════════════╝*/
/// @notice Creates a new market with the given parameters
/// @param _mode 0 = normal, 1 = winner takes all, 2 = hot potato
/// @param _timestamps for market opening, locking, and oracle resolution
/// @param _tokenURIs location of NFT metadata
/// @param _artistAddress where to send artist's cut, if any
/// @param _affiliateAddress where to send affiliate's cut, if any
/// @param _cardAffiliateAddresses where to send card specific affiliate's cut, if any
/// @param _realitioQuestion the details of the event to send to the oracle
/// @param _sponsorship amount of sponsorship to create the market with
/// @return The address of the new market
function createMarket(
uint32 _mode,
string memory _ipfsHash,
uint32[] memory _timestamps,
string[] memory _tokenURIs,
address _artistAddress,
address _affiliateAddress,
address[] memory _cardAffiliateAddresses,
string calldata _realitioQuestion,
uint256 _sponsorship
) external returns (address) {
address _creator = msgSender();
// check sponsorship
require(
_sponsorship >= sponsorshipRequired,
"Insufficient sponsorship"
);
treasury.checkSponsorship(_creator, _sponsorship);
// check stakeholder addresses
// artist
if (approvedArtistsOnly) {
require(
isArtistApproved[_artistAddress] ||
_artistAddress == address(0),
"Artist not approved"
);
}
// affiliate
if (approvedAffilliatesOnly) {
require(
isAffiliateApproved[_affiliateAddress] ||
_affiliateAddress == address(0),
"Affiliate not approved"
);
// card affiliates
for (uint256 i = 0; i < _cardAffiliateAddresses.length; i++) {
require(
isCardAffiliateApproved[_cardAffiliateAddresses[i]] ||
_cardAffiliateAddresses[i] == address(0),
"Card affiliate not approved"
);
}
}
// check market creator is approved
if (marketCreationGovernorsOnly) {
require(governors[_creator] || owner() == _creator, "Not approved");
}
// check timestamps
require(_timestamps.length == 3, "Incorrect number of array elements");
// check market opening time
if (advancedWarning != 0) {
require(
_timestamps[0] >= block.timestamp,
"Market opening time not set"
);
require(
_timestamps[0] - advancedWarning > block.timestamp,
"Market opens too soon"
);
}
// check market locking time
if (maximumDuration != 0) {
require(
_timestamps[1] < block.timestamp + maximumDuration,
"Market locks too late"
);
}
// check oracle resolution time (no more than 1 week after market locking to get result)
require(
_timestamps[1] + (1 weeks) > _timestamps[2] &&
_timestamps[1] <= _timestamps[2],
"Oracle resolution time error"
);
// check the number of NFTs to mint is within limits
require(
_tokenURIs.length <= nftMintingLimit,
"Too many tokens to mint"
);
// create the market and emit the appropriate events
// two events to avoid stack too deep error
address _newAddress = Clones.clone(referenceContractAddress);
emit LogMarketCreated1(
_newAddress,
address(treasury),
address(nfthub),
referenceContractVersion
);
emit LogMarketCreated2(
_newAddress,
_mode,
_tokenURIs,
_ipfsHash,
_timestamps,
totalNftMintCount
);
// tell Treasury, Orderbook, and NFT hub about new market
// before initialize as during initialize the market may call the treasury
treasury.addMarket(_newAddress);
nfthub.addMarket(_newAddress);
orderbook.addMarket(
_newAddress,
_tokenURIs.length,
minimumPriceIncreasePercent
);
// update internals
marketAddresses[_mode].push(_newAddress);
mappingOfMarkets[_newAddress] = true;
// initialize the market
IRCMarket(_newAddress).initialize({
_mode: _mode,
_timestamps: _timestamps,
_numberOfTokens: _tokenURIs.length,
_totalNftMintCount: totalNftMintCount,
_artistAddress: _artistAddress,
_affiliateAddress: _affiliateAddress,
_cardAffiliateAddresses: _cardAffiliateAddresses,
_marketCreatorAddress: _creator,
_realitioQuestion: _realitioQuestion
});
// create the NFTs
require(address(nfthub) != address(0), "Nfthub not set");
for (uint256 i = 0; i < _tokenURIs.length; i++) {
uint256 _tokenId = i + totalNftMintCount;
require(
nfthub.mint(_newAddress, _tokenId, _tokenURIs[i]),
"Nft Minting Failed"
);
}
// increment totalNftMintCount
totalNftMintCount = totalNftMintCount + _tokenURIs.length;
// pay sponsorship, if applicable
if (_sponsorship > 0) {
IRCMarket(_newAddress).sponsor(_creator, _sponsorship);
}
return _newAddress;
}
/// @dev called by the market upon initialise
/// @dev not passed to initialise to avoid stack too deep error
function getOracleSettings()
external
view
override
returns (
IRealitio,
address,
uint32
)
{
return (realitio, arbitrator, timeout);
}
/*
▲
▲ ▲
*/
}