Last active
May 22, 2023 23:48
-
-
Save ChristianOConnor/0ae15b021521c9583dde6c3cc782d4d3 to your computer and use it in GitHub Desktop.
Mint NFTs from enum based on random number from API3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: UNLICENSED | |
pragma solidity ^0.8.18; | |
import "@openzeppelin/contracts/access/Ownable.sol"; | |
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol"; | |
import "@api3/airnode-protocol/contracts/rrp/requesters/RrpRequesterV0.sol"; | |
contract RandomSurfaceReachT1 is ERC721URIStorage, Ownable, RrpRequesterV0 { | |
event RequestedUint256(bytes32 indexed requestId); | |
event ReturnedSurfaceReachT1(bytes32 indexed requestId, uint256 response); | |
address public airnode; | |
bytes32 public endpointIdUint256; | |
address public sponsorWallet; | |
uint256 public tokenCounter; | |
enum Classifier{FIRST, SECOND, THIRD} | |
mapping(uint256 => Classifier) public tokenIdToClassifier; | |
mapping(bytes32 => bool) public waitingFulfillment; | |
mapping(bytes32 => address) requestToRequester; | |
constructor(address _airnodeRrp) RrpRequesterV0(_airnodeRrp) ERC721("SURFACE REACHT1 NFT", "SURFACE REACHT1 NFT"){ | |
tokenCounter = 0; | |
} | |
function setRequestParameters( | |
address _airnode, | |
bytes32 _endpointIdUint256, | |
address _sponsorWallet | |
) external onlyOwner { | |
airnode = _airnode; | |
endpointIdUint256 = _endpointIdUint256; | |
sponsorWallet = _sponsorWallet; | |
} | |
// QRNG Request Function | |
function requestRandomSurfaceReachT1() external { | |
bytes32 requestId = airnodeRrp.makeFullRequest( | |
airnode, | |
endpointIdUint256, | |
address(this), | |
sponsorWallet, | |
address(this), | |
this.mintSurfaceReacher.selector, | |
"" | |
); | |
waitingFulfillment[requestId] = true; | |
requestToRequester[requestId] = msg.sender; | |
emit RequestedUint256(requestId); | |
} | |
// QRNG Fullfill Request | |
function mintSurfaceReacher(bytes32 requestId, bytes calldata data) | |
external | |
onlyAirnodeRrp | |
{ | |
require( | |
waitingFulfillment[requestId], | |
"Request ID not known" | |
); | |
waitingFulfillment[requestId] = false; | |
uint256 newId = tokenCounter; | |
uint256 qrngUint256 = abi.decode(data, (uint256)); | |
// Do what you want with `qrngUint256` here... | |
_safeMint(requestToRequester[requestId], newId); | |
_setTokenURI(newId, ""); | |
Classifier classifier = Classifier(qrngUint256 % 3); | |
tokenIdToClassifier[newId] = classifier; | |
tokenCounter = tokenCounter + 1; | |
emit ReturnedSurfaceReachT1(requestId, qrngUint256); | |
} | |
function setTokenURI(uint256 tokenId, string memory _tokenURI) public { | |
require( | |
_isApprovedOrOwner(_msgSender(), tokenId), | |
"ERC721: transfer caller is not owner nor approved" | |
); | |
_setTokenURI(tokenId, _tokenURI); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment