How to Build Dynamic NFTs on Polygon

Dynamic Non-Fungible Tokens (dNFTs) are the next stage in the evolution of the NFT space, combining the verifiably unique nature of NFTs with dynamic data inputs and off-chain computation. Oracles are fundamental in bringing dynamic elements to NFTs, supplying them with inputs such as provably fair, tamper-proof randomness and a wide variety of data from the real world. 

In this technical tutorial, you’ll learn how to build dNFTs based on real-time weather data supplied by Chainlink oracles on Polygon.

What Is Polygon?

Polygon (formerly Matic Network) is a scaling framework for building Ethereum-compatible blockchains. Instead of providing just one or two scaling solutions, Polygon is creating an ecosystem that connects multiple different scaling solutions, including sidechains with different consensus mechanisms and layer-2 options such as Plasma, Optimistic Rollups, and ZK-Rollups. Polygon’s framework also allows new projects to quickly and easily build their own unique scaling solutions. Polygon distinguishes itself from other blockchain scaling and interoperability projects with its Ethereum Virtual Machine (EVM) compatibility, an optional shared security model, and its advanced flexibility.

Building With Polygon

Popular NFT-based gaming projects such as Aavegotchi and Polychain Monsters have launched using Polygon’s scaling technology, with both having integrated Chainlink Verifiable Random Function (VRF) to create a more dynamic experience for their users. However, verifiable randomness is not the only input developers can use to build dApps on Polygon. By utilizing weather temperature feeds provided by Chainlink on Polygon, developers can create dynamic NFTs that change based on off-chain data provided by secure oracles.

Why Are Dynamic NFTs Important?

Non-Fungible Tokens (NFTs) are often used to represent the ownership of unique assets such as art, but they can also be used to represent dynamic assets, such as a player’s stats during a sports match. Cryptographically secure, decentralized, and fraud-resistant trading cards can be created using data and updated in real time when the data changes—when a new point is scored or a successful assist is recorded, for example. This creates a new level of novelty for NFT collectors, and unlocks new utility in NFT-based gaming applications.

Dynamic NFTs powered by oracles play a key role in gaming dApp Aavegotchi, which has integrated Chainlink VRF to supply it with a source of provable randomness. Chainlink VRF helps ensure that Aavegotchi dNFTs’ unique characteristics are determined fairly and also powers unpredictable in-game scenarios, as well as random DAO juror selection. Aavegotchi launched on Polygon’s layer-2 PoS chain, enabling the game to cost-effectively scale to meet user demand thanks to near-zero transaction fees and fast settlement times. Read the full Aavegotchi Chainlink case study to learn how the popular gaming dApp is pioneering dynamic NFTs on Polygon. 

Chainlink plays a crucial role in supporting scaling solutions such as Polygon by providing low-cost off-chain services that are resistant to manipulation. With Chainlink, developers can access weather data, for example, to build dNFT representations of real-world data such as the current temperature in certain geographic locations. 

One use case for dNFTs like this is to support blockchain-based insurance. Insurance policies can be turned into dNFTs, allowing for customizable crop insurance policies based on weather data that Chainlink oracles fetch from the external world. As they allow for real-time coverage across vast geographies and increased efficiency around payouts, dNFTs are a powerful alternative to traditional forms of insurance that are often subject to manual processing delays and subjective assessment. 

FarmerNet NFTs, winner of the GeoDB Geolocation Oracle and Government Tech prizes of the Chainlink Virtual Hackathon Spring 2021, used Chainlink to create a blockchain marketplace for farmers to earn revenue with carbon credits. Projects like this can grant buyers access to immutable proof for their claims of carbon reduction and renewable energy use through dNFTs. This is just one example of the many benefits oracles provide in the creation of next-generation NFTs.

How to Deploy Dynamic Weather NFTs

Because Polygon is EVM-compatible, we can use tools from our Solidity environment, such as Truffle, Hardhat, MetaMask, and more. In this tutorial, we’ll be using Truffle, a smart contract development framework that allows us to work with Polygon.

Set Up Environment Variables

First, we’ll need to set up the environment variables, so we’ll need a PRIVATE_KEY and a MATIC_RPC_URL environment variable. Your PRIVATE_KEY is the seed phrase of your wallet and you can find a MATIC_RPC_URL from node provider services such as Infura. You will also need some testnet MATIC (mumbai) tokens in your wallet, which you can get from the Mumbai Faucet (you can also get testnet LINK from the Chainlink Faucet).

Then, either set them in a bash_profile file or export them into your terminal:

export MNEMONIC='cat dog frog....'
export RINKEBY_RPC_URL='www.infura.io/asdfadsfafdadf'

Then you can get started with the repo by executing the following commands which will deploy the dynamic NFT on the Polygon chain:

yarn global add truffle

git clone https://github.com/kwsantiago/weather-nft

cd weather-nft

yarn

truffle migrate --network  mumbai

The dNFT we just deployed on-chain will update based on the weather data that is called in the getWeather() function from the WeatherFeed.sol file, which fetches the current temperature in Boston, Massachusetts.

function getWeather() public onlyOwner
returns (bytes32 requestId) {
    Chainlink.Request memory req = buildChainlinkRequest(jobid, address(this), this.fulfill.selector);
    req.add("city", "boston");
    req.add("copyPath", "weather.0.main");
    requestId = sendChainlinkRequestTo(oracle, req, fee);
}

Congratulations, you have deployed your very first dNFT and can see it in action as the weather changes!

Cloudy Chainlink dNFT

Rain Chainlink dNFT

Sun Chainlink dNFT

Snow Chainlink dNFT

See Your dNFT on Etherscan

You can get an Etherscan API key for free and interact with your dNFTs on-chain. Then, you can set ETHERSCAN_API_KEY as an environment variable.

We can verify this by doing the following:

yarn add truffle-plugin-verify

truffle run verify WeatherNFT --network mumbai --license MIT

truffle run verify WeatherFeed --network mumbai --license MIT

This will verify and publish your contract, and you can go to the `Read Contract` section of Etherscan that it gives you.

Otherwise, you can use oneclickdapp and just add the contract address and ABI. You can find the ABI in the `build/contracts` folder. Just remember that the ABI is not the whole file, just the section that says `ABI`.

Start Building Dynamic NFTs Today

Creating dNFTs is easy when you combine fast and cost-effective platforms like Polygon with powerful decentralized oracle networks (DONs) that expand what’s possible with smart contracts. Polygon dApps can also use Chainlink for a wide range of other use cases, such as using random numbers on Polygon to build provably fair blockchain games or referencing decentralized price feeds on Polygon to power the next revolutionary DeFi protocol. When it comes to building secure, feature-rich dApps, Chainlink’s proven oracle infrastructure unlocks countless possibilities for developers. 

Explore more ways to build with Chainlink by reading the documentation at docs.chain.link. To discuss an integration, reach out to an expert

Need Integration Support?
Talk to an expert
Faucets
Get testnet tokens
Read the Docs
Technical documentation