Building Decentralised Link Shortner on Ethereum blockchain using Truffle

Blockchain is emerging technology, it needs no introduction. By any chance if you are left behind and don’t know about blockchain then I recommend reading about blockchain first before reading this article. You can read about blockchain here or here or search on internet and you will find plenty of article to read from.

What is Ethereum ?

Launched in 2015, Ethereum is the world’s leading programmable blockchain. It is a global, open-source platform for decentralized applications. These decentralized applications (or “dapps”) gain the benefits of cryptocurrency and blockchain technology. Read more about ethereum here.

What is Truffle ?

Truffle is a framework for blockchain development, it streamlined smart contract creation, compilation, testing, and deployment onto Ethereum. Read more about truffle here

What is Decentralised App ?

Decentralised application does not require centralised server to work (hence no maintenance cost). It interacts with smart contract deployed on blockchain network.

What is Smart Contract ?

Smart contracts are programs which govern the behaviour of accounts within the Ethereum state. We will write smart contract in Solidity language, Solidity is an object-oriented, high-level language for implementing smart contracts. Read more about solidity from here.

Getting Started:

A) Install npm, node & Truffle

Follow for installing npn & node.

Then install truffle

check if truffle installed successfully or not

B) Create Project

Create new folder for project & initialise with truffle. We will use React Truflle box

If you are new to Truffle then read about created directory from

C) Install Ganache for blockchain setup on local machine

Link Shortner Smart Contract

Create LinkShortner.sol file inside contracts/ folder and write following content in it.

Now deploy this contract on local blockchain network:

Ganache Screenshot after contract deployment

React Application for interaction with Smart Contract

Open client/src/App.js file



Creating new link

Install Metamask chrome extension

and run React app

Deploying contract on Ropsten test network

Goto Truffle project, install truffle-hdwallet-provider

Create `.env` file, put MNEMONIC and <network>_URL to file

Update truffle-config with following content

Run following command to deploy

Sinatra API for reading Short Link on ethereum network
Create folder backend
Add following content in backend/app.rb

Deploy sinatra API on heroku

Now use deployed API for reading short link

That’s it, now you have your link shortner decentralised app deployed on ethereum network. Generated short link can be shared with anyone, irrespective of browser. For creating short link Metamask plugin is required.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store