-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPartyGovernanceNFTRenderer.sol
186 lines (151 loc) · 6.13 KB
/
PartyGovernanceNFTRenderer.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
// SPDX-License-Identifier: Beta Software
pragma solidity ^0.8;
import "../utils/LibSafeCast.sol";
import "../utils/vendor/Strings.sol";
import "../utils/vendor/Base64.sol";
import "./IERC721Renderer.sol";
import "../globals/IGlobals.sol";
import "../party/PartyGovernance.sol";
contract PartyGovernanceNFTRenderer is IERC721Renderer {
using LibSafeCast for uint256;
error InvalidTokenIdError();
IGlobals immutable _GLOBALS;
// The renderer is called via delegateCall, so we need to declare the storage layout.
// Run `yarn layout` to generate the current layout.
bool emergencyExecuteDisabled;
uint16 feeBps;
address payable feeRecipient;
bytes32 preciousListHash;
uint256 lastProposalId;
mapping(address => bool) isHost;
mapping(address => address) delegationsByVoter;
PartyGovernance.GovernanceValues _governanceValues;
mapping(uint256 => PartyGovernance.ProposalState) _proposalStateByProposalId;
mapping(address => PartyGovernance.VotingPowerSnapshot[]) _votingPowerSnapshotsByVoter;
string name;
string symbol;
mapping(uint256 => address) _ownerOf;
mapping(address => uint256) _balanceOf;
mapping(uint256 => address) getApproved;
mapping(address => mapping(address => bool)) isApprovedForAll;
address mintAuthority;
uint256 tokenCount;
mapping(uint256 => uint256) votingPowerByTokenId;
string constant baseStyle = 'base';
constructor(IGlobals globals) {
_GLOBALS = globals;
}
function textLine(string memory text, uint256 xPos, uint256 yPos) internal pure returns (string memory) {
string[3] memory parts;
parts[0] = string(abi.encodePacked(
'<text x="',
Strings.toString(xPos),
'" y="',
Strings.toString(yPos),
'" class="',
// TODO: Parameterize
baseStyle,
'">'
));
parts[1] = text;
parts[2] = '</text>';
return string(abi.encodePacked(
parts[0],
parts[1],
parts[2]
));
}
function renderNFTName() internal view returns (string memory) {
return string.concat(name, " Party");
}
function renderTokenName(uint256 tokenId) internal view returns (string memory) {
return string(abi.encodePacked(
name,
" #",
Strings.toString(tokenId)
));
}
function renderTokenId(uint256 tokenId) internal pure returns (string memory) {
return string(abi.encodePacked('#', Strings.toString(tokenId)));
}
function renderVotingPowerAndDistributionShare(uint256 tokenId) internal view returns (string memory) {
// TODO: Write decimal string library
uint256 votingPower = votingPowerByTokenId[tokenId] * 1e2 / _governanceValues.totalVotingPower;
return string(
abi.encodePacked(
'Voting power: ', Strings.toString(votingPowerByTokenId[tokenId]),
' (',
Strings.toString(votingPower),
'%)'
)
);
}
function renderOwnerAddress(uint256 tokenId) internal view returns (string memory) {
address owner = _ownerOf[tokenId];
return string(abi.encodePacked('Owner: ', Strings.toHexString(owner)));
}
function renderDelegateAddress(uint256 tokenId) internal view returns (string memory) {
address owner = _ownerOf[tokenId];
address delegatedAddress = delegationsByVoter[owner];
return string(abi.encodePacked('Delegate: ', Strings.toHexString(delegatedAddress)));
}
function tokenURI(uint256 tokenId) external view returns (string memory) {
if(_ownerOf[tokenId] == address(0)) {
revert InvalidTokenIdError();
}
string[8] memory svgParts;
svgParts[0] = '<svg xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMin meet" viewBox="0 0 350 350"><style>text { fill: white; font-family: -apple-system, BlinkMacSystemFont, sans-serif; } .base { font-size: 11px; } .detail {font-size: 10px;}</style><rect width="100%" height="100%" fill="black" />';
svgParts[1] = textLine(name, 10, 20);
svgParts[3] = textLine(renderTokenId(tokenId), 300, 20);
svgParts[2] = textLine(symbol, 10, 60);
svgParts[4] = textLine(renderVotingPowerAndDistributionShare(tokenId), 10, 80);
svgParts[5] = textLine(renderOwnerAddress(tokenId), 10, 120);
svgParts[6] = textLine(renderDelegateAddress(tokenId), 10, 140);
svgParts[7] = '</svg>';
string memory output = string(
abi.encodePacked(
svgParts[0], svgParts[1], svgParts[2],
svgParts[3], svgParts[4], svgParts[5],
svgParts[6], svgParts[7]
)
);
string memory json = Base64.encode(bytes(
string(
abi.encodePacked(
'{"name":"',
renderTokenName(tokenId),
'", "description": "AuctionCrowdfund Governance NFT", "image": "data:image/svg+xml;base64,',
Base64.encode(bytes(output)),
'"}'
)
)
));
output = string(abi.encodePacked('data:application/json;base64,', json));
return output;
}
function contractURI() external view returns (string memory) {
string memory json = Base64.encode(bytes(
string(
abi.encodePacked(
'{"name":"',
renderNFTName(),
'", "description":"',
"AuctionCrowdfund Governance NFTs give you voting power in a AuctionCrowdfund party.",
'"}'
// '", "image": "data:image/svg+xml;base64,',
// Base64.encode(bytes(output)),
// '"}'
)
)
));
return string(abi.encodePacked('data:application/json;base64,', json));
}
function royaltyInfo(uint256, uint256)
external
pure
returns (address receiver, uint256 royaltyAmount)
{
receiver = address(0);
royaltyAmount = 0;
}
}