Eda
Eda's blog

Eda's blog

Building with Polygon

Building with Polygon

Eda's photo
Eda
ยทMar 20, 2022ยท

9 min read

With increasing demand Ethereum has become expensive and slow. This created an urgent need for scaling solutions. That's where Polygon comes in.

Simply put, Polygon is an Ethereum scaling platform. Polygon has seen exponential growth over the past year. It's easy to use and doesn't sacrifice the security and decentralization provided by Ethereum. It uses Ethereum's technology, so developers can jump onto Polygon and mainly use the same code and tools.

I'm very impressed by the Polygon Ecosystem. I've been using and building with Polygon; it's time to put my findings into this post.

Here's what we'll be covering:

  • Why do we need scaling solutions on Ethereum
  • What is Polygon?
  • How to create a smart contract on Polygon

Introduction

Why do we need to scale?

Firstly, blockchain is a shared and immutable ledger. On the blockchain, the information is distributed among multiple computers which work together to provide the underlying infrastructure.

Ethereum is a smart contract platform that is built using blockchain technology. Ethereum has become increasingly popular.

First, Decentralized Finance(DeFi) got many people excited about crypto and onboarded the adventurous finance people onto the ecosystem. I missed that stage; it was called "DeFi summer."

Then there was the NFT wave. It was a period that onboarded and is continuing to onboard many to the crypto world. These are also known as images worth millions.

Screen Shot 2022-03-20 at 15.38.56.png

With such demand, scalability becomes a huge issue. Ethereum becomes slow and expensive. The Blockchain can only handle a certain amount of transactions at a specific time, so when many people submit a transaction simultaneously, this creates congestion.

We can summarize the issues with Ethereum as below:

  1. Low throughput
  2. Expensive, not user friendly.
  3. Limited options for specialized apps. It's a general-purpose blockchain; thus, it's not optimized for specific use cases.

The Ethereum team is working on methods to scale the Blockchain. One of the main scaling updates is coming with The Merge. However, Ethereum will still need a set of solutions running on top even with the updates. The vision of Ethereum is to create a solution on a global scale; this cannot be achieved by a single solution, as all systems will have their limitations. Multiple solutions can live in parallel to provide the best experience for the users.

Screen Shot 2022-03-20 at 15.23.18.png

This is where scaling solutions come into play. Scaling solutions are solutions built on top of Ethereum (also called Layer 1) to solve the pain points. Here's a great animation video on scaling solutions.

Polygon started with a vision to bring mass adoption to Ethereum. It wanted to make it easy for developers to build and convenient for users to use. It has quickly become one of the most popular scaling solutions built on Ethereum.

What's Polygon?

Polygon is a full suite of scaling solutions for Ethereum. It makes it easy for developers to build and users to use. Here is the description from the Polygon website:

Polygon is a decentralized Ethereum scaling platform that enables developers to build scalable user-friendly dApps with low transaction fees without ever sacrificing on security.

Screen Shot 2022-03-20 at 15.41.36.png

Polygon is both more efficient and affordable than directly using Ethereum. Two critical needs for adoption. It leverages the Ethereum technology and has solutions that talk to the Ethereum Network.

Three developers founded Polygon in 2017; the project was initially called Matic. Even though it has rebranded to Polygon, the token is still called MATIC. Matic is the cryptocurrency used to pay for transactions on Polygon. Here's a great video on Polygon with animations.

Today there are over +7000 Applications that use Polygon, from popular Decentralized Finance apps to Gaming Applications. OpenSea, Aave and Metamask are some examples.

Screen Shot 2022-03-20 at 15.45.36.png

So, what really is it?

It's a set of platforms and protocols to scale the public blockchains. It set out to offer a solution for Ethereum's scalability issues.

Polygon utilizes the main Ethereum code. So, developers can move their projects onto Polygon with just a few changes.

Screen Shot 2022-03-19 at 07.43.50.png

Polygon is not 1 or 2 scaling solution(s), but instead, Polygon is creating an entire ecosystem of Ethereum scaling solutions. This is because different applications want different things. With Polygon, developers can choose the parts that fit the need of their application; since every choice is a trade-off. This way, applications can prioritize the features they need the most. For example, if you're developing a finance application, you'd want to optimize for security. But on the other hand, speed may be one of the features to prioritize for a blockchain-based game.

What are the Scaling Solutions?

Polygon has different solutions; let's briefly talk about the most popular Polygon offering, that is the Polygon Proof of Stake(PoS) chain.

Screen Shot 2022-03-20 at 16.39.34.png Source: Polygonscan

Polygon PoS is a scaling solution that uses side-chains for transaction processing and Plasma bridging framework for additional security. It's a solution that compliments Ethereum and achieves high speed and low cost without sacrificing the security from Ethereum itself. Developers can deploy their contract to Polygon, and users can use the Polygon PoS chain instead of directly interacting with Ethereum.

The PoS chain is not the only offering; there are other solutions on the Polygon Ecosystem.

  • Polygon Hermez: solution mainly used for Payments that uses "zero-knowledge proof" (ZK) technology
  • Polygon Edge: framework for building Ethereum-compatible blockchain networks
  • Polygon Nightfall: privacy-focused Ethereum scaling solution for enterprises

Check out more on the Polygon website Screen Shot 2022-03-19 at 07.37.07.png


Build a Smart Contract on Polygon

Good news: It's just like building on Ethereum! There are very few changes needed. So if you're an Ethereum builder, you are already a Polygon builder. ๐Ÿ˜Ž

In the next part, we'll be deploying a smart contract to the Polygon PoS chain. It's the same steps & code from my "Intro to dApp's: Create your dApp backend w/Solidity" post.

Project Tools

  • Metamask Wallet: Crypto wallet.
  • Alchemy: Ethereum developer platform. We will be using the Alchemy API to interact with Alchemy's Polygon PoS infrastructure.
  • Hardhat: Ethereum development environment. It comes as an npm package.

Prerequisites

  1. Choose your IDE and get the "solidity extension." I am using VS Code.
  2. Create an account on Metamask and switch to the Mumbai Test Network.
  3. Get your Alchemy API key. You will need to create an account on Alchemy. When logged in, create an App on the Polygon Test Network(which is Polygon Mumbai). Your App settings should look like the following: Screen Shot 2022-03-20 at 14.56.43.png Once you've created the App, head over to the app details and get your HTTP URL by clicking "view key" on the top right.
  4. Install node.js.
  5. Get some Mumbai Test Network Tokens. You can get some here.

Building the Smart Contract

  1. Create a project folder and head over to its directory. Install hardhat.

    mkdir eda-sample-smart-contract
    cd eda-sample-smart-contract
    npm init -y
    npm install --save-dev hardhat
    
  2. In your project directory create a hardhat project by running npx hardhat.

    $ npx hardhat
    
  3. The setup wizard will show up. Click enter throughout and keep the default options. Hardhat will create a sample project and install the dependencies for you.

  4. Open the project from your code editor. Under the 'Contracts' folder, you should see the default Greeter.sol file. ".Sol" is the file extension for the solidity language, and it's our sample smart contract. I've added the smart contract below with some comments to explain what's going on.

    //SPDX-License-Identifier: Unlicense
    pragma solidity ^0.8.4; // solidity version for the compiler, must be the same as in the settings file
    
    import "hardhat/console.sol";
    
    // create the smart contract 
    contract Greeter {
    
      // create a variable called greeting that will be stored in the contract 
      string private greeting;
    
      constructor(string memory _greeting) {
          console.log("Deploying a Greeter with greeting:", _greeting); // print to the console 
          greeting = _greeting;
      }
    
      // create a function to give the greeting message 
      // public: functions are public by default, anyone reading from the contract can call it
      // view: the function will not modify the state, it will only return a value
      // returns: the function declaration tells the type of the return value, here the return type is a string 
      function greet() public view returns (string memory) {
          return greeting; 
      }
    
      // create a function to change the greeting message 
      function setGreeting(string memory _greeting) public {
          console.log("Changing greeting from '%s' to '%s'", greeting, _greeting); // print to the console
          greeting = _greeting;
      }
    }
    
  5. Here is where the code starts to change from the Ethereum Project. First, you will need to modify the networks in the hardhat.config.js file to be on Polygon Test Network, the one we will be using is the: Mumbai Test Network. Next, get your Alchemy HTTP URL; it's needed to talk to the Mumbai Test Network from the smart contract. Add it to the hardhat.config.js (see code snippet under step-6).

  6. Add your metamask private key to hardhat.config.js. You can get you private key by clicking Account Details --> Export Private Key from your Metamask extension.

    require('@nomiclabs/hardhat-waffle');
    
    module.exports = {
    solidity: '0.8.4', // make sure the version matches the one in smart contract file 
    networks: {
      mumbai: { // change the network to the Polygon test network 
        url: ' ', // Mumbai testnet url 
        accounts: [' '], // metamask privte key- NEVER SHARE THIS!! It has access to all your accounts
      },
    },
    };
    
  7. We need to take our contract from our local machine and put it onto the Mumbai Test Network. For this, we will use the sample-script.js file under the 'Scripts' folder. (It's usually called deploy.js; but let's leave it as is for this sample project.) Run the command below to deploy the contract to the Mumbai Test Network.

    npx hardhat run scripts/sample-script.js --network Mumbai
    

๐ŸŽ‰ Once you see the details of your smart contract on the terminal, it means that your smart contract is deployed onto the Mumbai Test Network!

The smart contract address is printed on the terminal. Head over to Etherscan for Polygon TestNet. Search for your contract address, and you'll see the details.

What's next

Check out the Polygon developer page for developer resources to continue building. You can find full suite of resources from the Polygon Team and also curated community resources.

Worth mentioning that Polygon has a Developer Support Program to help developers in the ecosystem. Benefits include getting early access to grants and technical guidance. I haven't been part of the program but could be interesting to check out!

Screen Shot 2022-03-20 at 15.06.53.png


The Polygon has team has always focused on adoption, and they've become one of the most popular platforms for developers. I'm excited for what Polygon is doing and look forward to following closely!

If you have any questions or comments, drop them below, or reach out to me on Twitter! ๐Ÿ‘ฉโ€๐Ÿ’ป

ย 
Share this