# on-chain URL shortening service

By [metalink.so](https://paragraph.com/@metalink-so) · 2022-10-10

---

Making a decentralized URL shortener on Polygon
-----------------------------------------------

![](https://storage.googleapis.com/papyrus_images/1bec6304c58d468805ab57888da875a98127ec164ea18e81f3636ded22d9d086.png)

[TinyQR](https://tqr.lol/) is an app that lets you create tiny links out and QR codes from long ugly URLs.

The web app deployed at tqr.lol, interacts with a smart contract deployed on the Polygon Mainnet.

### How it works

1.) Users make a transaction requesting the blockchain to store a URL string.

2.) Since it is a transaction that modifies the state of the blockchain, it needs to have some fee(also called gas) attached to it so that it can be mined to the blockchain by miners.

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

4.) The shortened URL is generated. eg this short URL [https://tqr.lol/t/0x04](https://tqr.lol/t/0x04) redirects to this mirror blog post.

5.) When the users clicks on the shortened URL another transaction is made requesting the blockchain which URL string is located at the provided reference. Since this transaction is not changing the state of the blockchain, we do not need to pay any gas fee this time, the user is seamlessly redirected to the long URL.

Here’s the smart contract source code

    pragma solidity ^0.8.0;
    //SPDX-License-Identifier: MIT
    
    
    contract e0x {
        event Log(string message);
        event LinkAdded(uint linkId, string url);
        
        struct LinkTemplate {
            address userAddress;
            string url;
        }
        
        uint lastLinkId;
        mapping (uint => LinkTemplate) public linkMapping;
        
        constructor() {
            lastLinkId = 0;
        }
        
        
        function createNewLink(string memory 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) public view
            returns(
                address,
                string memory
            ) {
                LinkTemplate memory link = linkMapping[linkId];
                return(
                    link.userAddress,
                    link.url
                );
            }
        
    
    }
    

Check it out at

[https://tqr.lol/](https://tqr.lol/)

Smart contract source code (on polygon scan):

[https://polygonscan.com/address/0x2137A56868aA8011F3fC817B913a0bFA3727b655#code](https://polygonscan.com/address/0x2137A56868aA8011F3fC817B913a0bFA3727b655#code)

web app source code (on github)

[https://github.com/sauravtom/ethereum-url-shortener/tree/2022](https://github.com/sauravtom/ethereum-url-shortener/tree/2022)

Pull requests are welcome if you want to build on top of tinyQR

Also, 100 mints on mirror for this article and we will launch on Optimism :)

---

*Originally published on [metalink.so](https://paragraph.com/@metalink-so/on-chain-url-shortening-service)*
