Fix shared state persistence
All checks were successful
deploy / deploy (push) Successful in 22s

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.
This commit is contained in:
philipp 2026-04-02 10:13:25 +02:00
parent d6fc99dc60
commit 57eb540b6e
2 changed files with 56 additions and 1 deletions

View file

@ -28,8 +28,9 @@ export function createJsonStateStore({ stateDir, fileName, initialState = {} })
} }
function replaceState(target, nextState) { function replaceState(target, nextState) {
const source = nextState === target ? structuredClone(nextState) : (nextState || {});
for (const key of Object.keys(target)) delete target[key]; for (const key of Object.keys(target)) delete target[key];
Object.assign(target, nextState || {}); Object.assign(target, source);
} }
function loadState(filePath, initialState) { function loadState(filePath, initialState) {

View file

@ -0,0 +1,54 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { createJsonStateStore } from '../src/core/json-state-store.mjs';
test('json state store preserves mutated in-memory state on setState', () => {
const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), 'unrip-json-state-'));
const store = createJsonStateStore({
stateDir,
fileName: 'state.json',
initialState: {
paused: false,
publish_count: 0,
deposit_addresses: {},
},
});
const state = store.getState();
state.publish_count = 1;
state.deposit_addresses['btc:mainnet'] = {
address: 'bc1qexample',
};
store.setState(state);
assert.deepEqual(store.getState(), {
paused: false,
publish_count: 1,
deposit_addresses: {
'btc:mainnet': {
address: 'bc1qexample',
},
},
});
const reloaded = createJsonStateStore({
stateDir,
fileName: 'state.json',
initialState: {},
});
assert.deepEqual(reloaded.getState(), {
paused: false,
publish_count: 1,
deposit_addresses: {
'btc:mainnet': {
address: 'bc1qexample',
},
},
});
});