This tutorial will show how to use IPFS on harmony blockchain
Harmony blockchain can be used to store data. But is it really worth the cost? To put into perspective 1 byte of a data can cost you 42,107 gas or 0.00042017 ONE token, or equal to about $0.00003092031 in today's market. It maybe small for just one byte of data but let's say you want to store a file with 1 GB data (1,000,000,000 bytes) it will cost you $30920.31. The solution is IPFS, the InterPlanetary File System.
IPFS is a distributed system for storing and accessing files, websites, applications, and data. Using IPFS as a storage you don't need to store entire files to harmony blockchain you just need to store the hash of the IPFS to the harmony blockchain, thus make it much more cheaper then just storing the file.
In this tutorial, we are going to use IPFS to store some files offchain and store the hash of the file to the blockchain. And we will also get the data back from blockchain, and show it to the webpage.
https://www.youtube.com/watch?v=AUzmku4xQLw
https://github.com/harmony-one/harmony-ipfs
In this entire tutorial i'm using windows 10, but you can use any other os.
To install IPFS on your machine, there are so many ways to do it. You can see for yourself here https://docs.ipfs.io/install/command-line/#official-distributions. After that go to your command prompt and type:
This command needed so that we can upload our file without any CORS problem. After that run this command to run ipfs locally:
There is also so many way to download metamask. If you're using chrome you can download it from here https://metamask.io/download.html, and click on install metamask for chrome
after you install the metamask you need to setup the metamask so that it can interact with harmony blockchain click on Custom RPC
like below :
Then after that write this one by one:
Now you have an harmony one account on metamask testnet we need to fill some harmony one token you can do that by going to harmony one testnet faucet like this one https://faucet.pops.one/, to get your address click on the three dot and click on view in explorer like this :
you will then get your address like this :
Put it in the harmony testnet faucet and click send me and you will get your harmony one token filling up.
First we need to install truffle. To install truffle go to your command prompt and type:
Now create a folder and go inside that folder and type on the command prompt:
After you run that command basically you will get some file and folder like this:
So now i want you to go to contract
folder and create a file called IPFSstorage.sol
and paste this code in that file:
So this is actually a really basic smart contract with the purpose of storing the hash of the file to the blockchain. sendHash
is to store the hash, and getHash
is to get the hash. After that go to the migrations
folder and create a file called 2_ipfs_storage_migration.js
and paste this code into that file :
This code is basically used for deploying our contract to our testnet later using truffle. Now go back to the root of our folder and look for truffle-config.js
file and replace the code inside of that file with this :
After that create a file called private-provider.js
and copy paste this code :
Now create a file called package.json
and copy paste this code :
And run npm install
to install all the dependencies. Now create a file called .env
and copy paste this code :
Because we are gonna use testnet, we need to get our private key and set it in the .env
file, on the TESTNET_PRIVATE_KEY
variable. Let's go to the metamask and get our private key. Click the three dot again and click account detail and click export private key :
After you input your password you should see your private key, after that just copy past it in the .env
file, on the TESTNET_PRIVATE_KEY
variable. After that to deploy our smart contract you just need to run this command :
Wait for it to complete and then you will get a contract address like this :
Save that contract address in the safe place, because we will need it in the next step.
First thing first, we need to create a react application to communicate with the harmony blockchain and IPFS, much more easily, we gonna use nextjs framework for this one so to create it first we need to run this command :
It will create a new directory called harmony-ipfs
and install all the dependencies for you. We also need to style our app with some css, so we gonna use chakra-ui for this. Go inside the harmony-ipfs
directory and install the dependencies :
After that create a directory called abi
and copy paste the abi file called IPFSstorage.json
from build/contracts
folder on the root of the project.
Now go inside pages
folder and create a file called _app.js
and copy paste this code :
Now go to index.js and replace the code with this one :
Add all of the import first below import { Container } from "@chakra-ui/react";
:
Let's create all the state that we need so that our application can function properly, below export default function Home() {
copy paste this code :
And then let's create a function that can be used for later to connect to our metamask put it below the state :
And then let's create some code that can trigger the choose file button and also upload to ipfs :
Let's create a function that can be used to upload the file to ipfs and harmony blockchain :
If you remember before you save the contract address of IPFSstorage contract right replace <contract address>
with your saved contract address, then after that let's create a function that can be used to get the hash of the file that we uploaded to IPFS :
Last but not least, create a html that can be used to upload and display our blockchain transaction also our ipfs hash that we get from harmony blockchain copy paste this code below </Head>
tag :
This is our final code of index.js
file :
If you're successfully follow this tutorial you will get something like this :
Let's click on login button and you will see something like this :
Choose your account and click next and connect.
If you see connected button like above you're successfully connected to the site.
Click on choose file button and choose your file.
Click on upload file
Click on confirm and wait for a while.
After that if you get your transaction detail you're successfully uploaded your file to IPFS and harmony blockchain.
If you click on "Get IPFS URL" you will get your IPFS URL.
Congrats! You're successfully uploaded your file to IPFS and harmony blockchain. This is just a simple use case to upload and get a file from harmony blockchain. You can be creative and create a more complex use case like NFT metadata storage or something like that. And if you notice we are using local IPFS node. Which means if our IPFS node is down, you can't get your file. One thing you can do is using service like infura it is a free with some limitation IPFS service. You can also automatically pin your file to infura by changing the localhost:8080
to ipfs.infura.io
, and your file can be seen forever, but you can't unpin it so be careful when using infura to upload your private file. That's it for now if you have any questions or suggestions feel free to ask in Official Harmony Discord
Filecoin and IPFS are complementary protocols for storing and sharing data in the distributed web. Both systems are free, open-source, and share many building blocks, including data representation formats (IPLD) and network communication protocols (libp2p).
IPFS allows users to store and transfer verifiable, content-addressed data in a peer-to-peer network. It is great for getting started using for all sorts of distributed web applications.
IPFS alone does not include a built-in mechanism to incentivize the storage of data for other people. Built on top of IPFS, Filecoin is the distributed storage network to add longer term data persistence via on-chain storage deals, along with built-in economic incentives to ensure files are stored reliably over time.
However, the processes of storing, verifying, and retrieving data are computationally expensive and can take time. Therefore, many solutions (called storage helpers) combine the two networks to get the best of both worlds: IPFS for content addressing & data discovery, and Filecoin for longer-term persistence.
In this section, you will find tutorials to guide you through the process of using IPFS and/or Filecoin for decentralized storage, including storage helpers that combine both systems such as .
Using IPFS
Using NFT.storage
This tutorial will teach you how to use NFT.Storage to store NFTs on IPFS and Filecoin for the Harmony blockchain.
NFT.Storage is a long-term storage service designed for off-chain NFT data (like metadata, images, and other assets). Data is using IPFS, meaning the URL pointing to a piece of data (“ipfs://…”) is completely unique to that data. IPFS URLs can be used in NFTs and metadata to ensure the NFT forever actually refers to the intended data (eliminating things like rug pulls). IPFS URLs are storage layer agnostic - meaning you can store your data on (whether it be on local storage, cloud storage, or multiple decentralized storage networks.
NFT.Storage stores many copies of uploaded data on the public IPFS network and Filecoin network in two primary ways: in dedicated IPFS servers managed by NFT.Storage, and decentralized on . Since IPFS is a standard used by many different storage services, it's easy to redundantly store data uploaded to NFT.Storage on any other IPFS-compatible storage solutions.
This tutorial will walk you through the process of creating a simple NFT on Harmony, using NFT.Storage to store off-chain NFT data on IPFS and Filecoin to achieve a fully decentralized, verifiable NFT.
This tutorial will walk you through the process of creating a simple NFT on Harmony, using NFT.Storage to store off-chain NFT data on IPFS and Filecoin to achieve a fully decentralized, verifiable NFT. It is organized in 4 steps:
Write an NFT smart contract using Truffle
Connect to Harmony blockchain via Metamask
Upload your NFT assets via NFT.Storage
Invoking NFT smart contract to mint NFT via MetaMask
This tutorial requires basic knowledge about Harmony blockchain, HRC721/ERC721, Truffle and Metamask.
Before you start this tutorial, make sure you have installed these necessary tools:
In this step, we will create a React application and use the Truffle framework to developer, compile, and deploy our NFT smart contract.
Create the React project and initialize Truffle
Once the project is created and initialized successfully, you will have a basic React project structure and Truffle project structure which will include:
contracts
for our smart contracts
migrations
for the deployment scripts
truffle.js
for all Truffle-related configurations
Create the Solidity contract
First, install openzepplin
via npm:
Then create a HarmonyNFT.sol
file under the contracts
folder, and copy the ERC721URIStorage smart contract code into that file.
Build smart contract
Now the NFT smart contract is ready, let's compile it.
If the contract compiles successfully, related manifest files will be generated in build/contracts
, which we will use later in this tutorial. (Tip: If you get a solidity compiler version incompatibility error, we can configure it in truffle.js
to use the right compiler version.)
Create deployment script
Next, we need to to deploy the Solidity contract to the Harmony blockchain. Create a file named 2_deploy_contracts.js
under migrations
folder with the following code:
Deploy NFT contract to Harmony Testnet
In order to deploy the NFT smart contract to Harmony Testnet, you need to let Truffle which network configuration and which Harmony wallet to use. You can do this by configuring truffle in truffle-config.js
.
First, install @truffle/hdwallet-provider
via npm.
Now the truffle configuration is all set, we can run the following command to deploy the NFT contract to Harmony Testnet.
In this step, we will write some front-end code to connect to a MetaMask wallet so that you can use it to mint the NFT and pay transaction fees.
Now, let's start coding.
Create a WalletConnect component
This component will detect the wallet provider and connect to MetaMask. Go to the harmony-nft/src
folder which was created as part of your React app in Part 1.
Within harmony-nft/src
, create a new folder components
. Inside components
, create a WalletConnect.js
file and paste in the following code:
Add the WalletConnect component to App.js
Now, let's test the wallet connection function. Run the following command to start the React application.
Once the application is up, you can click the Connect Wallet
button to invoke the MetaMask and authorize the connection.
In this step, we will write the code that mints the NFT. Have the following items ready:
NFT Smart Contract deployed to Harmony Testnet.
NFT contract address
NFT contract manifest JSON file
Metamask browser extension installed and filled your wallet with some TestNet token - ONE.
Create a NFT.Storage account.
In order to use NFT.Storage to upload NFT assets, we will need to sign up an account and create a new API Key which will be used in our code to access NFT.Storage services.
Also we will use ethers to interact with MetaMask to invoke NFT smart contract which is deployed on Harmony blockchain.
Create mintNFT component
Now we will create a MintNFT.js
component in src/components
folder. Let's first create a form to upload NFT Images in MintNFT.js
.
Once the NFT image is captured via form upload, we will upload the NFT asset and metadata to IPFS/Filecoin via NFT.storage, then invoke the NFT smart contract on Harmony to mint a new item. The basic logic will be like following:
Upload NFT asset via NFT.Storage
Now let's set up NFT.Storage so you can upload NFT assets.
Install NFT.Storage dependency in your React app.
First, import NFTstorage and get the API Key that we created in the first step.
Then, initialize the NFTStorage connection using your API KEY.
We can simply call nftStorage.store()
to upload NFT content, which will upload your NFT as well as its related metadata to IPFS & Filecoin. You can customize your NFT metadata by updating properties like name and description.
If NFT asset is uploaded successfully, NFT.Storage will do the following two things:
Store NFT image on IPFS & Filecoin.
Update NFT metadata with the IPFS link to your NFT image, then also store the NFT metadata on IPFS & Filecoin.
Mint a new NFT on Harmony
After getting the metadata on IPFS, we can invoke the NFT smart contract on Harmony to mint a new NFT using that metadata as tokenURI.
In this step, we will need to use ethers
to interact with Metamask, so we first install this dependency.
In order to invoke the NFT contract we have deployed to Harmony Testnet, we need to grab the contract ABI from the manifest JSON file which was generated via smart contract compilation.
Then we are ready to write code to send MintNFT transaction in MintNFT.js
. This will import ethers and the smart contract manifest file, and also create a nftContractAddress
for the deployed NFT contract.
Then we can load the deployed NFT contract and invoke mintItem method.
Preview the new minted NFT
By now, we have all the codes to upload NFT data, send mint item transaction to Harmony. If everything works as expected, we will be able to mint a fresh NFT with its images and metadata stored on IPFS & Filecoin, minting transaction recorded on Harmony Blockchain. Let's add some code to preview our fresh minted NFT, including the NFT image, NFT metadata on IPFS and mintNFT transaction on Harmony.
Now that we've finished writing all the code, we can run npm start
to start our React application. If everything is working, you should see a page like this:
Once we connect to MetaMask and upload a image to mint NFT, we will need to sign the transaction via MetaMask. Then a new NFT on Harmony is minted. We can also check the NFT metadata on IPFS and the minting transaction on Harmony Testnet.
Congratulations! You have written a simple React app for minting NFTs on Harmony with storage on IPFS and Filecoin. Feel free to explore more possibilities.
Next, let's create the smart contract. We will use the so that our NFT contract inherits standard ERC721 functions as well as storage based token URI management.
Add harmony testnet configuration to truffle-config.js
file. Also make sure requesting some TestNet token (ONE) from to the wallet so we can pay for transaction fees for smart contract deployment.
Once the smart contract is deployed success, remember to record your smart contract address which we will also use later. You can also find your NFT contract on , also test it on .
Before we move forward, make sure we have installed MetaMask browser extension and add Harmony Network RPC Endpoint (we will use Harmony Testnet in this tutorial). Check more instructions .
Also make sure requesting some TestNet token (ONE) from to the wallet so we can pay for transaction fees for minting NFT on Harmony Blockchain.
In this step, we will write code to mint a NFT on Harmony blockchain and also upload your NFT asset and metadata to IPFS & Filecoin so that your NFT is truly decentralized other than storing on centralized cloud. Here, we will use a tool called which offers free and long-term storage on IPFS and Filecoin.
We are all set to start coding your logic. Add the following code blocks to MintNFT.js
. (You can also .)
You can find the full .
Looking for the sample code from this tutorial? You can find it in the Github repo.