Skip to content

ERC-1155

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

Instantiating ERC1155

Instantiate an ERC1155 contract using one of the provided constructors:

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

Using ERC1155

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 ERC1155 with a custom contract address
public ERC1155 myToken = new ERC1155("contractAddress");
public async void CheckBalance(string walletAddress, BigInteger tokenId)
{
try
{
BigInteger balance = await myToken.BalanceOf(walletAddress, tokenId);
Debug.Log("Token Balance: " + balance);
}
catch (Exception ex)
{
Debug.LogError("Error: " + ex.Message);
}
}

In this example, we create an instance of ERC1155 with a custom contract address. Then, we use this instance to call methods like BalanceOf, BalanceOfBatch, IsApprovedForAll, and TokenURI to interact with the ERC-1155 token.

ERC1155

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

Read Methods

BalanceOf

Description

This method returns the balance of a specific ERC-1155 token owned by a given Ethereum wallet address.

Example Code

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

Parameters

  • owner (string): The Ethereum wallet address of the owner.
  • tokenId (BigInteger): The ID of the ERC-1155 token.

Returns

  • BigInteger: The balance of the specified ERC-1155 token owned by the specified wallet address.

BalanceOfBatch

Description

This method returns the balances of multiple ERC-1155 tokens owned by multiple Ethereum wallet addresses.

Example Code

string[] owners = { "0xOwner1", "0xOwner2" }; // Replace with the owner addresses
BigInteger[] tokenIds = { 123, 456 }; // Replace with the token IDs
BigInteger[] balances = await myToken.BalanceOfBatch(owners, tokenIds);
Debug.Log("Token Balances: " + string.Join(", ", balances));

Parameters

  • owners (string[]): An array of Ethereum wallet addresses.
  • tokenIds (BigInteger[]): An array of ERC-1155 token IDs.

Returns

  • BigInteger[]: An array of balances corresponding to the specified ERC-1155 tokens and wallet addresses.

IsApprovedForAll

Description

This method checks if an operator is approved to manage all ERC-1155 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 ERC-1155 tokens of the owner; otherwise, false.

TokenURI

Description

This method retrieves the Uniform Resource Identifier (URI) for a specific ERC-1155 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 ERC-1155 token.

Returns

  • string: The URI for the specified ERC-1155 token.

GetTokenMetadata

Description

This method retrieves the metadata for a specific ERC-1155 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-1155 token.

Returns

  • Metadata: An object containing metadata details.

Write Methods

Eidolon allows you to send transactions directly from an ERC1155 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
ERC1155 myToken = new ERC1155("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:

SetApprovalForAll

Description

This method sets or revokes approval for an operator to manage all ERC-1155 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.

SafeTransferFrom

Description

This method safely transfers a specified amount of ERC-1155 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
uint tokenId = 123; // Replace with the token ID
uint amount = 1; // Replace with the amount of tokens to transfer
byte[] data = Encoding.UTF8.GetBytes("AdditionalDataHere");
string transactionHash = await myToken.SafeTransferFrom(fromAddress, toAddress, tokenId, amount, data);
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 (uint): The ID of the ERC-1155 token.
  • amount (uint): The amount of tokens to transfer.
  • 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.

SafeBatchTransferFrom

Description

This method safely transfers specified amounts of multiple ERC-1155 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
uint[] tokenIds = { 123, 456 }; // Replace with the array of token IDs
uint[] amounts = { 1, 2 }; // Replace with the array of amounts
byte[] data = Encoding.UTF8.GetBytes("AdditionalDataHere");
string transactionHash = await myToken.SafeBatchTransferFrom(fromAddress, toAddress, tokenIds, amounts, data);
Debug.Log("Safe Batch Transfer Transaction Hash: " + transactionHash);

Parameters

  • from (string): The Ethereum wallet address of the owner.
  • to (string): The Ethereum wallet address of the recipient.
  • tokenIds (uint[]): The array of ERC-1155 token IDs.
  • amounts (uint[]): The array of amounts corresponding to each token ID.
  • 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 batch transfer.