Skip to content

Provider

The JsonRpcProvider class in the Eidolon.Unity SDK is a class that provides access to the Ethereum blockchain via a JSON-RPC (Remote Procedure Call) Protocol. It provides a simplified interface for developers to interact with the blockchain from within Unity.

Create a custom provider

You can create a custom provider using your own RPC URL by doing the following:

// Declare a new Provider
JsonRpcProvider customProvider = new JsonRpcProvider("https://yourRpcHere");

JsonRpcProvider Methods

After going through the initial project setup, you can utilize any of the below provider methods anywhere in your Unity project.


GetBlock

Description

This method retrieves the latest block number on the blockchain.

Example Code
BigInteger latestBlock = await customProvider.GetBlock();
Debug.Log("Latest Block Number: " + latestBlock);
Parameters

None

Returns
  • BigInteger: The latest block number.

GetBlockWithTransactions

Description

This method fetches a block and provides access to an array of block properties, including transaction information.

Example Code
string blockNumberToFetch = "12345"; // Replace with the desired block number
BlockWithTransactions blockData = await customProvider.GetBlockWithTransactions(blockNumberToFetch);
Debug.Log("Block Data: " + blockData);
Parameters
  • blockNumber (string): The block number to fetch.
Returns
  • BlockWithTransactions: A block object containing transaction information.

GetChainId

Description

This method returns the current chain ID for a given provider

Example Code
BigInteger chainId = customProvider.GetChainId();
Parameters

None

Returns
  • BigInteger: The current chain ID.

GetTransaction

Description

This method retrieves a transaction by its hash and allows you to access transaction data like the Transaction Nonce, Gas, Input etc.

Example Code
string transactionHashToRetrieve = "0xTransactionHash"; // Replace with the transaction hash
Transaction transaction = await customProvider.GetTransaction(transactionHashToRetrieve);
Debug.Log("Transaction Gas: " + transaction.Gas);
Parameters
  • transactionHash (string): The hash of the transaction to retrieve.
Returns
  • Transaction: The transaction details.

GetTransactionReceipt

Description

This method retrieves a transaction receipt by its hash and allows you to access transaction data like the logs, status, Input etc.

Example Code
string transactionHashToRetrieve = "0xTransactionHash"; // Replace with the transaction hash
TransactionReceipt transaction = await customProvider.GetTransactionReceipt(transactionHashToRetrieve);
Debug.Log("Transaction Blockhash: " + transaction.BlockHash);
Parameters
  • transactionHash (string): The hash of the transaction to retrieve.
Returns
  • TransactionReceipt: The transaction receipt.

GetTransactionStatus

Description

This method retrieves the status of a transaction by its hash.

Example Code
string transactionHashToCheck = "0xTransactionHash"; // Replace with the transaction hash
string transactionStatus = await customProvider.GetTransactionStatus(transactionHashToCheck);
Debug.Log("Transaction Status: " + transactionStatus);
Parameters
  • transactionHash (string): The hash of the transaction to check.
Returns
  • string: True or False. - True: Transaction has been successful. - False: Transaction has failed.

GetBalance

Description

This method retrieves the native chain balance (in Ether) for a specified wallet address.

Example Code
string walletAddressToCheck = "0xYourWalletAddress"; // Replace with the wallet address to check
decimal balance = await customProvider.GetBalance(walletAddressToCheck);
Debug.Log("Wallet Balance: " + balance);
Parameters
  • address (string): The Ethereum wallet address for which to retrieve the balance.
Returns
  • decimal: The balance in Ether.

GetTransactionCount

Description

This method retrieves the total transaction count for a designated wallet address.

Example Code
string walletAddressToCheck = "0xYourWalletAddress"; // Replace with the wallet address to check
BigInteger transactionCount = await customProvider.GetTransactionCount(walletAddressToCheck);
Debug.Log("Transaction Count: " + transactionCount);
Parameters
  • address (string): The Ethereum wallet address for which to retrieve the transaction count.
Returns
  • BigInteger: The total transaction count.

LookupAddress

Description

This method resolves the address for an ENS name asynchronously.

Example Code
string walletAddressToCheck = "0xYourWalletAddress";
string Url = await customProvider.LookupAddress(walletAddressToCheck);
Debug.Log("ENS URL: " + Url);
Parameters
  • address (string): The Ethereum wallet address to lookup.
Returns
  • string: The ENS Name for the provided address.

ResolveURL

Description

This method resolves the URL for an ENS name asynchronously.

Example Code
string walletAddressToCheck = "luc.eth";
string ensName = await customProvider.LookupAddress(walletAddressToCheck);
Debug.Log("ENS Name: " + ensName);
Parameters
  • address (string): The ENS address to lookup.
Returns
  • string: The URL for the provided ENS Name.

EstimateGas

Description

This method estimates gas for a transaction asynchronously.

Example Code
public JsonRpcProvider provider;
public SmartContract contract;
private void Awake()
{
provider = new JsonRpcProvider();
contract = new SmartContract("contractAddress", "contractABI");
}
private async void Start()
{
// Encode function data
string data = Util.EncodeFunctionData(contract, "MethodName", new object[]{"exampleArgument"});
// Estimate gas using transction input
var gasCost = await provider.EstimateGas("FromAddress", contract.Address(), data: data);
Debug.Log(gasCost);
}
Parameters
  • address (string): The ENS address to lookup.
Returns
  • string: The URL for the provided ENS Name.