Created
June 30, 2022 04:24
-
-
Save Ashar2shahid/29e24592ea503c7be4c5339955f37823 to your computer and use it in GitHub Desktop.
The contract we deployed during the workshop on June-28-2022. It uses QRNG to mint a random character and set its attributes
This file contains 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: MIT | |
pragma solidity 0.8.14; | |
import "@openzeppelin/contracts/access/Ownable.sol"; | |
import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; | |
import "@api3/airnode-protocol/contracts/rrp/requesters/RrpRequesterV0.sol"; | |
contract RandomCharacter is ERC721, Ownable, RrpRequesterV0 { | |
struct Character{ | |
uint256 strenght; | |
uint256 intelligence; | |
string name; | |
} | |
Character[] public characters; | |
address public airnode; | |
bytes32 public endpointId; | |
address public sponsorWallet; | |
mapping(bytes32 => bool) expectingRequestIdToBeFulfilled; | |
mapping(bytes32 => address) requestToRequester; | |
mapping(bytes32 => string) requestToName; | |
constructor(address _airnodeRrp) RrpRequesterV0(_airnodeRrp) ERC721("DEMO DAY","DEMO DAY") { | |
} | |
function setRequestParameters( | |
address _airnode, | |
bytes32 _endpointId, | |
address _sponsorWallet | |
) external onlyOwner { | |
airnode = _airnode; | |
endpointId = _endpointId; | |
sponsorWallet = _sponsorWallet; | |
} | |
// QRNG REQUEST FUNCTION | |
function requestRandomCharacter(string memory name) public returns (bytes32) { | |
bytes32 requestId = airnodeRrp.makeFullRequest( | |
airnode, | |
endpointId, | |
address(this), | |
sponsorWallet, | |
address(this), | |
this.mintCharacter.selector, | |
"" | |
); | |
expectingRequestIdToBeFulfilled[requestId] = true; | |
requestToRequester[requestId] = msg.sender; | |
requestToName[requestId] = name; | |
return requestId; | |
} | |
// QRNG FULLFILL FUNCTION | |
function mintCharacter(bytes32 requestId, bytes calldata data) public { | |
require( | |
expectingRequestIdToBeFulfilled[requestId], | |
"Request not awaiting fullfillment" | |
); | |
expectingRequestIdToBeFulfilled[requestId] = false; | |
uint256 randomNumber = abi.decode(data, (uint256)); | |
uint256 newId = characters.length; | |
uint256 strenght = randomNumber % 1000; | |
uint256 intelligence = randomNumber % 100 + 50; | |
characters.push( | |
Character(strenght,intelligence,requestToName[requestId]) | |
); | |
_safeMint(requestToRequester[requestId], newId); | |
} | |
function viewCharacterStats(uint256 tokenId) public view returns (uint256,uint256) { | |
return (characters[tokenId].strenght, characters[tokenId].intelligence); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment