Polygon Edge : Architecture & Local Setup with Metamask for deploying ERC20 & ERC721 Contracts

Atharva Paliwal
Coinmonks
9 min readAug 20, 2022

--

In this article we are going to cover-

  1. What is Polygon Edge?
  2. What are modules used by Polygon Edge?
  3. Polygon Edge local setup from scratch
  4. Deploying smart contracts on Polygon Edge
  5. Configuring Polygon Edge with Metamask

What is Polygon Edge?

Polygon Edge is an extensible and modular framework for developing Ethereum-compatible sidechains, blockchain networks, and general scaling solutions. The primary role of this framework is to bootstrap a new blockchain network while providing full compatibility with Ethereum transactions and smart contracts. It utilizes a centralized bridge solution to support communication with multiple blockchains, enabling the transfer of ERC-20 and ERC-721 tokens.

Consensus supported : PoA(Proof of Authority) , PoS(Proof of Stake)

Modules offered by Polygon Edge

Polygon Edge offers wide range of modules that developers can customize depending on their needs.

Source : Polygon Edge Documentation

1. Blockchain and State

Blockchain deals with all the logic that happens when a new block is included in the blockchain. State represents the state transition object. It deals with how the state changes when a new block is included.

This two parts are interconnected provide the blueprint for the protocol. Among other functionalities, they allow the execution of transactions and run the EVM.

For example, when the Blockchain layer receives a new block (and no reorganization occurred), it calls the State to perform a state transition.

2. Consensus

Consensus provides an interface for consensus mechanism. It provides two types of consensus engines-

  • IBFT PoA
  • IBFT PoS

3. Libp2p

Libp2p provides the basis for the underlying network layer and provides a great foundation for more advanced features used in Polygon Edge.

4. Networking(GPRC)

GRPC layer is vital for operator interactions. It leverages the communications between nodes which helps node operators interact easily with the client.

5. JSON RPC

The JSON RPC layer is an API layer that allows developers use to communicate with the blockchain.

6. Tx Pool

Tx Pool layer pools the transactions from different parts of the system and it is closely linked with other modules in the system, as transactions can be added from multiple entry points.

Polygon Edge local setup

So, after having a look at the architecture and the modules used by Polygon Edge. Let us set the Polygon Edge network locally. Before going for the further steps, let us discuss the pre-requisites-

1. Go >=1.17 on your local machine

2. Any editor of your choice

3. Metamask

After you are done with the prerequisites, we can install the Polygon Edge binary using go install.

In your terminal paste this command-

The binary file after this command will be located in your GOBIN environment variable.

After this you can confirm your installation by typing polygon-edge in your terminal.

Now we will setup working polygon-edge blockchain network working with IBFT consensus protocol.

Source : Polygon-Edge Documentation

The blockchain network shown in the above image consist of 4 nodes of whom all 4 are validator nodes, and as such are eligible for both proposing block, and validating blocks that came from other proposers.

For our article we will work with a single node who will be acting both as the validator and will also propose new block.

NOTE : There is no minimum to the number of nodes in a cluster, which means clusters with only 1 validator node are possible. Keep in mind that with a single node cluster, there is no crash tolerance and no BFT guarantee.

Therefore, the minimum recommended number of nodes for achieving a BFT guarantee is 4 — since in a 4 node cluster, the failure of 1 node can be tolerated, with the remaining 3 functioning normally.

For generating a fully functional IBFT cluster we need to follow below steps-

1. Initializing data directories

test-chain-1 here refers to the name of the node. You can name it as per your convenience.

The above commands prints the validator key and the node ID. We will require node ID for the next step.

You should also see a folder created by the node name that you would have specified.

2. Prepare the multiaddr connection string for the bootnode

If a node wants to establish a successful connection, it must know which bootnode server to connect to in order to gain information about all the remaining nodes on the network.

Since we are going with a single node for our demo, the same node will be acting as the bootnode as every polygon-edge node can serve as a bootnode but in case of multiple nodes, every polygon-edge node needs to have a set of bootnode specified which will be contacted to provide information on how to connect with all remaining nodes in the network.

NOTE : At least one bootnode is required, so other nodes in the network can discover each other. More bootnodes are recommended, as they provide resilience to the network in case of outages.

To create the connection string for specifying the bootnode, we will need to conform to the multiaddr format.

Here,

ip_address :127.0.0.1 (as we are running on localhost)

port :10001

node_id : 16Uiu2HAmPvqyBqtF5UxTsXftWBavEeoC5mUqtv3bWfhFna6FwrTe

You may have a different node_id, so replace it with yours.

3. Generate the genesis file with single node

Before executing this command, we can also premine some balance to our account address using --premine flag. You can take any address from your Metamask and provide some ethers.

As you can see, I have premined 1000 ETH to my account address and our genesis file is now successfully created.

4. Start the Node

As we are just running a single Polygon Edge node there will not be any conflict issue based on Ports, however if you have multiple nodes kindly check the ports to avoid the conflicts.

For our single node we have-

10000 : gRPC server

10001 : libp2p server

10002 : JSON-RPC server

Now let’s start the node using command-

You can also provide block time that you want to set for the creation of blocks using the flag --block-time. By default the block time is 2 sec.

Your terminal must have been running now, you can now go and check in your browser by typing your JSON-RPC URL.

If you got something like this, you have MADE ITTT!!! You have started your polygon edge node successfully!!!

Let’s check on Metamask whether we have got our premined balance or not.

Login to Metamask and add a new network with JSON-RPC URL as -http://localhost:10002/

Okay so we got the 1000 ETH that we premined. Ignore the Token Symbol as I have kept it as PGE. You can keep any token symbol of your choice.

Deploying smart contracts on Polygon Edge

Let us try deploying a ERC20 & ERC721 contract on Polygon Edge.

For this you need some prerequisites-

  1. You should setup hardhat environment locally
  2. Setup a basic Hardhat Javascript project

After setting up your hardhat project, your project structure might look like this. So first we will configure our hardhat.config file to add Polygon-Edge network inside it.

Add this settings to your hardhat.config file. Export your PRIVATE_KEY from your Metamask for your account address.

NOTE : By default the chainId is 100, although we can change that in the above steps using chainId flag.

Now let us write a simple ERC 20 & ERC 721 contract which mints fungible and non-fungible tokens respectively.

ERC20Contract
ERC721Contract

Let’s now write the deployment scripts for our contracts to deploy our contract.

In the scripts folder we make 2 files- ERC20Demo.js & ERC721Demo.js for deployment of our smart contracts.

NOTE : You can even deploy the 2 smart contracts from single file, but for a better understanding in our demo , I am using 2 different files.

ERC20Demo.js
ERC721Demo.js

After you are done with your deployment scripts, let’s now deploy the contracts using the command-

Make sure you run before running the command that your Polygon Edge network is up and running.

After you deploy the smart contracts, you will get deployed contract address for ERC20 & ERC721. Make sure you store it somewhere as we will be needing that.

Importing Tokens in your Metamask Wallet

Now you have got the deployed contract address, let’s now import it in our Metamask wallet.

Follow this path -

Metamask wallet -> Assets -> Import Tokens -> Your deployed token address

After entering the token address, it automatically detects your token symbol.

NOTE : Make sure you are in Polygon Edge network in Metamask.

After you add both the tokens to your Metamask, you might see the tokens being imported into our Metamask wallet.

Hurrayyyyy!!! We now have 100 ERC20 tokens and 1 ERC721 Token in our Metamask wallet.

Well, the article was supposed to end here but let’s do a bonus section by sending ERC20 Tokens to different account in Metamask.

Bonus Section

Follow this path-

Go to your ERC20 Token->Send->Transfer between my Accounts->Specify the amount you want to send->Confirm.

NOTE : Make sure you have more than 2 accounts in Metamask wallet

Now you may be wondering that why I wanted to show this?

In the above image check for the gas fees that we are consuming. SHOCKEDDD!!! Yes it is 0. We have not used any of the premined balance that we gave while setting up our Polygon Edge network.

Let’s now check whether the receiver has received the 50 E20 Tokens or not.

Well , if you check the balance of senders account it is 50 now.

Let’s check it on the receivers side-

You need to follow the previous steps of importing the ERC20 contract address in your other account.

Yayyyy!! We got the 50 E20 tokens at the receivers end. And yes that too without consuming any gas fees :)

That’s the end to this article. I hope this article covers all you need to get started for your Polygon Edge journey.

Happy Codinggg!!!

You can contact me here in case you have any doubts-

LinkedIn- https://in.linkedin.com/in/atharva-paliwal-70676a175

--

--

Atharva Paliwal
Coinmonks

Blockchain Developer (R&D) at Persistent Systems | Writes about new and less known things that you may want to add to your skills portfolio ✍️💫