@secondlayer/shared 15.6.0 → 15.6.1

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.
@@ -18,7 +18,10 @@ import { onChainPlane } from "../src/db/migration-role.ts";
18
18
  * supply shortfall: decoded mint−burn read 2,331.6 BTC vs on-chain 2,954.7).
19
19
  *
20
20
  * The logical key is `(block_height, tx_id, event_index)`:
21
- * - `event_index` is per-transaction, so `tx_id` is required to disambiguate.
21
+ * - Raw `event_index` has two eras: below ~block 7,440,500 (imported history)
22
+ * it restarts at 0 in each transaction, above that it counts across the
23
+ * whole block. Streams computes the block position itself. The key is
24
+ * unique under both, and `tx_id` is what disambiguates the per-tx era.
22
25
  * - `block_height` is included (not implied-redundant) on purpose: a reorg can
23
26
  * legitimately leave the same `(tx_id, event_index)` at two heights (orphan +
24
27
  * canonical). Keying on height permits those as distinct physical rows while
@@ -0,0 +1,69 @@
1
+ import { type Kysely, sql } from "kysely";
2
+ import { onControlPlane } from "../src/db/migration-role.ts";
3
+
4
+ /**
5
+ * Replaces `usage_ledger_unit_check` from 0196 so the ledger accepts the
6
+ * `subgraph.read` unit (hosted subgraph table reads, counted by the workload
7
+ * gateway). Same shape as 0196: every priced `MeterUnit` (retired `sentinel.*`
8
+ * included), `topup`, `grant`, `refund`; added `NOT VALID` so it binds every
9
+ * new write without blocking the deploy on historical rows. Skipped when the
10
+ * table is absent.
11
+ *
12
+ * Adding a unit means adding it to `prices.ts` AND to a new migration that
13
+ * replaces `usage_ledger_unit_check`; `meter-invariants.test.ts` fails when the
14
+ * two drift.
15
+ */
16
+
17
+ const PREVIOUS_METER_UNITS = [
18
+ "archive.partition",
19
+ "archive.partition.events",
20
+ "rows.delivered",
21
+ "memory.gb_hour",
22
+ "storage.gb_day",
23
+ "webhook.event",
24
+ "sentinel.run",
25
+ "sentinel.deep_audit",
26
+ "sentinel.check_run",
27
+ "sentinel.monitored_event",
28
+ ];
29
+ const METER_UNITS = [...PREVIOUS_METER_UNITS, "subgraph.read"];
30
+ const LEDGER_ONLY_UNITS = ["topup", "grant", "refund"];
31
+
32
+ function unitCheck(meterUnits: string[]): string {
33
+ const list = [...meterUnits, ...LEDGER_ONLY_UNITS]
34
+ .map((u) => `'${u}'`)
35
+ .join(", ");
36
+ return `unit IN (${list})`;
37
+ }
38
+
39
+ const NAME = "usage_ledger_unit_check";
40
+
41
+ async function tableExists(db: Kysely<unknown>, table: string) {
42
+ const { rows } = await sql<{ exists: boolean }>`
43
+ SELECT to_regclass(${`public.${table}`}) IS NOT NULL AS exists
44
+ `.execute(db);
45
+ return rows[0]?.exists === true;
46
+ }
47
+
48
+ async function replaceUnitCheck(db: Kysely<unknown>, meterUnits: string[]) {
49
+ await onControlPlane(async () => {
50
+ if (!(await tableExists(db, "usage_ledger"))) return;
51
+ await sql`ALTER TABLE usage_ledger DROP CONSTRAINT IF EXISTS ${sql.id(NAME)}`.execute(
52
+ db,
53
+ );
54
+ await sql`
55
+ ALTER TABLE usage_ledger
56
+ ADD CONSTRAINT ${sql.id(NAME)} CHECK (${sql.raw(unitCheck(meterUnits))}) NOT VALID
57
+ `.execute(db);
58
+ });
59
+ }
60
+
61
+ export async function up(db: Kysely<unknown>): Promise<void> {
62
+ await sql`SET lock_timeout = '30s'`.execute(db);
63
+ await replaceUnitCheck(db, METER_UNITS);
64
+ }
65
+
66
+ export async function down(db: Kysely<unknown>): Promise<void> {
67
+ await sql`SET lock_timeout = '30s'`.execute(db);
68
+ await replaceUnitCheck(db, PREVIOUS_METER_UNITS);
69
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@secondlayer/shared",
3
- "version": "15.6.0",
3
+ "version": "15.6.1",
4
4
  "description": "Internal building blocks shared across Secondlayer packages.",
5
5
  "license": "MIT",
6
6
  "repository": {