unrip/src/core/json-state-store.mjs
philipp 57eb540b6e
All checks were successful
deploy / deploy (push) Successful in 22s
Fix shared state persistence
Proof: Liquidity manager must persist live deposit handles and funding observations so the active NEAR Intents proof can reach credited inventory and real execution.

Assumptions: Services commonly mutate store.getState() in place before calling setState, so the state store must preserve same-reference updates instead of clearing them.

Still fake: Strategy and executor remain disarmed, no live inventory is credited yet, and no live mainnet quote response has been sent.
2026-04-02 10:13:25 +02:00

53 lines
1.4 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
export function createJsonStateStore({ stateDir, fileName, initialState = {} }) {
if (!stateDir) throw new Error('Missing stateDir');
if (!fileName) throw new Error('Missing fileName');
fs.mkdirSync(stateDir, { recursive: true });
const filePath = path.join(stateDir, fileName);
const state = loadState(filePath, initialState);
return {
getState() {
return state;
},
setState(nextState) {
replaceState(state, nextState);
persistState(filePath, state);
return state;
},
update(mutator) {
const nextState = mutator(structuredClone(state));
replaceState(state, nextState);
persistState(filePath, state);
return state;
},
};
}
function replaceState(target, nextState) {
const source = nextState === target ? structuredClone(nextState) : (nextState || {});
for (const key of Object.keys(target)) delete target[key];
Object.assign(target, source);
}
function loadState(filePath, initialState) {
if (!fs.existsSync(filePath)) return structuredClone(initialState);
try {
return {
...structuredClone(initialState),
...JSON.parse(fs.readFileSync(filePath, 'utf8')),
};
} catch {
return structuredClone(initialState);
}
}
function persistState(filePath, state) {
const tempPath = `${filePath}.tmp`;
fs.writeFileSync(tempPath, JSON.stringify(state, null, 2));
fs.renameSync(tempPath, filePath);
}