diff --git a/state/futures/actions.ts b/state/futures/actions.ts index 46da3fbd2d..492a6145fb 100644 --- a/state/futures/actions.ts +++ b/state/futures/actions.ts @@ -87,6 +87,7 @@ import { selectMarketAssetRate, selectMarketInfo, selectMarketKey, + selectMarkets, selectOrderType, selectPosition, selectTradeSizeInputs, @@ -99,20 +100,27 @@ import { ModifyIsolatedPositionInputs, } from './types'; -export const fetchMarkets = createAsyncThunk< - { markets: FuturesMarket[]; fundingRates: FundingRateSerialized[] }, - void, - ThunkConfig ->('futures/fetchMarkets', async (_, { extra: { sdk } }) => { - const markets = await sdk.futures.getMarkets(); - const serializedMarkets = serializeMarkets(markets); - const averageFundingRates = await sdk.futures.getAverageFundingRates(markets, Period.ONE_HOUR); - const seriailizedRates = averageFundingRates.map((r) => ({ - ...r, - fundingRate: r.fundingRate ? r.fundingRate.toString() : null, - })); - return { markets: serializedMarkets, fundingRates: seriailizedRates }; -}); +export const fetchMarkets = createAsyncThunk[], void, ThunkConfig>( + 'futures/fetchMarkets', + async (_, { extra: { sdk } }) => { + const markets = await sdk.futures.getMarkets(); + const serializedMarkets = serializeMarkets(markets); + return serializedMarkets; + } +); + +export const fetchFundingRates = createAsyncThunk( + 'futures/fetchFundingRates', + async (_, { getState, extra: { sdk } }) => { + const markets = selectMarkets(getState()); + const averageFundingRates = await sdk.futures.getAverageFundingRates(markets, Period.ONE_HOUR); + const seriailizedRates = averageFundingRates.map((r) => ({ + ...r, + fundingRate: r.fundingRate ? r.fundingRate.toString() : null, + })); + return seriailizedRates; + } +); export const fetchCrossMarginBalanceInfo = createAsyncThunk< CrossMarginBalanceInfo, diff --git a/state/futures/hooks.ts b/state/futures/hooks.ts index 0c59abd230..2ac04daf5b 100644 --- a/state/futures/hooks.ts +++ b/state/futures/hooks.ts @@ -5,6 +5,7 @@ import { selectNetwork, selectWallet } from 'state/wallet/selectors'; import { fetchCrossMarginAccountData, fetchCrossMarginSettings, + fetchFundingRates, fetchIsolatedMarginAccountData, fetchMarkets, fetchOpenOrders, @@ -52,6 +53,12 @@ export const usePollDashboardFuturesData = () => { const crossMarginAddress = useAppSelector(selectCrossMarginAccount); usePollAction('fetchMarkets', fetchMarkets, { intervalTime: 60000, dependencies: [networkId] }); + usePollAction('fetchFundingRates', fetchFundingRates, { + intervalTime: 60000, + disabled: markets.length === 0, + dependencies: [networkId, markets.length], + }); + usePollAction('fetchIsolatedMarginAccountData', fetchIsolatedMarginAccountData, { intervalTime: 30000, dependencies: [wallet, markets.length, networkId], diff --git a/state/futures/reducer.ts b/state/futures/reducer.ts index 3402c36923..ed2063b8fa 100644 --- a/state/futures/reducer.ts +++ b/state/futures/reducer.ts @@ -22,6 +22,7 @@ import { fetchIsolatedMarginTradePreview, fetchCrossMarginTradePreview, fetchKeeperEthBalance, + fetchFundingRates, } from './actions'; import { CrossMarginTradeFees, @@ -85,6 +86,7 @@ const initialState: FuturesState = { crossMarginSettings: DEFAULT_QUERY_STATUS, isolatedTradePreview: DEFAULT_QUERY_STATUS, crossMarginTradePreview: DEFAULT_QUERY_STATUS, + fetchFundingRates: DEFAULT_QUERY_STATUS, }, transaction: undefined, transactionEstimations: {} as TransactionEstimations, @@ -278,8 +280,7 @@ const futuresSlice = createSlice({ }); builder.addCase(fetchMarkets.fulfilled, (futuresState, action) => { futuresState.queryStatuses.markets = SUCCESS_STATUS; - futuresState.markets = action.payload.markets; - futuresState.fundingRates = action.payload.fundingRates; + futuresState.markets = action.payload; }); builder.addCase(fetchMarkets.rejected, (futuresState) => { futuresState.queryStatuses.markets = { @@ -429,6 +430,21 @@ const futuresSlice = createSlice({ builder.addCase(fetchKeeperEthBalance.fulfilled, (futuresState, action) => { futuresState.crossMargin.balanceInfo.keeperEthBal = action.payload; }); + + // Fetch funding rates + builder.addCase(fetchFundingRates.pending, (futuresState) => { + futuresState.queryStatuses.fetchFundingRates = LOADING_STATUS; + }); + builder.addCase(fetchFundingRates.fulfilled, (futuresState, action) => { + futuresState.queryStatuses.fetchFundingRates = SUCCESS_STATUS; + futuresState.fundingRates = action.payload; + }); + builder.addCase(fetchFundingRates.rejected, (futuresState) => { + futuresState.queryStatuses.fetchFundingRates = { + status: FetchStatus.Error, + error: 'Failed to fetch funding rates', + }; + }); }, }); diff --git a/state/futures/types.ts b/state/futures/types.ts index 76f2d162fb..001fff4dab 100644 --- a/state/futures/types.ts +++ b/state/futures/types.ts @@ -46,6 +46,7 @@ export type FundingRate = { export type FuturesQueryStatuses = { markets: QueryStatus; + fetchFundingRates: QueryStatus; crossMarginBalanceInfo: QueryStatus; dailyVolumes: QueryStatus; crossMarginPositions: QueryStatus;