Deploy and Track ERC20 Events

The following guide will explain you how to deploy a ERC20 Smart Contract, Sync and Watch a Contract Events and saving the data into the database:

For this example we deployed an ERC20 Contract with symbol MOR with ERC20PresetMinterPauser.sol using our local ganache instance, Remix and Metamask. and we want to track all _mint events for our Contract (keep in mind that _mint events in OpenZeppelin emits a Transfer event, more info HERE)

Set Up Ganache

The first step will be to download and install Ganache from https://www.trufflesuite.com/ganache, install it and quickstart a new ethereum chain:

After quickstarting you will have 10 ETH Wallets with 100.00 ETH on each one.

Set Up Metamask

The next step will be to set up Metamask in your browser, if you don't have it already you can get it from here https://metamask.io/ and we will connect it to our local ganache instance

Click to metamask icon on the browser and select the network drop-down menu. Here we should connect to pur Local Ganache Instance. Click to “Custom RPC”.

  • Network Name: Ganache

  • New RPC URL: http://localhost:7545

  • Chain Id: 1337

This information should be enough to connect MetaMask to your Local Ganache.

We are going to connect now, one of the ETH address to our Metamask, for this we choose one address and click on the key icon to get the private key

Then we open MetaMask and click on the Top Right Avatar and click on import Account

Just Paste your Private Key, and account will be now connected.

Deploy Smart Contract

Now, we can create our mintable token on Remix. Open Remix on your browser or go to this link https://remix.ethereum.org/

Click one New File

Give it a name, we called it erc20.sol

Since we will use an ERC-20 contract from OpenZeppelin, just paste this line to the file and save, to save just use CONTROL + S on Windows OR COMMAND + S on Mac

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/presets/ERC20PresetMinterPauser.sol";

After save, you should see the contract imported from github

Now to compile our contract we need to pay attention to the following things

  1. We have selected the ERC20PresetMinterPauser.sol file

  2. We are on the Solidity Compiler Tab

  3. Pragma solifidy version matchs the compiler version

Click on Compile ERC20PresetMinterPauser.sol

For the next Step we need to make your our account is connected to Remix, so we just click on MetaMask, and if its not connected you should see the following pop-up, just click on Connect.

If the account is connected we can go now to the Deploy and Run Transactions Tab, and select from Enviroment Injected Web3

You should see your wallet with 100 ETH, select ERC20PresetMinterPauser.sol from the Contract List, and click on the arrow to give it a name to your Token

After Clicking on transact, you will get a pop-up signature validation from MetaMask, just confirm.

You should see your Smart Contract in the Deployed Contracts Area

We are going now to Mint some tokens to our address, Click on the arrow of your contract and open the mint event, put the address and amount

Enter your address and an amount in WEI. For example, I will mint 1000 MOR token so, I entered “1000000000000000000000”

After Clicking on transact, confirm transaction with Metamask and the tokens should be now in the address.

ADD Token to Metamask

To add the Token to Metamask, we first need to get our Token Address, for this we just click on the clipboard Icon on the Deployed Contracts Area

Then We go to Metamask, to Add Custom Token, and we put the Token Address and the Token Symbol should be automatically populated.

Congratulations! You deployed your own ERC-20 Token.

Connecting Moralis to your local ganache instance

Now on the Moralis Admin go to your Ganache Server and click View Details and go to the Ganache Proxy Server Tab

Moralis comes wih a built-in proxy server just for you, that enables direct connection from your ganache instance to your server without worrying about firewalls.

We need to get FRP from https://github.com/fatedier/frp/releases depending on your OS/Hardware, In some Windows Versions, FRP could be blocked by firewall, just use a older release, for example frp_0.34.3_windows_386, its a known issue more information here: https://github.com/fatedier/frp/issues/2095

Just replace the content in "frpc.ini", the code assumes your ganache instance is hosted at port 7545

Then you just need to run

./frpc -c frpc.ini (Linux)
frpc.exe -c frpc.ini (Windows)

To verify the connection made is correctly, just refresh the page (F5) and you should see now the CONNECTED status

Syncing and Watching Contract Events from Moralis

Next step will be to create a new plugin in the admin panel to Sync and Watch Contract Events:

  • description: Sync MOR Transfer Events, It's a small description for us to keep track of all the plugins we add to our instance

  • topic: We use Transfer(address,address,uint256) but you can also use the sha3 topic 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef

  • abi: Our abi includes 3 inputs (from, to, value), we get this abi directly from REMIX

  • address: 0x4F27558d3F86670a9E2EfF294b7d10600266533F (our MOR Contract Address)

  • tableName: MORTransferEvent its the name of the table that will be created in our database with all the events.

To get the ABI from Remix, just go to the Solidity Compiler Tab, choose ERC20PresetMinterPauser.sol contract and click on the ABI icon, the Full ABI, will be copied to the clipboard, make sure you use only the ABI for the event you are syncing

Make sure your frp-ganache is connected, and you will see the table being filled with historical and realtime data

More details on Sync and Watch Smart Contracts HERE.