Gas costs are exploding again, ETH2.0 is still too far away and people are now looking at layer 2 solutions. Here's a good overview of existing layer 2 projects: https://github.com/Awesome-Layer-2/awesome-layer-2. Today we will take a closer look at Polygon (previously known as Matic) as a solution for your Dapp.

Why Polygon?

Historical context: The architecture discussion below describes Matic in 2020. Polygon PoS is a sidechain with its own validator consensus; its Ethereum checkpoints do not make it an Ethereum rollup. The Plasma branch and associated tradeoffs describe the earlier design.

With Polygon you essentially get two layer 2 solutions in one, sidechains and Plasma. They have recently launched their mainnet Proof-of-Stake sidechain and you can deploy your smart contracts to it. Or you could use Plasma for less complex functionality, but with higher security.

Polygon has a three-layer architecture:

  1. Staking and Plasma smart contracts on Ethereum
  2. Heimdall (Proof of Stake layer)
  3. Bor (Block producer layer)

Matic Architecture

The system ensures liveliness using PoS checkpoints which are pushed to the Ethereum mainchain. Those checkpoints for Plasma ensure high security of funds using the mainchain as security. For the sidechain they allow synchronisation with the mainchain relying on the PoS security.

Let's see the differences between both approaches.

Sidechains

Sidechains are generally EVM-compatible, separate blockchains with their own consensus models. Polygon is using a Proof-of-Stake mechanism using their own MATIC token on the Ethereum mainnet. Sidechains are designed to easily allow transfer of value and arbitrary state with the Ethereum chain.

+ allow you to run fully functioning smart contracts
+ arbitrary data exchange with mainchain
- security relies on small set of nodes in the sidechain
- not suitable for high-value applications


Token update: POL replaced MATIC as the native gas and staking token of Polygon PoS in September 2024. The MATIC references above describe the original article.

side chain vs plasma

Plasma

Plasma uses a combination of smart contracts and cryptographic verification. Together, these enable fast and cheap transactions by offloading these transactions from the main Ethereum blockchain into a plasma chain. In contrast to regular sidechains, you cannot run any smart contract in here, but they allow users to retrieve funds even if the plasma chain operator is malicious. For more details have a look here.

+ rely on mainchain security
+ require much less trust in chain operator
- no arbitrary data exchange
- only allow simple transfers, e.g., ERC-20


What makes Polygon different

Polygon's hybrid approach allows you to use Plasma and sidechains in one system. For general smart contracts, you can deploy and use it as a general sidechain, while for simple, high-value transfers you can make use of Plasma. Polygon also uses a public checkpointing layer which publishes checkpoints after periodic intervals allowing the side chains to operate at high speeds while publishing the checkpoints in batches.

How to deploy to Polygon

Deploying to the Polygon sidechain is very simple. If you're using Truffle, simply add the Polygon network configuration like this:

javascript
// Historical Mumbai/Truffle example: Mumbai is retired.
// Do not use this endpoint for a new deployment; see the linked Amoy network reference.
matic: {
      provider: () => new HDWalletProvider(mnemonic, `https://rpc-mumbai.matic.today`),
      network_id: 80001,
      confirmations: 2,
      timeoutBlocks: 200,
      skipDryRun: true
    },
}

Mumbai and its Goerli parent network are retired. For new test deployments, use Polygon Amoy (chain ID 80002, POL for gas), anchored to Ethereum Sepolia. The original Mumbai configuration is retained above only to document the old Truffle setup. Rebuild and test the complete toolchain before using a current network.

Onboarding users

MetaMask

Use the current Polygon network details when adding a network to your wallet: Polygon PoS mainnet is chain ID 137, and Amoy testnet is 80002. Both use POL for gas. The older Matic/Mumbai screenshots and labels in this article are historical.

Matic MetaMask

Using the MaticJS library

Historical context: The following Matic.js calls document the older MaticPOSClient API and Goerli/Mumbai bridge deployment. Those networks are retired. For a new integration, start with the current PoS bridge documentation; changing the RPC URLs alone does not update the SDK API or contract addresses.

For the best user experience, you may want to integrate moving funds from sidechain <-> mainchain properly in your Dapp. You can use the MaticJs library for that. For the mainnet, just make sure to use the correct RPC urls.

Terminal / configuration
$ npm install @maticnetwork/maticjs --save

Then you can initialize the client.

javascript
import Matic from "@maticnetwork/maticjs";

const maticPOSClient = new Matic.MaticPOSClient({
  parentProvider: new HDWalletProvider(
    PRIVATE_KEY,
    "https://goerli.infura.io/v3/75aa7935112647bc8cc49d20beafa189"
  ),
  maticProvider: new HDWalletProvider(
    PRIVATE_KEY,
    "https://rpc-mumbai.matic.today"
  ),
  posRootChainManager: "0xBbD7cBFA79faee899Eaf900F13C9065bF03B1A74",
});

A PoS withdrawal burns the token on Polygon, then waits for the relevant checkpoint on Ethereum before an exit proof releases the Ethereum-side asset. This is not a fixed seven-day wait: that description belongs to the historical Plasma exit process. Check the current PoS withdrawal flow and its checkpoint status when building a bridge integration.

javascript
const txConfig = { from, gasPrice: "10000000000" };

// Deposit
await maticPOSClient.depositEtherForUser(from, amount, txConfig);

// Burn
const burnHash = await maticPOSClient.burnERC20(
  "0x4DfAe612aaCB5b448C12A591cD0879bFa2e51d62", // WETH address
  amount,
  txConfig
);

// Retrieve in main after checkpoint
await maticPOSClient.exitERC20(burnHash);

For current bridging entry points and requirements, follow the maintained Polygon PoS bridge documentation. The earlier Matic wallet and Mumbai links in this tutorial describe the 2020 interface.

The Plasma way

Using the MaticJs library, the deposit and withdraw via Plasma are very similar. Just keep in mind that if you want to ensure the Plasma securities, you cannot run any smart contract. You can however allow simple transfers, for example like this:

javascript
await maticPlasma.transferERC20Tokens(token, recipient, amount, { from })

See here on how to initialize the maticPlasma client.

Layer-2 solutions

That's it, it's really quite simple from a developer's perspective. But really make sure you understand the trade-offs between layer-1, sidechains and plasma before making a decision. That's the hard part.

 If you feel like adding Polygon, you might also want to look at the 50,000$ bounty program they currently have ongoing: https://blog.matic.network/launching-of-large-scale-developer-initiatives-for-mainnet-adoption/.


Have you used Polygon before? Maybe even on Mainnet? Or what other layer-2 solutions did you look at or would be interested to hear about next? Please let me know in the comments.