@voltro/plugin-economy 0.0.0-stage → 0.87.0
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/CHANGELOG.md +24329 -0
- package/LICENSE +61 -0
- package/README.md +2719 -2
- package/SECURITY.md +56 -0
- package/THIRD-PARTY-NOTICES.md +9189 -0
- package/dist/amount.js +21 -0
- package/dist/appleIap.server.js +263 -0
- package/dist/appleIapNotifications.server.js +163 -0
- package/dist/appleIapTrust.server.js +31 -0
- package/dist/bundleValue.server.js +113 -0
- package/dist/canonicalDataJson.js +11 -0
- package/dist/capture.js +54 -0
- package/dist/codePort.fixture.js +124 -0
- package/dist/codeStore.server.js +440 -0
- package/dist/codeTables.entity.js +44 -0
- package/dist/codes.d.ts +150 -0
- package/dist/codes.js +83 -0
- package/dist/components.d.ts +397 -0
- package/dist/components.js +284 -0
- package/dist/consistency.server.js +116 -0
- package/dist/containerStore.server.js +688 -0
- package/dist/containerStore.testFixtures.js +76 -0
- package/dist/containerTables.entity.js +46 -0
- package/dist/containers.d.ts +311 -0
- package/dist/containers.fixture.js +129 -0
- package/dist/containers.js +215 -0
- package/dist/containers.server.js +40 -0
- package/dist/containersPort.fixture.js +75 -0
- package/dist/currency.js +33 -0
- package/dist/declarationFixtures.js +62 -0
- package/dist/economy-app/events/economy.refundReversed.event.ts +1 -0
- package/dist/economy-app/events/economy.refunded.event.ts +1 -0
- package/dist/economyPort.fixture.js +125 -0
- package/dist/errors.d.ts +14 -0
- package/dist/errors.js +22 -0
- package/dist/events.d.ts +46 -0
- package/dist/events.js +1 -0
- package/dist/googleIap.server.js +392 -0
- package/dist/googleIapNotifications.server.js +131 -0
- package/dist/googleIapSubscription.server.js +184 -0
- package/dist/iap.d.ts +309 -0
- package/dist/iap.js +304 -0
- package/dist/iap.testFixtures.js +145 -0
- package/dist/iapAccountTables.entity.js +40 -0
- package/dist/iapAccounts.server.js +201 -0
- package/dist/iapBinding.server.js +524 -0
- package/dist/iapCheckout.server.js +98 -0
- package/dist/iapCompletion.server.js +56 -0
- package/dist/iapCurrentState.server.js +108 -0
- package/dist/iapCurrentState.testFixtures.js +23 -0
- package/dist/iapCurrentStatePort.fixture.js +64 -0
- package/dist/iapEntitlements.d.ts +79 -0
- package/dist/iapEntitlements.js +58 -0
- package/dist/iapEntitlements.server.js +49 -0
- package/dist/iapEntitlementsBinding.server.js +106 -0
- package/dist/iapEntitlementsPort.fixture.js +81 -0
- package/dist/iapInstalledEntitlements.testFixtures.js +76 -0
- package/dist/iapInstalledNotifications.testFixtures.js +94 -0
- package/dist/iapInventoryRefunds.server.js +262 -0
- package/dist/iapNotificationCustodyPort.fixture.js +68 -0
- package/dist/iapNotificationPort.fixture.js +74 -0
- package/dist/iapNotificationProcessor.server.js +135 -0
- package/dist/iapNotificationProcessor.testFixtures.js +64 -0
- package/dist/iapNotificationProcessorPort.fixture.js +56 -0
- package/dist/iapNotificationSchedule.server.js +70 -0
- package/dist/iapNotificationStore.server.js +353 -0
- package/dist/iapNotificationStore.testFixtures.js +32 -0
- package/dist/iapNotificationTables.entity.js +49 -0
- package/dist/iapNotificationTarget.server.js +37 -0
- package/dist/iapNotificationTarget.testFixtures.js +30 -0
- package/dist/iapNotifications.d.ts +644 -0
- package/dist/iapNotifications.js +6 -0
- package/dist/iapNotifications.server.js +96 -0
- package/dist/iapNotificationsBinding.server.js +233 -0
- package/dist/iapNotificationsSystemPort.fixture.js +51 -0
- package/dist/iapPlugin.testFixtures.js +60 -0
- package/dist/iapPort.fixture.js +75 -0
- package/dist/iapProviderCapture.server.js +18 -0
- package/dist/iapRecovery.d.ts +146 -0
- package/dist/iapRecovery.js +90 -0
- package/dist/iapRefundEventDelivery.server.js +83 -0
- package/dist/iapRefundEvents.js +45 -0
- package/dist/iapRefundEventsPort.fixture.js +60 -0
- package/dist/iapRefundPort.fixture.js +55 -0
- package/dist/iapRefundTables.entity.js +56 -0
- package/dist/iapRefunds.d.ts +755 -0
- package/dist/iapRefunds.js +6 -0
- package/dist/iapRefunds.server.js +554 -0
- package/dist/iapRefunds.testFixtures.js +65 -0
- package/dist/iapSpendAdmission.server.js +99 -0
- package/dist/iapStore.server.js +741 -0
- package/dist/iapStore.testFixtures.js +128 -0
- package/dist/iapTables.entity.js +81 -0
- package/dist/iapVerification.server.js +121 -0
- package/dist/index.d.ts +2984 -0
- package/dist/index.js +15 -0
- package/dist/intentionRecovery.js +134 -0
- package/dist/ledger.js +78 -0
- package/dist/loyalty.d.ts +274 -0
- package/dist/loyalty.fixture.js +93 -0
- package/dist/loyalty.js +312 -0
- package/dist/loyaltyBinding.server.js +204 -0
- package/dist/loyaltyPort.fixture.js +87 -0
- package/dist/loyaltyShopSource.server.js +91 -0
- package/dist/loyaltyStore.server.js +553 -0
- package/dist/loyaltyStore.testFixtures.js +83 -0
- package/dist/loyaltyTables.entity.js +50 -0
- package/dist/offerMerchandising.fixture.js +115 -0
- package/dist/offerMerchandising.js +283 -0
- package/dist/offerPolicies.d.ts +441 -0
- package/dist/offerPolicies.fixture.js +54 -0
- package/dist/offerPolicies.js +268 -0
- package/dist/piggyBanks.d.ts +272 -0
- package/dist/piggyBanks.fixture.js +71 -0
- package/dist/piggyBanks.js +201 -0
- package/dist/piggyBinding.server.js +205 -0
- package/dist/piggyPort.fixture.js +73 -0
- package/dist/piggyStore.server.js +611 -0
- package/dist/piggyStore.testFixtures.js +98 -0
- package/dist/piggyTables.entity.js +41 -0
- package/dist/plugin.js +1048 -0
- package/dist/port.js +13 -0
- package/dist/price.d.ts +99 -0
- package/dist/price.js +144 -0
- package/dist/purchaseRecovery.js +56 -0
- package/dist/reports.server.js +115 -0
- package/dist/rpc.d.ts +3201 -0
- package/dist/rpc.js +1201 -0
- package/dist/sanctionsTradeAuthority.server.js +80 -0
- package/dist/server.d.ts +3856 -0
- package/dist/server.js +43 -0
- package/dist/shop.d.ts +279 -0
- package/dist/shop.js +196 -0
- package/dist/shop.testFixtures.js +114 -0
- package/dist/shopPort.fixture.js +72 -0
- package/dist/shopPresentation.js +68 -0
- package/dist/shopStore.server.js +624 -0
- package/dist/shopTables.entity.js +63 -0
- package/dist/shopUiCopy.js +57 -0
- package/dist/spendLimits.d.ts +140 -0
- package/dist/spendLimits.fixture.js +53 -0
- package/dist/spendLimits.js +155 -0
- package/dist/spendLimitsPort.fixture.js +120 -0
- package/dist/subscriptionBinding.server.js +211 -0
- package/dist/subscriptionStore.server.js +643 -0
- package/dist/subscriptionStore.testFixtures.js +72 -0
- package/dist/subscriptionTables.entity.js +37 -0
- package/dist/subscriptions.d.ts +394 -0
- package/dist/subscriptions.fixture.js +97 -0
- package/dist/subscriptions.js +213 -0
- package/dist/subscriptionsPort.fixture.js +76 -0
- package/dist/systemFixtures.js +89 -0
- package/dist/tables.entity.js +83 -0
- package/dist/testing.d.ts +4615 -0
- package/dist/testing.js +29 -0
- package/dist/tradeAuthority.server.js +172 -0
- package/dist/tradeBinding.server.js +201 -0
- package/dist/tradeComponents.js +342 -0
- package/dist/tradeEscrow.server.js +232 -0
- package/dist/tradeEscrow.testFixtures.js +133 -0
- package/dist/tradeExpiry.server.js +43 -0
- package/dist/tradeRecovery.js +48 -0
- package/dist/tradeStore.server.js +750 -0
- package/dist/tradeStore.testFixtures.js +73 -0
- package/dist/tradeTables.entity.js +66 -0
- package/dist/tradeUiCopy.js +79 -0
- package/dist/trades.d.ts +246 -0
- package/dist/trades.fixture.js +103 -0
- package/dist/trades.js +289 -0
- package/dist/tradesPort.fixture.js +94 -0
- package/dist/walletEscrow.js +106 -0
- package/dist/walletEscrow.server.js +376 -0
- package/dist/walletKeys.server.js +5 -0
- package/dist/walletStore.js +571 -0
- package/dist/web.d.ts +2257 -0
- package/dist/web.js +9 -0
- package/package.json +161 -4
|
@@ -0,0 +1,750 @@
|
|
|
1
|
+
import { EconomyError as e, economyError as t } from "./errors.js";
|
|
2
|
+
import { captureBoundMethods as n, dataList as r, dataObject as i, identifier as a } from "./capture.js";
|
|
3
|
+
import { captureCurrencies as o } from "./currency.js";
|
|
4
|
+
import { shopInteger as s } from "./shop.js";
|
|
5
|
+
import { canonicalDataJson as c } from "./canonicalDataJson.js";
|
|
6
|
+
import { captureTradeParticipantAuthority as l, captureTradeProposeIntention as u, captureTradeReceipt as d, defaultTradeEligibilityPolicy as f, defaultTradeTaxPolicy as p, defineTradeRules as m, resolveTradeEligibility as h, resolveTradeTax as g, tradeAcceptIntent as _, tradeCancelIntent as v, tradeProposeIntent as ee } from "./trades.js";
|
|
7
|
+
import { captureTradeExpiryInput as te } from "./tradeExpiry.server.js";
|
|
8
|
+
import { tradeEscrowParticipant as ne } from "./tradeEscrow.server.js";
|
|
9
|
+
import { tradeOperationsTable as y, tradeSubjectsTable as b, tradeTenantsTable as x, tradesTable as S } from "./tradeTables.entity.js";
|
|
10
|
+
import { Effect as C, Result as w } from "effect";
|
|
11
|
+
import { systemSubject as re } from "@voltro/protocol";
|
|
12
|
+
import { createHash as ie } from "node:crypto";
|
|
13
|
+
import { and as T, eq as E, queryFor as D } from "@voltro/database";
|
|
14
|
+
import { executeSqlMutations as ae, prepareSqlMutation as oe, prepareSqlMutationGroup as O } from "@voltro/database/sql";
|
|
15
|
+
import { InventoryError as se } from "@voltro/plugin-inventory/errors";
|
|
16
|
+
import { SanctionsError as k } from "@voltro/plugin-sanctions/errors";
|
|
17
|
+
//#region src/tradeStore.server.ts
|
|
18
|
+
var ce = (e) => {
|
|
19
|
+
let c = i(e, [
|
|
20
|
+
"currencies",
|
|
21
|
+
"rules",
|
|
22
|
+
"taxPolicy",
|
|
23
|
+
"economy",
|
|
24
|
+
"inventory",
|
|
25
|
+
"allowedServices",
|
|
26
|
+
"authority",
|
|
27
|
+
"eligibility",
|
|
28
|
+
"maxHistoryRows",
|
|
29
|
+
"now"
|
|
30
|
+
]), l = c.now ?? Date.now, u = c.eligibility ?? f, d = c.taxPolicy ?? p;
|
|
31
|
+
if (typeof l != "function" || typeof u != "function" || typeof d != "function") throw t("invalid_input");
|
|
32
|
+
let h = Object.freeze(r(c.allowedServices ?? [], 256).map((e) => a(e)));
|
|
33
|
+
if (new Set(h).size !== h.length) throw t("invalid_input");
|
|
34
|
+
return Object.freeze({
|
|
35
|
+
currencies: o(c.currencies),
|
|
36
|
+
rules: m(c.rules),
|
|
37
|
+
authority: n(c.authority, ["read", "prepare"]),
|
|
38
|
+
economy: n(c.economy, [
|
|
39
|
+
"prepareEscrowReserve",
|
|
40
|
+
"prepareEscrowRelease",
|
|
41
|
+
"prepareEscrowTaxSettle",
|
|
42
|
+
"prepareEscrowReserveTaxSettle",
|
|
43
|
+
"escrow"
|
|
44
|
+
]),
|
|
45
|
+
inventory: c.inventory === void 0 ? void 0 : n(c.inventory, [
|
|
46
|
+
"prepareReserve",
|
|
47
|
+
"prepareRelease",
|
|
48
|
+
"prepareReservationTransfer",
|
|
49
|
+
"prepareReserveTransfer",
|
|
50
|
+
"reservation",
|
|
51
|
+
"reservationEvidence"
|
|
52
|
+
]),
|
|
53
|
+
now: l,
|
|
54
|
+
eligibility: u,
|
|
55
|
+
taxPolicy: d,
|
|
56
|
+
allowedServices: h,
|
|
57
|
+
maxHistoryRows: s(c.maxHistoryRows ?? 1e4, 1, 1e5)
|
|
58
|
+
});
|
|
59
|
+
}, A = (e) => ie("sha256").update(c(e)).digest("hex"), j = (e, ...t) => e + A(t).slice(0, 56), M = (e) => s(e, 0, 864e13), N = (e) => M(e instanceof Date ? e.getTime() : typeof e == "string" ? Date.parse(e) : NaN), P = (e, t) => c(e) === c(t), F = (n) => n instanceof k ? t(n.code === "forbidden" || n.code === "identity_required" ? "forbidden" : n.code === "conflict" ? "conflict" : "unavailable") : n instanceof e ? n : n instanceof se && [
|
|
60
|
+
"invalid_input",
|
|
61
|
+
"invalid_record",
|
|
62
|
+
"not_found",
|
|
63
|
+
"erased",
|
|
64
|
+
"forbidden",
|
|
65
|
+
"conflict",
|
|
66
|
+
"limit_exceeded"
|
|
67
|
+
].includes(n.code) ? t(n.code) : t("unavailable"), I = async (e) => {
|
|
68
|
+
let t = await C.runPromise(C.result(e));
|
|
69
|
+
if (w.isFailure(t)) throw t.failure;
|
|
70
|
+
return t.success;
|
|
71
|
+
}, L = (e) => C.tryPromise({
|
|
72
|
+
try: e,
|
|
73
|
+
catch: F
|
|
74
|
+
}), R = (e) => {
|
|
75
|
+
try {
|
|
76
|
+
return e();
|
|
77
|
+
} catch (e) {
|
|
78
|
+
return C.fail(F(e));
|
|
79
|
+
}
|
|
80
|
+
}, z = class extends Error {
|
|
81
|
+
constructor() {
|
|
82
|
+
super("Original Trade operation won admission"), this.name = "TradeReplay";
|
|
83
|
+
}
|
|
84
|
+
}, B = (e, c) => {
|
|
85
|
+
let f = ce(c), p = f.currencies, C = f.rules, w = f.authority, ie = f.now, se = f.eligibility, k = f.maxHistoryRows, F = new Set(f.allowedServices), B = re("economy.trades"), V = {
|
|
86
|
+
currencies: p,
|
|
87
|
+
rules: C,
|
|
88
|
+
economy: f.economy,
|
|
89
|
+
inventory: f.inventory,
|
|
90
|
+
taxPolicy: f.taxPolicy,
|
|
91
|
+
allowedServices: [...F]
|
|
92
|
+
}, le = ne(e, V), ue = n(V.economy, ["escrow"]), de = f.inventory === void 0 ? void 0 : n(f.inventory, ["reservationEvidence"]), H = () => M(ie()), U = (e, t) => j("ets_", e, t), W = (e) => j("ett_", e), G = (e, t, n) => j("eto_", e, t, n), fe = (e, t, n) => j("etd_", e, t, n), K = (e) => {
|
|
93
|
+
let n = structuredClone(e);
|
|
94
|
+
if (n.type !== "user") throw t("forbidden");
|
|
95
|
+
return Object.freeze({
|
|
96
|
+
tenant: a(n.tenantId),
|
|
97
|
+
owner: a(n.id),
|
|
98
|
+
subject: n
|
|
99
|
+
});
|
|
100
|
+
}, q = (e) => {
|
|
101
|
+
let n = structuredClone(e);
|
|
102
|
+
if (n.type !== "system" && (n.type !== "service" || !F.has(n.service))) throw t("forbidden");
|
|
103
|
+
}, pe = async (t, n) => {
|
|
104
|
+
await e.insertIgnore(x.tableName, {
|
|
105
|
+
id: W(t),
|
|
106
|
+
tenantId: t,
|
|
107
|
+
admission: 0,
|
|
108
|
+
tradesCount: 0
|
|
109
|
+
}, { conflictColumns: ["id"] }), await e.insertIgnore(b.tableName, {
|
|
110
|
+
id: U(t, n),
|
|
111
|
+
tenantId: t,
|
|
112
|
+
ownerId: n,
|
|
113
|
+
erased: !1,
|
|
114
|
+
admission: 0,
|
|
115
|
+
operationsCount: 0
|
|
116
|
+
}, { conflictColumns: ["id"] });
|
|
117
|
+
}, me = async (e, n, r, i = !1) => {
|
|
118
|
+
let a = (await e.query(D(b).where(E("id", U(n, r))).limit(1).descriptor))[0];
|
|
119
|
+
if (a) {
|
|
120
|
+
if (a.tenantId !== n || a.admission !== 0 || typeof a.erased != "boolean" || a.ownerId !== (a.erased ? null : r)) throw t("invalid_record");
|
|
121
|
+
if (s(a.operationsCount, 0, k), a.erased && !i) throw t("erased");
|
|
122
|
+
return a;
|
|
123
|
+
}
|
|
124
|
+
}, J = async (e, n, r, i = !1) => {
|
|
125
|
+
if (await e.updateMany(x.tableName, { admission: 0 }, { where: E("id", W(n)) }) !== 1) throw t("unavailable");
|
|
126
|
+
for (let a of [...new Set(r)].sort()) if (await e.updateMany(b.tableName, { admission: 0 }, { where: E("id", U(n, a)) }) !== 1 || !await me(e, n, a, i)) throw t("unavailable");
|
|
127
|
+
}, Y = async (e, n, r, i = !1) => {
|
|
128
|
+
let o = await me(e, n, r, i), s = await e.query(D(y).where(T(E("tenantId", n), E("ownerKey", U(n, r)))).limit(k + 1).descriptor);
|
|
129
|
+
if (s.length > k) throw t("limit_exceeded");
|
|
130
|
+
if (s.length > 0 && !o) throw t("invalid_record");
|
|
131
|
+
for (let e of s) {
|
|
132
|
+
if (e.tenantId !== n || e.ownerKey !== U(n, r) || e.ownerId !== (o?.erased ? null : r) || e.admission !== 0 || typeof e.id != "string" || !/^eto_[a-f0-9]{56}$/.test(e.id) || ![
|
|
133
|
+
"propose",
|
|
134
|
+
"accept",
|
|
135
|
+
"cancel",
|
|
136
|
+
"expire"
|
|
137
|
+
].includes(String(e.kind)) || ![
|
|
138
|
+
"pending",
|
|
139
|
+
"fulfilled",
|
|
140
|
+
"erased"
|
|
141
|
+
].includes(String(e.state)) || (a(e.tradeId), e.sequence === null && (e.state !== "pending" && e.state !== "erased" || e.receipt !== null || e.request !== null || e.digest !== null || e.at !== null))) throw t("invalid_record");
|
|
142
|
+
if (e.state === "fulfilled") {
|
|
143
|
+
let i = e.kind === "propose" ? u(e.request) : e.kind === "accept" ? _(e.request) : v(e.request), a = d(e.receipt);
|
|
144
|
+
if (!P(a, e.receipt) || !P(i, e.request) || e.id !== G(n, r, i.requestId) || e.digest !== A([e.kind, i]) || a.operationId !== e.id || a.requestId !== i.requestId || a.trade.tradeId !== e.tradeId || e.kind === "propose" && (a.trade.state !== "proposed" || a.trade.proposerId !== r || e.tradeId !== fe(n, r, i.requestId)) || e.kind === "accept" && (a.trade.state !== "accepted" || a.trade.accepterId !== r) || e.kind === "cancel" && (a.trade.state !== "cancelled" || a.trade.proposerId !== r) || e.kind === "expire" && (a.trade.state !== "expired" || a.trade.proposerId !== r)) throw t("invalid_record");
|
|
145
|
+
M(N(e.at));
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
let c = s.filter((e) => e.sequence !== null).sort((e, t) => Number(e.sequence) - Number(t.sequence));
|
|
149
|
+
if (c.length !== (o?.operationsCount ?? 0)) throw t("invalid_record");
|
|
150
|
+
for (let [e, n] of c.entries()) if (n.sequence !== e + 1 || n.state !== "fulfilled" && n.state !== "erased" || n.state === "erased" && (n.receipt !== null || n.request !== null || n.digest !== null || n.at !== null)) throw t("invalid_record");
|
|
151
|
+
return {
|
|
152
|
+
parent: o,
|
|
153
|
+
rows: s
|
|
154
|
+
};
|
|
155
|
+
}, X = async (e, n) => {
|
|
156
|
+
let r = (await e.query(D(x).where(E("id", W(n))).limit(1).descriptor))[0], i = await e.query(D(S).where(E("tenantId", n)).limit(k + 1).descriptor);
|
|
157
|
+
if (i.length > k) throw t("limit_exceeded");
|
|
158
|
+
if (i.length > 0 && !r || r && (r.tenantId !== n || r.admission !== 0) || i.some((e) => typeof e.accepted != "boolean") || i.filter((e) => e.accepted === !0).length !== (r ? s(r.tradesCount, 0, k) : 0)) throw t("invalid_record");
|
|
159
|
+
for (let e of i) if (e.accepted === !1 && (e.state !== "pending" && e.state !== "erased" || e.proposal !== null || e.digest !== null || e.closing !== null || e.createdAt !== null || e.expiresAt !== null)) throw t("invalid_record");
|
|
160
|
+
return {
|
|
161
|
+
parent: r,
|
|
162
|
+
rows: i
|
|
163
|
+
};
|
|
164
|
+
}, he = (e) => A({
|
|
165
|
+
tenantId: e.tenantId,
|
|
166
|
+
proposerId: e.proposerId,
|
|
167
|
+
intent: e.intent,
|
|
168
|
+
currencies: e.currencies,
|
|
169
|
+
rules: e.rules,
|
|
170
|
+
createdAt: e.createdAt,
|
|
171
|
+
expiresAt: e.expiresAt,
|
|
172
|
+
taxes: e.taxes
|
|
173
|
+
}), Z = (e, t, n) => Object.freeze({
|
|
174
|
+
tradeId: e,
|
|
175
|
+
tradeDigest: he(t),
|
|
176
|
+
proposerId: t.proposerId,
|
|
177
|
+
toId: t.intent.toId,
|
|
178
|
+
offered: t.intent.offered,
|
|
179
|
+
requested: t.intent.requested,
|
|
180
|
+
createdAt: t.createdAt,
|
|
181
|
+
expiresAt: t.expiresAt,
|
|
182
|
+
accepterId: n?.kind === "accept" ? n.ownerId : null,
|
|
183
|
+
rulesVersion: t.rules.version,
|
|
184
|
+
taxes: Object.freeze(t.taxes.map((e, n) => Object.freeze({
|
|
185
|
+
leg: n < t.intent.offered.currencies.length ? "offered" : "requested",
|
|
186
|
+
...e
|
|
187
|
+
}))),
|
|
188
|
+
state: n?.kind === "accept" ? "accepted" : n?.kind === "cancel" ? "cancelled" : n ? "expired" : "proposed"
|
|
189
|
+
}), ge = (e) => {
|
|
190
|
+
let n = i(e.proposal, [
|
|
191
|
+
"tenantId",
|
|
192
|
+
"proposerId",
|
|
193
|
+
"intent",
|
|
194
|
+
"currencies",
|
|
195
|
+
"rules",
|
|
196
|
+
"createdAt",
|
|
197
|
+
"expiresAt",
|
|
198
|
+
"taxes",
|
|
199
|
+
"held"
|
|
200
|
+
], "invalid_record"), s = o(n.currencies), c = m(n.rules), l = ee(n.intent, s, c), u = a(n.tenantId), d = a(n.proposerId), f = M(n.createdAt), p = M(n.expiresAt), h = r(n.taxes, 64).map((e) => {
|
|
201
|
+
let n = i(e, [
|
|
202
|
+
"currency",
|
|
203
|
+
"gross",
|
|
204
|
+
"net",
|
|
205
|
+
"tax"
|
|
206
|
+
], "invalid_record"), r = g({
|
|
207
|
+
currency: a(n.currency),
|
|
208
|
+
amount: n.gross,
|
|
209
|
+
rules: c
|
|
210
|
+
}, () => n.tax);
|
|
211
|
+
if (!P(r, n)) throw t("invalid_record");
|
|
212
|
+
return r;
|
|
213
|
+
}), _ = [...l.offered.currencies, ...l.requested.currencies].map((e) => [e.currency, e.amount]);
|
|
214
|
+
if (h.length !== _.length || h.some((e, t) => e.currency !== _[t][0] || e.gross !== _[t][1]) || p !== f + l.durationMs || l.toId === d || e.tenantId !== u || e.id !== fe(u, d, l.requestId) || e.proposerKey !== U(u, d) || e.targetKey !== (l.toId === null ? null : U(u, l.toId)) || N(e.createdAt) !== f || N(e.expiresAt) !== p) throw t("invalid_record");
|
|
215
|
+
let v = Object.freeze({
|
|
216
|
+
tenantId: u,
|
|
217
|
+
proposerId: d,
|
|
218
|
+
intent: l,
|
|
219
|
+
currencies: s,
|
|
220
|
+
rules: c,
|
|
221
|
+
createdAt: f,
|
|
222
|
+
expiresAt: p,
|
|
223
|
+
taxes: Object.freeze(h),
|
|
224
|
+
held: n.held
|
|
225
|
+
});
|
|
226
|
+
if (e.digest !== he(v)) throw t("invalid_record");
|
|
227
|
+
return v;
|
|
228
|
+
}, Q = async (e, n, r, o = !1, s) => {
|
|
229
|
+
let c = (s ?? await X(e, n)).rows.find((e) => e.id === r);
|
|
230
|
+
if (!c || c.accepted === !1 && c.state === "pending") throw t("not_found");
|
|
231
|
+
if (c.state === "erased") throw c.proposal !== null || c.closing !== null || c.digest !== null ? t("invalid_record") : t("erased");
|
|
232
|
+
if (c.accepted !== !0) throw t("invalid_record");
|
|
233
|
+
let l = ge(c), u = G(n, l.proposerId, l.intent.requestId), d = null;
|
|
234
|
+
if (c.closing !== null) {
|
|
235
|
+
let e = i(c.closing, [
|
|
236
|
+
"ownerId",
|
|
237
|
+
"kind",
|
|
238
|
+
"request",
|
|
239
|
+
"at",
|
|
240
|
+
"value"
|
|
241
|
+
], "invalid_record"), n = a(e.ownerId), o = M(e.at);
|
|
242
|
+
if (e.kind !== "accept" && e.kind !== "cancel" && e.kind !== "expire" || o < l.createdAt) throw t("invalid_record");
|
|
243
|
+
let s = e.kind === "accept" ? _(e.request) : v(e.request);
|
|
244
|
+
if (s.tradeId !== r || e.kind === "accept" && (s.tradeDigest !== c.digest || n === l.proposerId || l.intent.toId !== null && l.intent.toId !== n || o >= l.expiresAt) || e.kind !== "accept" && n !== l.proposerId || e.kind === "expire" && o < l.expiresAt) throw t("invalid_record");
|
|
245
|
+
d = Object.freeze({
|
|
246
|
+
ownerId: n,
|
|
247
|
+
kind: e.kind,
|
|
248
|
+
request: s,
|
|
249
|
+
at: o,
|
|
250
|
+
value: e.value
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
let f = Z(r, l, d), p = d?.kind === "accept" ? U(n, d.ownerId) : null;
|
|
254
|
+
if (c.accepterKey !== p || !o && c.state !== f.state) throw t("invalid_record");
|
|
255
|
+
let m = await e.query(D(y).where(T(E("tenantId", n), E("tradeId", r))).limit(k + 1).descriptor);
|
|
256
|
+
if (m.length > k) throw t("limit_exceeded");
|
|
257
|
+
let h = m.filter((e) => e.sequence !== null);
|
|
258
|
+
if (h.length !== (d ? 2 : 1)) throw t("invalid_record");
|
|
259
|
+
let g = (e, r, a, o, s, c) => {
|
|
260
|
+
if (!e || e.state !== "fulfilled" || e.kind !== a || e.ownerId !== r || e.ownerKey !== U(n, r) || e.digest !== A([a, o]) || !P(e.request, o) || N(e.at) !== c || !P(e.receipt, {
|
|
261
|
+
operationId: e.id,
|
|
262
|
+
requestId: i(o, a === "propose" ? [
|
|
263
|
+
"toId",
|
|
264
|
+
"offered",
|
|
265
|
+
"requested",
|
|
266
|
+
"durationMs",
|
|
267
|
+
"requestId"
|
|
268
|
+
] : a === "accept" ? [
|
|
269
|
+
"tradeId",
|
|
270
|
+
"tradeDigest",
|
|
271
|
+
"requestId"
|
|
272
|
+
] : ["tradeId", "requestId"]).requestId,
|
|
273
|
+
trade: s
|
|
274
|
+
})) throw t("invalid_record");
|
|
275
|
+
};
|
|
276
|
+
return g(h.find((e) => e.id === u), l.proposerId, "propose", l.intent, Z(r, l, null), l.createdAt), d && g(h.find((e) => e.id === G(n, d.ownerId, d.request.requestId)), d.ownerId, d.kind, d.request, f, d.at), {
|
|
277
|
+
row: c,
|
|
278
|
+
proposal: l,
|
|
279
|
+
closing: d,
|
|
280
|
+
view: f
|
|
281
|
+
};
|
|
282
|
+
}, _e = (n) => ne(e, {
|
|
283
|
+
...V,
|
|
284
|
+
currencies: n.currencies,
|
|
285
|
+
rules: n.rules,
|
|
286
|
+
taxPolicy: (e) => {
|
|
287
|
+
let r = n.taxes.filter((t) => t.currency === e.currency && t.gross === String(e.amount));
|
|
288
|
+
if (!r.length || r.some((e) => e.tax !== r[0].tax)) throw t("invalid_record");
|
|
289
|
+
return r[0].tax;
|
|
290
|
+
}
|
|
291
|
+
}), ve = (e) => ({
|
|
292
|
+
tenantId: e.proposal.tenantId,
|
|
293
|
+
tradeId: e.view.tradeId,
|
|
294
|
+
proposerId: e.proposal.proposerId,
|
|
295
|
+
offered: e.proposal.intent.offered,
|
|
296
|
+
requested: e.proposal.intent.requested
|
|
297
|
+
}), $ = async (e) => {
|
|
298
|
+
let { proposal: n, closing: a } = e;
|
|
299
|
+
if (i(n.held, [
|
|
300
|
+
"tradeId",
|
|
301
|
+
"inventory",
|
|
302
|
+
"currencies"
|
|
303
|
+
], "invalid_record"), a && a.kind !== "accept") {
|
|
304
|
+
let o = i(a.value, [
|
|
305
|
+
"tradeId",
|
|
306
|
+
"inventory",
|
|
307
|
+
"currencies"
|
|
308
|
+
], "invalid_record");
|
|
309
|
+
if (o.tradeId !== e.view.tradeId || r(o.currencies, 32).length !== n.intent.offered.currencies.length) throw t("invalid_record");
|
|
310
|
+
}
|
|
311
|
+
let o = async (r, o, s, c) => {
|
|
312
|
+
if (c && i(c, ["inventory", "currencies"], "invalid_record"), o.items.length) {
|
|
313
|
+
if (!de) throw t("unavailable");
|
|
314
|
+
let i = await I(de.reservationEvidence(B, {
|
|
315
|
+
tenantId: n.tenantId,
|
|
316
|
+
owner: {
|
|
317
|
+
ownerKind: "player",
|
|
318
|
+
ownerId: r
|
|
319
|
+
},
|
|
320
|
+
reservationId: e.view.tradeId
|
|
321
|
+
}));
|
|
322
|
+
if (i.items.length !== o.items.length || i.items.some((e, t) => e.instance.id !== o.items[t].instanceId || e.qty !== o.items[t].qty) || i.state !== (a?.kind === "accept" ? "transferred" : a ? "released" : "reserved") || !i.reserve || s && !P(i.reserve, s.inventory) || c && (!i.transfer?.to || !P(c.inventory, {
|
|
323
|
+
reservationId: e.view.tradeId,
|
|
324
|
+
from: i.transfer.from,
|
|
325
|
+
to: i.transfer.to
|
|
326
|
+
})) || a && a.kind !== "accept" && !P(i.release, a.value.inventory)) throw t("invalid_record");
|
|
327
|
+
} else if (s?.inventory !== null && s !== null || c?.inventory !== null && c !== null) throw t("invalid_record");
|
|
328
|
+
if (s && (s.tradeId !== e.view.tradeId || s.currencies.length !== o.currencies.length) || c && c.currencies.length !== o.currencies.length) throw t("invalid_record");
|
|
329
|
+
for (let [l, u] of o.currencies.entries()) {
|
|
330
|
+
let o = await I(ue.escrow(B, {
|
|
331
|
+
tenantId: n.tenantId,
|
|
332
|
+
owner: {
|
|
333
|
+
kind: "wallet",
|
|
334
|
+
ownerKind: "player",
|
|
335
|
+
ownerId: r
|
|
336
|
+
},
|
|
337
|
+
reservationId: e.view.tradeId,
|
|
338
|
+
currency: u.currency
|
|
339
|
+
}));
|
|
340
|
+
if (o.state === "not-observed" || o.amount !== u.amount || o.state !== (a?.kind === "accept" ? "settled" : a ? "released" : "held") || s && !P(o.receipt, s.currencies[l])) throw t("invalid_record");
|
|
341
|
+
if (c) {
|
|
342
|
+
let r = c.currencies[l];
|
|
343
|
+
i(r, ["reserve", "settlement"], "invalid_record"), i(r.settlement, [
|
|
344
|
+
"reservationId",
|
|
345
|
+
"currency",
|
|
346
|
+
"gross",
|
|
347
|
+
"net",
|
|
348
|
+
"tax",
|
|
349
|
+
"payout",
|
|
350
|
+
"taxReceipt"
|
|
351
|
+
], "invalid_record");
|
|
352
|
+
let a = n.taxes.find((e) => e.currency === u.currency && e.gross === u.amount);
|
|
353
|
+
if (!a || !P(o.receipt, r.reserve) || !P(o.closingReceipt, r.settlement.payout) || !P(o.taxReceipt, r.settlement.taxReceipt) || !P({
|
|
354
|
+
currency: r.settlement.currency,
|
|
355
|
+
gross: r.settlement.gross,
|
|
356
|
+
net: r.settlement.net,
|
|
357
|
+
tax: r.settlement.tax
|
|
358
|
+
}, a) || r.settlement.reservationId !== e.view.tradeId) throw t("invalid_record");
|
|
359
|
+
} else if (a && !P(o.closingReceipt, a.value.currencies[l])) throw t("invalid_record");
|
|
360
|
+
}
|
|
361
|
+
}, s = a?.kind === "accept" ? a.value : null;
|
|
362
|
+
if (s && (i(s, [
|
|
363
|
+
"tradeId",
|
|
364
|
+
"offered",
|
|
365
|
+
"requested"
|
|
366
|
+
], "invalid_record"), s.tradeId !== e.view.tradeId)) throw t("invalid_record");
|
|
367
|
+
await o(n.proposerId, n.intent.offered, n.held, s?.offered ?? null), s && await o(a.ownerId, n.intent.requested, null, s.requested);
|
|
368
|
+
}, ye = async (n, r, i, o, s) => {
|
|
369
|
+
let c = await e.transactional(async (e) => {
|
|
370
|
+
await Y(e, n, r);
|
|
371
|
+
let c = (await e.query(D(y).where(E("id", G(n, r, i))).limit(1).descriptor))[0];
|
|
372
|
+
if (c && c.state !== "pending") {
|
|
373
|
+
if (c.state === "erased") throw t("erased");
|
|
374
|
+
if (o !== void 0 && (c.kind !== o || c.digest !== A([o, s]))) throw t("conflict");
|
|
375
|
+
return {
|
|
376
|
+
value: await Q(e, n, a(c.tradeId)),
|
|
377
|
+
op: c
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
});
|
|
381
|
+
if (c) return await $(c.value), await e.transactional(async (e) => {
|
|
382
|
+
if (await Y(e, n, r), !P((await Q(e, n, c.value.view.tradeId)).row, c.value.row)) throw t("conflict");
|
|
383
|
+
}), c.op.receipt;
|
|
384
|
+
}, be = async (n, r, i, a, o) => {
|
|
385
|
+
await pe(n, r), await e.transactional(async (e) => {
|
|
386
|
+
await J(e, n, [r]), await Y(e, n, r);
|
|
387
|
+
let s = G(n, r, i), c = (await e.query(D(y).where(E("id", s)).limit(1).descriptor))[0];
|
|
388
|
+
if (c) {
|
|
389
|
+
if (c.state === "erased") throw t("erased");
|
|
390
|
+
} else await e.insert(y.tableName, {
|
|
391
|
+
id: s,
|
|
392
|
+
tenantId: n,
|
|
393
|
+
ownerKey: U(n, r),
|
|
394
|
+
ownerId: r,
|
|
395
|
+
tradeId: o,
|
|
396
|
+
kind: a,
|
|
397
|
+
state: "pending",
|
|
398
|
+
admission: 0,
|
|
399
|
+
sequence: null,
|
|
400
|
+
digest: null,
|
|
401
|
+
request: null,
|
|
402
|
+
receipt: null,
|
|
403
|
+
at: null
|
|
404
|
+
});
|
|
405
|
+
});
|
|
406
|
+
}, xe = async (e, n, r, i, a, o, c) => {
|
|
407
|
+
let l = G(n, r, a.requestId), u = await Y(e, n, r), d = u.rows.find((e) => e.id === l);
|
|
408
|
+
if (!d || d.state !== "pending" || d.digest !== null || d.receipt !== null || d.request !== null || d.sequence !== null || d.kind !== i || d.tradeId !== o.tradeId) throw t("conflict");
|
|
409
|
+
let f = s(u.parent?.operationsCount, 0, k - 1) + 1, p = Object.freeze({
|
|
410
|
+
operationId: l,
|
|
411
|
+
requestId: a.requestId,
|
|
412
|
+
trade: o
|
|
413
|
+
});
|
|
414
|
+
return await e.update(y.tableName, l, {
|
|
415
|
+
state: "fulfilled",
|
|
416
|
+
sequence: f,
|
|
417
|
+
digest: A([i, a]),
|
|
418
|
+
request: a,
|
|
419
|
+
receipt: p,
|
|
420
|
+
at: new Date(c)
|
|
421
|
+
}), await e.update(b.tableName, U(n, r), { operationsCount: f }), p;
|
|
422
|
+
}, Se = async (e, n) => {
|
|
423
|
+
try {
|
|
424
|
+
return await e();
|
|
425
|
+
} catch (e) {
|
|
426
|
+
let r = await n();
|
|
427
|
+
if (r) return r;
|
|
428
|
+
throw e instanceof z ? t("invalid_record") : e;
|
|
429
|
+
}
|
|
430
|
+
}, Ce = (n, r) => R(() => {
|
|
431
|
+
let i = K(n), a = u(r);
|
|
432
|
+
if (a.toId === i.owner) throw t("invalid_input");
|
|
433
|
+
return L(async () => {
|
|
434
|
+
let n = () => ye(i.tenant, i.owner, a.requestId, "propose", a);
|
|
435
|
+
return await n() || Se(async () => {
|
|
436
|
+
let n = ee(a, p, C), r = fe(i.tenant, i.owner, n.requestId), o = H(), c = M(o + n.durationMs), u = l(await I(w.read({
|
|
437
|
+
tenantId: i.tenant,
|
|
438
|
+
ownerId: i.owner,
|
|
439
|
+
tradeId: r,
|
|
440
|
+
at: o
|
|
441
|
+
})), i.tenant, i.owner);
|
|
442
|
+
if (!u.allowed || u.trustScore < C.minTrustScore || n.offered.items.some((e) => {
|
|
443
|
+
let t = u.items.find((t) => t.instanceId === e.instanceId);
|
|
444
|
+
return !t || !t.tradable || t.locked || t.qty < e.qty || t.expiresAt !== null && t.expiresAt <= o;
|
|
445
|
+
})) throw t("forbidden");
|
|
446
|
+
let d = [...n.offered.currencies, ...n.requested.currencies].map((e) => g({
|
|
447
|
+
currency: e.currency,
|
|
448
|
+
amount: e.amount,
|
|
449
|
+
rules: C
|
|
450
|
+
}, V.taxPolicy));
|
|
451
|
+
if (d.some((e) => d.some((t) => e.currency === t.currency && e.gross === t.gross && e.tax !== t.tax))) throw t("invalid_input");
|
|
452
|
+
await be(i.tenant, i.owner, n.requestId, "propose", r), n.toId !== null && await pe(i.tenant, n.toId), await e.insertIgnore(S.tableName, {
|
|
453
|
+
id: r,
|
|
454
|
+
tenantId: i.tenant,
|
|
455
|
+
proposerKey: U(i.tenant, i.owner),
|
|
456
|
+
targetKey: n.toId === null ? null : U(i.tenant, n.toId),
|
|
457
|
+
accepterKey: null,
|
|
458
|
+
accepted: !1,
|
|
459
|
+
state: "pending",
|
|
460
|
+
admission: 0,
|
|
461
|
+
digest: null,
|
|
462
|
+
proposal: null,
|
|
463
|
+
closing: null,
|
|
464
|
+
createdAt: null,
|
|
465
|
+
expiresAt: null
|
|
466
|
+
}, { conflictColumns: ["id"] });
|
|
467
|
+
let f = await I(w.prepare(Object.freeze({
|
|
468
|
+
tenantId: i.tenant,
|
|
469
|
+
tradeId: r,
|
|
470
|
+
phase: "propose",
|
|
471
|
+
at: o,
|
|
472
|
+
expiresAt: c,
|
|
473
|
+
participants: Object.freeze([u])
|
|
474
|
+
}))), m = O(e, [f], async (e) => {
|
|
475
|
+
await J(e, i.tenant, [i.owner, ...n.toId === null ? [] : [n.toId]]), await Y(e, i.tenant, i.owner);
|
|
476
|
+
let a = (await X(e, i.tenant)).rows.find((e) => e.id === r);
|
|
477
|
+
if (!a || a.state === "erased") throw t("erased");
|
|
478
|
+
if (a.proposal !== null) throw new z();
|
|
479
|
+
if (a.proposerKey !== U(i.tenant, i.owner) || a.targetKey !== (n.toId === null ? null : U(i.tenant, n.toId))) throw t("conflict");
|
|
480
|
+
}), h = await I(le.prepareReserve(B, {
|
|
481
|
+
tenantId: i.tenant,
|
|
482
|
+
tradeId: r,
|
|
483
|
+
proposerId: i.owner,
|
|
484
|
+
offered: n.offered,
|
|
485
|
+
requested: n.requested
|
|
486
|
+
}, m)), _ = O(e, [h], async (e, [t]) => {
|
|
487
|
+
let a = Object.freeze({
|
|
488
|
+
tenantId: i.tenant,
|
|
489
|
+
proposerId: i.owner,
|
|
490
|
+
intent: n,
|
|
491
|
+
currencies: p,
|
|
492
|
+
rules: C,
|
|
493
|
+
createdAt: o,
|
|
494
|
+
expiresAt: c,
|
|
495
|
+
taxes: Object.freeze(d),
|
|
496
|
+
held: t
|
|
497
|
+
}), l = Z(r, a, null), u = await X(e, i.tenant), f = s(u.parent?.tradesCount, 0, k - 1) + 1;
|
|
498
|
+
return await e.update(S.tableName, r, {
|
|
499
|
+
accepted: !0,
|
|
500
|
+
state: "proposed",
|
|
501
|
+
digest: l.tradeDigest,
|
|
502
|
+
proposal: a,
|
|
503
|
+
createdAt: new Date(o),
|
|
504
|
+
expiresAt: new Date(c)
|
|
505
|
+
}), await e.update(x.tableName, W(i.tenant), { tradesCount: f }), xe(e, i.tenant, i.owner, "propose", n, l, o);
|
|
506
|
+
});
|
|
507
|
+
return (await ae(e, [_]))[0];
|
|
508
|
+
}, n);
|
|
509
|
+
});
|
|
510
|
+
}), we = (n, r, i, o) => R(() => {
|
|
511
|
+
let s = i === "expire" ? (q(n), {
|
|
512
|
+
tenant: a(o),
|
|
513
|
+
owner: "",
|
|
514
|
+
subject: structuredClone(n)
|
|
515
|
+
}) : K(n), c = i === "accept" ? _(r) : v(r);
|
|
516
|
+
return L(async () => {
|
|
517
|
+
let n = await e.transactional((e) => Q(e, s.tenant, c.tradeId)), r = i === "expire" ? n.proposal.proposerId : s.owner, a = () => ye(s.tenant, r, c.requestId, i, c);
|
|
518
|
+
return await a() || Se(async () => {
|
|
519
|
+
if (n.closing || i !== "accept" && r !== n.proposal.proposerId || i === "accept" && (c.tradeDigest !== n.view.tradeDigest || r === n.proposal.proposerId || n.proposal.intent.toId !== null && n.proposal.intent.toId !== r)) throw t("conflict");
|
|
520
|
+
let a = H();
|
|
521
|
+
if (a < n.proposal.createdAt || i === "accept" && a >= n.proposal.expiresAt || i === "expire" && a < n.proposal.expiresAt) throw t("forbidden");
|
|
522
|
+
let o;
|
|
523
|
+
if (i === "accept") {
|
|
524
|
+
let e = await Promise.all([n.proposal.proposerId, r].map(async (e) => l(await I(w.read({
|
|
525
|
+
tenantId: s.tenant,
|
|
526
|
+
ownerId: e,
|
|
527
|
+
tradeId: c.tradeId,
|
|
528
|
+
at: a
|
|
529
|
+
})), s.tenant, e)));
|
|
530
|
+
if (!h({
|
|
531
|
+
tenantId: s.tenant,
|
|
532
|
+
proposerId: n.proposal.proposerId,
|
|
533
|
+
accepterId: r,
|
|
534
|
+
tradeId: c.tradeId,
|
|
535
|
+
proposedToId: n.proposal.intent.toId,
|
|
536
|
+
createdAt: n.proposal.createdAt,
|
|
537
|
+
at: a,
|
|
538
|
+
expiresAt: n.proposal.expiresAt,
|
|
539
|
+
rules: n.proposal.rules,
|
|
540
|
+
currencies: n.proposal.currencies,
|
|
541
|
+
offered: n.proposal.intent.offered,
|
|
542
|
+
requested: n.proposal.intent.requested,
|
|
543
|
+
proposer: e[0],
|
|
544
|
+
accepter: e[1]
|
|
545
|
+
}, se)) throw t("forbidden");
|
|
546
|
+
o = await I(w.prepare(Object.freeze({
|
|
547
|
+
tenantId: s.tenant,
|
|
548
|
+
tradeId: c.tradeId,
|
|
549
|
+
phase: "accept",
|
|
550
|
+
at: a,
|
|
551
|
+
expiresAt: n.proposal.expiresAt,
|
|
552
|
+
participants: Object.freeze(e)
|
|
553
|
+
})));
|
|
554
|
+
}
|
|
555
|
+
await be(s.tenant, r, c.requestId, i, c.tradeId);
|
|
556
|
+
let u = oe(e, async (e) => {
|
|
557
|
+
let a = i === "accept" ? [r, n.proposal.proposerId] : [r];
|
|
558
|
+
await J(e, s.tenant, a);
|
|
559
|
+
for (let t of a) await Y(e, s.tenant, t);
|
|
560
|
+
let o = await Q(e, s.tenant, c.tradeId);
|
|
561
|
+
if ((await e.query(D(y).where(E("id", G(s.tenant, r, c.requestId))).limit(1).descriptor))[0]?.state === "fulfilled") throw new z();
|
|
562
|
+
if (o.closing || !P(o.proposal, n.proposal)) throw t("conflict");
|
|
563
|
+
}), d = o ? O(e, [o, u], async () => {}) : u, f = _e(n.proposal);
|
|
564
|
+
return (async (t) => {
|
|
565
|
+
let o = O(e, [t], async (e, [t]) => {
|
|
566
|
+
let o = Object.freeze({
|
|
567
|
+
ownerId: r,
|
|
568
|
+
kind: i,
|
|
569
|
+
request: c,
|
|
570
|
+
at: a,
|
|
571
|
+
value: t
|
|
572
|
+
}), l = Z(c.tradeId, n.proposal, o);
|
|
573
|
+
return await e.update(S.tableName, c.tradeId, {
|
|
574
|
+
state: l.state,
|
|
575
|
+
closing: o,
|
|
576
|
+
accepterKey: i === "accept" ? U(s.tenant, r) : null
|
|
577
|
+
}), xe(e, s.tenant, r, i, c, l, a);
|
|
578
|
+
});
|
|
579
|
+
return (await ae(e, [o]))[0];
|
|
580
|
+
})(i === "accept" ? await I(f.prepareExchange(B, {
|
|
581
|
+
...ve(n),
|
|
582
|
+
accepterId: r
|
|
583
|
+
}, d)) : await I(f.prepareRelease(B, ve(n), d)));
|
|
584
|
+
}, a);
|
|
585
|
+
});
|
|
586
|
+
}), Te = (n, r) => R(() => {
|
|
587
|
+
let o = i(n, [
|
|
588
|
+
"tenantId",
|
|
589
|
+
"subjectId",
|
|
590
|
+
"kind"
|
|
591
|
+
]);
|
|
592
|
+
if (o.kind !== "guest" && o.kind !== "user") throw t("invalid_input");
|
|
593
|
+
let s = a(o.tenantId), c = a(o.subjectId);
|
|
594
|
+
return L(async () => (await pe(s, c), e.transactional(async (e) => (await J(e, s, [c], !0), await Y(e, s, c, !0), await X(e, s), r(e, s, c)))));
|
|
595
|
+
}), Ee = Object.freeze({
|
|
596
|
+
expireDue: (n, r) => R(() => {
|
|
597
|
+
q(n);
|
|
598
|
+
let i = structuredClone(n), o = te(r);
|
|
599
|
+
return L(async () => {
|
|
600
|
+
let n = H(), r = await e.transactional(async (e) => {
|
|
601
|
+
let r = await X(e, o.tenantId), i = [], s = /* @__PURE__ */ new Set(), c = async (t) => {
|
|
602
|
+
s.has(t) || (await Y(e, o.tenantId, t), s.add(t));
|
|
603
|
+
};
|
|
604
|
+
for (let s of r.rows) {
|
|
605
|
+
if (s.state === "erased") {
|
|
606
|
+
if (s.proposal !== null || s.closing !== null || s.digest !== null || s.createdAt !== null || s.expiresAt !== null) throw t("invalid_record");
|
|
607
|
+
continue;
|
|
608
|
+
}
|
|
609
|
+
if (s.accepted !== !0) continue;
|
|
610
|
+
let l = await Q(e, o.tenantId, a(s.id), !1, r);
|
|
611
|
+
await c(l.proposal.proposerId), l.closing?.kind === "accept" && await c(l.closing.ownerId), l.view.state === "proposed" && l.proposal.expiresAt <= n && i.push(l.view.tradeId);
|
|
612
|
+
}
|
|
613
|
+
if (i.length > o.maxTrades) throw t("limit_exceeded");
|
|
614
|
+
return {
|
|
615
|
+
scannedRows: r.rows.length,
|
|
616
|
+
due: i.sort()
|
|
617
|
+
};
|
|
618
|
+
}), s = 0, c = 0;
|
|
619
|
+
for (let t of r.due) try {
|
|
620
|
+
await I(Ee.expire(i, {
|
|
621
|
+
tenantId: o.tenantId,
|
|
622
|
+
tradeId: t
|
|
623
|
+
})), s++;
|
|
624
|
+
} catch (n) {
|
|
625
|
+
let r = await e.transactional((e) => Q(e, o.tenantId, t));
|
|
626
|
+
if (r.view.state === "proposed") throw n;
|
|
627
|
+
await $(r), c++;
|
|
628
|
+
}
|
|
629
|
+
return Object.freeze({
|
|
630
|
+
capturedAt: n,
|
|
631
|
+
scannedRows: r.scannedRows,
|
|
632
|
+
dueTrades: r.due.length,
|
|
633
|
+
expiredTrades: s,
|
|
634
|
+
alreadyClosedTrades: c
|
|
635
|
+
});
|
|
636
|
+
});
|
|
637
|
+
}),
|
|
638
|
+
propose: Ce,
|
|
639
|
+
accept: (e, t) => we(e, t, "accept"),
|
|
640
|
+
cancel: (e, t) => we(e, t, "cancel"),
|
|
641
|
+
expire: (e, t) => R(() => {
|
|
642
|
+
q(e);
|
|
643
|
+
let n = i(t, ["tenantId", "tradeId"]), r = a(n.tenantId), o = a(n.tradeId);
|
|
644
|
+
return we(e, {
|
|
645
|
+
tradeId: o,
|
|
646
|
+
requestId: j("expiry.", r, o)
|
|
647
|
+
}, "expire", r);
|
|
648
|
+
}),
|
|
649
|
+
mine: (t) => R(() => {
|
|
650
|
+
let n = K(t);
|
|
651
|
+
return L(async () => {
|
|
652
|
+
let t = await e.transactional(async (e) => {
|
|
653
|
+
await Y(e, n.tenant, n.owner);
|
|
654
|
+
let t = await X(e, n.tenant), r = U(n.tenant, n.owner), i = [];
|
|
655
|
+
for (let o of t.rows) o.accepted === !0 && o.state !== "erased" && [
|
|
656
|
+
o.proposerKey,
|
|
657
|
+
o.targetKey,
|
|
658
|
+
o.accepterKey
|
|
659
|
+
].includes(r) && i.push(await Q(e, n.tenant, a(o.id)));
|
|
660
|
+
return i;
|
|
661
|
+
});
|
|
662
|
+
for (let e of t) await $(e);
|
|
663
|
+
return Object.freeze(t.map((e) => e.view).sort((e, t) => e.tradeId < t.tradeId ? -1 : 1));
|
|
664
|
+
});
|
|
665
|
+
}),
|
|
666
|
+
receipt: (e, t) => R(() => {
|
|
667
|
+
let n = K(e), r = i(t, ["requestId"]), o = a(r.requestId);
|
|
668
|
+
return L(async () => {
|
|
669
|
+
let e = await ye(n.tenant, n.owner, o);
|
|
670
|
+
return Object.freeze(e ? {
|
|
671
|
+
requestId: o,
|
|
672
|
+
state: "fulfilled",
|
|
673
|
+
receipt: e
|
|
674
|
+
} : {
|
|
675
|
+
requestId: o,
|
|
676
|
+
state: "not-observed"
|
|
677
|
+
});
|
|
678
|
+
});
|
|
679
|
+
}),
|
|
680
|
+
rebuild: (n, r) => R(() => {
|
|
681
|
+
q(n);
|
|
682
|
+
let o = i(r, ["tenantId", "tradeId"]), s = a(o.tenantId), c = a(o.tradeId);
|
|
683
|
+
return L(async () => {
|
|
684
|
+
let n = await e.transactional((e) => Q(e, s, c, !0));
|
|
685
|
+
return await $(n), e.transactional(async (e) => {
|
|
686
|
+
await J(e, s, [n.proposal.proposerId, ...n.closing?.kind === "accept" ? [n.closing.ownerId] : []]);
|
|
687
|
+
let r = await Q(e, s, c, !0);
|
|
688
|
+
for (let t of [n.proposal.proposerId, ...n.closing?.kind === "accept" ? [n.closing.ownerId] : []]) await Y(e, s, t);
|
|
689
|
+
if (!P(r.row, n.row)) throw t("conflict");
|
|
690
|
+
return await e.update(S.tableName, c, { state: r.view.state }), r.view;
|
|
691
|
+
});
|
|
692
|
+
});
|
|
693
|
+
}),
|
|
694
|
+
exportSubject: (e) => Te(e, async (e, t, n) => {
|
|
695
|
+
let r = U(t, n), i = await X(e, t), a = await Y(e, t, n, !0);
|
|
696
|
+
return {
|
|
697
|
+
trades: i.rows.filter((e) => [
|
|
698
|
+
e.proposerKey,
|
|
699
|
+
e.targetKey,
|
|
700
|
+
e.accepterKey
|
|
701
|
+
].includes(r) && e.proposal !== null).map((e) => ({
|
|
702
|
+
tradeId: e.id,
|
|
703
|
+
proposal: e.proposal,
|
|
704
|
+
closing: e.closing
|
|
705
|
+
})),
|
|
706
|
+
operations: a.rows.map((e) => ({
|
|
707
|
+
operationId: e.id,
|
|
708
|
+
state: e.state,
|
|
709
|
+
request: e.request,
|
|
710
|
+
receipt: e.receipt
|
|
711
|
+
}))
|
|
712
|
+
};
|
|
713
|
+
}),
|
|
714
|
+
eraseSubject: (e) => Te(e, async (e, t, n) => {
|
|
715
|
+
let r = U(t, n), i = (await X(e, t)).rows.filter((e) => [
|
|
716
|
+
e.proposerKey,
|
|
717
|
+
e.targetKey,
|
|
718
|
+
e.accepterKey
|
|
719
|
+
].includes(r));
|
|
720
|
+
for (let n of i) await e.update(S.tableName, String(n.id), {
|
|
721
|
+
state: "erased",
|
|
722
|
+
digest: null,
|
|
723
|
+
proposal: null,
|
|
724
|
+
closing: null,
|
|
725
|
+
createdAt: null,
|
|
726
|
+
expiresAt: null
|
|
727
|
+
}), await e.updateMany(y.tableName, {
|
|
728
|
+
state: "erased",
|
|
729
|
+
digest: null,
|
|
730
|
+
request: null,
|
|
731
|
+
receipt: null,
|
|
732
|
+
at: null
|
|
733
|
+
}, { where: T(E("tenantId", t), E("tradeId", n.id)) });
|
|
734
|
+
await e.updateMany(y.tableName, {
|
|
735
|
+
state: "erased",
|
|
736
|
+
ownerId: null,
|
|
737
|
+
digest: null,
|
|
738
|
+
request: null,
|
|
739
|
+
receipt: null,
|
|
740
|
+
at: null
|
|
741
|
+
}, { where: T(E("tenantId", t), E("ownerKey", r)) }), await e.update(b.tableName, r, {
|
|
742
|
+
ownerId: null,
|
|
743
|
+
erased: !0
|
|
744
|
+
});
|
|
745
|
+
})
|
|
746
|
+
});
|
|
747
|
+
return Ee;
|
|
748
|
+
};
|
|
749
|
+
//#endregion
|
|
750
|
+
export { B as dataStoreTrades, ce as resolveTradeStoreOptions };
|