@moneypot/hub 1.4.5 → 1.4.7

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.
@@ -135,9 +135,23 @@ export type DbHash = {
135
135
  hash_chain_id: string;
136
136
  iteration: number;
137
137
  digest: Uint8Array;
138
+ client_seed: string;
138
139
  metadata: Record<string, unknown>;
139
140
  };
140
- export type DbHubOutcome = {
141
+ export type DbOutcome = {
141
142
  weight: number;
142
143
  profit: number;
143
144
  };
145
+ export type DbOutcomeBet = {
146
+ id: string;
147
+ hash_id: DbHash["id"];
148
+ kind: string;
149
+ wager: number;
150
+ currency_key: string;
151
+ profit: number;
152
+ user_id: string;
153
+ casino_id: string;
154
+ experience_id: string;
155
+ outcomes: DbOutcome[];
156
+ outcome_idx: number;
157
+ };
@@ -6,10 +6,11 @@ export declare function dbLockHubHashChain(pgClient: PgClientInTransaction, { us
6
6
  casinoId: DbCasino["id"];
7
7
  hashChainId: DbHashChain["id"];
8
8
  }): Promise<DbHashChain | null>;
9
- export declare function dbInsertHubHash(pgClient: PgClientInTransaction, { hashChainId, kind, digest, iteration, metadata, }: {
9
+ export declare function dbInsertHubHash(pgClient: PgClientInTransaction, { hashChainId, kind, digest, iteration, clientSeed, metadata, }: {
10
10
  hashChainId: DbHashChain["id"];
11
11
  kind: DbHash["kind"];
12
12
  digest: DbHash["digest"];
13
13
  iteration: number;
14
+ clientSeed?: string;
14
15
  metadata?: DbHash["metadata"];
15
16
  }): Promise<DbHash>;
@@ -1,3 +1,4 @@
1
+ import { DbHashKind, } from "../db/types.js";
1
2
  import { exactlyOneRow, maybeOneRow, } from "../db/index.js";
2
3
  import { assert } from "tsafe";
3
4
  export async function dbLockHubHashChain(pgClient, { userId, experienceId, casinoId, hashChainId, }) {
@@ -17,18 +18,20 @@ export async function dbLockHubHashChain(pgClient, { userId, experienceId, casin
17
18
  .then(maybeOneRow)
18
19
  .then((row) => row ?? null);
19
20
  }
20
- export async function dbInsertHubHash(pgClient, { hashChainId, kind, digest, iteration, metadata = {}, }) {
21
+ export async function dbInsertHubHash(pgClient, { hashChainId, kind, digest, iteration, clientSeed, metadata = {}, }) {
21
22
  assert(pgClient._inTransaction, "dbInsertHash must be called in a transaction");
23
+ assert(kind === DbHashKind.INTERMEDIATE && typeof clientSeed === "string", "clientSeed must be provided for INTERMEDIATE hashes");
22
24
  return pgClient
23
25
  .query(`
24
- INSERT INTO hub.hash (hash_chain_id, kind, digest, iteration, metadata)
25
- VALUES ($1, $2, $3, $4, $5)
26
+ INSERT INTO hub.hash (hash_chain_id, kind, digest, iteration, client_seed, metadata)
27
+ VALUES ($1, $2, $3, $4, $5, $6)
26
28
  RETURNING *
27
29
  `, [
28
30
  hashChainId,
29
31
  kind,
30
32
  digest,
31
33
  iteration,
34
+ clientSeed,
32
35
  metadata,
33
36
  ])
34
37
  .then(exactlyOneRow);
@@ -0,0 +1,54 @@
1
+ -- Migration: Add hash_id to hub.outcome_bet table
2
+
3
+ -- Add the hash_id column (nullable initially)
4
+ ALTER TABLE hub.outcome_bet
5
+ ADD COLUMN hash_id uuid REFERENCES hub.hash(id);
6
+
7
+ -- Index
8
+ CREATE INDEX outcome_bet_hash_id_idx ON hub.outcome_bet(hash_id);
9
+
10
+ -- Update existing rows to set hash_id based on hash_chain_id
11
+ -- This uses a window function to assign row numbers within each hash_chain_id group
12
+ -- ordered by the bet creation time (id)
13
+ WITH bet_iterations AS (
14
+ SELECT
15
+ ob.id,
16
+ ob.hash_chain_id,
17
+ hc.max_iteration,
18
+ -- Row number starting from 1 for each hash_chain_id group
19
+ -- Ordered by id (which is uuid v7, so it's time-ordered)
20
+ ROW_NUMBER() OVER (PARTITION BY ob.hash_chain_id ORDER BY ob.id) as bet_sequence
21
+ FROM hub.outcome_bet ob
22
+ JOIN hub.hash_chain hc ON hc.id = ob.hash_chain_id
23
+ ),
24
+ hash_mapping AS (
25
+ SELECT
26
+ bi.id as bet_id,
27
+ h.id as hash_id
28
+ FROM bet_iterations bi
29
+ JOIN hub.hash h ON
30
+ h.hash_chain_id = bi.hash_chain_id AND
31
+ -- Calculate the iteration: max_iteration - bet_sequence
32
+ h.iteration = bi.max_iteration - bi.bet_sequence
33
+ WHERE h.kind = 'INTERMEDIATE'
34
+ )
35
+ UPDATE hub.outcome_bet ob
36
+ SET hash_id = hm.hash_id
37
+ FROM hash_mapping hm
38
+ WHERE ob.id = hm.bet_id;
39
+
40
+ -- Make hash_id NOT NULL after populating it
41
+ ALTER TABLE hub.outcome_bet ALTER COLUMN hash_id SET NOT NULL;
42
+
43
+ -- Add client_seed column to hash table
44
+ ALTER TABLE hub.hash ADD COLUMN client_seed text null;
45
+
46
+ -- Update hash table with client_seed from hash_chain for each outcome_bet
47
+ UPDATE hub.hash h
48
+ SET client_seed = hc.client_seed
49
+ FROM hub.outcome_bet ob
50
+ JOIN hub.hash_chain hc ON hc.id = ob.hash_chain_id
51
+ WHERE h.id = ob.hash_id;
52
+
53
+ -- Drop hash chain id
54
+ ALTER TABLE hub.outcome_bet DROP COLUMN hash_chain_id;
@@ -1,15 +1,6 @@
1
1
  import { z } from "zod";
2
+ import { DbOutcome } from "../db/index.js";
2
3
  import { Result } from "../util.js";
3
- declare const OutcomeSchema: z.ZodObject<{
4
- weight: z.ZodNumber;
5
- profit: z.ZodNumber;
6
- }, "strict", z.ZodTypeAny, {
7
- profit: number;
8
- weight: number;
9
- }, {
10
- profit: number;
11
- weight: number;
12
- }>;
13
4
  declare const InputSchema: z.ZodObject<{
14
5
  kind: z.ZodString;
15
6
  clientSeed: z.ZodString;
@@ -63,14 +54,13 @@ declare const InputSchema: z.ZodObject<{
63
54
  metadata?: Record<string, unknown> | undefined;
64
55
  }>;
65
56
  type Input = z.infer<typeof InputSchema>;
66
- type Outcome = z.infer<typeof OutcomeSchema>;
67
57
  type Metadata = NonNullable<Input["metadata"]>;
68
58
  type FinalizeMetadataData = {
69
59
  wager: number;
70
60
  currencyKey: string;
71
61
  clientSeed: string;
72
62
  hash: Uint8Array;
73
- outcomes: Outcome[];
63
+ outcomes: DbOutcome[];
74
64
  outcomeIdx: number;
75
65
  roll: number;
76
66
  };
@@ -232,11 +232,12 @@ export function MakeOutcomeBetPlugin({ betConfigs }) {
232
232
  throw new Error(`Unknown bet hash result: ${_exhaustiveCheck}`);
233
233
  }
234
234
  }
235
- await dbInsertHubHash(pgClient, {
235
+ const dbHash = await dbInsertHubHash(pgClient, {
236
236
  hashChainId: dbHashChain.id,
237
237
  kind: DbHashKind.INTERMEDIATE,
238
238
  digest: betHashResult.hash,
239
239
  iteration: betHashIteration,
240
+ clientSeed: input.clientSeed,
240
241
  });
241
242
  const result = await pgClient.query(`
242
243
  UPDATE hub.hash_chain
@@ -309,11 +310,10 @@ export function MakeOutcomeBetPlugin({ betConfigs }) {
309
310
  wager: input.wager,
310
311
  profit: outcome.profit,
311
312
  currency_key: dbCurrency.key,
312
- hash_chain_id: input.hashChainId,
313
+ hash_id: dbHash.id,
313
314
  user_id: session.user_id,
314
315
  casino_id: session.casino_id,
315
316
  experience_id: session.experience_id,
316
- client_seed: input.clientSeed,
317
317
  metadata: finalizedMetadata || {},
318
318
  ...(betConfig.saveOutcomes
319
319
  ? {
@@ -332,31 +332,29 @@ export function MakeOutcomeBetPlugin({ betConfigs }) {
332
332
  user_id,
333
333
  casino_id,
334
334
  experience_id,
335
- hash_chain_id,
335
+ hash_id,
336
336
  kind,
337
337
  currency_key,
338
338
  wager,
339
339
  profit,
340
340
  outcomes,
341
341
  outcome_idx,
342
- client_seed,
343
342
  metadata
344
343
  )
345
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
344
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
346
345
  RETURNING id
347
346
  `,
348
347
  values: [
349
348
  newBet.user_id,
350
349
  newBet.casino_id,
351
350
  newBet.experience_id,
352
- newBet.hash_chain_id,
351
+ newBet.hash_id,
353
352
  newBet.kind,
354
353
  newBet.currency_key,
355
354
  newBet.wager,
356
355
  newBet.profit,
357
356
  newBet.outcomes.map((o) => `(${o.weight},${o.profit})`),
358
357
  newBet.outcome_idx,
359
- newBet.client_seed,
360
358
  newBet.metadata,
361
359
  ],
362
360
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moneypot/hub",
3
- "version": "1.4.5",
3
+ "version": "1.4.7",
4
4
  "author": "moneypot.com",
5
5
  "homepage": "https://moneypot.com/hub",
6
6
  "keywords": [
@@ -1,9 +0,0 @@
1
- alter table hub.outcome_bet add column client_seed text;
2
-
3
- update hub.outcome_bet
4
- set client_seed = hash_chain.client_seed
5
- from hub.hash_chain where outcome_bet.hash_chain_id = hash_chain.id;
6
-
7
- alter table hub.outcome_bet alter column client_seed set not null;
8
-
9
- alter table hub.hash_chain drop column client_seed;