Skip to content

Interacting with Smart Contracts Simplified

The SmartContract class in the Eidolon.Unity SDK provides a convenient way to interact with Ethereum smart contracts in Unity. It allows developers to send transactions and read data from smart contracts easily. This documentation will explain how to use the SmartContract class to interact with smart contracts.

Constructor

The SmartContract class has a constructor that initializes a new instance. You can use this constructor to create instances of smart contracts for different blockchains. Both single-chain and multi-chain constructors are dynamic and work even if mixed together in the same script. Feel free to creatively use this to create awesome experiences.

Read-Only Constructor

The below constructor only allows you to read data from a smart contract with either the default RPC set in the project settings or the RPC provider passed into the constructor. It’s the easiest to instantiate, but also provides you with the least amount of functionality.

This is a dynamic constructor that allows you to instantiate contracts on multiple networks at runtime.

SmartContract(string contractAddress, string abi, JsonRpcProvider provider = null)
  • contractAddress (string): The Ethereum address of the smart contract you want to interact with.
  • abi (string): The Application Binary Interface (ABI) of the smart contract, which defines its methods and data structures.
  • provider (JsonRpcProvider): The RPC (Remote Procedure Call) endpoint URL of the blockchain network.

Example:

// Reference to your contract
SmartContract myContract;
public void Awake()
{
// Initialize an instance of the SmartContract class using the default RPC
SmartContract myContract = new SmartContract("0xYourContractAddress", "YourContractABI");
// Initialize an instance of the SmartContract class using a custom RPC
SmartContract myContract = new SmartContract("0xYourContractAddress", "YourContractABI", new JsonRpcProvider("RpcUrlHere"));
}

Once your contract instance has been instantiated, you can use it to either make a Call or Send a Transaction. You can also instantiate as many contracts as you need.

Full Constructor (Read & Write)

In order to sign transactions, a wallet needs to be assigned to a Smart Contract. This Smart Contract constructor allows you to overwrite the default RPC by specifying an RPC within the constructor. This allows you to create multi-chain wallets, able to sign transactions on contracts all across the EVM (Ethereum Virtual Machine)!

When passing a Wallet to your Smart Contract constructor, it will inherit the RPC used by your Wallet object.

SmartContract(string contractAddress, string abi, IWallet wallet)
  • contractAddress (string): The Ethereum address of the smart contract you want to interact with.
  • abi (string): The Application Binary Interface (ABI) of the smart contract, which defines its methods and data structures.
  • wallet (IWallet): The wallet of choice that you would like to use for signing transactions.

Example:

// Create a new wallet instance using the default RPC set with the project settings.
// WebGL Example using the default RPC set with the project settings.
WebGLWallet myWallet = new Wallet();
// WebGL Override for using a custom RPC
WebGLWallet myWallet = new Wallet(new JsonRpcProvider("RpcUrlHere"));
// Desktop & Mobile using the default RPC set with the project settings.
EmbeddedWallet myWallet = new EmbeddedWallet("PrivateKeyHere");
// Desktop Override for using a custom chain
EmbeddedWallet myWallet = new EmbeddedWallet("PrivateKeyHere", "chainIdHere", new JsonRpcProvider("RpcUrlHere"));
// More wallets are available as extensions to Eidolon.Web3
// Pass the wallet to the constructor to let Eidolon know this wallet will be signing transactions.
SmartContract myContract = new SmartContract("myContractAddress","myContractABI", myWallet);

In the above we pass our Wallet to the Smart Contract constructor, which now inherits the RPC used by the Wallet.

SendTransaction Method

SendTransaction(string methodName, string gas = null, string gasPrice = null, string value = null, params object[] parameters)

This method sends a transaction to the smart contract to execute a specified method. Parameters like gas, gasPrice and value are optional

Parameters:

  • methodName (string): The name of the method to execute in the smart contract.
  • gas (optional) (string): The gas limit for the transaction.
  • gasPrice (optional) (string): The gas price for the transaction.
  • value (optional) (string): The value for chain native tokens.
  • parameters (object[], optional): Any parameters required by the method.

Returns:

  • string: The transaction hash if the transaction is successful, or null if it fails.

Example:

string methodName = "YourMethod";
object[] arguments = { argument1, argument2 };
string transactionHash = await contract.SendTransaction(methodName, parameters: arguments);

Call Method

Call<T>(string methodName, object[] parameters = null)

This method reads data from the smart contract by executing a specified method.

Parameters:

  • methodName (string): The name of the method to execute in the smart contract.
  • parameters (object[], optional): Any parameters required by the method.

Returns:

  • T: The result of the method call.

Example:

string methodName = "YourMethod";
object[] parameters = { parameter1, parameter2 };
T result = await contract.Call<T>(methodName, parameters);

Example Usage

Here’s an example of how to use the SmartContract class to interact with a smart contract. We’ll use a WebGLWallet for this example.

Instantiate Contracts

SmartContract Analytics;
SmartContract Token;
WebGLWallet myWallet;
private void Awake()
{
// Create a new wallet instance
myWallet = new WebGLWallet();
// Contract A (We pass our wallet object to this contract which enables us to send a transaction)
Token = new SmartContract("0xYourContractAddress", "YourContractABI", myWallet);
// Contract B (Only used to read data, so we don't have to pass it a wallet object)
Analytics = new SmartContract("0xYourContractAddress", "YourContractABI");
}

Contract Write

In the below example we create our transaction body and then send a transaction to the users default browser wallet. They can either choose to accept or decline the transaction.

public async void TransferTokens()
{
// Method
string methodName = "transfer";
// Arguments
string to = "0x28aAFd3dD531A09FE223DebaFFFCf6ddD028dF4F";
BigInteger amount = 10000;
// Argument Object
object[] arguments = new object[] { to, amount };
// Send the transaction by only passing the method and parameters through the use of Named arguments.
var transactionHash = await Token.SendTransaction(methodName, parameters: arguments);
Debug.Log("Transaction Hash: " + transactionHash);
}

Contract Read

In the below example we read data from a smart contract by calling the totalClaimed contract method.

public async void GetTotalClaimed()
{
// Method
string methodName = "totalClaimed";
// Call
var balance = await Analytics.Call<BigInteger>(methodName);
// Result
Debug.Log("Total Tokens Claimed: " + balance);
}