What are ERC20 tokens?
ERC20 is an Ethereum standard that defines a unified interface for standard token behavior in a smart contract. This allows for custom tokens to be created beyond the native token for that chain (ETH in the case of Ethereum). Examples of ERC20 tokens include stablecoins like USDT & USDC or Defi protocol tokens like UNI. The ERC20 standard defines just a few Solidity functions that these contracts must implement:Read-Only Functions
Notice that these functions (shown as their Solidity function signatures) all contain theview
keyword. This means that these functions can be invoked using eth_call
RPC method and do not change the state of the blockchain. This also means that calling these functions does not require gas.
State Changing Functions
These functions do not containview
keyword, which means they do change the state of the blockchain. This requires sending a signed transaction to the blockchain, along with gas, to update the global state. These functions are invoked using the eth_sendTransaction
RPC method.
Transferring ERC20 with your Portal Wallet
The Portal SDK supports the processing of web3 transactions using the Portal Provider. Because of this, working with ERC20 tokens using your Portal Wallet is very similar to working with ETH itself. The main difference is that you’ll need to interface with the ERC20 token’s contract in order to build the appropriate transaction before signing with the Portal Provider. This can be thought of in three basic steps:- Gathering inputs for generating a transaction
- Using Web3.swift to generate an ERC20 transaction
- Using Provider to sign and send the transaction
Gathering inputs for generating a transaction
In this example, we’ll be looking at transferring ERC20 tokens from your Portal Wallet to another wallet (sending ERC20 funds to another wallet). In order to accomplish this, you’ll need your application to manage certain inputs required to generate the transaction: the receiver’s address, and the token address for the ERC20 token being sent.In this case, we’ll use Uniswap (UNI) as an example, so we’ll be using the
tokenAddress
for UNI (0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984
).
Using Web3.swift to generate an ERC20 transaction
TheWeb3.swift
library allows you to easily interface with token contracts to build the appropriate transaction payload for an ERC20 transaction without needing to manually build eth_call
requests. We’ll be using it for this purpose in this example.
The following code demonstrates how to use the Web3.swift
library to access an instance of the UNI Contract and generating a basic send
transaction.
In order to use Web3.swift in your application, you’ll need to add the package as a dependency. More details on adding Web3.swift to your project can be found here.