DEV Community

Cover image for Decentralizing the Future: Knowing Polygon and Its Impact
Andres Ramirez
Andres Ramirez

Posted on

Decentralizing the Future: Knowing Polygon and Its Impact

Web3 is the future and present; decentralized applications, redesign of the classic finance concepts, and redefining data ownership. At kommit, we're interested in all of these concepts as we believe they are the technologies that can pave the way for building the future. In this case, we look at Polygon, the most famous side chain based on the Ethereum blockchain. In this blog post, you will meet Polygon, its advantages and disadvantages and how supports the Ethereum chain with new features, some famous uses case, and our Polygon implementation in a token application to reward our workers for contributing to different aspects.

The Ethereum problem

Ethereum has undoubtedly brought significant advances to the blockchain and cryptocurrencies world, like smart contracts or the creation of applications as decentralized platforms (dApps), but it has not been without its challenges, Ethereum has had many problems related to its performance and costs compared with other blockchains.
For example, Ethereum can only manage between 13 and 30 transactions per second. In contrast, other blockchains such as Stellar, Solana, Kadena, and NEAR can handle between 1,000 and 65,000 transactions per second.

Another problem with Ethereum it's its high costs per transaction. On average, deploying a smart contract could cost around $500 - $1000 (depending on the contract's complexity), and the average of each transaction could cost $0.0015 – $0.0025 depending on the gas price; this means that per 10,000, you can spend between $150 and $300.
These problems are inherent to all the dApps deployed in the network.

Polygon as a solution

Based on the aforementioned Ethereum issues, in 2016, three Indian developers (Jayanti Kanani, Sandeep Nailwal, and Anurag Arjun) created Polygon as an open-source, decentralized platform, running next to the Ethereum Blockchain with full compatibility with the EVM (Ethereum Virtual Machine). That means it allows running any dApps from Ethereum on its network, with cheaper transactions and higher transaction rates per second, relying on Ethereum security without losing its scalable focus. Currently, there are more than 53,000 dApps deployed on the polygon network, many of which have migrated from the Ethereum blockchain.
One of the significant advantages of Polygon is that it is possible to deploy a dApps that works with some specific nodes to manage higher performance, this centralization can look like a disadvantage for a blockchain in terms of security, but all the blockchain data is written in a summarized way on the main blockchain, so we are not losing security.
Polygon also can use the standards for token creation from Ethereum, this will allow us to create a token with particular functionalities we want in an environment with better performance.

Polygon Architecture

Polygon is basically a layer-2 scaling platform, an Ethereum side chain network that uses a Proof of Stake as a consensus mechanism. This allows for improving the performance of the main blockchain by connecting other blockchains together, this means that Polygon is basically a series of blockchains connected together that help to scale Ethereum. Also, Polygon groups a bunch of transactions, processes and validates them together, and notifies the results to the main Ethereum network. In this way, the main network doesn’t have to process all of these transactions just only one at a time, once to record the current state of the Polygon blockchain. Because of this, Polygon can achieve a high amount of transactions per second and, at the same time rely on Ethereum security.

The validator nodes not only create the blocks for the blockchain but also create a Merkle Tree that correlates unequivocally with all the transactions in a block. That tree is the only data sent to the Ethereum main blockchain.

Innovations

Layer 2 scaling solution: Polygon uses this solution to offload some of the data and processing from the base blockchain, separating the data and processing in another layer, allowing faster and cheaper transactions without losing the security provided by Ethereum.

Multi-chain framework: Polygon provide a framework to create child chains when necessary, allows fragmenting the load and processing it in different layers, allowing more flexibility for the developers to make chains with specific objectives and behaviors.

Known use cases

Descentraland

It's a decentralized virtual reality game based on the Ethereum blockchain. In this game, the users can create and monetize their content and applications because each player can permanently buy their parcel with MANA (Descentraland's cryptocurrency token).

The users with their parcels have complete control of them and can create 3D scenes, interactive applications, or games. Also, they can exchange collectibles and buy and sell digital elements like wearables for avatars.

In Descentraland are different tokens like an ERC-20 token called MANA and ERC-721 non-fungible tokens called LAND and Estate used to create avatars, wearables, and unique parcels of land.

Descentraland was born in 2017 and created by two Argentine professionals, Ari Meilich and Esteban Ordano. They sold the first parcel for about $20. In April 2021, due to the NFT popularity, Descentraland parcels were sold from $6,000 to more than $100,000.

Link: https://decentraland.org/

descentraland

Continuum

It's a free social network for education where creators and learners earn cryptocurrency and NFTs meanwhile they are learning. They receive a reward for completing short video courses, engaging with the community, and improving the network.

Link: https://continuum.xyz/

continuum

Lenstube

It's an open-source web3 project, similar to the YouTube platform, but it is a decentralized platform in this case. Lenstube allows sharing videos on their platform built on the Polygon chain. Also, reviewers can support the creators in minting their favorite content, joining their channel using membership, and more features that allow creating a connection between content creators and their community.
The creators can monetize their content with cryptocurrencies like NFTs modules and tips. Lenstube stores the videos permanently using the Arweave platform and the Polygon blockchain to keep the video metadata without any fee.

Link: https://lenstube.xyz/

Lenstube

Main Features

As we told before, Polygon is a public blockchain compatible with the EVM (Ethereum Virtual Machine). Polygon allows excellent performance with a cost of over $0.4 per transaction and rates up to 65,000 transactions per second. Also, it implements Proof of Stake to validate the transactions.

The blockchain was known before as MATIC but later was named Polygon. Even though the native token is still named MATIC, its main purpose it's to pay for the staking and allow the mobilization of value between users. Its supply is limited to 10.000.000.000 MATIC tokens.

Study case

Recently, at kommit, we have been exploring interesting Web3 technologies, which led us to examine the Polygon network as part of the blockchains that we are testing and looking for future implementations.

For Polygon we developed an implementation of an intelligent contract creating a token using the standard ERC20 in Solidity. The idea was to create a token and a script that uses the smart contract to manage a kommit compensation program using cryptocurrencies, which allows rewarding the employees who make additional work that benefits the organization, like contributing to open source and sharing knowledge.

For the contract development, we used Solidity as the programming language, we also implemented Openzeppelins ERC20 standard, alchemy as the platform to connect to the Polygon network using its API and node, Hardhat as the development environment, Metamask as the Ethereum wallet manager to interact with the contract, and finally, we used Polygon Mumbai (Polygon testnet) to deploy the contract.

The smart contract

As mentioned above, we implemented the Openzeppelin’s ERC20, to ensure compliance with the fungible token standard and to give more functionalities: the Ownable interface to allow minting tokens, and the ERC20Burnable interface to allow burning tokens.

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
Enter fullscreen mode Exit fullscreen mode
contract KommitToken is ERC20, ERC20Burnable, Ownable {
    mapping(string => address) private allowedUsers; 

        // Define the token's metadata 
    constructor(uint256 _initialSupply) ERC20("kommitToken", "MKT") {
        _mint(_msgSender(), _initialSupply);
    }
}
Enter fullscreen mode Exit fullscreen mode

The main functionalities of the contract are:

  1. We’ve created a list of the allowed users to handle the token, using a mapping where the user name is linked with the wallet number as a key-value object and it’s stored in the smart contract. This mapping is checked on every operation with the token to ensure that only the company members can use it.

    mapping(string => address) private allowedUsers;
    
  2. As said above, we can pay only using the username. To get that, we created a function and modifier to see if a user is in the allowed users:

    // Retrieves if the address is allowed for the token
    function isAllowed(string memory _user) public view returns (bool) {
        return allowedUsers[_user] != address(0);
    }
    
    modifier onlyAllowed(string memory _user, string memory _message) {
        require(allowedUsers[_user] != address(0), _message);
        _;
    }
    

    Then, we used that modifier to implement a function that allows only the owner of the contract to make a transfer of money using a username:

    function transferMoney(string memory destinationUser, uint256 amount)
        public
        onlyAllowed(
            destinationUser,
            "ERROR: Destination address not allowed to receive the token."
        )
        returns (bool)
    {
        address detinationAddress = allowedUsers[destinationUser];
        address owner = _msgSender();
        _transfer(owner, detinationAddress, amount);
        return true;
    }
    
  3. Finally, we created a function that returns the token to the owner account when a user is removed from the allowed users, and this is to guarantee that the tokens stay in the organization:

    function refundEmployeeTokens(string memory _user)
        public
        onlyOwner
        onlyAllowed(_user, "ERROR: User not in the allowed users.")
        returns (bool)
    {
        address userAddress = allowedUsers[_user]; // Get the address of the user
        removeAccess(_user); // Remove the user from the allowed users
        uint256 amount = balanceOf(userAddress); // Know the actual balance
        _burn(userAddress, amount); // Burn the entire token of the user
        _mint(_msgSender(), amount); // Generate the burned tokens
        return true; // Return true when everithing it's ok
    }
    

The script

We also created a JS script that makes use of the contract deployed to make a payment requiring a username and a rewarding type:

// Testing input
data = {
    user: "Test",
    action: "discord",
};

// Select the reward to pay
switch (data.action) {
    case "research":
        value = 1000;
        break;
    case "open-source":
        value = 2000;
        break;
    case "reward":
        value = 3000;
        break;
    case "discord":
        value = 4000;
        break;
} 

// Send money
const result = await contract.transferMoney(data.user, value);
Enter fullscreen mode Exit fullscreen mode

Advantages and disadvantages

Advantages

  • High performance: Lower fees and higher transaction rates per second without losing security, due to Polygon working on Ethereum main net, has the advantage of the Ethereum security. Also, add its security layer using sidechains to validate blocks with their own mechanisms, Polygon increases the transaction rate. It lowers the cost than other Ethereum and other blockchains.
  • Compatibility with EMV: Full compatibility with the Ethereum blockchain, which means that every dApp based on Ethereum works without any problem on Polygon. This is because Polygon uses the EVM (Ethereum Virtual Machine), and this gives us the following benefits:
    • Consistency due to the EVM, because at the moment of adding confirmed Polygon results of a transactions block to the main chain, Ethereum doesn’t have the possibility of ambiguities.
    • High efficiency in transaction processing without losing security because all the Polygon security relies on Ethereum.
    • Simplicity at the moment of developing a Solidity contract, you don’t need to worry about if your project runs on Polygon or Ethereum.
  • Proof of stake: This mechanism is faster than Ethereum’s proof of work and doesn't require higher electrical power to validate transactions and create new blocs.
  • Interoperability: We can move cryptocurrency between different networks. This is because Polygon works with some blockchains, allowing a bridge between chains and increasing its action field.

Disadvantages

  • Dependence: As a layer 2 scaling solution for Ethereum, Polygon depends on the limitations of this blockchain. This can impact the overall scalability of Polygon and compromise the infrastructure because if the Ethereum chain has any technical problem, Polygon will be affected directly.
  • Stability: Because the network is still growing, there may be periods of instability, including issues with network congestion, slower transaction times, and higher fees.
  • Limited Adoption: Although Polygon is gaining popularity, it still needs to be more wholly adopted than other older blockchains like Ethereum, so there may be limited resources. Some dApps are in an early stage of the development process.

Conclusion

Polygon is a promising solution for the current challenges faced by the Ethereum blockchain. With its ability to deploy dApps with better performance and lower transaction costs. Due to its compatibility with Ethereum Virtual Machine (EVM), Polygon is rapidly gaining popularity among developers and businesses. Its architecture uses layer-2 scaling and a multi-chain framework ensuring that it can handle many transactions while maintaining the security provided by Ethereum. With already successful use cases such as Decentraland, Polygon is a valuable addition to the web3 ecosystem and a significant player in decentralizing the future. To develop in Polygon, there are tools like Hardhat and Alchemy, which make the development experience very pleasant and not limiting; they allow us to develop smart contracts, tests, and scripts in the same repository in a simple way. Overall, our experience with Polygon has been positive, and it has a lot of potential to grow.

Top comments (0)