@moneypot/hub 1.3.0-dev.12 → 1.3.0-dev.14
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.
|
@@ -6,6 +6,7 @@ import { DbHashKind, exactlyOneRow, maybeOneRow, superuserPool, withPgPoolTransa
|
|
|
6
6
|
import { assert } from "tsafe";
|
|
7
7
|
import { dbInsertHubHash, dbLockHubHashChain, } from "../hash-chain/db-hash-chain.js";
|
|
8
8
|
import { getIntermediateHash, getPreimageHash, } from "../hash-chain/get-hash.js";
|
|
9
|
+
import { createHmac } from "node:crypto";
|
|
9
10
|
const FLOAT_EPSILON = 1e-10;
|
|
10
11
|
function sum(ns) {
|
|
11
12
|
return ns.reduce((a, b) => a + b, 0);
|
|
@@ -78,11 +79,11 @@ export function MakeOutcomeBetPlugin({ betConfigs }) {
|
|
|
78
79
|
metadata: JSON
|
|
79
80
|
}
|
|
80
81
|
|
|
81
|
-
type
|
|
82
|
+
type HubMakeOutcomeBetSuccess {
|
|
82
83
|
bet: HubOutcomeBet!
|
|
83
84
|
}
|
|
84
85
|
|
|
85
|
-
union HubMakeOutcomeBetResult =
|
|
86
|
+
union HubMakeOutcomeBetResult = HubMakeOutcomeBetSuccess | HubBadHashChainError
|
|
86
87
|
|
|
87
88
|
type HubMakeOutcomeBetPayload {
|
|
88
89
|
result: HubMakeOutcomeBetResult!
|
|
@@ -239,7 +240,18 @@ export function MakeOutcomeBetPlugin({ betConfigs }) {
|
|
|
239
240
|
if (result.rowCount !== 1) {
|
|
240
241
|
throw new GraphQLError("Failed to update hash chain iteration");
|
|
241
242
|
}
|
|
242
|
-
const
|
|
243
|
+
const finalHash = (() => {
|
|
244
|
+
const serverHash = betHashResult.hash;
|
|
245
|
+
const clientSeed = dbHashChain.client_seed;
|
|
246
|
+
const finalHash = createHmac("sha256", serverHash)
|
|
247
|
+
.update(clientSeed)
|
|
248
|
+
.digest();
|
|
249
|
+
return finalHash;
|
|
250
|
+
})();
|
|
251
|
+
const { outcome, outcomeIdx } = pickRandomOutcome({
|
|
252
|
+
outcomes: input.outcomes,
|
|
253
|
+
hash: finalHash,
|
|
254
|
+
});
|
|
243
255
|
const netPlayerAmount = input.wager * outcome.profit;
|
|
244
256
|
await pgClient.query({
|
|
245
257
|
text: `
|
|
@@ -340,7 +352,7 @@ export function MakeOutcomeBetPlugin({ betConfigs }) {
|
|
|
340
352
|
})
|
|
341
353
|
.then(exactlyOneRow);
|
|
342
354
|
return {
|
|
343
|
-
__typename: "
|
|
355
|
+
__typename: "HubMakeOutcomeBetSuccess",
|
|
344
356
|
betId: bet.id,
|
|
345
357
|
};
|
|
346
358
|
});
|
|
@@ -350,7 +362,7 @@ export function MakeOutcomeBetPlugin({ betConfigs }) {
|
|
|
350
362
|
});
|
|
351
363
|
},
|
|
352
364
|
},
|
|
353
|
-
|
|
365
|
+
HubMakeOutcomeBetSuccess: {
|
|
354
366
|
__assertStep: ObjectStep,
|
|
355
367
|
bet($data) {
|
|
356
368
|
const $betId = access($data, "betId");
|
|
@@ -362,7 +374,7 @@ export function MakeOutcomeBetPlugin({ betConfigs }) {
|
|
|
362
374
|
result($data) {
|
|
363
375
|
const $result = $data.get("result");
|
|
364
376
|
return polymorphicBranch($result, {
|
|
365
|
-
|
|
377
|
+
HubMakeOutcomeBetSuccess: {},
|
|
366
378
|
HubBadHashChainError: {},
|
|
367
379
|
});
|
|
368
380
|
},
|
|
@@ -377,7 +389,7 @@ function normalizeHash(hash) {
|
|
|
377
389
|
const uint32Value = view.getUint32(0, false);
|
|
378
390
|
return uint32Value / Math.pow(2, 32);
|
|
379
391
|
}
|
|
380
|
-
function pickRandomOutcome(outcomes, hash) {
|
|
392
|
+
function pickRandomOutcome({ outcomes, hash, }) {
|
|
381
393
|
assert(outcomes.length >= 2, "Outcome count must be >= 2");
|
|
382
394
|
const totalWeight = sum(outcomes.map((o) => o.weight));
|
|
383
395
|
const outcomesWithProbability = outcomes.map((o) => ({
|