EVM L2 Blockchain & SideChains

There has been a huge debate regarding the issue of scaling the Ethereum blockchain. Ethereum has become one of the most popular blockchains for awhile due to its ability to employ Turing complete smart-contracts. Though, after it gained popularity there has been a significant network congestion. The beginning of this decade led to the popularity of DEFI causing even more congestion on the network as a result high gas fees.

Difference Between Ethereum L2 and Sidechains.

L1 Blockchain refers to the main blockchain layer such as Ethereum.Layer1 scaling solutions are implemented directly on the main blockchain thus deriving the name on-chain solutions. Some examples of the on-chain scaling solutions involve Consensus Protocol Switch and Sharding.

L2 Blockchain these are add-on solutions built on the base layer. Thus deriving the name off-chain scaling solution, since they intend to take away workload from the blockchain while utilizing its security.

Sidechains are Ethereum compatible independent blockchains with their own consensus model. Sidechains achieve interoperability with Ethereum by the the use of the same EVM. Since they are independent from the main-chain sidechains are responsible for their own security. If a sidechain’s security is compromised it will pose no impact to the main-chain. e.g Gnosis chain and Polygon.

Why Would A Developer Choose L2 or Sidechain

The main blockchain (Layer 1) is usually slow. layer2 and sidechains came up as a solution to speed up the blockchain without having to compromise its security and decentralization thus an improved user experience and reduce network congestion.

Transaction fees are cheaper on L2 making Ethereum more inclusive and accessible by everyone. This is made possible through the employment of rollups which work by rolling a number of transactions into batches.

How To Deploy A Smart Contract On Polygon

At the moment you must be curious on how you can get the perks of building on a sidechain or a layer2 scaling solution. Alchemy provides a platform where developers can build DApps and leverage the Ethereum scaling solutions .

In this step by step guide we will demonstrate on how you can deploy a smart contract on Polygon with Alchemy. If you are not familiar with full stack DApp development check out this Tutorial .

So you already have your smart contract developed now you are thinking of how to deploy on polygon here is what you will need to do:

  1. Connect with Polygon

Go to Alchemy and login. Once you are logged In on the right side of the dashboard you will see a “Create App” button. Click on it to create a new app. Fill in details, name of your app ,description of the app then choose the chain you want deploy your app on, we choose polygon and for the network we choose polygon Mumbai from the dropdown.

alch.png

once you are done creating the app you can access it on the dashboard. You will need API keys for your smart contract to interact with Alchemy(node) and Polygon click on ‘View Key’ to access them. Your keys are private and shouldn’t be accessed by unauthorized parties.

key.png

  1. Setup Polygon on Metamask

    Ensure that you have the Metamask extension installed go to https://metamask.io/download/ .Once you launch your metamask you need to set up the other networks since the network available at first is the Ethereum main network. On the right hand side of metamask click the accounts icon ,scroll down to settings click on it to change the network setting

    Settings→Network→Add new Network→Save

    while adding the network Use a network name that will reference the chain and node you are using. The New RPC URL is the HTTP from alchemy “Key” .The chain ID for polygon Mumbai (Polygon’s testnet) is 80001 and the Currency symbol is MATIC (capital letters).Save and Switch from Ethereum Mainnet to Alchemy Polygon Mumbai.

nw2.png

  1. Get Mumbai test Faucet

    For every transaction to take place on Ethereum you have to pay for gas fees .Test Faucet are free Eth for testing purposes. Go to https://mumbaifaucet.com/ enter your wallet address from metamask to get some MATIC. You can confirm your Mumbai MATIC balance on metamask.

mummetamask.png

  1. Update HardHat.config.js

    Update your hardhart.config.js file with the highlighted lines of code to configure the network that your smart contract will be deployed to Polygon Mumbai:

     require("@nomiclabs/hardhat-waffle");
    
     // This is a sample Hardhat task. To learn how to create your own go to
     // https://hardhat.org/guides/create-task.html
     task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
       const accounts = await hre.ethers.getSigners();
    
       for (const account of accounts) {
         console.log(account.address);
       }
     });
    
     // You need to export an object to set up your config
     // Go to https://hardhat.org/config/ to learn more
    
     /**
      * @type import('hardhat/config').HardhatUserConfig
      */
     module.exports = {
       solidity: "0.8.4",
       //add the following lines to configure the network to Mumbai
       networks: {
         mumbai: {
           url: 'Add your HTTP key from Alchemy',
           accounts: [`Your metamask Private Key`]
         }
       }
     };
    

    The URL comes from your alchemy ‘Key’ from your app dashboard. On accounts paste your metamask wallet private key. To access your wallet private on Metamask Click on the three dots on the right to access your account details then export your private key, The address below the QR code that is your public key, However I highly encourage you to save these sensitive details in a separate file which you wont commit to GitHub, Your accounts might get hacked.

priv2.png

  1. Compile and Deploy.

    We are done with the network configuration its now time to compile and deploy our smart contract to Polygon Mumbai network. On your project terminal run the following command to compile your smart contract

     npx hardhat compile
    

    If it compiles successfully this will be the output on the terminal

     Compiled 1 Solidity file successfully
    

    To deploy our contract to Polygon’s Mumbai testnet run the following command:

     npx hardhat run scripts/sample-scripts.js --network mumbai
    

    If the smart contract deploys successfully this will be the output on the terminal that the smart contract has been deployed a certain address.

     "Your contract has been deployed to" : 0xB3636.....
    

    To get more details about your deployed contract copy the address that will be displayed on your terminal and paste it into https://mumbai.polygonscan.com/ .