TradeInit : {exchange: "SP:BTCUSDT",…} exchange : "SP:BTCUSDT" items : [{timestamp:…
- User
TradeInit : {exchange: "SP:BTCUSDT",…} exchange : "SP:BTCUSDT" items : [{timestamp: 1690705038079, value: {Buy: {price: 2929665, quantity: 689}}},…] [0 … 99] 0 : {timestamp: 1690705038079, value: {Buy: {price: 2929665, quantity: 689}}} 1 : {timestamp: 1690705038944, value: {Sell: {price: 2929666, quantity: 146}}} timestamp : 1690705038944 value : {Sell: {price: 2929666, quantity: 146}} Sell : {price: 2929666, quantity: 146} price : 2929666 quantity : 146
с сервера приходит такой объект, нужно добавлять его в state с таким типом export interface InitialTradeItem { price_float: number; price_micro: number; quantity: number; side: string; time: number; } price_float и price_micro это данные price диспатчить уже обработанные данные с типом InitialTradeItem dispatch(setOrderClusterTrades({ trades: tradeInit.items, symbol: tradeInit.exchange.replace("SP:", "").toLowerCase(), }));
import {createSlice} from "@reduxjs/toolkit"; import {HYDRATE} from "next-redux-wrapper"; import {InitialTradeItem} from "../hooks/rustWsServer";
interface OrderClusterSlice { trades: {[key: string]: Array}; }
const initialState: OrderClusterSlice = { trades: {}, };
export const orderClustersSlice = createSlice({ name: "orderClustersSlice", initialState, reducers: { setOrderClusterTrades(state, action) { const {symbol, trades} = action.payload as {symbol: string, trades: Array}; state.trades[symbol] = trades; }, }, extraReducers: { [HYDRATE]: (state, action) => { return { ...state, ...action.payload, }; }, }, });
export const { setOrderClusterTrades, } = orderClustersSlice.actions;
export default orderClustersSlice.reducer;
- Assistant
The server is sending an object that needs to be added to the state with the following type:
export interface InitialTradeItem { price_float: number; price_micro: number; quantity: number; side: string; time: number; }price_floatandprice_microare the data fromprice.The processed data should be dispatched with the type
InitialTradeItem.dispatch(setOrderClusterTrades({ trades: tradeInit.items, symbol: tradeInit.exchange.replace("SP:", "").toLowerCase(), }));Here's an example of how to implement this logic with Redux Toolkit:
import { createSlice } from "@reduxjs/toolkit"; import { HYDRATE } from "next-redux-wrapper"; import { InitialTradeItem } from "../hooks/rustWsServer"; interface OrderClusterSlice { trades: { [key: string]: Array<InitialTradeItem> }; } const initialState: OrderClusterSlice = { trades: {}, }; export const orderClustersSlice = createSlice({ name: "orderClustersSlice", initialState, reducers: { setOrderClusterTrades(state, action) { const { symbol, trades } = action.payload as { symbol: string; trades: Array<InitialTradeItem>; }; state.trades[symbol] = trades; }, }, extraReducers: { [HYDRATE]: (state, action) => { return { ...state, ...action.payload, }; }, }, }); export const { setOrderClusterTrades } = orderClustersSlice.actions; export default orderClustersSlice.reducer;You can then use this slice in your Redux store and access the
tradesdata usingstate.orderClusters.trades. - User
ты долбаеб, tradeInit приходит TradeInit : {exchange: "SP:BTCUSDT",…} exchange : "SP:BTCUSDT" items : [{timestamp: 1690705038079, value: {Buy: {price: 2929665, quantity: 689}}},…] [0 … 99] 0 : {timestamp: 1690705038079, value: {Buy: {price: 2929665, quantity: 689}}} 1 : {timestamp: 1690705038944, value: {Sell: {price: 2929666, quantity: 146}}} timestamp : 1690705038944 value : {Sell: {price: 2929666, quantity: 146}} Sell : {price: 2929666, quantity: 146} price : 2929666 quantity : 146
- Assistant
Sorry for the confusion. Here's an updated version of the code with the correct
TradeInitobject structure:import { createSlice } from "@reduxjs/toolkit"; import { HYDRATE } from "next-redux-wrapper"; interface TradeItem { timestamp: number; value: { Buy?: { price: number; quantity: number; }; Sell?: { price: number; quantity: number; }; }; } interface OrderClusterSlice { trades: { [symbol: string]: Array<TradeItem> }; } const initialState: OrderClusterSlice = { trades: {}, }; export const orderClustersSlice = createSlice({ name: "orderClustersSlice", initialState, reducers: { setOrderClusterTrades(state, action) { const { symbol, trades } = action.payload as { symbol: string; trades: Array<TradeItem>; }; state.trades[symbol] = trades.map((item) => { const side = item.value.Buy ? "Buy" : "Sell"; return { price_float: item.value[side].price, price_micro: 0, // Add logic here if needed quantity: item.value[side].quantity, side, time: item.timestamp, }; }); }, }, extraReducers: { [HYDRATE]: (state, action) => { return { ...state, ...action.payload, }; }, }, }); export const { setOrderClusterTrades } = orderClustersSlice.actions; export default orderClustersSlice.reducer;You can use this updated code in your Redux store to handle the
TradeInitdata correctly.