@voltro/plugin-economy 0.0.0-stage → 0.88.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 +24492 -0
- package/LICENSE +61 -0
- package/README.md +2719 -2
- package/SECURITY.md +56 -0
- package/THIRD-PARTY-NOTICES.md +9841 -0
- package/dist/amount.js +20 -0
- package/dist/appleIap.server.js +262 -0
- package/dist/appleIapNotifications.server.js +162 -0
- package/dist/appleIapTrust.server.js +30 -0
- package/dist/bundleValue.server.js +112 -0
- package/dist/canonicalDataJson.js +10 -0
- package/dist/capture.js +53 -0
- package/dist/codePort.fixture.js +123 -0
- package/dist/codeStore.server.js +439 -0
- package/dist/codeTables.entity.js +43 -0
- package/dist/codes.d.ts +150 -0
- package/dist/codes.js +82 -0
- package/dist/components.d.ts +397 -0
- package/dist/components.js +283 -0
- package/dist/consistency.server.js +115 -0
- package/dist/containerStore.server.js +687 -0
- package/dist/containerStore.testFixtures.js +75 -0
- package/dist/containerTables.entity.js +45 -0
- package/dist/containers.d.ts +311 -0
- package/dist/containers.fixture.js +128 -0
- package/dist/containers.js +214 -0
- package/dist/containers.server.js +39 -0
- package/dist/containersPort.fixture.js +74 -0
- package/dist/currency.js +32 -0
- package/dist/declarationFixtures.js +61 -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 +124 -0
- package/dist/errors.d.ts +14 -0
- package/dist/errors.js +21 -0
- package/dist/events.d.ts +46 -0
- package/dist/events.js +1 -0
- package/dist/googleIap.server.js +391 -0
- package/dist/googleIapNotifications.server.js +130 -0
- package/dist/googleIapSubscription.server.js +183 -0
- package/dist/iap.d.ts +309 -0
- package/dist/iap.js +303 -0
- package/dist/iap.testFixtures.js +144 -0
- package/dist/iapAccountTables.entity.js +39 -0
- package/dist/iapAccounts.server.js +200 -0
- package/dist/iapBinding.server.js +523 -0
- package/dist/iapCheckout.server.js +97 -0
- package/dist/iapCompletion.server.js +55 -0
- package/dist/iapCurrentState.server.js +107 -0
- package/dist/iapCurrentState.testFixtures.js +22 -0
- package/dist/iapCurrentStatePort.fixture.js +63 -0
- package/dist/iapEntitlements.d.ts +79 -0
- package/dist/iapEntitlements.js +57 -0
- package/dist/iapEntitlements.server.js +48 -0
- package/dist/iapEntitlementsBinding.server.js +105 -0
- package/dist/iapEntitlementsPort.fixture.js +80 -0
- package/dist/iapInstalledEntitlements.testFixtures.js +75 -0
- package/dist/iapInstalledNotifications.testFixtures.js +93 -0
- package/dist/iapInventoryRefunds.server.js +261 -0
- package/dist/iapNotificationCustodyPort.fixture.js +67 -0
- package/dist/iapNotificationPort.fixture.js +73 -0
- package/dist/iapNotificationProcessor.server.js +134 -0
- package/dist/iapNotificationProcessor.testFixtures.js +63 -0
- package/dist/iapNotificationProcessorPort.fixture.js +55 -0
- package/dist/iapNotificationSchedule.server.js +69 -0
- package/dist/iapNotificationStore.server.js +352 -0
- package/dist/iapNotificationStore.testFixtures.js +31 -0
- package/dist/iapNotificationTables.entity.js +48 -0
- package/dist/iapNotificationTarget.server.js +36 -0
- package/dist/iapNotificationTarget.testFixtures.js +29 -0
- package/dist/iapNotifications.d.ts +644 -0
- package/dist/iapNotifications.js +6 -0
- package/dist/iapNotifications.server.js +95 -0
- package/dist/iapNotificationsBinding.server.js +232 -0
- package/dist/iapNotificationsSystemPort.fixture.js +50 -0
- package/dist/iapPlugin.testFixtures.js +59 -0
- package/dist/iapPort.fixture.js +74 -0
- package/dist/iapProviderCapture.server.js +17 -0
- package/dist/iapRecovery.d.ts +146 -0
- package/dist/iapRecovery.js +89 -0
- package/dist/iapRefundEventDelivery.server.js +82 -0
- package/dist/iapRefundEvents.js +44 -0
- package/dist/iapRefundEventsPort.fixture.js +59 -0
- package/dist/iapRefundPort.fixture.js +54 -0
- package/dist/iapRefundTables.entity.js +55 -0
- package/dist/iapRefunds.d.ts +755 -0
- package/dist/iapRefunds.js +6 -0
- package/dist/iapRefunds.server.js +553 -0
- package/dist/iapRefunds.testFixtures.js +64 -0
- package/dist/iapSpendAdmission.server.js +98 -0
- package/dist/iapStore.server.js +740 -0
- package/dist/iapStore.testFixtures.js +127 -0
- package/dist/iapTables.entity.js +80 -0
- package/dist/iapVerification.server.js +120 -0
- package/dist/index.d.ts +2984 -0
- package/dist/index.js +15 -0
- package/dist/intentionRecovery.js +133 -0
- package/dist/ledger.js +77 -0
- package/dist/loyalty.d.ts +274 -0
- package/dist/loyalty.fixture.js +92 -0
- package/dist/loyalty.js +311 -0
- package/dist/loyaltyBinding.server.js +203 -0
- package/dist/loyaltyPort.fixture.js +86 -0
- package/dist/loyaltyShopSource.server.js +90 -0
- package/dist/loyaltyStore.server.js +552 -0
- package/dist/loyaltyStore.testFixtures.js +82 -0
- package/dist/loyaltyTables.entity.js +49 -0
- package/dist/offerMerchandising.fixture.js +114 -0
- package/dist/offerMerchandising.js +282 -0
- package/dist/offerPolicies.d.ts +441 -0
- package/dist/offerPolicies.fixture.js +53 -0
- package/dist/offerPolicies.js +267 -0
- package/dist/piggyBanks.d.ts +272 -0
- package/dist/piggyBanks.fixture.js +70 -0
- package/dist/piggyBanks.js +200 -0
- package/dist/piggyBinding.server.js +204 -0
- package/dist/piggyPort.fixture.js +72 -0
- package/dist/piggyStore.server.js +610 -0
- package/dist/piggyStore.testFixtures.js +97 -0
- package/dist/piggyTables.entity.js +40 -0
- package/dist/plugin.js +1047 -0
- package/dist/port.js +12 -0
- package/dist/price.d.ts +99 -0
- package/dist/price.js +143 -0
- package/dist/purchaseRecovery.js +55 -0
- package/dist/reports.server.js +114 -0
- package/dist/rpc.d.ts +3201 -0
- package/dist/rpc.js +1200 -0
- package/dist/sanctionsTradeAuthority.server.js +79 -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 +195 -0
- package/dist/shop.testFixtures.js +113 -0
- package/dist/shopPort.fixture.js +71 -0
- package/dist/shopPresentation.js +67 -0
- package/dist/shopStore.server.js +623 -0
- package/dist/shopTables.entity.js +62 -0
- package/dist/shopUiCopy.js +56 -0
- package/dist/spendLimits.d.ts +140 -0
- package/dist/spendLimits.fixture.js +52 -0
- package/dist/spendLimits.js +154 -0
- package/dist/spendLimitsPort.fixture.js +119 -0
- package/dist/subscriptionBinding.server.js +210 -0
- package/dist/subscriptionStore.server.js +642 -0
- package/dist/subscriptionStore.testFixtures.js +71 -0
- package/dist/subscriptionTables.entity.js +36 -0
- package/dist/subscriptions.d.ts +394 -0
- package/dist/subscriptions.fixture.js +96 -0
- package/dist/subscriptions.js +212 -0
- package/dist/subscriptionsPort.fixture.js +75 -0
- package/dist/systemFixtures.js +88 -0
- package/dist/tables.entity.js +82 -0
- package/dist/testing.d.ts +4615 -0
- package/dist/testing.js +29 -0
- package/dist/tradeAuthority.server.js +171 -0
- package/dist/tradeBinding.server.js +200 -0
- package/dist/tradeComponents.js +341 -0
- package/dist/tradeEscrow.server.js +231 -0
- package/dist/tradeEscrow.testFixtures.js +132 -0
- package/dist/tradeExpiry.server.js +42 -0
- package/dist/tradeRecovery.js +47 -0
- package/dist/tradeStore.server.js +749 -0
- package/dist/tradeStore.testFixtures.js +72 -0
- package/dist/tradeTables.entity.js +65 -0
- package/dist/tradeUiCopy.js +78 -0
- package/dist/trades.d.ts +246 -0
- package/dist/trades.fixture.js +102 -0
- package/dist/trades.js +288 -0
- package/dist/tradesPort.fixture.js +93 -0
- package/dist/walletEscrow.js +105 -0
- package/dist/walletEscrow.server.js +375 -0
- package/dist/walletKeys.server.js +4 -0
- package/dist/walletStore.js +570 -0
- package/dist/web.d.ts +2257 -0
- package/dist/web.js +8 -0
- package/package.json +161 -4
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
import { EconomyError as e, economyError as t } from "./errors.js";
|
|
2
|
+
import { dataObject as n, identifier as r } from "./capture.js";
|
|
3
|
+
import { positiveAmount as i } from "./amount.js";
|
|
4
|
+
import { walletHash as a, walletKey as o } from "./walletKeys.server.js";
|
|
5
|
+
import { economyAccountKey as s } from "./ledger.js";
|
|
6
|
+
import { economySubjectsTable as c, economyTransactionsTable as l } from "./tables.entity.js";
|
|
7
|
+
import { captureWalletEscrowInput as u, captureWalletEscrowReserveInput as d, captureWalletEscrowReserveTaxSettleInput as f, captureWalletEscrowSettleInput as p, captureWalletEscrowTaxSettleInput as m } from "./walletEscrow.js";
|
|
8
|
+
import { Effect as h, Result as g } from "effect";
|
|
9
|
+
import { eq as _, queryFor as v } from "@voltro/database";
|
|
10
|
+
import { prepareSqlMutation as y, prepareSqlMutationGroup as b } from "@voltro/database/sql";
|
|
11
|
+
// src/walletEscrow.server.ts
|
|
12
|
+
var x = (x, S) => {
|
|
13
|
+
let C = new Set(S.allowedServices), w = (n) => h.tryPromise({
|
|
14
|
+
try: n,
|
|
15
|
+
catch: (n) => n instanceof e ? n : t("unavailable")
|
|
16
|
+
}), T = (n) => {
|
|
17
|
+
try {
|
|
18
|
+
return n();
|
|
19
|
+
} catch (n) {
|
|
20
|
+
return h.fail(n instanceof e ? n : t("invalid_input"));
|
|
21
|
+
}
|
|
22
|
+
}, E = (e) => {
|
|
23
|
+
if (e.type !== "system" && !(e.type === "service" && C.has(e.service))) throw t("forbidden");
|
|
24
|
+
}, D = (e, t = e.owner) => o("ecs_", e.tenantId, s(t)), O = (e, t) => o("ecq_", t, e.tenantId, D(e), e.reservationId, e.currency), k = (e, t) => o("ect_", e.tenantId, O(e, t)), A = (e) => o("escrow.", e.tenantId, D(e), e.reservationId, e.currency), j = (e, t) => e.tenantId === t.tenantId && e.reservationId === t.reservationId && e.currency === t.currency && s(e.owner) === s(t.owner), M = async (e, n) => {
|
|
25
|
+
let r = (await e.query(v(c).where(_("id", D(n))).limit(1).descriptor))[0];
|
|
26
|
+
if (r) {
|
|
27
|
+
if (r.tenantId !== n.tenantId || r.ownerKind !== n.owner.ownerKind) throw t("invalid_record");
|
|
28
|
+
if (r.erased === !0) throw t("erased");
|
|
29
|
+
if (r.erased !== !1 || r.ownerId !== n.owner.ownerId) throw t("invalid_record");
|
|
30
|
+
}
|
|
31
|
+
}, N = (e) => {
|
|
32
|
+
let a = n(e.escrow, ["intent", "close"], "invalid_record"), o = d(a.intent);
|
|
33
|
+
if (e.tenantId !== o.tenantId || e.id !== k(o, "reserve") || e.state !== "committed") throw t("invalid_record");
|
|
34
|
+
let s = null;
|
|
35
|
+
if (a.close !== null) {
|
|
36
|
+
let c = n(a.close, [
|
|
37
|
+
"mode",
|
|
38
|
+
"targetKey",
|
|
39
|
+
"receipt",
|
|
40
|
+
"tax"
|
|
41
|
+
], "invalid_record"), l = r(c.targetKey);
|
|
42
|
+
if (c.mode !== "release" && c.mode !== "settle" || !/^ecs_[a-f0-9]{56}$/.test(l) || c.mode === "release" != (l === D(o))) throw t("invalid_record");
|
|
43
|
+
let u = null;
|
|
44
|
+
if (c.tax !== null) {
|
|
45
|
+
let a = n(c.tax, [
|
|
46
|
+
"amount",
|
|
47
|
+
"account",
|
|
48
|
+
"receipt"
|
|
49
|
+
], "invalid_record"), s = i(a.amount).toString(), l = r(a.account);
|
|
50
|
+
if (c.mode !== "settle" || !l.startsWith("escrow.tax.") || BigInt(s) > BigInt(String(o.amount))) throw t("invalid_record");
|
|
51
|
+
let d = S.decodeReceipt({
|
|
52
|
+
id: k(o, "tax"),
|
|
53
|
+
createdAt: e.createdAt,
|
|
54
|
+
receipt: n(a.receipt, [
|
|
55
|
+
"txId",
|
|
56
|
+
"currency",
|
|
57
|
+
"amount",
|
|
58
|
+
"at"
|
|
59
|
+
], "invalid_record")
|
|
60
|
+
});
|
|
61
|
+
if (d.currency !== o.currency || d.amount !== s) throw t("invalid_record");
|
|
62
|
+
u = Object.freeze({
|
|
63
|
+
amount: s,
|
|
64
|
+
account: l,
|
|
65
|
+
receipt: d
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
let d = BigInt(String(o.amount)) - BigInt(u?.amount ?? "0"), f = c.receipt === null ? null : S.decodeReceipt({
|
|
69
|
+
id: k(o, "close"),
|
|
70
|
+
createdAt: e.createdAt,
|
|
71
|
+
receipt: n(c.receipt, [
|
|
72
|
+
"txId",
|
|
73
|
+
"currency",
|
|
74
|
+
"amount",
|
|
75
|
+
"at"
|
|
76
|
+
], "invalid_record")
|
|
77
|
+
});
|
|
78
|
+
if (d === 0n ? f !== null : !f || f.currency !== o.currency || f.amount !== d.toString()) throw t("invalid_record");
|
|
79
|
+
s = Object.freeze({
|
|
80
|
+
mode: c.mode,
|
|
81
|
+
targetKey: l,
|
|
82
|
+
receipt: f,
|
|
83
|
+
tax: u
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
return Object.freeze({
|
|
87
|
+
intent: Object.freeze({
|
|
88
|
+
...o,
|
|
89
|
+
amount: i(o.amount).toString()
|
|
90
|
+
}),
|
|
91
|
+
close: s
|
|
92
|
+
});
|
|
93
|
+
}, P = async (e, n, i, c, l, u, d) => {
|
|
94
|
+
let f = k(n, i), p = (await S.readEvidence(e, n.tenantId, [f], n.currency)).get(f);
|
|
95
|
+
if (!p || l.txId !== f || l.currency !== n.currency || l.amount !== c) throw t("invalid_record");
|
|
96
|
+
let m = p.pair.find((e) => BigInt(String(e.debit)) > 0n), h = p.pair.find((e) => BigInt(String(e.credit)) > 0n);
|
|
97
|
+
if (!m || !h || String(m.debit) !== c || String(h.credit) !== c || S.ledgerAt(m.at) !== l.at || S.ledgerAt(h.at) !== l.at) throw t("invalid_record");
|
|
98
|
+
let g = i === "reserve" ? h : m, _ = i === "reserve" ? m : h;
|
|
99
|
+
if (g.accountKind !== "system" || g.systemAccount !== A(n) || _.accountKind !== "wallet" || g.reason !== (i === "reserve" ? "escrow.reserve" : d === "release" ? "escrow.release" : "escrow.settle")) throw t("invalid_record");
|
|
100
|
+
if (p.receipt.state === "committed") {
|
|
101
|
+
if (g.refKind !== "economy.escrow" || g.refId !== n.reservationId) throw t("invalid_record");
|
|
102
|
+
let e = S.decodeReceipt(p.receipt);
|
|
103
|
+
if (a(e) !== a(l) || _.ownerKind !== "player" && _.ownerKind !== "guild" || o("ecs_", n.tenantId, s({
|
|
104
|
+
kind: "wallet",
|
|
105
|
+
ownerKind: _.ownerKind,
|
|
106
|
+
ownerId: r(_.ownerId)
|
|
107
|
+
})) !== (i === "reserve" ? D(n) : u)) throw t("invalid_record");
|
|
108
|
+
} else if (i === "reserve") throw t("erased");
|
|
109
|
+
}, F = async (e, n, r) => {
|
|
110
|
+
let i = (await S.readEvidence(e, n.tenantId, [k(n, "tax")], n.currency)).get(k(n, "tax"));
|
|
111
|
+
if (!i || r.receipt.txId !== k(n, "tax") || r.receipt.currency !== n.currency || r.receipt.amount !== r.amount) throw t("invalid_record");
|
|
112
|
+
let o = i.pair.find((e) => BigInt(String(e.debit)) > 0n), s = i.pair.find((e) => BigInt(String(e.credit)) > 0n);
|
|
113
|
+
if (!o || !s || o.accountKind !== "system" || s.accountKind !== "system" || o.systemAccount !== A(n) || s.systemAccount !== r.account || String(o.debit) !== r.amount || String(s.credit) !== r.amount || o.reason !== "escrow.tax" || S.ledgerAt(o.at) !== r.receipt.at || S.ledgerAt(s.at) !== r.receipt.at || i.receipt.state === "committed" && (a(S.decodeReceipt(i.receipt)) !== a(r.receipt) || o.refKind !== "economy.escrow" || o.refId !== n.reservationId)) throw t("invalid_record");
|
|
114
|
+
}, I = async (e, n, r) => {
|
|
115
|
+
if (!r.close) throw t("invalid_record");
|
|
116
|
+
if (r.close.receipt) await P(e, n, "close", r.close.receipt.amount, r.close.receipt, r.close.targetKey, r.close.mode);
|
|
117
|
+
else {
|
|
118
|
+
let r = (await e.query(v(l).where(_("id", k(n, "close"))).limit(1).descriptor))[0];
|
|
119
|
+
if (r && (r.tenantId !== n.tenantId || r.state !== "pending" || r.receipt !== null || r.digest !== null || r.escrow !== null)) throw t("invalid_record");
|
|
120
|
+
}
|
|
121
|
+
if (r.close.tax) await F(e, n, r.close.tax);
|
|
122
|
+
else {
|
|
123
|
+
let r = (await e.query(v(l).where(_("id", k(n, "tax"))).limit(1).descriptor))[0];
|
|
124
|
+
if (r && (r.tenantId !== n.tenantId || r.state !== "pending" || r.receipt !== null || r.digest !== null || r.escrow !== null)) throw t("invalid_record");
|
|
125
|
+
}
|
|
126
|
+
}, L = (e, n) => {
|
|
127
|
+
if (!n.close) throw t("invalid_record");
|
|
128
|
+
let r = n.intent.amount, i = n.close.tax?.amount ?? "0";
|
|
129
|
+
return Object.freeze({
|
|
130
|
+
reservationId: e.reservationId,
|
|
131
|
+
currency: e.currency,
|
|
132
|
+
gross: r,
|
|
133
|
+
net: (BigInt(r) - BigInt(i)).toString(),
|
|
134
|
+
tax: i,
|
|
135
|
+
payout: n.close.receipt,
|
|
136
|
+
taxReceipt: n.close.tax?.receipt ?? null
|
|
137
|
+
});
|
|
138
|
+
}, R = async (e, n) => {
|
|
139
|
+
await M(e, n);
|
|
140
|
+
let r = (await e.query(v(l).where(_("id", k(n, "reserve"))).limit(1).descriptor))[0];
|
|
141
|
+
if (!r) return;
|
|
142
|
+
if (r.tenantId !== n.tenantId) throw t("invalid_record");
|
|
143
|
+
if (r.state === "pending") {
|
|
144
|
+
if (r.escrow !== null || r.digest !== null || r.receipt !== null) throw t("invalid_record");
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
if (r.state === "erased") throw t("erased");
|
|
148
|
+
if (r.escrow === null) throw t("invalid_record");
|
|
149
|
+
let i = N(r), a = S.decodeReceipt(r);
|
|
150
|
+
if (!j(n, i.intent) || a.amount !== i.intent.amount || a.currency !== n.currency) throw t("invalid_record");
|
|
151
|
+
if (await P(e, n, "reserve", i.intent.amount, a), i.close) await I(e, n, i);
|
|
152
|
+
else for (let r of ["close", "tax"]) {
|
|
153
|
+
let i = (await e.query(v(l).where(_("id", k(n, r))).limit(1).descriptor))[0];
|
|
154
|
+
if (i && (i.tenantId !== n.tenantId || i.state !== "pending" || i.escrow !== null || i.digest !== null || i.receipt !== null)) throw t("invalid_record");
|
|
155
|
+
}
|
|
156
|
+
return {
|
|
157
|
+
body: i,
|
|
158
|
+
receipt: a
|
|
159
|
+
};
|
|
160
|
+
}, z = async (e, n, r) => {
|
|
161
|
+
let i = r && s(r) !== s(n.owner) ? [n.owner, r] : [n.owner];
|
|
162
|
+
for (let t of i.sort((e, t) => D(n, e) < D(n, t) ? -1 : 1)) await S.fenceWallet(e, n.tenantId, t);
|
|
163
|
+
if (await S.fenceCurrency(e, n.tenantId, n.currency), await e.updateMany(l.tableName, { admission: 0 }, { where: _("id", k(n, "reserve")) }) !== 1) throw t("not_found");
|
|
164
|
+
}, B = (e, n) => T(() => {
|
|
165
|
+
let r = structuredClone(e), i = d(n);
|
|
166
|
+
return E(r), w(async () => {
|
|
167
|
+
let e = await x.transactional((e) => R(e, i));
|
|
168
|
+
if (e) {
|
|
169
|
+
if (e.body.intent.amount !== String(i.amount)) throw t("conflict");
|
|
170
|
+
return y(x, async (e) => {
|
|
171
|
+
await z(e, i);
|
|
172
|
+
let n = await R(e, i);
|
|
173
|
+
if (!n || n.body.intent.amount !== String(i.amount)) throw t("conflict");
|
|
174
|
+
return n.receipt;
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
let n = await h.runPromise(h.result(S.prepareTransfer(r, {
|
|
178
|
+
tenantId: i.tenantId,
|
|
179
|
+
requestId: O(i, "reserve"),
|
|
180
|
+
currency: i.currency,
|
|
181
|
+
amount: i.amount,
|
|
182
|
+
from: i.owner,
|
|
183
|
+
to: {
|
|
184
|
+
kind: "system",
|
|
185
|
+
id: A(i)
|
|
186
|
+
},
|
|
187
|
+
reason: "escrow.reserve",
|
|
188
|
+
ref: {
|
|
189
|
+
kind: "economy.escrow",
|
|
190
|
+
id: i.reservationId
|
|
191
|
+
}
|
|
192
|
+
})));
|
|
193
|
+
if (g.isFailure(n)) throw n.failure;
|
|
194
|
+
return b(x, [n.success], async (e, [n]) => {
|
|
195
|
+
let r = (await e.query(v(l).where(_("id", n.txId)).limit(1).descriptor))[0];
|
|
196
|
+
if (!r || r.state !== "committed") throw t("invalid_record");
|
|
197
|
+
if (r.escrow !== null) {
|
|
198
|
+
let e = N(r);
|
|
199
|
+
if (!j(e.intent, i) || e.intent.amount !== String(i.amount)) throw t("conflict");
|
|
200
|
+
} else await e.update(l.tableName, n.txId, { escrow: Object.freeze({
|
|
201
|
+
intent: i,
|
|
202
|
+
close: null
|
|
203
|
+
}) });
|
|
204
|
+
return await P(e, i, "reserve", String(i.amount), n), n;
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
}), V = (e, n, r, o, s) => T(() => {
|
|
208
|
+
let c = structuredClone(e), d = o ? m(n) : r === "settle" ? p(n) : u(n);
|
|
209
|
+
E(c);
|
|
210
|
+
let f = r === "settle" ? d.to : d.owner, C = D(d, f), T = o ? d.taxAmount : "0", F = o ? d.taxAccountId : "escrow.tax.default", B = (e) => e.close?.mode === r && e.close.targetKey === C && (e.close.tax?.amount ?? "0") === T && (T === "0" || e.close.tax?.account === F);
|
|
211
|
+
return w(async () => {
|
|
212
|
+
let e = await x.transactional((e) => R(e, d));
|
|
213
|
+
if (!e && s === void 0) throw t("not_found");
|
|
214
|
+
let n = e?.body.intent.amount ?? i(s).toString();
|
|
215
|
+
if (s !== void 0 && s !== n) throw t("conflict");
|
|
216
|
+
if (BigInt(T) > BigInt(n)) throw t("invalid_amount");
|
|
217
|
+
if (e?.body.close) {
|
|
218
|
+
if (!B(e.body)) throw t("conflict");
|
|
219
|
+
return y(x, async (e) => {
|
|
220
|
+
await z(e, d, f);
|
|
221
|
+
let n = await R(e, d);
|
|
222
|
+
if (!n || !B(n.body)) throw t("conflict");
|
|
223
|
+
return L(d, n.body);
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
await S.seedWallet(d.tenantId, f);
|
|
227
|
+
let o = (BigInt(n) - BigInt(T)).toString(), u = [];
|
|
228
|
+
if (o !== "0") {
|
|
229
|
+
let e = await h.runPromise(h.result(S.prepareTransfer(c, {
|
|
230
|
+
tenantId: d.tenantId,
|
|
231
|
+
requestId: O(d, "close"),
|
|
232
|
+
currency: d.currency,
|
|
233
|
+
amount: o,
|
|
234
|
+
from: {
|
|
235
|
+
kind: "system",
|
|
236
|
+
id: A(d)
|
|
237
|
+
},
|
|
238
|
+
to: f,
|
|
239
|
+
reason: r === "release" ? "escrow.release" : "escrow.settle",
|
|
240
|
+
ref: {
|
|
241
|
+
kind: "economy.escrow",
|
|
242
|
+
id: d.reservationId
|
|
243
|
+
}
|
|
244
|
+
})));
|
|
245
|
+
if (g.isFailure(e)) throw e.failure;
|
|
246
|
+
u.push(e.success);
|
|
247
|
+
}
|
|
248
|
+
if (T !== "0") {
|
|
249
|
+
let e = await h.runPromise(h.result(S.prepareTransfer(c, {
|
|
250
|
+
tenantId: d.tenantId,
|
|
251
|
+
requestId: O(d, "tax"),
|
|
252
|
+
currency: d.currency,
|
|
253
|
+
amount: T,
|
|
254
|
+
from: {
|
|
255
|
+
kind: "system",
|
|
256
|
+
id: A(d)
|
|
257
|
+
},
|
|
258
|
+
to: {
|
|
259
|
+
kind: "system",
|
|
260
|
+
id: F
|
|
261
|
+
},
|
|
262
|
+
reason: "escrow.tax",
|
|
263
|
+
ref: {
|
|
264
|
+
kind: "economy.escrow",
|
|
265
|
+
id: d.reservationId
|
|
266
|
+
}
|
|
267
|
+
})));
|
|
268
|
+
if (g.isFailure(e)) throw e.failure;
|
|
269
|
+
u.push(e.success);
|
|
270
|
+
}
|
|
271
|
+
let p = y(x, async (e) => {
|
|
272
|
+
await z(e, d, f);
|
|
273
|
+
let r = await R(e, d);
|
|
274
|
+
if (!r || r.body.intent.amount !== n || r.body.close && !B(r.body)) throw t("conflict");
|
|
275
|
+
});
|
|
276
|
+
return b(x, [p, ...u], async (e, [, ...i]) => {
|
|
277
|
+
await M(e, d);
|
|
278
|
+
let s = (await e.query(v(l).where(_("id", k(d, "reserve"))).limit(1).descriptor))[0];
|
|
279
|
+
if (!s) throw t("invalid_record");
|
|
280
|
+
let c = N(s), u = S.decodeReceipt(s);
|
|
281
|
+
if (!j(c.intent, d) || c.intent.amount !== n) throw t("invalid_record");
|
|
282
|
+
await P(e, d, "reserve", c.intent.amount, u);
|
|
283
|
+
let f = i.find((e) => e.txId === k(d, "close")) ?? null, p = i.find((e) => e.txId === k(d, "tax")) ?? null;
|
|
284
|
+
if ((o === "0" ? f !== null : !f || f.amount !== o || f.currency !== d.currency) || (T === "0" ? p !== null : !p || p.amount !== T || p.currency !== d.currency)) throw t("invalid_record");
|
|
285
|
+
let m = Object.freeze({
|
|
286
|
+
mode: r,
|
|
287
|
+
targetKey: C,
|
|
288
|
+
receipt: f,
|
|
289
|
+
tax: p ? Object.freeze({
|
|
290
|
+
amount: T,
|
|
291
|
+
account: F,
|
|
292
|
+
receipt: p
|
|
293
|
+
}) : null
|
|
294
|
+
});
|
|
295
|
+
if (c.close) {
|
|
296
|
+
if (!B(c) || a(c.close) !== a(m)) throw t("conflict");
|
|
297
|
+
} else await e.update(l.tableName, k(d, "reserve"), { escrow: Object.freeze({
|
|
298
|
+
intent: c.intent,
|
|
299
|
+
close: m
|
|
300
|
+
}) });
|
|
301
|
+
let h = {
|
|
302
|
+
intent: c.intent,
|
|
303
|
+
close: m
|
|
304
|
+
};
|
|
305
|
+
return await I(e, d, h), L(d, h);
|
|
306
|
+
});
|
|
307
|
+
});
|
|
308
|
+
}), H = (e, n, r) => h.flatMap(V(e, n, r, !1), (e) => h.sync(() => b(x, [e], async (e, [n]) => {
|
|
309
|
+
if (!n.payout || n.tax !== "0") throw t("invalid_record");
|
|
310
|
+
return n.payout;
|
|
311
|
+
}))), U = (e, t) => T(() => {
|
|
312
|
+
let n = structuredClone(e), r = f(t);
|
|
313
|
+
E(n);
|
|
314
|
+
let i = Object.freeze({
|
|
315
|
+
tenantId: r.tenantId,
|
|
316
|
+
owner: r.owner,
|
|
317
|
+
reservationId: r.reservationId,
|
|
318
|
+
currency: r.currency,
|
|
319
|
+
amount: r.amount
|
|
320
|
+
}), a = Object.freeze({
|
|
321
|
+
tenantId: r.tenantId,
|
|
322
|
+
owner: r.owner,
|
|
323
|
+
reservationId: r.reservationId,
|
|
324
|
+
currency: r.currency,
|
|
325
|
+
to: r.to,
|
|
326
|
+
taxAmount: r.taxAmount,
|
|
327
|
+
taxAccountId: r.taxAccountId
|
|
328
|
+
});
|
|
329
|
+
return h.flatMap(B(n, i), (e) => h.map(V(n, a, "settle", !0, r.amount), (t) => b(x, [e, t], async (e, [t, n]) => Object.freeze({
|
|
330
|
+
reserve: t,
|
|
331
|
+
settlement: n
|
|
332
|
+
}))));
|
|
333
|
+
}), W = (e, t) => T(() => {
|
|
334
|
+
let n = structuredClone(e), r = u(t);
|
|
335
|
+
return E(n), w(() => x.transactional(async (e) => {
|
|
336
|
+
let t = await R(e, r);
|
|
337
|
+
return Object.freeze(t ? {
|
|
338
|
+
reservationId: r.reservationId,
|
|
339
|
+
currency: r.currency,
|
|
340
|
+
amount: t.body.intent.amount,
|
|
341
|
+
state: t.body.close?.mode === "release" ? "released" : t.body.close ? "settled" : "held",
|
|
342
|
+
receipt: t.receipt,
|
|
343
|
+
closingReceipt: t.body.close?.receipt ?? null,
|
|
344
|
+
taxReceipt: t.body.close?.tax?.receipt ?? null
|
|
345
|
+
} : {
|
|
346
|
+
reservationId: r.reservationId,
|
|
347
|
+
currency: r.currency,
|
|
348
|
+
state: "not-observed"
|
|
349
|
+
});
|
|
350
|
+
}));
|
|
351
|
+
}), G = async (e, n, r, i) => {
|
|
352
|
+
let a = [];
|
|
353
|
+
for (let o of i) {
|
|
354
|
+
let i = (await e.query(v(l).where(_("id", o)).limit(1).descriptor))[0];
|
|
355
|
+
if (!i || i.tenantId !== n) throw t("invalid_record");
|
|
356
|
+
if (i.escrow === null) continue;
|
|
357
|
+
let c = N(i);
|
|
358
|
+
if (c.intent.tenantId !== n || s(c.intent.owner) !== s(r)) throw t("invalid_record");
|
|
359
|
+
c.close?.tax && (await F(e, c.intent, c.close.tax), a.push(c.close.tax.receipt.txId));
|
|
360
|
+
}
|
|
361
|
+
return Object.freeze([...new Set(a)]);
|
|
362
|
+
}, K = Object.freeze({
|
|
363
|
+
prepareEscrowReserve: B,
|
|
364
|
+
prepareEscrowReserveTaxSettle: U,
|
|
365
|
+
prepareEscrowRelease: (e, t) => H(e, t, "release"),
|
|
366
|
+
prepareEscrowSettle: (e, t) => H(e, t, "settle"),
|
|
367
|
+
prepareEscrowTaxSettle: (e, t) => V(e, t, "settle", !0),
|
|
368
|
+
escrow: W
|
|
369
|
+
});
|
|
370
|
+
return Object.freeze({
|
|
371
|
+
...K,
|
|
372
|
+
taxTransactionIds: G
|
|
373
|
+
});
|
|
374
|
+
};
|
|
375
|
+
export { x as walletEscrowParticipant };
|