08 / 15Module 2: Model NFTs in RellAdvanced
Mint NFTs
Now that the NFT model exists, you can move on to minting new NFTs.
Minting
The marketplace will expose operations that let users buy random cards. Each purchase mints a new card whose attributes come from the game rules combined with a random element. The first step is a function that mints a card.
To begin, the function creates an nft_card with fixed properties. Later on, these values will be calculated instead. For now, the goal is only to get the NFT created. Add this function:
function mint_card(account) { val nft = create nft(account); val health = 10; val strength = 8; create nft_card(.nft = nft, .strength = strength, .health = health);}The function first creates an nft instance registered to an account.
It then declares the card attributes and creates the game-specific nft_card, passing in the fixed attribute values.
With minting in place, you can start on the marketplace. For authentication to work in the dapp, you must register an auth_handler. An auth handler is a function that defines the rules every call to the auth.authenticate function must follow. Add this handler:
import lib.ft4.auth;@extend(auth.auth_handler)function () = auth.add_auth_handler( flags = ["T"]);The handler requires the T flag for all operations in the marketplace module. This is the same flag you used when registering the account. If you want a handler to apply only to one specific operation, you can give it a scope. Here, the handler stays global instead, so every operation that uses FT4 authentication falls back to it.
Users first need a way to buy a new card. Create a new file, src/rell_marketplace/marketplace.rell, and add an operation for it:
operation buy_mystery_card() { val account = auth.authenticate(); mint_card(account);}The operation calls the FT4 function auth.authenticate() to check that the user is allowed to perform it. When authentication succeeds, it retrieves the account and uses it to mint a new card.
The next step adds randomness to each generated card so that cards differ from one another.