first level title
introduceERC721following(NFTStandard Non-Fungible TokensChainlink VRF) has always been a difficult problem for smart contract developers. Now,
Already launched on the main network, Solidity-based smart contracts can seamlessly generate tamper-proof on-chain random numbers that can be proven to be fair and supported by cryptographic proofs. With Chainlink VRF, creating dynamic NFTs that require a secure source of randomness is simple and safe. While Chainlink can also implement any kind of dynamic properties for NFTs using off-chain data sources, we will focus on using ERC721 nonces, or NFTs.In this tutorial, we will build afirst level title
What are NFTs?
What are NFTs?ERC20NFT (following the ERC721 standard) defines a framework for making tokens that are unique and different from each other (hence the term non-fungible), while the popular"The standard defines"Homogenization"tokens, which means that the tokens are all interchangeable and guaranteed to have the same value."Homogenization
Examples of currencies are USD, EUR, and JPY, while examples of fungible blockchain tokens are AAVE, SNX, and YFI. In these cases, 1 fungible token is equal to 1 other token of the same kind, just like 1 USD is 1 USD and 1 LINK is 1 LINK. However, NFT/ERC721 is different because each token is unique and does not represent the same value or interchangeable items.Since all NFTs are unique, they can represent a tokenized ownership claim on a real-world asset, such as a specific piece of land, or actual ownership of a digital asset, such as a rare digital trading card. And they're growing in popularity. you can refer toOpenSea's NFT Bible
Come read more.
build your random character
uint256 strength;
uint256 dexterity;
uint256 constitution;
uint256 intelligence;
uint256 wisdom;
uint256 charisma;
What we're looking at is creating a character that has the six attributes of a D&D character, namely:
uint256 experience;
string name;
Roles also include:
So we can level them up and give them a fun name.
We took some liberties here and didn't follow the Dungeons & Dragons guidelines 100%, but this can be easily modified if you want a more accurate representation of the game.
This contract should establish the following:
1. Allows you to transfer ownership of the NFT and all other NFT standards.
2. Give the character a name and random attributes.
We've set up the code repository for you, and we'll show you how to use it to get started!
clone code
git clone https://github.com/PatrickAlphaC/dungeons-and-dragons-nft
cd dungeons-and-dragons-nft
npm install
first level title
set environment variablesInfura。
Then, either set them in the `bash_profile` file, or export them to your terminal, e.g.:
export MNEMONIC='cat dog frog....'
export RINKEBY_RPC_URL='www.infura.io/YOUR_PROJECT_ID_HERE'
first level title
what's in the code directory
pragma solidity ^0.6.6;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@chainlink/contracts/src/v0.6/VRFConsumerBase.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
contract DungeonsAndDragonsCharacter is ERC721, VRFConsumerBase, Ownable {
is a collection of packages that make development work easier for Solidity and smart contract engineers. If you haven't used it before, get ready to use it a lot! Since ERC721 is just a token standard, every ERC721 should be similar, so we know that we can use a template. The `ERC721.sol` file we imported defines all the criteria for an NFT. We just inherit it in the contract, defining `contract DungeonsAndDragonsCharacter is ERC721`. We need `VRFConsumerBase.sol` to interact with Chainlink VRF and get nonces. The last two imports just help us with permissions and working with strings.
first level title
Role structure and constructor
struct Character {
uint256 strength;
uint256 dexterity;
uint256 constitution;
uint256 intelligence;
uint256 wisdom;
uint256 charisma;
uint256 experience;
string name;
}
Character[] public characters;
In the Character structure we define the properties our character will have and make a list of characters so we can keep track of every character that is created. Since we are using an array to store the list of characters, each character will have a unique ID in the array to define it. This is called `tokenId` and we will refer to it more.
constructor()
public
VRFConsumerBase(VRFCoordinator, LinkToken)
ERC721("DungeonsAndDragonsCharacter", "D&D")
{
keyHash = 0x2ed0feb3e7fd2022120aa84fab1945545a9f2ffc9076fd6156fa96eaff4c1311;
fee = 0.1 * 10**18; // 0.1 LINK
}
Once defined, we can create the constructor.Corresponding to incoming `VRFConsumerBase` and ERC721 parameters. Chainlink VRF requires VRF coordinator address and LinkToken address. We have hardcoded these addresses in the Rinkeby network. There are also some other variables defined for Chainlink VRF, such as keyHash and fee. you can atChainlink VRF Documentation"DungeonsAndDragonsCharacter", "D&D"Read more about the role of these variables in . `ERC721("D&D"` will be what is shown in MetaMask and the NFT marketplace.
first level title
generate your random character
We want random stats for each of the character's six stats, but we want to be able to decide the character's name ourselves! A simple call to Chainlink VRF allows us to generate random numbers in this NFT/ERC721. In our request function, we don't need to do much, just give the new role a name, and a `userProvidedSeed`. The seed we give it is used to verify to the VRF coordinator that the number provided is truly random. You can choose any seed you like, and you can read about choosing a random seed to learn more.
We want to keep track of the `requestId` so that when the random number is fetched, we can map it to the character we are creating. This will start the Chainlink Job, just wait for the Chainlink node to call back to our contract! You can read more about the request model in the Chainlink documentation to learn more about how sending Chainlink requests works.
When the Chainlink node finishes processing the request, it responds by calling the `fulfillRandomness` function. This function contains calculations for given attributes, adding the character to the list, and minting the NFT.
As you can see, all six attributes are created with just one random number. Subsets the returned large random number using the modulo operation. If we don't want to do this, we can also directly call Chainlink VRF six times, but this way the effect is the same. The last two digits of the nonce returned are for strength, the first two digits are for agility, and so on. This is similar to the way CryptoKitties uses genes to assign values to cats.
*Note: Doing bit operations would be more efficient than what we have here, but it's easier to understand, so we don't have to study how bit operations work. *
We'll be using Truffle and Chainlink, so if you're not familiar with Truffle, this blog post on [How to use Chainlink With Truffle]( will give you a refresher, but we'll cover all the commands in this blog post too!
first level title
Now that we know what's going on, let's deploy our random NFT! you need someRinkeby LINKandRinkeby ETHand
truffle migrate --reset --network rinkeby
truffle exec scripts/fund-contract.js --network rinkeby
truffle exec scripts/generate-character.js --network rinkeby
truffle exec scripts/get-character.js --network rinkeby
to run these scripts.
The above command does the following things:
1. Deploy NFT contract
2. Fund the contract so that Chainlink VRF calls can be made
3. Use Chainlink VRF calls to generate characters
4. Return the NFT valueEtherscan APIOnce deployed, you can also verify the contract and even read the contract on Etherscan using the etherscan plugin. you need to get a
truffle run verify DungeonsAndDragonsCharacter --network rinkeby --license MIT
key, and set the environment variable `ETHSCAN_API_KEY`. After that run:
It will then give you a link to the NFT on Etherscan. You can read the contract content on Etherscan.
This takes you to the page where you can interact with the contract. If you go to the character section, you can enter the tokenId we just generated, 0, and you'll be able to see the stats for your new D&D character.Rinkeybyfirst level title
Summarize
Summarize
developer documentationdeveloper documentationand joinDiscordhereherecontact us.
contact us.
