Skip to content

Commit 31d7d10

Browse files
committed
Switch/Add Networks Metamask SDK
1 parent a7e39ec commit 31d7d10

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

Assets/Thirdweb/Core/Scripts/Wallet.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using Nethereum.Hex.HexTypes;
1919
using Nethereum.ABI.EIP712;
2020
using Nethereum.Signer.EIP712;
21+
using Newtonsoft.Json;
2122

2223
//using WalletConnectSharp.NEthereum;
2324

@@ -80,16 +81,13 @@ public async Task<string> Connect(WalletConnection walletConnection)
8081
oldSession.siweSession
8182
);
8283

83-
// Switch to chain
8484
try
8585
{
8686
await WalletConnect.Instance.WalletSwitchEthChain(new EthChain() { chainId = ThirdwebManager.Instance.SDK.currentChainData.chainId });
8787
}
8888
catch (System.Exception e)
8989
{
9090
Debug.LogWarning("Switching chain error, attempting to add chain: " + e.Message);
91-
92-
// Add chain
9391
try
9492
{
9593
await WalletConnect.Instance.WalletAddEthChain(ThirdwebManager.Instance.SDK.currentChainData);
@@ -152,6 +150,24 @@ public async Task<string> Connect(WalletConnection walletConnection)
152150

153151
await new WaitUntil(() => connected || !Application.isPlaying);
154152

153+
try
154+
{
155+
await MetaMaskUnity.Instance.WalletSwitchEthChain(new EthChain() { chainId = ThirdwebManager.Instance.SDK.currentChainData.chainId });
156+
}
157+
catch (System.Exception e)
158+
{
159+
Debug.LogWarning("Switching chain error, attempting to add chain: " + e.Message);
160+
try
161+
{
162+
await MetaMaskUnity.Instance.WalletAddEthChain(ThirdwebManager.Instance.SDK.currentChainData);
163+
await MetaMaskUnity.Instance.WalletSwitchEthChain(new EthChain() { chainId = ThirdwebManager.Instance.SDK.currentChainData.chainId });
164+
}
165+
catch (System.Exception f)
166+
{
167+
Debug.LogWarning("Adding chain error: " + f.Message);
168+
}
169+
}
170+
155171
break;
156172
default:
157173
throw new UnityException("This wallet connection method is not supported on this platform!");

Assets/Thirdweb/Plugins/MetaMask/Plugins/Libraries/SocketIOUnity/Runtime/WebSocketDispatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static WebSocketDispatcher Instance
1414
{
1515
get
1616
{
17-
if (instance == null)
17+
if (instance == null && Application.isPlaying)
1818
{
1919
instance = new GameObject("WebSocket Dispatcher").AddComponent<WebSocketDispatcher>();
2020
}

Assets/Thirdweb/Plugins/MetaMask/Scripts/MetaMaskUnity.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using MetaMask.Transports.Unity.UI;
1010
using Nethereum.ABI.EIP712;
1111
using UnityEngine;
12+
using Newtonsoft.Json;
1213

1314
namespace MetaMask.Unity
1415
{
@@ -259,6 +260,20 @@ public async Task<string> SignTypedDataV4<T, TDomain>(T data, TypedData<TDomain>
259260
return result.GetString();
260261
}
261262

263+
public async Task<string> WalletAddEthChain(object ethChainData)
264+
{
265+
var request = new MetaMaskEthereumRequest { Method = "wallet_addEthereumChain", Parameters = new object[] { ethChainData } };
266+
var result = await MetaMaskUnity.Instance.Wallet.Request(request);
267+
return result.GetString();
268+
}
269+
270+
public async Task<string> WalletSwitchEthChain(object ethChain)
271+
{
272+
var request = new MetaMaskEthereumRequest { Method = "wallet_switchEthereumChain", Parameters = new object[] { ethChain } };
273+
var result = await MetaMaskUnity.Instance.Wallet.Request(request);
274+
return result.GetString();
275+
}
276+
262277
#endregion
263278
}
264279
}

0 commit comments

Comments
 (0)