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
01using link.magic.unity.sdk;
02using UnityEngine;
03
04public class MagicUnity : MonoBehaviour
05{
06 // Start is called before the first frame update
07 void Start()
08 {
09 Magic magic = new Magic("YOUR_PUBLISHABLE_KEY");
10 Magic.Instance = magic;
11 }
12}
#Use Different Networks
#Choose Different Testnet
01Magic magic = new Magic("YOUR_PUBLISHABLE_KEY", EthNetwork.Rinkeby);
#Configure Custom Nodes
01var config = new CustomNodeConfiguration(rpcUrl: "https://alchemy.io")
02Magic magic = new Magic("YOUR_PUBLISHABLE_KEY", config);
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 URLchainId
: Your own node's chainId
Magic.EthNetwork
01enum class EthNetwork {
02 Mainnet, Kovan, Rinkeby, Ropsten
03 }
#Nethereum
There are two ways to set the wallet provider to Magic in Nethereum
1. Construct an ethApiService and pass Magic provider to it
01var ethApiService = new EthApiService(Magic.Instance.Provider);
02var accounts = await ethApiService.Accounts.SendRequestAsync();
03var balance = await ethApiService.GetBalance.SendRequestAsync(accounts[0]);
2. By directly passing Magic provider to each of the Nethereum RPC methods
01var ethAccounts = new EthAccounts(Magic.Instance.Provider);
02var accounts = await ethAccounts.SendRequestAsync();
The following example will use either method to make an RPC call
#Get User Info
01public class MagicUnityButton : MonoBehaviour
02{
03 public async void Login()
04 {
05 // ⭐️ After user is successfully authenticated
06 var ethApiService = new EthApiService(Magic.Instance.Provider);
07 var accounts = await ethApiService.Accounts.SendRequestAsync();
08 Debug.Log($"token {token}");
09 }
10}
#Send Transaction
01public class MagicUnityButton : MonoBehaviour
02{
03 public async void SendTransaction()
04 {
05 // ⭐️ After user is successfully authenticated
06 var ethAccounts = new EthAccounts(Magic.Instance.Provider);
07 var accounts = await ethAccounts.SendRequestAsync();
08
09 // Construct a transaction
10 var transaction = new EthSendTransaction(Magic.Instance.Provider);
11 var transactionInput = new TransactionInput
12 { To = accounts[0], Value = new HexBigInteger(10), From = accounts[0]};
13
14 // Send the transaction
15 var hash = await transaction.SendRequestAsync(transactionInput);
16 Debug.Log(hash);
17 }
18}
#Eth Sign
01public class MagicUnityButton : MonoBehaviour
02{
03 public async void EthSign()
04 {
05 // ⭐️ After user is successfully authenticated
06 var ethAccounts = new EthAccounts(Magic.Instance.Provider);
07 var accounts = await ethAccounts.SendRequestAsync();
08 var ethSign = new EthSign(Magic.Instance.Provider);
09 var signedResult = await ethSign.SendRequestAsync(accounts[0], "hello world");
10 Debug.Log(signedResult);
11 }
12}