Skip to content

ERC-721

The ERC721 class in the Eidolon.Unity SDK facilitates interaction with ERC-721 tokens on the Ethereum blockchain. Users can create an instance of the ERC721 class with a custom contract address to interact with a specific ERC-721 token.

Instantiating ERC721

Instantiate an ERC721 contract using one of the provided constructors:

public ERC721 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 ERC721("contractAddress", provider);
}

Using ERC721

Now that we have our instance, let’s utilize it for a BalanceOf call:

using System;
using System.Numerics;
using System.Threading.Tasks;
using UnityEngine;
// Instantiate ERC721 with a custom contract address
public ERC721 myToken = new ERC721("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 this example, we create an instance of ERC721 with a custom contract address. Then, we use this instance to call methods like BalanceOf, GetApproved, IsApprovedForAll, Name, OwnerOf, Symbol, and TokenURI to interact with the ERC-721 token.

ERC721

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

Read Methods

BalanceOf

Description

This method returns the balance of tokens owned by a specific Ethereum wallet address for an ERC-721 contract.

Example Code

string ownerAddress = "0xOwnerAddress"; // Replace with the owner's address
BigInteger balance = await myToken.BalanceOf(ownerAddress);
Debug.Log("Token Balance: " + balance);

Parameters

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

Returns

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

GetApproved

Description

This method retrieves the approved address for a specific token.

Example Code

BigInteger tokenId = 123; // Replace with the token ID
string approvedAddress = await myToken.GetApproved(tokenId);
Debug.Log("Approved Address: " + approvedAddress);

Parameters

  • tokenId (BigInteger): The ID of the token.

Returns

  • string: The address approved to manage the specified token.

IsApprovedForAll

Description

This method checks if an operator is approved to manage all tokens on behalf of the owner.

Example Code

string ownerAddress = "0xOwnerAddress"; // Replace with the owner's address
string operatorAddress = "0xOperatorAddress"; // Replace with the operator's address
bool isApproved = await myToken.IsApprovedForAll(ownerAddress, operatorAddress);
Debug.Log("Is Approved For All: " + isApproved);

Parameters

  • owner (string): The Ethereum wallet address of the owner.
  • operatorAddress (string): The Ethereum wallet address of the operator.

Returns

  • bool: true if the operator is approved for all tokens of the owner; otherwise, false.

Name

Description

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

Example Code

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

Returns

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

OwnerOf

Description

This method retrieves the owner of a specific token.

Example Code

BigInteger tokenId = 123; // Replace with the token ID
string ownerAddress = await myToken.OwnerOf(tokenId);
Debug.Log("Token Owner: " + ownerAddress);

Parameters

  • tokenId (BigInteger): The ID of the token.

Returns

  • string: The Ethereum wallet address of the token owner.

Symbol

Description

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

Example Code

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

Returns

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

TokenURI

Description

This method retrieves the Uniform Resource Identifier (URI) for a specific token.

Example Code

BigInteger tokenId = 123; // Replace with the token ID
string tokenUri = await myToken.TokenURI(tokenId);
Debug.Log("Token URI: " + tokenUri);

Parameters

  • tokenId (BigInteger): The ID of the token.

Returns

  • string: The URI for the specified token.

GetTokenMetadata

Description

This method retrieves the metadata for a specific ERC-721 token, including details such as name, description, and image URL.

Example Code

BigInteger tokenId = 123; // Replace with the token ID
Metadata metadata = await myToken.GetTokenMetadata(tokenId);
Debug.Log("Token Metadata: " + metadata.Name + ", " + metadata.Description + ", " + metadata.ImageUrl);

Parameters

  • tokenId (BigInteger): The ID of the ERC-721 token.

Returns

  • Metadata: An object containing metadata details.

Write Methods

Eidolon allows you to send transactions directly from an ERC721 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
ERC721 myToken = new ERC721("contractAddress", myWallet);
public void Transfer()
{
// Send a transaction with built-in ERC721 functions.
string transactionHash = await myToken.Transfer(recipientAddress, amount);
}

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

Approve

Description

This method approves an address to manage the specified token.

Example Code

string toAddress = "0xToAddress"; // Replace with the recipient's address
BigInteger tokenId = 123; // Replace with the token ID
string transactionHash = await myToken.Approve(toAddress, tokenId);
Debug.Log("Approval Transaction Hash: " + transactionHash);

Parameters

  • to (string): The Ethereum wallet address of the recipient.

  • tokenId (BigInteger): The ID of the token.

  • 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 fromAddress = "0xFromAddress"; // Replace with the sender's address
string toAddress = "0xToAddress"; // Replace with the recipient's address
BigInteger tokenId = 123; // Replace with the token ID
string transactionHash = await myToken.TransferFrom(fromAddress, toAddress, tokenId);
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.
  • tokenId (BigInteger): The ID of the token.
  • 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.

SafeTransferFrom

Description

This method safely transfers a specified amount of tokens from the owner’s address to the recipient’s address.

Example Code

string fromAddress = "0xFromAddress"; // Replace with the sender's address
string toAddress = "0xToAddress"; // Replace with the recipient's address
BigInteger tokenId = 123; // Replace with the token ID
string transactionHash = await myToken.SafeTransferFrom(fromAddress, toAddress, tokenId);
Debug.Log("Safe Transfer Transaction Hash: " + transactionHash);

Parameters

  • from (string): The Ethereum wallet address of the owner.
  • to (string): The Ethereum wallet address of the recipient.
  • tokenId (BigInteger): The ID of the token.
  • 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 safe transfer.

SafeTransferFromWithData

Description

This method safely transfers a specified amount of tokens from the owner’s address to the recipient’s address with additional data.

Example Code

string fromAddress = "0xFromAddress"; // Replace with the sender's address
string toAddress = "0xToAddress"; // Replace with the recipient's address
BigInteger tokenId = 123; // Replace with the token ID
byte[] data = Encoding.UTF8.GetBytes("AdditionalDataHere");
string transactionHash = await myToken.SafeTransferFromWithData(fromAddress, toAddress, tokenId, data);
Debug.Log("Safe Transfer with Data Transaction Hash: " + transactionHash);

Parameters

  • from (string): The Ethereum wallet address of the owner.
  • to (string): The Ethereum wallet address of the recipient.
  • tokenId (BigInteger): The ID of the token.
  • data (byte[]): Additional data for the transaction.
  • 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 safe transfer with data.

SetApprovalForAll

Description

This method sets or revokes approval for the operator to manage all tokens on behalf of the owner.

Example Code

string operatorAddress = "0xOperatorAddress"; // Replace with the operator's address
bool approved = true; // Replace with `true` to set approval, `false` to revoke
string transactionHash = await myToken.SetApprovalForAll(operatorAddress, approved);
Debug.Log("Set Approval For All Transaction Hash: " + transactionHash);

Parameters

  • operatorAddress (string): The Ethereum wallet address of the operator.
  • approved (bool): true to set approval, false to revoke.
  • 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 set approval for all.