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.
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:
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.
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:
In the above we pass our Wallet
to the Smart Contract
constructor, which now inherits the RPC used by the Wallet.
SendTransaction Method
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, ornull
if it fails.
Example:
Call Method
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:
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
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.
Contract Read
In the below example we read data from a smart contract by calling the totalClaimed
contract method.