Creating Dynamic NFTs Using Chainlink Keepers + Filecoin's NFT Storage

Intro - What is this blog about?

NFTs alone have already caused disruption in the culture. But this is just the beginning of what we as developers can do and create with NFTs. In this step-by-step tutorial I will explain how I built dynamic NFTs using Chainlink Keepers that automatically change IPFS images that are stored in Filecoin's NFT storage. So before we begin let me explain wtf Chainlink & Filecoin are just in case this is your first time coming across this technology.

What is Chainlink?

This was written by chainlink themselves, you can find this brief summary of what chainlink is under every youtube video. It really sums up everything in a comprehensive way.

Chainlink is the industry standard for building, accessing, and selling oracle services needed to power hybrid smart contracts on any blockchain. Chainlink oracle networks provide smart contracts with a way to reliably connect to any external API and leverage secure off-chain computations for enabling feature-rich applications. Chainlink currently secures tens of billions of dollars across DeFi, insurance, gaming, and other major industries, and offers global enterprises and leading data providers a universal gateway to all blockchains.

What is NFT.Storage & Filecoin?

NFT.Storage is a long-term storage service designed for off-chain NFT data (like metadata, images, and other assets) for up to 31GiB in size per individual upload.NFT.Storage stores many copies of uploaded data on the public IPFS network in two primary ways: in dedicated IPFS servers managed by NFT.Storage, and decentralized on Filecoin.

What exactly is a dynamic NFT?

Put simply, a dynamic NFT is an NFT that can change based on external conditions. Change in a dynamic NFT often refers to changes in the NFT’s metadata triggered by a smart contract. This is done by encoding automatic changes within the NFT smart contract, which provides instructions to the underlying NFT regarding when and how its metadata should change. dNFTs offer a best-of-both-worlds approach, with NFTs retaining their unique identifiers while able to update aspects of their metadata.

Hashlips art engine download from Github

The hashlips art engine is super simple to use, and in my opinion the best way to quickly generate your art in a custom way, while also generating your metadata.

Download the zip file that is located under the green button labeled 'code'

Downloading the Hashlips art generator

Once you download the zip file go ahead and unzip, then open it in Visual Studio Code, or whichever IDE you prefer to use.

Setting up & creating your images folder for the generator

Next, under the layers folder you will create a new folder called 'daytimes'

This is where you will upload your images you want to use. In this case we are uploading 3 images

Configuring the art generator

Once your files are loaded head back to VSC and under your 'src' folder, select the 'config.js' file. This is where you can update the metadata and configure how you want your artwork to be generated.

Scroll down to line 25 under the function called 'layerConfigurations'.

You will go ahead and insert the new folder we made earlier called 'daytimes' within the string. With this function the layer of each object represents the name of the folder we created

Generating the metadata / artwork

To generate the metadata and artwork go ahead and type this in your command line

npm run build

or

node index.js

You will then find your metadata files and image files under the 'build' folder. Keep in mind that it doesn't matter what order they generate in this case since we will be placing the order ourselves in the smart contract.

Create NFT.STORAGE account and download app

https://nft.storage/

Go ahead and head to nft.storage and create an account. In their dashboard is where you will be able to refer back to your files once they are uploaded.

Upload images you generated to NFT Storage

Once you created your account and installed the app, you will then drag the images we generated to the NFT Storage app

This will prompt you that images are being uploaded - give it a few moments to upload and it will provide you with a CID, IPFS url, and a Gateway URL.

Copy CID to update base URI

Copy the CID that is prompted once your files uploaded, and head back to the 'config.js' hashlips generator. 

At line 10, go ahead and paste the CID you copied

And if you want to make any metadata changes, this would be the time to do so.

Updating CID

After you save the updates changes, go ahead and run 'npm run update_info' in your terminal. This will update the metadata across all your generated files with the new CID from NFT Storage. 

npm run update_info

You know you did this right when a message in your terminal pops up and says

Updated baseUri for images to ===>

Upload metadata to NFT Storage

Now that you have updated the metadata with the new CID go ahead and drag your metadata files to your NFT Storage app. Just like you did with the images

Confirming you did that sh*t right

You know you uploaded everything correctly when you copy the IPFS url provided to you and paste it in your URL. When you see your files appear as so with the details - you’re gucci

Keep this page open because we will need the IPFS urls for each of these here shortly.

Remix IDE

https://remix.ethereum.org/

Head over to Remix.Ethereum.org - this is where we will be testing our contract and quickly getting things set up for this tutorial. Leave this tab open as we will be using Remix moving forward.

If you guys would like to see this tutorial with a hardhat environment, let me know and I can add a PART 2.

Go to ChainLink's repo and grab the smart contract

Chainlink Repo

We will go ahead and use the smart contract that ChainLink made that already contains the Keepers functionality + as well as other functions that we need to complete an ERC-721 contract.

Go ahead and paste the smart contract from github inside remix

Copy IPFS URLs from json to contract in Remix

Go ahead and paste the IPFS urls for each of the 3 images that we uploaded in lines 16-18

Set up environment in Remix to test on Mumbai(polygon) test network

Now we will set up the Remix environment so we don't have any errors when we begin minting and testing this out on the Mumbai Network

Keep in mind that you have to have metamask installed already, along with the polygon test network mumbai added to your wallet as well. Oh, and make sure you have link and matic to use for gas!

Deploy contract

Once you got the compiler environment setup, go ahead and deploy with an interval of 30 as shown below

You know your contract was deployed successfully when you see your contract appear here - as well as in the remix terminal

Mint nft

To mint a NFT, copy your wallet address in metamask. Then select the function called 'safeMint' and paste your address. Once you do so go ahead and Mint your nft

We can confirm it was a success not only by our terminal, but we can copy the contract address on the opensea testnet and check out our minted nft. BUT, before we do just that let's register our chainlink keeper.

Copy contract address and register chainlink keeper

Copy your contract address from remix(shown below) and head over to https://keepers.chain.link/mumbai to begin registering your keeper

Chainlink keeper set up

Follow the setup I have shown below for you to quickly get your chainlink keeper set up. 

Copy contract address and view in opensea

Cool, now that your keeper is registered and set up lets go ahead and check out the nft we minted just a few minutes ago

In remix, check and confirm keeper has been triggered

Dope, now that you see your NFT in opensea.. How tf do we actually see it update?

Let's head back to remix and play with some functionalities from our smart contract. Under 'checkUpkeep', add these brackets '[]'. This allows us to confirm if the keeper has been triggered.

We can also look back at our chainlink keeper dashboard that will show you the keepers update as well

Go back to opensea and refresh metadata to view updated nft

When we do see our keeper make an update, we can go back to our opensea and refresh our metadata… and wallah! Now you can see that the image along with the metadata has been updated

Summary

Using Chainlink keepers is a great way to automate your smart contracts. To be honest, I don't know anyone who likes to spend money on gas. Using keepers really allows you to eliminate the experience of people spending money on gas to trigger a function, V.S. offloading expensive on-chain automation logic to a decentralized Keepers Network. As for filecoin & NFT storage… that to me is a no brainer… especially for DECENTRALIZED storage. They made the experience for developers super simple.. 

Learn more about Chainlink: 

Website: https://chain.link 

Docs: https://docs.chain.link 

Twitter: https://twitter.com/chainlink