-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCrowdfundNFTRenderer.sol
177 lines (143 loc) · 6.43 KB
/
CrowdfundNFTRenderer.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
// 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 "../crowdfund/Crowdfund.sol";
contract CrowdfundNFTRenderer is IERC721Renderer {
using LibSafeCast for uint256;
error InvalidTokenIdError();
IGlobals immutable _GLOBALS;
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(Crowdfund(payable(address(this))).name(), " Crowdfund Party");
}
function renderTokenName(uint256 tokenId) internal view returns (string memory) {
return string(abi.encodePacked(
Crowdfund(payable(address(this))).name(),
" #",
Strings.toString(tokenId)
));
}
function renderTokenId(uint256 tokenId) internal pure returns (string memory) {
return string(abi.encodePacked('#', Strings.toString(tokenId)));
}
function renderOwnerAddress(uint256 tokenId) internal view returns (string memory) {
address owner = Crowdfund(payable(address(this))).ownerOf(tokenId);
return string(abi.encodePacked('Owner: ', Strings.toHexString(owner)));
}
function renderCrowdfundState() internal view returns (string memory crowdfundState) {
Crowdfund.CrowdfundLifecycle cfl = Crowdfund(payable(address(this))).getCrowdfundLifecycle();
if (cfl == Crowdfund.CrowdfundLifecycle.Invalid) {
crowdfundState = "Invalid";
} else if (cfl == Crowdfund.CrowdfundLifecycle.Active) {
crowdfundState = "Active";
} else if (cfl == Crowdfund.CrowdfundLifecycle.Expired) {
crowdfundState = "Expired";
} else if (cfl == Crowdfund.CrowdfundLifecycle.Busy) {
crowdfundState = "Busy";
} else if (cfl == Crowdfund.CrowdfundLifecycle.Lost) {
crowdfundState = "Lost";
} else if (cfl == Crowdfund.CrowdfundLifecycle.Won) {
crowdfundState = "Won";
} else {
crowdfundState = "Unknown";
}
}
function renderEthContributed(address contributor) internal view returns (string memory) {
(uint256 ethContributed,,,) =
Crowdfund(payable(address(this))).getContributorInfo(contributor);
return string(abi.encodePacked('ETH contributed: ', Strings.toString(ethContributed)));
}
function renderEthUsed(address contributor) internal view returns (string memory) {
(,uint256 ethUsed,,) =
Crowdfund(payable(address(this))).getContributorInfo(contributor);
return string(abi.encodePacked('ETH used: ', Strings.toString(ethUsed)));
}
function renderEthOwed(address contributor) internal view returns (string memory) {
(,,uint256 ethOwed,) =
Crowdfund(payable(address(this))).getContributorInfo(contributor);
return string(abi.encodePacked('ETH owed: ', Strings.toString(ethOwed)));
}
function tokenURI(uint256 tokenId) external view returns (string memory) {
if (Crowdfund(payable(address(this))).ownerOf(tokenId) == address(0)) {
revert InvalidTokenIdError();
}
string[10] 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(Crowdfund(payable(address(this))).name(), 10, 20);
svgParts[3] = textLine(renderTokenId(tokenId), 10, 40);
svgParts[2] = textLine(Crowdfund(payable(address(this))).symbol(), 10, 80);
svgParts[4] = textLine(renderOwnerAddress(tokenId), 10, 140);
svgParts[5] = textLine(renderCrowdfundState(), 10, 100);
svgParts[6] = textLine(renderEthContributed(address(uint160(tokenId))), 10, 160);
if (Crowdfund(payable(address(this))).getCrowdfundLifecycle() == Crowdfund.CrowdfundLifecycle.Won) {
svgParts[7] = textLine(renderEthUsed(address(uint160(tokenId))), 10, 170);
// svgParts[8] = textLine(renderEthOwed(address(uint160(tokenId))), 10, 180);
}
svgParts[9] = '</svg>';
string memory output = string(
abi.encodePacked(
svgParts[0], svgParts[1], svgParts[2],
svgParts[3], svgParts[4], svgParts[5],
svgParts[6], svgParts[7], /* svgParts[8], */
svgParts[9]
)
);
string memory json = Base64.encode(bytes(
string(
abi.encodePacked(
'{"name":"',
renderTokenName(tokenId),
'", "description": "AuctionCrowdfund Crowdfund 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 Crowdfund NFTs represent your spot in a AuctionCrowdfund party.",
'"}'
// '", "image": "data:image/svg+xml;base64,',
// Base64.encode(bytes(output)),
// '"}'
)
)
));
return string(abi.encodePacked('data:application/json;base64,', json));
}
}