-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPartyGovernanceNFT.sol
501 lines (428 loc) · 19.9 KB
/
PartyGovernanceNFT.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.20;
import "../utils/LibSafeCast.sol";
import "../utils/LibAddress.sol";
import "openzeppelin/contracts/interfaces/IERC2981.sol";
import "../globals/IGlobals.sol";
import "../tokens/IERC721.sol";
import "../vendor/solmate/ERC721.sol";
import "./PartyGovernance.sol";
import "../renderers/RendererStorage.sol";
/// @notice ERC721 functionality built on top of `PartyGovernance`.
contract PartyGovernanceNFT is PartyGovernance, ERC721, IERC2981 {
using LibSafeCast for uint256;
using LibSafeCast for uint96;
using LibERC20Compat for IERC20;
using LibAddress for address payable;
error FixedRageQuitTimestampError(uint40 rageQuitTimestamp);
error CannotRageQuitError(uint40 rageQuitTimestamp);
error CannotDisableRageQuitAfterInitializationError();
error InvalidTokenOrderError();
error BelowMinWithdrawAmountError(uint256 amount, uint256 minAmount);
error NothingToBurnError();
event AuthorityAdded(address indexed authority);
event AuthorityRemoved(address indexed authority);
event RageQuitSet(uint40 oldRageQuitTimestamp, uint40 newRageQuitTimestamp);
event Burn(address caller, uint256 tokenId, uint256 votingPower);
event RageQuit(address caller, uint256[] tokenIds, IERC20[] withdrawTokens, address receiver);
event PartyCardIntrinsicVotingPowerSet(uint256 indexed tokenId, uint256 intrinsicVotingPower);
uint40 private constant ENABLE_RAGEQUIT_PERMANENTLY = 0x6b5b567bfe; // uint40(uint256(keccak256("ENABLE_RAGEQUIT_PERMANENTLY")))
uint40 private constant DISABLE_RAGEQUIT_PERMANENTLY = 0xab2cb21860; // uint40(uint256(keccak256("DISABLE_RAGEQUIT_PERMANENTLY")))
// Token address used to indicate ETH.
address private constant ETH_ADDRESS = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
// The `Globals` contract storing global configuration values. This contract
// is immutable and its address will never change.
IGlobals private immutable _GLOBALS;
/// @notice The number of tokens that have been minted.
uint96 public tokenCount;
/// @notice The total minted voting power.
/// Capped to `_governanceValues.totalVotingPower` unless minting
/// party cards for initial crowdfund.
uint96 public mintedVotingPower;
/// @notice The timestamp until which ragequit is enabled. Can be set to the
/// `ENABLE_RAGEQUIT_PERMANENTLY`/`DISABLE_RAGEQUIT_PERMANENTLY`
/// values to enable/disable ragequit permanently.
/// `DISABLE_RAGEQUIT_PERMANENTLY` can only be set during
/// initialization.
uint40 public rageQuitTimestamp;
/// @notice The voting power of `tokenId`.
mapping(uint256 => uint256) public votingPowerByTokenId;
/// @notice Address with authority to mint cards and update voting power for the party.
mapping(address => bool) public isAuthority;
function _assertAuthority() internal view {
if (!isAuthority[msg.sender]) {
revert NotAuthorized();
}
}
modifier onlySelf() {
if (msg.sender != address(this)) {
revert NotAuthorized();
}
_;
}
// Set the `Globals` contract. The name or symbol of ERC721 does not matter;
// it will be set in `_initialize()`.
constructor(IGlobals globals) payable PartyGovernance(globals) ERC721("", "") {
_GLOBALS = globals;
}
// Initialize storage for proxy contracts.
function _initialize(
string memory name_,
string memory symbol_,
uint256 customizationPresetId,
PartyGovernance.GovernanceOpts memory governanceOpts,
ProposalStorage.ProposalEngineOpts memory proposalEngineOpts,
IERC721[] memory preciousTokens,
uint256[] memory preciousTokenIds,
address[] memory authorities,
uint40 rageQuitTimestamp_
) internal {
PartyGovernance._initialize(
governanceOpts,
proposalEngineOpts,
preciousTokens,
preciousTokenIds
);
name = name_;
symbol = symbol_;
rageQuitTimestamp = rageQuitTimestamp_;
unchecked {
for (uint256 i; i < authorities.length; ++i) {
isAuthority[authorities[i]] = true;
}
}
if (customizationPresetId != 0) {
RendererStorage(_GLOBALS.getAddress(LibGlobals.GLOBAL_RENDERER_STORAGE))
.useCustomizationPreset(customizationPresetId);
}
}
/// @inheritdoc EIP165
function supportsInterface(
bytes4 interfaceId
) public pure override(PartyGovernance, ERC721, IERC165) returns (bool) {
return
PartyGovernance.supportsInterface(interfaceId) ||
ERC721.supportsInterface(interfaceId) ||
interfaceId == type(IERC2981).interfaceId;
}
/// @inheritdoc ERC721
function tokenURI(uint256) public view override returns (string memory) {
_delegateToRenderer();
return ""; // Just to make the compiler happy.
}
/// @notice Returns a URI for the storefront-level metadata for your contract.
function contractURI() external view returns (string memory) {
_delegateToRenderer();
return ""; // Just to make the compiler happy.
}
/// @notice Called with the sale price to determine how much royalty
// is owed and to whom.
function royaltyInfo(uint256, uint256) external view returns (address, uint256) {
_delegateToRenderer();
return (address(0), 0); // Just to make the compiler happy.
}
/// @notice Return the distribution share amount of a token. Included as an alias
/// for `votePowerByTokenId` for backwards compatibility with old
/// `TokenDistributor` implementations.
/// @param tokenId The token ID to query.
/// @return share The distribution shares of `tokenId`.
function getDistributionShareOf(uint256 tokenId) external view returns (uint256) {
return votingPowerByTokenId[tokenId];
}
/// @notice Return the voting power share of a token. Denominated
/// fractions of 1e18. I.e., 1e18 = 100%.
/// @param tokenId The token ID to query.
/// @return share The voting power percentage of `tokenId`.
function getVotingPowerShareOf(uint256 tokenId) public view returns (uint256) {
uint256 totalVotingPower = _getSharedProposalStorage().governanceValues.totalVotingPower;
return
totalVotingPower == 0 ? 0 : (votingPowerByTokenId[tokenId] * 1e18) / totalVotingPower;
}
/// @notice Mint a governance NFT for `owner` with `votingPower` and
/// immediately delegate voting power to `delegate.` Only callable
/// by an authority.
/// @param owner The owner of the NFT.
/// @param votingPower The voting power of the NFT.
/// @param delegate The address to delegate voting power to.
function mint(
address owner,
uint256 votingPower,
address delegate
) external returns (uint256 tokenId) {
_assertAuthority();
uint96 mintedVotingPower_ = mintedVotingPower;
uint96 totalVotingPower = _getSharedProposalStorage().governanceValues.totalVotingPower;
// Cap voting power to remaining unminted voting power supply.
uint96 votingPower_ = votingPower.safeCastUint256ToUint96();
// Allow minting past total voting power if minting party cards for
// initial crowdfund when there is no total voting power.
if (totalVotingPower != 0 && totalVotingPower - mintedVotingPower_ < votingPower_) {
unchecked {
votingPower_ = totalVotingPower - mintedVotingPower_;
}
}
// Update state.
unchecked {
tokenId = ++tokenCount;
}
mintedVotingPower += votingPower_;
votingPowerByTokenId[tokenId] = votingPower_;
emit PartyCardIntrinsicVotingPowerSet(tokenId, votingPower_);
// Use delegate from party over the one set during crowdfund.
address delegate_ = delegationsByVoter[owner];
if (delegate_ != address(0)) {
delegate = delegate_;
}
_adjustVotingPower(owner, votingPower_.safeCastUint96ToInt192(), delegate);
_safeMint(owner, tokenId);
}
/// @notice Add voting power to an existing NFT. Only callable by an
/// authority.
/// @param tokenId The ID of the NFT to add voting power to.
/// @param votingPower The amount of voting power to add.
function increaseVotingPower(uint256 tokenId, uint96 votingPower) external {
_assertAuthority();
uint96 mintedVotingPower_ = mintedVotingPower;
uint96 totalVotingPower = _getSharedProposalStorage().governanceValues.totalVotingPower;
// Cap voting power to remaining unminted voting power supply. Allow
// minting past total voting power if minting party cards for initial
// crowdfund when there is no total voting power.
if (totalVotingPower != 0 && totalVotingPower - mintedVotingPower_ < votingPower) {
unchecked {
votingPower = totalVotingPower - mintedVotingPower_;
}
}
// Update state.
mintedVotingPower += votingPower;
uint256 newIntrinsicVotingPower = votingPowerByTokenId[tokenId] + votingPower;
votingPowerByTokenId[tokenId] = newIntrinsicVotingPower;
emit PartyCardIntrinsicVotingPowerSet(tokenId, newIntrinsicVotingPower);
_adjustVotingPower(ownerOf(tokenId), votingPower.safeCastUint96ToInt192(), address(0));
}
/// @notice Remove voting power from an existing NFT. Only callable by an
/// authority.
/// @param tokenId The ID of the NFT to remove voting power from.
/// @param votingPower The amount of voting power to remove.
function decreaseVotingPower(uint256 tokenId, uint96 votingPower) external {
_assertAuthority();
mintedVotingPower -= votingPower;
votingPowerByTokenId[tokenId] -= votingPower;
_adjustVotingPower(ownerOf(tokenId), -votingPower.safeCastUint96ToInt192(), address(0));
}
/// @notice Increase the total voting power of the party. Only callable by
/// an authority.
/// @param votingPower The new total voting power to add.
function increaseTotalVotingPower(uint96 votingPower) external {
_assertAuthority();
_getSharedProposalStorage().governanceValues.totalVotingPower += votingPower;
}
/// @notice Decrease the total voting power of the party. Only callable by
/// an authority.
/// @param votingPower The new total voting power to add.
function decreaseTotalVotingPower(uint96 votingPower) external {
_assertAuthority();
_getSharedProposalStorage().governanceValues.totalVotingPower -= votingPower;
}
/// @notice Burn governance NFTs and remove their voting power. Can only
/// be called by an authority before the party has started.
/// @param tokenIds The IDs of the governance NFTs to burn.
function burn(uint256[] memory tokenIds) public {
_assertAuthority();
_burnAndUpdateVotingPower(tokenIds, false);
}
function _burnAndUpdateVotingPower(
uint256[] memory tokenIds,
bool checkIfAuthorizedToBurn
) private returns (uint96 totalVotingPowerBurned) {
for (uint256 i; i < tokenIds.length; ++i) {
uint256 tokenId = tokenIds[i];
address owner = ownerOf(tokenId);
// Check if caller is authorized to burn the token.
if (checkIfAuthorizedToBurn) {
if (
msg.sender != owner &&
getApproved[tokenId] != msg.sender &&
!isApprovedForAll[owner][msg.sender]
) {
revert NotAuthorized();
}
}
// Must be retrieved before updating voting power for token to be burned.
uint96 votingPower = votingPowerByTokenId[tokenId].safeCastUint256ToUint96();
totalVotingPowerBurned += votingPower;
// Update voting power for token to be burned.
delete votingPowerByTokenId[tokenId];
emit PartyCardIntrinsicVotingPowerSet(tokenId, 0);
_adjustVotingPower(owner, -votingPower.safeCastUint96ToInt192(), address(0));
// Burn token.
_burn(tokenId);
emit Burn(msg.sender, tokenId, votingPower);
}
// Update minted voting power.
mintedVotingPower -= totalVotingPowerBurned;
}
/// @notice Burn governance NFT and remove its voting power. Can only be
/// called by an authority before the party has started.
/// @param tokenId The ID of the governance NFTs to burn.
function burn(uint256 tokenId) external {
uint256[] memory tokenIds = new uint256[](1);
tokenIds[0] = tokenId;
burn(tokenIds);
}
/// @notice Set the timestamp until which ragequit is enabled.
/// @param newRageQuitTimestamp The new ragequit timestamp.
function setRageQuit(uint40 newRageQuitTimestamp) external {
_assertHost();
// Prevent disabling ragequit after initialization.
if (newRageQuitTimestamp == DISABLE_RAGEQUIT_PERMANENTLY) {
revert CannotDisableRageQuitAfterInitializationError();
}
uint40 oldRageQuitTimestamp = rageQuitTimestamp;
// Prevent setting timestamp if it is permanently enabled/disabled.
if (
oldRageQuitTimestamp == ENABLE_RAGEQUIT_PERMANENTLY ||
oldRageQuitTimestamp == DISABLE_RAGEQUIT_PERMANENTLY
) {
revert FixedRageQuitTimestampError(oldRageQuitTimestamp);
}
emit RageQuitSet(oldRageQuitTimestamp, rageQuitTimestamp = newRageQuitTimestamp);
}
/// @notice Burn a governance NFT and withdraw a fair share of fungible tokens from the party.
/// @param tokenIds The IDs of the governance NFTs to burn.
/// @param withdrawTokens The fungible tokens to withdraw. Specify the
/// `ETH_ADDRESS` value to withdraw ETH.
/// @param minWithdrawAmounts The minimum amount of to withdraw for each token.
/// @param receiver The address to receive the withdrawn tokens.
function rageQuit(
uint256[] calldata tokenIds,
IERC20[] calldata withdrawTokens,
uint256[] calldata minWithdrawAmounts,
address receiver
) external {
if (tokenIds.length == 0) revert NothingToBurnError();
// Check if called by an authority.
bool isAuthority_ = isAuthority[msg.sender];
// Check if ragequit is allowed.
uint40 currentRageQuitTimestamp = rageQuitTimestamp;
if (!isAuthority_) {
if (currentRageQuitTimestamp != ENABLE_RAGEQUIT_PERMANENTLY) {
if (
currentRageQuitTimestamp == DISABLE_RAGEQUIT_PERMANENTLY ||
currentRageQuitTimestamp < block.timestamp
) {
revert CannotRageQuitError(currentRageQuitTimestamp);
}
}
}
// Used as a reentrancy guard. Will be updated back after ragequit.
rageQuitTimestamp = DISABLE_RAGEQUIT_PERMANENTLY;
// Update last rage quit timestamp.
lastRageQuitTimestamp = uint40(block.timestamp);
// Sum up total amount of each token to withdraw.
uint256[] memory withdrawAmounts = new uint256[](withdrawTokens.length);
{
IERC20 prevToken;
for (uint256 i; i < withdrawTokens.length; ++i) {
// Check if order of tokens to transfer is valid.
// Prevent null and duplicate transfers.
if (prevToken >= withdrawTokens[i]) revert InvalidTokenOrderError();
prevToken = withdrawTokens[i];
// Check token's balance.
uint256 balance = address(withdrawTokens[i]) == ETH_ADDRESS
? address(this).balance
: withdrawTokens[i].balanceOf(address(this));
// Add fair share of tokens from the party to total.
for (uint256 j; j < tokenIds.length; ++j) {
// Must be retrieved before burning the token.
withdrawAmounts[i] += (balance * getVotingPowerShareOf(tokenIds[j])) / 1e18;
}
}
}
{
// Burn caller's party cards. This will revert if caller is not the
// the owner or approved for any of the card they are attempting to
// burn, not an authority, or if there are duplicate token IDs.
uint96 totalVotingPowerBurned = _burnAndUpdateVotingPower(tokenIds, !isAuthority_);
// Update total voting power of party.
_getSharedProposalStorage().governanceValues.totalVotingPower -= totalVotingPowerBurned;
}
{
uint16 feeBps_ = feeBps;
for (uint256 i; i < withdrawTokens.length; ++i) {
IERC20 token = withdrawTokens[i];
uint256 amount = withdrawAmounts[i];
// Take fee from amount.
uint256 fee = (amount * feeBps_) / 1e4;
if (fee > 0) {
amount -= fee;
// Transfer fee to fee recipient.
if (address(token) == ETH_ADDRESS) {
payable(feeRecipient).transferEth(fee);
} else {
token.compatTransfer(feeRecipient, fee);
}
}
if (amount > 0) {
uint256 minAmount = minWithdrawAmounts[i];
// Check amount is at least minimum.
if (amount < minAmount) {
revert BelowMinWithdrawAmountError(amount, minAmount);
}
// Transfer token from party to recipient.
if (address(token) == ETH_ADDRESS) {
payable(receiver).transferEth(amount);
} else {
token.compatTransfer(receiver, amount);
}
}
}
}
// Update ragequit timestamp back to before.
rageQuitTimestamp = currentRageQuitTimestamp;
emit RageQuit(msg.sender, tokenIds, withdrawTokens, receiver);
}
/// @inheritdoc ERC721
function transferFrom(address owner, address to, uint256 tokenId) public override {
// Transfer voting along with token.
_transferVotingPower(owner, to, votingPowerByTokenId[tokenId]);
super.transferFrom(owner, to, tokenId);
}
/// @inheritdoc ERC721
function safeTransferFrom(address owner, address to, uint256 tokenId) public override {
// super.safeTransferFrom() will call transferFrom() first which will
// transfer voting power.
super.safeTransferFrom(owner, to, tokenId);
}
/// @inheritdoc ERC721
function safeTransferFrom(
address owner,
address to,
uint256 tokenId,
bytes calldata data
) public override {
// super.safeTransferFrom() will call transferFrom() first which will
// transfer voting power.
super.safeTransferFrom(owner, to, tokenId, data);
}
/// @notice Add a new authority.
/// @dev Used in `AddAuthorityProposal`. Only the party itself can add
/// authorities to prevent it from being used anywhere else.
function addAuthority(address authority) external onlySelf {
isAuthority[authority] = true;
emit AuthorityAdded(authority);
}
/// @notice Relinquish the authority role.
function abdicateAuthority() external {
_assertAuthority();
delete isAuthority[msg.sender];
emit AuthorityRemoved(msg.sender);
}
function _delegateToRenderer() private view {
_readOnlyDelegateCall(
// Instance of IERC721Renderer.
_GLOBALS.getAddress(LibGlobals.GLOBAL_GOVERNANCE_NFT_RENDER_IMPL),
msg.data
);
assert(false); // Will not be reached.
}
}