11import { CosmWasmClient } from '@cosmjs/cosmwasm-stargate'
22import {
3- ChainInfoID ,
43 getChainInfo ,
54 getClient ,
65 useWallet ,
98} from '@marsprotocol/wallet-connector'
109import { useQueryClient } from '@tanstack/react-query'
1110import { MARS_SYMBOL } from 'constants/appConstants'
12- import { NETWORK } from 'constants/env'
1311import {
1412 useBlockHeight ,
1513 useDepositAndDebt ,
@@ -20,12 +18,14 @@ import {
2018 useUserDebt ,
2119 useUserIcns ,
2220} from 'hooks/queries'
21+ import { usePythVaa } from 'hooks/queries/usePythVaa'
2322import { useSpotPrice } from 'hooks/queries/useSpotPrice'
2423import { useUserCollaterals } from 'hooks/queries/useUserCollaterals'
2524import { ReactNode , useEffect , useState } from 'react'
2625import useStore from 'store'
2726import { State } from 'types/enums'
28- import { Network } from 'types/enums/network'
27+
28+ import { MigrationInProgress } from '../MigrationInProgress/MigrationInProgress'
2929
3030interface CommonContainerProps {
3131 children : ReactNode
@@ -35,101 +35,110 @@ export const CommonContainer = ({ children }: CommonContainerProps) => {
3535 // ------------------
3636 // EXTERNAL HOOKS
3737 // ---------------
38- const { recentWallet , simulate, sign, broadcast } = useWallet ( )
39- const { status } = useWalletManager ( )
38+ const { simulate, sign, broadcast } = useWallet ( )
39+ const { status, connectedWallet } = useWalletManager ( )
4040 const queryClient = useQueryClient ( )
4141
42- const chainInfo = recentWallet ?. network
43- ? getChainInfo ( recentWallet ?. network . chainId as ChainInfoID )
44- : undefined
45- const address = status !== WalletConnectionStatus . Connected ? '' : recentWallet ?. account . address
46-
4742 const [ cosmWasmClient , setCosmWasmClient ] = useState < CosmWasmClient | undefined > ( )
4843
4944 // ------------------
5045 // STORE STATE
5146 // ------------------
52- const chainID = useStore ( ( s ) => s . chainInfo ?. chainId )
47+ const assetPricesUSDState = useStore ( ( s ) => s . assetPricesUSDState )
48+ const assetPricesUSD = useStore ( ( s ) => s . assetPricesUSD )
49+ const chainId = useStore ( ( s ) => s . currentNetwork )
5350 const exchangeRates = useStore ( ( s ) => s . exchangeRates )
5451 const exchangeRatesState = useStore ( ( s ) => s . exchangeRatesState )
55- const isNetworkLoaded = useStore ( ( s ) => s . isNetworkLoaded )
56- const rpc = useStore ( ( s ) => s . chainInfo ?. rpc )
52+ const networkConfig = useStore ( ( s ) => s . networkConfig )
5753 const marketDeposits = useStore ( ( s ) => s . marketDeposits )
5854 const marketInfo = useStore ( ( s ) => s . marketInfo )
5955 const marketIncentiveInfo = useStore ( ( s ) => s . marketIncentiveInfo )
56+ const migrationInProgress = useStore ( ( s ) => s . migrationInProgress )
6057 const redBankState = useStore ( ( s ) => s . redBankState )
58+ const rpc = useStore ( ( s ) => s . networkConfig . rpcUrl )
6159 const userBalances = useStore ( ( s ) => s . userBalances )
6260 const userBalancesState = useStore ( ( s ) => s . userBalancesState )
6361 const userDebts = useStore ( ( s ) => s . userDebts )
6462 const userDeposits = useStore ( ( s ) => s . userDeposits )
6563 const userWalletAddress = useStore ( ( s ) => s . userWalletAddress )
6664 const whitelistedAssets = useStore ( ( s ) => s . whitelistedAssets )
67- const loadNetworkConfig = useStore ( ( s ) => s . loadNetworkConfig )
6865 const setRedBankAssets = useStore ( ( s ) => s . setRedBankAssets )
69- const setChainInfo = useStore ( ( s ) => s . setChainInfo )
70- const setCurrentNetwork = useStore ( ( s ) => s . setCurrentNetwork )
7166 const setLcdClient = useStore ( ( s ) => s . setLcdClient )
72- const setClient = useStore ( ( s ) => s . setClient )
67+ const setChainInfo = useStore ( ( s ) => s . setChainInfo )
7368 const setUserBalancesState = useStore ( ( s ) => s . setUserBalancesState )
7469 const setUserWalletAddress = useStore ( ( s ) => s . setUserWalletAddress )
70+ const pythVaa = useStore ( ( s ) => s . pythVaa )
7571
7672 // ------------------
7773 // SETTERS
7874 // ------------------
75+
7976 useEffect ( ( ) => {
80- if ( NETWORK === 'mainnet' ) {
81- setCurrentNetwork ( Network . MAINNET )
77+ if ( status !== WalletConnectionStatus . Connected && cosmWasmClient ) {
78+ setCosmWasmClient ( undefined )
79+ useStore . setState ( {
80+ client : undefined ,
81+ creditManagerClient : undefined ,
82+ accountNftClient : undefined ,
83+ userWalletAddress : '' ,
84+ } )
8285 }
83- loadNetworkConfig ( )
84- } , [ loadNetworkConfig , setCurrentNetwork ] )
86+ } , [ status , cosmWasmClient ] )
8587
8688 useEffect ( ( ) => {
87- if ( ! chainInfo ) return
89+ const chainInfo = getChainInfo ( chainId , {
90+ rpc : networkConfig . rpcUrl ,
91+ rest : networkConfig . restUrl ,
92+ } )
8893 setChainInfo ( chainInfo )
89- } , [ chainInfo , setChainInfo ] )
94+ } , [ chainId , networkConfig , setChainInfo ] )
9095
9196 useEffect ( ( ) => {
92- setUserWalletAddress ( address || '' )
93- } , [ setUserWalletAddress , address ] )
97+ if ( ! connectedWallet || connectedWallet . network . chainId !== chainId ) return
98+ setUserWalletAddress ( connectedWallet . account . address )
99+ } , [ setUserWalletAddress , connectedWallet , chainId ] )
94100
95101 useEffect ( ( ) => {
96- if ( ! rpc || ! chainID ) return
97- setLcdClient ( rpc , chainID )
98- } , [ rpc , chainID , setLcdClient ] )
102+ if ( ! rpc || ! chainId ) return
103+ setLcdClient ( rpc , chainId )
104+ } , [ rpc , chainId , setLcdClient ] )
99105
100106 useEffect ( ( ) => {
101- if ( userDebts && userDeposits && userBalances ) {
107+ if ( userBalances ) {
102108 setUserBalancesState ( State . READY )
103109 } else {
104110 setUserBalancesState ( State . ERROR )
105111 }
106112 } , [ userDebts , userDeposits , userBalances , setUserBalancesState ] )
107113
108114 useEffect ( ( ) => {
109- if ( ! recentWallet ) return
115+ if ( ! connectedWallet || connectedWallet . network . chainId !== chainId ) return
110116 if ( ! cosmWasmClient ) {
111117 const getCosmWasmClient = async ( ) => {
112- const cosmClient = await getClient ( recentWallet . network . rpc )
118+ const cosmClient = await getClient ( networkConfig . rpcUrl )
113119 setCosmWasmClient ( cosmClient )
114120 }
115121
116122 getCosmWasmClient ( )
117123 return
118124 }
119125
120- const client = {
121- broadcast,
122- cosmWasmClient,
123- recentWallet,
124- sign,
125- simulate,
126- }
127- setClient ( client )
128- } , [ simulate , sign , recentWallet , cosmWasmClient , broadcast , setClient ] )
126+ useStore . setState ( {
127+ client : {
128+ broadcast,
129+ cosmWasmClient,
130+ connectedWallet,
131+ sign,
132+ simulate,
133+ } ,
134+ } )
135+ } , [ simulate , sign , connectedWallet , cosmWasmClient , broadcast , networkConfig , chainId ] )
129136
130137 useEffect ( ( ) => {
131138 setRedBankAssets ( )
132139 } , [
140+ assetPricesUSD ,
141+ assetPricesUSDState ,
133142 exchangeRatesState ,
134143 redBankState ,
135144 userBalancesState ,
@@ -151,17 +160,19 @@ export const CommonContainer = ({ children }: CommonContainerProps) => {
151160 // ------------------
152161 // QUERY RELATED
153162 // ------------------
163+ useUsdPrice ( )
154164 useBlockHeight ( )
155- useRedBank ( )
156165 useUserBalance ( )
157166 useUserIcns ( )
158167 useUserDebt ( )
159168 useUserCollaterals ( )
160169 useMarsOracle ( )
161170 useSpotPrice ( MARS_SYMBOL )
162- useUsdPrice ( )
163171 useDepositAndDebt ( )
164172 useRedBank ( )
173+ usePythVaa ( )
174+
175+ if ( migrationInProgress ) return < MigrationInProgress />
165176
166- return < > { isNetworkLoaded && children } </ >
177+ return < > { children } </ >
167178}
0 commit comments