-
Notifications
You must be signed in to change notification settings - Fork 14
/
Chainlink.sol
65 lines (54 loc) · 2.36 KB
/
Chainlink.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
//SPDX-License-Identifier: CC0
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/utils/Strings.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@chainlink/contracts/src/v0.8/VRFConsumerBase.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "../BatchReveal.sol";
contract ChainlinkExample is ERC721, VRFConsumerBase, BatchReveal {
using Strings for uint256;
string public baseURI;
string public unrevealedURI;
// Constants from https://docs.chain.link/docs/vrf-contracts/
bytes32 immutable private s_keyHash;
address immutable private linkToken;
address immutable private linkCoordinator;
constructor(string memory _baseURI, string memory _unrevealedURI, bytes32 _s_keyHash, address _linkToken, address _linkCoordinator)
ERC721("Mock NFT", "MNFT")
VRFConsumerBase(_linkCoordinator, _linkToken)
{
linkToken = _linkToken;
linkCoordinator = _linkCoordinator;
s_keyHash = _s_keyHash;
unrevealedURI = _unrevealedURI;
baseURI = _baseURI;
}
uint public totalSupply = 0;
function mint(uint amount) public {
require((totalSupply + amount) <= TOKEN_LIMIT, "limit reached");
for(uint i = 0; i<amount; i++){
_mint(msg.sender, totalSupply);
totalSupply++;
}
}
// batchNumber belongs to [0, TOKEN_LIMIT/REVEAL_BATCH_SIZE]
// if fee is incorrect chainlink's coordinator will just revert the tx so it's good
function requestRandomSeed(uint s_fee) public returns (bytes32 requestId) {
require(totalSupply >= (lastTokenRevealed + REVEAL_BATCH_SIZE), "totalSupply too low");
// checking LINK balance
require(IERC20(linkToken).balanceOf(address(this)) >= s_fee, "Not enough LINK to pay fee");
// requesting randomness
requestId = requestRandomness(s_keyHash, s_fee);
}
function fulfillRandomness(bytes32, uint256 randomness) internal override {
require(totalSupply >= (lastTokenRevealed + REVEAL_BATCH_SIZE), "totalSupply too low");
setBatchSeed(randomness);
}
function tokenURI(uint256 id) public view override returns (string memory) {
if(id >= lastTokenRevealed){
return unrevealedURI;
} else {
return string(abi.encodePacked(baseURI, getShuffledTokenId(id).toString()));
}
}
}