Skip to content
The Chat Archive
gpt-3.5-turbo-0613· OpenAI WildChat-4.8M (AI2)

TradeInit : {exchange: "SP:BTCUSDT",…} exchange : "SP:BTCUSDT" items : [{timestamp:…

  1. 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;

  2. 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_float and price_micro are the data from price.

    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 trades data using state.orderClusters.trades.

  3. 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

  4. Assistant

    Sorry for the confusion. Here's an updated version of the code with the correct TradeInit object 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 TradeInit data correctly.