Skip to content

Commit

Permalink
Switch/Add Networks Metamask SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
0xFirekeeper committed May 22, 2023
1 parent a7e39ec commit 31d7d10
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
22 changes: 19 additions & 3 deletions Assets/Thirdweb/Core/Scripts/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Nethereum.Hex.HexTypes;
using Nethereum.ABI.EIP712;
using Nethereum.Signer.EIP712;
using Newtonsoft.Json;

//using WalletConnectSharp.NEthereum;

Expand Down Expand Up @@ -80,16 +81,13 @@ public async Task<string> Connect(WalletConnection walletConnection)
oldSession.siweSession
);

// Switch to chain
try
{
await WalletConnect.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);

// Add chain
try
{
await WalletConnect.Instance.WalletAddEthChain(ThirdwebManager.Instance.SDK.currentChainData);
Expand Down Expand Up @@ -152,6 +150,24 @@ public async Task<string> 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!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static WebSocketDispatcher Instance
{
get
{
if (instance == null)
if (instance == null && Application.isPlaying)
{
instance = new GameObject("WebSocket Dispatcher").AddComponent<WebSocketDispatcher>();
}
Expand Down
15 changes: 15 additions & 0 deletions Assets/Thirdweb/Plugins/MetaMask/Scripts/MetaMaskUnity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using MetaMask.Transports.Unity.UI;
using Nethereum.ABI.EIP712;
using UnityEngine;
using Newtonsoft.Json;

namespace MetaMask.Unity
{
Expand Down Expand Up @@ -259,6 +260,20 @@ public async Task<string> SignTypedDataV4<T, TDomain>(T data, TypedData<TDomain>
return result.GetString();
}

public async Task<string> 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<string> 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
}
}

0 comments on commit 31d7d10

Please sign in to comment.