1- import { useEffect , useRef } from 'react' ;
1+ import { useEffect , useRef , useState } from 'react' ;
22
3- import { getNetworkConfigFromApi , useGetAccountFromApi } from 'apiCalls' ;
3+ import {
4+ getNetworkConfigFromApi ,
5+ getGasStationMetadataFromApi ,
6+ useGetAccountFromApi
7+ } from 'apiCalls' ;
48import {
59 DEVNET_CHAIN_ID ,
610 MAINNET_CHAIN_ID ,
@@ -46,6 +50,7 @@ import {
4650} from 'reduxStore/slices' ;
4751import { decodeNativeAuthToken } from 'services/nativeAuth/helpers' ;
4852import { LoginMethodsEnum } from 'types/enums.types' ;
53+ import { NetworkType , ApiNetworkConfigType } from 'types/network.types' ;
4954import {
5055 getAddress ,
5156 getLatestNonce ,
@@ -67,7 +72,6 @@ import {
6772 handleGuardianWarning
6873} from './helpers' ;
6974import { useSetLedgerProvider } from './hooks' ;
70-
7175let initalizingLedger = false ;
7276
7377export function ProviderInitializer ( ) {
@@ -85,6 +89,7 @@ export function ProviderInitializer() {
8589 const tokenLogin = useSelector ( tokenLoginSelector ) ;
8690 const userAccount = useSelector ( accountSelector ) ;
8791 const nativeAuthConfig = tokenLogin ?. nativeAuthConfig ;
92+ const [ lastChainId , setLastChainId ] = useState < string > ( chainID ) ;
8893
8994 const loginService = useLoginService (
9095 nativeAuthConfig ? nativeAuthConfig : false
@@ -136,6 +141,51 @@ export function ProviderInitializer() {
136141 }
137142 } , [ isLoggedIn , userAccount ] ) ;
138143
144+ useEffect ( ( ) => {
145+ checkGasMetadata ( ) ;
146+ } , [
147+ userAccount . shard ,
148+ userAccount . address ,
149+ isLoggedIn ,
150+ lastChainId ,
151+ chainID
152+ ] ) ;
153+
154+ async function checkGasMetadata ( ) {
155+ const hasAccountShard = isLoggedIn && userAccount . shard != null ;
156+
157+ if ( ! hasAccountShard ) {
158+ return ;
159+ }
160+
161+ const shard = Number ( userAccount . shard ) ;
162+ const fetchedGasMetadata = await getGasStationMetadataFromApi ( shard ) ;
163+
164+ if ( ! fetchedGasMetadata ?. [ shard ] ?. lastBlock ) {
165+ return ;
166+ }
167+
168+ const hasDifferentGasStationMetadata =
169+ ! network . gasStationMetadata ||
170+ ! network . gasStationMetadata [ shard ] ||
171+ network . gasStationMetadata [ shard ] . lastBlock !==
172+ fetchedGasMetadata [ shard ] . lastBlock ;
173+
174+ const hasDifferentChainId = lastChainId !== chainID ;
175+
176+ if ( hasDifferentChainId ) {
177+ setLastChainId ( chainID ) ;
178+ }
179+
180+ if ( hasDifferentGasStationMetadata || hasDifferentChainId ) {
181+ dispatch (
182+ updateNetworkConfig ( {
183+ gasStationMetadata : fetchedGasMetadata
184+ } )
185+ ) ;
186+ }
187+ }
188+
139189 // We need to get the roundDuration for networks that do not support websocket (e.g. sovereign)
140190 // The round duration is used for polling interval
141191 async function refreshNetworkConfig ( ) {
@@ -146,30 +196,37 @@ export function ProviderInitializer() {
146196 ) &&
147197 ! network . roundDuration ;
148198
149- const shouldGetConfig =
199+ const shouldGetBaseConfig =
150200 ! network . chainId || needsRoundDurationForPollingInterval ;
151201
152- if ( ! shouldGetConfig ) {
202+ if ( ! shouldGetBaseConfig ) {
153203 return ;
154204 }
155205
156206 try {
157- const networkConfig = await getNetworkConfigFromApi ( ) ;
158- const hasDifferentNetworkConfig =
159- networkConfig &&
160- ( network . chainId !== networkConfig . erd_chain_id ||
161- network . roundDuration !== networkConfig . erd_round_duration ) ;
207+ const fetchedNetworkConfig = await getNetworkConfigFromApi ( ) ;
162208
163- if ( hasDifferentNetworkConfig ) {
164- dispatch (
165- updateNetworkConfig ( {
166- chainId : networkConfig . erd_chain_id ,
167- roundDuration : networkConfig . erd_round_duration
168- } )
169- ) ;
209+ const updates : Partial < NetworkType > = { } ;
210+
211+ if ( fetchedNetworkConfig ) {
212+ const networkConfigResult =
213+ fetchedNetworkConfig as ApiNetworkConfigType ;
214+
215+ const hasDifferentNetworkConfig =
216+ network . chainId !== networkConfigResult . erd_chain_id ||
217+ network . roundDuration !== networkConfigResult . erd_round_duration ;
218+
219+ if ( hasDifferentNetworkConfig ) {
220+ updates . chainId = networkConfigResult . erd_chain_id ;
221+ updates . roundDuration = networkConfigResult . erd_round_duration ;
222+ }
223+ }
224+
225+ if ( Object . keys ( updates ) . length > 0 ) {
226+ dispatch ( updateNetworkConfig ( updates ) ) ;
170227 }
171228 } catch ( err ) {
172- console . error ( 'failed refreshing chainId ' , err ) ;
229+ console . error ( 'Failed refreshing network config: ' , err ) ;
173230 }
174231 }
175232
0 commit comments