From a2d8b68abbe6dcfdbb79b03957c059be77165ca9 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 28 Aug 2021 17:39:12 +0200 Subject: [PATCH] Add botstorewrapper --- src/store/index.ts | 9 ++-- src/store/modules/botStoreWrapper.ts | 36 ++++++++++++++ src/store/modules/ftbot/index.ts | 4 ++ src/store/modules/ftbot/state.ts | 73 ++++++++++++++-------------- 4 files changed, 82 insertions(+), 40 deletions(-) create mode 100644 src/store/modules/botStoreWrapper.ts diff --git a/src/store/index.ts b/src/store/index.ts index 406b9fb4..9aad6184 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -4,7 +4,8 @@ import Vuex from 'vuex'; import userService from '@/shared/userService'; import { getCurrentTheme, getTheme, storeCurrentTheme } from '@/shared/themes'; import axios, { AxiosInstance } from 'axios'; -import { createBotSubStore, BotStoreGetters } from './modules/ftbot'; +import createBotStore from './modules/botStoreWrapper'; +import { BotStoreGetters } from './modules/ftbot'; import alertsModule from './modules/alerts'; import layoutModule from './modules/layout'; import settingsModule from './modules/settings'; @@ -16,7 +17,7 @@ const initCurrentTheme = getCurrentTheme(); export default new Vuex.Store({ modules: { - ftbot: createBotSubStore(), + ftbot: createBotStore(), alerts: alertsModule, layout: layoutModule, uiSettings: settingsModule, @@ -122,7 +123,7 @@ export default new Vuex.Store({ commit('setRefreshing', false); } }, - async refreshSlow({ dispatch, commit, getters, state }, forceUpdate = false) { + async refreshSlow({ dispatch, getters, state }, forceUpdate = false) { if (state.refreshing && !forceUpdate) { return; } @@ -137,7 +138,7 @@ export default new Vuex.Store({ updates.push(dispatch('ftbot/getBlacklist')); await Promise.all(updates); - commit('ftbot/updateRefreshRequired', false); + dispatch('ftbot/setRefreshRequired', false); } }, refreshFrequent({ dispatch }, slow = true) { diff --git a/src/store/modules/botStoreWrapper.ts b/src/store/modules/botStoreWrapper.ts new file mode 100644 index 00000000..540afb15 --- /dev/null +++ b/src/store/modules/botStoreWrapper.ts @@ -0,0 +1,36 @@ +import { BotStoreActions, BotStoreGetters, createBotSubStore } from './ftbot'; + +export default function createBotStore() { + const state = { + selectedBot: 'ftbot.0', + }; + const actions = { + // Actions automatically filled below + addBot(context, botName: string) { + // + }, + }; + const getters = {}; + // Autocreate getters + Object.keys(BotStoreGetters).forEach((e) => { + getters[e] = (state, getters) => { + return getters[`${state.selectedBot}/${e}`]; + }; + }); + // Autocreate Actions + Object.keys(BotStoreActions).forEach((e) => { + actions[e] = ({ state, dispatch }, ...args) => { + return dispatch(`${state.selectedBot}/${e}`, ...args); + }; + }); + + return { + namespaced: true, + modules: { + 'ftbot.0': createBotSubStore(), + }, + state, + getters, + actions, + }; +} diff --git a/src/store/modules/ftbot/index.ts b/src/store/modules/ftbot/index.ts index 9cbd7a03..f7e80a98 100644 --- a/src/store/modules/ftbot/index.ts +++ b/src/store/modules/ftbot/index.ts @@ -87,6 +87,7 @@ export enum BotStoreGetters { export enum BotStoreActions { ping = 'ping', + setRefreshRequired = 'setRefreshRequired', setDetailTrade = 'setDetailTrade', setSelectedPair = 'setSelectedPair', getTrades = 'getTrades', @@ -388,6 +389,9 @@ export function createBotSubStore() { .catch(console.error); } }, + [BotStoreActions.setRefreshRequired]({ commit }, refreshRequired: boolean) { + commit('setRefreshRequired', refreshRequired); + }, [BotStoreActions.setDetailTrade]({ commit }, trade: Trade) { commit('setDetailTrade', trade); }, diff --git a/src/store/modules/ftbot/state.ts b/src/store/modules/ftbot/state.ts index aa6de438..d7ada007 100644 --- a/src/store/modules/ftbot/state.ts +++ b/src/store/modules/ftbot/state.ts @@ -54,41 +54,42 @@ export interface FtbotStateType { backtestHistory: Record; } -const state: FtbotStateType = { - version: '', - lastLogs: [], - refreshRequired: true, - trades: [], - openTrades: [], - tradeCount: 0, - performanceStats: [], - whitelist: [], - blacklist: [], - profit: {}, - botState: undefined, - balance: {}, - dailyStats: {}, - pairlistMethods: [], - detailTradeId: undefined, - selectedPair: '', - candleData: {}, - history: {}, - strategyPlotConfig: undefined, - customPlotConfig: {}, - plotConfigName: getPlotConfigName(), - availablePlotConfigNames: getAllPlotConfigNames(), - strategyList: [], - strategy: {}, - pairlist: [], - currentLocks: undefined, - // backtesting - backtestRunning: false, - backtestProgress: 0.0, - backtestStep: BacktestSteps.none, - backtestTradeCount: 0, - backtestResult: undefined, - selectedBacktestResultKey: '', - backtestHistory: {}, +const state = (): FtbotStateType => { + return { + version: '', + lastLogs: [], + refreshRequired: true, + trades: [], + openTrades: [], + tradeCount: 0, + performanceStats: [], + whitelist: [], + blacklist: [], + profit: {}, + botState: undefined, + balance: {}, + dailyStats: {}, + pairlistMethods: [], + detailTradeId: undefined, + selectedPair: '', + candleData: {}, + history: {}, + strategyPlotConfig: undefined, + customPlotConfig: {}, + plotConfigName: getPlotConfigName(), + availablePlotConfigNames: getAllPlotConfigNames(), + strategyList: [], + strategy: {}, + pairlist: [], + currentLocks: undefined, + // backtesting + backtestRunning: false, + backtestProgress: 0.0, + backtestStep: BacktestSteps.none, + backtestTradeCount: 0, + backtestResult: undefined, + selectedBacktestResultKey: '', + backtestHistory: {}, + }; }; - export default state;