Modify

Add an exclusive NFT collection

The following steps will guide you through a modification of the Kitty Items project. You will add a new NFT kind, representing an exclusive collection of rare NFTs of sunglasses:

sunglasses

Video Walkthrough

YouTube video player
YouTube video player
00:0

Start in emulator

In the last step, you started the project on the testnet. For local development, however, it is recommended to emulate the blockchain network locally using the Flow Emulator. Once changes are implemented and tested locally, you will deploy the updates to the testnet.

Let's start the project in the emulator by running the command below in the root project folder.

Important: You will notice that the previous script execution finished once all services successfully started. You do not need to stop the previous process. By running the next command, existing services will be configured to communicate with the emulator instead of the testnet.

1
npm run dev:emulator

You should see similar logs as in the last step:

1
✔ Emulator started
2
ℹ Flow Emulator is running at: http://localhost:8080
3
ℹ View log output: npx pm2 logs emulator
4
5
✔ Developer Wallet started
6
ℹ FCL Dev Wallet running at: http://localhost:8701
7
ℹ View log output: npx pm2 logs dev-wallet
8
9
✔ Contracts deployed
10
ℹ Contracts were deployed to: 0xf8d6e0586b0a20c7 (emulator)
11
12
✔ Admin account initialized
13
ℹ ./cadence/transactions/nftStorefront/setup_account.cdc was executed successfully.
14
ℹ ./cadence/transactions/kittyItems/setup_account.cdc was executed successfully.
15
16
✔ API server started
17
ℹ Kitty Items API is running at: http://localhost:3000
18
ℹ View log output: npx pm2 logs api
19
20
✔ Storefront web app started
21
ℹ Kitty Items Web App is running at: http://localhost:3001
22
ℹ View log output: npx pm2 logs web
23
24
KITTY ITEMS HAS STARTED
25
26
27
Visit: http://localhost:3001
28
29
? Would you like to view the logs for all processes? (Y/n)

Once again, you will be asked if you want to show all logs. You can enter n for now.

You may notice that you were not prompted for an account. This is because the local setup includes a developer wallet that simulates user accounts.

Add new NFT collection

To add the new NFT collection, we will have to make changes to several components of the project:

  • Backend API: Add the new collection to the list of available collections
  • Web application: Define the new collection name and add it to the map of available collections
  • Cadence: Define the new collection name, add it to the map of available collections, set the content hash for the images (stored on IPFS)

To start making your changes, it is recommended to open the project in a code editor like Visual Studio Code.

Recommended: Install the VSCode extension for Cadence to get syntax highlighting, type checking, and code completion support. If you have VSCode in your $PATH, you can use the CLI to install the extension: flow cadence install-vscode-extension.

Update the backend API

Open the file /api/src/services/kitty-items.ts and add new element (Shades) at the bottom of the Kind enum:

1
enum Kind {
2
Fishbowl = 0,
3
Fishhat,
4
Milkshake,
5
TukTuk,
6
Skateboard,
7
Shades
8
}

Update the web application

Open the file /web/src/global/constants.js and add a new item (Shades) to the ITEM_KIND_MAP constant:

1
export const ITEM_KIND_MAP = {
2
0: "Fishbowl",
3
1: "Fish Hat",
4
2: "Milkshake",
5
3: "TukTuk",
6
4: "Skateboard",
7
5: "Shades",
8
};

Note: To simplify this tutorial, we already added the images for the NFT collection to the project folder. If you were to add new NFTs on your own, you would also need to add images to the web/public/images/kitty-items folder.

Update the Cadence smart contract

Open the /cadence/contracts/KittyItems.cdc file and make the following changes.

Add a new kind

Locate the enum Kind object and add a new case (shades) to the bottom of the list:

1
pub enum Kind: UInt8 {
2
pub case fishbowl
3
pub case fishhat
4
pub case milkshake
5
pub case tuktuk
6
pub case skateboard
7
pub case shades
8
}

Update the kindToString method

This method is used to set the name and description of a specified NFT. Locate the the kindToString method and add a new case (Kind.shades) to the bottom of the switch statement:

1
pub fun kindToString(_ kind: Kind): String {
2
switch kind {
3
case Kind.fishbowl:
4
return "Fishbowl"
5
case Kind.fishhat:
6
return "Fish Hat"
7
case Kind.milkshake:
8
return "Milkshake"
9
case Kind.tuktuk:
10
return "Tuk-Tuk"
11
case Kind.skateboard:
12
return "Skateboard"
13
case Kind.shades:
14
return "Shades"
15
}
16
17
return ""
18
}

Create a transaction to update the list of images for your new kind

Next, you need to create a transaction to update the contract with new links to the images for the new NFT kind.

Note: You can't modify the list of images in the init() function directly because this function only runs the first time your contract is deployed - not on subsequent updates. You can read more about here.

In the root directory of the project, run the following commands to create a new file for the transaction:

1
touch cadence/transactions/kittyItems/add_nft_images_for_new_kind.cdc

Navigate to the new file, open it, and paste the following code:

1
import KittyItems from "../../contracts/KittyItems.cdc"
2
3
// This transction uses the NFTMinter resource to add new image URIs for a new Kind of KittyItems NFT.
4
5
transaction {
6
7
let minter: &KittyItems.NFTMinter
8
9
prepare(signer: AuthAccount) {
10
11
self.minter = signer.borrow<&KittyItems.NFTMinter>(from: KittyItems.MinterStoragePath)
12
?? panic("Only authorized KittyItems NFT Minter can update KittyItems NFT...")
13
14
let NewImages: { KittyItems.Kind: {KittyItems.Rarity: String}} = {
15
16
// The image URIs are hardcoded here for demonstration,
17
// but could also be passed in to the transaction as arguments.
18
19
KittyItems.Kind.shades: {
20
KittyItems.Rarity.blue: "bafybeibtxvitlnvksnzwrwmsqdgnoznosknr3fx5jxjazjcerpa2qo4jy4",
21
KittyItems.Rarity.green: "bafybeicp5bagsziwkyarey76m5jkr6i3a5yrgr7r435qyuutbtlqxcdbwu",
22
KittyItems.Rarity.purple: "bafybeidjigkvt67dtuwrgrpdt2z4dojq2efpbw66ndnffkb6eyr4baml2i",
23
KittyItems.Rarity.gold: "bafybeibtxvitlnvksnzwrwmsqdgnoznosknr3fx5jxjazjcerpa2qo4jy4"
24
}
25
}
26
27
self.minter.addNewImagesForKind(from: signer, newImages: NewImages);
28
29
}
30
31
// the FLOAT NFT secret code is: witty-kitty
32
execute {}
33
}

This transaction borrows the minter Resource from the signing account and calls the addNewImagesForKind method on the minter Resource. It passes a struct containing the links to the new images for the shades kind.

Note: If the signing account does not have the minter Resource, this transaction will fail. This is good, because we only want the account that mints NFTs to be able to modify the contract. Also keep in mind, that if you were to add your own NFT, you would have to upload images to IPFS and store the new hashes instead.

Make sure you saved the file before proceeding to the next step.

Update contract on the emulator

Because you changed the smart contract for Kitty Items, you have to redeploy it to the emulator. Run the following command in your terminal, inside the root folder.

Note: You will notice that the previous script execution finished once all services successfully started. You do not need to stop the previous process. You can run the next command in the same terminal without impact on the services running in the background.

1
npm run update:emulator

The script will update all contracts included in the project. The last one is the one you changed.

1
Deploying 4 contracts for accounts: emulator-account
2
3
NonFungibleToken -> 0xf8d6e0586b0a20c7 (3346...6218)
4
NFTStorefront -> 0xf8d6e0586b0a20c7 (c0e6...662b)
5
MetadataViews -> 0xf8d6e0586b0a20c7 (e6c2...ef11)
6
KittyItems -> 0xf8d6e0586b0a20c7 (3612...dcdb)
7
8
✨ All contracts deployed successfully

Run the new transaction to update the KittyItems contract

Run the following command to send the transaction to update the contract with the new shades images.

1
flow transactions send ./cadence/transactions/kittyItems/add_nft_images_for_new_kind.cdc -n emulator

You should see the following response. Take note of the Status - it should be ✅ SEALED.

1
Transaction ID: af76f20581a3923375fd52428cef078670673295c9d786a8ac9e616661a7f88e
2
3
Status ✅ SEALED
4
ID af76f20581a3923375fd52428cef078670673295c9d786a8ac9e616661a7f88e
5
Payer f8d6e0586b0a20c7
6
Authorizers [f8d6e0586b0a20c7]
7
8
Proposal Key:
9
Address f8d6e0586b0a20c7
10
Index 0
11
Sequence 15
12
13
No Payload Signatures
14
15
Envelope Signature 0: f8d6e0586b0a20c7
16
Signatures (minimized, use --include signatures)
17
18
Events:
19
Index 0
20
Type A.f8d6e0586b0a20c7.KittyItems.ImagesAddedForNewKind
21
Tx ID af76f20581a3923375fd52428cef078670673295c9d786a8ac9e616661a7f88e
22
Values
23
- kind (UInt8): 5

As indicated, the transaction was sealed and you are ready to mint your new KittyItems NFT.

Congratulations! You have completed all changes and your project now includes a new NFT collection for sunglasses.

Mint new sunglasses NFT

To mint one of the new NFTs in your local environment, you have to repeat the minting steps from the last page.

First, open the Kitty Items admin dashboard. Keep in mind that the password is KittyItems.

admin-ui

Now, hit the "Mint Item" button and see a new NFT being generated. The generation of new NFTs is randomized, so you will have to mint a few new NFTs until you will see an NFT from your new sunglasses collection.

During the minting process, you should see the sunglasses flash in the preview pane on the left side of the screen.

Note: It is best to hit the back button in your browser to get back to the "Mint a New Item" screen. You can jump to this screen by opening the admin dashboard directly.