April 12, 2022

How to Deploy a Smart Contract on MATIC/Polygon

Overview

Ethereum is a very lovely blockchain to work with, but recently, heavy traffic and many people building on it have resulted in the chain being a bit congested. Layer 2 solutions solve this issue by extending Ethereum's scalability. Polygon (FKA MATIC) is one such solution. It is an Ethereum sidechain with lower gas fees along with the security of Ethereum. Today in this guide, we will learn more about the Matic/Polygon chain and how to deploy a smart contract on Matic/Polygon.

Prerequisites
  • A Polygon node.
  • Web browser.
  • MetaMask installed in the browser.
  • Love of polygon.

Why Polygon?

With the rapid adoption of Ethereum, the problem of scalability arose, where gas prices started getting higher as more and more people wanted to get that precious Ethereum block space. Polygon, which was launched as MATIC network, was created to provide a scalable solution to Ethereum. It is a Proof-of-stake blockchain, just like other PoS blockchains, with the exception that transactions are collected and then settled on the Ethereum blockchain. This gives Polygon the interoperability, liquidity, and security of Ethereum.

Polygon's main chain is a Proof-of-Stake (PoS) sidechain. Where MATIC, the native token of Polygon network, is used as staking token to validate transactions and vote on network upgrades. MATIC is also used to pay for the gas fees on Polygon.

Now that we know what Polygon is let us deploy a contract on this fantastic network.

Booting your Polygon node

We'll deploy our contract on Polygon Mumbai testnet. We can simply get a free trial node from QuickNode, which is much better than investing time looking at different custom configs to launch your own node.

Screenshot of Quicknode Mumbai Endpoint


Copy the HTTP URL, which will be needed in the next step.

Setting up MetaMask with Polygon node.

After creating your QuickNode Polygon Node, we’ll have to set it up in the MetaMask wallet as a custom RPC

Getting test MATIC

We will need some test MATIC to pay for gas fees to deploy and interact with the smart contract.

Copy your address from MetaMask, paste it into the address field of Polygon faucet, and click on submit. The faucet will send you 0.1 test MATIC.

Writing the contract

Time to write our smart contract, go to REMIX Ethereum and make a new solidity file, for example: mumbai01.sol

Paste the following code into your new Solidity script:

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.0 <0.7.0;
contract SimpleStorage {
    uint storedData;
    function set(uint x) public {
        storedData = x;
    }
    function get() public view returns (uint) {
        return storedData;
    }
}

Explanation of the code above.

Line 1: Specifying SPDX license type, which is an addition after Solidity ^0.6.8. 

Whenever the source code of a smart contract is made available to the public, these licenses can help resolve/avoid copyright issues. If you do not wish to specify any license type, you can use a special license UNLICENSED or simply skip the whole comment (it won’t result in an error, just a warning).
 
Line 2: On the first line, we are declaring which Solidity compiler we want to use. For instance, we are targeting any version between ≥ 0.4.0 and <0.7.0.

Line 3: We are declaring our contract here and naming it as SimpleStorage.

Line 4: Declaring a uint (Unsigned Integer) variable named storedData, this variable will be used to store data.

Line 5-7: Next, we will add a set function, using which we will change the value of our variable storeData. The set function accepts a parameter x whose value we are placing into storeData. In addition, the function is marked as public which means that the function can be called outside the scope of this function and by other contracts.

Line 8-10: We will add a get function to retrieve the value of storeData variable. This function is marked as view, which tells the Solidity compiler that this is a read-only function.

Other than that, the get function also has returns (uint), which means that the function will return a uint value.

Deploying the contract

Now click on the Solidity logo from the left menu and click on compile. After successful compilation, a green tick will appear on the Solidity logo.


Now, click on the option from the left menu to deploy the complied contract and select Injected Web3 as the environment. Below the environment, the name and chainid of our network will appear. In this case, 'custom 80001' for Matic testnet. Make sure you have the correct contract name selected under the contract option. Once you have checked everything, click on "Deploy" and accept the transaction from the MetaMask pop-up window.


Once the contract deployment transaction is approved, the deployed contract will appear under the `Deployed Contracts` section.


Expand the deployed contract and click on "get". It will return the value of storedData, which is currently zero since we have not input any number yet.


To input a value, enter a number in the field near the set button, click on "set" and approve the transaction from the MetaMask pop-up. Once the transaction is approved, the value of storedData will be the input number. To verify this, click on "get", and the previously input value will be printed.


This is how contracts are deployed Polygon!

Conclusion

A big thank you and kudos if you are reading this; you learned what MATIC/Polygon is, how to set up a QuickNode Polygon node in MetaMask, get test MATIC, deploy a smart contract, and interact with it!

Subscribe to our newsletter for more articles and guides on Ethereum. If you have any feedback, feel free to reach out to us via Twitter. You can always chat with us on our Discord community server, featuring some of the coolest developers you’ll ever meet :)
Ready to try QuickNode? Start your 7-day free trial

Related articles 14

Apr 12, 2022 How to build an Ethereum URL shortener dApp

dApps (decentralized applications) are an integral part of the Ethereum development ecosystem. There are thousands of dApps already residing on the Ethereum blockchain. In this guide, we will learn how to build an Ethereum dApp that...

Continue reading
Jan 31, 2022 How to Create a BEP20 Token

BEP20 is the Binance Smart Chain equivalent to the popular ERC20 specification for tokens on the Ethereum network. The difference is the BEP20 Token isn't run on the Ethereum Network, but the Binance Smart Chain network. Despite this difference, because BSC (Binance Smart...

Continue reading
Dec 27, 2021 An overview of how smart contracts work on Ethereum

Smart contracts are the first-class citizens in the Ethereum blockchain; they are a critical part of Ethereum development. In this guide, we’ll see an overview of how smart contracts work on the Ethereum blockchain. This will include how to set up an Ethereum IDE and then...

Continue reading
Mar 7, 2022 How to create and deploy an ERC20 token

Ethereum network’s launch in 2015 created a lot of buzz in the developer community and sprouted a lot of tokens on the network. Initially there weren’t any templates or guidelines for token development. This resulted in a variety of tokens quite different from each other....

Continue reading
Dec 27, 2021 How to create your own Oracle with an Ethereum smart contract

Ethereum smart contracts are the most valuable features of the Ethereum blockchain network; The development of applications driven by smart contracts on the Ethereum blockchain has skyrocketed recently. These smart-contracts are sandboxed and can’t access the data...

Continue reading
Dec 27, 2021 What is an ABI?

While interacting with a smart contract ABI is one of the essential components. In this guide, let us understand what the ABI of smart contracts is.

Continue reading
Dec 27, 2021 How to call another smart contract from your solidity code

Smart contracts are the most popular feature of the Ethereum network. Smart contracts have functions that sometimes need to be accessed by other smart contracts. Before understanding smart contracts' interactions, let's brush up on our basics about smart...

Continue reading
Dec 27, 2021 How to create and deploy an ERC-721 (NFT)

Digital collectibles compatible with the ERC-721 standard have become very popular since the launch of Cryptokitties and have moved forward towards mass adoption in recent months. This guide will cover creating and deploying our...

Continue reading
Dec 27, 2021 How to write an Ethereum smart contract using Solidity

This article is intended for developers new to Ethereum development. In this article, we will talk about Solidity and smart contracts, What they are and what role they actually play in the ethereum development with the end goal of writing a smart contract using...

Continue reading
Mar 14, 2022 How to Create and Deploy an ERC-1155 NFT

ERC1155 has emerged as a gold standard to create NFTs; every major marketplace lists new tokens as an ERC1155 standard. In this guide, we will learn about the ERC1155 token standard and how to create an ERC1155 token.What we will...

Continue reading
Jan 31, 2022 Como crear un Token BEP20

BEP20 es en Binance Smart Chain el equivalente al popular token ERC20 de la red de Ethereum. La diferencia es que el token BEP20 no corre en la red de Ethereum sino en la red de Binance Smart Chain. Más allá de esta diferencia, como BSC (Binance Smart Chain) implementa la...

Continue reading
Feb 12, 2022 How to Create a Smart Contract Factory in Solidity using Hardhat

Hello reader! Today we are going to be leveling up your Solidity skills. We are going to walk you through how to implement the factory contract pattern. Do not worry if you have not heard this term before; all will be explained to you by the end of the...

Continue reading
Apr 12, 2022 How to Create and Deploy a Factory ERC-1155 Contract on Polygon using Truffle

With the increasing popularity of NFTs, Polygon, a fast-growing blockchain, has become the go-to choice for many users and developers. Compared to Ethereum, Polygon allows users and developers to interact with blockchains at a more affordable level. For these reasons,...

Continue reading
Apr 12, 2022 How to Create a Dutch Auction Smart Contract

Often to perform any transaction between two untrusted parties, there is a need for a trusted middle man; smart contracts eliminate this need for a middle man altogether. In this guide, we will learn how to create a dutch auction smart contract which will make the entire...

Continue reading