How to Integrate with the Ethereum Blockchain with Magic in Unity

How to Integrate with the Ethereum Blockchain with Magic in Unity

#Installation

To interact with the Ethereum blockchain, Magic Unity SDK integrates Nethereum as sub dependency.

#Initializing Provider

using link.magic.unity.sdk;
using UnityEngine;

public class MagicUnity : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Magic magic = new Magic("YOUR_PUBLISHABLE_KEY");
        Magic.Instance = magic;
    }
}

#Use Different Networks

#Choose Different Testnet

Magic magic = new Magic("YOUR_PUBLISHABLE_KEY", EthNetwork.Rinkeby);

#Configure Custom Nodes

var config = new CustomNodeConfiguration(rpcUrl: "https://alchemy.io")
Magic magic = new Magic("YOUR_PUBLISHABLE_KEY", config);
important

Do not set the custom nodes to local IP address (E.x. "http://127.0.0.1"\), because local IP will point to the network environment inside mobile device / simulator. Try accessible IP address in the same Wifi/Internet Environment (E.x. "http://10.0.0.93:3000"\)

#Associated Class

CustomNodeConfiguration(rpcUrl: String, chainId: Int?)

  • rpcUrl :Your own node URL
  • chainId : Your own node's chainId

Magic.EthNetwork

enum class EthNetwork {
        Mainnet, Kovan, Rinkeby, Ropsten
 }

#Nethereum

There are two ways to set the wallet provider to Magic in Nethereum

1. Construct an ethApiService and pass Magic provider to it

var ethApiService = new EthApiService(Magic.Instance.Provider);
var accounts = await ethApiService.Accounts.SendRequestAsync();
var balance = await ethApiService.GetBalance.SendRequestAsync(accounts[0]);

2. By directly passing Magic provider to each of the Nethereum RPC methods

var ethAccounts = new EthAccounts(Magic.Instance.Provider);
var accounts = await ethAccounts.SendRequestAsync();

The following example will use either method to make an RPC call

#Get User Info

public class MagicUnityButton : MonoBehaviour
⁠{
    public async void Login()
    {
        // ⭐️ After user is successfully authenticated
        var ethApiService = new EthApiService(Magic.Instance.Provider);
        var accounts = await ethApiService.Accounts.SendRequestAsync();
        Debug.Log($"token {token}");
    }
}

#Send Transaction

public class MagicUnityButton : MonoBehaviour
⁠{
    public async void SendTransaction()
    {
        // ⭐️ After user is successfully authenticated
        var ethAccounts = new EthAccounts(Magic.Instance.Provider);
        var accounts = await ethAccounts.SendRequestAsync();

        // Construct a transaction
        var transaction = new EthSendTransaction(Magic.Instance.Provider);
        var transactionInput = new TransactionInput
            { To = accounts[0], Value = new HexBigInteger(10), From = accounts[0]};
        // Send the transaction
        var hash = await transaction.SendRequestAsync(transactionInput);
        Debug.Log(hash);
    }
}

#Eth Sign

public class MagicUnityButton : MonoBehaviour
⁠{
    public async void EthSign()
    {
        // ⭐️ After user is successfully authenticated
        var ethAccounts = new EthAccounts(Magic.Instance.Provider);
        var accounts = await ethAccounts.SendRequestAsync();
        var ethSign = new EthSign(Magic.Instance.Provider);
        var signedResult = await ethSign.SendRequestAsync(accounts[0], "hello world");
        Debug.Log(signedResult);
    }
}

Did you find what you were looking for?

Did you find what you were looking for?