Native token ERC-20
Detailed guide by Tanssi
The native token ERC-20 precompiled contract on Parodychain allows developers to interact with the native protocol token through an ERC-20 interface. Although parodychain's native token is not an ERC-20 token, now you can interact with it as if it was a vanilla ERC-20.
One of the main benefits of this precompile is that it removes the necessity of having a wrapped representation of the protocol token as an ERC-20 smart contract, such as WETH on Ethereum. Furthermore, it minimizes the need for multiple wrapped representations of the same protocol token. Consequently, dApps that need to interact with the protocol token via an ERC-20 interface can do so without needing a separate smart contract.
Under the hood, the ERC-20 precompile executes specific Substrate actions related to the Substrate balances module, which is coded in Rust. The balances module provides functionality for handling the various types of balances.
This guide will show you how to interact with PDY tokens, the native protocol token of Parodychain, via the ERC-20 precompile.
The precompile is located at the following address:
Note
There can be some unintended consequences when using precompiles. Parodychain's Native Token ERC-20 Precompile is derived from Moonbeam's, and as such, please familiarize yourself with Moonbeam's Precompile Security Considerations.
The ERC-20 Solidity Interface
The ERC20.sol
interface on Parodychain follows the EIP-20 Token Standard, which is the standard API interface for tokens within smart contracts. The standard defines the required functions and events a token contract must implement to be interoperable with different applications.
Note
The ERC-20 precompile does not include deposit
and withdraw
functions and subsequent events expected from a wrapped token contract, such as WETH.
Interact with the Solidity Interface
Checking Prerequisites
To follow along with this tutorial, you will need to have your wallet configured to work with parodychain and an account funded with native tokens.
Add Token to MetaMask
If you want to interact with your parodychain's native token like you would with an ERC-20 in MetaMask, you can add a custom token to your wallet using the precompile address.
To get started, open up MetaMask and make sure you are connected to parodychain and:
Switch to the Assets tab
Click on Import tokens
Now, you can create a custom token:
Enter the precompile address for the token contract address -
0x0000000000000000000000000000000000000800
. When you enter the address, the Token Symbol and Token Decimal fields should automatically populate. If they do not, you can enterPDY
for the symbol and18
for the decimal places.Click Next
MetaMask will prompt you to confirm the import. You can review the token details and click Import Tokens to import PDY tokens into your wallet.
And that's it! You've successfully added the PDY token as a custom ERC-20 token on your Parodychain.
Remix Set Up
You can interact with the ERC-20 precompile using Remix. To add the precompile to Remix, you will need to:
Get a copy of
ERC20.sol
Paste the file contents into a Remix file named
IERC20.sol
Compile the Contract
Next, you will need to compile the interface in Remix:
Click on the Compile tab, second from top
Compile the interface by clicking on Compile IERC20.sol
When compilation is completed, you will see a green checkmark next to the Compile tab.
Access the Contract
Instead of deploying the ERC-20 precompile, you will access the interface given the address of the precompiled contract:
Click on the Deploy and Run tab directly below the Compile tab in Remix. Please note that the precompiled contracts are already accessible at their respective addresses. Therefore, there is no deployment step
Make sure Injected Web3 is selected in the ENVIRONMENT dropdown. Once you select Injected Web3, you may be prompted by MetaMask to connect your account to Remix if it's not already connected
Make sure the correct account is displayed under ACCOUNT
Ensure IERC20 - IERC20.sol is selected in the CONTRACT dropdown. Given that it is a precompiled contract, there is no deployment step. Instead, you are going to provide the address of the precompile in the At Address field
Provide the address of the ERC-20 precompile:
0x0000000000000000000000000000000000000800
and click At Address
The IERC20 precompile will appear in the list of Deployed Contracts.
Get Basic Token Information
The ERC-20 interface lets you quickly obtain token information, including the token's total supply, name, symbol, and decimal places. You can retrieve this information by following these steps:
Expand the IERC20 contract under Deployed Contracts
Click decimals to get the decimal places of your parodychain's native protocol token
Click name to get the name of the token
Click symbol to get the symbol of the token
Click totalSupply to obtain the total supply of native token of parodychain
The results of each function call are displayed under the respective functions.
Get Account Balance
You can check the balance of any address on parodychain by calling the balanceOf
function and passing in an address:
Expand the balanceOf function
Enter an address you would like to check the balance of for the owner
Click call
Your balance will be displayed under the balanceOf
function.
Approve a Spend
To approve a token spend allowance, you'll need to provide an address for the spender and the number of tokens the spender is allowed to spend. The spender can be an externally owned account (EOA) or a smart contract. For this example, you can approve the spender with an allowance of 1 PDY token. To get started, please follow these steps:
Expand the approve function
Enter the address of the spender. You should have created two accounts before starting, so you can use the second account as the spender
Enter the amount of tokens the spender can spend for the value. For this example, you can allow the spender to spend 1 PDY token in Wei units (
1000000000000000000
)Click transact
MetaMask will pop up, and you will be prompted to review the transaction details. Click Confirm to send the transaction
After the transaction is confirmed, you'll notice that the balance of your account has stayed the same. This is because you have only approved the allowance for the given amount, and the spender hasn't spent the funds. In the next section, you will use the allowance
function to verify that the spender can spend 1 PDY token on your behalf.
Get Allowance of Spender
To check that the spender received the allowance approved in the Approve a Spend section, you can:
Expand the allowance function
Enter your address for the owner
Enter the address of the spender that you used in the previous section
Click call
Once the call is complete, the allowance of the spender will be displayed, which should be equivalent to 1 PDY token (1000000000000000000
).
Send Transfer
To send tokens from your account directly to another account, you can call the transfer
function by following these steps:
Expand the transfer function
Enter the address to send PDY tokens to
Enter the amount of PDY tokens to send. For this example, you can send 1 PDY token (
1000000000000000000
)Click transact
MetaMask will pop up, and you will be prompted to review the transaction details. Click Confirm to send the transaction
Once the transaction is complete, you can check your balance using the balanceOf
function or by looking at MetaMask. You'll notice that your balance has decreased by 1 PDY token. You can also use the balanceOf
function to ensure that the recipients balance has increased by 1 PDY token as expected.
Send Transfer From Specific Account
So far, you have approved an allowance of 1 PDY token for the spender and sent 1 PDY token via the standard transfer
function. The transferFrom
function varies from the standard transfer
function as it allows you to define the address to which you want to send the tokens. So you can specify an address with an allowance or your address as long as you have funds. For this example, you will use the spender's account to initiate a transfer of the allowed funds from the owner to the spender. The spender can send the funds to any account, but you can send the funds from the owner to the spender for this example.
First, you need to switch to the spender's account in MetaMask. Once you switch to the spender's account, you'll notice that the selected address in Remix under the Accounts tab is now the spender's.
Next, you can initiate and send the transfer. To do so, take the following steps:
Expand the transferFrom function
Enter your address as the owner in the from field
Enter the recipient address, which should be the spender's address, in the to field
Enter the amount of PDY tokens to send. Again, the spender is currently only allowed to send 1 PDY token, so enter
1000000000000000000
Click transact
Once the transaction is complete, you can check the balance of the owner and spender using the balanceOf
function. The spender's balance should have increased by 1 PDY token, and their allowance should now be depleted. To verify that the spender no longer has an allowance, you can call the allowance
function by passing in the owner and spender's addresses. You should receive a result of 0.
And that's it! You've successfully interacted with the ERC-20 precompile using MetaMask and Remix!
Last updated