Share this
Tutorial: How to deploy smart contracts on Polygon (Testnet and Mainnet)
by Sara Mandic on May 11, 2022 3:22:56 PM
For all newbies in web3, creating and deploying a smart contract can sound like a very difficult and mysterious task. However, the very basic, but consistently achieved knowledge can lead to it easy and very pleasant implementation.
Basic Terms
To begin a process of smart contracts deployment, it is crucial to quickly get acquainted with fundamental terms, you will need during that procedure.
Smart Contracts
Easily said, smart contracts are programs stored on a blockchain, which are executed when predetermined conditions are met. Typically, they are created to make the execution of an agreement easier, quicker and without any intermediary’s involvement. The last feature is the essence of an entire decentralization idea, which is provided by blockchain.
Polygon
Polygon is a “layer two” or a “side chain” of an Ethereum blockchain. The big advantage of this scaling solution is that smart contracts deployed to polygon cost less fees than in Ethereum. Cryptocurrency used to pay fees in a Polygon is called “MATIC”.
Mainnet & Testnet
Testnet is just a prototype of an existing blockchain, but Mainnet is a completely developed ecosystem, which allows users to receive and transfer cryptocurrencies.
Metamask
Metamask is a software cryptocurrency wallet, which allows users to interact with Ethereum blockchain through a browser extension or a mobile app. In this tutorial it will be used to connect with Remix and pay the fees of deployment.
Remix
Remix IDE is an open source Ethereum IDE, which allows users to create, edit, compile and deploy smart contracts in a very intuitive and easy way, from the browser editor.
The tutorial of how to deploy a smart contract on polygon
Connecting MetaMask Wallet to Polygon (Testnet & Mainnet).
This process is very simple and intuitive. Assuming that you have created an account in Metamask, adding those ecosystems requires just a quick visit of this website and some additional adjustments:
- Connect your wallet to this page, by clicking the “connect wallet” button at the top. Accept it in a Metamask plugin.
- Enable the testnet networks by switching the toggle “Testnets”,
- Seek needed networks, by typing “Polygon” in the search bar,
- Add Polygon Mainnet and Mumbai to Metamask.
Adding Matics to a wallet.
Deploying a smart contract, even in Testnet, requires payment for transactions in tokens which are in circulation inside the ecosystem. In Polygon, these tokens are represented by “MATIC”.
To add Matics in the Testnet network, just visit this website and paste a wallet address in an input, then submit the form. After a moment (1-2 minutes) your wallet will obtain Matics in the Mumbai network.
Increasing a balance in a mainnet network requires “real tokens”, receiving in exchange or by buying through Metamask. This example presents a way of buying MATICs by Metamask.
In Metamask plugin, select the Polygon network and click the buy button under the balance of MATIC. It will redirect you to the “TRANSAK”. Then, after a quick KYC process, select the payment method and the currency. Finally, purchase tokens and wait for the indicated amount of time.
Creating a simple smart contract in remix.
The most fundamental part is creating a concrete, working and fully-sufficient smart contract.
Remember, that a shorter contract and less used memory in a network, causes the lower price of the deployment.
One of the easiest ways of preparing a smart contract is by creating it in remix (online IDE). Below we present the special, very simple and written in Solidity smart contrac
// SPDX-license-Indentifier: MIT pragma solidity ^0.8.13;
contract BalanceManagement {
uint256 balance;
address payable public admin;
constructor() {
admin = payable(msg.sender);
balance = 0;
updateBalance();
}
function updateBalance() internal {
balance += msg.value;
}
function withdraw(uint256 amount) public{
require(msg.sender == admin, "Withdrawing is possible only for an admin");
require(amount <= balance, "Withdraw can not be proceeded, because of limited balance");
balance = balance - amount;
}
function deposite(uint256 amount) public returns (uint256) { return balance = balance + amount; }
function checkDepositBalance() public view returns (uint256) { return balance; }
}
This smart contract enables users to manage their deposit, by checking its balance and transferring tokens. So, to keep it as simple as possible, it only works as a preview, without deeper transactions, only for showing the deployment process.
Below is a screenshot of a smart contract code in a Remix IDE.
What is important, smart contract is created in a separate file, “BalanceManagement.sol” in a workspace, this file will be further compiled and deployed.
Compiling and deploying smart contract
To go through the deployment process, it is necessary to firstly compile a smart contract. This is reachable via the Compile button in the sidebar.
If there are no errors, smart contract is ready for deployment.
So, the first process is run for a Testnet. Completing that, should begin with clicking an Ethereum icon on the left sidebar and choosing an environment for “injected Web3”. It will force a Metamask to connect with the Remix.
Then it is necessary to click the orange button “Deploy” and confirm the transaction in Metamask. Fees paid by MATICs sourced from Polygon Faucet. Remember to have a correct network set in Metamask, before deployment, it will save your real tokens.
You can see the transaction details, by clicking the link “View on the block explorer” in Metamask, under the transaction details. So it will redirect to mumbai Polygonscan and show the overview.
Deployment of the smart contract in a Mainnet is practically the same as in Testnet. It requires only changing the network in Metamask for Polygon and paying fees on MATICs.
Conclusion
Deployment of smart contracts can be simple and intuitive, especially using simple tools like remix and Metamask. Combining it with super cheap blockchain, which is Polygon, it can be extremely efficient and fun. It is worth mentioning that if the production is very complex, the deployment must preceded by serious testing. Since once a smart contract is deployed, it cannot be changed, only a new smart contract with correct code can be published. However paying a lot of fees just for wrongly typed piece of code is definitely something worth avoiding, by creating proper tests.
No Comments Yet
Let us know what you think