From 31d7d10e42a4870767deab1ddfe170058c9537cb Mon Sep 17 00:00:00 2001 From: 0xFirekeeper <0xFirekeeper@gmail.com> Date: Mon, 22 May 2023 22:13:01 +0300 Subject: [PATCH] Switch/Add Networks Metamask SDK --- Assets/Thirdweb/Core/Scripts/Wallet.cs | 22 ++++++++++++++++--- .../Runtime/WebSocketDispatcher.cs | 2 +- .../Plugins/MetaMask/Scripts/MetaMaskUnity.cs | 15 +++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/Assets/Thirdweb/Core/Scripts/Wallet.cs b/Assets/Thirdweb/Core/Scripts/Wallet.cs index a2a5ae3c..ec0d9e1f 100644 --- a/Assets/Thirdweb/Core/Scripts/Wallet.cs +++ b/Assets/Thirdweb/Core/Scripts/Wallet.cs @@ -18,6 +18,7 @@ using Nethereum.Hex.HexTypes; using Nethereum.ABI.EIP712; using Nethereum.Signer.EIP712; +using Newtonsoft.Json; //using WalletConnectSharp.NEthereum; @@ -80,7 +81,6 @@ public async Task Connect(WalletConnection walletConnection) oldSession.siweSession ); - // Switch to chain try { await WalletConnect.Instance.WalletSwitchEthChain(new EthChain() { chainId = ThirdwebManager.Instance.SDK.currentChainData.chainId }); @@ -88,8 +88,6 @@ public async Task Connect(WalletConnection walletConnection) catch (System.Exception e) { Debug.LogWarning("Switching chain error, attempting to add chain: " + e.Message); - - // Add chain try { await WalletConnect.Instance.WalletAddEthChain(ThirdwebManager.Instance.SDK.currentChainData); @@ -152,6 +150,24 @@ public async Task Connect(WalletConnection walletConnection) await new WaitUntil(() => connected || !Application.isPlaying); + try + { + await MetaMaskUnity.Instance.WalletSwitchEthChain(new EthChain() { chainId = ThirdwebManager.Instance.SDK.currentChainData.chainId }); + } + catch (System.Exception e) + { + Debug.LogWarning("Switching chain error, attempting to add chain: " + e.Message); + try + { + await MetaMaskUnity.Instance.WalletAddEthChain(ThirdwebManager.Instance.SDK.currentChainData); + await MetaMaskUnity.Instance.WalletSwitchEthChain(new EthChain() { chainId = ThirdwebManager.Instance.SDK.currentChainData.chainId }); + } + catch (System.Exception f) + { + Debug.LogWarning("Adding chain error: " + f.Message); + } + } + break; default: throw new UnityException("This wallet connection method is not supported on this platform!"); diff --git a/Assets/Thirdweb/Plugins/MetaMask/Plugins/Libraries/SocketIOUnity/Runtime/WebSocketDispatcher.cs b/Assets/Thirdweb/Plugins/MetaMask/Plugins/Libraries/SocketIOUnity/Runtime/WebSocketDispatcher.cs index 2c372054..de79acb4 100644 --- a/Assets/Thirdweb/Plugins/MetaMask/Plugins/Libraries/SocketIOUnity/Runtime/WebSocketDispatcher.cs +++ b/Assets/Thirdweb/Plugins/MetaMask/Plugins/Libraries/SocketIOUnity/Runtime/WebSocketDispatcher.cs @@ -14,7 +14,7 @@ public static WebSocketDispatcher Instance { get { - if (instance == null) + if (instance == null && Application.isPlaying) { instance = new GameObject("WebSocket Dispatcher").AddComponent(); } diff --git a/Assets/Thirdweb/Plugins/MetaMask/Scripts/MetaMaskUnity.cs b/Assets/Thirdweb/Plugins/MetaMask/Scripts/MetaMaskUnity.cs index e0f0d11a..998297ca 100644 --- a/Assets/Thirdweb/Plugins/MetaMask/Scripts/MetaMaskUnity.cs +++ b/Assets/Thirdweb/Plugins/MetaMask/Scripts/MetaMaskUnity.cs @@ -9,6 +9,7 @@ using MetaMask.Transports.Unity.UI; using Nethereum.ABI.EIP712; using UnityEngine; +using Newtonsoft.Json; namespace MetaMask.Unity { @@ -259,6 +260,20 @@ public async Task SignTypedDataV4(T data, TypedData return result.GetString(); } + public async Task WalletAddEthChain(object ethChainData) + { + var request = new MetaMaskEthereumRequest { Method = "wallet_addEthereumChain", Parameters = new object[] { ethChainData } }; + var result = await MetaMaskUnity.Instance.Wallet.Request(request); + return result.GetString(); + } + + public async Task WalletSwitchEthChain(object ethChain) + { + var request = new MetaMaskEthereumRequest { Method = "wallet_switchEthereumChain", Parameters = new object[] { ethChain } }; + var result = await MetaMaskUnity.Instance.Wallet.Request(request); + return result.GetString(); + } + #endregion } }