DEV Community

Anil Kumar Maurya
Anil Kumar Maurya

Posted on • Originally published at Medium on

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

npm install -g truffle

check if truffle installed successfully or not

$ truffle version
Truffle v5.0.21 (core: 5.0.21)
Solidity v0.5.0 (solc-js)
Node v11.0.0
Web3.js v1.0.0-beta.37

B) Create Project

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

$ mkdir link\_shortner
$ cd link\_shortner/
$ truffle unbox react
✔ Preparing to download
✔ Downloading
✔ Cleaning up temporary files
✔ Setting up box

Unbox successful. Sweet!


Compile: truffle compile
  Migrate: truffle migrate
  Test contracts: truffle test

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.

pragma solidity ^0.5.0;

contract LinkShortner {
  event LinkAdded(uint linkId, string url);
  uint lastLinkId;

struct LinkTemplate {
  address userAddress;
  string url;

mapping (uint => LinkTemplate) public linkMapping;

constructor() public {
  lastLinkId = 0;

function createNewLink(string memory url) public returns (uint, string memory) {
  linkMapping[lastLinkId] = LinkTemplate(msg.sender, url);
    emit LinkAdded(lastLinkId, url);
  return(lastLinkId, url);

function getLink(uint linkId) public view returns(address, string memory) {
  LinkTemplate memory link = linkMapping[linkId];
  return(link.userAddress, link.url);

function getLastLink() public view returns(address, string memory, uint) {
  LinkTemplate memory link = linkMapping[lastLinkId];
  return(link.userAddress, link.url, lastLinkId);


Now deploy this contract on local blockchain network:

$ truffle compile
$ truffle migrate

Ganache Screenshot after contract deployment

React Application for interaction with Smart Contract

Open client/src/App.js file


import SimpleStorageContract from "./contracts/SimpleStorage.json";


import SimpleStorageContract from "./contracts/LinkShortner.json";

Creating new link

contract.methods.createNewLink(this.state.url).send({ from: accounts[0] })

Install Metamask chrome extension

and run React app

cd client
npm run start

Deploying contract on Ropsten test network

- Register new account on
- Create new project
- Get project api and connection link:

Goto Truffle project, install truffle-hdwallet-provider

npm install truffle-hdwallet-provider — save

Create .env file, put MNEMONIC and _URL to file

MNEMONIC=wallet mnemonic 12 words

Update truffle-config with following content

const path = require("path");
const HDWalletProvider = require('truffle-hdwallet-provider')
const MNEMONIC = process.env.MNEMONIC
const ROPSTEN\_URL = process.env.ROPSTEN\_URL

module.exports = {
  // See <[](>
  // to customize your Truffle configuration!
  contracts\_build\_directory: path.join(\_\_dirname, "client/src/contracts"),
  networks: {
    ropsten: {
      provider: function() {
        return new HDWalletProvider(MNEMONIC, ROPSTEN\_URL);
      network\_id: '3',
    development: {
      host: "",
      port: 7545,
      network\_id: "\*",
   test: {
     host: "",
     port: 7545,
     network\_id: "\*",

Run following command to deploy

truffle migrate --network ropsten

Sinatra API for reading Short Link on ethereum network

Create folder backend

Add following content in backend/app.rb

# Require the bundler gem and then call Bundler.require to load in all gems
# listed in Gemfile.
require 'bundler'

require 'sinatra'
require 'ethereum'

before do
  content\_type 'application/json'

class Contract
  def initialize
[@client]( ="[](<API-KEY>")
    contract\_json = JSON.parse('LinkShortner.json'))
[@contract\_abi]( = contract\_json['abi']
[@address]( = contract\_json["networks"]["3"]["address"]
[@client](\_account = "0x3b8B0b23C4850FA8289da815a6abEE4Fc2DF941A"

def result(id)
    return nil unless id

def contract\_instance
    Ethereum::Contract.create(name: "LinkShortner", address: [@address](, abi: [@contract\_abi](,
                              client: [@client](
class App < Sinatra::Base
  get '/url' do
    response.headers["Access-Control-Allow-Origin"] = "\*"
    return {url:[:id])}.to\_json

Deploy sinatra API on heroku

heroku create
heroku buildpacks:set [](
heroku buildpacks:add heroku/ruby
heroku config:set PROJECT\_PATH=backend
git push heroku master

Now use deployed API for reading short link

fetch("https://<heroku-app-url>/url?id="+id).then((response) => {
  return response.json();
}).then((response) => {
  const url = response.url

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.

Code is hosted on github

Application is hosted at


Top comments (0)