@hyperbridge/simplex 0.16.12 → 0.17.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/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,
@@ -670,6 +704,7 @@ var LIMIT_ORDER_COLUMNS = `
670
704
  status,
671
705
  commitment,
672
706
  order_nonce as orderNonce,
707
+ entry_point as entryPoint,
673
708
  book_expires_at as bookExpiresAt,
674
709
  book_price as bookPrice,
675
710
  last_error as lastError,
@@ -706,6 +741,7 @@ var SqliteLimitOrderStore = class {
706
741
  status TEXT NOT NULL,
707
742
  commitment TEXT,
708
743
  order_nonce TEXT NOT NULL DEFAULT '0',
744
+ entry_point TEXT,
709
745
  book_expires_at TEXT,
710
746
  book_price TEXT,
711
747
  last_error TEXT,
@@ -723,12 +759,21 @@ var SqliteLimitOrderStore = class {
723
759
  commitment TEXT NOT NULL,
724
760
  bid TEXT,
725
761
  amount TEXT NOT NULL,
762
+ amount_in TEXT,
726
763
  transaction_hash TEXT,
727
764
  filled_at TEXT NOT NULL DEFAULT (datetime('now'))
728
765
  );
729
766
 
730
767
  CREATE INDEX IF NOT EXISTS idx_limit_order_fills_order ON limit_order_fills(limit_order_id);
731
768
  `);
769
+ if (!columnNames(this.db, "limit_orders").has("entry_point")) {
770
+ this.db.exec("ALTER TABLE limit_orders ADD COLUMN entry_point TEXT");
771
+ this.logger.info({ column: "entry_point" }, "Migrated limit order storage schema");
772
+ }
773
+ if (!columnNames(this.db, "limit_order_fills").has("amount_in")) {
774
+ this.db.exec("ALTER TABLE limit_order_fills ADD COLUMN amount_in TEXT");
775
+ this.logger.info({ column: "amount_in" }, "Migrated limit order fill schema");
776
+ }
732
777
  }
733
778
  // biome-ignore lint/suspicious/noExplicitAny: raw sqlite row
734
779
  toLimitOrder(row) {
@@ -789,13 +834,14 @@ var SqliteLimitOrderStore = class {
789
834
  const result = this.db.prepare(`
790
835
  UPDATE limit_orders
791
836
  SET commitment = ?, book_expires_at = ?, book_price = ?, order_nonce = ?,
792
- status = ?, last_error = ?, updated_at = datetime('now')
837
+ entry_point = ?, status = ?, last_error = ?, updated_at = datetime('now')
793
838
  WHERE id = ?${statusGuard(only)}
794
839
  `).run(
795
840
  posting.commitment,
796
841
  posting.bookExpiresAt,
797
842
  posting.bookPrice,
798
843
  posting.orderNonce,
844
+ posting.entryPoint,
799
845
  posting.status,
800
846
  posting.lastError,
801
847
  id,
@@ -838,12 +884,27 @@ var SqliteLimitOrderStore = class {
838
884
  }
839
885
  async recordFill(fill) {
840
886
  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);
887
+ "INSERT INTO limit_order_fills (limit_order_id, commitment, bid, amount, amount_in, transaction_hash) VALUES (?, ?, ?, ?, ?, ?)"
888
+ ).run(
889
+ fill.limitOrderId,
890
+ fill.commitment,
891
+ fill.bid ?? null,
892
+ fill.amount,
893
+ fill.amountIn ?? null,
894
+ fill.transactionHash ?? null
895
+ );
896
+ }
897
+ async fillHistory() {
898
+ return this.db.prepare(
899
+ `SELECT f.id, f.limit_order_id as limitOrderId, o.book, o.base, o.quote, o.side, o.price,
900
+ f.amount, f.amount_in as amountIn, f.filled_at as filledAt
901
+ FROM limit_order_fills f JOIN limit_orders o ON o.id = f.limit_order_id
902
+ ORDER BY f.filled_at, f.id`
903
+ ).all();
843
904
  }
844
905
  async fills(limitOrderId, limit = 100) {
845
906
  return this.db.prepare(
846
- `SELECT id, limit_order_id as limitOrderId, commitment, bid, amount, transaction_hash as transactionHash,
907
+ `SELECT id, limit_order_id as limitOrderId, commitment, bid, amount, amount_in as amountIn, transaction_hash as transactionHash,
847
908
  filled_at as filledAt
848
909
  FROM limit_order_fills WHERE limit_order_id = ? ORDER BY id DESC LIMIT ?`
849
910
  ).all(limitOrderId, Math.min(Math.max(limit, 1), 500));
@@ -1030,6 +1091,7 @@ var SqliteDataStore = class {
1030
1091
  this.activity = new SqliteActivityStore(activityDb, loggers);
1031
1092
  this.state = new SqliteStateStore(bidsDb, dataDir, loggers);
1032
1093
  this.limitOrders = new SqliteLimitOrderStore(bidsDb, loggers);
1094
+ this.inventory = new SqliteInventoryStore(activityDb);
1033
1095
  this.logger.info({ dataDir }, "SQLite data store opened");
1034
1096
  }
1035
1097
  /**
@@ -1065,6 +1127,7 @@ var SqliteDataStore = class {
1065
1127
  SqliteActivityStore,
1066
1128
  SqliteBidStore,
1067
1129
  SqliteDataStore,
1130
+ SqliteInventoryStore,
1068
1131
  SqliteLimitOrderStore,
1069
1132
  SqliteStateStore
1070
1133
  });