@@ -58,12 +58,13 @@ export const setWalletConnection = (type: TransferWallet, wallet: Wallet) => {
58
58
walletConnection [ type ] = wallet ;
59
59
} ;
60
60
61
+ // Returns false if the wallet connection was rejected by the user
61
62
export const connectWallet = async (
62
63
type : TransferWallet ,
63
64
chain : Chain ,
64
65
walletInfo : WalletData ,
65
66
dispatch : Dispatch < any > ,
66
- ) => {
67
+ ) : Promise < boolean > => {
67
68
const { wallet, name } = walletInfo ;
68
69
69
70
setWalletConnection ( type , wallet ) ;
@@ -74,7 +75,18 @@ export const connectWallet = async (
74
75
}
75
76
76
77
const { chainId, context } = chainConfig ;
77
- await wallet . connect ( { chainId } ) ;
78
+
79
+ try {
80
+ await wallet . connect ( { chainId } ) ;
81
+ } catch ( e : any ) {
82
+ if ( e . message && e . message . toLowerCase ( ) . includes ( 'rejected' ) ) {
83
+ console . info ( 'User rejected wallet connection' ) ;
84
+ // If user doesn't want to connect to this wallet, this is not an error we need to throw
85
+ return false ;
86
+ } else {
87
+ throw e ;
88
+ }
89
+ }
78
90
79
91
config . triggerEvent ( {
80
92
type : 'wallet.connect' ,
@@ -126,6 +138,8 @@ export const connectWallet = async (
126
138
if ( name !== ReadOnlyWallet . NAME ) {
127
139
localStorage . setItem ( `wormhole-connect:wallet:${ context } ` , name ) ;
128
140
}
141
+
142
+ return true ;
129
143
} ;
130
144
131
145
// Checks localStorage for previously used wallet for this chain
@@ -135,26 +149,20 @@ export const connectLastUsedWallet = async (
135
149
chain : Chain ,
136
150
dispatch : Dispatch < any > ,
137
151
) => {
138
- const localStorageKey = `wormhole-connect:wallet:${ chainConfig . context } ` ;
139
152
const chainConfig = config . chains [ chain ! ] ! ;
153
+ const localStorageKey = `wormhole-connect:wallet:${ chainConfig . context } ` ;
140
154
const lastUsedWallet = localStorage . getItem ( localStorageKey ) ;
141
155
142
- try {
143
- // if the last used wallet is not WalletConnect, try to connect to it
144
- if ( lastUsedWallet && lastUsedWallet !== 'WalletConnect' ) {
145
- const options = await getWalletOptions ( chainConfig ) ;
146
- const wallet = options . find ( ( w ) => w . name === lastUsedWallet ) ;
147
- if ( wallet ) {
148
- await connectWallet ( type , chain , wallet , dispatch ) ;
156
+ // if the last used wallet is not WalletConnect, try to connect to it
157
+ if ( lastUsedWallet && lastUsedWallet !== 'WalletConnect' ) {
158
+ const options = await getWalletOptions ( chainConfig ) ;
159
+ const wallet = options . find ( ( w ) => w . name === lastUsedWallet ) ;
160
+ if ( wallet ) {
161
+ const connected = await connectWallet ( type , chain , wallet , dispatch ) ;
162
+ if ( ! connected ) {
163
+ localStorage . removeItem ( localStorageKey ) ;
149
164
}
150
165
}
151
- } catch ( e : any ) {
152
- if ( e . message && e . message . includes ( 'UserRejectedRequestError' ) ) {
153
- // If user doesn't want to connect to this wallet, remove it from localStorage
154
- localStorage . removeItem ( localStorageKey ) ;
155
- } else {
156
- throw e ;
157
- }
158
166
}
159
167
} ;
160
168
0 commit comments