The Eidolon Util Class
The Util
class in the Eidolon.Unity SDK provides utility methods for Ethereum-related conversions and handling web requests in Unity WebGL.
Methods
ConvertToWei
Description
This method converts a value from Ether to Wei. This is primarily used for Desktop and Mobile builds and you’ll often use it to convert normal integers into blockchain-ready values.
Example Code
// Default is 18 decimalsBigInteger valueInWei = Util.ConvertToWei(10);
// Override default by specifying the amount of decimals. In this case, it's 8.BigInteger valueInWei = Util.ConvertToWei(10, 8);
Parameters
value
(BigInteger): A value in Ether.decimals
(optional): The amount of decimals a token has.
Returns
BigInteger
: The value in Wei.
ConvertFromWei
Description
This method converts a value from Wei to Ether.
Example Code
// Default is 18 decimalsBigDecimal valueInEther = Util.ConvertFromWei("1000000000000000000");
// Override default by specifying the amount of decimals. In this case, it's 8.BigDecimal valueInEther = Util.ConvertFromWei("1000000000000000000", 8);
Parameters
value
(BigInteger): A value in Wei.decimals
(optional): The amount of decimals a token has.
Returns
BigDecimal
: The value in Ether.
ConvertBigIntToDecimal
Description
This method converts a BigInteger to a decimal number.
Example Code
decimal decimalValue = Util.ConvertBigIntToDecimal(BigInteger.Parse("1000000000000000000"));
Parameters
value
(BigInteger): A value in BigInteger format.decimals
(optional): The amount of decimals you want to divide by.
Returns
decimal
: The value in decimal format.
ConvertStringToHex
Description
This method converts a string value to a hexadecimal string.
Example Code
string hexString = Util.ConvertStringToHex("10");
Parameters
value
(String): A string value.
Returns
string
: A hexadecimal string.
ConvertDecimalToHex
Description
This method converts a decimal value to a hexadecimal string.
Example Code
// Returns "0x1"string hexChainId = Util.ConvertDecimalToHex(1);
Parameters
value
(BigInteger): A BigInteger value.
Returns
string
: A hexadecimal string.
ConvertHexToDecimal
Description
This method converts a hex value to a string decimal.
Example Code
// Returns "1"string decimalChainId = Util.ConvertHexToDecimal("0x1");
Parameters
value
(string): A string hexadecimal value.
Returns
string
: A decimal string.
ConvertHexStringToBigInteger
Description
This method converts a hexadecimal string to a big integer.
Example Code
BigInteger value = Util.ConvertHexStringBigInteger("1A");
Parameters
hexString
(string): A hexadecimal string.
Returns
BigInteger
: A big integer.
IsValidEthereumAddress
Description
This method checks if a provided string is a valid Ethereum address in the correct format.
Example Code
bool isValid = Util.IsValidEthereumAddress("0x742d35Cc6634C0532925a3b844Bc454e4438f44e");
Parameters
address
(string): An Ethereum address.
Returns
bool
:true
if the address is valid; otherwise,false
.
ConvertLink
Description
This method converts a given URL to a WebGL-ready URL using a CORS proxy. You can use this method to get around CORS errors when interacting with Web Requests.
Example Code
string modifiedUrl = Util.ConvertLink("https://example.com/image.png");
Parameters
url
(string): The original URL.
Returns
string
: A modified URL that is WebGL-ready.
GetTextureFromURL
Description
This method fetches a Texture2D
from a provided URL and invokes the given callback with the fetched texture. You can use this to fetch NFT images and apply them to in-game objects.
Example Code
Util.GetTextureFromURL("https://example.com/image.png", (texture) => { // Handle the fetched texture here. myRawImage.texture = texture;});
Parameters
url
(string): The URL to fetch the texture from.
GetTransactionLogs
Description
This method retrieves the transaction properties from a receipt.
Example Code
// Insantiate a new RPCJsonRpcProvider provider = new JsonRpcProvider();
// The transaction hash in questionvar transactionHash = "0xTransactionHash";
// Get the transaction receipt from the hashTransactionReceipt transactionReceipt = provider.GetTransactionReceipt(transactionHash);
// Fetch the receipt filters and return them in an arrayvar transactionLogs = await Util.GetTransactionLogs(transactionReceipt);
// Iterate through the receipt object to fetch various bits of dataforeach (var item in transactionLogs){ Debug.Log("Transaction data: " + item.Data);}
Parameters
transactionHash
(string): The hash of the transaction to retrieve.
Returns
Transaction Properties
(FilterLogs): The transaction logs in a list format.
Encode Function Data
Description
This method encodes the data for a provided smart contract function.
Example Code
// Contract objectSmartContract contract = new SmartContract("ContractAddress", "ContractABI");
// Contract function datastring method = "mint";string amount = "100";
// Encode the datastring data = Util.EncodeFunctionData(contract, method, new object[] { amount });
// returns 0x55241077000000....
Parameters
contract
(SmartContract): The smart contractmethodName
(String): The method nameparameters
(object): The method arguments
Returns
- `TransactionInput (String): The transaction input to send off to the blockchain.