@moneypot/hub 1.4.2 → 1.4.4

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/README.md CHANGED
@@ -76,6 +76,10 @@ insert into hub.api_key default values returning key;
76
76
 
77
77
  ## Changelog
78
78
 
79
+ ### 1.4.4
80
+
81
+ - Added `allowLossBeyondWager` option to `MakeOutcomeBetPlugin`. Must be set to `true` to allow outcome profits to be less than -1.
82
+
79
83
  ### 1.4.1
80
84
 
81
85
  - Added `hubPutAlert` subscription
@@ -1,8 +1,7 @@
1
- import * as pg from "pg";
2
1
  import { DbBalance, DbBankroll, DbCurrency, DbSession } from "./types.js";
3
- import { PgClientInTransaction } from "./index.js";
4
- export declare function getActiveSessionById(pgClient: pg.PoolClient, sessionId: string): Promise<DbSession | undefined>;
5
- export declare function getCasinoCurrencyByKey(pgClient: pg.PoolClient, { currencyKey, casinoId }: {
2
+ import { PgClientInTransaction, QueryExecutor } from "./index.js";
3
+ export declare function dbGetActiveSessionById(pgClient: QueryExecutor, sessionId: string): Promise<DbSession | undefined>;
4
+ export declare function dbGetCasinoCurrencyByKey(pgClient: QueryExecutor, { currencyKey, casinoId }: {
6
5
  currencyKey: string;
7
6
  casinoId: string;
8
7
  }): Promise<DbCurrency | undefined>;
@@ -17,6 +16,6 @@ export declare function dbLockPlayerBalanceAndHouseBankroll(pgClient: PgClientIn
17
16
  dbHouseBankroll: null;
18
17
  } | {
19
18
  found: true;
20
- dbPlayerBalance: DbBalance["amount"];
21
- dbHouseBankroll: DbBankroll["amount"];
19
+ dbPlayerBalance: Pick<DbBalance, "id" | "amount">;
20
+ dbHouseBankroll: Pick<DbBankroll, "id" | "amount">;
22
21
  }>;
@@ -1,22 +1,22 @@
1
1
  import { maybeOneRow } from "./util.js";
2
2
  import { assert } from "tsafe";
3
- export async function getActiveSessionById(pgClient, sessionId) {
3
+ export async function dbGetActiveSessionById(pgClient, sessionId) {
4
4
  return pgClient
5
- .query("select * from hub.active_session where id = $1", [
6
- sessionId,
7
- ])
5
+ .query(`
6
+ SELECT *
7
+ FROM hub.active_session
8
+ WHERE id = $1
9
+ `, [sessionId])
8
10
  .then(maybeOneRow);
9
11
  }
10
- export async function getCasinoCurrencyByKey(pgClient, { currencyKey, casinoId }) {
12
+ export async function dbGetCasinoCurrencyByKey(pgClient, { currencyKey, casinoId }) {
11
13
  return pgClient
12
- .query({
13
- text: `
14
+ .query(`
14
15
  SELECT *
15
16
  FROM hub.currency
16
- WHERE key = $1 AND casino_id = $2
17
- `,
18
- values: [currencyKey, casinoId],
19
- })
17
+ WHERE key = $1
18
+ AND casino_id = $2
19
+ `, [currencyKey, casinoId])
20
20
  .then(maybeOneRow);
21
21
  }
22
22
  export async function dbLockPlayerBalanceAndHouseBankroll(pgClient, { userId, casinoId, experienceId, currencyKey, }) {
@@ -24,7 +24,7 @@ export async function dbLockPlayerBalanceAndHouseBankroll(pgClient, { userId, ca
24
24
  const row = await pgClient
25
25
  .query(`
26
26
  WITH locked_balance AS (
27
- SELECT amount
27
+ SELECT id, amount
28
28
  FROM hub.balance
29
29
  WHERE user_id = $1
30
30
  AND casino_id = $2
@@ -34,8 +34,8 @@ export async function dbLockPlayerBalanceAndHouseBankroll(pgClient, { userId, ca
34
34
  FOR UPDATE
35
35
  )
36
36
  SELECT
37
- pb.amount as player_balance,
38
- br.amount as house_bankroll
37
+ json_build_object('id', pb.id, 'amount', pb.amount) as player_balance,
38
+ json_build_object('id', br.id, 'amount', br.amount) as house_bankroll
39
39
  FROM locked_balance pb
40
40
  JOIN hub.bankroll br
41
41
  ON br.casino_id = $2
@@ -73,6 +73,7 @@ type FinalizeMetadataData = {
73
73
  export type OutcomeBetConfig = {
74
74
  houseEdge: number;
75
75
  saveOutcomes: boolean;
76
+ allowLossBeyondWager: boolean;
76
77
  initializeMetadataFromUntrustedUserInput?: (input: Input) => Result<Metadata, string>;
77
78
  finalizeMetadata?: (validatedMetadata: Metadata, data: FinalizeMetadataData) => Metadata;
78
79
  };
@@ -53,6 +53,7 @@ const BetConfigsSchema = z.record(BetKindSchema, z.object({
53
53
  .gte(0, "House edge must be >= 0")
54
54
  .lte(1, "House edge must be <= 1"),
55
55
  saveOutcomes: z.boolean(),
56
+ allowLossBeyondWager: z.boolean().optional().default(false),
56
57
  initializeMetadataFromUntrustedUserInput: z
57
58
  .function()
58
59
  .optional(),
@@ -119,6 +120,12 @@ export function MakeOutcomeBetPlugin({ betConfigs }) {
119
120
  if (!betConfig) {
120
121
  throw new GraphQLError(`Invalid bet kind`);
121
122
  }
123
+ if (!betConfig.allowLossBeyondWager) {
124
+ const minProfit = Math.min(...input.outcomes.map((o) => o.profit));
125
+ if (minProfit < -1) {
126
+ throw new GraphQLError(`Loss beyond wager not allowed: outcome profit must be >= -1`);
127
+ }
128
+ }
122
129
  let initializedMetadata;
123
130
  if (betConfig.initializeMetadataFromUntrustedUserInput) {
124
131
  const result = betConfig.initializeMetadataFromUntrustedUserInput(input);
@@ -167,7 +174,7 @@ export function MakeOutcomeBetPlugin({ betConfigs }) {
167
174
  maxPlayerLoss,
168
175
  dbPlayerBalance,
169
176
  });
170
- if (dbPlayerBalance < maxPlayerLoss) {
177
+ if (dbPlayerBalance.amount < maxPlayerLoss) {
171
178
  if (minProfit === -1) {
172
179
  throw new GraphQLError(`You cannot afford wager`);
173
180
  }
@@ -175,7 +182,7 @@ export function MakeOutcomeBetPlugin({ betConfigs }) {
175
182
  }
176
183
  const maxProfitMultiplier = Math.max(...input.outcomes.map((o) => o.profit));
177
184
  const maxPotentialPayout = input.wager * maxProfitMultiplier;
178
- const maxAllowablePayout = dbHouseBankroll * 0.01;
185
+ const maxAllowablePayout = dbHouseBankroll.amount * 0.01;
179
186
  if (maxPotentialPayout > maxAllowablePayout) {
180
187
  throw new GraphQLError(`House risk limit exceeded. Max payout: ${maxPotentialPayout.toFixed(4)}`);
181
188
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moneypot/hub",
3
- "version": "1.4.2",
3
+ "version": "1.4.4",
4
4
  "author": "moneypot.com",
5
5
  "homepage": "https://moneypot.com/hub",
6
6
  "keywords": [