@@ -45,9 +45,9 @@ public ThirdwebSession(ThirdwebSDK.Options options, BigInteger chainId, string r
4545
4646 #endregion
4747
48- #region Public Methods
48+ #region Internal Methods
4949
50- public async Task < string > Connect ( WalletConnection walletConnection )
50+ internal async Task < string > Connect ( WalletConnection walletConnection )
5151 {
5252 switch ( walletConnection . provider )
5353 {
@@ -91,7 +91,14 @@ public async Task<string> Connect(WalletConnection walletConnection)
9191 Web3 = await ActiveWallet . GetWeb3 ( ) ;
9292 Web3 . Client . OverridingRequestInterceptor = new ThirdwebInterceptor ( ActiveWallet ) ;
9393
94- await EnsureCorrectNetwork ( ) ;
94+ try
95+ {
96+ await EnsureCorrectNetwork ( ChainId ) ;
97+ }
98+ catch ( System . Exception e )
99+ {
100+ Debug . LogWarning ( "WalletProvider unable to switch chains, proceeding anyway. Error:" + e . Message ) ;
101+ }
95102
96103 var addy = await ActiveWallet . GetAddress ( ) ;
97104
@@ -100,59 +107,84 @@ public async Task<string> Connect(WalletConnection walletConnection)
100107 return addy ;
101108 }
102109
103- public async Task Disconnect ( )
110+ internal async Task Disconnect ( )
104111 {
105112 await ActiveWallet . Disconnect ( ) ;
106113 ThirdwebManager . Instance . SDK . session = new ThirdwebSession ( Options , ChainId , RPC ) ;
107114 }
108115
109- public async Task < T > Request < T > ( string method , params object [ ] parameters )
116+ internal async Task < T > Request < T > ( string method , params object [ ] parameters )
110117 {
111118 var request = new RpcRequest ( Nonce , method , parameters ) ;
112119 Nonce ++ ;
113120 return await Web3 . Client . SendRequestAsync < T > ( request ) ;
114121 }
115122
116- #endregion
123+ internal async Task EnsureCorrectNetwork ( BigInteger newChainId )
124+ {
125+ ThirdwebChainData newChainData = null ;
126+ try
127+ {
128+ newChainData = Options . supportedChains . ToList ( ) . Find ( x => x . chainId == new HexBigInteger ( newChainId ) . HexValue ) ;
129+ }
130+ catch
131+ {
132+ throw new UnityException ( "The chain you are trying to switch to is not part of the ThirdwebManager's supported chains." ) ;
133+ }
117134
118- #region Private Methods
135+ NetworkSwitchAction switchResult = await ActiveWallet . PrepareForNetworkSwitch ( newChainId , newChainData . rpcUrls [ 0 ] ) ;
119136
120- private async Task EnsureCorrectNetwork ( )
121- {
122- var hexChainId = await Request < string > ( "eth_chainId" ) ;
123- var connectedChainId = ( int ) hexChainId . HexToBigInteger ( false ) ;
124- if ( connectedChainId != ChainId )
137+ switch ( switchResult )
125138 {
126- try
127- {
128- await SwitchNetwork ( new ThirdwebChain ( ) { chainId = CurrentChainData . chainId } ) ;
129- }
130- catch ( System . Exception e )
131- {
132- Debug . LogWarning ( "Switching chain error, attempting to add chain: " + e . Message ) ;
133- try
139+ case NetworkSwitchAction . ContinueSwitch :
140+ var hexChainId = await Request < string > ( "eth_chainId" ) ;
141+ var connectedChainId = hexChainId . HexToBigInteger ( false ) ;
142+ if ( connectedChainId != ChainId )
134143 {
135- await AddNetwork ( CurrentChainData ) ;
136- await SwitchNetwork ( new ThirdwebChain ( ) { chainId = CurrentChainData . chainId } ) ;
144+ try
145+ {
146+ await SwitchNetwork ( new ThirdwebChain ( ) { chainId = newChainData . chainId } ) ;
147+ }
148+ catch ( System . Exception e )
149+ {
150+ Debug . LogWarning ( "Switching chain error, attempting to add chain: " + e . Message ) ;
151+ try
152+ {
153+ await AddNetwork ( newChainData ) ;
154+ await SwitchNetwork ( new ThirdwebChain ( ) { chainId = newChainData . chainId } ) ;
155+ }
156+ catch ( System . Exception f )
157+ {
158+ throw new UnityException ( "Adding chain error: " + f . Message ) ;
159+ }
160+ }
137161 }
138- catch ( System . Exception f )
139- {
140- Debug . LogWarning ( "Adding chain error: " + f . Message ) ;
141- }
142- }
162+ break ;
163+ case NetworkSwitchAction . Handled :
164+ break ;
165+ case NetworkSwitchAction . Unsupported :
166+ throw new UnityException ( "Network switching is not supported by the active wallet." ) ;
143167 }
168+
169+ ChainId = newChainId ;
170+ CurrentChainData = newChainData ;
171+ RPC = CurrentChainData . rpcUrls [ 0 ] ;
172+ Web3 = await ActiveWallet . GetWeb3 ( ) ;
173+ Web3 . Client . OverridingRequestInterceptor = new ThirdwebInterceptor ( ActiveWallet ) ;
144174 }
145175
176+ #endregion
177+
178+ #region Private Methods
179+
146180 private async Task SwitchNetwork ( ThirdwebChain newChain )
147181 {
148182 await Request < object > ( "wallet_switchEthereumChain" , new object [ ] { newChain } ) ;
149- CurrentChainData . chainId = newChain . chainId ;
150183 }
151184
152185 private async Task AddNetwork ( ThirdwebChainData newChainData )
153186 {
154187 await Request < object > ( "wallet_addEthereumChain" , new object [ ] { newChainData } ) ;
155- CurrentChainData = newChainData ;
156188 }
157189
158190 public static ThirdwebChainData FetchChainData ( BigInteger chainId , string rpcOverride = null )
0 commit comments