This tutorial will show how to use IPFS on harmony blockchain
Context
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.
What is IPFS?
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.
This command needed so that we can upload our file without any CORS problem. After that run this command to run ipfs locally:
ipfs daemon
Step 2 - Install Metamask
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:
Network Name :
Harmony Testnet
New RPC URL :
https://api.s0.b.hmny.io
Chain ID :
1666700000
Currency Symbol :
ONE
Block Explorer URL :
https://explorer.pops.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.
Step 3 - Create a smart contract
First we need to install truffle. To install truffle go to your command prompt and type:
npm install -g truffle
Now create a folder and go inside that folder and type on the command prompt:
truffle init
After you run that command basically you will get some file and folder like this:
contracts/ migrations/ test/ truffle-config.js
So now i want you to go to contract folder and create a file called IPFSstorage.sol and paste this code in that file:
pragma solidity 0.8.6;
contract IPFSstorage {
string ipfsHash;
function sendHash(string memory x) public {
ipfsHash = x;
}
function getHash() public view returns (string memory x) {
return ipfsHash;
}
}
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 :
var IPFSstorage =artifacts.require("./IPFSstorage.sol");module.exports=function(deployer) {// Demo is the contract's namedeployer.deploy(IPFSstorage);};
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 :
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 :
truffle migrate --network testnet --reset
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.
Step 4 - Integrating IPFS + Harmony Blockchain
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 :
npm i @chakra-ui/react @emotion/react@^11 @emotion/styled@^11 framer-motion@^4 ipfs-http-client
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 :
And then let's create some code that can trigger the choose file button and also upload to ipfs :
constfileInput=useRef(null); // trigger choose file button for laterconstclient=create('http://localhost:5001/api/v0') // function to create a client to upload to ipfs laterasyncfunctiononChange(e){ // function to detect file changeconstfile=e.target.files[0]setInputFile(file) }
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 :
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.
Conclusion
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