src/account/plausibility.ts
v0.1.0 · 1.2 KB
import type { DecodedState, DecodedView } from './decode.js';
export function viewContradictsState(
first: DecodedState,
second: DecodedState,
view: DecodedView | null,
): string | null {
if (!view || view.slot === null) return null;
if (view.slot < first.slot || view.slot > second.slot) return null;
const viewSet = new Set<string>();
for (const [symbol, vp] of view.positions) {
if (vp.sizeNonZero === null) return null;
if (vp.sizeNonZero) viewSet.add(symbol);
}
const stateSet = new Set(
second.positions.filter((p) => p.subaccountIndex === 0 && p.lots !== 0n).map((p) => p.symbol),
);
const onlyView = [...viewSet].filter((s) => !stateSet.has(s)).sort();
const onlyState = [...stateSet].filter((s) => !viewSet.has(s)).sort();
if (!onlyView.length && !onlyState.length) return null;
return (
`State slots ${first.slot}–${second.slot} contradict equity view slot ${view.slot}: ` +
`${onlyView.length ? `view reports positions absent from state: ${onlyView.join(', ')}` : ''}` +
`${onlyView.length && onlyState.length ? '; ' : ''}` +
`${onlyState.length ? `state reports positions absent from view: ${onlyState.join(', ')}` : ''}`
);
}