forked from solangegueiros/chainlink-bootcamp-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Runners.sol
202 lines (171 loc) · 7.32 KB
/
Runners.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
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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
// Deploy this contract on Fuji
import "@openzeppelin/[email protected]/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/[email protected]/utils/Counters.sol";
import "@openzeppelin/[email protected]/utils/Base64.sol";
//import "@chainlink/contracts/src/v0.8/interfaces/VRFCoordinatorV2Interface.sol";
//import "@chainlink/contracts/src/v0.8/vrf/VRFConsumerBaseV2Plus.sol";
import {IVRFCoordinatorV2Plus} from "@chainlink/contracts/src/v0.8/vrf/dev/interfaces/IVRFCoordinatorV2Plus.sol";
import {VRFConsumerBaseV2Plus} from "@chainlink/contracts/src/v0.8/vrf/dev/VRFConsumerBaseV2Plus.sol";
import {VRFV2PlusClient} from "@chainlink/contracts/src/v0.8/vrf/dev/libraries/VRFV2PlusClient.sol";
contract Runners is ERC721, ERC721URIStorage, VRFConsumerBaseV2Plus {
using Counters for Counters.Counter;
using Strings for uint256;
// VRF
event RequestSent(uint256 requestId, uint32 numWords);
event RequestFulfilled(uint256 requestId, uint256[] randomWords);
struct RequestStatus {
bool fulfilled; // whether the request has been successfully fulfilled
bool exists; // whether a requestId exists
uint256[] randomWords;
}
mapping(uint256 => RequestStatus) public s_requests; /* requestId --> requestStatus */
// Fuji coordinator
// https://docs.chain.link/vrf/v2-5/supported-networks#avalanche-fuji-testnet
IVRFCoordinatorV2Plus COORDINATOR;
address vrfCoordinator = 0x5C210eF41CD1a72de73bF76eC39637bB0d3d7BEE;
bytes32 keyHash = 0xc799bd1e3bd4d1a41cd4968997a4e03dfd2a3c7c04b695881138580163f42887;
uint32 callbackGasLimit = 2500000;
uint16 requestConfirmations = 3;
uint32 numWords = 1;
// past requests Ids.
uint256[] public requestIds;
uint256 public lastRequestId;
uint256[] public lastRandomWords;
// Your subscription ID.
uint256 public s_subscriptionId;
//Runners NFT
Counters.Counter public tokenIdCounter;
string[] characters_image = [
"https://ipfs.io/ipfs/QmTgqnhFBMkfT9s8PHKcdXBn1f5bG3Q5hmBaR4U6hoTvb1?filename=Chainlink_Elf.png",
"https://ipfs.io/ipfs/QmZGQA92ri1jfzSu61JRaNQXYg1bLuM7p8YT83DzFA2KLH?filename=Chainlink_Knight.png",
"https://ipfs.io/ipfs/QmW1toapYs7M29rzLXTENn3pbvwe8ioikX1PwzACzjfdHP?filename=Chainlink_Orc.png",
"https://ipfs.io/ipfs/QmPMwQtFpEdKrUjpQJfoTeZS1aVSeuJT6Mof7uV29AcUpF?filename=Chainlink_Witch.png"
];
string[] characters_name = [
"Elf",
"Knight",
"Orc",
"Witch"
];
struct Runner {
string name;
string image;
uint256 distance;
uint256 round;
}
Runner[] public runners;
mapping(uint256 => uint256) public request_runner; /* requestId --> tokenId*/
constructor(uint256 subscriptionId) ERC721("Runners", "RUN")
VRFConsumerBaseV2Plus(vrfCoordinator)
{
COORDINATOR = IVRFCoordinatorV2Plus(vrfCoordinator);
s_subscriptionId = subscriptionId;
safeMint(msg.sender,0);
}
function safeMint(address to, uint256 charId) public {
uint8 aux = uint8 (charId);
require( (aux >= 0) && (aux <= 3), "invalid charId");
string memory yourCharacterImage = characters_image[charId];
runners.push(Runner(characters_name[charId], yourCharacterImage, 0, 0));
uint256 tokenId = tokenIdCounter.current();
string memory uri = Base64.encode(
bytes(
string(
abi.encodePacked(
'{"name": "', runners[tokenId].name, '",'
'"description": "Chainlink runner",',
'"image": "', runners[tokenId].image, '",'
'"attributes": [',
'{"trait_type": "distance",',
'"value": ', runners[tokenId].distance.toString(),'}',
',{"trait_type": "round",',
'"value": ', runners[tokenId].round.toString(),'}',
']}'
)
)
)
);
// Create token URI
string memory finalTokenURI = string(
abi.encodePacked("data:application/json;base64,", uri)
);
tokenIdCounter.increment();
_safeMint(to, tokenId);
_setTokenURI(tokenId, finalTokenURI);
}
function run(uint256 tokenId) external returns (uint256 requestId) {
require (tokenId < tokenIdCounter.current(), "tokenId not exists");
requestId = COORDINATOR.requestRandomWords(
VRFV2PlusClient.RandomWordsRequest({
keyHash: keyHash,
subId: s_subscriptionId,
requestConfirmations: requestConfirmations,
callbackGasLimit: callbackGasLimit,
numWords: numWords,
extraArgs: VRFV2PlusClient._argsToBytes(VRFV2PlusClient.ExtraArgsV1({nativePayment: false}))
})
);
s_requests[requestId] = RequestStatus({
randomWords: new uint256[](0),
exists: true,
fulfilled: false
});
requestIds.push(requestId);
lastRequestId = requestId;
emit RequestSent(requestId, numWords);
request_runner[requestId] = tokenId;
return requestId;
}
function fulfillRandomWords(
uint256 _requestId, /* requestId */
uint256[] memory _randomWords
) internal override {
require (tokenIdCounter.current() >= 0, "You must mint a NFT");
require(s_requests[_requestId].exists, "request not found");
s_requests[_requestId].fulfilled = true;
s_requests[_requestId].randomWords = _randomWords;
lastRandomWords = _randomWords;
uint aux = (lastRandomWords[0] % 10 + 1) * 10;
uint256 tokenId = request_runner[_requestId];
runners[tokenId].distance += aux;
runners[tokenId].round ++;
string memory uri = Base64.encode(
bytes(
string(
abi.encodePacked(
'{"name": "', runners[tokenId].name, '",'
'"description": "Chainlink runner",',
'"image": "', runners[tokenId].image, '",'
'"attributes": [',
'{"trait_type": "distance",',
'"value": ', runners[tokenId].distance.toString(),'}',
',{"trait_type": "round",',
'"value": ', runners[tokenId].round.toString(),'}',
']}'
)
)
)
);
// Create token URI
string memory finalTokenURI = string(
abi.encodePacked("data:application/json;base64,", uri)
);
_setTokenURI(tokenId, finalTokenURI);
}
function getRequestStatus(
uint256 _requestId
) external view returns (bool fulfilled, uint256[] memory randomWords) {
}
// The following functions are overrides required by Solidity.
function tokenURI(uint256 tokenId)
public view override(ERC721, ERC721URIStorage) returns (string memory)
{
return super.tokenURI(tokenId);
}
function _burn(uint256 tokenId) internal override(ERC721, ERC721URIStorage)
{
super._burn(tokenId);
}
}