-
Notifications
You must be signed in to change notification settings - Fork 2
/
OnChain.sol
83 lines (64 loc) · 2.6 KB
/
OnChain.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
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;
import "hardhat/console.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
contract Pig is ERC721 {
using Counters for Counters.Counter;
Counters.Counter public idCounter;
enum Rarity {
COMMON,
RARE,
LEGENDARY
}
event MintRequested(address to, uint256 targetBlock);
event PigMinted(address to, uint256 id, Rarity rarity);
// Mapping from address to array of target block
mapping(address => uint256[]) public mintRequests;
mapping(address => uint256[]) public tokenIds;
mapping(uint256 => Rarity) public pigRarity;
uint256 public mintPrice;
constructor() ERC721("Pig", "PIG") {
mintPrice = 0.0001 ether;
}
function mint() external payable {
require(msg.value == mintPrice, "Wrong amount of native token");
requestRandomPig(_msgSender());
}
function requestRandomPig(address to) internal {
// Needed to be + 1 to make impossible to predict
uint256 targetBlock = block.number + 1;
mintRequests[to].push(targetBlock);
emit MintRequested(msg.sender, targetBlock);
}
function processMintRequests() external {
address to = _msgSender();
uint256[] storage requests = mintRequests[to];
for (uint256 i = requests.length; i > 0; --i) {
// block.number needed to be the block defined in the previuous steps to avoid miner withholding
uint256 targetBlock = requests[i - 1];
require(block.number > targetBlock, "Target block not arrived");
uint256 seed = uint256(blockhash(targetBlock));
// Ethereum blockchain only allows access to the 256 most recent blocks
// in a real scenario, a revert case should be implement in case that blockhash is not available anymore
require(seed != 0, "Hash block isn't available");
createPig(to, seed);
requests.pop();
}
}
function createPig(address to, uint256 seed) internal {
uint256 id = idCounter.current();
idCounter.increment();
uint256 randomNumber = uint256(keccak256(abi.encode(seed, id)));
Rarity rarity = Rarity(randomNumber % 3);
pigRarity[id] = Rarity(rarity);
_safeMint(to, id);
emit PigMinted(to, id, rarity);
}
function getRarity(uint256 id) public view returns (Rarity) {
return pigRarity[id];
}
function totalSupply() public view returns (uint256) {
return idCounter.current();
}
}