11import { type CreateConnectorFn , createConnector } from "@wagmi/core" ;
22import type { Prettify } from "@wagmi/core/chains" ;
3- import { defineChain , getAddress , type ThirdwebClient } from "thirdweb" ;
3+ import {
4+ type Chain ,
5+ defineChain ,
6+ getAddress ,
7+ type ThirdwebClient ,
8+ } from "thirdweb" ;
49import {
510 EIP1193 ,
611 ecosystemWallet ,
@@ -53,6 +58,7 @@ type StorageItem = {
5358
5459const activeWalletIdKey = "thirdweb:active-wallet-id" ;
5560const connectedWalletIdsKey = "thirdweb:connected-wallet-ids" ;
61+ const activeChainIdKey = "thirdweb:active-chain" ;
5662
5763/**
5864 * Connect to an in-app wallet using the auth strategy of your choice.
@@ -112,13 +118,14 @@ export function inAppWalletConnector(
112118 ? window . localStorage
113119 : undefined ;
114120 return createConnector ( ( config ) => ( {
115- connect : async ( params ?: ConnectionOptions < false > ) => {
121+ connect : async ( params ) => {
116122 wallet =
117123 params && "wallet" in params ? ( params . wallet as Wallet ) : wallet ;
118- const lastChainIdStr = await config . storage ?. getItem (
119- "thirdweb:lastChainId" ,
120- ) ;
121- const lastChainId = lastChainIdStr ? Number ( lastChainIdStr ) : undefined ;
124+ const lastChainIdStr = rawStorage ?. getItem ( activeChainIdKey ) ;
125+ const lastChain = lastChainIdStr
126+ ? ( JSON . parse ( lastChainIdStr ) as Chain )
127+ : undefined ;
128+ const lastChainId = lastChain ? lastChain . id : undefined ;
122129 if ( params ?. isReconnecting ) {
123130 const { autoConnect } = await import ( "thirdweb/wallets" ) ;
124131 const chainId = lastChainId || args . smartAccount ?. chain ?. id || 1 ;
@@ -133,7 +140,6 @@ export function inAppWalletConnector(
133140 if ( ! account ) {
134141 throw new Error ( "Wallet failed to reconnect" ) ;
135142 }
136-
137143 return {
138144 accounts : [ getAddress ( account . address ) ] as any ,
139145 chainId : chainId ,
@@ -144,7 +150,7 @@ export function inAppWalletConnector(
144150 const alreadyConnectedAccount = wallet . getAccount ( ) ;
145151 if ( alreadyConnectedAccount ) {
146152 return {
147- accounts : [ getAddress ( alreadyConnectedAccount . address ) ] ,
153+ accounts : [ getAddress ( alreadyConnectedAccount . address ) ] as any ,
148154 chainId : wallet . getChain ( ) ?. id || 1 ,
149155 } ;
150156 }
@@ -191,9 +197,11 @@ export function inAppWalletConnector(
191197 }
192198 rawStorage . setItem ( activeWalletIdKey , wallet . id ) ;
193199 }
194- await config . storage ?. setItem ( "thirdweb:lastChainId" , chain . id ) ;
195200 args . onConnect ?.( wallet ) ;
196- return { accounts : [ getAddress ( account . address ) ] , chainId : chain . id } ;
201+ return {
202+ accounts : [ getAddress ( account . address ) ] as any ,
203+ chainId : chain . id ,
204+ } ;
197205 } ,
198206 disconnect : async ( ) => {
199207 await wallet . disconnect ( ) ;
@@ -209,9 +217,7 @@ export function inAppWalletConnector(
209217 return wallet . getChain ( ) ?. id || 1 ;
210218 } ,
211219 getProvider : async ( params ) => {
212- const lastChainIdStr = await config . storage ?. getItem (
213- "thirdweb:lastChainId" ,
214- ) ;
220+ const lastChainIdStr = await rawStorage ?. getItem ( activeChainIdKey ) ;
215221 const lastChainId = lastChainIdStr ? Number ( lastChainIdStr ) : undefined ;
216222 const chain = defineChain (
217223 params ?. chainId || args . smartAccount ?. chain ?. id || lastChainId || 1 ,
@@ -262,7 +268,10 @@ export function inAppWalletConnector(
262268 config . emitter . emit ( "change" , {
263269 chainId : chain . id ,
264270 } ) ;
265- await config . storage ?. setItem ( "thirdweb:lastChainId" , chain . id ) ;
271+ rawStorage ?. setItem (
272+ activeChainIdKey ,
273+ JSON . stringify ( defineChain ( chain . id ) ) ,
274+ ) ;
266275 return chain ;
267276 } ,
268277 type : "in-app" ,
0 commit comments