Deploying your first smart contract to Polygon Testnet

Nnamdi
8 min readMar 7, 2022

Polygon formerly known as the Matic Network is a layer-2 solution built on top of the ethereum blockchain.

Polygon has several benefits over ethereum including:

  1. Faster transactions that ethereum.
  2. Cheaper than ethereum. Gas fees on Polygon are really cheap making it one of the most affordable L2’s out there.

To write this smart contract we will be making use of the Remix IDE and Metamask. Remix is a browser-based IDE that allows us to write and deploy smart contract directly from our web browser. To access Remix, visit remix.ethereum.org.

If you do not have the Metamask Chrome extension, you can download it from the chrome store. For instructions on how to setup Metamask check this out

Project Summary

In this blog post, we will be writing a smart contract that allows someone to request for money($MATIC in this case since we are using Polygon) and allows multiple people to donate to the requester.

Code architecture

To be able to build this mini-project, we will be needing two global variables

  1. Requester variable — this will be the person that requests the money. The requester variable will be an address.
  2. Sender variable — this will be a dynamic array of senders. We chose a dynamic array over a fixed array because we don’t know how many senders we will have. This dynamic array will accept addresses.

Now that we know the variables we are working with let us discuss the functions we will be needing.

  1. generateRequest — this is a public function that allows someone to request money
  2. donate — this function is what we will use to get people to donate to the request that was generated.
  3. makePayment- Our smart contract will hold the matic donated, we will then create a function that tells our contract to donate the money to the requester.
  4. getSenders- this will return us a list of everyone that donated to the requester.

The Contract Code

remix ide

Navigate to the code editor part of the Remix IDE to do this click on contract and select ballot.sol once there we can start to write our solidity code.

Explaining the Code:

The first line of the code pragma solidity ≥0.4.17 <0.9.0specifies that the source code is for a Solidity version greater than or equal to 0.4.17 and less than 0.9.0. Pragmas are common instructions for compilers about how to treat the source code (e.g., pragma once).

Next, we will create the contract called getFunds inside this contract is where we will declare our global variables and write all our functions.

As discussed earlier, we will have two(2) global variables; requester and senders.

The address is one of the basic solidity types that stores wallet addresses. The other basic types are String, int, uint, Boolean, fixed, and ufixed. Using the [] in the senders variable is what informs our solidity code that we are making a dynamic array.

The public keyword allows us to access variables from outside a contract and creates a function that other contracts or SDKs can call to access the value of whatever we have in the function.

The generateRequest function is a public function that is used to generate a request by the requester. It uses the msg object which is one of the special variables that solidity has to get the address of the person that is generating the request and assign it to the requester.

This msg object contains the following objects

  1. msg.data it gets the “data” field from the call or transaction that invoked the current function
  2. msg.sender address of the account that started this current object invocation
  3. msg.value Amount of ether or Matic in our case (in Wei) that was sent along with the function invocation.

Next, we have the donate function, this is also a public function, however, we have a payable keyword that means that the function can receive Ether (or Matic for us) without this keyword nobody will be able to send any money to the requester. We use required because we want to ensure that there is a minimum amount of Matic (in Wei) that someone can donate. If we don’t enforce this then we can have people donating as little as 0.000000001 Matic. To know the amount of Matic(in Wei) we can use this helpful tool https://polygonscan.com/unitconverter. The senders.push(msg.sender) lets us populate our senders array with the address of anyone that sends money to the requester.

The makePayment function is a public function as well and we use it to notify the smart contract to send out Matic to the requester. The first thing we want to do here is to ensure that the person that calls our makePayment function is the requester. We don’t want a scenario where anyone can just call this function. Next, we use the transfer method to initiate a transfer, while this.balance lets us send all the amount that we have in that contract. Finally, we use senders= new address[](0) so that when makePayment has been called we do not have to redeploy a new smart contract for a new donation to occur. This new address[](0) empties out our senders array and creates a new one starting at position 0.

The last function is the getSenders function. This returns the entire array of senders to us. Unlike other functions that had just public and/or payable, this function has something called views the views function lets our smart contract know that we do not intend to modify the state of the variables or anything in the function. We then use returns to indicate what that function will be returning in this case a dynamic array of addresses so we add address[] Then we add the return statement which returns our senders array.

Compiling and Deploying the code

Now that we are done understanding the code, the next step is to compile the code in our Remix IDE.

When you write your contract’s source code, it needs to be compiled using something called a Solidity compiler.

The frontend doesn’t know/understand the backend, so the compiler produces two things;

  1. Application Binary Interface — this is the standard way to interact with contracts in the blockchain ecosystem, both from outside the blockchain and from contract-to-contract interaction. In short, this is the only way the javascript interacts with your contract.
  2. Contract Bytecode — this is what gets deployed to the Polygon network. EVM bytecode is a low-level programming language that is compiled from a high-level programming language such as solidity. EVM is a virtual machine that allows us to run our compiled code as well as to keep the state of our code. Thankfully to EVM, smart contracts can be run on almost any of computers.

Remix helps us do this so we won’t directly be interfacing with it, however, it is good to understand how it works and why we need to compile our solidity code.

When you see the green check it means your code has been compiled successfully.

Let us deploy our contract. We need to setup Metamask to use the Polygon network

  • Put in a Network name — “Matic Mumbai Testnet”
  • In URL field you can add the URL as “https://rpc-mumbai.maticvigil.com"
  • Enter the Chain ID: 80001
  • (Optional Fields) Symbol: “maticmum” and Block Explorer URL: “https://mumbai.polygonscan.com/"

When we have deployed our smart contract you will be able to check this out using the link https://mumbai.polygonscan.com.

Once we are done setting up the Polygon network on Metamask, we will need to request for some test token. We will use this test token to pay gas fees to deploy our contract as well as to pay the minimum amount required to donate.

To get some test tokens you can use this Faucet and add your wallet address.

To deploy our contract, you will need to click on the icon just below the green checkmark

In the image above you will see Environment ensure you set it to “Injected Web3”. It should show you your wallet address and your Matic balance.

You can then click deploy (only add the value when you want to call the donate function). Clicking deploy will initiate Metamask

Click connect.

Once connected, the ‘Deploy’ transaction would generate another Metamask popup that requires transaction confirmation.

When you have successfully deployed your contract you should be able to see all your function in the Remix IDE and you can interact with them directly from Remix.

Congratulations! You have successfully deployed getFunds Smart Contract to the Polygon testnet. Now you can interact with the Smart Contract. Check the progress here: https://mumbai.polygonscan.com/.

It is important to note that contracts aren’t deployed immediately. It takes a little time (usually less than 15secs) for the contract to be deployed.

Verifying your Smart Contract Code

Once you are done deploying, you need to verify your smart contract on the Polygon testnet. To do that follow these steps:

  1. Visit https://mumbai.polygonscan.com/verifyContract

2. Enter the contract address. You can get your contract address by visiting https://mumbai.polygonscan.com/ and inputting your wallet address.

Click on Contract Creation that will show you the contract address.

3. Now that we have the contract address we can then enter it into the field provided, select a compiler type — I chose 0.4.17 but you can choose anyone you want. For the Open source license type, I selected None.

4. Click Continue

5. Next step, we will add our contract source code.

6. It should be successful and you can click on Compiler output to see our contract’s bytecode and ABI

We have now successfully created a smart contract, compiled it, deployed it, and verified it on the Polygon Network.

If you enjoyed this please leave a clap and follow me on twitter @NnamdiO_

--

--