Skip to content

Streamlined Player Onboarding with Eidolon

In this section we’ll look at how we can use the WebGLWallet class to interact with browser based wallets like MetaMask, TrustWallet and more. We’ll instantiate a new wallet and connect our player to a specified network.

Step 1: Instantiate a Wallet

To create a new wallet instance, follow these steps:

using UnityEngine;
using System.Numerics;
using Nethereum.Web3;
public class WalletExample : MonoBehaviour
{
// Reference to your wallet provider
WebGLWallet wallet;
// Reference to our smart contract
SmartContract contract;
public void Awake()
{
// Instantiate a new wallet using the Wallet class
wallet = new WebGLWallet();
// Instantiate a new smart contract and pass our wallet to the constructor
contract = new SmartContract("contractAddress", "ContractABI", wallet)
}
}

In the example above, we instantiate a new wallet and smart contract instance by providing the contract address and ABI of the smart contract we want to interact with. We then pass the wallet in the smart contract constructor to let Eidolon know we want to sign transactions with that wallet.

Step 2: Connect to a Network

Now, let’s connect the player to an example network, in this case, the SKALE Chaos Test Network. We will set the required network-specific information.

private void ConnectToNetwork()
{
string chainId = "1351057110"; // SKALE Calypso Test Network
// Connect to the SKALE Network and retrieve the connected account address
string account = wallet.Connect(chainId);
}

In this step, we set up the connection to the SKALE Calypso Test Network by providing the chain ID.

Step 3: Send a Test Transaction

Let’s send a test transaction to a smart contract using the wallet and smart contract instance. We use Named arguments to pass only the method name and arguments. You can however adjust the gas, gasPrice, value and nonce if required.

private void SendTransactionToSmartContract()
{
string methodName = "transfer"; // Replace with the desired method name
object[] arguments = new object[] { "0xAddressTo", 100 }; // Example parameters
// Send a transaction to a smart contract method
contract.SendTransaction(methodName, parameters: arguments);
}

Here, we send a test transaction by calling a method on a smart contract with the provided parameters.

Step 5: Read Data from Smart Contract

To read data from a smart contract, use the Call method asynchronously. In this example, we read the balance of an address.

private async void ReadDataFromSmartContract()
{
string methodName = "balanceOf"; // Replace with the desired method name
object[] parameters = new object[] { "0xAddress" }; // Example parameters
BigInteger result = await contract.Call<BigInteger>(methodName, parameters);
}

In this step, we asynchronously read data from the smart contract and handle the result when the task is completed.

Conclusion

By following these steps, you can successfully use the Wallet class to create a new wallet, connect a player to any EVM network, and interact with smart contracts in your Unity project. This allows you to integrate blockchain functionality into your game seamlessly.