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 :