add presist

This commit is contained in:
discountry
2025-09-23 02:01:55 +08:00
parent 667ede7ca8
commit 1386455870
9 changed files with 319 additions and 80 deletions
+37
View File
@@ -0,0 +1,37 @@
import fs from "fs/promises";
import path from "path";
const DATA_DIR = path.resolve(process.cwd(), "data");
let dataDirReady = false;
async function ensureDataDir(): Promise<void> {
if (dataDirReady) return;
await fs.mkdir(DATA_DIR, { recursive: true }).catch(() => undefined);
dataDirReady = true;
}
export async function loadState<T>(fileName: string): Promise<T | null> {
try {
await ensureDataDir();
const filePath = path.join(DATA_DIR, fileName);
const data = await fs.readFile(filePath, "utf8");
return JSON.parse(data) as T;
} catch (error: any) {
if (error?.code === "ENOENT") return null;
console.error(`[state] load ${fileName} failed`, error);
return null;
}
}
export async function saveState(fileName: string, state: unknown): Promise<void> {
try {
await ensureDataDir();
const filePath = path.join(DATA_DIR, fileName);
const tempPath = `${filePath}.tmp`;
const payload = JSON.stringify(state, null, 2);
await fs.writeFile(tempPath, payload, "utf8");
await fs.rename(tempPath, filePath);
} catch (error) {
console.error(`[state] save ${fileName} failed`, error);
}
}
+1 -1
View File
@@ -1,4 +1,4 @@
import { AsterAccountSnapshot, AsterKline } from "../exchanges/types";
import type { AsterAccountSnapshot, AsterKline } from "../exchanges/types";
export interface PositionSnapshot {
positionAmt: number;