-
Notifications
You must be signed in to change notification settings - Fork 0
/
RobotsNFT.sol
64 lines (53 loc) · 2.29 KB
/
RobotsNFT.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
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "solady/src/auth/OwnableRoles.sol";
/**
* @title A simple NFT with OwnableRoles
* @dev Only an address with 'minterRole' can mint
*/
contract RobotsNFT is ERC721, OwnableRoles {
uint256 constant minterRole = _ROLE_0;
uint256 newRobotId;
uint256 public totalSupply;
struct Robot {
uint8 attack; // value is in [1;10]
uint8 defence; // value is in [1;10]
uint32 readyTime;
}
// robotId => Robot
mapping (uint256 => Robot) public robots;
constructor () ERC721("Robots", "RBTS") {
_initializeOwner(msg.sender);
}
function setMinter(address _newMinter) external onlyOwner {
_grantRoles(_newMinter, minterRole);
}
function removeMinter(address _oldMinter) external onlyOwner {
_removeRoles(_oldMinter, minterRole);
}
function mint(address _to, uint8 _attack, uint8 _defence, uint32 _readyTime) external onlyRoles(minterRole) returns (uint256) {
_mint(_to, newRobotId);
robots[newRobotId] = Robot(_attack, _defence, _readyTime);
++totalSupply;
unchecked {return ++newRobotId-1;} // saves gas, no underflow, because '++' is always before '-1'
}
/**
* @dev Delete can be removed to keep stats of burned robots,
* also requires an update in getStats(uint256)
*/
function burn(uint256 _robotId) external {
require(ownerOf(_robotId) == msg.sender, "Not the owner!");
_burn(_robotId);
unchecked {--totalSupply;} // saves gas, no underflow, because cannot burn nonexisting robot
delete robots[_robotId];
}
function updateReadyTime(uint256 _robotId, uint32 _newReadyTime) external onlyRoles(minterRole) {
require(_exists(_robotId), "The robot doesn't exist!");
robots[_robotId].readyTime = _newReadyTime;
}
function getStats(uint256 _robotId) external view returns (uint8 attack, uint8 defence, uint32 readyTime) {
require(_exists(_robotId), "The robot doesn't exist!"); // remove, if 'delete robots[]' in burn(uint256) is removed
return (robots[_robotId].attack, robots[_robotId].defence, robots[_robotId].readyTime);
}
}