Bot & Automation
Treding Forex Ai
/root/hermes-projects/Treding Forex Ai
dist/src/services/storage/jsonStore.js
text
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonStore = void 0;
const promises_1 = require("node:fs/promises");
const node_path_1 = require("node:path");
class JsonStore {
dataDir;
constructor(dataDir) {
this.dataDir = dataDir;
}
async savePendingOrder(order) {
const store = await this.readStore();
const nextOrders = store.pendingOrders.filter((item) => item.id !== order.id).concat(order);
await this.writeStore({ ...store, pendingOrders: nextOrders });
}
async getPendingOrder(orderId) {
const store = await this.readStore();
return store.pendingOrders.find((order) => order.id === orderId) ?? null;
}
async removePendingOrder(orderId) {
const store = await this.readStore();
await this.writeStore({
...store,
pendingOrders: store.pendingOrders.filter((order) => order.id !== orderId)
});
}
async appendPaperTrade(trade) {
const store = await this.readStore();
await this.writeStore({
...store,
paperTrades: store.paperTrades.concat(trade)
});
}
async listOpenPaperTrades(chatId) {
const store = await this.readStore();
return store.paperTrades.filter((trade) => trade.chatId === chatId && trade.status === "executed" && !trade.closedAt);
}
async listTodayPaperTrades(chatId) {
const today = new Date().toISOString().slice(0, 10);
const store = await this.readStore();
return store.paperTrades.filter((trade) => trade.chatId === chatId && trade.executedAt.startsWith(today));
}
async listAllOpenTrades() {
const store = await this.readStore();
return store.paperTrades.filter((trade) => trade.status === "executed" && !trade.closedAt);
}
async closeTrade(orderId, closePrice, pnl) {
const store = await this.readStore();
const index = store.paperTrades.findIndex((trade) => trade.id === orderId);
if (index === -1) {
return null;
}
const updated = {
...store.paperTrades[index],
closedAt: new Date().toISOString(),
closePrice,
pnl
};
const nextTrades = [...store.paperTrades];
nextTrades[index] = updated;
await this.writeStore({ ...store, paperTrades: nextTrades });
return updated;
}
async addWatchlist(chatId, symbolCode) {
const store = await this.readStore();
const exists = store.watchlists.some((entry) => entry.chatId === chatId && entry.symbolCode === symbolCode);
if (exists) {
return;
}
await this.writeStore({
...store,
watchlists: store.watchlists.concat({
chatId,
symbolCode,
addedAt: new Date().toISOString()
})
});
}
async listWatchlist(chatId) {
const store = await this.readStore();
return store.watchlists.filter((entry) => entry.chatId === chatId);
}
async readStore() {
await (0, promises_1.mkdir)(this.dataDir, { recursive: true });
const path = this.storePath();
try {
const raw = await (0, promises_1.readFile)(path, "utf8");
return JSON.parse(raw);
}
catch {
return {
pendingOrders: [],
paperTrades: [],
watchlists: []
};
}
}
async writeStore(store) {
await (0, promises_1.mkdir)(this.dataDir, { recursive: true });
await (0, promises_1.writeFile)(this.storePath(), JSON.stringify(store, null, 2), "utf8");
}
storePath() {
return (0, node_path_1.join)(this.dataDir, "store.json");
}
}
exports.JsonStore = JsonStore;