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:
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:
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
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
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
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
Returns
string
: The name of the ERC-20 token.
Symbol
Description
This method retrieves the symbol of the ERC-20 token.
Example Code
Returns
string
: The symbol of the ERC-20 token.
TotalSupply
Description
This method retrieves the total supply of the ERC-20 token.
Example Code
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
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
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
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
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
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
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
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.