Skip to main content

ERC-20 Interactions

info

A standard interface for native & custom fungible tokens. A native token is specifically designed to be used as a currency, medium of exchange, or gas token within that blockchain (e.g. ETH, MATIC, BNB, CRO). A custom token, on the other hand, is built on an existing blockchain platform, such as Ethereum, using a smart contract standard like the ERC-20 fungible token standard.

Video Explanation Of ERC-20 Prefabs

Here's a video explanation to help you better understand our new ERC-20 prefabs:

Call Custom Blockchain Tokens

Connect to any EVM-compatible blockchain by providing an RPC. All methods have an optional field to add an RPC URL. This returns a custom ERC-20 token's balance. If you'd like to get the balance of a native ERC-20 token, see Balance of Native Token.

using Web3Unity.Scripts.Library.Ethers.Providers;
using Web3Unity.Scripts.Library.Ethers.Contracts;
using UnityEngine;

public class ERC20CustomTokenBalance : MonoBehaviour
{
async void Start()
{
// abi in json format
string contractAbi = "YOUR_TOKEN_ABI";
// address of contract
string contractAddress = "YOUR_TOKEN_ADDRESS";
var provider = new JsonRpcProvider("YOUR_NODE");
var contract = new Contract(contractAbi, contractAddress, provider);
var calldata = await contract.Call("balanceOf", new object[]
{
PlayerPrefs.GetString("Account")
});
Debug.Log(calldata[0].ToString());
}
}
info

In the following code snippet examples, we will use Circle's USDC ERC-20 token contract as found on the Goerli testnet for demonstration purposes. We use "0xd25b827D92b0fd656A1c829933e9b0b836d5C3e2" as the example address to fetch from.

Balance Of Custom Token

Returns the balance of an ERC-20 token for a specific Ethereum account (e.g. "USDC").

using System.Numerics;
using Web3Unity.Scripts.Library.ETHEREUEM.EIP;
using UnityEngine;

public class ERC20BalanceOfExample : MonoBehaviour
{
async void Start()
{
string contract = "0x07865c6E87B9F70255377e024ace6630C1Eaa37F";
string account = "0xd25b827D92b0fd656A1c829933e9b0b836d5C3e2";

BigInteger balanceOf = await ERC20.BalanceOf(contract, account);
Debug.Log("Balance Of: " + balanceOf);
}
}

Balance Of Native Token

Returns the balance of a native token for a specific Ethereum account (e.g. "ETH").

using UnityEngine;
using Web3Unity.Scripts.Library.Ethers.Providers;

public class ERC20NativeBalanceOfExample : MonoBehaviour
{
async void Start()
{
string account = "0xd25b827D92b0fd656A1c829933e9b0b836d5C3e2";
var provider = new JsonRpcProvider("YOUR_NODE_HERE");
var getBalance = await provider.GetBalance(account);
Debug.Log("Account Balance: " + getBalance); // balance is returned in wei
}
}

Name

Returns the name of an ERC-20 token associated with a specified contract address (e.g. "USD Coin").

using Web3Unity.Scripts.Library.ETHEREUEM.EIP;
using UnityEngine;

public class ERC20NameExample : MonoBehaviour
{
async void Start()
{
string contract = "0x07865c6E87B9F70255377e024ace6630C1Eaa37F";
string name = await ERC20.Name(contract);
print(name);
}
}

Symbol

Returns the symbol of an ERC-20 token associated with a specified contract address (e.g. "USDC").

using UnityEngine;
using Web3Unity.Scripts.Library.ETHEREUEM.EIP;

public class ERC20SymbolExample : MonoBehaviour
{
async void Start()
{
string contract = "0x07865c6E87B9F70255377e024ace6630C1Eaa37F";

string symbol = await ERC20.Symbol(contract);
print(symbol);
}
}

Decimals

Returns the number of decimal places a specified ERC-20 token uses (e.g. 6 for USDC), which means to divide the token amount by 1000000 to get its human readable representation.

using System.Numerics;
using Web3Unity.Scripts.Library.ETHEREUEM.EIP;
using UnityEngine;

public class ERC20DecimalsExample : MonoBehaviour
{
async void Start()
{
string contract = "0x07865c6E87B9F70255377e024ace6630C1Eaa37F";

BigInteger decimals = await ERC20.Decimals(contract);
print(decimals);
}
}

Total Supply

Returns the total token supply of an ERC-20 token associated with a specified contract address.

using System.Numerics;
using Web3Unity.Scripts.Library.ETHEREUEM.EIP;
using UnityEngine;

public class ERC20TotalSupplyExample : MonoBehaviour
{
async void Start()
{
string contract = "0x07865c6E87B9F70255377e024ace6630C1Eaa37F";
BigInteger totalSupply = await ERC20.TotalSupply(contract);
print(totalSupply);
}
}

Convert WEI To ETH And ETH To WEI

The ConversionExamples class provides two methods to convert values between Ethereum's smallest unit (wei) and its largest unit (ether). The first method, ConvertToDecimals(), takes a string value representing an amount of wei and converts it to ether by dividing the wei value by 10^18. The result is then printed as a decimal string using the Convert.ToDecimal() method.

The second method, ConvertToWei(), takes a string value representing an amount of ether and converts it to wei by multiplying the ether value by 10^18. The result is then printed as a decimal string using the Convert.ToDecimal() method.

These conversion methods are useful for working with Ethereum amounts in code. By converting values between wei and ether, developers can manage Ethereum amounts with greater precision and accuracy.

using System;
using UnityEngine;

public class ConversionExamples : MonoBehaviour
{

public void ConvertToDecimals()
{
float wei = float.Parse("10123755");
float decimals = 1000000000000000000; // 18 decimals
float eth = wei / decimals;
print(Convert.ToDecimal(eth).ToString());
}

public void ConvertToWei()
{
float eth = float.Parse("0.1");
float decimals = 1000000000000000000; // 18 decimals
float wei = eth * decimals;
print(Convert.ToDecimal(wei).ToString());
}
}