-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPartyGovernanceNFT.sol
181 lines (160 loc) · 5.67 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
// SPDX-License-Identifier: Beta Software
pragma solidity ^0.8;
import "../utils/ReadOnlyDelegateCall.sol";
import "../utils/LibSafeCast.sol";
import "openzeppelin/contracts/interfaces/IERC2981.sol";
import "../globals/IGlobals.sol";
import "../tokens/IERC721.sol";
import "../vendor/solmate/ERC721.sol";
import "./PartyGovernance.sol";
/// @notice ERC721 functionality built on top of `PartyGovernance`.
contract PartyGovernanceNFT is
PartyGovernance,
ERC721,
IERC2981
{
using LibSafeCast for uint256;
error OnlyMintAuthorityError(address actual, address expected);
// The `Globals` contract storing global configuration values. This contract
// is immutable and it’s address will never change.
IGlobals private immutable _GLOBALS;
/// @notice Who can call `mint()`. Usually this will be the crowdfund contract that
/// created the party.
address public mintAuthority;
/// @notice The number of tokens that have been minted.
uint256 public tokenCount;
/// @notice The voting power of `tokenId`.
mapping (uint256 => uint256) public votingPowerByTokenId;
modifier onlyMinter() {
if (msg.sender != mintAuthority) {
revert OnlyMintAuthorityError(msg.sender, mintAuthority);
}
_;
}
// Set the `Globals` contract. The name of symbol of ERC721 does not matter;
// it will be set in `_initialize()`.
constructor(IGlobals globals) PartyGovernance(globals) ERC721('', '') {
_GLOBALS = globals;
}
// Initialize storage for proxy contracts.
function _initialize(
string memory name_,
string memory symbol_,
PartyGovernance.GovernanceOpts memory governanceOpts,
IERC721[] memory preciousTokens,
uint256[] memory preciousTokenIds,
address mintAuthority_
)
internal
{
PartyGovernance._initialize(governanceOpts, preciousTokens, preciousTokenIds);
name = name_;
symbol = symbol_;
mintAuthority = mintAuthority_;
}
/// @inheritdoc ERC721
function ownerOf(uint256 tokenId)
public
view
override(ERC721, ITokenDistributorParty)
returns (address owner)
{
return ERC721.ownerOf(tokenId);
}
/// @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 override view 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.
}
/// @inheritdoc ITokenDistributorParty
function getDistributionShareOf(uint256 tokenId) external view returns (uint256) {
return votingPowerByTokenId[tokenId] * 1e18 / _getTotalVotingPower();
}
/// @notice Mint a governance NFT for `owner` with `votingPower` and
/// immediately delegate voting power to `delegate.`
/// @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
onlyMinter
onlyDelegateCall
{
uint256 tokenId = ++tokenCount;
votingPowerByTokenId[tokenId] = votingPower;
_adjustVotingPower(owner, votingPower.safeCastUint256ToInt192(), delegate);
_mint(owner, tokenId);
}
/// @inheritdoc ERC721
function transferFrom(address owner, address to, uint256 tokenId)
public
override
onlyDelegateCall
{
// 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
onlyDelegateCall
{
// Transfer voting along with token.
_transferVotingPower(owner, to, votingPowerByTokenId[tokenId]);
super.safeTransferFrom(owner, to, tokenId);
}
/// @inheritdoc ERC721
function safeTransferFrom(address owner, address to, uint256 tokenId, bytes calldata data)
public
override
onlyDelegateCall
{
// Transfer voting along with token.
_transferVotingPower(owner, to, votingPowerByTokenId[tokenId]);
super.safeTransferFrom(owner, to, tokenId, data);
}
/// @notice Relinquish the ability to call `mint()` by an authority.
function abdicate() external onlyMinter onlyDelegateCall {
delete mintAuthority;
}
function _delegateToRenderer() private view {
_readOnlyDelegateCall(
// Instance of IERC721Renderer.
_GLOBALS.getAddress(LibGlobals.GLOBAL_GOVERNANCE_NFT_RENDER_IMPL),
msg.data
);
assert(false); // Will not be reached.
}
}