@senpi/runtime 3.0.243 → 3.0.245
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/dist/integrity-manifest.json +29 -21
- package/dist/runtime/pending-opens-ledger.d.ts +35 -4
- package/dist/runtime/pending-opens-ledger.d.ts.map +1 -1
- package/dist/runtime/pending-opens-ledger.js +62 -15
- package/dist/runtime/pending-opens-ledger.js.map +1 -1
- package/dist/runtime/pending-opens-store.d.ts +17 -0
- package/dist/runtime/pending-opens-store.d.ts.map +1 -0
- package/dist/runtime/pending-opens-store.js +71 -0
- package/dist/runtime/pending-opens-store.js.map +1 -0
- package/dist/runtime/resting-entry-cancel.d.ts +41 -0
- package/dist/runtime/resting-entry-cancel.d.ts.map +1 -0
- package/dist/runtime/resting-entry-cancel.js +148 -0
- package/dist/runtime/resting-entry-cancel.js.map +1 -0
- package/dist/runtime/runtime.d.ts +9 -1
- package/dist/runtime/runtime.d.ts.map +1 -1
- package/dist/runtime/runtime.js +45 -4
- package/dist/runtime/runtime.js.map +1 -1
- package/dist/senpi/client.d.ts +7 -1
- package/dist/senpi/client.d.ts.map +1 -1
- package/dist/senpi/client.js +39 -1
- package/dist/senpi/client.js.map +1 -1
- package/dist/senpi/index.d.ts +1 -1
- package/dist/senpi/index.d.ts.map +1 -1
- package/dist/senpi/types.d.ts +13 -0
- package/dist/senpi/types.d.ts.map +1 -1
- package/dist/utils/event-catalog.d.ts +17 -0
- package/dist/utils/event-catalog.d.ts.map +1 -1
- package/dist/utils/event-catalog.js +5 -0
- package/dist/utils/event-catalog.js.map +1 -1
- package/openclaw.plugin.json +1 -1
- package/package.json +1 -1
|
@@ -934,10 +934,14 @@
|
|
|
934
934
|
"dist/runtime/parse-interval.d.ts.map": "5db0181bec37fe364b9bb219ea127f82e52b7dd9c413f4416db1cce65ad8027e",
|
|
935
935
|
"dist/runtime/parse-interval.js": "41847bc402fd554af39026314c89025ab2b68c956bd553be1fc33479a46cb225",
|
|
936
936
|
"dist/runtime/parse-interval.js.map": "83c8e8bfa6b88c91c195e12ba20aabb578031b3c207962de0b3fb8470f103c5f",
|
|
937
|
-
"dist/runtime/pending-opens-ledger.d.ts": "
|
|
938
|
-
"dist/runtime/pending-opens-ledger.d.ts.map": "
|
|
939
|
-
"dist/runtime/pending-opens-ledger.js": "
|
|
940
|
-
"dist/runtime/pending-opens-ledger.js.map": "
|
|
937
|
+
"dist/runtime/pending-opens-ledger.d.ts": "393c0fc322965e175f52233e92b083c9ec5024ed3e52ba8def014708135ffc03",
|
|
938
|
+
"dist/runtime/pending-opens-ledger.d.ts.map": "3e6099141c0c7be9669da4f16b6f35a8230d059cc2b31c43d0ea394497aa6994",
|
|
939
|
+
"dist/runtime/pending-opens-ledger.js": "a199ad7def62a52aae91b36649ba05c4c377e5b918ed858f57300517f928af1e",
|
|
940
|
+
"dist/runtime/pending-opens-ledger.js.map": "f0e5fa1d4d5f0ddfb5214036c5ba6ab9a29b35b0c96d2e1a28ec04a1fd3da5e3",
|
|
941
|
+
"dist/runtime/pending-opens-store.d.ts": "398b639dd783faa7fa7e1b9e4f9037ceb097bc81a3781f37d2b6f19669e40a81",
|
|
942
|
+
"dist/runtime/pending-opens-store.d.ts.map": "e13ec5830b62058c1de53868f868d872b35c1d854c6e7b30728ea0d86e4e4565",
|
|
943
|
+
"dist/runtime/pending-opens-store.js": "fea1b244e3600808911c9df49c0ccb126deb4ae762cad75ce35c30ddc361a002",
|
|
944
|
+
"dist/runtime/pending-opens-store.js.map": "1744f670bf2a03ce41799b1a85423b7d91282f0801cbdb4370f96c8060778dba",
|
|
941
945
|
"dist/runtime/position-id-registry.d.ts": "7062aaadb44078878157acbf6da65d4201c7d560674cde4b327f5bb2704e00ef",
|
|
942
946
|
"dist/runtime/position-id-registry.d.ts.map": "c95e08df76ec594a70de6fa25b36c8b133ba589d609360028a8b4a3bdf3844ba",
|
|
943
947
|
"dist/runtime/position-id-registry.js": "ae4bf329a65cd8e7067b31877d82eeece12493f80e6effdedb3535501e1d2235",
|
|
@@ -970,6 +974,10 @@
|
|
|
970
974
|
"dist/runtime/reconcile-state.d.ts.map": "65f47ad1d9b3128adfdf262e67c65664a5620df0145b308bf9e411650ddd9aac",
|
|
971
975
|
"dist/runtime/reconcile-state.js": "dcf95e7fbde99e75d417a2df0ddd35683075551f0da610097b8d015c29c5240c",
|
|
972
976
|
"dist/runtime/reconcile-state.js.map": "93a0e96c270d7a95b6e7c03495e392413ba7c305dd093188ac6580a9f368bd73",
|
|
977
|
+
"dist/runtime/resting-entry-cancel.d.ts": "5067d57c224340b1906b92fb57e3dbb5904cf6c556836a84cd59fb76bd353446",
|
|
978
|
+
"dist/runtime/resting-entry-cancel.d.ts.map": "1c60f1f5bb96617e4d437de3847cafb66c8b65c5271608adc18318dacfe83833",
|
|
979
|
+
"dist/runtime/resting-entry-cancel.js": "cee99f4f4780617db09520e23b19542513fb8b8bb1ea9f137348c266b1651652",
|
|
980
|
+
"dist/runtime/resting-entry-cancel.js.map": "9d80dbaa5e501799b52fecc50ab0a5af8e01b0ce7a832003b2833587bacae3e6",
|
|
973
981
|
"dist/runtime/risk-guard.d.ts": "e5f89479971d90fb011ade5389a6a1a2c65e4685380659c0d0f68dc84385ade4",
|
|
974
982
|
"dist/runtime/risk-guard.d.ts.map": "c24b151e2c067787cc95fcad026f4480bfec946eddbdf0e50ac7d9aa7fea0f8f",
|
|
975
983
|
"dist/runtime/risk-guard.js": "9a01f6f5ac594c4d85f1dcb12ee7a2085fe295fd64d1826f97bddece2cd1482a",
|
|
@@ -994,10 +1002,10 @@
|
|
|
994
1002
|
"dist/runtime/runtime-version.d.ts.map": "2646400bd507a9c70da607215d137573ec4458936abe353eadc21fc1e7c55abf",
|
|
995
1003
|
"dist/runtime/runtime-version.js": "fefbb0364e36e7d77a154e7fe87c09ebd6a77b9dfaec42cc585986de9a20fa33",
|
|
996
1004
|
"dist/runtime/runtime-version.js.map": "dc54d4f1c6df38761155149237513dee5556cda7cfc038ffacee27cec37d46cb",
|
|
997
|
-
"dist/runtime/runtime.d.ts": "
|
|
998
|
-
"dist/runtime/runtime.d.ts.map": "
|
|
999
|
-
"dist/runtime/runtime.js": "
|
|
1000
|
-
"dist/runtime/runtime.js.map": "
|
|
1005
|
+
"dist/runtime/runtime.d.ts": "02a95c012458c76ab2c4ad4022204d1111596312f8e7334e0b7882a26ef86003",
|
|
1006
|
+
"dist/runtime/runtime.d.ts.map": "a32acdfc3ccd982de809e650f02b929d4591ba64d983260d3cd9d228eb614d51",
|
|
1007
|
+
"dist/runtime/runtime.js": "511055a77676fba260b897279423d40e213a76fe2e855d872333eff53a585985",
|
|
1008
|
+
"dist/runtime/runtime.js.map": "14cebbc53b72c41ddbfef62dee2d550452b46043e9ce512dd79f1854afd91871",
|
|
1001
1009
|
"dist/runtime/scanner-composition.d.ts": "ead28210fd7b563611170c6c08d077e19a389edb82ad9fe4a3b7e1433fbaa185",
|
|
1002
1010
|
"dist/runtime/scanner-composition.d.ts.map": "443e3fb78319fa7c56636b96cf49f4e421b09586c3d66f4c3217f8a25a307e0b",
|
|
1003
1011
|
"dist/runtime/scanner-composition.js": "8b1d23380a1ca907476e61657ee09f15744cab58fcb575f9767b9b18f55ebf8b",
|
|
@@ -1312,16 +1320,16 @@
|
|
|
1312
1320
|
"dist/senpi/__tests__/helpers/fake-simulation-mcp.d.ts.map": "501d24f809723a1d4c14e220e4edc7c5f213945efed9f14881d6880c43bbecd7",
|
|
1313
1321
|
"dist/senpi/__tests__/helpers/fake-simulation-mcp.js": "62c7eb0f0d2ffa2ee83205ccaf35536ead39b89adcb5f95a1a894ca24177910a",
|
|
1314
1322
|
"dist/senpi/__tests__/helpers/fake-simulation-mcp.js.map": "e2f82c05dc94590333d6dd558c8edc0522473daab4cf5bc55cf8722d201fd1d9",
|
|
1315
|
-
"dist/senpi/client.d.ts": "
|
|
1316
|
-
"dist/senpi/client.d.ts.map": "
|
|
1317
|
-
"dist/senpi/client.js": "
|
|
1318
|
-
"dist/senpi/client.js.map": "
|
|
1323
|
+
"dist/senpi/client.d.ts": "b33c4a5c5f2193ef7a3c7989a91afe89d743251bd98424f557475f973edfae36",
|
|
1324
|
+
"dist/senpi/client.d.ts.map": "71237e725fd2b06366e405fe58cc509c2ea2f19a3b3b69039d4bc000f3564221",
|
|
1325
|
+
"dist/senpi/client.js": "28f130fe52484cd1be28e96871b76c17f8ae113cd69197ddf54f59ab26b036a3",
|
|
1326
|
+
"dist/senpi/client.js.map": "8158214a6b8dd39a8fe74952cf4be05e978eb449d39fcd080a9e19a82d030177",
|
|
1319
1327
|
"dist/senpi/constants.d.ts": "e37360cfe354470fdf945a620d3bd800e1f9c9ecced137b3746c79f1cca4db21",
|
|
1320
1328
|
"dist/senpi/constants.d.ts.map": "57b9949c97061bbff1b93af701180614bd4888dd041efc900cc01d28ae4085d2",
|
|
1321
1329
|
"dist/senpi/constants.js": "d5b1cc1e6b1a09687d076aeec023e17a288debb4fc5a83e7fdc49cc4ea0c3cfc",
|
|
1322
1330
|
"dist/senpi/constants.js.map": "7eaba24071837239df95d0c2cae7a77e9871310caa553e75808baf19a9d36ae5",
|
|
1323
|
-
"dist/senpi/index.d.ts": "
|
|
1324
|
-
"dist/senpi/index.d.ts.map": "
|
|
1331
|
+
"dist/senpi/index.d.ts": "11967e8724f390d8c07471179dd6e4bd5054432ce9ca498307594f394dfdd7c8",
|
|
1332
|
+
"dist/senpi/index.d.ts.map": "aedb1bac15364e9bd9607b7a6921e6b3ae0d09829f9b777291356e256ea728d9",
|
|
1325
1333
|
"dist/senpi/index.js": "c03d7dfc7a6cbc32421df2da0fc6697efff16e66064fdf3e04a312a0e6dab291",
|
|
1326
1334
|
"dist/senpi/index.js.map": "03e2f7dd5185f6fac06050d4fc4cf82cf515d1d96fbc07e4768a02347b714718",
|
|
1327
1335
|
"dist/senpi/simulation.d.ts": "96512eea65a7c7dc29b6de5670129fc05124a5cd232ffb8358398e4b1d6784c6",
|
|
@@ -1336,8 +1344,8 @@
|
|
|
1336
1344
|
"dist/senpi/tool-errors.d.ts.map": "74cea973b34bd8d2e5689e32f4a02eae1f2705577d42f7d019512fc0f9f68393",
|
|
1337
1345
|
"dist/senpi/tool-errors.js": "16bd4bf6fc0de64e36f94766e2bd537df00c86bd9b9fbc9c20fdaf685ddff790",
|
|
1338
1346
|
"dist/senpi/tool-errors.js.map": "eb0fcd23fd1538dcf5322e2058b29ddd30d0e0b4f359b87a61d15542532893ad",
|
|
1339
|
-
"dist/senpi/types.d.ts": "
|
|
1340
|
-
"dist/senpi/types.d.ts.map": "
|
|
1347
|
+
"dist/senpi/types.d.ts": "1cea0d49677d3582d9cf24f592e9985eb7f8675c2ef185127ca1f642637f9fe2",
|
|
1348
|
+
"dist/senpi/types.d.ts.map": "56e1738daa6f232fde7653e61aa2785d148f699f7a122f137e2a71dda7d9f626",
|
|
1341
1349
|
"dist/senpi/types.js": "01ae2a5b120382f9a648ced7ee8507493a134f216d100fc61600c6c9738235d2",
|
|
1342
1350
|
"dist/senpi/types.js.map": "559745a28518645d68c937240fa4d890936086950cd1aaf0cb268c5afcc616f4",
|
|
1343
1351
|
"dist/skill-router/catalog.d.ts": "2f99c03e8907469966233d1dedaf48c87d159093a36cb9368e58128b313840e2",
|
|
@@ -1589,10 +1597,10 @@
|
|
|
1589
1597
|
"dist/utils/clearing-house-staleness.d.ts.map": "7a9d5deb50d37c2ad901cced20bdef02754fc652afd16935ee2f5e40292aba37",
|
|
1590
1598
|
"dist/utils/clearing-house-staleness.js": "9612955070f84ce9d9f09f83ec7baf8bd51ac54a2f32f42fa02fcca9c14e89bc",
|
|
1591
1599
|
"dist/utils/clearing-house-staleness.js.map": "5022eb6e1211b7a1f192af0c9703326b68e62f37436329ca710149230fb4c7b9",
|
|
1592
|
-
"dist/utils/event-catalog.d.ts": "
|
|
1593
|
-
"dist/utils/event-catalog.d.ts.map": "
|
|
1594
|
-
"dist/utils/event-catalog.js": "
|
|
1595
|
-
"dist/utils/event-catalog.js.map": "
|
|
1600
|
+
"dist/utils/event-catalog.d.ts": "ba6956f4d7897cee8ec8d22036e4c20720df24de9ef5e44834abcf7ba6903386",
|
|
1601
|
+
"dist/utils/event-catalog.d.ts.map": "f8714a599dddf26968ab11e56db6443219b750a823cab90b9d2a5075bde2c035",
|
|
1602
|
+
"dist/utils/event-catalog.js": "7ba2515de758e44ce60c8d009ad90a3a9924c02cf62aee73ff8a7efe75450f74",
|
|
1603
|
+
"dist/utils/event-catalog.js.map": "3e3b24c9d9390a486e2180e82de71fe307d6f1078f6201f0f60a6d92f9830d5b",
|
|
1596
1604
|
"dist/utils/keyed-mutex.d.ts": "0f7b21d5d90366669a83ab5aa5946767f97a984fb3661bfa0bc93f18efdbaf03",
|
|
1597
1605
|
"dist/utils/keyed-mutex.d.ts.map": "bd7fec3be0d45c963942df2ebf05d214192c36e263c62d2ab4b05655f9d470c3",
|
|
1598
1606
|
"dist/utils/keyed-mutex.js": "5e173b70d410c51b8d445c627054518f85145a0969414bb9004b1c424227d90f",
|
|
@@ -1749,6 +1757,6 @@
|
|
|
1749
1757
|
"dist/widgets/widgets/quant-desk-recommendations.d.ts.map": "410c41d3d2b37a1d8329aa888424f920424cc3afa1abd6acc64989093cbba24a",
|
|
1750
1758
|
"dist/widgets/widgets/quant-desk-recommendations.js": "91018c50913ace64ca1941c22e3de17549130a3370132b7f2ccc0477ee6d2aa0",
|
|
1751
1759
|
"dist/widgets/widgets/quant-desk-recommendations.js.map": "80b24b605271cff9acd56800f7c098c2335136e5362055c349e622b8751714c2",
|
|
1752
|
-
"openclaw.plugin.json": "
|
|
1760
|
+
"openclaw.plugin.json": "8ce59069216b5d54852bedd7cabddd2629b90451f814a03ead76220afcbcf2dd"
|
|
1753
1761
|
}
|
|
1754
1762
|
}
|
|
@@ -35,6 +35,14 @@ import type { OpenPosition } from "../types/strategy.js";
|
|
|
35
35
|
* (the provisional kind a reservation starts as, before `createPosition` returns).
|
|
36
36
|
*/
|
|
37
37
|
export type PendingOpenKind = "filled" | "resting" | "ambiguous";
|
|
38
|
+
/**
|
|
39
|
+
* - `open` — counting on its kind's TTL.
|
|
40
|
+
* - `cancelDue` — a resting entry past its TTL whose order the runtime must cancel. It keeps
|
|
41
|
+
* counting until {@link PendingOpensLedger.resolveCancel} settles what happened to the order.
|
|
42
|
+
*/
|
|
43
|
+
export type PendingOpenState = "open" | "cancelDue";
|
|
44
|
+
/** What the cancel sweep learned about a `cancelDue` entry's order. */
|
|
45
|
+
export type CancelOutcome = "cancelled" | "not_open" | "failed";
|
|
38
46
|
/** One submitted-but-unconfirmed open. */
|
|
39
47
|
export interface PendingOpenEntry {
|
|
40
48
|
/** Normalized `coin|dex` key — the same key a snapshot row reduces to. */
|
|
@@ -44,6 +52,7 @@ export interface PendingOpenEntry {
|
|
|
44
52
|
/** Margin the submit committed, subtracted from the free-margin budget while unseen. */
|
|
45
53
|
readonly marginAmount: number;
|
|
46
54
|
readonly kind: PendingOpenKind;
|
|
55
|
+
readonly state: PendingOpenState;
|
|
47
56
|
/** When the entry last changed kind (the TTL clock). */
|
|
48
57
|
readonly ts: number;
|
|
49
58
|
/**
|
|
@@ -91,12 +100,21 @@ export declare const PENDING_FILLED_TTL_MS = 60000;
|
|
|
91
100
|
*/
|
|
92
101
|
export declare const PENDING_AMBIGUOUS_TTL_MS: number;
|
|
93
102
|
/**
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
* the
|
|
103
|
+
* How long a resting entry order may sit unfilled. At this TTL the entry turns `cancelDue` and
|
|
104
|
+
* the runtime's sweep cancels the order by its oid, so a late fill can never land after the
|
|
105
|
+
* entry stopped counting. Mirroring {@link PENDING_OPEN_STALE_MS} keeps it in step with the other
|
|
106
|
+
* place the runtime gives up on an unobserved open (the position-id registry).
|
|
98
107
|
*/
|
|
99
108
|
export declare const PENDING_RESTING_TTL_MS: number;
|
|
109
|
+
/** A resting entry past its TTL, waiting for the sweep to cancel the order it names. */
|
|
110
|
+
export type DueCancel = PendingOpenEntry & {
|
|
111
|
+
readonly state: "cancelDue";
|
|
112
|
+
readonly orderId: string;
|
|
113
|
+
};
|
|
114
|
+
export interface PendingOpensSnapshot {
|
|
115
|
+
readonly entries: readonly PendingOpenEntry[];
|
|
116
|
+
readonly landed: readonly PendingOpenEntry[];
|
|
117
|
+
}
|
|
100
118
|
export interface PendingOpensLedger {
|
|
101
119
|
/**
|
|
102
120
|
* Record an open about to be submitted. Call this SYNCHRONOUSLY before awaiting
|
|
@@ -108,6 +126,17 @@ export interface PendingOpensLedger {
|
|
|
108
126
|
settle(key: string, kind: PendingOpenKind, orderId?: string): void;
|
|
109
127
|
/** Drop a reservation the venue rejected outright — it holds neither slot nor margin. */
|
|
110
128
|
release(key: string): void;
|
|
129
|
+
/**
|
|
130
|
+
* Resting entries past their TTL whose order must be cancelled. They count toward slots,
|
|
131
|
+
* margin and the daily cap until {@link resolveCancel} settles them.
|
|
132
|
+
*/
|
|
133
|
+
dueCancels(): DueCancel[];
|
|
134
|
+
/**
|
|
135
|
+
* Settle a `cancelDue` entry: `cancelled` drops it, `not_open` lands it (it filled, or
|
|
136
|
+
* someone else cancelled it, so it counts toward the daily cap until a row shows it), and
|
|
137
|
+
* `failed` leaves it due for the next sweep. No-op unless the entry is `cancelDue`.
|
|
138
|
+
*/
|
|
139
|
+
resolveCancel(key: string, outcome: CancelOutcome): void;
|
|
111
140
|
/** Drop every entry the snapshot now confirms, plus anything past its TTL. */
|
|
112
141
|
reconcile(positions: readonly OpenPosition[] | undefined): void;
|
|
113
142
|
/** Live entries this snapshot does NOT show — what the caller must add to the snapshot's own count. */
|
|
@@ -125,6 +154,8 @@ export interface PendingOpensLedgerOptions {
|
|
|
125
154
|
now?: () => number;
|
|
126
155
|
/** Per-kind TTL overrides (tests); anything unset uses the documented default. */
|
|
127
156
|
ttlMsByKind?: Partial<Record<PendingOpenKind, number>>;
|
|
157
|
+
restore?: PendingOpensSnapshot;
|
|
158
|
+
onChange?: (snapshot: PendingOpensSnapshot) => void;
|
|
128
159
|
}
|
|
129
160
|
/**
|
|
130
161
|
* The ledger key for a market: base coin + dex bucket, with `main` and the `xyz:` coin
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pending-opens-ledger.d.ts","sourceRoot":"","sources":["../../src/runtime/pending-opens-ledger.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"pending-opens-ledger.d.ts","sourceRoot":"","sources":["../../src/runtime/pending-opens-ledger.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAOzD;;;;;;;GAOG;AACH,MAAM,MAAM,eAAe,GAAG,QAAQ,GAAG,SAAS,GAAG,WAAW,CAAC;AAEjE;;;;GAIG;AACH,MAAM,MAAM,gBAAgB,GAAG,MAAM,GAAG,WAAW,CAAC;AAEpD,uEAAuE;AACvE,MAAM,MAAM,aAAa,GAAG,WAAW,GAAG,UAAU,GAAG,QAAQ,CAAC;AAEhE,0CAA0C;AAC1C,MAAM,WAAW,gBAAgB;IAC/B,0EAA0E;IAC1E,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,wFAAwF;IACxF,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,IAAI,EAAE,eAAe,CAAC;IAC/B,QAAQ,CAAC,KAAK,EAAE,gBAAgB,CAAC;IACjC,wDAAwD;IACxD,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB;;;;;OAKG;IACH,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;CAC3B;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GAAG,IAAI,CAAC;IACzD,8EAA8E;IAC9E,SAAS,CAAC,SAAS,EAAE,SAAS,YAAY,EAAE,GAAG,SAAS,GAAG,IAAI,CAAC;IAChE,uGAAuG;IACvG,MAAM,CAAC,SAAS,EAAE,SAAS,YAAY,EAAE,GAAG,SAAS,GAAG,gBAAgB,EAAE,CAAC;IAC3E;;;;;OAKG;IACH,eAAe,CAAC,KAAK,EAAE,SAAS,UAAU,EAAE,EAAE,OAAO,EAAE,MAAM,GAAG,gBAAgB,EAAE,CAAC;CACpF;AAED,MAAM,WAAW,yBAAyB;IACxC,gCAAgC;IAChC,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;IACnB,kFAAkF;IAClF,WAAW,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,eAAe,EAAE,MAAM,CAAC,CAAC,CAAC;IACvD,OAAO,CAAC,EAAE,oBAAoB,CAAC;IAC/B,QAAQ,CAAC,EAAE,CAAC,QAAQ,EAAE,oBAAoB,KAAK,IAAI,CAAC;CACrD;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,MAAM,CAGjE;AAOD,wBAAgB,wBAAwB,CACtC,OAAO,GAAE,yBAA8B,GACtC,kBAAkB,CA2IpB"}
|
|
@@ -26,7 +26,9 @@
|
|
|
26
26
|
* opened at or after its reservation and not closed before it, shows it.
|
|
27
27
|
*/
|
|
28
28
|
import { normalizeDslCoinDex } from "../utils/perp-coin.js";
|
|
29
|
+
import { createLogger } from "../utils/logger.js";
|
|
29
30
|
import { PENDING_OPEN_STALE_MS } from "./position-id-registry.js";
|
|
31
|
+
const logger = createLogger("[senpi-core][pending-opens]");
|
|
30
32
|
/**
|
|
31
33
|
* How far a row's venue-stamped open time may sit before the box's reservation clock and
|
|
32
34
|
* still be this open's fill. The venue fills after the submit, so only clock skew and the
|
|
@@ -50,10 +52,10 @@ export const PENDING_FILLED_TTL_MS = 60_000;
|
|
|
50
52
|
*/
|
|
51
53
|
export const PENDING_AMBIGUOUS_TTL_MS = 10 * 60_000;
|
|
52
54
|
/**
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
* the
|
|
55
|
+
* How long a resting entry order may sit unfilled. At this TTL the entry turns `cancelDue` and
|
|
56
|
+
* the runtime's sweep cancels the order by its oid, so a late fill can never land after the
|
|
57
|
+
* entry stopped counting. Mirroring {@link PENDING_OPEN_STALE_MS} keeps it in step with the other
|
|
58
|
+
* place the runtime gives up on an unobserved open (the position-id registry).
|
|
57
59
|
*/
|
|
58
60
|
export const PENDING_RESTING_TTL_MS = PENDING_OPEN_STALE_MS;
|
|
59
61
|
/** Trade history reports times in whole seconds, so a close inside the reservation's own second reads as that second's start. */
|
|
@@ -75,6 +77,9 @@ const DEFAULT_TTL_BY_KIND = {
|
|
|
75
77
|
resting: PENDING_RESTING_TTL_MS,
|
|
76
78
|
ambiguous: PENDING_AMBIGUOUS_TTL_MS,
|
|
77
79
|
};
|
|
80
|
+
function isDueCancel(entry) {
|
|
81
|
+
return entry.state === "cancelDue" && entry.orderId !== undefined;
|
|
82
|
+
}
|
|
78
83
|
/**
|
|
79
84
|
* The ledger key for a market: base coin + dex bucket, with `main` and the `xyz:` coin
|
|
80
85
|
* prefix normalized away so one market never occupies two keys.
|
|
@@ -92,30 +97,49 @@ export function createPendingOpensLedger(options = {}) {
|
|
|
92
97
|
const ttlFor = (kind) => options.ttlMsByKind?.[kind] ?? DEFAULT_TTL_BY_KIND[kind];
|
|
93
98
|
const entries = new Map();
|
|
94
99
|
/** Opens that became positions, kept for the daily-entry count until their UTC day ends. */
|
|
95
|
-
let landed = [];
|
|
100
|
+
let landed = [...(options.restore?.landed ?? [])];
|
|
101
|
+
for (const entry of options.restore?.entries ?? [])
|
|
102
|
+
entries.set(entry.key, entry);
|
|
103
|
+
function changed() {
|
|
104
|
+
options.onChange?.({ entries: [...entries.values()], landed: [...landed] });
|
|
105
|
+
}
|
|
96
106
|
function land(entry) {
|
|
97
107
|
entries.delete(entry.key);
|
|
98
108
|
landed.push(entry);
|
|
99
109
|
}
|
|
100
|
-
/**
|
|
101
|
-
* Drop everything past its kind's TTL. Runs before every read so no caller sees a stale
|
|
102
|
-
* claim. An expired `filled` entry still happened, so it lands; resting and ambiguous ones
|
|
103
|
-
* were never known to fill and are forgotten.
|
|
104
|
-
*/
|
|
105
110
|
function pruneExpired() {
|
|
106
111
|
const t = now();
|
|
112
|
+
let pruned = false;
|
|
107
113
|
for (const entry of [...entries.values()]) {
|
|
108
|
-
if (t - entry.ts < ttlFor(entry.kind))
|
|
114
|
+
if (entry.state === "cancelDue" || t - entry.ts < ttlFor(entry.kind))
|
|
109
115
|
continue;
|
|
110
|
-
|
|
116
|
+
pruned = true;
|
|
117
|
+
if (entry.kind === "filled") {
|
|
111
118
|
land(entry);
|
|
112
|
-
|
|
119
|
+
}
|
|
120
|
+
else if (entry.kind === "resting" && entry.orderId !== undefined) {
|
|
121
|
+
entries.set(entry.key, { ...entry, state: "cancelDue" });
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
if (entry.kind === "resting") {
|
|
125
|
+
logger.warn("resting entry expired with no order id to cancel; dropping it", {
|
|
126
|
+
coin: entry.coin,
|
|
127
|
+
dex: entry.dex,
|
|
128
|
+
ageMs: t - entry.reservedAt,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
113
131
|
entries.delete(entry.key);
|
|
132
|
+
}
|
|
114
133
|
}
|
|
115
134
|
const d = new Date(t);
|
|
116
135
|
const dayStart = Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate());
|
|
136
|
+
const landedBefore = landed.length;
|
|
117
137
|
landed = landed.filter((entry) => entry.reservedAt >= dayStart);
|
|
138
|
+
if (pruned || landed.length !== landedBefore)
|
|
139
|
+
changed();
|
|
118
140
|
}
|
|
141
|
+
if (options.restore)
|
|
142
|
+
pruneExpired();
|
|
119
143
|
return {
|
|
120
144
|
reserve(coin, dex, marginAmount) {
|
|
121
145
|
const { coin: base, dex: bucket } = normalizeDslCoinDex(coin, dex ?? "");
|
|
@@ -129,9 +153,11 @@ export function createPendingOpensLedger(options = {}) {
|
|
|
129
153
|
// exactly what `ambiguous` means, and starting anywhere else would under-count a
|
|
130
154
|
// reservation whose caller never got to settle it (a crash mid-submit).
|
|
131
155
|
kind: "ambiguous",
|
|
156
|
+
state: "open",
|
|
132
157
|
ts: now(),
|
|
133
158
|
reservedAt: now(),
|
|
134
159
|
});
|
|
160
|
+
changed();
|
|
135
161
|
return key;
|
|
136
162
|
},
|
|
137
163
|
settle(key, kind, orderId) {
|
|
@@ -144,19 +170,40 @@ export function createPendingOpensLedger(options = {}) {
|
|
|
144
170
|
ts: now(),
|
|
145
171
|
...(orderId !== undefined ? { orderId } : {}),
|
|
146
172
|
});
|
|
173
|
+
changed();
|
|
147
174
|
},
|
|
148
175
|
release(key) {
|
|
149
|
-
entries.delete(key)
|
|
176
|
+
if (entries.delete(key))
|
|
177
|
+
changed();
|
|
178
|
+
},
|
|
179
|
+
dueCancels() {
|
|
180
|
+
pruneExpired();
|
|
181
|
+
return [...entries.values()].filter(isDueCancel);
|
|
182
|
+
},
|
|
183
|
+
resolveCancel(key, outcome) {
|
|
184
|
+
const entry = entries.get(key);
|
|
185
|
+
if (entry?.state !== "cancelDue" || outcome === "failed")
|
|
186
|
+
return;
|
|
187
|
+
if (outcome === "cancelled")
|
|
188
|
+
entries.delete(key);
|
|
189
|
+
else
|
|
190
|
+
land(entry);
|
|
191
|
+
changed();
|
|
150
192
|
},
|
|
151
193
|
reconcile(positions) {
|
|
152
194
|
pruneExpired();
|
|
153
195
|
if (!positions)
|
|
154
196
|
return;
|
|
197
|
+
let landedAny = false;
|
|
155
198
|
for (const position of positions) {
|
|
156
199
|
const entry = entries.get(positionKey(position));
|
|
157
|
-
if (entry !== undefined)
|
|
200
|
+
if (entry !== undefined) {
|
|
158
201
|
land(entry);
|
|
202
|
+
landedAny = true;
|
|
203
|
+
}
|
|
159
204
|
}
|
|
205
|
+
if (landedAny)
|
|
206
|
+
changed();
|
|
160
207
|
},
|
|
161
208
|
unseen(positions) {
|
|
162
209
|
pruneExpired();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pending-opens-ledger.js","sourceRoot":"","sources":["../../src/runtime/pending-opens-ledger.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAGH,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,qBAAqB,EAAE,MAAM,2BAA2B,CAAC;
|
|
1
|
+
{"version":3,"file":"pending-opens-ledger.js","sourceRoot":"","sources":["../../src/runtime/pending-opens-ledger.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAGH,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,qBAAqB,EAAE,MAAM,2BAA2B,CAAC;AAElE,MAAM,MAAM,GAAG,YAAY,CAAC,6BAA6B,CAAC,CAAC;AA0D3D;;;;;GAKG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,MAAM,CAAC;AAEzC;;;;;GAKG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,MAAM,CAAC;AAE5C;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG,EAAE,GAAG,MAAM,CAAC;AAEpD;;;;;GAKG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,qBAAqB,CAAC;AAE5D,iIAAiI;AACjI,SAAS,aAAa,CAAC,EAAU;IAC/B,OAAO,IAAI,CAAC,KAAK,CAAC,EAAE,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC;AACtC,CAAC;AAED;;;;;;GAMG;AACH,SAAS,WAAW,CAAC,GAAe,EAAE,YAAoB;IACxD,OAAO,GA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,UAAU,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;gBACjC,MAAM,CAAC,GAAG,IAAI,CAAC,SAAS,CACtB,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CACX,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;oBACd,GAAG,CAAC,GAAG,KAAK,KAAK,CAAC,GAAG;oBACrB,GAAG,CAAC,UAAU,IAAI,KAAK,CAAC,UAAU,GAAG,kBAAkB;oBACvD,WAAW,CAAC,GAAG,EAAE,KAAK,CAAC,UAAU,CAAC,CACrC,CAAC;gBACF,IAAI,CAAC,GAAG,CAAC;oBAAE,OAAO,IAAI,CAAC;gBACvB,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;gBACZ,OAAO,KAAK,CAAC;YACf,CAAC,CAAC,CAAC;QACL,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { StateManager } from "../state/index.js";
|
|
2
|
+
import type { PendingOpensSnapshot } from "./pending-opens-ledger.js";
|
|
3
|
+
export declare const PENDING_OPENS_STATE_VERSION = 1;
|
|
4
|
+
export type PendingOpensStateStore = Pick<StateManager, "getOrLoad" | "set" | "withKeyLock">;
|
|
5
|
+
export declare function pendingOpensStateKey(address: string): string;
|
|
6
|
+
export interface PendingOpensStore {
|
|
7
|
+
load(): Promise<PendingOpensSnapshot | undefined>;
|
|
8
|
+
save(snapshot: PendingOpensSnapshot): void;
|
|
9
|
+
flush(): Promise<void>;
|
|
10
|
+
/**
|
|
11
|
+
* Finishes queued saves and drops every later one. A stopped runtime's cancel sweep may still
|
|
12
|
+
* answer after the next runtime loaded the file; its save would overwrite that runtime's entries.
|
|
13
|
+
*/
|
|
14
|
+
close(): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
export declare function createPendingOpensStore(state: PendingOpensStateStore, address: string): PendingOpensStore;
|
|
17
|
+
//# sourceMappingURL=pending-opens-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pending-opens-store.d.ts","sourceRoot":"","sources":["../../src/runtime/pending-opens-store.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEtD,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAItE,eAAO,MAAM,2BAA2B,IAAI,CAAC;AAE7C,MAAM,MAAM,sBAAsB,GAAG,IAAI,CAAC,YAAY,EAAE,WAAW,GAAG,KAAK,GAAG,aAAa,CAAC,CAAC;AAE7F,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAE5D;AAoBD,MAAM,WAAW,iBAAiB;IAChC,IAAI,IAAI,OAAO,CAAC,oBAAoB,GAAG,SAAS,CAAC,CAAC;IAClD,IAAI,CAAC,QAAQ,EAAE,oBAAoB,GAAG,IAAI,CAAC;IAC3C,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACvB;;;OAGG;IACH,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB;AAED,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,sBAAsB,EAAE,OAAO,EAAE,MAAM,GAAG,iBAAiB,CAgDzG"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { createLogger } from "../utils/logger.js";
|
|
3
|
+
const logger = createLogger("[senpi-core][pending-opens]");
|
|
4
|
+
export const PENDING_OPENS_STATE_VERSION = 1;
|
|
5
|
+
export function pendingOpensStateKey(address) {
|
|
6
|
+
return `pending-opens/${address.toLowerCase()}`;
|
|
7
|
+
}
|
|
8
|
+
const entrySchema = z.object({
|
|
9
|
+
key: z.string(),
|
|
10
|
+
coin: z.string(),
|
|
11
|
+
dex: z.string(),
|
|
12
|
+
marginAmount: z.number(),
|
|
13
|
+
kind: z.enum(["filled", "resting", "ambiguous"]),
|
|
14
|
+
state: z.enum(["open", "cancelDue"]),
|
|
15
|
+
ts: z.number(),
|
|
16
|
+
reservedAt: z.number(),
|
|
17
|
+
orderId: z.string().optional(),
|
|
18
|
+
});
|
|
19
|
+
const persistedSchema = z.object({
|
|
20
|
+
v: z.literal(PENDING_OPENS_STATE_VERSION),
|
|
21
|
+
entries: z.array(entrySchema),
|
|
22
|
+
landed: z.array(entrySchema),
|
|
23
|
+
});
|
|
24
|
+
export function createPendingOpensStore(state, address) {
|
|
25
|
+
const key = pendingOpensStateKey(address);
|
|
26
|
+
let last = Promise.resolve();
|
|
27
|
+
let closed = false;
|
|
28
|
+
return {
|
|
29
|
+
async load() {
|
|
30
|
+
let raw;
|
|
31
|
+
try {
|
|
32
|
+
raw = await state.getOrLoad(key);
|
|
33
|
+
}
|
|
34
|
+
catch (err) {
|
|
35
|
+
logger.warn("could not read the saved pending-opens ledger; starting empty", { address, err });
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
if (raw === undefined)
|
|
39
|
+
return undefined;
|
|
40
|
+
const parsed = persistedSchema.safeParse(raw);
|
|
41
|
+
if (!parsed.success) {
|
|
42
|
+
logger.warn("saved pending-opens ledger is not valid; starting empty", {
|
|
43
|
+
address,
|
|
44
|
+
issues: parsed.error.issues.slice(0, 5),
|
|
45
|
+
});
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
return { entries: parsed.data.entries, landed: parsed.data.landed };
|
|
49
|
+
},
|
|
50
|
+
save(snapshot) {
|
|
51
|
+
if (closed) {
|
|
52
|
+
logger.info("pending-opens ledger changed after its runtime stopped; not saving it", { address });
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const value = { v: PENDING_OPENS_STATE_VERSION, entries: snapshot.entries, landed: snapshot.landed };
|
|
56
|
+
last = state
|
|
57
|
+
.withKeyLock(key, () => state.set(key, value))
|
|
58
|
+
.catch((err) => {
|
|
59
|
+
logger.warn("pending-opens ledger save failed; the in-memory ledger still counts", { address, err });
|
|
60
|
+
});
|
|
61
|
+
},
|
|
62
|
+
flush() {
|
|
63
|
+
return last;
|
|
64
|
+
},
|
|
65
|
+
close() {
|
|
66
|
+
closed = true;
|
|
67
|
+
return last;
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=pending-opens-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pending-opens-store.js","sourceRoot":"","sources":["../../src/runtime/pending-opens-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAGlD,MAAM,MAAM,GAAG,YAAY,CAAC,6BAA6B,CAAC,CAAC;AAE3D,MAAM,CAAC,MAAM,2BAA2B,GAAG,CAAC,CAAC;AAI7C,MAAM,UAAU,oBAAoB,CAAC,OAAe;IAClD,OAAO,iBAAiB,OAAO,CAAC,WAAW,EAAE,EAAE,CAAC;AAClD,CAAC;AAED,MAAM,WAAW,GAAG,CAAC,CAAC,MAAM,CAAC;IAC3B,GAAG,EAAE,CAAC,CAAC,MAAM,EAAE;IACf,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE;IAChB,GAAG,EAAE,CAAC,CAAC,MAAM,EAAE;IACf,YAAY,EAAE,CAAC,CAAC,MAAM,EAAE;IACxB,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,QAAQ,EAAE,SAAS,EAAE,WAAW,CAAC,CAAC;IAChD,KAAK,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;IACpC,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE;IACd,UAAU,EAAE,CAAC,CAAC,MAAM,EAAE;IACtB,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;CAC/B,CAAC,CAAC;AAEH,MAAM,eAAe,GAAG,CAAC,CAAC,MAAM,CAAC;IAC/B,CA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AM,EAAE,CAAC;gBACX,MAAM,CAAC,IAAI,CAAC,uEAAuE,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC;gBAClG,OAAO;YACT,CAAC;YACD,MAAM,KAAK,GAAG,EAAE,CAAC,EAAE,2BAA2B,EAAE,OAAO,EAAE,QAAQ,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,CAAC,MAAM,EAAE,CAAC;YACrG,IAAI,GAAG,KAAK;iBACT,WAAW,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;iBAC7C,KAAK,CAAC,CAAC,GAAY,EAAE,EAAE;gBACtB,MAAM,CAAC,IAAI,CAAC,qEAAqE,EAAE,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC,CAAC;YACvG,CAAC,CAAC,CAAC;QACP,CAAC;QAED,KAAK;YACH,OAAO,IAAI,CAAC;QACd,CAAC;QAED,KAAK;YACH,MAAM,GAAG,IAAI,CAAC;YACd,OAAO,IAAI,CAAC;QACd,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cancels the runtime's own resting entry orders once their pending window ends.
|
|
3
|
+
*
|
|
4
|
+
* A resting entry counts toward slots, margin and the daily-entry cap only for
|
|
5
|
+
* {@link PENDING_RESTING_TTL_MS}. Left on the book past that, a late fill would land after it
|
|
6
|
+
* stopped counting and push the strategy one over all three. The ledger hands such an entry over
|
|
7
|
+
* as `cancelDue`; this sweep cancels its order and tells the ledger what happened.
|
|
8
|
+
*
|
|
9
|
+
* Only the exact oid the open path stored is ever cancelled. Runtime entries and agent manual
|
|
10
|
+
* orders both carry `AGT:` cloids, so the cloid cannot tell them apart. The guards around the oid
|
|
11
|
+
* are the coin, the order not being an exit leg, and the cloid not being a user's (`USR:`).
|
|
12
|
+
*/
|
|
13
|
+
import type { VenueOpenOrder } from "../senpi/types.js";
|
|
14
|
+
import type { Logger } from "../utils/logger.js";
|
|
15
|
+
import { type PendingOpensLedger } from "./pending-opens-ledger.js";
|
|
16
|
+
/** How often the sweep runs, so an entry is cancelled within this long of its TTL. */
|
|
17
|
+
export declare const RESTING_ENTRY_SWEEP_INTERVAL_MS = 30000;
|
|
18
|
+
/**
|
|
19
|
+
* How long one read or cancel may take before the sweep counts it failed. The gateway transport
|
|
20
|
+
* has no time limit of its own, and stop and rebuild wait for an in-flight sweep.
|
|
21
|
+
*/
|
|
22
|
+
export declare const RESTING_ENTRY_CALL_TIMEOUT_MS = 45000;
|
|
23
|
+
export interface RestingEntryCancelDeps {
|
|
24
|
+
wallet: string;
|
|
25
|
+
ledger: PendingOpensLedger;
|
|
26
|
+
readOpenOrders(wallet: string, dex: string): Promise<VenueOpenOrder[]>;
|
|
27
|
+
cancelOrder(wallet: string, oid: number): Promise<unknown>;
|
|
28
|
+
logger: Logger;
|
|
29
|
+
now?: () => number;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Whether a cloid carries the engine's user marker. On the venue the engine's mode-tagged id is
|
|
33
|
+
* hex-encoded into a 128-bit cloid, so the marker is read from the decoded bytes; a raw id is
|
|
34
|
+
* accepted too. The simulation venue hashes its cloids, and some rows carry none, so only a
|
|
35
|
+
* provable user order is refused: the oid, taken from this runtime's own create reply, is what
|
|
36
|
+
* makes the order ours.
|
|
37
|
+
*/
|
|
38
|
+
export declare function isUserCloid(cloid: string | undefined): boolean;
|
|
39
|
+
/** One pass over every `cancelDue` entry. Never throws: a failure leaves the entry due. */
|
|
40
|
+
export declare function sweepDueCancels(deps: RestingEntryCancelDeps): Promise<void>;
|
|
41
|
+
//# sourceMappingURL=resting-entry-cancel.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resting-entry-cancel.d.ts","sourceRoot":"","sources":["../../src/runtime/resting-entry-cancel.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAExD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAEjD,OAAO,EAAkC,KAAK,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAEpG,sFAAsF;AACtF,eAAO,MAAM,+BAA+B,QAAS,CAAC;AAEtD;;;GAGG;AACH,eAAO,MAAM,6BAA6B,QAAS,CAAC;AAoBpD,MAAM,WAAW,sBAAsB;IACrC,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,kBAAkB,CAAC;IAC3B,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,EAAE,CAAC,CAAC;IACvE,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAC3D,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;CACpB;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAM9D;AAYD,2FAA2F;AAC3F,wBAAsB,eAAe,CAAC,IAAI,EAAE,sBAAsB,GAAG,OAAO,CAAC,IAAI,CAAC,CAwFjF"}
|