-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTreasure.sol
More file actions
249 lines (213 loc) · 7.67 KB
/
Treasure.sol
File metadata and controls
249 lines (213 loc) · 7.67 KB
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import {ERC721Upgradeable} from "@openzeppelin/contracts-upgradeable/token/ERC721/ERC721Upgradeable.sol";
import {ERC721URIStorageUpgradeable} from "@openzeppelin/contracts-upgradeable/token/ERC721/extensions/ERC721URIStorageUpgradeable.sol";
import {CountersUpgradeable} from "@openzeppelin/contracts-upgradeable/utils/CountersUpgradeable.sol";
//Only used for rescues.
import {IERC20Upgradeable} from "@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol";
interface IChessSVG {
function tokenURIFromGameState(uint256 gameState, uint256 tokenId, bool flipped)
external pure returns (string memory);
}
contract Treasure is
Initializable,
ERC721Upgradeable,
ERC721URIStorageUpgradeable,
UUPSUpgradeable,
OwnableUpgradeable
{
using CountersUpgradeable for CountersUpgradeable.Counter;
CountersUpgradeable.Counter private _tokenIds;
//Access Storage
mapping(address => bool) public admins;
mapping(uint16 => string) public achievements;
//Treasure Storage
struct Game {
bytes32 movesHash;
uint8 level;
uint16 achievement1; //65,000 types should be more than enough forever.
uint16 achievement2; //If these are onchain, it can be something that future gov token...
uint16 achievement3; //...holders can vote on later.
bool color; //0 white, 1 black
uint256 gameState; // board state for on-chain SVG rendering
}
/// NFT storage
mapping(uint256 => Game) public games;
mapping(uint256 => address) public originalPlayers;
mapping(bytes32 => uint256) public movesHashToId;
// Contract Level Metadata.
string public contractMetaData;
// On-chain SVG renderer
address public svgRenderer;
//Events
event GameMinted(address to, uint256 id, string uri, bytes32 movesHash);
event AdminAdded(address addedBy, address newAdmin);
event AdminRemoved(address addedBy, address newAdmin);
event Received(address sender, uint256 amount);
event RescuedEther(address recipient, uint256 amount);
event RescuedERC20(address token, address recipient, uint256 amount);
event AchievementUpdated(uint16 index, string newText, string oldText);
function initialize() public initializer {
__ERC721_init("Treasure Chess", "CHESS");
__Ownable_init();
admins[msg.sender] = true;
}
function _authorizeUpgrade(address) internal override onlyOwner {}
modifier onlyAdmin() {
require(admins[msg.sender], "Only admins can call this function.");
_;
}
function setSvgRenderer(address _renderer) public onlyAdmin {
svgRenderer = _renderer;
}
function mint(
address player,
string memory _tokenURI,
bytes32 _moveHash,
uint8 _level,
uint16 _achievement1,
uint16 _achievement2,
uint16 _achievement3,
bool _color,
uint256 _gameState
) public onlyAdmin returns (uint256) {
require(
movesHashToId[_moveHash] == 0,
"This game has already been minted."
);
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(player, newItemId);
_setTokenURI(newItemId, _tokenURI);
Game memory newGame = Game(
_moveHash,
_level,
_achievement1,
_achievement2,
_achievement3,
_color,
_gameState
);
games[newItemId] = newGame;
originalPlayers[newItemId] = player;
movesHashToId[_moveHash] = newItemId; //mapping of offchain id to on chain id
emit GameMinted(player, newItemId, _tokenURI, _moveHash);
return newItemId;
}
function getOriginalPlayer(uint256 _id) public view returns (address) {
return originalPlayers[_id];
}
function getTotalGames() public view returns (uint256) {
return _tokenIds.current();
}
function addAdmin(address _admin) public onlyOwner {
admins[_admin] = true;
emit AdminAdded(msg.sender, _admin);
}
/// @param _admin the admin to be removed
function removeAdmin(address _admin) public onlyOwner {
admins[_admin] = false;
emit AdminRemoved(msg.sender, _admin);
}
//Only owner, possibly community can vote on these later.
function updateAchievements(
uint16 _achievementIndex,
string memory _newText
) public onlyOwner {
string memory temp = achievements[_achievementIndex];
achievements[_achievementIndex] = _newText;
emit AchievementUpdated(_achievementIndex, _newText, temp);
}
function getTextAcheivementsByTreasure(uint256 _id)
public
view
returns (string[3] memory)
{
return [
achievements[games[_id].achievement1],
achievements[games[_id].achievement2],
achievements[games[_id].achievement3]
];
}
struct FullGame {
bytes32 movesHash;
uint8 level;
uint16 achievement1; //65,000 types should be more than enough forever.
uint16 achievement2; //If these are onchain, it can be something that future gov token...
uint16 achievement3; //...holders can vote on later.
bool color; //0 white, 1 black
string[3] achTexts; //array of the achiement texts
}
function getFullGameObject(uint256 _id)
public
view
returns (FullGame memory)
{
Game memory game = games[_id];
FullGame memory fullGame = FullGame(
game.movesHash,
game.level,
game.achievement1,
game.achievement2,
game.achievement3,
game.color,
getTextAcheivementsByTreasure(_id)
);
return fullGame;
}
/*
Rescue ERC20s and ERC721s mistakingly sent to the contract
*/
function rescueEtherOwner(address payable to, uint256 amount)
public
onlyOwner
{
(bool ok,) = to.call{value: amount}("");
require(ok, "ETH transfer failed");
emit RescuedEther(to, amount);
}
function rescueERC20Owner(
address _token,
address _to,
uint256 _amount
) public onlyOwner {
IERC20Upgradeable(_token).transfer(_to, _amount);
emit RescuedERC20(_token, _to, _amount);
}
// Override functions required for upgradable
function _burn(uint256 tokenId)
internal
virtual
override(ERC721URIStorageUpgradeable, ERC721Upgradeable)
{
super._burn(tokenId);
}
function tokenURI(uint256 tokenId)
public
view
virtual
override(ERC721Upgradeable, ERC721URIStorageUpgradeable)
returns (string memory)
{
if (svgRenderer != address(0) && games[tokenId].gameState != 0) {
return IChessSVG(svgRenderer).tokenURIFromGameState(
games[tokenId].gameState,
tokenId,
games[tokenId].color // flipped = black's perspective
);
}
return super.tokenURI(tokenId);
}
receive() external payable {
emit Received(msg.sender, msg.value);
}
function contractURI() public view returns (string memory) {
return contractMetaData;
}
function setContractURI(string memory _uri) public onlyOwner {
contractMetaData = _uri;
}
}