@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.cjs CHANGED
@@ -33,6 +33,7 @@ __export(sqlite_exports, {
33
33
  SqliteActivityStore: () => SqliteActivityStore,
34
34
  SqliteBidStore: () => SqliteBidStore,
35
35
  SqliteDataStore: () => SqliteDataStore,
36
+ SqliteInventoryStore: () => SqliteInventoryStore,
36
37
  SqliteLimitOrderStore: () => SqliteLimitOrderStore,
37
38
  SqliteStateStore: () => SqliteStateStore
38
39
  });
@@ -652,6 +653,39 @@ var SqliteBidStore = class {
652
653
  }
653
654
  };
654
655
 
656
+ // src/data/sqlite/inventory.ts
657
+ var SqliteInventoryStore = class {
658
+ constructor(db) {
659
+ this.db = db;
660
+ this.db.exec(`
661
+ CREATE TABLE IF NOT EXISTS inventory_snapshots (
662
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
663
+ taken_at TEXT NOT NULL DEFAULT (datetime('now')),
664
+ balances TEXT NOT NULL
665
+ );
666
+
667
+ CREATE INDEX IF NOT EXISTS idx_inventory_snapshots_taken_at ON inventory_snapshots(taken_at);
668
+ `);
669
+ }
670
+ toSnapshot(row) {
671
+ return { id: row.id, takenAt: row.takenAt, balances: JSON.parse(row.balances) };
672
+ }
673
+ async record(balances) {
674
+ this.db.prepare("INSERT INTO inventory_snapshots (balances) VALUES (?)").run(JSON.stringify(balances));
675
+ }
676
+ async latest() {
677
+ const row = this.db.prepare("SELECT id, taken_at as takenAt, balances FROM inventory_snapshots ORDER BY id DESC LIMIT 1").get();
678
+ return row ? this.toSnapshot(row) : null;
679
+ }
680
+ async since(from) {
681
+ const cutoff = from.toISOString().replace("T", " ").replace(/\.\d{3}Z$/, "");
682
+ const rows = this.db.prepare(
683
+ "SELECT id, taken_at as takenAt, balances FROM inventory_snapshots WHERE taken_at >= ? ORDER BY taken_at, id"
684
+ ).all(cutoff);
685
+ return rows.map((row) => this.toSnapshot(row));
686
+ }
687
+ };
688
+
655
689
  // src/data/sqlite/limit-orders.ts
656
690
  var LIMIT_ORDER_COLUMNS = `
657
691
  id,
@@ -723,12 +757,17 @@ var SqliteLimitOrderStore = class {
723
757
  commitment TEXT NOT NULL,
724
758
  bid TEXT,
725
759
  amount TEXT NOT NULL,
760
+ amount_in TEXT,
726
761
  transaction_hash TEXT,
727
762
  filled_at TEXT NOT NULL DEFAULT (datetime('now'))
728
763
  );
729
764
 
730
765
  CREATE INDEX IF NOT EXISTS idx_limit_order_fills_order ON limit_order_fills(limit_order_id);
731
766
  `);
767
+ if (!columnNames(this.db, "limit_order_fills").has("amount_in")) {
768
+ this.db.exec("ALTER TABLE limit_order_fills ADD COLUMN amount_in TEXT");
769
+ this.logger.info({ column: "amount_in" }, "Migrated limit order fill schema");
770
+ }
732
771
  }
733
772
  // biome-ignore lint/suspicious/noExplicitAny: raw sqlite row
734
773
  toLimitOrder(row) {
@@ -838,12 +877,27 @@ var SqliteLimitOrderStore = class {
838
877
  }
839
878
  async recordFill(fill) {
840
879
  this.db.prepare(
841
- "INSERT INTO limit_order_fills (limit_order_id, commitment, bid, amount, transaction_hash) VALUES (?, ?, ?, ?, ?)"
842
- ).run(fill.limitOrderId, fill.commitment, fill.bid ?? null, fill.amount, fill.transactionHash ?? null);
880
+ "INSERT INTO limit_order_fills (limit_order_id, commitment, bid, amount, amount_in, transaction_hash) VALUES (?, ?, ?, ?, ?, ?)"
881
+ ).run(
882
+ fill.limitOrderId,
883
+ fill.commitment,
884
+ fill.bid ?? null,
885
+ fill.amount,
886
+ fill.amountIn ?? null,
887
+ fill.transactionHash ?? null
888
+ );
889
+ }
890
+ async fillHistory() {
891
+ return this.db.prepare(
892
+ `SELECT f.id, f.limit_order_id as limitOrderId, o.book, o.base, o.quote, o.side, o.price,
893
+ f.amount, f.amount_in as amountIn, f.filled_at as filledAt
894
+ FROM limit_order_fills f JOIN limit_orders o ON o.id = f.limit_order_id
895
+ ORDER BY f.filled_at, f.id`
896
+ ).all();
843
897
  }
844
898
  async fills(limitOrderId, limit = 100) {
845
899
  return this.db.prepare(
846
- `SELECT id, limit_order_id as limitOrderId, commitment, bid, amount, transaction_hash as transactionHash,
900
+ `SELECT id, limit_order_id as limitOrderId, commitment, bid, amount, amount_in as amountIn, transaction_hash as transactionHash,
847
901
  filled_at as filledAt
848
902
  FROM limit_order_fills WHERE limit_order_id = ? ORDER BY id DESC LIMIT ?`
849
903
  ).all(limitOrderId, Math.min(Math.max(limit, 1), 500));
@@ -1030,6 +1084,7 @@ var SqliteDataStore = class {
1030
1084
  this.activity = new SqliteActivityStore(activityDb, loggers);
1031
1085
  this.state = new SqliteStateStore(bidsDb, dataDir, loggers);
1032
1086
  this.limitOrders = new SqliteLimitOrderStore(bidsDb, loggers);
1087
+ this.inventory = new SqliteInventoryStore(activityDb);
1033
1088
  this.logger.info({ dataDir }, "SQLite data store opened");
1034
1089
  }
1035
1090
  /**
@@ -1065,6 +1120,7 @@ var SqliteDataStore = class {
1065
1120
  SqliteActivityStore,
1066
1121
  SqliteBidStore,
1067
1122
  SqliteDataStore,
1123
+ SqliteInventoryStore,
1068
1124
  SqliteLimitOrderStore,
1069
1125
  SqliteStateStore
1070
1126
  });