Skip to content

Commit 80d77df

Browse files
committed
BigInteger Chain ID on native excl WCV1/ML // remove TWManager helpers
1 parent f76aa10 commit 80d77df

File tree

10 files changed

+47
-63
lines changed

10 files changed

+47
-63
lines changed

Assets/Thirdweb/Core/Scripts/Bridge.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public static async Task<string> Connect(WalletConnection walletConnection)
9494
ThirdwebConnect(
9595
taskId,
9696
walletConnection.provider.ToString().Substring(0,1).ToLower() + walletConnection.provider.ToString().Substring(1),
97-
walletConnection.chainId, string.IsNullOrEmpty(walletConnection.password) ? Utils.GetDeviceIdentifier() : walletConnection.password,
97+
(int)walletConnection.chainId, string.IsNullOrEmpty(walletConnection.password) ? Utils.GetDeviceIdentifier() : walletConnection.password,
9898
walletConnection.email,
9999
walletConnection.personalWallet.ToString().Substring(0,1).ToLower() + walletConnection.personalWallet.ToString().Substring(1),
100100
jsCallback

Assets/Thirdweb/Core/Scripts/ThirdwebManager.cs

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using UnityEngine;
22
using Thirdweb;
33
using System.Collections.Generic;
4+
using System.Numerics;
45

56
[System.Serializable]
67
public class ChainData
@@ -117,18 +118,18 @@ private void Awake()
117118

118119
// Inspector chain data dictionary.
119120

120-
ChainData currentChain = GetChainData(chain);
121+
ChainData currentChain = supportedChains.Find(x => x.identifier == chain);
121122

122123
// Chain ID must be provided on native platforms.
123124

124-
int chainId = -1;
125+
BigInteger chainId = -1;
125126

126127
if (!Utils.IsWebGLBuild())
127128
{
128129
if (string.IsNullOrEmpty(currentChain.chainId))
129130
throw new UnityException("You must provide a Chain ID on native platforms!");
130131

131-
if (!int.TryParse(currentChain.chainId, out chainId))
132+
if (!BigInteger.TryParse(currentChain.chainId, out chainId))
132133
throw new UnityException("The Chain ID must be a non-negative integer!");
133134
}
134135

@@ -198,24 +199,4 @@ private void Awake()
198199

199200
SDK = new ThirdwebSDK(chainOrRPC, chainId, options);
200201
}
201-
202-
public ChainData GetChainData(string chainIdentifier)
203-
{
204-
return supportedChains.Find(x => x.identifier == chainIdentifier);
205-
}
206-
207-
public ChainData GetCurrentChainData()
208-
{
209-
return supportedChains.Find(x => x.identifier == chain);
210-
}
211-
212-
public int GetCurrentChainID()
213-
{
214-
return int.Parse(GetCurrentChainData().chainId);
215-
}
216-
217-
public string GetCurrentChainIdentifier()
218-
{
219-
return chain;
220-
}
221202
}

Assets/Thirdweb/Core/Scripts/ThirdwebSDK.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Numerics;
23
using UnityEngine;
34

45
namespace Thirdweb
@@ -110,7 +111,7 @@ public struct BiconomyOptions
110111
/// </summary>
111112
/// <param name="chainOrRPC">The chain name or RPC url to connect to</param>
112113
/// <param name="options">Configuration options</param>
113-
public ThirdwebSDK(string chainOrRPC, int chainId = -1, Options options = new Options())
114+
public ThirdwebSDK(string chainOrRPC, BigInteger? chainId = null, Options options = new Options())
114115
{
115116
this.chainOrRPC = chainOrRPC;
116117
this.wallet = new Wallet();
@@ -123,10 +124,10 @@ public struct BiconomyOptions
123124
}
124125
else
125126
{
126-
if (chainId == -1)
127+
if (chainId == null)
127128
throw new UnityException("Chain ID override required for native platforms!");
128129
string rpc = !chainOrRPC.StartsWith("https://") ? $"https://{chainOrRPC}.rpc.thirdweb.com/339d65590ba0fa79e4c8be0af33d64eda709e13652acb02c6be63f5a1fbef9c3" : chainOrRPC;
129-
this.session = new ThirdwebSession(options, chainId, rpc);
130+
this.session = new ThirdwebSession(options, chainId.Value, rpc);
130131
}
131132
}
132133

Assets/Thirdweb/Core/Scripts/ThirdwebSession.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class ThirdwebSession
1616

1717

1818
public ThirdwebSDK.Options Options { get; private set; }
19-
public int ChainId { get; private set; }
19+
public BigInteger ChainId { get; private set; }
2020
public string RPC { get; private set; }
2121
public SiweMessageService SiweSession { get; private set; }
2222
public Web3 Web3 { get; private set; }
@@ -30,7 +30,7 @@ public class ThirdwebSession
3030

3131
#region Constructors
3232

33-
public ThirdwebSession(ThirdwebSDK.Options options, int chainId, string rpcUrl)
33+
public ThirdwebSession(ThirdwebSDK.Options options, BigInteger chainId, string rpcUrl)
3434
{
3535
Options = options;
3636
ChainId = chainId;
@@ -159,7 +159,7 @@ private ThirdwebChainData FetchChainData()
159159
{
160160
chainId = BigInteger.Parse(currentNetwork.chainId).ToHex(false, true) ?? BigInteger.Parse(ChainId.ToString()).ToHex(false, true),
161161
blockExplorerUrls = explorerUrls.ToArray(),
162-
chainName = currentNetwork.name ?? ThirdwebManager.Instance.GetCurrentChainIdentifier(),
162+
chainName = currentNetwork.name ?? ThirdwebManager.Instance.chain,
163163
iconUrls = new string[] { "ipfs://QmdwQDr6vmBtXmK2TmknkEuZNoaDqTasFdZdu3DRw8b2wt" },
164164
nativeCurrency = new ThirdwebNativeCurrency()
165165
{

Assets/Thirdweb/Core/Scripts/Utils.cs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public static bool DeleteLocalAccount()
288288
}
289289
}
290290

291-
public static Account UnlockOrGenerateLocalAccount(int chainId, string password = null, string privateKey = null)
291+
public static Account UnlockOrGenerateLocalAccount(BigInteger chainId, string password = null, string privateKey = null)
292292
{
293293
password = string.IsNullOrEmpty(password) ? GetDeviceIdentifier() : password;
294294

@@ -380,43 +380,44 @@ public static string ToChecksumAddress(this string address)
380380
return Nethereum.Util.AddressUtil.Current.ConvertToChecksumAddress(address);
381381
}
382382

383-
public static string GetNativeTokenWrapper(int chainId)
383+
public static string GetNativeTokenWrapper(BigInteger chainId)
384384
{
385-
switch (chainId)
385+
string id = chainId.ToString();
386+
switch (id)
386387
{
387-
case 1:
388+
case "1":
388389
return "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
389-
case 4:
390+
case "4":
390391
return "0xc778417E063141139Fce010982780140Aa0cD5Ab"; // rinkeby
391-
case 5:
392+
case "5":
392393
return "0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6"; // goerli
393-
case 137:
394+
case "137":
394395
return "0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270";
395-
case 80001:
396+
case "80001":
396397
return "0x9c3C9283D3e44854697Cd22D3Faa240Cfb032889";
397-
case 43114:
398+
case "43114":
398399
return "0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7";
399-
case 43113:
400+
case "43113":
400401
return "0xd00ae08403B9bbb9124bB305C09058E32C39A48c";
401-
case 250:
402+
case "250":
402403
return "0x21be370D5312f44cB42ce377BC9b8a0cEF1A4C83";
403-
case 4002:
404+
case "4002":
404405
return "0xf1277d1Ed8AD466beddF92ef448A132661956621";
405-
case 10:
406+
case "10":
406407
return "0x4200000000000000000000000000000000000006"; // optimism
407-
case 69:
408+
case "69":
408409
return "0xbC6F6b680bc61e30dB47721c6D1c5cde19C1300d"; // optimism kovan
409-
case 420:
410+
case "420":
410411
return "0x4200000000000000000000000000000000000006"; // optimism goerli
411-
case 42161:
412+
case "42161":
412413
return "0x82af49447d8a07e3bd95bd0d56f35241523fbab1"; // arbitrum
413-
case 421611:
414+
case "421611":
414415
return "0xEBbc3452Cc911591e4F18f3b36727Df45d6bd1f9"; // arbitrum rinkeby
415-
case 421613:
416+
case "421613":
416417
return "0xe39Ab88f8A4777030A534146A9Ca3B52bd5D43A3"; // arbitrum goerli
417-
case 56:
418+
case "56":
418419
return "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c"; // binance mainnet
419-
case 97:
420+
case "97":
420421
return "0xae13d989daC2f0dEbFf460aC112a837C89BAa7cd"; // binance testnet
421422
default:
422423
throw new UnityException("Native Token Wrapper Unavailable For This Chain!");

Assets/Thirdweb/Core/Scripts/Wallet.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ public class WalletConnection
492492
{
493493
public WalletProvider provider;
494494

495-
public int chainId;
495+
public BigInteger chainId;
496496

497497
public string password;
498498

@@ -508,7 +508,7 @@ public class WalletConnection
508508
/// <param name="password">The wallet password if using local wallets.</param>
509509
/// <param name="email">The email to login with if using email based providers.</param>
510510
/// <param name="personalWallet">The personal wallet provider if using smart wallets.</param>
511-
public WalletConnection(WalletProvider provider, int chainId = 1, string password = null, string email = null, WalletProvider personalWallet = WalletProvider.LocalWallet)
511+
public WalletConnection(WalletProvider provider, BigInteger chainId, string password = null, string email = null, WalletProvider personalWallet = WalletProvider.LocalWallet)
512512
{
513513
this.provider = provider;
514514
this.chainId = chainId;

Assets/Thirdweb/Core/Scripts/Wallets/ThirdwebMagicLink.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public ThirdwebMagicLink(string magicLinkApiKey)
2525

2626
public async Task<string> Connect(WalletConnection walletConnection, string rpc)
2727
{
28-
_magic = new Magic(_magicLinkApiKey, new CustomNodeConfiguration(rpc, walletConnection.chainId));
28+
_magic = new Magic(_magicLinkApiKey, new CustomNodeConfiguration(rpc, (int)walletConnection.chainId));
2929

3030
await _magic.Auth.LoginWithEmailOtp(walletConnection.email);
3131
_web3 = new Web3(_magic.Provider);

Assets/Thirdweb/Examples/Scripts/Prefabs/Prefab_ConnectWallet.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using UnityEngine.UI;
77
using UnityEngine.Events;
88
using WalletConnectSharp.Core.Models;
9+
using System.Numerics;
910

1011
[Serializable]
1112
public struct WalletButton
@@ -75,10 +76,7 @@ public class Prefab_ConnectWallet : MonoBehaviour
7576

7677
public string Address
7778
{
78-
get
79-
{
80-
return address;
81-
}
79+
get { return address; }
8280
}
8381

8482
// UI Initialization
@@ -168,7 +166,9 @@ public async void OnConnect(WalletProvider wallet, string password = null, strin
168166
{
169167
try
170168
{
171-
address = await ThirdwebManager.Instance.SDK.wallet.Connect(new WalletConnection(wallet, ThirdwebManager.Instance.GetCurrentChainID(), password, email, personalWallet));
169+
ChainData currentChain = ThirdwebManager.Instance.supportedChains.Find(x => x.identifier == ThirdwebManager.Instance.chain);
170+
171+
address = await ThirdwebManager.Instance.SDK.wallet.Connect(new WalletConnection(wallet, BigInteger.Parse(currentChain.chainId), password, email, personalWallet));
172172

173173
if (wallet == WalletProvider.LocalWallet || (wallet == WalletProvider.SmartWallet && personalWallet == WalletProvider.LocalWallet))
174174
{
@@ -206,7 +206,7 @@ async void OnConnected()
206206
balanceText2.text = balanceText.text;
207207
walletAddressText.text = await Utils.GetENSName(address) ?? address.ShortenAddress();
208208
walletAddressText2.text = walletAddressText.text;
209-
currentNetworkText.text = ThirdwebManager.Instance.GetCurrentChainIdentifier();
209+
currentNetworkText.text = ThirdwebManager.Instance.chain;
210210
currentNetworkImage.sprite = networkSprites.Find(x => x.chain == _chain).sprite;
211211
connectButton.SetActive(false);
212212
connectedButton.SetActive(true);
@@ -230,7 +230,8 @@ public async void OnSwitchNetwork(string _chain)
230230
try
231231
{
232232
ThirdwebManager.Instance.chain = _chain;
233-
await ThirdwebManager.Instance.SDK.wallet.SwitchNetwork(int.Parse(ThirdwebManager.Instance.GetCurrentChainData().chainId));
233+
ChainData currentChain = ThirdwebManager.Instance.supportedChains.Find(x => x.identifier == ThirdwebManager.Instance.chain);
234+
await ThirdwebManager.Instance.SDK.wallet.SwitchNetwork(int.Parse(currentChain.chainId));
234235
OnConnected();
235236
OnSwitchNetworkCallback?.Invoke();
236237
Debug.Log($"Switched Network Successfully: {_chain}");

Assets/Thirdweb/Plugins/WalletConnectSharp.Unity/WalletConnect.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void Initialize()
8282
public async Task<WCSessionData> EnableWalletConnect()
8383
{
8484
SetMode(true);
85-
var sessionData = await Connect(ThirdwebManager.Instance.SDK.session.ChainId);
85+
var sessionData = await Connect((int)ThirdwebManager.Instance.SDK.session.ChainId);
8686
SetMode(false);
8787
return sessionData;
8888
}
@@ -336,7 +336,7 @@ private async void OnApplicationPause(bool pauseStatus)
336336
if (pauseStatus)
337337
SaveSession();
338338
else if (PlayerPrefs.HasKey(SessionKey))
339-
await Connect(ThirdwebManager.Instance.SDK.session.ChainId);
339+
await Connect((int)ThirdwebManager.Instance.SDK.session.ChainId);
340340
}
341341

342342
private void SaveSession()

Assets/Thirdweb/Plugins/WalletConnectSharp.Unity/WalletConnectUnitySession.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public override async Task Connect()
5454

5555
public override async Task<WCSessionData> ConnectSession()
5656
{
57-
return await unityObjectSource.Connect(ThirdwebManager.Instance.SDK.session.ChainId);
57+
return await unityObjectSource.Connect((int)ThirdwebManager.Instance.SDK.session.ChainId);
5858
}
5959
}
6060
}

0 commit comments

Comments
 (0)