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 ProviderJsonRpcProvider 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 numberBlockWithTransactions 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 hashTransaction 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 hashTransactionReceipt 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 hashstring transactionStatus = await customProvider.GetTransactionStatus(transactionHashToCheck);Debug.Log("Transaction Status: " + transactionStatus);
Parameters
transactionHash
(string): The hash of the transaction to check.
Returns
string
:True
orFalse
. -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 checkdecimal 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 checkBigInteger 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.
GetFeeData
Description
Calculates EIP-1559 gas fees according to the Ethereum specification. Base fee is set by the protocol and priority fee is market driven. Includes fallback to legacy gas pricing for non-EIP-1559 chains.
Example Code
private JsonRpcProvider provider;
private void Awake(){ provider = new JsonRpcProvider("YOUR_RPC_URL");}
private async void Start(){ try { // Get gas fees with default multipliers var (priorityFee, maxFee) = await provider.GetFeeData(); Debug.Log($"Priority Fee: {priorityFee}, Max Fee: {maxFee}");
// Get gas fees with custom multipliers for urgent transactions var (urgentPriorityFee, urgentMaxFee) = await provider.GetFeeData( priorityMultiplier: 2.0m, baseFeeMultiplier: 2.5m ); Debug.Log($"Urgent Priority Fee: {urgentPriorityFee}, Urgent Max Fee: {urgentMaxFee}"); } catch (Exception ex) { Debug.LogError($"Failed to get gas fees: {ex.Message}"); }}
Parameters
priorityMultiplier
(decimal): Optional multiplier for the priority fee. Default is 1.0. Higher values increase transaction priority.baseFeeMultiplier
(decimal): Optional multiplier for the base fee. Default is 2.0. Higher values ensure inclusion during network congestion.
Returns
A tuple containing:
MaxPriorityFeePerGas
(HexBigInteger): Maximum priority fee per gas unit.MaxFeePerGas
(HexBigInteger): Maximum total fee per gas unit.