@dsh-ft/dsh-fft-paper-trading 0.7.2 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -3
- package/README.zh.md +4 -3
- package/lib/agent/account-tools.d.ts +18 -0
- package/lib/agent/account-tools.d.ts.map +1 -0
- package/lib/agent/account-tools.js +127 -0
- package/lib/agent/account-tools.js.map +1 -0
- package/lib/agent/futures-tools.d.ts.map +1 -1
- package/lib/agent/futures-tools.js +1 -6
- package/lib/agent/futures-tools.js.map +1 -1
- package/lib/agent/ledger-tools.d.ts +15 -3
- package/lib/agent/ledger-tools.d.ts.map +1 -1
- package/lib/agent/ledger-tools.js +11 -15
- package/lib/agent/ledger-tools.js.map +1 -1
- package/lib/agent/tool-result.d.ts +19 -0
- package/lib/agent/tool-result.d.ts.map +1 -0
- package/lib/agent/tool-result.js +22 -0
- package/lib/agent/tool-result.js.map +1 -0
- package/lib/agent/tools.d.ts +10 -2
- package/lib/agent/tools.d.ts.map +1 -1
- package/lib/agent/tools.js +26 -11
- package/lib/agent/tools.js.map +1 -1
- package/lib/client.js +1448 -282
- package/lib/contract/accounts.d.ts +76 -0
- package/lib/contract/accounts.d.ts.map +1 -0
- package/lib/contract/accounts.js +6 -0
- package/lib/contract/accounts.js.map +1 -0
- package/lib/contract/common.d.ts +144 -0
- package/lib/contract/common.d.ts.map +1 -0
- package/lib/contract/common.js +33 -0
- package/lib/contract/common.js.map +1 -0
- package/lib/contract/domain.d.ts +196 -0
- package/lib/contract/domain.d.ts.map +1 -0
- package/lib/contract/domain.js +14 -0
- package/lib/contract/domain.js.map +1 -0
- package/lib/contract/errors.d.ts +6 -0
- package/lib/contract/errors.d.ts.map +1 -0
- package/lib/contract/errors.js +6 -0
- package/lib/contract/errors.js.map +1 -0
- package/lib/contract/index.d.ts +15 -0
- package/lib/contract/index.d.ts.map +1 -0
- package/lib/contract/index.js +15 -0
- package/lib/contract/index.js.map +1 -0
- package/lib/contract/ledger.d.ts +40 -0
- package/lib/contract/ledger.d.ts.map +1 -0
- package/lib/contract/ledger.js +6 -0
- package/lib/contract/ledger.js.map +1 -0
- package/lib/contract/market.d.ts +30 -0
- package/lib/contract/market.d.ts.map +1 -0
- package/lib/contract/market.js +6 -0
- package/lib/contract/market.js.map +1 -0
- package/lib/contract/rpc.d.ts +24 -0
- package/lib/contract/rpc.d.ts.map +1 -0
- package/lib/contract/rpc.js +6 -0
- package/lib/contract/rpc.js.map +1 -0
- package/lib/contract.d.ts +6 -403
- package/lib/contract.d.ts.map +1 -1
- package/lib/contract.js +6 -24
- package/lib/contract.js.map +1 -1
- package/lib/core/session.d.ts +1 -1
- package/lib/core/session.d.ts.map +1 -1
- package/lib/core/session.js +1 -2
- package/lib/core/session.js.map +1 -1
- package/lib/data/cached-market-data.d.ts +61 -0
- package/lib/data/cached-market-data.d.ts.map +1 -0
- package/lib/data/cached-market-data.js +174 -0
- package/lib/data/cached-market-data.js.map +1 -0
- package/lib/data/futures-market-data.d.ts.map +1 -1
- package/lib/data/futures-market-data.js +9 -10
- package/lib/data/futures-market-data.js.map +1 -1
- package/lib/index.d.ts +25 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +156 -18
- package/lib/index.js.map +1 -1
- package/lib/service/account-moves.d.ts +64 -0
- package/lib/service/account-moves.d.ts.map +1 -0
- package/lib/service/account-moves.js +49 -0
- package/lib/service/account-moves.js.map +1 -0
- package/lib/service/accounts-rpc.d.ts +17 -0
- package/lib/service/accounts-rpc.d.ts.map +1 -0
- package/lib/service/accounts-rpc.js +77 -0
- package/lib/service/accounts-rpc.js.map +1 -0
- package/lib/service/compare-rpc.d.ts +22 -0
- package/lib/service/compare-rpc.d.ts.map +1 -0
- package/lib/service/compare-rpc.js +70 -0
- package/lib/service/compare-rpc.js.map +1 -0
- package/lib/service/errors.d.ts +20 -4
- package/lib/service/errors.d.ts.map +1 -1
- package/lib/service/errors.js +12 -5
- package/lib/service/errors.js.map +1 -1
- package/lib/service/futures-rpc.d.ts.map +1 -1
- package/lib/service/futures-rpc.js +7 -25
- package/lib/service/futures-rpc.js.map +1 -1
- package/lib/service/futures-service.d.ts +28 -8
- package/lib/service/futures-service.d.ts.map +1 -1
- package/lib/service/futures-service.js +144 -146
- package/lib/service/futures-service.js.map +1 -1
- package/lib/service/ledger-admin.d.ts +67 -0
- package/lib/service/ledger-admin.d.ts.map +1 -0
- package/lib/service/ledger-admin.js +102 -0
- package/lib/service/ledger-admin.js.map +1 -0
- package/lib/service/paper-service.d.ts +39 -12
- package/lib/service/paper-service.d.ts.map +1 -1
- package/lib/service/paper-service.js +177 -156
- package/lib/service/paper-service.js.map +1 -1
- package/lib/service/route.d.ts.map +1 -1
- package/lib/service/route.js +5 -2
- package/lib/service/route.js.map +1 -1
- package/lib/service/rpc-support.d.ts +34 -0
- package/lib/service/rpc-support.d.ts.map +1 -0
- package/lib/service/rpc-support.js +57 -0
- package/lib/service/rpc-support.js.map +1 -0
- package/lib/service/rpc.d.ts +20 -3
- package/lib/service/rpc.d.ts.map +1 -1
- package/lib/service/rpc.js +44 -25
- package/lib/service/rpc.js.map +1 -1
- package/lib/store/accounts.d.ts +110 -0
- package/lib/store/accounts.d.ts.map +1 -0
- package/lib/store/accounts.js +352 -0
- package/lib/store/accounts.js.map +1 -0
- package/lib/store/atomic-file.d.ts +16 -0
- package/lib/store/atomic-file.d.ts.map +1 -0
- package/lib/store/atomic-file.js +34 -0
- package/lib/store/atomic-file.js.map +1 -0
- package/lib/store/futures-repo.d.ts +8 -49
- package/lib/store/futures-repo.d.ts.map +1 -1
- package/lib/store/futures-repo.js +34 -246
- package/lib/store/futures-repo.js.map +1 -1
- package/lib/store/ledger-repo-base.d.ts +120 -0
- package/lib/store/ledger-repo-base.d.ts.map +1 -0
- package/lib/store/ledger-repo-base.js +275 -0
- package/lib/store/ledger-repo-base.js.map +1 -0
- package/lib/store/notes.d.ts +29 -0
- package/lib/store/notes.d.ts.map +1 -0
- package/lib/store/notes.js +78 -0
- package/lib/store/notes.js.map +1 -0
- package/lib/store/paths.d.ts +2 -0
- package/lib/store/paths.d.ts.map +1 -1
- package/lib/store/paths.js +4 -0
- package/lib/store/paths.js.map +1 -1
- package/lib/store/repo.d.ts +15 -55
- package/lib/store/repo.d.ts.map +1 -1
- package/lib/store/repo.js +41 -206
- package/lib/store/repo.js.map +1 -1
- package/lib/store/shortlist.d.ts.map +1 -1
- package/lib/store/shortlist.js +4 -6
- package/lib/store/shortlist.js.map +1 -1
- package/lib/store/snapshots.d.ts +0 -3
- package/lib/store/snapshots.d.ts.map +1 -1
- package/lib/store/snapshots.js +0 -3
- package/lib/store/snapshots.js.map +1 -1
- package/package.json +1 -1
- package/src/agent/account-tools.ts +136 -0
- package/src/agent/futures-tools.ts +1 -6
- package/src/agent/ledger-tools.ts +29 -16
- package/src/agent/tool-result.ts +27 -0
- package/src/agent/tools.ts +30 -10
- package/src/client/entry.js +14 -0
- package/src/client/parts/10-core.js +535 -0
- package/src/client/parts/20-styles.js +336 -0
- package/src/client/parts/30-atoms.js +769 -0
- package/src/client/parts/35-hooks.js +447 -0
- package/src/client/parts/40-stock-tabs.js +641 -0
- package/src/client/parts/50-futures.js +911 -0
- package/src/client/parts/60-shell.js +706 -0
- package/src/client/parts/70-plugin.js +67 -0
- package/src/contract/accounts.ts +82 -0
- package/src/contract/common.ts +173 -0
- package/src/contract/domain.ts +217 -0
- package/src/contract/errors.ts +34 -0
- package/src/contract/index.ts +15 -0
- package/src/contract/ledger.ts +42 -0
- package/src/contract/market.ts +39 -0
- package/src/contract/rpc.ts +15 -0
- package/src/contract.ts +6 -472
- package/src/core/session.ts +1 -2
- package/src/data/cached-market-data.ts +193 -0
- package/src/data/futures-market-data.ts +9 -10
- package/src/index.ts +168 -20
- package/src/service/account-moves.ts +72 -0
- package/src/service/accounts-rpc.ts +80 -0
- package/src/service/compare-rpc.ts +77 -0
- package/src/service/errors.ts +23 -4
- package/src/service/futures-rpc.ts +7 -24
- package/src/service/futures-service.ts +161 -159
- package/src/service/ledger-admin.ts +140 -0
- package/src/service/paper-service.ts +183 -153
- package/src/service/route.ts +5 -2
- package/src/service/rpc-support.ts +61 -0
- package/src/service/rpc.ts +59 -23
- package/src/store/accounts.ts +373 -0
- package/src/store/atomic-file.ts +37 -0
- package/src/store/futures-repo.ts +35 -260
- package/src/store/ledger-repo-base.ts +337 -0
- package/src/store/notes.ts +86 -0
- package/src/store/paths.ts +5 -0
- package/src/store/repo.ts +47 -229
- package/src/store/shortlist.ts +3 -5
- package/src/store/snapshots.ts +0 -3
- package/src/client/index.js +0 -3275
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Registry of paper-trading accounts.
|
|
3
|
+
*
|
|
4
|
+
* One JSON file next to the ledgers (`$DSH_HOME/paper-trading/accounts.json`)
|
|
5
|
+
* owns the account list. Ledgers stay where they are: an account is just a
|
|
6
|
+
* directory name, so the registry never rewrites trading data and an old
|
|
7
|
+
* installation needs no migration.
|
|
8
|
+
*
|
|
9
|
+
* The registry deliberately knows nothing about sessions, models or owners —
|
|
10
|
+
* this plugin provides ledgers and tools, and every caller addresses an account
|
|
11
|
+
* by id or display name. Display names may repeat; `id` is the reliable handle.
|
|
12
|
+
*
|
|
13
|
+
* The file is rewritten atomically (temp file + rename) and every mutation goes
|
|
14
|
+
* through one Promise queue, mirroring {@link AccountRepo}'s single-writer rule.
|
|
15
|
+
* @module @dsh-ft/dsh-fft-paper-trading/store/accounts
|
|
16
|
+
*/
|
|
17
|
+
import type { AccountKind, AccountMeta, AccountsView } from '../contract.js';
|
|
18
|
+
/** Keeps one host's tick loop and the panel's account picker bounded. */
|
|
19
|
+
export declare const MAX_ACCOUNTS_PER_KIND = 20;
|
|
20
|
+
export declare const ACCOUNT_NAME_MAX = 16;
|
|
21
|
+
export declare const DEFAULT_TICK_MS = 5000;
|
|
22
|
+
/** On-disk shape. Unknown fields are ignored; a broken file must not block trading. */
|
|
23
|
+
interface AccountsFile {
|
|
24
|
+
version: number;
|
|
25
|
+
updatedAt: string;
|
|
26
|
+
tickMs: number;
|
|
27
|
+
accounts: AccountMeta[];
|
|
28
|
+
}
|
|
29
|
+
export interface AccountRegistryOptions {
|
|
30
|
+
home: string;
|
|
31
|
+
now?: () => Date;
|
|
32
|
+
logger?: {
|
|
33
|
+
warn(message: string): void;
|
|
34
|
+
};
|
|
35
|
+
/** Limits used only by tests. */
|
|
36
|
+
limits?: {
|
|
37
|
+
maxPerKind?: number;
|
|
38
|
+
tickMs?: {
|
|
39
|
+
min?: number;
|
|
40
|
+
max?: number;
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
export declare class AccountRegistry {
|
|
45
|
+
private readonly options;
|
|
46
|
+
private state;
|
|
47
|
+
private loading;
|
|
48
|
+
private queue;
|
|
49
|
+
private readonly now;
|
|
50
|
+
private readonly file;
|
|
51
|
+
private readonly root;
|
|
52
|
+
constructor(options: AccountRegistryOptions);
|
|
53
|
+
/** Absolute path of the registry file, for diagnostics and export. */
|
|
54
|
+
get filePath(): string;
|
|
55
|
+
/** Load (once) and return the live view. */
|
|
56
|
+
private ready;
|
|
57
|
+
private load;
|
|
58
|
+
/** Merge a persisted file over the defaults; never trust its shape. */
|
|
59
|
+
private normalise;
|
|
60
|
+
/**
|
|
61
|
+
* Register ledger directories the file does not know about: an old install
|
|
62
|
+
* predates the registry, and a hand-made account directory should not vanish.
|
|
63
|
+
*/
|
|
64
|
+
private discover;
|
|
65
|
+
private save;
|
|
66
|
+
/** Serialise mutations the same way the ledger serialises appends. */
|
|
67
|
+
private enqueue;
|
|
68
|
+
private mutate;
|
|
69
|
+
/**
|
|
70
|
+
* `includeArchived` keeps the picker clean but lets tooling (and the panel's
|
|
71
|
+
* "已归档" section) see everything; `kind` lets the stock panel ask for its
|
|
72
|
+
* plane without filtering client-side.
|
|
73
|
+
*/
|
|
74
|
+
view(options?: {
|
|
75
|
+
includeArchived?: boolean;
|
|
76
|
+
kind?: AccountKind;
|
|
77
|
+
}): Promise<AccountsView>;
|
|
78
|
+
/** Post-load lock flag; the service guard must not go stale on a copy. */
|
|
79
|
+
lockedSync(id: string): boolean;
|
|
80
|
+
/** Archived accounts are frozen like locked ones; the service asks this first. */
|
|
81
|
+
archivedSync(id: string): boolean;
|
|
82
|
+
tickMs(): Promise<number>;
|
|
83
|
+
/**
|
|
84
|
+
* Resolve an account reference to its metadata.
|
|
85
|
+
*
|
|
86
|
+
* `undefined` means "the kind's default account", which keeps every caller
|
|
87
|
+
* that predates multi-account working unchanged. An id always wins; a name
|
|
88
|
+
* may be ambiguous because names are not unique, and ambiguity is an error
|
|
89
|
+
* rather than a guess.
|
|
90
|
+
*/
|
|
91
|
+
resolve(reference: string | undefined, kind?: AccountKind): Promise<AccountMeta>;
|
|
92
|
+
private requireDefault;
|
|
93
|
+
create(input: {
|
|
94
|
+
name: string;
|
|
95
|
+
kind?: AccountKind;
|
|
96
|
+
initialCashCents?: number;
|
|
97
|
+
}): Promise<AccountMeta>;
|
|
98
|
+
private nextId;
|
|
99
|
+
/** Change the display name only; the directory id never moves. */
|
|
100
|
+
rename(reference: string, name: string): Promise<AccountMeta>;
|
|
101
|
+
/** Flip a boolean flag on one account; used by archive/lock and their inverses. */
|
|
102
|
+
private setFlag;
|
|
103
|
+
archive(reference: string): Promise<AccountMeta>;
|
|
104
|
+
unarchive(reference: string): Promise<AccountMeta>;
|
|
105
|
+
lock(reference: string): Promise<AccountMeta>;
|
|
106
|
+
unlock(reference: string): Promise<AccountMeta>;
|
|
107
|
+
}
|
|
108
|
+
/** Default starting cash for a brand-new account when the caller names no amount. */
|
|
109
|
+
export type { AccountsFile };
|
|
110
|
+
//# sourceMappingURL=accounts.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"accounts.d.ts","sourceRoot":"","sources":["../../src/store/accounts.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAIH,OAAO,KAAK,EAAE,WAAW,EAAE,W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trading accounts.
|
|
3
|
+
*
|
|
4
|
+
* One JSON file next to the ledgers (`$DSH_HOME/paper-trading/accounts.json`)
|
|
5
|
+
* owns the account list. Ledgers stay where they are: an account is just a
|
|
6
|
+
* directory name, so the registry never rewrites trading data and an old
|
|
7
|
+
* installation needs no migration.
|
|
8
|
+
*
|
|
9
|
+
* The registry deliberately knows nothing about sessions, models or owners —
|
|
10
|
+
* this plugin provides ledgers and tools, and every caller addresses an account
|
|
11
|
+
* by id or display name. Display names may repeat; `id` is the reliable handle.
|
|
12
|
+
*
|
|
13
|
+
* The file is rewritten atomically (temp file + rename) and every mutation goes
|
|
14
|
+
* through one Promise queue, mirroring {@link AccountRepo}'s single-writer rule.
|
|
15
|
+
* @module @dsh-ft/dsh-fft-paper-trading/store/accounts
|
|
16
|
+
*/
|
|
17
|
+
import { mkdir, readdir, readFile, stat } from 'node:fs/promises';
|
|
18
|
+
import { join } from 'node:path';
|
|
19
|
+
import { DEFAULT_ACCOUNT_ID } from '../contract.js';
|
|
20
|
+
import { FUTURES_ACCOUNT_ID } from '../futures-contract.js';
|
|
21
|
+
import { PaperError } from '../service/errors.js';
|
|
22
|
+
import { paperRoot } from './paths.js';
|
|
23
|
+
import { writeFileAtomic } from './atomic-file.js';
|
|
24
|
+
const REGISTRY_FILE = 'accounts.json';
|
|
25
|
+
const REGISTRY_VERSION = 4;
|
|
26
|
+
/** Keeps one host's tick loop and the panel's account picker bounded. */
|
|
27
|
+
export const MAX_ACCOUNTS_PER_KIND = 20;
|
|
28
|
+
export const ACCOUNT_NAME_MAX = 16;
|
|
29
|
+
export const DEFAULT_TICK_MS = 5_000;
|
|
30
|
+
const MIN_TICK_MS = 1_000;
|
|
31
|
+
const MAX_TICK_MS = 300_000;
|
|
32
|
+
export class AccountRegistry {
|
|
33
|
+
options;
|
|
34
|
+
state;
|
|
35
|
+
loading;
|
|
36
|
+
queue = Promise.resolve();
|
|
37
|
+
now;
|
|
38
|
+
file;
|
|
39
|
+
root;
|
|
40
|
+
constructor(options) {
|
|
41
|
+
this.options = options;
|
|
42
|
+
this.now = options.now ?? (() => new Date());
|
|
43
|
+
this.root = paperRoot(options.home);
|
|
44
|
+
this.file = join(this.root, REGISTRY_FILE);
|
|
45
|
+
}
|
|
46
|
+
/** Absolute path of the registry file, for diagnostics and export. */
|
|
47
|
+
get filePath() {
|
|
48
|
+
return this.file;
|
|
49
|
+
}
|
|
50
|
+
/** Load (once) and return the live view. */
|
|
51
|
+
async ready() {
|
|
52
|
+
if (this.state !== undefined)
|
|
53
|
+
return this.state;
|
|
54
|
+
if (this.loading === undefined) {
|
|
55
|
+
this.loading = this.load().catch(error => {
|
|
56
|
+
// A transient read failure must not poison the instance for the rest
|
|
57
|
+
// of the session: drop the cached promise so the next call retries.
|
|
58
|
+
this.loading = undefined;
|
|
59
|
+
throw error;
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
return this.loading;
|
|
63
|
+
}
|
|
64
|
+
async load() {
|
|
65
|
+
let parsed;
|
|
66
|
+
let readable = true;
|
|
67
|
+
try {
|
|
68
|
+
parsed = JSON.parse(await readFile(this.file, 'utf8'));
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
const code = error.code;
|
|
72
|
+
if (code !== 'ENOENT' && !(error instanceof SyntaxError))
|
|
73
|
+
throw error;
|
|
74
|
+
if (error instanceof SyntaxError)
|
|
75
|
+
this.options.logger?.warn(`paper-trading: ignoring unreadable ${REGISTRY_FILE}`);
|
|
76
|
+
parsed = undefined;
|
|
77
|
+
readable = code === 'ENOENT';
|
|
78
|
+
}
|
|
79
|
+
const state = this.normalise(parsed);
|
|
80
|
+
const discovered = await this.discover(state);
|
|
81
|
+
if (discovered)
|
|
82
|
+
state.updatedAt = this.now().toISOString();
|
|
83
|
+
const changed = !readable || discovered || parsed?.version !== REGISTRY_VERSION;
|
|
84
|
+
if (changed)
|
|
85
|
+
await this.save(state);
|
|
86
|
+
this.state = state;
|
|
87
|
+
return state;
|
|
88
|
+
}
|
|
89
|
+
/** Merge a persisted file over the defaults; never trust its shape. */
|
|
90
|
+
normalise(persisted) {
|
|
91
|
+
const input = (typeof persisted === 'object' && persisted !== null ? persisted : {});
|
|
92
|
+
const accounts = [];
|
|
93
|
+
for (const raw of Array.isArray(input.accounts) ? input.accounts : []) {
|
|
94
|
+
const meta = normaliseMeta(raw);
|
|
95
|
+
if (meta !== undefined && !accounts.some(item => item.id === meta.id))
|
|
96
|
+
accounts.push(meta);
|
|
97
|
+
}
|
|
98
|
+
for (const [id, name, kind] of [
|
|
99
|
+
[DEFAULT_ACCOUNT_ID, '默认账户', 'stock'],
|
|
100
|
+
[FUTURES_ACCOUNT_ID, '期货账户', 'futures'],
|
|
101
|
+
]) {
|
|
102
|
+
if (!accounts.some(item => item.id === id)) {
|
|
103
|
+
accounts.push({ id, kind, name, createdAt: this.now().toISOString(), archived: false, locked: false });
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
version: REGISTRY_VERSION,
|
|
108
|
+
updatedAt: typeof input.updatedAt === 'string' ? input.updatedAt : this.now().toISOString(),
|
|
109
|
+
tickMs: clampInt(input.tickMs, this.options.limits?.tickMs?.min ?? MIN_TICK_MS, this.options.limits?.tickMs?.max ?? MAX_TICK_MS, DEFAULT_TICK_MS),
|
|
110
|
+
accounts,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Register ledger directories the file does not know about: an old install
|
|
115
|
+
* predates the registry, and a hand-made account directory should not vanish.
|
|
116
|
+
*/
|
|
117
|
+
async discover(state) {
|
|
118
|
+
let entries;
|
|
119
|
+
try {
|
|
120
|
+
entries = await readdir(this.root);
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
let changed = false;
|
|
126
|
+
for (const entry of entries) {
|
|
127
|
+
if (entry === 'accounts.json' || entry === 'shortlist.json' || entry === 'backups')
|
|
128
|
+
continue;
|
|
129
|
+
if (state.accounts.some(item => item.id === entry)) {
|
|
130
|
+
const known = state.accounts.find(item => item.id === entry);
|
|
131
|
+
if (known.missing === true) {
|
|
132
|
+
known.missing = false;
|
|
133
|
+
changed = true;
|
|
134
|
+
}
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
try {
|
|
138
|
+
await stat(join(this.root, entry, 'events.jsonl'));
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
const kind = entry === FUTURES_ACCOUNT_ID ? 'futures' : 'stock';
|
|
144
|
+
state.accounts.push({ id: entry, kind, name: entry, createdAt: this.now().toISOString(), archived: false, locked: false });
|
|
145
|
+
changed = true;
|
|
146
|
+
}
|
|
147
|
+
for (const meta of state.accounts) {
|
|
148
|
+
if (meta.id === DEFAULT_ACCOUNT_ID || meta.id === FUTURES_ACCOUNT_ID)
|
|
149
|
+
continue;
|
|
150
|
+
try {
|
|
151
|
+
await stat(join(this.root, meta.id));
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
if (meta.missing !== true) {
|
|
155
|
+
meta.missing = true;
|
|
156
|
+
changed = true;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return changed;
|
|
161
|
+
}
|
|
162
|
+
async save(state) {
|
|
163
|
+
await writeFileAtomic(this.file, `${JSON.stringify({ ...state, version: REGISTRY_VERSION }, null, 2)}\n`);
|
|
164
|
+
}
|
|
165
|
+
/** Serialise mutations the same way the ledger serialises appends. */
|
|
166
|
+
enqueue(task) {
|
|
167
|
+
const next = this.queue.then(task);
|
|
168
|
+
this.queue = next.catch(() => undefined);
|
|
169
|
+
return next;
|
|
170
|
+
}
|
|
171
|
+
async mutate(task) {
|
|
172
|
+
await this.enqueue(async () => {
|
|
173
|
+
const state = await this.ready();
|
|
174
|
+
task(state);
|
|
175
|
+
state.updatedAt = this.now().toISOString();
|
|
176
|
+
await this.save(state);
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* `includeArchived` keeps the picker clean but lets tooling (and the panel's
|
|
181
|
+
* "已归档" section) see everything; `kind` lets the stock panel ask for its
|
|
182
|
+
* plane without filtering client-side.
|
|
183
|
+
*/
|
|
184
|
+
async view(options = {}) {
|
|
185
|
+
const state = await this.ready();
|
|
186
|
+
let accounts = options.includeArchived === true ? state.accounts : state.accounts.filter(item => item.archived !== true);
|
|
187
|
+
if (options.kind !== undefined)
|
|
188
|
+
accounts = accounts.filter(item => item.kind === options.kind);
|
|
189
|
+
return {
|
|
190
|
+
accounts: accounts.map(meta => ({ ...meta })),
|
|
191
|
+
defaultIds: { stock: DEFAULT_ACCOUNT_ID, futures: FUTURES_ACCOUNT_ID },
|
|
192
|
+
tickMs: state.tickMs,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
/** Post-load lock flag; the service guard must not go stale on a copy. */
|
|
196
|
+
lockedSync(id) {
|
|
197
|
+
return this.state?.accounts.some(item => item.id === id && item.locked === true) ?? false;
|
|
198
|
+
}
|
|
199
|
+
/** Archived accounts are frozen like locked ones; the service asks this first. */
|
|
200
|
+
archivedSync(id) {
|
|
201
|
+
return this.state?.accounts.some(item => item.id === id && item.archived === true) ?? false;
|
|
202
|
+
}
|
|
203
|
+
async tickMs() {
|
|
204
|
+
return (await this.ready()).tickMs;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Resolve an account reference to its metadata.
|
|
208
|
+
*
|
|
209
|
+
* `undefined` means "the kind's default account", which keeps every caller
|
|
210
|
+
* that predates multi-account working unchanged. An id always wins; a name
|
|
211
|
+
* may be ambiguous because names are not unique, and ambiguity is an error
|
|
212
|
+
* rather than a guess.
|
|
213
|
+
*/
|
|
214
|
+
async resolve(reference, kind = 'stock') {
|
|
215
|
+
const state = await this.ready();
|
|
216
|
+
if (reference === undefined)
|
|
217
|
+
return this.requireDefault(state, kind);
|
|
218
|
+
if (reference.trim() === '')
|
|
219
|
+
throw new PaperError('INVALID_ARGUMENT', '账户名不能为空');
|
|
220
|
+
const byId = state.accounts.find(item => item.id === reference);
|
|
221
|
+
if (byId !== undefined)
|
|
222
|
+
return byId;
|
|
223
|
+
const byName = state.accounts.filter(item => item.kind === kind && item.name === reference);
|
|
224
|
+
if (byName.length === 1)
|
|
225
|
+
return byName[0];
|
|
226
|
+
if (byName.length > 1) {
|
|
227
|
+
throw new PaperError('INVALID_ARGUMENT', `账户名「${reference}」不唯一,请改用账户 id:${byName.map(item => item.id).join('、')}`);
|
|
228
|
+
}
|
|
229
|
+
throw new PaperError('ACCOUNT_NOT_FOUND', `没有名为「${reference}」的账户`);
|
|
230
|
+
}
|
|
231
|
+
requireDefault(state, kind) {
|
|
232
|
+
const id = kind === 'futures' ? FUTURES_ACCOUNT_ID : DEFAULT_ACCOUNT_ID;
|
|
233
|
+
const meta = state.accounts.find(item => item.id === id);
|
|
234
|
+
if (meta === undefined)
|
|
235
|
+
throw new PaperError('ACCOUNT_NOT_FOUND', `默认账户 ${id} 不存在`);
|
|
236
|
+
return meta;
|
|
237
|
+
}
|
|
238
|
+
async create(input) {
|
|
239
|
+
const kind = input.kind ?? 'stock';
|
|
240
|
+
const name = validateName(input.name);
|
|
241
|
+
let created;
|
|
242
|
+
await this.mutate(state => {
|
|
243
|
+
const live = state.accounts.filter(item => item.kind === kind);
|
|
244
|
+
const limit = this.options.limits?.maxPerKind ?? MAX_ACCOUNTS_PER_KIND;
|
|
245
|
+
if (live.length >= limit)
|
|
246
|
+
throw new PaperError('ACCOUNT_LIMIT', `账户数量已达上限 ${limit}`);
|
|
247
|
+
created = {
|
|
248
|
+
id: this.nextId(state),
|
|
249
|
+
kind,
|
|
250
|
+
name,
|
|
251
|
+
createdAt: this.now().toISOString(),
|
|
252
|
+
archived: false,
|
|
253
|
+
locked: false,
|
|
254
|
+
...(input.initialCashCents !== undefined ? { initialCashCents: Math.round(input.initialCashCents) } : {}),
|
|
255
|
+
};
|
|
256
|
+
state.accounts.push(created);
|
|
257
|
+
});
|
|
258
|
+
// Materialise the directory now: a registered account without a directory
|
|
259
|
+
// would look "missing" until its ledger is first opened.
|
|
260
|
+
await mkdir(join(this.root, created.id), { recursive: true });
|
|
261
|
+
return { ...created };
|
|
262
|
+
}
|
|
263
|
+
nextId(state) {
|
|
264
|
+
for (let attempt = 0; attempt < 50; attempt += 1) {
|
|
265
|
+
const id = `acc-${Math.random().toString(36).slice(2, 8)}`;
|
|
266
|
+
if (!state.accounts.some(item => item.id === id))
|
|
267
|
+
return id;
|
|
268
|
+
}
|
|
269
|
+
return `acc-${this.now().getTime().toString(36)}`;
|
|
270
|
+
}
|
|
271
|
+
/** Change the display name only; the directory id never moves. */
|
|
272
|
+
async rename(reference, name) {
|
|
273
|
+
const target = await this.resolve(reference);
|
|
274
|
+
if (target.id === FUTURES_ACCOUNT_ID) {
|
|
275
|
+
// The futures plane has a fixed panel and never reads the registry name,
|
|
276
|
+
// so renaming it would only create misleading metadata.
|
|
277
|
+
throw new PaperError('INVALID_ARGUMENT', '期货账户名称固定,不可改名');
|
|
278
|
+
}
|
|
279
|
+
const next = validateName(name);
|
|
280
|
+
await this.mutate(state => {
|
|
281
|
+
const meta = state.accounts.find(item => item.id === target.id);
|
|
282
|
+
meta.name = next;
|
|
283
|
+
});
|
|
284
|
+
return { ...(await this.resolve(target.id)) };
|
|
285
|
+
}
|
|
286
|
+
/** Flip a boolean flag on one account; used by archive/lock and their inverses. */
|
|
287
|
+
async setFlag(reference, patch) {
|
|
288
|
+
const target = await this.resolve(reference);
|
|
289
|
+
if (patch.archived === true && (target.id === DEFAULT_ACCOUNT_ID || target.id === FUTURES_ACCOUNT_ID)) {
|
|
290
|
+
// Archiving the default would leave the panel without a picker entry that
|
|
291
|
+
// can never be selected back; the default account stays available.
|
|
292
|
+
throw new PaperError('INVALID_ARGUMENT', `默认账户不能归档(${target.name})`);
|
|
293
|
+
}
|
|
294
|
+
await this.mutate(state => {
|
|
295
|
+
const meta = state.accounts.find(item => item.id === target.id);
|
|
296
|
+
Object.assign(meta, patch);
|
|
297
|
+
});
|
|
298
|
+
return { ...(await this.resolve(target.id)) };
|
|
299
|
+
}
|
|
300
|
+
archive(reference) {
|
|
301
|
+
return this.setFlag(reference, { archived: true });
|
|
302
|
+
}
|
|
303
|
+
unarchive(reference) {
|
|
304
|
+
return this.setFlag(reference, { archived: false });
|
|
305
|
+
}
|
|
306
|
+
lock(reference) {
|
|
307
|
+
return this.setFlag(reference, { locked: true });
|
|
308
|
+
}
|
|
309
|
+
unlock(reference) {
|
|
310
|
+
return this.setFlag(reference, { locked: false });
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
/** Trim and reject empty or over-long display names. Duplicates are allowed. */
|
|
314
|
+
function validateName(value) {
|
|
315
|
+
if (typeof value !== 'string')
|
|
316
|
+
throw new PaperError('INVALID_ARGUMENT', '账户名必须是字符串');
|
|
317
|
+
const name = value.trim();
|
|
318
|
+
if (name === '')
|
|
319
|
+
throw new PaperError('INVALID_ARGUMENT', '账户名不能为空');
|
|
320
|
+
if ([...name].length > ACCOUNT_NAME_MAX)
|
|
321
|
+
throw new PaperError('INVALID_ARGUMENT', `账户名不能超过 ${ACCOUNT_NAME_MAX} 个字符`);
|
|
322
|
+
return name;
|
|
323
|
+
}
|
|
324
|
+
/** v1 wrote `archived` + bindings, v2 added the note, v3 dropped it, v4 renames `hidden` back to `archived`. */
|
|
325
|
+
function normaliseMeta(raw) {
|
|
326
|
+
if (typeof raw !== 'object' || raw === null)
|
|
327
|
+
return undefined;
|
|
328
|
+
const input = raw;
|
|
329
|
+
if (typeof input.id !== 'string' || input.id === '')
|
|
330
|
+
return undefined;
|
|
331
|
+
const kind = input.kind === 'futures' ? 'futures' : 'stock';
|
|
332
|
+
const name = typeof input.name === 'string' && input.name.trim() !== '' ? input.name.trim().slice(0, ACCOUNT_NAME_MAX) : input.id;
|
|
333
|
+
return {
|
|
334
|
+
id: input.id,
|
|
335
|
+
kind,
|
|
336
|
+
name,
|
|
337
|
+
createdAt: typeof input.createdAt === 'string' ? input.createdAt : new Date(0).toISOString(),
|
|
338
|
+
// v3 wrote `hidden`, v1 wrote `archived`; both mean archived now.
|
|
339
|
+
archived: input.archived === true || input.hidden === true,
|
|
340
|
+
locked: input.locked === true,
|
|
341
|
+
...(typeof input.initialCashCents === 'number' && Number.isFinite(input.initialCashCents) && input.initialCashCents > 0
|
|
342
|
+
? { initialCashCents: Math.round(input.initialCashCents) }
|
|
343
|
+
: {}),
|
|
344
|
+
...(input.missing === true ? { missing: true } : {}),
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
function clampInt(value, min, max, fallback) {
|
|
348
|
+
if (typeof value !== 'number' || !Number.isFinite(value))
|
|
349
|
+
return fallback;
|
|
350
|
+
return Math.min(max, Math.max(min, Math.round(value)));
|
|
351
|
+
}
|
|
352
|
+
//# sourceMappingURL=accounts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"accounts.js","sourceRoot":"","sources":["../../src/store/accounts.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAA;AACjE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAEhC,OAAO,EAAE,kBAAkB,EAAoB,MAAM,gBAAgB,CAAA;AACrE,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAA;AAC3D,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AACtC,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA;AAElD,MAAM,aAAa,GAAG,eAAe,CAAA;AACrC,MAAM,gBAAgB,GAAG,CAAC,CAAA;AAC1B,yEAAyE;AACzE,MAAM,CAAC,MAAM,qBAAqB,GAAG,EAAE,CAAA;AACvC,MAAM,CAAC,MAAM,g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KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,gBAAgB,CAAC,IAAI,KAAK,CAAC,gBAAgB,GAAG,CAAC;YACrH,CAAC,CAAC,EAAE,gBAAgB,EAAE,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,gBAAgB,CAAC,EAAE;YAC1D,CAAC,CAAC,EAAE,CAAC;QACP,GAAG,CAAC,KAAK,CAAC,OAAO,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KACrD,CAAA;AACH,CAAC;AAED,SAAS,QAAQ,CAAC,KAAc,EAAE,GAAW,EAAE,GAAW,EAAE,QAAgB;IAC1E,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC;QAAE,OAAO,QAAQ,CAAA;IACzE,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;AACxD,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Atomic whole-file write shared by the small JSON files beside the ledgers.
|
|
3
|
+
*
|
|
4
|
+
* The event log has its own append/fsync path; the registry, the notes file,
|
|
5
|
+
* the shortlist and the futures contract cache are written whole. They all need
|
|
6
|
+
* the same guarantee — a crash leaves either the old or the new file, never a
|
|
7
|
+
* half-written one — and the same unique temp name, so two writers can never
|
|
8
|
+
* clobber each other's staging file.
|
|
9
|
+
* @module @dsh-ft/dsh-fft-paper-trading/store/atomic-file
|
|
10
|
+
*/
|
|
11
|
+
export interface AtomicWriteOptions {
|
|
12
|
+
mode?: number;
|
|
13
|
+
}
|
|
14
|
+
/** Write `content` to `file` via a fsynced sibling temp file and a rename. */
|
|
15
|
+
export declare function writeFileAtomic(file: string, content: string, options?: AtomicWriteOptions): Promise<void>;
|
|
16
|
+
//# sourceMappingURL=atomic-file.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"atomic-file.d.ts","sourceRoot":"","sources":["../../src/store/atomic-file.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAMH,MAAM,WAAW,kBAAkB;IACjC,IAAI,CAAC,EAAE,MAAM,CAAA;CACd;AAED,8EAA8E;AAC9E,wBAAsB,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,GAAE,kBAAuB,GAAG,OAAO,CAAC,IAAI,CAAC,CAgBpH"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Atomic whole-file write shared by the small JSON files beside the ledgers.
|
|
3
|
+
*
|
|
4
|
+
* The event log has its own append/fsync path; the registry, the notes file,
|
|
5
|
+
* the shortlist and the futures contract cache are written whole. They all need
|
|
6
|
+
* the same guarantee — a crash leaves either the old or the new file, never a
|
|
7
|
+
* half-written one — and the same unique temp name, so two writers can never
|
|
8
|
+
* clobber each other's staging file.
|
|
9
|
+
* @module @dsh-ft/dsh-fft-paper-trading/store/atomic-file
|
|
10
|
+
*/
|
|
11
|
+
import { randomUUID } from 'node:crypto';
|
|
12
|
+
import { mkdir, open, rename, unlink } from 'node:fs/promises';
|
|
13
|
+
import { dirname } from 'node:path';
|
|
14
|
+
/** Write `content` to `file` via a fsynced sibling temp file and a rename. */
|
|
15
|
+
export async function writeFileAtomic(file, content, options = {}) {
|
|
16
|
+
await mkdir(dirname(file), { recursive: true, mode: 0o700 });
|
|
17
|
+
const temp = `${file}.tmp-${process.pid}-${randomUUID()}`;
|
|
18
|
+
try {
|
|
19
|
+
const handle = await open(temp, 'w', options.mode ?? 0o600);
|
|
20
|
+
try {
|
|
21
|
+
await handle.writeFile(content, 'utf8');
|
|
22
|
+
await handle.sync();
|
|
23
|
+
}
|
|
24
|
+
finally {
|
|
25
|
+
await handle.close();
|
|
26
|
+
}
|
|
27
|
+
await rename(temp, file);
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
await unlink(temp).catch(() => undefined);
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=atomic-file.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"atomic-file.js","sourceRoot":"","sources":["../../src/store/atomic-file.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;AAC9D,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAMnC,8EAA8E;AAC9E,MAAM,CAAC,KAAK,UAAU,eAAe,CAAC,IAAY,EAAE,OAAe,EAAE,UAA8B,EAAE;IACnG,MAAM,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAA;IAC5D,MAAM,IAAI,GAAG,GAAG,IAAI,QAAQ,OAAO,CAAC,GAAG,IAAI,UAAU,EAAE,EAAE,CAAA;IACzD,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,OAAO,CAAC,IAAI,IAAI,KAAK,CAAC,CAAA;QAC3D,IAAI,CAAC;YACH,MAAM,MAAM,CAAC,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC,CAAA;YACvC,MAAM,MAAM,CAAC,IAAI,EAAE,CAAA;QACrB,CAAC;gBAAS,CAAC;YACT,MAAM,MAAM,CAAC,KAAK,EAAE,CAAA;QACtB,CAAC;QACD,MAAM,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,CAAA;IAC1B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,MAAM,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAA;QACzC,MAAM,KAAK,CAAA;IACb,CAAC;AACH,CAAC"}
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Futures ledger repository: `$DSH_HOME/paper-trading/futures/events.jsonl`.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
4
|
+
* Storage is the shared {@link LedgerRepoBase}: one write queue, contiguous
|
|
5
5
|
* sequence numbers, fsync per append, atomic replacement for reset/repair, a
|
|
6
|
-
* backup before any destructive operation,
|
|
7
|
-
* replay finds an inconsistency
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
6
|
+
* backup before any destructive operation, automatic `blocked` state when
|
|
7
|
+
* replay finds an inconsistency, daily snapshots and in-process restore. This
|
|
8
|
+
* file only supplies the futures fold (replay/verify/summary) and the futures
|
|
9
|
+
* mark tick, which quantizes prices to the contract tick.
|
|
11
10
|
* @module @dsh-ft/dsh-fft-paper-trading/store/futures-repo
|
|
12
11
|
*/
|
|
13
12
|
import type { FuturesAccountState, FuturesLedgerEvent } from '../core/futures/types.js';
|
|
13
|
+
import { LedgerRepoBase } from './ledger-repo-base.js';
|
|
14
14
|
import type { LedgerSnapshot } from './port.js';
|
|
15
15
|
export interface FuturesRepoOptions {
|
|
16
16
|
home: string;
|
|
@@ -54,54 +54,13 @@ export interface FuturesStore {
|
|
|
54
54
|
/** Replace the live ledger with one snapshot; allowed while blocked. */
|
|
55
55
|
restore(name: string): Promise<FuturesAccountState>;
|
|
56
56
|
}
|
|
57
|
-
/**
|
|
58
|
-
|
|
59
|
-
*
|
|
60
|
-
* Every write goes through one Promise queue and one sequence invariant, so a
|
|
61
|
-
* concurrent read cannot duplicate a settlement event. Repairs and resets use
|
|
62
|
-
* an atomic file replacement; a rejected write never touches the file.
|
|
63
|
-
*/
|
|
64
|
-
export declare class FuturesRepo implements FuturesStore {
|
|
65
|
-
private readonly options;
|
|
66
|
-
private readonly store;
|
|
67
|
-
private readonly accountId;
|
|
68
|
-
private state;
|
|
69
|
-
private loading;
|
|
70
|
-
private queue;
|
|
57
|
+
/** Loads, replays and appends the single futures ledger. */
|
|
58
|
+
export declare class FuturesRepo extends LedgerRepoBase<FuturesAccountState, FuturesLedgerEvent> implements FuturesStore {
|
|
71
59
|
constructor(options: FuturesRepoOptions);
|
|
72
|
-
get ledgerPath(): string;
|
|
73
|
-
private backups;
|
|
74
|
-
private ensureLoaded;
|
|
75
|
-
private loadOnce;
|
|
76
|
-
private compact;
|
|
77
|
-
private recordViolation;
|
|
78
|
-
current(): Promise<FuturesAccountState>;
|
|
79
|
-
transact<T>(build: (state: FuturesAccountState) => FuturesTransaction<T> | Promise<FuturesTransaction<T>>): Promise<FuturesTransactionOutcome<T>>;
|
|
80
|
-
append(events: readonly FuturesLedgerEvent[]): Promise<FuturesAccountState>;
|
|
81
|
-
/**
|
|
82
|
-
* Replace the file with a new ledger (reset, compaction). Validated and
|
|
83
|
-
* replayed before the atomic rename, so a reset never leaves a ledger that
|
|
84
|
-
* would lock the account on the next start.
|
|
85
|
-
*/
|
|
86
|
-
replace(events: readonly FuturesLedgerEvent[]): Promise<FuturesAccountState>;
|
|
87
60
|
/** Refresh position marks for the views only; never writes the ledger. */
|
|
88
61
|
markPrices(marks: readonly {
|
|
89
62
|
symbol: string;
|
|
90
63
|
last: number;
|
|
91
64
|
}[]): Promise<FuturesAccountState>;
|
|
92
|
-
backup(label: string): Promise<boolean>;
|
|
93
|
-
readLedger(): Promise<FuturesLedgerFile>;
|
|
94
|
-
/** List snapshots with replay metadata, newest first. */
|
|
95
|
-
snapshots(): Promise<LedgerSnapshot[]>;
|
|
96
|
-
private inspectSnapshot;
|
|
97
|
-
/**
|
|
98
|
-
* Replace the live futures ledger with one snapshot.
|
|
99
|
-
*
|
|
100
|
-
* Allowed while blocked; validated before the live file is touched, and the
|
|
101
|
-
* current file is snapshotted first so the recovery can be undone.
|
|
102
|
-
*/
|
|
103
|
-
restore(name: string): Promise<FuturesAccountState>;
|
|
104
|
-
private assertSequence;
|
|
105
|
-
private enqueue;
|
|
106
65
|
}
|
|
107
66
|
//# sourceMappingURL=futures-repo.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"futures-repo.d.ts","sourceRoot":"","sources":["../../src/store/futures-repo.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"futures-repo.d.ts","sourceRoot":"","sources":["../../src/store/futures-repo.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAMH,OAAO,KAAK,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAA;AACvF,OAAO,EAAE,cAAc,EAA4C,MAAM,uBAAuB,CAAA;AAChG,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,WAAW,CAAA;AAE/C,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,MAAM,CAAA;IACZ,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,MAAM,CAAC,EAAE;QAAE,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAA;KAAE,CAAA;CACzC;AAED,MAAM,WAAW,kBAAkB,CAAC,CAAC;IACnC,MAAM,EAAE,kBAAkB,EAAE,CAAA;IAC5B,KAAK,EAAE,CAAC,CAAA;CACT;AAED,MAAM,WAAW,yBAAyB,CAAC,CAAC;IAC1C,KAAK,EAAE,mBAAmB,CAAA;IAC1B,KAAK,EAAE,CAAC,CAAA;IACR,2EAA2E;IAC3E,MAAM,EAAE,kBAAkB,EAAE,CAAA;CAC7B;AAED,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,MAAM,CAAA;IACZ,iDAAiD;IACjD,OAAO,EAAE,MAAM,GAAG,IAAI,CAAA;CACvB;AAED,uDAAuD;AACvD,MAAM,WAAW,YAAY;IAC3B,qEAAqE;IACrE,QAAQ,CAAC,UAAU,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