@@ -11,9 +11,10 @@ import React, {
11
11
useRef ,
12
12
} from 'react' ;
13
13
import { useQueryClient } from 'react-query' ;
14
+ import invariant from 'ts-invariant' ;
14
15
15
16
import { connectToContractWithAdaptor , useEthersContext } from '~~/context' ;
16
- import { invalidateCache , isValidEthersAdaptor , sortContractsByChainId } from '~~/functions' ;
17
+ import { invalidateCache , isValidEthersAdaptor , sortContractsByChainId , sortContractsByName } from '~~/functions' ;
17
18
import { TTypedContract , TEthersAdaptor , TConnectorList } from '~~/models' ;
18
19
import { keyNamespace } from '~~/models/constants' ;
19
20
import { TAppContractsContext , defaultAppContractsContext , TContractsByName } from '~~/models/contractContextTypes' ;
@@ -120,6 +121,21 @@ export const contractsContextFactory = <
120
121
return newState ;
121
122
} ;
122
123
124
+ const removeInvalidContracts = (
125
+ state : TAppContractsContext < GContractNames > ,
126
+ ethersAdaptor : TEthersAdaptor | undefined
127
+ ) : TAppContractsContext < GContractNames > => {
128
+ if ( ethersAdaptor ?. chainId != null ) {
129
+ const newState = cloneContextState ( state ) ;
130
+ const chainId = ethersAdaptor . chainId ;
131
+ delete newState . contractsByChainId [ chainId ] ;
132
+
133
+ newState . contractsByName = sortContractsByName ( newState . contractsByChainId ) ;
134
+ return newState ;
135
+ }
136
+ return state ;
137
+ } ;
138
+
123
139
/* *************** ******** ****************************************** */
124
140
/* *************** Contract Action Helper Functions ****************** */
125
141
/**
@@ -132,8 +148,11 @@ export const contractsContextFactory = <
132
148
state : TAppContractsContext < GContractNames > ,
133
149
ethersAdaptor : TEthersAdaptor | undefined
134
150
) : TAppContractsContext < GContractNames > => {
151
+ if ( ethersAdaptor == null || ! isValidEthersAdaptor ( ethersAdaptor ) ) {
152
+ invariant . log ( 'connectToAllContracts: Invalid ethers adaptor' ) ;
153
+ return removeInvalidContracts ( state , ethersAdaptor ) ;
154
+ }
135
155
const newState = cloneContextState ( state ) ;
136
- if ( ethersAdaptor == null || ! isValidEthersAdaptor ( ethersAdaptor ) ) return newState ;
137
156
138
157
const { chainId, signer, provider } = ethersAdaptor ;
139
158
const providerOrSigner = signer ?? provider ;
@@ -161,7 +180,10 @@ export const contractsContextFactory = <
161
180
contractName : GContractNames ,
162
181
ethersAdaptor : TEthersAdaptor | undefined
163
182
) : TAppContractsContext < GContractNames > => {
164
- if ( ethersAdaptor == null || ! isValidEthersAdaptor ( ethersAdaptor ) ) return state ;
183
+ if ( ethersAdaptor == null || ! isValidEthersAdaptor ( ethersAdaptor ) ) {
184
+ invariant . log ( 'connectToAllContracts: Invalid ethers adaptor' ) ;
185
+ return removeInvalidContracts ( state , ethersAdaptor ) ;
186
+ }
165
187
166
188
const newState = cloneContextState ( state ) ;
167
189
const { chainId } = ethersAdaptor ;
@@ -292,14 +314,15 @@ export const contractsContextFactory = <
292
314
const useConnectAppContracts = ( adaptor : TEthersAdaptor | undefined ) : void => {
293
315
const actions = useAppContractsActions ( ) ;
294
316
const queryClient = useQueryClient ( ) ;
317
+ const validAdaptorState = isValidEthersAdaptor ( adaptor ) ;
295
318
296
319
const connect = useCallback ( ( ) => {
297
320
if ( adaptor ?. chainId != null && actions != null ) {
298
321
invalidateCache ( queryClient , keyNamespace . contracts ) ;
299
322
actions . dispatch ( { type : 'CONNECT_TO_CONTRACTS_WITH_ADAPTOR' , payload : { ethersAdaptor : adaptor } } ) ;
300
323
}
301
324
// eslint-disable-next-line react-hooks/exhaustive-deps
302
- } , [ adaptor ?. provider , adaptor ?. signer , adaptor ?. chainId ] ) ;
325
+ } , [ adaptor ?. provider , adaptor ?. signer , adaptor ?. chainId , adaptor ?. account , validAdaptorState ] ) ;
303
326
304
327
useEffect ( ( ) => {
305
328
void connect ( ) ;
0 commit comments