Chromia
Start Building

07 / 15Module 2: Model NFTs in RellAdvanced

Define the NFT model

In this project, you define a complete NFT model on-chain and implement it in Rell.

Begin with a base entity called nft. It represents an NFT through a unique ID and a mutable attribute that holds the NFT's owner. Create the file src/rell_marketplace/nft.rell and add this code:

src/rell_marketplace/nft.rell
entity nft {    key id: byte_array = op_context.transaction.tx_rid;    mutable owner: account;    index owner;}

The id property holds the Transaction RID of the minted NFT. The owner property is mutable and has the type account. Because you can update the owning account, you can transfer the NFT to another account.

The account entity comes from the FT4 library and represents an account registered in FT4. For details, see the FT4 accounts documentation.

An index on owner speeds up queries that look up who owns an NFT. The trade-off is that changing the owner becomes a more expensive operation. With the base entity defined, you can now build the Gaming Trade Card NFT and give it its own attributes.

src/rell_marketplace/nft.rell
entity nft_card {    key nft;    strength: integer;    health: integer;}

Here is what each part does:

  • key nft links the card to the NFT entity and enforces uniqueness on that link.

  • strength and health are the attributes that make each card distinct.

With these few steps, you have a simple NFT model in place.

Written for this site from Chromia’s public course. Code samples are the course’s own.