ChainScore Labs

Building on Polygon

A developer's guide to Ethereum scaling solutions

Explore Polygon's suite of solutions for building scalable, fast, and low-cost dApps on Ethereum.

What is Polygon?

A framework for building and connecting Ethereum-compatible blockchain networks

Ethereum Scaling

Polygon addresses Ethereum's scalability challenges by providing a suite of scaling solutions, including Layer 2 chains and sidechains. These solutions enable faster and cheaper transactions while maintaining compatibility with the Ethereum ecosystem.

🚀

Why Build on Polygon?

Developers choose Polygon for its: *Low transaction fees.* *Fast transaction speeds.* *EVM compatibility.* *Growing ecosystem of dApps and tools.* *Strong community support.* Polygon allows developers to build scalable dApps without sacrificing the security and decentralization of Ethereum.

🏗️

Architecture & Components

Polygon offers various scaling solutions: *Polygon PoS Chain:* A Proof-of-Stake sidechain. *Polygon Plasma Chains:* Using Plasma framework for off-chain transactions. *Polygon zkEVM:* a zero knowledge-based rollup. *Polygon Miden:* STARK-based, zero-knowledge rollup solution *Polygon Avail:* A general-purpose, scalable, and data-availability-focused blockchain.

🔷

MATIC Token

The MATIC token is the native cryptocurrency of the Polygon network. It's used for: *Paying transaction fees (gas).* *Participating in network governance.* *Staking (in PoS chain).

Understanding Polygon's Architecture

The technical layers and components of the Polygon ecosystem

The Polygon SDK is a modular and flexible framework that allows developers to build and connect secured chains (Layer 2) and sovereign chains (sidechains). It provides tools for creating custom blockchain networks tailored to specific needs.

Polygon's Scaling Solutions

Exploring the different chains and frameworks offered by Polygon

🔷

Polygon PoS Chain

A Proof-of-Stake sidechain that offers fast and low-cost transactions. It's EVM-compatible, making it easy to deploy existing Ethereum smart contracts. The PoS chain uses a set of validators to secure the network, and users can stake MATIC tokens to become validators. The most widely adopted Polygon solution.

🔗

Polygon Plasma Chains

Use the Plasma framework to create child chains that process transactions off-chain. Plasma chains periodically submit checkpoints (Merkle roots) to the Ethereum mainnet, providing a higher level of security than pure sidechains. Plasma is particularly useful for applications that require high transaction throughput.

🔐

Polygon zkEVM

A zero-knowledge (ZK) rollup solution that provides EVM compatibility. zk-Rollups bundle multiple transactions into a single proof (a ZK-SNARK) that is submitted to the Ethereum mainnet. This significantly increases throughput and reduces transaction costs while maintaining the security of Ethereum. zkEVM allows developers to deploy existing Solidity smart contracts without modification

🔒

Polygon Miden

Polygon Miden is a STARK-based, zero-knowledge rollup solution, focussed on its Miden VM instead of EVM equivalence.

☁️

Polygon Avail

Polygon Avail A general-purpose, scalable, and data-availability-focused blockchain. It's designed to be a highly efficient and secure data availability layer for other blockchains, including Layer 2 solutions and standalone chains. Avail allows blockchain networks to scale without sacrificing data integrity.

🛠️

Polygon Edge

Polygon Edge is a library for building custom blockchain networks compatible with the Polygon ecosystem. (Note: This is different than the above-mentioned 'Polygon SDK').

Development Process

General steps for building on Polygon

Choose a Polygon Solution

The first stage will be to decide, depending on the use case, which Polygon solution you want to develop for. You can decide between Polygon PoS, zkEVM, or other options, each having its advantages and disadvantages.

⚙️

Set up Development Environment

Install necessary tools, including Node.js, a code editor (e.g., VS Code), and Ethereum development frameworks like Truffle or Hardhat. Configure your environment to connect to the chosen Polygon network (e.g., Mumbai testnet for PoS).

📜

Write Smart Contracts

Write your smart contracts, typically in Solidity. You can often reuse existing Ethereum smart contracts with minimal or no changes, thanks to Polygon's EVM compatibility.

🚀

Compile and Deploy Smart Contracts

Compile your Solidity code using tools like Truffle, Hardhat, or Remix. Deploy the compiled contracts to the chosen Polygon network (testnet first, then mainnet).

💻

Develop Client Application

Build the front-end and back-end logic of your dApp, using libraries like Web3.js or Ethers.js to interact with the smart contracts on Polygon.

🧪

Test Thoroughly

Test your dApp extensively, including unit tests for smart contracts, integration tests, and end-to-end tests. Use testnets (e.g., Mumbai for PoS, Goerli for bridging) to simulate real-world conditions.

🌐

Deploy to Mainnet

Once you're confident in your application's functionality and security, deploy it to the Polygon mainnet. Monitor your application's performance and address any issues that arise.

🌉

Consider Bridging

If you need to move assets between Ethereum and Polygon, learn how to use the Polygon Bridge. This allows users to transfer tokens and other assets between the two networks

Smart Contract Development on Polygon

Using Solidity and leveraging EVM compatibility

Polygon's EVM compatibility means you can write smart contracts in Solidity, the most popular language for Ethereum development. You can often reuse existing Ethereum smart contracts on Polygon with minimal or no changes. This makes it easy to migrate existing dApps to Polygon or build new ones that leverage the existing Ethereum ecosystem.

Development Tools for Polygon

Essential tools and resources for building on Polygon

🛠️

Truffle Suite

Truffle is a popular development framework that provides tools for compiling, testing, and deploying smart contracts. It works seamlessly with Polygon, allowing you to manage your development workflow efficiently.

🏗️

Hardhat

Hardhat is another widely used development environment for Ethereum and Polygon. It offers similar features to Truffle, including compiling, testing, and deploying contracts. Hardhat is known for its flexibility and extensibility.

🌐

Remix IDE

Remix is a web-based IDE for Solidity development. It's a great tool for beginners and for quickly prototyping smart contracts. Remix can connect to Polygon networks using MetaMask or other wallet providers.

🦊

MetaMask

MetaMask is a popular browser extension that acts as a Web3 provider and allows users to interact with dApps and manage their accounts. You can configure MetaMask to connect to Polygon networks (PoS, zkEVM, etc.). MetaMask is essential for testing and interacting with your dApps.

📚

Web3.js / Ethers.js

These are popular JavaScript libraries for interacting with Ethereum and Polygon. They provide convenient abstractions for sending transactions, interacting with smart contracts, and managing accounts. Web3.js and Ethers.js are essential for building client-side applications that connect to Polygon.

🔎

Polygonscan

Polygonscan is a block explorer for the Polygon PoS Chain. It allows you to view transactions, blocks, accounts, and smart contracts on the network. Polygonscan is a valuable tool for debugging and understanding the state of the blockchain. Similar explorers exist for other Polygon chains.

🚰

Polygon Faucet

The Polygon Faucet provides test MATIC tokens for use on the testnets (e.g., Mumbai). You can request tokens to pay for gas fees when testing your applications. Faucets are essential for development and testing on public test networks.

🔌

Alchemy / Infura

These are node-as-a-service providers. They offer API endpoints to interact with Polygon without having to run your own node, including archive nodes capabilities (useful to get historical states from smart contracts).

Testing and Deployment

Validating and launching your Polygon dApps

Unit tests verify individual functions within your smart contracts. You should write unit tests and get high code coverage. Development frameworks provides such capabilities.

Using Polygon Bridges

Transferring assets between Ethereum and Polygon

🌉

What are Bridges?

Bridges are mechanisms that allow users to transfer assets (tokens, NFTs) between different blockchains, such as Ethereum and Polygon. Polygon provides official bridges for moving assets between the Ethereum mainnet and Polygon chains.

🔷

Polygon PoS Bridge

This bridge allows transferring assets between Ethereum and the Polygon PoS chain. It uses a lock-and-mint mechanism for ERC-20 tokens and a burn-and-mint mechanism for ERC-721 and ERC-1155 tokens. It's a trusted bridge, meaning that a set of trusted validators manage the bridge.

🔗

Polygon Plasma Bridge

The Plasma Bridge uses the Plasma framework for transferring assets. It provides a higher level of security than the PoS Bridge because it relies on the security of the Ethereum mainnet. However, withdrawals from Plasma can take longer (typically a 7-day challenge period).

➡️

Depositing Assets

To deposit assets from Ethereum to Polygon, you interact with the bridge contract on the Ethereum mainnet. You approve the bridge contract to transfer your tokens, and then call a deposit function. The bridge contract locks the tokens on Ethereum and mints corresponding tokens on Polygon.

⬅️

Withdrawing Assets

To withdraw assets from Polygon back to Ethereum, you initiate a withdrawal transaction on Polygon. The bridge contract burns the tokens on Polygon. For the PoS bridge, tokens will be available on Ethereum after a checkpoint is submitted. For the Plasma bridge, you need to wait for the challenge period to expire before claiming your tokens on Ethereum.

💸

Bridge Fees

Using bridges typically involves fees. There's usually a small fee on Polygon for initiating the withdrawal, and you'll need to pay gas fees on Ethereum for claiming your assets.

🛡️

Bridge Security

Understand the security model of the bridge you're using. The PoS Bridge relies on the security of the Polygon PoS validators, while the Plasma Bridge relies more on the security of Ethereum mainnet. Always use official bridges and verify contract addresses.

Gas Fees on Polygon

Understanding and optimizing transaction costs

Gas on Polygon

Like Ethereum, Polygon uses gas to measure the computational resources used to execute transactions. Every operation in a smart contract consumes a certain amount of gas. However, gas fees on Polygon are significantly lower than on the Ethereum mainnet.

🔷

MATIC for Gas

On the Polygon PoS Chain, and typically in other Polygon chains, you pay for gas using the MATIC token. You'll need to have MATIC in your wallet to submit transactions.

📈

Gas Price Fluctuation

The gas price on Polygon can fluctuate based on network demand. During periods of high network activity, the gas price may increase. You can use tools like Polygonscan to track the current gas price.

⚙️

Gas Optimization

Even though gas fees are low, you still should optimize contracts: *Avoid unnecessary storage operations.* *Use efficient data structures.* *Batch operations when possible.* *Consider using Layer 2 solutions on top of Polygon for further gas savings.*

Gas Stations

Polygon has gas stations (or gas-free meta transaction services) that allows relayers to pay gas on behalf of the users. This is useful to improve user experience and have users interact with the network without necessarily owning MATIC.

EIP-1559

Polygon PoS implements EIP-1559, which introduces a base fee and a priority fee (tip) for transactions. The base fee is burned, while the priority fee goes to the validators. This mechanism helps to stabilize gas prices and make them more predictable.

Security Best Practices

Protecting your Polygon dApps and smart contracts

📜

Smart Contract Security

Follow secure coding practices when writing Solidity smart contracts. *Validate all inputs.* *Handle errors properly.* *Avoid common vulnerabilities (e.g., reentrancy, integer overflows).* *Use established security patterns.* *Thoroughly test your contracts.* *Consider formal verification and security audits.*

🔑

Key Management

Securely manage your private keys. Never store private keys in plain text or commit them to version control. Use hardware wallets or key management services to protect your keys. Consider using multi-signature wallets for deploying and managing high-value contracts.

🌉

Bridge Security

If you're using bridges, understand their security model. Use official bridges and verify contract addresses. Be aware of the risks associated with bridging assets between different chains.

👀

Monitor Your Applications

Regularly monitor your dApps and smart contracts for any unusual activity. Use monitoring tools to track key metrics and set up alerts for potential issues. Review logs and transaction data to identify any suspicious behavior.

⬆️

Stay Updated

Keep your development tools, libraries, and dependencies updated to the latest versions. New releases often include security patches and bug fixes. Subscribe to security announcements for Polygon and the tools you're using.

🔒

Access Control

Implement proper access control in your dApps and smart contracts. Restrict access to sensitive functions and data to authorized users or accounts. Use role-based access control or other access control mechanisms.

Polygon Use Cases

Exploring applications and scenarios where Polygon excels

💱

Decentralized Finance (DeFi)

Polygon's low fees and fast transactions make it a popular platform for DeFi applications, such as: *Decentralized exchanges (DEXs).* *Lending and borrowing platforms.* *Yield farming protocols.* *Stablecoins.* Many popular Ethereum DeFi projects have also deployed on Polygon.

🖼️

Non-Fungible Tokens (NFTs)

Polygon is a popular platform for minting, trading, and using NFTs, including: *Digital art.* *Collectibles.* *Gaming items.* *Virtual land.* The low gas fees on Polygon make it more affordable to create and trade NFTs compared to Ethereum mainnet.

🎮

Gaming

Polygon's scalability and low fees are well-suited for blockchain-based games. Game developers can use Polygon to create in-game economies, manage virtual assets (NFTs), and provide a better user experience for players. Many play-to-earn games utilize Polygon

📱

Decentralized Applications (dApps)

Polygon is a general-purpose platform for building a wide range of dApps that require high throughput and low transaction costs. dApps that might have been too expensive or slow to operate reliably on Ethereum, become feasible on polygon.

📦

Supply Chain Management

Polygon is used in some supply chain settings to record transactions, track goods, and enhance visibility across the various touchpoints.

📢

Web3 Social Networks

Some decentralized social networks have chosen to use Polygon for better scalability.

Future of Polygon

Upcoming developments and roadmap for the ecosystem

Polygon is continuously evolving and expanding its suite of scaling solutions. Ongoing development includes: *Improvements to existing chains (PoS, Plasma, zkEVM).* *Development of new scaling solutions (e.g., Miden, Avail).* *Enhancements to the Polygon SDK.* *Focus on interoperability between different Polygon chains and with Ethereum.*

Frequently Asked Questions

Common questions about developing on Polygon

Ready to Build on Polygon?

Start building scalable dApps on Ethereum with Polygon