2023-05-29 21:36:23 +00:00
|
|
|
import { defineStore } from 'pinia';
|
|
|
|
import { useBotStore } from './ftbotwrapper';
|
|
|
|
|
2023-05-30 11:41:37 +00:00
|
|
|
import {
|
|
|
|
Pairlist,
|
|
|
|
PairlistConfig,
|
|
|
|
PairlistParamType,
|
|
|
|
PairlistPayloadItem,
|
|
|
|
PairlistsPayload,
|
|
|
|
} from '@/types';
|
2023-05-29 21:36:23 +00:00
|
|
|
import { computed, ref, toRaw } from 'vue';
|
|
|
|
import { showAlert } from './alerts';
|
|
|
|
|
2023-05-29 22:24:48 +00:00
|
|
|
export const usePairlistConfigStore = defineStore(
|
|
|
|
'pairlistConfig',
|
|
|
|
() => {
|
|
|
|
const botStore = useBotStore();
|
2023-05-29 21:36:23 +00:00
|
|
|
|
2023-05-29 22:24:48 +00:00
|
|
|
const evaluating = ref<boolean>(false);
|
|
|
|
const intervalId = ref<number>();
|
|
|
|
const whitelist = ref<string[]>([]);
|
2023-05-30 11:28:55 +00:00
|
|
|
const blacklist = ref<string[]>([]);
|
2023-05-29 22:24:48 +00:00
|
|
|
const config = ref<PairlistConfig>({ name: '', pairlists: [] });
|
|
|
|
const savedConfigs = ref<PairlistConfig[]>([]);
|
2023-05-29 21:36:23 +00:00
|
|
|
|
2023-05-29 22:24:48 +00:00
|
|
|
const firstPairlistIsGenerator = computed<boolean>(() => {
|
|
|
|
// First pairlist must be a generator
|
|
|
|
if (config.value.pairlists[0]?.is_pairlist_generator) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
2023-05-29 21:36:23 +00:00
|
|
|
|
2023-05-29 22:24:48 +00:00
|
|
|
const pairlistValid = computed<boolean>(() => {
|
|
|
|
return firstPairlistIsGenerator.value && config.value.pairlists.length > 0;
|
|
|
|
});
|
2023-05-29 21:36:23 +00:00
|
|
|
|
2023-05-30 11:41:37 +00:00
|
|
|
const configJSON = computed(() => {
|
|
|
|
return JSON.stringify(configToPayloadItems(), null, 2);
|
|
|
|
});
|
2023-05-29 21:36:23 +00:00
|
|
|
|
2023-05-29 22:24:48 +00:00
|
|
|
function addToConfig(pairlist: Pairlist, index: number) {
|
|
|
|
pairlist = structuredClone(toRaw(pairlist));
|
|
|
|
for (const param in pairlist.params) {
|
|
|
|
pairlist.params[param].value = pairlist.params[param].default
|
|
|
|
? pairlist.params[param].default.toString()
|
|
|
|
: '';
|
|
|
|
}
|
|
|
|
config.value.pairlists.splice(index, 0, pairlist);
|
2023-05-29 21:36:23 +00:00
|
|
|
}
|
|
|
|
|
2023-05-29 22:24:48 +00:00
|
|
|
function removeFromConfig(index: number) {
|
|
|
|
config.value.pairlists.splice(index, 1);
|
|
|
|
}
|
2023-05-29 21:36:23 +00:00
|
|
|
|
2023-05-30 10:42:17 +00:00
|
|
|
function saveConfig() {
|
2023-05-29 22:24:48 +00:00
|
|
|
const i = savedConfigs.value.findIndex((c) => c.name === config.value.name);
|
2023-05-29 21:36:23 +00:00
|
|
|
|
2023-05-29 22:24:48 +00:00
|
|
|
if (i > -1) {
|
2023-05-30 10:42:17 +00:00
|
|
|
savedConfigs.value[i] = config.value;
|
2023-05-29 22:24:48 +00:00
|
|
|
} else {
|
2023-05-30 10:42:17 +00:00
|
|
|
savedConfigs.value.push(config.value);
|
2023-05-29 22:24:48 +00:00
|
|
|
}
|
2023-05-30 10:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function newConfig() {
|
|
|
|
config.value = { name: '', pairlists: [] };
|
|
|
|
}
|
|
|
|
|
|
|
|
function selectConfig(selected: PairlistConfig) {
|
|
|
|
config.value = structuredClone(toRaw(selected));
|
|
|
|
}
|
2023-05-29 21:36:23 +00:00
|
|
|
|
2023-05-30 11:28:55 +00:00
|
|
|
function addToBlacklist() {
|
|
|
|
blacklist.value.push('');
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeFromBlacklist(index: number) {
|
|
|
|
blacklist.value.splice(index, 1);
|
|
|
|
}
|
|
|
|
|
2023-05-30 11:41:37 +00:00
|
|
|
async function startPairlistEvaluation() {
|
|
|
|
const payload: PairlistsPayload = configToPayload();
|
|
|
|
|
|
|
|
evaluating.value = true;
|
|
|
|
await botStore.activeBot.evaluatePairlist(payload);
|
|
|
|
|
|
|
|
intervalId.value = setInterval(async () => {
|
|
|
|
const res = await botStore.activeBot.getPairlistEvalStatus();
|
|
|
|
if (res.status === 'success' && res.result) {
|
|
|
|
clearInterval(intervalId.value);
|
|
|
|
evaluating.value = false;
|
|
|
|
whitelist.value = res.result.whitelist;
|
|
|
|
} else if (res.error) {
|
|
|
|
showAlert(res.error, 'danger');
|
|
|
|
clearInterval(intervalId.value);
|
|
|
|
evaluating.value = false;
|
|
|
|
}
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
function convertToParamType(type: PairlistParamType, value: string) {
|
|
|
|
if (type === PairlistParamType.number) {
|
|
|
|
return Number(value);
|
|
|
|
} else if (type === PairlistParamType.boolean) {
|
|
|
|
return Boolean(value);
|
|
|
|
} else {
|
|
|
|
return String(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function configToPayload() {
|
|
|
|
const pairlists: PairlistPayloadItem[] = configToPayloadItems();
|
|
|
|
return {
|
|
|
|
pairlists: pairlists,
|
|
|
|
stake_currency: botStore.activeBot.stakeCurrency,
|
|
|
|
blacklist: blacklist.value,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function configToPayloadItems() {
|
|
|
|
const pairlists: PairlistPayloadItem[] = [];
|
|
|
|
config.value.pairlists.forEach((config) => {
|
|
|
|
const pairlist = {
|
|
|
|
method: config.name,
|
|
|
|
};
|
|
|
|
for (const key in config.params) {
|
|
|
|
const param = config.params[key];
|
|
|
|
if (param.value) {
|
|
|
|
pairlist[key] = convertToParamType(param.type, param.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pairlists.push(pairlist);
|
|
|
|
});
|
|
|
|
|
|
|
|
return pairlists;
|
|
|
|
}
|
|
|
|
|
2023-05-29 22:24:48 +00:00
|
|
|
return {
|
|
|
|
evaluating,
|
|
|
|
whitelist,
|
|
|
|
config,
|
2023-05-30 11:41:37 +00:00
|
|
|
configJSON,
|
2023-05-29 22:24:48 +00:00
|
|
|
savedConfigs,
|
2023-05-30 11:28:55 +00:00
|
|
|
blacklist,
|
2023-05-29 22:24:48 +00:00
|
|
|
startPairlistEvaluation,
|
|
|
|
addToConfig,
|
|
|
|
removeFromConfig,
|
|
|
|
saveConfig,
|
2023-05-30 10:42:17 +00:00
|
|
|
selectConfig,
|
|
|
|
newConfig,
|
2023-05-30 11:28:55 +00:00
|
|
|
addToBlacklist,
|
|
|
|
removeFromBlacklist,
|
2023-05-29 22:24:48 +00:00
|
|
|
firstPairlistIsGenerator,
|
|
|
|
pairlistValid,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
{
|
|
|
|
persist: {
|
|
|
|
key: 'pairlist-configs',
|
2023-05-30 11:28:55 +00:00
|
|
|
paths: ['savedConfigs', 'blacklist'],
|
2023-05-29 22:24:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|