The following guide will explain how to deploy an ERC-20 smart contract, sync and watch contract events, and saving the data into the Moralis database:
For this example, we deployed an ERC-20 contract with the symbol MOR with "ERC20PresetMinterPauser.sol" using our local Ganache instance, Remix, and MetaMask. We also want to track all "_mint" events for our contract (keep in mind that "_mint" events in OpenZeppelin emits a transfer event, more info HERE).
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 quick-starting, you'll have ten ETH Wallets with 100.00 ETH on each one.
The next step will be to set up MetaMask in your browser. If you don't have it already then you can get it from https://metamask.io/ and we'll connect it to our local Ganache instance.
Click the MetaMask icon on the browser, and select the network drop-down menu. Here we should connect to our local Ganache Instance. Click “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're now going to connect one of the ETH addresses 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 the account will be now connected.
Now, we can create our mintable token on Remix. Open Remix on your browser, or go to this link https://remix.ethereum.org/.
Click on "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 it. To save, just use CONTROL + S on Windows or COMMAND + S on Mac.
After save, you should see the contract imported from GitHub:
Now, to compile our contract we need to pay attention to the following things:
We have selected the "ERC20PresetMinterPauser.sol" file.
We are on the "Solidity Compiler" tab.
The Pragma Solidity version matches the compiler version.
Click on "Compile ERC20PresetMinterPauser.sol".
For the next step, we need to make sure our account is connected to Remix. So, we just click on MetaMask, and if it's not connected then you should see the following pop-up, just click on "Connect."
If the account is connected, then we can go to the "Deploy and Run Transactions Tab", and select environment "Injected Web3".
You should see your wallet with 100 ETH. Now, select "ERC20PresetMinterPauser.sol" from the contract list, and click on the arrow to give your token a name.
After clicking on transact, you'll receive a pop-up from MetaMask asking for signature validation, just click on confirm.
You should see your smart contract in the "Deployed Contracts" area.
We're going now to mint some tokens to our address. Click on the arrow of your contract and open the mint event, input the address and amount.
Enter your address and an amount in WEI. For example, I will mint 1000 MOR tokes so, I entered “1000000000000000000000”.
After clicking on transact, confirm the transaction with MetaMask and the tokens should now have arrived at the proper address.
To add the token(s) 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, add the custom token, and enter the token address. The token symbol should be automatically populated.
Congratulations! You deployed your own ERC-20 token.
Now, on the Moralis admin page, go to your Ganache server and click "View Details", and go to the "Ganache Proxy Server" tab:
Moralis comes with 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 the firewall. If so, just use an older release, for example, frp_0.34.3_windows_386. This is a known issue, more information here: https://github.com/fatedier/frp/issues/2095
Replace the content in "frpc.ini", the code assumes that 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 correct, just refresh the page (you can use F5) and you should now see the "CONNECTED" status:
The next step will be to crea
te a new plugin in the admin panel to sync and watch contract events:
"description" - Enter "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 three inputs: from, to, and value. We get this abi directly from Remix.
"address" - "0x4F27558d3F86670a9E2EfF294b7d10600266533F" (our MOR contract address).
"tableName" - "MORTransferEvent", it's 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 the "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 continuously being filled with historical and real-time data.
More details on Sync and Watch Smart Contracts here.