Skip to main content

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:
In order to send or check the balance of your ERC20 tokens you must interact with the smart contract. Depending on if those functions are Read-Only or State Changing you interact with these functions in two different ways.

Read-Only Functions

Notice that these functions (shown as their Solidity function signatures) all contain the view 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 contain view 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:
  1. Gathering inputs for generating a transaction
  2. Using Web3.swift to generate an ERC20 transaction
  3. 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

The Web3.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.

Using Portal to sign and send the transaction

Once you’ve generated a transaction using the token Contract, you can use it to create a request using your Portal instance to sign and send the transaction.

The sendERC20Token() function

Now that we’ve covered the steps required to sign and send a transaction using ERC20 tokens, let’s look at an example of a full function for sending ERC20 tokens to another wallet.