Working with Ethereum and IPFS
Tags: bitcoin, ethereum, ethereumblockchain, blockchain
Published: 2021-07-01 10:34:36

---
title: Working with Ethereum and IPFS
published: true
date: 2021-06-29 16:41:27 UTC
tags: bitcoin,ethereum,ethereumblockchain,blockchain
canonical_url: https://medium.com/coinmonks/working-with-ethereum-and-ipfs-b9d83fe09863
---

![](https://cdn-images-1.medium.com/max/1024/0*nQvruA52e8rHOsLX)<figcaption>Photo by <a href="https://unsplash.com/@zoltantasi?utm_source=medium&amp;utm_medium=referral">Zoltan Tasi</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption>

We’ll see a quick demo of how we can use [IPFS](https://ipfs.io/)and [Ethereum](https://ethereum.org/en/)together. We’re going to upload some data on the IPFS network and save the returned Content Identifier (CID) on the Ethereum blockchain, which we can access via the smart contract.   
To start off swiftly you can use a boilerplate that already has the structure for a Dapp project. There are tonnes out there, but get [**this**](https://github.com/cyberhawk12121/Ethereum-dapp-boiler)one in order to follow along.  
**(Note: All the commands are run from within this project directory)**

### Ethereum Setup and Code

Ethereum is a blockchain-based platform for developing decentralised apps and smart contracts. Ethereum blockchain is like a general-purpose blockchain where anybody who wants to do something doesn’t need to make their own blockchain for their applications. They can build apps on top of this blockchain. While IPFS, short for Interplaneary File System is a blockchain network to store files of all sorts. Ethereum was never made to store huge amount of data as it would be very costly for the network. Therefore, we have a solution like IPFS.

Assuming NodeJS and React.js is installed on your device install the following:

1. Install all packages: npm install
2. Install truffle: npm install -g truffle
3. Download Ganache app [**here**](https://www.trufflesuite.com/ganache)

#### Truffle

Truffle is a development environment, testing framework and asset pipeline for Ethereum, aiming to make life as an Ethereum developer easier. You can run truffle compile, truffle migrate and truffle test to compile your contracts, deploy those contracts to the network, and run their associated unit tests. In this demo we’ll only be working in the local environment.

#### Ganache

We also need a local Ethereum blockchain and some Ether to pay the gas fees which is required in transactions that change the state of the blockchain. For that we’ve downloaded [Ganache](https://www.trufflesuite.com/ganache). You can just **QuickStart** Ganache and it’ll be enough.

![](https://cdn-images-1.medium.com/max/1024/1*Qzm2XXG4uOFKBMvM1VB5Pw.png)<figcaption>Ganache main screen after QuickStart</figcaption>

You can see many addresses there and these can be used for paying gas fees in the development process.

#### Writing smart contract

Now, in the ./contracts folder create myContract.sol and write the following code.

```
pragma solidity >=0.4.21 <0.6.0;

contract myContract{
    string data;
    function set(string memory data){
        data=data;
    }

function get() public view returns(string memory){
        return data;
    }
}
```

Here, we have two functions set() and get(). The function set() is just setter function that write some value in the variable data and get() just returns that variables.

> NOTE: If the variable was string **public** data then solidity by-default gives the getter function with the same name as that of the variable, i.e., in this case data() which would simply return that same variable.

#### Deploying smart contract

In the ./migrations directory create a file by the name 2\_deploy\_migrations.js and write the following code:

```
const myContract= artifacts.require('myContract');
module.exports = function(deployer) {
    deployer.deploy(myContract);
};
```

Here, we’ll import the contract then run the deploy command to put it on the blockchain. This file will be used when we’re deploying our smart contract. Before migrations make sure to test your smart contract, because once it’s on the blockchain it cannot be changed. Also compile before doing anything using the commandtruffle compile . To deploy the smart contract on the network, make sure Ganache app is running. Then, we’ll simply go to the console and run:

```
truffle migrate
```

We will receive a transaction confirmation receipt, that will tell everything related to the transaction.   
Please note this deployment happens on the local blockchain network with the app Ganache. Now, you might be wondering how did our application connect with Ganache and deployed the contract there. This all happened because we’re communicating with the application in the background. You can check truffle-config.js where all the configurations are written.

### Setting up IPFS

One way to connect to IPFS is to download IPFS on the system and run it on the machine to make it an IPFS node and connect your app to this, read [here](https://docs.ipfs.io/install/)for the steps necessary. Another method (suitable for this quick demo) is a public client like [Infura](https://infura.io/docs/ipfs). We can use Infura to test and learn about IPFS functionalities and we can upload files to the IPFS network very swiftly without any hassle. To work with IPFS you need to install ipfs-http-client library:

```
npm install --save ipfs-http-client
```

Then we can just connect to the node like this:

```
const create = require('ipfs-http-client')

const ipfs = create('https://ipfs.infura.io:5001')
```

If you had the node running on the local machine the client address would be something like [_http://localhost:5001_](http://localhost:5001). Once that’s done, we can simply run functions like this:

```
const run = async ()=>{
    const data= await ipfs.add(“Hello World”)
    const resultCID= file[‘path’]
    console.log(resultCID);
} // You could put a file buffer as well or anything
run();
```

The function, add() returns some data back and the most one important to note is the CID, which is a string starting with “Qm”. We can access using the second line here (Keep the CID saved somewhere we’ll need it again). You could also make a form in the react app and upload any sort of file to the IPFS network. Learn more about different functions [here](https://www.npmjs.com/package/ipfs-http-client).

> **Content Identifiers (CID)**: A _content identifier_, or CID, is a label used to point to material in IPFS. It doesn’t indicate _where_ the content is stored, but it forms a kind of address based on the content itself. CIDs are formed with the hash of the data itself, hence any change in data would mean different CID allocation for the data. IPFS uses an advanced data structure called Merkle Directed Acyclic Graphs (DAG) in which every node carries the payload and is given an identifier. Read more [**here**](https://docs.ipfs.io/concepts/content-addressing/) **.**

Go to the terminal and run truffle console this will open the console for truffle. You can run the functions set() and get() to set the CID and fetch it back from Ethereum blockchain and then retrieve the data back from IPFS. Before that run the command truffle compile again   
(If needed run deployment commands again). Now, run the console and run these commands.

```
1. const contract= myContract.deployed()
2. setData= contract.set(<paste the CID that was earlier received>)
    // This set() command will consume gas as it's changing state
3. getData= contract.get()
4. getData
```

#### Getting data back from IPFS

To get the data we can hit the URL[https://infura.io/ipfs](https://ipfs.io/ipfs/QmUXTtySmd7LD4p6RG6rZW6RuUuPZXTtNMmRQ6DSQo3aMw)/<CID> or http://localhost:5001/ipfs/<CID> (If node running on the local machine) with the CID of the content and it will show the data.   
Additionally, you can also build a client-side interface and using [web3.js](https://web3js.readthedocs.io/en/v1.3.4/) library users can interact with the smart contract. Let’s save that for another demo.

_Thank you for reading!_

> Join [Coinmonks Telegram Channel](https://t.me/coincodecap) and learn about crypto trading and investing

#### Also, Read

- [Grid Trading Bots](https://blog.coincodecap.com/grid-trading) | [Cryptohopper Review](https://medium.com/coinmonks/cryptohopper-review-a388ff5bae88) | [Crypto Trading Bot](https://blog.coincodecap.com/best-crypto-trading-bots)
- [Crypto Copy Trading Platforms](https://medium.com/coinmonks/top-10-crypto-copy-trading-platforms-for-beginners-d0c37c7d698c) |[How to buy Bitcoin on WazirX](https://medium.com/coinmonks/buy-bitcoin-on-wazirx-2d12b7989af1)
- [CoinLoan Review](https://medium.com/coinmonks/coinloan-review-18128b9badc4) | [Crypto.com Review](https://medium.com/coinmonks/crypto-com-review-f143dca1f74c) | [Huobi Margin Trading](https://medium.com/coinmonks/huobi-margin-trading-b3b06cdc1519)
- [YouHodler vs CoinLoan vs Hodlnaut](https://medium.com/coinmonks/youhodler-vs-coinloan-vs-hodlnaut-b1050acde55a) | [Cryptohopper vs HaasBot](https://blog.coincodecap.com/cryptohopper-vs-haasbot)
- [Top paid cryptocurrency and blockchain courses](https://blog.coincodecap.com/blockchain-courses) | [Binance Review](https://medium.com/coinmonks/binance-review-ee10d3bf3b6e)
- [MXC Exchange Review](https://medium.com/coinmonks/mxc-exchange-review-3af0ec1cba8c) | [Pionex vs Binance](https://blog.coincodecap.com/pionex-vs-binance) | [Pionex Arbitrage Bot](https://blog.coincodecap.com/pionex-arbitrage-bot)
- [How to buy Bitcoin in India?](https://medium.com/coinmonks/buy-bitcoin-in-india-feb50ddfef94) | [WazirX Review](https://medium.com/coinmonks/wazirx-review-5c811b074f5b) | [BitMEX Review](https://blog.coincodecap.com/bitmex-review)
- [Crypto exchanges in India](https://medium.com/coinmonks/bitcoin-exchange-in-india-7f1fe79715c9) | [Bitcoin Savings Account](https://medium.com/coinmonks/bitcoin-savings-account-e65b13f92451) | [HitBTC Review](https://medium.com/coinmonks/hitbtc-review-c5143c5d53c2)
- [Binance Fees](https://medium.com/coinmonks/binance-fees-8588ec17965) | [Botcrypto Review](https://medium.com/coinmonks/botcrypto-review-2021-build-your-own-trading-bot-coincodecap-6b8332d736c7) | [Hotbit Review](https://medium.com/coinmonks/hotbit-review-cd5bec41dafb) | [KuCoin Review](https://blog.coincodecap.com/kucoin-review)
- [My Experience with Crypto Copy Trading](https://medium.com/coinmonks/my-experience-with-crypto-copy-trading-d6feb2ce3ac5) | [AAX Exchange Review](https://medium.com/coinmonks/aax-exchange-review-2021-67c5ea09330c)
- [Bybit Margin Trading](https://medium.com/coinmonks/bybit-margin-trading-e5071676244e) | [Binance Margin Trading](https://medium.com/coinmonks/binance-margin-trading-c9eb5e9d2116) | [Overbit Review](https://medium.com/coinmonks/overbit-review-9446ed4f2188)
- [Cryptocurrency Savings Accounts](https://medium.com/coinmonks/cryptocurrency-savings-accounts-be3bc0feffbf) | [YoBit Review](https://medium.com/coinmonks/yobit-review-175464162c62) | [Bitbns Review](https://medium.com/coinmonks/bitbns-review-38256a07e161)
- [Botsfolio vs Napbots vs Mudrex](https://medium.com/coinmonks/botsfolio-vs-napbots-vs-mudrex-c81344970c02) | [Gate.io Exchange Review](https://medium.com/coinmonks/gate-io-exchange-review-61bf87b7078f)
- [Godex.io Review](https://medium.com/coinmonks/godex-io-review-7366086519fb) | [Invity Review](https://medium.com/coinmonks/invity-review-70f3030c0502) | [BitForex Review](https://medium.com/coinmonks/bitforex-review-c4bb28d9e271) | [Kraken Review](https://medium.com/coinmonks/kraken-review-6165fc1056ac)

* * *