#if UNITY_WEBGL using System; using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; using Cysharp.Threading.Tasks; using UnityEngine; using WalletConnectSharp.Core.Models; namespace MoralisUnity { public class Web3GL { private static bool isConnected; [DllImport("__Internal")] private static extern void ConnectWeb3(string appLogo, string appTitile, string appDesc); [DllImport("__Internal")] private static extern void SendContractJs(string method, string abi, string contract, string args, string value, string gasLimit, string gasPrice); [DllImport("__Internal")] private static extern string SendContractResponse(); [DllImport("__Internal")] private static extern void SetContractResponse(string value); [DllImport("__Internal")] private static extern void SendTransactionJs(string to, string value, string gasLimit, string gasPrice); [DllImport("__Internal")] private static extern string SendTransactionResponse(); [DllImport("__Internal")] private static extern void SetTransactionResponse(string value); [DllImport("__Internal")] private static extern void SignMessage(string value); [DllImport("__Internal")] private static extern string SignMessageResponse(); [DllImport("__Internal")] private static extern void SetSignMessageResponse(string value); [DllImport("__Internal")] private static extern int GetNetwork(); [DllImport("__Internal")] private static extern string ConnectAccount(); [DllImport("__Internal")] private static extern string SetConnectAccount(string value); [DllImport("__Internal")] private static extern bool SetDebugMode(bool mode); /// /// Connect to the browser wallet with web3. /// /// /// string - account address public async static UniTask Connect(ClientMeta clientMeta) { string account = ""; string appLogo = "https://moralis.io/wp-content/uploads/2021/06/Powered-by-Moralis-Badge-Black.svg"; int waitLoops = 240; // approx 2min @ 60FPS int loop = 0; if (clientMeta.Icons.Length > 0) { appLogo = clientMeta.Icons[0]; } ConnectWeb3(appLogo, clientMeta.Name, clientMeta.Description); while (loop < waitLoops && account.Length < 1) { await UniTask.DelayFrame(30); account = ConnectAccount(); loop++; } if (account.Length > 0 && account != "false") { isConnected = true; return account; } else { // Reset account after error SetConnectAccount(""); throw new Exception("Web3 Login timed out or failed."); } } /// /// Indicates if the web3 objects has been created and connected properly. /// /// bool public static bool IsConnected() => isConnected; /// /// Executes a method on a contract /// /// Function to call /// Contract ABI /// Contract Address /// Function parameters as json /// value to send as wei /// gas limit OPTIONAL /// gas price OPTIONAL /// public async static UniTask SendContract(string _method, string _abi, string _contract, string _args, string _value, string _gasLimit = "", string _gasPrice = "") { // Set response to empty SetContractResponse(""); SendContractJs(_method, _abi, _contract, _args, _value, _gasLimit, _gasPrice); string response = SendContractResponse(); while (response == "") { await UniTask.DelayFrame(30); response = SendContractResponse(); } SetContractResponse(""); // check if user submitted or user rejected if (response.Length == 66) { return response; } else { throw new Exception(response); } } /// /// Transfer value /// /// /// /// /// /// public async static UniTask SendTransaction(string _to, string _value, string _gasLimit = "", string _gasPrice = "") { // Set response to empty SetTransactionResponse(""); SendTransactionJs(_to, _value, _gasLimit, _gasPrice); string response = SendTransactionResponse(); while (response == "") { await UniTask.DelayFrame(30); response = SendTransactionResponse(); } SetTransactionResponse(""); // check if user submmited or user rejected if (response.Length == 66) { return response; } else { throw new Exception(response); } } /// /// Generate a signature using message /// /// /// public async static UniTask Sign(string _message) { SignMessage(_message); string response = SignMessageResponse(); while (response == "") { await UniTask.DelayFrame(30); response = SignMessageResponse(); } // Set response to empty SetSignMessageResponse(""); // check if user submmited or user rejected if (response.Length == 132) { return response; } else { throw new Exception(response); } } /// /// Returns the current chainId /// /// public static int ChainId() { return GetNetwork(); } /// /// Connected Account /// /// public static string Account() { return ConnectAccount(); } /// /// Turns web3 /// /// public static void DebugMode(bool mode) { SetDebugMode(mode); } } } #endif