Skip to content

ERC-20

The ERC20 class in the Eidolon.Unity SDK provides a flexible way to interact with ERC-20 tokens on the Ethereum blockchain. Developers can choose to create an instance of the ERC20 class with a custom contract address to interact with a specific ERC-20 token.

Instantiating ERC20

Here’s how to instantiate an ERC20 contract:

public ERC20 myToken;
public void Start()
{
// You can manually override the global RPC by specifying an RPC in the constructor.
// Custom RPC Provider
JsonRpcProvider provider = new JsonRpcProvider("customRpcHere");
myToken = new ERC20("contractAddress", provider);
}

In the example above, you can create an instance of ERC20 by specifying the contract address. Once you have an instance, you can use its methods to interact with the ERC-20 token.

Using ERC20

Now that we have our instance, let’s use it to make a BalanceOf call:

using System;
using System.Numerics;
using System.Threading.Tasks;
using UnityEngine;
// Instantiate ERC20 with a custom contract address
public ERC20 myToken = new ERC20("contractAddress");
public async void CheckBalance(string walletAddress)
{
try
{
BigInteger balance = await myToken.BalanceOf(walletAddress);
Debug.Log("Token Balance: " + balance);
}
catch (Exception ex)
{
Debug.LogError("Error: " + ex.Message);
}
}

In the example above, an instance of ERC20 is created with a custom contract address. You can then use this instance to call methods like BalanceOf, Allowance, Name, Symbol, TotalSupply, and Decimals to interact with the ERC-20 token.

ERC20

The ERC20 class provides methods for both reading and writing data to an ERC-20 contract on the Ethereum blockchain.

Read Methods

BalanceOf

Description

This method returns the balance of a user for an ERC-20 contract.

Example Code

string userAddress = "0xUserAddress"; // Replace with the user's address
BigInteger balance = await myToken.BalanceOf(userAddress);
Debug.Log("User Balance: " + balance);

Parameters

  • user (string): The Ethereum wallet address for which to retrieve the token balance.

Returns

  • BigInteger: The balance of tokens for the specified wallet address.

FormattedBalanceOf

Description

This method returns the formatted balance of a user for an ERC-20 contract. Optionally, it includes the token symbol.

Example Code

string userAddress = "0xUserAddress"; // Replace with the user's address
string formattedBalance = await myToken.FormattedBalanceOf(userAddress, true);
Debug.Log("Formatted Balance: " + formattedBalance);

Parameters

  • user (string): The Ethereum wallet address for which to retrieve the token balance.
  • withSymbol (bool, optional): Whether to include the token symbol in the formatted balance.

Returns

  • string: The formatted balance of tokens for the specified wallet address.

Allowance

Description

This method retrieves the allowance of tokens that one address (owner) has granted to another address (spender) for spending tokens on their behalf.

Example Code

string ownerAddress = "0xOwnerAddress"; // Replace with the owner's address
string spenderAddress = "0xSpenderAddress"; // Replace with the spender's address
BigInteger allowance = await myToken.Allowance(ownerAddress, spenderAddress);
Debug.Log("Allowance: " + allowance);

Parameters

  • owner (string): The owner’s Ethereum wallet address.
  • spender (string): The spender’s Ethereum wallet address.

Returns

  • BigInteger: The allowance of tokens granted by the owner to the spender.

Name

Description

This method retrieves the name of the ERC-20 token.

Example Code

string tokenName = await myToken.Name();
Debug.Log("Token Name: " + tokenName);

Returns

  • string: The name of the ERC-20 token.

Symbol

Description

This method retrieves the symbol of the ERC-20 token.

Example Code

string tokenSymbol = await myToken.Symbol();
Debug.Log("Token Symbol: " + tokenSymbol);

Returns

  • string: The symbol of the ERC-20 token.

TotalSupply

Description

This method retrieves the total supply of the ERC-20 token.

Example Code

BigInteger totalSupply = await myToken.TotalSupply();
Debug.Log("Total Supply: " + totalSupply);

Returns

  • BigInteger: The total supply of the ERC-20 token.

Decimals

Description

This method retrieves the number of decimal places used by the ERC-20 token.

Example Code

BigInteger decimals = await myToken.Decimals();
Debug.Log("Token Decimals: " + decimals);

Returns

  • BigInteger: The number of decimal places used by the ERC-20 token.

Write Methods

Eidolon allows you to send transactions directly from an ERC20 instance through the use of a Wallet. You can either create a new Wallet or use an existing one.

Example Code

// We'll use an EmbeddedWallet for this example
EmbeddedWalletWallet myWallet = new EmbeddedWalletWallet("PrivateKeyHere");
// We pass the wallet object in the constructor to let Eidolon know this wallet will be used for transactions
ERC20 myToken = new ERC20("contractAddress", myWallet);
public void Transfer()
{
// Send a transaction with built-in ERC20 functions.
string transactionHash = await myToken.Transfer(recipientAddress, amount);
}

Let’s have a look at the built-in functions available to us:

Transfer

Description

This method initiates a transfer of tokens from the caller’s address to the specified recipient.

Example Code

string recipientAddress = "0xRecipientAddress"; // Replace with the recipient's address
BigInteger amount = 100; // Replace with the amount of tokens to transfer
string transactionHash = await myToken.Transfer(recipientAddress, amount);
Debug.Log("Transfer Transaction Hash: " + transactionHash);

Parameters

  • to (string): The Ethereum wallet address of the recipient.
  • amount (BigInteger): The amount of tokens to transfer.
  • gas (string, optional): The gas limit for the transaction.
  • gasPrice (string, optional): The gas price for the transaction.

Returns

  • string: The transaction hash of the transfer.

Approve

Description

This method approves the spender to spend a specified amount of tokens on behalf of the owner.

Example Code

string spenderAddress = "0xSpenderAddress"; // Replace with the spender's address
BigInteger amount = 100; // Replace with the amount of tokens to approve
string transactionHash = await myToken.Approve(spenderAddress, amount);
Debug.Log("Approval Transaction Hash: " + transactionHash);

Parameters

  • spender (string): The Ethereum wallet address of the spender.
  • amount (BigInteger): The amount of tokens to approve.
  • gas (string, optional): The gas limit for the transaction.
  • gasPrice (string, optional): The gas price for the transaction.

Returns

  • string: The transaction hash of the approval.

TransferFrom

Description

This method allows the spender to transfer a specified amount of tokens from the owner’s address to the recipient’s address.

Example Code

string ownerAddress = "0xOwnerAddress"; // Replace with the owner's address
string recipientAddress = "0xRecipientAddress"; // Replace with the recipient's address
BigInteger amount = 100; // Replace with the amount of tokens to transfer
string transactionHash = await myToken.TransferFrom(ownerAddress, recipientAddress, amount);
Debug.Log("Transfer From Transaction Hash: " + transactionHash);

Parameters

  • from (string): The Ethereum wallet address of the owner.
  • to (string): The Ethereum wallet address of the recipient.
  • amount (BigInteger): The amount of tokens to transfer.
  • gas (string, optional): The gas limit for the transaction.
  • gasPrice (string, optional): The gas price for the transaction.

Returns

  • string: The transaction hash of the transfer from.

IncreaseAllowance

Description

This method increases the amount of tokens that the spender is allowed to spend on behalf of the owner.

Example Code

string spenderAddress = "0xSpenderAddress"; // Replace with the spender's address
BigInteger addedValue = 50; // Replace with the additional amount of tokens to allow
string transactionHash = await myToken.IncreaseAllowance(spenderAddress, addedValue);
Debug.Log("Increase Allowance Transaction Hash: " + transactionHash);

Parameters

  • spender (string): The Ethereum wallet address of the spender.
  • addedValue (BigInteger): The additional amount of tokens to allow.
  • gas (string, optional): The gas limit for the transaction.
  • gasPrice (string, optional): The gas price for the transaction.

Returns

  • string: The transaction hash of the increase allowance.

DecreaseAllowance

Description

This method decreases the amount of tokens that the spender is allowed to spend on behalf of the owner.

Example Code

string spenderAddress = "0xSpenderAddress"; // Replace with the spender's address
BigInteger subtractedValue = 30; // Replace with the amount of tokens to decrease
string transactionHash = await myToken.DecreaseAllowance(spenderAddress, subtractedValue);
Debug.Log("Decrease Allowance Transaction Hash: " + transactionHash);

Parameters

  • spender (string): The Ethereum wallet address of the spender.
  • subtractedValue (BigInteger): The amount of tokens to decrease.
  • gas (string, optional): The gas limit for the transaction.
  • gasPrice (string, optional): The gas price for the transaction.

Returns

  • string: The transaction hash of the decrease allowance.