Forked from BetterProgramming/advancedcollectible.sol
Created
March 25, 2021 01:19
-
-
Save XjSv/ef7b5981836489d6e26cb01acf4e9e5d to your computer and use it in GitHub Desktop.
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
pragma solidity 0.6.6; | |
import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; | |
import "@chainlink/contracts/src/v0.6/VRFConsumerBase.sol";contract | |
AdvancedCollectible is ERC721, VRFConsumerBase { | |
uint256 public tokenCounter; | |
enum Breed{PUG, SHIBA_INU, BRENARD} | |
// add other things | |
mapping(bytes32 => address) public requestIdToSender; | |
mapping(bytes32 => string) public requestIdToTokenURI; | |
mapping(uint256 => Breed) public tokenIdToBreed; | |
mapping(bytes32 => uint256) public requestIdToTokenId; | |
event requestedCollectible(bytes32 indexed requestId); | |
bytes32 internal keyHash; | |
uint256 internal fee; | |
uint256 public randomResult; | |
constructor(address _VRFCoordinator, address _LinkToken, bytes32 _keyhash) | |
public | |
VRFConsumerBase(_VRFCoordinator, _LinkToken) | |
ERC721("Dogie", "DOG") | |
{ | |
tokenCounter = 0; | |
keyHash = _keyhash; | |
fee = 0.1 * 10 ** 18; | |
} | |
function createCollectible(string memory tokenURI, uint256 userProvidedSeed) | |
public returns (bytes32){ | |
bytes32 requestId = requestRandomness(keyHash, fee, userProvidedSeed); | |
requestIdToSender[requestId] = msg.sender; | |
requestIdToTokenURI[requestId] = tokenURI; | |
emit requestedCollectible(requestId); | |
} | |
function fulfillRandomness(bytes32 requestId, uint256 randomNumber) internal override { | |
address dogOwner = requestIdToSender[requestId]; | |
string memory tokenURI = requestIdToTokenURI[requestId]; | |
uint256 newItemId = tokenCounter; | |
_safeMint(dogOwner, newItemId); | |
_setTokenURI(newItemId, tokenURI); | |
Breed breed = Breed(randomNumber % 3); | |
tokenIdToBreed[newItemId] = breed; | |
requestIdToTokenId[requestId] = newItemId; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment