@hyperbridge/simplex 0.16.11 → 0.16.13

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/sqlite.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { A as ActivityStore, L as LoggerContext, c as ActivityInsert, d as ActivityEvent, z as OrderSummary, O as OrderHistoryPage, W as WalletTx, B as BidStore, n as BidInsert, i as StoredBid, p as LimitOrderHold, k as BidStats, e as LimitOrderStore, q as LimitOrderInsert, f as LimitOrder, g as LimitOrderFilter, r as LimitOrderPosting, t as LimitOrderStatus, o as LimitOrderFillInsert, h as LimitOrderFill, l as StateStore, R as RuntimeState, S as SimplexDataStore } from './types-B5zkFhxR.js';
1
+ import { A as ActivityStore, L as LoggerContext, c as ActivityInsert, d as ActivityEvent, D as OrderSummary, O as OrderHistoryPage, W as WalletTx, B as BidStore, n as BidInsert, i as StoredBid, r as LimitOrderHold, k as BidStats, I as InventoryStore, o as InventorySnapshot, e as LimitOrderStore, s as LimitOrderInsert, f as LimitOrder, g as LimitOrderFilter, t as LimitOrderPosting, v as LimitOrderStatus, p as LimitOrderFillInsert, q as LimitOrderFillRecord, h as LimitOrderFill, l as StateStore, R as RuntimeState, S as SimplexDataStore } from './types-BISgqJhM.js';
2
2
  import { DatabaseSync } from 'node:sqlite';
3
3
  import 'pino';
4
4
 
@@ -51,6 +51,22 @@ declare class SqliteBidStore implements BidStore {
51
51
  recent(limit?: number): Promise<StoredBid[]>;
52
52
  }
53
53
 
54
+ /**
55
+ * SQLite-backed {@link InventoryStore}, in `activity.db` beside the rest of the history.
56
+ *
57
+ * One row per snapshot, with the balances as a JSON object rather than a row per token: a
58
+ * snapshot is only ever read whole, and the set of tokens a solver holds changes over time.
59
+ * A database written before snapshots existed simply gains the table, with nothing in it.
60
+ */
61
+ declare class SqliteInventoryStore implements InventoryStore {
62
+ private db;
63
+ constructor(db: DatabaseSync);
64
+ private toSnapshot;
65
+ record(balances: Record<string, number>): Promise<void>;
66
+ latest(): Promise<InventorySnapshot | null>;
67
+ since(from: Date): Promise<InventorySnapshot[]>;
68
+ }
69
+
54
70
  declare class SqliteLimitOrderStore implements LimitOrderStore {
55
71
  private db;
56
72
  private logger;
@@ -67,6 +83,7 @@ declare class SqliteLimitOrderStore implements LimitOrderStore {
67
83
  reserve(id: string, amount: string): Promise<boolean>;
68
84
  transaction<T>(settle: () => Promise<T>): Promise<T>;
69
85
  recordFill(fill: LimitOrderFillInsert): Promise<void>;
86
+ fillHistory(): Promise<LimitOrderFillRecord[]>;
70
87
  fills(limitOrderId: string, limit?: number): Promise<LimitOrderFill[]>;
71
88
  drawDown(id: string, amount: string): Promise<LimitOrder | null>;
72
89
  clampRemaining(id: string, room: string): Promise<LimitOrder | null>;
@@ -134,6 +151,7 @@ declare class SqliteStateStore implements StateStore {
134
151
  * `activity.db`) so an existing data directory written by an earlier version is
135
152
  * picked up unchanged. Operator state and limit orders ride in `bids.db` beside
136
153
  * the bids; a `runtime-state.json` from before that is imported once and deleted.
154
+ * Inventory snapshots ride in `activity.db`, which is the history.
137
155
  *
138
156
  * Built on `node:sqlite`, so there is nothing to install and nothing to
139
157
  * compile — the engine ships inside the Node runtime. That is why the package
@@ -145,6 +163,7 @@ declare class SqliteDataStore implements SimplexDataStore {
145
163
  readonly activity: ActivityStore;
146
164
  readonly state: StateStore;
147
165
  readonly limitOrders: LimitOrderStore;
166
+ readonly inventory: InventoryStore;
148
167
  private databases;
149
168
  private logger;
150
169
  constructor(dataDir: string, loggers?: LoggerContext);
@@ -168,4 +187,4 @@ declare class SqliteDataStore implements SimplexDataStore {
168
187
  close(): Promise<void>;
169
188
  }
170
189
 
171
- export { SqliteActivityStore, SqliteBidStore, SqliteDataStore, SqliteLimitOrderStore, SqliteStateStore };
190
+ export { SqliteActivityStore, SqliteBidStore, SqliteDataStore, SqliteInventoryStore, SqliteLimitOrderStore, SqliteStateStore };
package/dist/sqlite.js CHANGED
@@ -616,6 +616,39 @@ var SqliteBidStore = class {
616
616
  }
617
617
  };
618
618
 
619
+ // src/data/sqlite/inventory.ts
620
+ var SqliteInventoryStore = class {
621
+ constructor(db) {
622
+ this.db = db;
623
+ this.db.exec(`
624
+ CREATE TABLE IF NOT EXISTS inventory_snapshots (
625
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
626
+ taken_at TEXT NOT NULL DEFAULT (datetime('now')),
627
+ balances TEXT NOT NULL
628
+ );
629
+
630
+ CREATE INDEX IF NOT EXISTS idx_inventory_snapshots_taken_at ON inventory_snapshots(taken_at);
631
+ `);
632
+ }
633
+ toSnapshot(row) {
634
+ return { id: row.id, takenAt: row.takenAt, balances: JSON.parse(row.balances) };
635
+ }
636
+ async record(balances) {
637
+ this.db.prepare("INSERT INTO inventory_snapshots (balances) VALUES (?)").run(JSON.stringify(balances));
638
+ }
639
+ async latest() {
640
+ const row = this.db.prepare("SELECT id, taken_at as takenAt, balances FROM inventory_snapshots ORDER BY id DESC LIMIT 1").get();
641
+ return row ? this.toSnapshot(row) : null;
642
+ }
643
+ async since(from) {
644
+ const cutoff = from.toISOString().replace("T", " ").replace(/\.\d{3}Z$/, "");
645
+ const rows = this.db.prepare(
646
+ "SELECT id, taken_at as takenAt, balances FROM inventory_snapshots WHERE taken_at >= ? ORDER BY taken_at, id"
647
+ ).all(cutoff);
648
+ return rows.map((row) => this.toSnapshot(row));
649
+ }
650
+ };
651
+
619
652
  // src/data/sqlite/limit-orders.ts
620
653
  var LIMIT_ORDER_COLUMNS = `
621
654
  id,
@@ -687,12 +720,17 @@ var SqliteLimitOrderStore = class {
687
720
  commitment TEXT NOT NULL,
688
721
  bid TEXT,
689
722
  amount TEXT NOT NULL,
723
+ amount_in TEXT,
690
724
  transaction_hash TEXT,
691
725
  filled_at TEXT NOT NULL DEFAULT (datetime('now'))
692
726
  );
693
727
 
694
728
  CREATE INDEX IF NOT EXISTS idx_limit_order_fills_order ON limit_order_fills(limit_order_id);
695
729
  `);
730
+ if (!columnNames(this.db, "limit_order_fills").has("amount_in")) {
731
+ this.db.exec("ALTER TABLE limit_order_fills ADD COLUMN amount_in TEXT");
732
+ this.logger.info({ column: "amount_in" }, "Migrated limit order fill schema");
733
+ }
696
734
  }
697
735
  // biome-ignore lint/suspicious/noExplicitAny: raw sqlite row
698
736
  toLimitOrder(row) {
@@ -802,12 +840,27 @@ var SqliteLimitOrderStore = class {
802
840
  }
803
841
  async recordFill(fill) {
804
842
  this.db.prepare(
805
- "INSERT INTO limit_order_fills (limit_order_id, commitment, bid, amount, transaction_hash) VALUES (?, ?, ?, ?, ?)"
806
- ).run(fill.limitOrderId, fill.commitment, fill.bid ?? null, fill.amount, fill.transactionHash ?? null);
843
+ "INSERT INTO limit_order_fills (limit_order_id, commitment, bid, amount, amount_in, transaction_hash) VALUES (?, ?, ?, ?, ?, ?)"
844
+ ).run(
845
+ fill.limitOrderId,
846
+ fill.commitment,
847
+ fill.bid ?? null,
848
+ fill.amount,
849
+ fill.amountIn ?? null,
850
+ fill.transactionHash ?? null
851
+ );
852
+ }
853
+ async fillHistory() {
854
+ return this.db.prepare(
855
+ `SELECT f.id, f.limit_order_id as limitOrderId, o.book, o.base, o.quote, o.side, o.price,
856
+ f.amount, f.amount_in as amountIn, f.filled_at as filledAt
857
+ FROM limit_order_fills f JOIN limit_orders o ON o.id = f.limit_order_id
858
+ ORDER BY f.filled_at, f.id`
859
+ ).all();
807
860
  }
808
861
  async fills(limitOrderId, limit = 100) {
809
862
  return this.db.prepare(
810
- `SELECT id, limit_order_id as limitOrderId, commitment, bid, amount, transaction_hash as transactionHash,
863
+ `SELECT id, limit_order_id as limitOrderId, commitment, bid, amount, amount_in as amountIn, transaction_hash as transactionHash,
811
864
  filled_at as filledAt
812
865
  FROM limit_order_fills WHERE limit_order_id = ? ORDER BY id DESC LIMIT ?`
813
866
  ).all(limitOrderId, Math.min(Math.max(limit, 1), 500));
@@ -994,6 +1047,7 @@ var SqliteDataStore = class {
994
1047
  this.activity = new SqliteActivityStore(activityDb, loggers);
995
1048
  this.state = new SqliteStateStore(bidsDb, dataDir, loggers);
996
1049
  this.limitOrders = new SqliteLimitOrderStore(bidsDb, loggers);
1050
+ this.inventory = new SqliteInventoryStore(activityDb);
997
1051
  this.logger.info({ dataDir }, "SQLite data store opened");
998
1052
  }
999
1053
  /**
@@ -1028,6 +1082,7 @@ export {
1028
1082
  SqliteActivityStore,
1029
1083
  SqliteBidStore,
1030
1084
  SqliteDataStore,
1085
+ SqliteInventoryStore,
1031
1086
  SqliteLimitOrderStore,
1032
1087
  SqliteStateStore
1033
1088
  };