sauravtom
sauravtom

4 min read - Posted 04 Dec 18

Making a URL shortener on Ethereum Blockchain ⛓


The Product

🔗 https://t.co/Rte4yKrN1q shutting down is old news. But have you found a replacement? Well, we did! A link shortener that will keep your link stored for a lifetime ... because it's on the blockchain. Check out @sauravtom's Ethereum URL shortener at https://t.co/QhsWph5eOi!

 — @makersup

How it works

article image

The web app deployed at 0x.now.sh , interacts with a smart contract deployed on Ethereum Ropsten testnet . Being on testnet means you can interact with it using free ethers. Get your free ethers here . > 0x.now.sh > Ethereum Ropsten testnet > free ethers here

The Process
  • Users make a transaction requesting the blockchain to store a URL string.

  • Since it is a transaction that modifies the state of the blockchain, it needs to have some amount of ethers attached to it so that it can be mined to the blockchain by miners.

  • The amount of fee (also called gas ) is determined by the Metamask chrome extension and displayed in the prompt box. > gas

  • There is some waiting time for the transaction to be confirmed (5–10sec) depending on how busy the blockchain is.

  • The shortened URL is generated. eg https://0x.now.sh/s?id=23 > https://0x.now.sh/s?id=23

  • When the users clicks on the shortened URL another transaction is made requesting the blockchain which URL string is located at the provided reference (in this case 23)

  • Since this transaction is not changing the state of the blockchain, we do not need to pay any ETH this time, the user is seamlessly redirected to the long URL.

The SmartContract 📃
pragma solidity ^0.4.24;
contract e0x {
 event LinkVisited(string url, uint linkId);
 event LinkAdded(uint linkId, string url);
 
 struct LinkTemplate {
  address userAddress;
  string url;
 }
 
 uint lastLinkId;
 mapping (uint => LinkTemplate) public linkMapping;
 
 constructor() public {
  lastLinkId = 0;
 }
 
 
 function createNewLink(string url) public returns (uint) {
     lastLinkId++;
  linkMapping[lastLinkId] = LinkTemplate(msg.sender, url);
  emit LinkAdded(lastLinkId, url);
  return lastLinkId;
 }
 
 modifier linkExists(uint linkId) {
     //link with the given hash does not exist
  if(linkMapping[linkId].userAddress == 0x0000000000000000000000000000000000000000) {
   revert();
  }
  _;
 }
 
 function getLink(uint linkId) linkExists(linkId) public constant
  returns(
   address,
   string
  ) {
      //emit LinkVisited(linkId, link.url);
      LinkTemplate memory link = linkMapping[linkId];
   return(
      link.userAddress,
      link.url
   );
  }
event LinkAdded(uint linkId, string url);
 
 struct LinkTemplate {
  address userAddress;
  string url;
 }
 
 uint lastLinkId;
 mapping (uint => LinkTemplate) public linkMapping;
 
 constructor() public {
  lastLinkId = 0;
 }
 
 
 function createNewLink(string url) public returns (uint) {
     lastLinkId++;
  linkMapping[lastLinkId] = LinkTemplate(msg.sender, url);
  emit LinkAdded(lastLinkId, url);
  return lastLinkId;
 }
 
 modifier linkExists(uint linkId) {
     //link with the given hash does not exist
  if(linkMapping[linkId].userAddress == 0x0000000000000000000000000000000000000000) {
   revert();
  }
  _;
 }
 
 function getLink(uint linkId) linkExists(linkId) public constant
  returns(
   address,
   string
  ) {
      //emit LinkVisited(link.url,linkId);
      LinkTemplate memory link = linkMapping[linkId];
   return(
       link.userAddress,
       link.url
   );
  }

Technical Challenges

Making the shortened URLs accessible in all Browsers

This was quite tricky because interacting with blockchain required the browser to be web3 enabled, and have some wallet installed (metamask or similar). Currently web3 and wallet support is only for Chrome and Firefox on Desktop only. To solve this, I had to create a wallet on the fly as follows

provider = ethers.getDefaultProvider('ropsten');
wallet = ethers.Wallet.createRandom();
wallet = wallet.connect(provider);

Shout out to ethersJS library for having support for this. > ethersJS library

Counting URL visits

The flexibility for using this one all browsers comes at a cost. Each time user clicks on the shortened URL no change is made to the blockchain state, hence it is not possible to determine how many times each link was clicked. This issues remains unsolved. The complete source code for this project is on Github . > Github Pull Requests welcome 😁

Created with Sketch.Content is"CC-BY-SA 4.0" licensed
Article On-chain
Article Author
0 Comments
Related Articles
Remix IDE - Your first smart contract

The easiest place to start writing smart contracts in Solidity in with the online Remix IDE. As its an online IDE, no installation or development environment setup is required, you can navigate to the site and get started! Remix also provides good tools for debugging, static analysis, and deployment all within the online environment. You can find the source code used in this tutorial in this GitHub repository. Before we get started, a quick reminder of what we are building: A dApp which allows a

Chris Ward

20 Nov 19

Getting return values from a solidity transaction

In solidity we have learned that there are two ways to directly interact with smart contracts, by transactions and calls. Transactions change the state of the blockchain, Calls allow you to read that state. There is, however a little wrinkle that I decided to investigate as part of some work into upgradable contracts. I havent seen anybody write about this before so I thought this may be useful to somebody. Writing to the chain A transaction when signed and submitted to the transaction pool and

Dave Appleton

21 Jun 19