@moneypot/hub 1.22.1 → 1.22.3
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.
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { exactlyOneRow } from "../../db/util.js";
|
|
2
2
|
export async function dbCreateLiability(pgClient, { lockedBankrollId, casinoId, experienceId, userId, currencyKey, refId, refType, maxPayout, deadline, }) {
|
|
3
|
+
await pgClient.query({
|
|
4
|
+
text: `SELECT 1 FROM hub.bankroll WHERE id = $1 FOR UPDATE`,
|
|
5
|
+
values: [lockedBankrollId],
|
|
6
|
+
});
|
|
3
7
|
const row = await pgClient
|
|
4
8
|
.query({
|
|
5
9
|
text: `
|
|
@@ -8,9 +12,16 @@ export async function dbCreateLiability(pgClient, { lockedBankrollId, casinoId,
|
|
|
8
12
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
|
9
13
|
RETURNING *
|
|
10
14
|
),
|
|
15
|
+
-- Sub-statements share one snapshot, so the sum can't see the
|
|
16
|
+
-- inserted row; its payout is added separately.
|
|
11
17
|
bankroll_update AS (
|
|
12
18
|
UPDATE hub.bankroll
|
|
13
|
-
SET liability =
|
|
19
|
+
SET liability = $8 + (
|
|
20
|
+
SELECT COALESCE(SUM(max_payout), 0)
|
|
21
|
+
FROM hub.liability
|
|
22
|
+
WHERE bankroll_id = $1
|
|
23
|
+
AND resolved_at IS NULL
|
|
24
|
+
)
|
|
14
25
|
WHERE id = $1
|
|
15
26
|
)
|
|
16
27
|
SELECT * FROM inserted
|
|
@@ -32,6 +43,17 @@ export async function dbCreateLiability(pgClient, { lockedBankrollId, casinoId,
|
|
|
32
43
|
}
|
|
33
44
|
export async function dbResolveLiabilityByRef(pgClient, { refType, refId, lockedBankrollId, }) {
|
|
34
45
|
const _proofOfGoodBehavior = lockedBankrollId;
|
|
46
|
+
await pgClient.query({
|
|
47
|
+
text: `
|
|
48
|
+
SELECT 1 FROM hub.bankroll
|
|
49
|
+
WHERE id = (
|
|
50
|
+
SELECT bankroll_id FROM hub.liability
|
|
51
|
+
WHERE ref_type = $1 AND ref_id = $2
|
|
52
|
+
)
|
|
53
|
+
FOR UPDATE
|
|
54
|
+
`,
|
|
55
|
+
values: [refType, refId],
|
|
56
|
+
});
|
|
35
57
|
const result = await pgClient.query({
|
|
36
58
|
text: `
|
|
37
59
|
WITH resolved AS (
|
|
@@ -40,10 +62,18 @@ export async function dbResolveLiabilityByRef(pgClient, { refType, refId, locked
|
|
|
40
62
|
WHERE ref_type = $1
|
|
41
63
|
AND ref_id = $2
|
|
42
64
|
AND resolved_at IS NULL
|
|
43
|
-
RETURNING
|
|
65
|
+
RETURNING id, bankroll_id
|
|
44
66
|
)
|
|
67
|
+
-- The sum reads the statement's snapshot, where the resolved row is
|
|
68
|
+
-- still active, so it is excluded by id.
|
|
45
69
|
UPDATE hub.bankroll
|
|
46
|
-
SET liability =
|
|
70
|
+
SET liability = (
|
|
71
|
+
SELECT COALESCE(SUM(l.max_payout), 0)
|
|
72
|
+
FROM hub.liability l
|
|
73
|
+
WHERE l.bankroll_id = resolved.bankroll_id
|
|
74
|
+
AND l.resolved_at IS NULL
|
|
75
|
+
AND l.id <> resolved.id
|
|
76
|
+
)
|
|
47
77
|
FROM resolved
|
|
48
78
|
WHERE hub.bankroll.id = resolved.bankroll_id
|
|
49
79
|
`,
|
|
@@ -178,13 +178,18 @@ export async function processTakeRequests({ abortSignal, controllerId, casinoId,
|
|
|
178
178
|
if (abortSignal.aborted) {
|
|
179
179
|
break;
|
|
180
180
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
181
|
+
try {
|
|
182
|
+
await processSingleTakeRequest({
|
|
183
|
+
mpTakeRequestId: takeRequest.id,
|
|
184
|
+
mpTakeRequest: takeRequest,
|
|
185
|
+
casinoId,
|
|
186
|
+
graphqlClient,
|
|
187
|
+
pool,
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
catch (error) {
|
|
191
|
+
logger.error(error, `[processTakeRequests] Failed to process take request ${takeRequest.id}`);
|
|
192
|
+
}
|
|
188
193
|
}
|
|
189
194
|
await processPendingTransferCompletions({
|
|
190
195
|
casinoId,
|
|
@@ -252,7 +257,11 @@ async function createAndProcessNewTakeRequest({ pgClient, mpTakeRequest, casinoI
|
|
|
252
257
|
currencyKey: mpTakeRequest.currencyKey,
|
|
253
258
|
casinoId,
|
|
254
259
|
});
|
|
255
|
-
|
|
260
|
+
if (!dbUser || !dbExperience || !dbCurrency || !dbBalance) {
|
|
261
|
+
logger.warn(`[createAndProcessNewTakeRequest] Rejecting take request ${mpTakeRequest.id}: no hub balance`);
|
|
262
|
+
await rejectMpTakeRequest(pgClient, graphqlClient, mpTakeRequest.id);
|
|
263
|
+
return null;
|
|
264
|
+
}
|
|
256
265
|
const amountToTransfer = Math.floor(typeof mpTakeRequest.amount === "number"
|
|
257
266
|
? Math.min(mpTakeRequest.amount, dbBalance.amount)
|
|
258
267
|
: dbBalance.amount);
|