@plasius/economy 0.2.0 → 0.3.1
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 +71 -6
- package/dist/index.cjs +1425 -157
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +429 -21
- package/dist/index.d.ts +429 -21
- package/dist/index.js +1392 -157
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -25,20 +25,31 @@ __export(index_exports, {
|
|
|
25
25
|
BASELINE_GBP_TOKEN_PACKS: () => BASELINE_GBP_TOKEN_PACKS,
|
|
26
26
|
BASELINE_MONTHLY_SUBSCRIPTION_PLAN: () => BASELINE_MONTHLY_SUBSCRIPTION_PLAN,
|
|
27
27
|
BASELINE_PURCHASE_LIMIT_POLICY: () => BASELINE_PURCHASE_LIMIT_POLICY,
|
|
28
|
+
EARLY_BACKER_SETTLEMENT_POLICY_V2: () => EARLY_BACKER_SETTLEMENT_POLICY_V2,
|
|
28
29
|
ECONOMY_CONTRACT_VERSION: () => ECONOMY_CONTRACT_VERSION,
|
|
29
30
|
EconomyError: () => EconomyError,
|
|
30
31
|
PRE_UTILITY_BACKER_COHORT: () => PRE_UTILITY_BACKER_COHORT,
|
|
31
32
|
TOKEN_SUBUNITS_MAX: () => TOKEN_SUBUNITS_MAX,
|
|
32
33
|
TOKEN_SUBUNITS_MIN: () => TOKEN_SUBUNITS_MIN,
|
|
33
34
|
TOKEN_SUBUNITS_PER_TOKEN: () => TOKEN_SUBUNITS_PER_TOKEN,
|
|
35
|
+
advanceJournalChainHead: () => advanceJournalChainHead,
|
|
36
|
+
applySourceLotMovement: () => applySourceLotMovement,
|
|
34
37
|
applyTransactionToProjection: () => applyTransactionToProjection,
|
|
38
|
+
applyWalletBalanceDelta: () => applyWalletBalanceDelta,
|
|
39
|
+
applyWalletLifetimeDeltas: () => applyWalletLifetimeDeltas,
|
|
35
40
|
assertActivityEntry: () => assertActivityEntry,
|
|
36
41
|
assertBalancedTransaction: () => assertBalancedTransaction,
|
|
42
|
+
assertDeterministicWalletBalanceSummary: () => assertDeterministicWalletBalanceSummary,
|
|
37
43
|
assertDistinctAdjustmentApproval: () => assertDistinctAdjustmentApproval,
|
|
44
|
+
assertEconomicJournalTransaction: () => assertEconomicJournalTransaction,
|
|
45
|
+
assertEconomyCommandEnvelope: () => assertEconomyCommandEnvelope,
|
|
46
|
+
assertEconomyCommandWorkflowEvent: () => assertEconomyCommandWorkflowEvent,
|
|
47
|
+
assertEconomyIdempotencyScope: () => assertEconomyIdempotencyScope,
|
|
38
48
|
assertEconomyIdentifier: () => assertEconomyIdentifier,
|
|
39
49
|
assertFlatTokenCatalog: () => assertFlatTokenCatalog,
|
|
40
50
|
assertFxSnapshot: () => assertFxSnapshot,
|
|
41
51
|
assertGameplayAllocation: () => assertGameplayAllocation,
|
|
52
|
+
assertJournalChainHead: () => assertJournalChainHead,
|
|
42
53
|
assertNonNegativeAccounts: () => assertNonNegativeAccounts,
|
|
43
54
|
assertOpenPurchaseIntent: () => assertOpenPurchaseIntent,
|
|
44
55
|
assertPurchaseIntentBinding: () => assertPurchaseIntentBinding,
|
|
@@ -46,24 +57,46 @@ __export(index_exports, {
|
|
|
46
57
|
assertReversalAvailable: () => assertReversalAvailable,
|
|
47
58
|
assertRewardConversion: () => assertRewardConversion,
|
|
48
59
|
assertRewardRate: () => assertRewardRate,
|
|
60
|
+
assertScopedPersistedIdempotencyResult: () => assertScopedPersistedIdempotencyResult,
|
|
49
61
|
assertSourceLot: () => assertSourceLot,
|
|
62
|
+
assertSourceLotMovement: () => assertSourceLotMovement,
|
|
50
63
|
assertSpendRequest: () => assertSpendRequest,
|
|
51
64
|
assertTokenPack: () => assertTokenPack,
|
|
52
65
|
assertTokenReferenceRate: () => assertTokenReferenceRate,
|
|
53
66
|
assertTokenSubscriptionPlan: () => assertTokenSubscriptionPlan,
|
|
67
|
+
assertVersionedSourceLot: () => assertVersionedSourceLot,
|
|
54
68
|
assertWallet: () => assertWallet,
|
|
69
|
+
assertWalletActivityEntry: () => assertWalletActivityEntry,
|
|
70
|
+
assertWalletActivityPage: () => assertWalletActivityPage,
|
|
71
|
+
assertWalletActivityPageForPortfolio: () => assertWalletActivityPageForPortfolio,
|
|
72
|
+
assertWalletActivityPageRequest: () => assertWalletActivityPageRequest,
|
|
73
|
+
assertWalletBalanceDelta: () => assertWalletBalanceDelta,
|
|
74
|
+
assertWalletBalanceDeltaBatch: () => assertWalletBalanceDeltaBatch,
|
|
75
|
+
assertWalletBalanceProjection: () => assertWalletBalanceProjection,
|
|
55
76
|
assertWalletBalanceSummary: () => assertWalletBalanceSummary,
|
|
77
|
+
assertWalletLifetimeSnapshot: () => assertWalletLifetimeSnapshot,
|
|
56
78
|
assertWalletLifetimeTotals: () => assertWalletLifetimeTotals,
|
|
79
|
+
assertWalletOwnerReference: () => assertWalletOwnerReference,
|
|
80
|
+
assertWalletPortfolioLifetime: () => assertWalletPortfolioLifetime,
|
|
81
|
+
assertWalletPortfolioReadScope: () => assertWalletPortfolioReadScope,
|
|
82
|
+
assertWalletPortfolioSummary: () => assertWalletPortfolioSummary,
|
|
57
83
|
availableSourceLotAmount: () => availableSourceLotAmount,
|
|
58
84
|
boostGameplayAllocation: () => boostGameplayAllocation,
|
|
59
85
|
canUseSourceLot: () => canUseSourceLot,
|
|
60
86
|
canonicalTransactionPayload: () => canonicalTransactionPayload,
|
|
61
87
|
convertRewardPayout: () => convertRewardPayout,
|
|
62
88
|
createGameplayAllocation: () => createGameplayAllocation,
|
|
89
|
+
createInitialSourceLotSnapshot: () => createInitialSourceLotSnapshot,
|
|
63
90
|
createProjectionSnapshot: () => createProjectionSnapshot,
|
|
64
91
|
createReversalTransaction: () => createReversalTransaction,
|
|
92
|
+
createWalletBalanceSummary: () => createWalletBalanceSummary,
|
|
93
|
+
createWalletPortfolioLifetime: () => createWalletPortfolioLifetime,
|
|
94
|
+
createWalletPortfolioSummary: () => createWalletPortfolioSummary,
|
|
95
|
+
deriveWalletBalanceDeltas: () => deriveWalletBalanceDeltas,
|
|
96
|
+
deriveWalletLifetimeDeltas: () => deriveWalletLifetimeDeltas,
|
|
65
97
|
economyAssert: () => economyAssert,
|
|
66
98
|
evaluateEarlyBacker: () => evaluateEarlyBacker,
|
|
99
|
+
evaluateEarlyBackerBySettlementV2: () => evaluateEarlyBackerBySettlementV2,
|
|
67
100
|
isWholeTokenAmount: () => isWholeTokenAmount,
|
|
68
101
|
mapBitLabsConversionState: () => mapBitLabsConversionState,
|
|
69
102
|
parseIsoTimestamp: () => parseIsoTimestamp,
|
|
@@ -163,6 +196,14 @@ function sumTokenSubunits(values) {
|
|
|
163
196
|
return total;
|
|
164
197
|
}
|
|
165
198
|
|
|
199
|
+
// src/canonical-order.ts
|
|
200
|
+
function compareUnicodeCodeUnits(left, right) {
|
|
201
|
+
if (left === right) {
|
|
202
|
+
return 0;
|
|
203
|
+
}
|
|
204
|
+
return left < right ? -1 : 1;
|
|
205
|
+
}
|
|
206
|
+
|
|
166
207
|
// src/contracts.ts
|
|
167
208
|
var ECONOMY_CONTRACT_VERSION = "1";
|
|
168
209
|
var IDENTIFIER = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/u;
|
|
@@ -199,7 +240,9 @@ function parseIsoTimestamp(value) {
|
|
|
199
240
|
}
|
|
200
241
|
function sortStringRecord(value) {
|
|
201
242
|
return Object.fromEntries(
|
|
202
|
-
Object.entries(value).sort(
|
|
243
|
+
Object.entries(value).sort(
|
|
244
|
+
([left], [right]) => compareUnicodeCodeUnits(left, right)
|
|
245
|
+
)
|
|
203
246
|
);
|
|
204
247
|
}
|
|
205
248
|
|
|
@@ -803,6 +846,7 @@ function reclaimGameplayAllocation(allocation, input) {
|
|
|
803
846
|
|
|
804
847
|
// src/early-backers.ts
|
|
805
848
|
var PRE_UTILITY_BACKER_COHORT = "pre_utility_backer_v1";
|
|
849
|
+
var EARLY_BACKER_SETTLEMENT_POLICY_V2 = "settlement-window-v2";
|
|
806
850
|
function isWithinWindow(timestamp, launch, cutoff) {
|
|
807
851
|
const value = parseIsoTimestamp(timestamp);
|
|
808
852
|
return value >= launch && (cutoff === void 0 || value < cutoff);
|
|
@@ -874,6 +918,71 @@ function evaluateEarlyBacker(payerAccountId, receivingHouseholdId, paidLots, win
|
|
|
874
918
|
evaluatedAt
|
|
875
919
|
};
|
|
876
920
|
}
|
|
921
|
+
function evaluateEarlyBackerBySettlementV2(payerAccountId, receivingHouseholdId, paidLots, window, evaluatedAt) {
|
|
922
|
+
assertEconomyIdentifier(payerAccountId, "payerAccountId");
|
|
923
|
+
assertEconomyIdentifier(receivingHouseholdId, "receivingHouseholdId");
|
|
924
|
+
const launch = parseIsoTimestamp(window.publicTokensLaunchAt);
|
|
925
|
+
const cutoff = window.firstPublicSpendLiveAt === void 0 ? void 0 : parseIsoTimestamp(window.firstPublicSpendLiveAt);
|
|
926
|
+
economyAssert(
|
|
927
|
+
cutoff === void 0 || cutoff > launch,
|
|
928
|
+
"INVALID_TIME_WINDOW",
|
|
929
|
+
"First public spend must occur after public Token launch"
|
|
930
|
+
);
|
|
931
|
+
const evaluationTime = parseIsoTimestamp(evaluatedAt);
|
|
932
|
+
let retained = 0n;
|
|
933
|
+
const contributingLotIds = [];
|
|
934
|
+
const seenLotIds = /* @__PURE__ */ new Set();
|
|
935
|
+
for (const lot of paidLots) {
|
|
936
|
+
economyAssert(
|
|
937
|
+
lot.schemaVersion === ECONOMY_CONTRACT_VERSION,
|
|
938
|
+
"INVALID_CONTRACT",
|
|
939
|
+
"Unsupported paid-lot retention contract version"
|
|
940
|
+
);
|
|
941
|
+
assertEconomyIdentifier(lot.lotId, "lotId");
|
|
942
|
+
economyAssert(
|
|
943
|
+
!seenLotIds.has(lot.lotId),
|
|
944
|
+
"DUPLICATE_IDENTIFIER",
|
|
945
|
+
"Paid-lot retention input must not repeat a source lot"
|
|
946
|
+
);
|
|
947
|
+
seenLotIds.add(lot.lotId);
|
|
948
|
+
assertEconomyIdentifier(lot.payerAccountId, "payerAccountId");
|
|
949
|
+
assertEconomyIdentifier(lot.receivingHouseholdId, "receivingHouseholdId");
|
|
950
|
+
assertEconomyIdentifier(lot.purchaseId, "purchaseId");
|
|
951
|
+
assertEconomyIdentifier(lot.catalogVersion, "catalogVersion");
|
|
952
|
+
const purchasedAt = parseIsoTimestamp(lot.purchasedAt);
|
|
953
|
+
const settledAt = parseIsoTimestamp(lot.settledAt);
|
|
954
|
+
const creditedAt = parseIsoTimestamp(lot.creditedAt);
|
|
955
|
+
economyAssert(
|
|
956
|
+
purchasedAt <= settledAt && settledAt <= creditedAt,
|
|
957
|
+
"INVALID_TIME_WINDOW",
|
|
958
|
+
"Paid-lot timestamps must follow purchase, settlement, then credit order"
|
|
959
|
+
);
|
|
960
|
+
const amount = parseTokenSubunits(lot.retainedAmount);
|
|
961
|
+
economyAssert(
|
|
962
|
+
amount >= 0n,
|
|
963
|
+
"INVALID_AMOUNT",
|
|
964
|
+
"Retained paid-lot basis cannot be negative"
|
|
965
|
+
);
|
|
966
|
+
if (lot.payerAccountId === payerAccountId && lot.receivingHouseholdId === receivingHouseholdId && settledAt >= launch && (cutoff === void 0 || settledAt < cutoff) && settledAt <= evaluationTime && amount > 0n) {
|
|
967
|
+
retained += amount;
|
|
968
|
+
contributingLotIds.push(lot.lotId);
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
return {
|
|
972
|
+
schemaVersion: ECONOMY_CONTRACT_VERSION,
|
|
973
|
+
policyVersion: EARLY_BACKER_SETTLEMENT_POLICY_V2,
|
|
974
|
+
qualificationEvent: "settled-at",
|
|
975
|
+
cohortKey: PRE_UTILITY_BACKER_COHORT,
|
|
976
|
+
status: retained > 0n ? "provisional" : "not-qualified",
|
|
977
|
+
payerAccountId,
|
|
978
|
+
receivingHouseholdId,
|
|
979
|
+
netRetainedAmount: serializeTokenSubunits(retained),
|
|
980
|
+
contributingLotIds: contributingLotIds.sort(
|
|
981
|
+
(left, right) => left.localeCompare(right)
|
|
982
|
+
),
|
|
983
|
+
evaluatedAt
|
|
984
|
+
};
|
|
985
|
+
}
|
|
877
986
|
|
|
878
987
|
// src/ledger.ts
|
|
879
988
|
var ACTIVITY_TYPES = /* @__PURE__ */ new Set([
|
|
@@ -1053,9 +1162,19 @@ function assertBalancedTransaction(transaction) {
|
|
|
1053
1162
|
"Ledger postings must sum to zero TokenSubunits"
|
|
1054
1163
|
);
|
|
1055
1164
|
}
|
|
1165
|
+
function assertEconomicJournalTransaction(transaction) {
|
|
1166
|
+
assertBalancedTransaction(transaction);
|
|
1167
|
+
economyAssert(
|
|
1168
|
+
transaction.status === "held" || transaction.status === "settled",
|
|
1169
|
+
"INVALID_CONTRACT",
|
|
1170
|
+
"Pending, failed, and derived reversed activity cannot be journal transactions"
|
|
1171
|
+
);
|
|
1172
|
+
}
|
|
1056
1173
|
function canonicalTransactionPayload(transaction) {
|
|
1057
1174
|
assertBalancedTransaction(transaction);
|
|
1058
|
-
const postings = [...transaction.postings].sort(
|
|
1175
|
+
const postings = [...transaction.postings].sort(
|
|
1176
|
+
(left, right) => compareUnicodeCodeUnits(left.postingId, right.postingId)
|
|
1177
|
+
).map((posting) => ({
|
|
1059
1178
|
schemaVersion: posting.schemaVersion,
|
|
1060
1179
|
postingId: posting.postingId,
|
|
1061
1180
|
transactionId: posting.transactionId,
|
|
@@ -1132,6 +1251,71 @@ function assertReversalAvailable(originalTransactionId, existingTransactions) {
|
|
|
1132
1251
|
);
|
|
1133
1252
|
}
|
|
1134
1253
|
|
|
1254
|
+
// src/integrity.ts
|
|
1255
|
+
function assertCanonicalHash2(value, label) {
|
|
1256
|
+
economyAssert(
|
|
1257
|
+
typeof value === "string" && /^sha256:[a-f0-9]{64}$/u.test(value),
|
|
1258
|
+
"INVALID_CONTRACT",
|
|
1259
|
+
`${label} must be a canonical SHA-256 reference`
|
|
1260
|
+
);
|
|
1261
|
+
}
|
|
1262
|
+
function assertJournalChainHead(head) {
|
|
1263
|
+
economyAssert(
|
|
1264
|
+
head.schemaVersion === ECONOMY_CONTRACT_VERSION,
|
|
1265
|
+
"INVALID_CONTRACT",
|
|
1266
|
+
"Unsupported journal-chain-head contract version"
|
|
1267
|
+
);
|
|
1268
|
+
assertEconomyIdentifier(head.chainId, "chainId");
|
|
1269
|
+
economyAssert(
|
|
1270
|
+
Number.isSafeInteger(head.version) && head.version >= 0,
|
|
1271
|
+
"INVALID_CONTRACT",
|
|
1272
|
+
"Journal-chain-head version must be a non-negative safe integer"
|
|
1273
|
+
);
|
|
1274
|
+
parseIsoTimestamp(head.updatedAt);
|
|
1275
|
+
const hasTransaction = head.lastTransactionId !== void 0;
|
|
1276
|
+
const hasHash = head.canonicalHash !== void 0;
|
|
1277
|
+
economyAssert(
|
|
1278
|
+
hasTransaction === hasHash && head.version > 0 === hasTransaction,
|
|
1279
|
+
"INVALID_CONTRACT",
|
|
1280
|
+
"Journal-chain head identity, hash, and version must describe the same state"
|
|
1281
|
+
);
|
|
1282
|
+
if (head.lastTransactionId !== void 0) {
|
|
1283
|
+
assertEconomyIdentifier(head.lastTransactionId, "lastTransactionId");
|
|
1284
|
+
}
|
|
1285
|
+
if (head.canonicalHash !== void 0) {
|
|
1286
|
+
assertCanonicalHash2(head.canonicalHash, "Journal-chain-head hash");
|
|
1287
|
+
}
|
|
1288
|
+
}
|
|
1289
|
+
function advanceJournalChainHead(head, transaction) {
|
|
1290
|
+
assertJournalChainHead(head);
|
|
1291
|
+
assertEconomicJournalTransaction(transaction);
|
|
1292
|
+
assertCanonicalHash2(transaction.canonicalHash, "Transaction hash");
|
|
1293
|
+
economyAssert(
|
|
1294
|
+
transaction.previousCanonicalHash === head.canonicalHash,
|
|
1295
|
+
"INVALID_CONTRACT",
|
|
1296
|
+
"Transaction previous hash must match the locked journal-chain head"
|
|
1297
|
+
);
|
|
1298
|
+
economyAssert(
|
|
1299
|
+
transaction.canonicalHash !== head.canonicalHash,
|
|
1300
|
+
"DUPLICATE_TRANSACTION",
|
|
1301
|
+
"Transaction hash must advance the journal chain"
|
|
1302
|
+
);
|
|
1303
|
+
const recordedAt = parseIsoTimestamp(transaction.recordedAt);
|
|
1304
|
+
economyAssert(
|
|
1305
|
+
recordedAt >= parseIsoTimestamp(head.updatedAt),
|
|
1306
|
+
"INVALID_TIME_WINDOW",
|
|
1307
|
+
"Journal-chain update cannot precede its locked head"
|
|
1308
|
+
);
|
|
1309
|
+
return {
|
|
1310
|
+
schemaVersion: ECONOMY_CONTRACT_VERSION,
|
|
1311
|
+
chainId: head.chainId,
|
|
1312
|
+
version: head.version + 1,
|
|
1313
|
+
lastTransactionId: transaction.transactionId,
|
|
1314
|
+
canonicalHash: transaction.canonicalHash,
|
|
1315
|
+
updatedAt: transaction.recordedAt
|
|
1316
|
+
};
|
|
1317
|
+
}
|
|
1318
|
+
|
|
1135
1319
|
// src/lots.ts
|
|
1136
1320
|
function assertSourceLot(lot) {
|
|
1137
1321
|
economyAssert(
|
|
@@ -1228,6 +1412,199 @@ function assertSourceLot(lot) {
|
|
|
1228
1412
|
"Remaining and reversed source-lot amounts exceed the original"
|
|
1229
1413
|
);
|
|
1230
1414
|
}
|
|
1415
|
+
function assertVersionedSourceLot(snapshot) {
|
|
1416
|
+
economyAssert(
|
|
1417
|
+
snapshot.schemaVersion === ECONOMY_CONTRACT_VERSION,
|
|
1418
|
+
"INVALID_CONTRACT",
|
|
1419
|
+
"Unsupported versioned source-lot contract version"
|
|
1420
|
+
);
|
|
1421
|
+
assertSourceLot(snapshot.lot);
|
|
1422
|
+
economyAssert(
|
|
1423
|
+
Number.isSafeInteger(snapshot.version) && snapshot.version >= 1,
|
|
1424
|
+
"INVALID_CONTRACT",
|
|
1425
|
+
"Source-lot version must be a positive safe integer"
|
|
1426
|
+
);
|
|
1427
|
+
economyAssert(
|
|
1428
|
+
parseIsoTimestamp(snapshot.updatedAt) >= parseIsoTimestamp(snapshot.lot.creditedAt),
|
|
1429
|
+
"INVALID_TIME_WINDOW",
|
|
1430
|
+
"Source-lot update time cannot precede its credit"
|
|
1431
|
+
);
|
|
1432
|
+
}
|
|
1433
|
+
function createInitialSourceLotSnapshot(lot) {
|
|
1434
|
+
assertSourceLot(lot);
|
|
1435
|
+
economyAssert(
|
|
1436
|
+
lot.refundState === "none" && lot.originalAmount === lot.remainingAmount && parseTokenSubunits(lot.heldAmount) === 0n && parseTokenSubunits(lot.reversedAmount) === 0n,
|
|
1437
|
+
"INVALID_CONTRACT",
|
|
1438
|
+
"A new source lot must start fully remaining, clear, and unreversed"
|
|
1439
|
+
);
|
|
1440
|
+
return {
|
|
1441
|
+
schemaVersion: ECONOMY_CONTRACT_VERSION,
|
|
1442
|
+
lot,
|
|
1443
|
+
version: 1,
|
|
1444
|
+
updatedAt: lot.creditedAt
|
|
1445
|
+
};
|
|
1446
|
+
}
|
|
1447
|
+
function assertRefundState(value) {
|
|
1448
|
+
economyAssert(
|
|
1449
|
+
[
|
|
1450
|
+
"none",
|
|
1451
|
+
"partial",
|
|
1452
|
+
"refunded",
|
|
1453
|
+
"disputed",
|
|
1454
|
+
"chargeback-lost"
|
|
1455
|
+
].includes(value),
|
|
1456
|
+
"INVALID_CONTRACT",
|
|
1457
|
+
"Source-lot movement has an unsupported refund state"
|
|
1458
|
+
);
|
|
1459
|
+
}
|
|
1460
|
+
function assertSourceLotMovement(movement) {
|
|
1461
|
+
economyAssert(
|
|
1462
|
+
movement.schemaVersion === ECONOMY_CONTRACT_VERSION,
|
|
1463
|
+
"INVALID_CONTRACT",
|
|
1464
|
+
"Unsupported source-lot-movement contract version"
|
|
1465
|
+
);
|
|
1466
|
+
assertEconomyIdentifier(movement.movementId, "movementId");
|
|
1467
|
+
assertEconomyIdentifier(movement.transactionId, "transactionId");
|
|
1468
|
+
assertEconomyIdentifier(movement.lotId, "lotId");
|
|
1469
|
+
economyAssert(
|
|
1470
|
+
[
|
|
1471
|
+
"allocate",
|
|
1472
|
+
"boost",
|
|
1473
|
+
"reclaim",
|
|
1474
|
+
"spend",
|
|
1475
|
+
"hold",
|
|
1476
|
+
"release-hold",
|
|
1477
|
+
"refund",
|
|
1478
|
+
"chargeback",
|
|
1479
|
+
"reversal"
|
|
1480
|
+
].includes(movement.movementType),
|
|
1481
|
+
"INVALID_CONTRACT",
|
|
1482
|
+
"Source-lot movement has an unsupported type"
|
|
1483
|
+
);
|
|
1484
|
+
const remaining = parseTokenSubunits(movement.remainingDelta);
|
|
1485
|
+
const held = parseTokenSubunits(movement.heldDelta);
|
|
1486
|
+
const reversed = parseTokenSubunits(movement.reversedDelta);
|
|
1487
|
+
economyAssert(
|
|
1488
|
+
remaining !== 0n || held !== 0n || reversed !== 0n,
|
|
1489
|
+
"INVALID_AMOUNT",
|
|
1490
|
+
"Source-lot movement must change at least one amount"
|
|
1491
|
+
);
|
|
1492
|
+
economyAssert(
|
|
1493
|
+
Number.isSafeInteger(movement.expectedVersion) && movement.expectedVersion >= 1 && movement.resultingVersion === movement.expectedVersion + 1,
|
|
1494
|
+
"INVALID_CONTRACT",
|
|
1495
|
+
"Source-lot movement must advance its optimistic version exactly once"
|
|
1496
|
+
);
|
|
1497
|
+
assertRefundState(movement.expectedRefundState);
|
|
1498
|
+
assertRefundState(movement.resultingRefundState);
|
|
1499
|
+
parseIsoTimestamp(movement.occurredAt);
|
|
1500
|
+
if (movement.movementType === "allocate" || movement.movementType === "boost" || movement.movementType === "spend") {
|
|
1501
|
+
economyAssert(
|
|
1502
|
+
remaining < 0n && held === 0n && reversed === 0n && movement.resultingRefundState === movement.expectedRefundState,
|
|
1503
|
+
"INVALID_CONTRACT",
|
|
1504
|
+
"Allocation, boost, and spend movements only consume remaining lot value"
|
|
1505
|
+
);
|
|
1506
|
+
} else if (movement.movementType === "reclaim") {
|
|
1507
|
+
economyAssert(
|
|
1508
|
+
remaining > 0n && held === 0n && reversed === 0n && movement.resultingRefundState === movement.expectedRefundState,
|
|
1509
|
+
"INVALID_CONTRACT",
|
|
1510
|
+
"Reclaim movements only restore remaining lot value"
|
|
1511
|
+
);
|
|
1512
|
+
} else if (movement.movementType === "hold") {
|
|
1513
|
+
economyAssert(
|
|
1514
|
+
remaining === 0n && held > 0n && reversed === 0n && movement.resultingRefundState === "disputed",
|
|
1515
|
+
"INVALID_CONTRACT",
|
|
1516
|
+
"Hold movements must enter disputed state and increase held value"
|
|
1517
|
+
);
|
|
1518
|
+
} else if (movement.movementType === "release-hold") {
|
|
1519
|
+
economyAssert(
|
|
1520
|
+
remaining === 0n && held < 0n && reversed === 0n && movement.expectedRefundState === "disputed" && movement.resultingRefundState !== "disputed" && movement.resultingRefundState !== "refunded" && movement.resultingRefundState !== "chargeback-lost",
|
|
1521
|
+
"INVALID_CONTRACT",
|
|
1522
|
+
"Released holds must leave disputed state without reversing value"
|
|
1523
|
+
);
|
|
1524
|
+
} else {
|
|
1525
|
+
economyAssert(
|
|
1526
|
+
remaining < 0n && reversed === -remaining && held <= 0n && (movement.resultingRefundState === "partial" || movement.resultingRefundState === "refunded" || movement.resultingRefundState === "chargeback-lost"),
|
|
1527
|
+
"INVALID_CONTRACT",
|
|
1528
|
+
"Refund, chargeback, and reversal movements must compensate remaining value exactly"
|
|
1529
|
+
);
|
|
1530
|
+
if (movement.movementType === "chargeback") {
|
|
1531
|
+
economyAssert(
|
|
1532
|
+
movement.resultingRefundState === "chargeback-lost",
|
|
1533
|
+
"INVALID_CONTRACT",
|
|
1534
|
+
"Chargeback movements must enter chargeback-lost state"
|
|
1535
|
+
);
|
|
1536
|
+
} else {
|
|
1537
|
+
economyAssert(
|
|
1538
|
+
movement.resultingRefundState !== "chargeback-lost",
|
|
1539
|
+
"INVALID_CONTRACT",
|
|
1540
|
+
"Only a chargeback movement may enter chargeback-lost state"
|
|
1541
|
+
);
|
|
1542
|
+
}
|
|
1543
|
+
}
|
|
1544
|
+
}
|
|
1545
|
+
function applySourceLotMovement(snapshot, movement) {
|
|
1546
|
+
assertVersionedSourceLot(snapshot);
|
|
1547
|
+
assertSourceLotMovement(movement);
|
|
1548
|
+
economyAssert(
|
|
1549
|
+
movement.lotId === snapshot.lot.lotId && movement.expectedVersion === snapshot.version && movement.expectedRefundState === snapshot.lot.refundState,
|
|
1550
|
+
"INVALID_CONTRACT",
|
|
1551
|
+
"Source-lot movement does not match the locked lot version and refund state"
|
|
1552
|
+
);
|
|
1553
|
+
economyAssert(
|
|
1554
|
+
parseIsoTimestamp(movement.occurredAt) >= parseIsoTimestamp(snapshot.updatedAt),
|
|
1555
|
+
"INVALID_TIME_WINDOW",
|
|
1556
|
+
"Source-lot movement cannot precede its current projection"
|
|
1557
|
+
);
|
|
1558
|
+
const lot = {
|
|
1559
|
+
...snapshot.lot,
|
|
1560
|
+
remainingAmount: serializeTokenSubunits(
|
|
1561
|
+
parseTokenSubunits(snapshot.lot.remainingAmount) + parseTokenSubunits(movement.remainingDelta)
|
|
1562
|
+
),
|
|
1563
|
+
heldAmount: serializeTokenSubunits(
|
|
1564
|
+
parseTokenSubunits(snapshot.lot.heldAmount) + parseTokenSubunits(movement.heldDelta)
|
|
1565
|
+
),
|
|
1566
|
+
reversedAmount: serializeTokenSubunits(
|
|
1567
|
+
parseTokenSubunits(snapshot.lot.reversedAmount) + parseTokenSubunits(movement.reversedDelta)
|
|
1568
|
+
),
|
|
1569
|
+
refundState: movement.resultingRefundState
|
|
1570
|
+
};
|
|
1571
|
+
assertSourceLot(lot);
|
|
1572
|
+
const remaining = parseTokenSubunits(lot.remainingAmount);
|
|
1573
|
+
const held = parseTokenSubunits(lot.heldAmount);
|
|
1574
|
+
const reversed = parseTokenSubunits(lot.reversedAmount);
|
|
1575
|
+
const original = parseTokenSubunits(lot.originalAmount);
|
|
1576
|
+
if (lot.refundState === "none") {
|
|
1577
|
+
economyAssert(
|
|
1578
|
+
reversed === 0n && held === 0n,
|
|
1579
|
+
"INVALID_CONTRACT",
|
|
1580
|
+
"A clear lot cannot retain held or reversed value"
|
|
1581
|
+
);
|
|
1582
|
+
} else if (lot.refundState === "partial") {
|
|
1583
|
+
economyAssert(
|
|
1584
|
+
reversed > 0n && remaining > 0n && held === 0n,
|
|
1585
|
+
"INVALID_CONTRACT",
|
|
1586
|
+
"A partial refund requires retained/reversed value and no active hold"
|
|
1587
|
+
);
|
|
1588
|
+
} else if (lot.refundState === "disputed") {
|
|
1589
|
+
economyAssert(
|
|
1590
|
+
held > 0n,
|
|
1591
|
+
"INVALID_CONTRACT",
|
|
1592
|
+
"A disputed lot must hold affected value"
|
|
1593
|
+
);
|
|
1594
|
+
} else {
|
|
1595
|
+
economyAssert(
|
|
1596
|
+
remaining === 0n && held === 0n && reversed === original,
|
|
1597
|
+
"INVALID_CONTRACT",
|
|
1598
|
+
"Final refund and lost-chargeback states must reverse the full lot"
|
|
1599
|
+
);
|
|
1600
|
+
}
|
|
1601
|
+
return {
|
|
1602
|
+
schemaVersion: ECONOMY_CONTRACT_VERSION,
|
|
1603
|
+
lot,
|
|
1604
|
+
version: movement.resultingVersion,
|
|
1605
|
+
updatedAt: movement.occurredAt
|
|
1606
|
+
};
|
|
1607
|
+
}
|
|
1231
1608
|
function availableSourceLotAmount(lot) {
|
|
1232
1609
|
assertSourceLot(lot);
|
|
1233
1610
|
return parseTokenSubunits(lot.remainingAmount) - parseTokenSubunits(lot.heldAmount);
|
|
@@ -1330,193 +1707,137 @@ function assertDistinctAdjustmentApproval(request) {
|
|
|
1330
1707
|
}
|
|
1331
1708
|
}
|
|
1332
1709
|
|
|
1333
|
-
// src/
|
|
1334
|
-
function
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1710
|
+
// src/ports/persistence.ts
|
|
1711
|
+
function assertScopedPersistedIdempotencyResult(result) {
|
|
1712
|
+
economyAssert(
|
|
1713
|
+
result.schemaVersion === ECONOMY_CONTRACT_VERSION,
|
|
1714
|
+
"INVALID_CONTRACT",
|
|
1715
|
+
"Unsupported scoped idempotency-result contract version"
|
|
1716
|
+
);
|
|
1717
|
+
assertEconomyIdentifier(result.commandId, "commandId");
|
|
1718
|
+
assertEconomyIdentifier(result.transactionId, "transactionId");
|
|
1719
|
+
economyAssert(
|
|
1720
|
+
/^sha256:[a-f0-9]{64}$/u.test(result.responseHash),
|
|
1721
|
+
"INVALID_CONTRACT",
|
|
1722
|
+
"Idempotency response hash must be a canonical SHA-256 reference"
|
|
1723
|
+
);
|
|
1724
|
+
parseIsoTimestamp(result.recordedAt);
|
|
1344
1725
|
}
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1726
|
+
var ECONOMY_COMMAND_TYPES = /* @__PURE__ */ new Set([
|
|
1727
|
+
"credit-purchase",
|
|
1728
|
+
"credit-subscription",
|
|
1729
|
+
"credit-reward",
|
|
1730
|
+
"credit-event",
|
|
1731
|
+
"credit-competition",
|
|
1732
|
+
"allocate",
|
|
1733
|
+
"boost",
|
|
1734
|
+
"reclaim",
|
|
1735
|
+
"spend",
|
|
1736
|
+
"hold",
|
|
1737
|
+
"release-hold",
|
|
1738
|
+
"refund",
|
|
1739
|
+
"chargeback",
|
|
1740
|
+
"reverse",
|
|
1741
|
+
"adjust"
|
|
1742
|
+
]);
|
|
1743
|
+
function assertEconomyIdempotencyScope(scope) {
|
|
1744
|
+
economyAssert(
|
|
1745
|
+
scope.schemaVersion === ECONOMY_CONTRACT_VERSION && ECONOMY_COMMAND_TYPES.has(scope.commandType),
|
|
1746
|
+
"INVALID_CONTRACT",
|
|
1747
|
+
"Unsupported economy idempotency scope"
|
|
1748
|
+
);
|
|
1749
|
+
assertEconomyIdentifier(scope.idempotencyKey, "idempotencyKey");
|
|
1750
|
+
assertEconomyIdentifier(scope.actorAccountId, "actorAccountId");
|
|
1751
|
+
assertEconomyIdentifier(scope.subjectAccountId, "subjectAccountId");
|
|
1752
|
+
}
|
|
1753
|
+
function assertEconomyCommandEnvelope(envelope) {
|
|
1754
|
+
economyAssert(
|
|
1755
|
+
envelope.schemaVersion === ECONOMY_CONTRACT_VERSION,
|
|
1756
|
+
"INVALID_CONTRACT",
|
|
1757
|
+
"Unsupported economy-command-envelope contract version"
|
|
1758
|
+
);
|
|
1759
|
+
assertEconomyIdentifier(envelope.commandId, "commandId");
|
|
1760
|
+
economyAssert(
|
|
1761
|
+
ECONOMY_COMMAND_TYPES.has(envelope.commandType),
|
|
1762
|
+
"INVALID_CONTRACT",
|
|
1763
|
+
"Economy command has an unsupported type"
|
|
1764
|
+
);
|
|
1765
|
+
assertEconomyIdentifier(envelope.idempotencyKey, "idempotencyKey");
|
|
1766
|
+
assertEconomyIdentifier(envelope.actorAccountId, "actorAccountId");
|
|
1767
|
+
assertEconomyIdentifier(envelope.subjectAccountId, "subjectAccountId");
|
|
1768
|
+
if (envelope.relationshipId !== void 0) {
|
|
1769
|
+
assertEconomyIdentifier(envelope.relationshipId, "relationshipId");
|
|
1770
|
+
}
|
|
1771
|
+
if (envelope.authorizationVersion !== void 0) {
|
|
1356
1772
|
economyAssert(
|
|
1357
|
-
|
|
1358
|
-
"
|
|
1359
|
-
"
|
|
1773
|
+
Number.isSafeInteger(envelope.authorizationVersion) && envelope.authorizationVersion >= 1,
|
|
1774
|
+
"INVALID_CONTRACT",
|
|
1775
|
+
"Command authorization version must be a positive safe integer"
|
|
1360
1776
|
);
|
|
1361
|
-
if (transaction.providerEventId !== void 0) {
|
|
1362
|
-
economyAssert(
|
|
1363
|
-
!providerEvents.has(transaction.providerEventId),
|
|
1364
|
-
"DUPLICATE_TRANSACTION",
|
|
1365
|
-
"Provider event identifiers must be unique"
|
|
1366
|
-
);
|
|
1367
|
-
providerEvents.add(transaction.providerEventId);
|
|
1368
|
-
}
|
|
1369
|
-
transactionIds.add(transaction.transactionId);
|
|
1370
|
-
idempotencyKeys.add(transaction.idempotencyKey);
|
|
1371
|
-
projection = applyTransactionToProjection(projection, transaction);
|
|
1372
1777
|
}
|
|
1373
|
-
return projection;
|
|
1374
|
-
}
|
|
1375
|
-
function assertNonNegativeAccounts(projection, protectedAccountIds) {
|
|
1376
|
-
for (const accountId of protectedAccountIds) {
|
|
1377
|
-
economyAssert(
|
|
1378
|
-
parseTokenSubunits(projection[accountId] ?? "0") >= 0n,
|
|
1379
|
-
"NEGATIVE_PROJECTION",
|
|
1380
|
-
"A protected economy account would become negative"
|
|
1381
|
-
);
|
|
1382
|
-
}
|
|
1383
|
-
}
|
|
1384
|
-
function createProjectionSnapshot(transactions, rebuiltAt) {
|
|
1385
|
-
parseIsoTimestamp(rebuiltAt);
|
|
1386
|
-
const last = transactions.at(-1);
|
|
1387
|
-
return {
|
|
1388
|
-
schemaVersion: ECONOMY_CONTRACT_VERSION,
|
|
1389
|
-
balances: rebuildBalanceProjection(transactions),
|
|
1390
|
-
...last === void 0 ? {} : { lastTransactionId: last.transactionId },
|
|
1391
|
-
rebuiltAt
|
|
1392
|
-
};
|
|
1393
|
-
}
|
|
1394
|
-
|
|
1395
|
-
// src/recurrence.ts
|
|
1396
|
-
var BASELINE_MONTHLY_SUBSCRIPTION_PLAN = Object.freeze({
|
|
1397
|
-
schemaVersion: ECONOMY_CONTRACT_VERSION,
|
|
1398
|
-
planId: "monthly-10-100-v1",
|
|
1399
|
-
catalogVersion: "gbp-subscriptions-v1",
|
|
1400
|
-
interval: "monthly",
|
|
1401
|
-
priceMinorUnits: "1000",
|
|
1402
|
-
currency: "GBP",
|
|
1403
|
-
grantAmount: serializeTokenSubunits(100000n),
|
|
1404
|
-
enabled: false,
|
|
1405
|
-
effectiveFrom: "2026-07-15T00:00:00.000Z"
|
|
1406
|
-
});
|
|
1407
|
-
function assertTokenSubscriptionPlan(plan) {
|
|
1408
1778
|
economyAssert(
|
|
1409
|
-
|
|
1779
|
+
envelope.relationshipId === void 0 === (envelope.authorizationVersion === void 0),
|
|
1410
1780
|
"INVALID_CONTRACT",
|
|
1411
|
-
"
|
|
1781
|
+
"Relationship identity and authorization version must be supplied together"
|
|
1412
1782
|
);
|
|
1413
|
-
assertEconomyIdentifier(plan.planId, "planId");
|
|
1414
|
-
assertEconomyIdentifier(plan.catalogVersion, "catalogVersion");
|
|
1415
1783
|
economyAssert(
|
|
1416
|
-
[
|
|
1784
|
+
/^sha256:[a-f0-9]{64}$/u.test(envelope.payloadHash),
|
|
1417
1785
|
"INVALID_CONTRACT",
|
|
1418
|
-
"
|
|
1786
|
+
"Command payload hash must be a canonical SHA-256 reference"
|
|
1419
1787
|
);
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
const grantAmount = parseTokenSubunits(plan.grantAmount);
|
|
1426
|
-
economyAssert(
|
|
1427
|
-
grantAmount > 0n && isWholeTokenAmount(grantAmount),
|
|
1428
|
-
"AMOUNT_NOT_WHOLE_TOKEN",
|
|
1429
|
-
"Subscription grants must contain whole Tokens"
|
|
1788
|
+
parseIsoTimestamp(envelope.acceptedAt);
|
|
1789
|
+
assertEconomyIdentifier(envelope.acceptedRegion, "acceptedRegion");
|
|
1790
|
+
assertEconomyIdentifier(
|
|
1791
|
+
envelope.writerFencingToken,
|
|
1792
|
+
"writerFencingToken"
|
|
1430
1793
|
);
|
|
1431
|
-
parseIsoTimestamp(plan.effectiveFrom);
|
|
1432
1794
|
}
|
|
1433
|
-
|
|
1434
|
-
// src/spend-requests.ts
|
|
1435
|
-
function assertSpendRequest(request) {
|
|
1795
|
+
function assertEconomyCommandWorkflowEvent(event) {
|
|
1436
1796
|
economyAssert(
|
|
1437
|
-
|
|
1797
|
+
event.schemaVersion === ECONOMY_CONTRACT_VERSION,
|
|
1438
1798
|
"INVALID_CONTRACT",
|
|
1439
|
-
"Unsupported
|
|
1799
|
+
"Unsupported command-workflow-event contract version"
|
|
1440
1800
|
);
|
|
1441
|
-
assertEconomyIdentifier(
|
|
1442
|
-
assertEconomyIdentifier(
|
|
1443
|
-
assertEconomyIdentifier(request.allocationId, "allocationId");
|
|
1801
|
+
assertEconomyIdentifier(event.workflowEventId, "workflowEventId");
|
|
1802
|
+
assertEconomyIdentifier(event.commandId, "commandId");
|
|
1444
1803
|
economyAssert(
|
|
1445
|
-
|
|
1804
|
+
["accepted", "processing", "failed", "completed"].includes(event.state),
|
|
1446
1805
|
"INVALID_CONTRACT",
|
|
1447
|
-
"
|
|
1806
|
+
"Command workflow has an unsupported state"
|
|
1448
1807
|
);
|
|
1449
1808
|
economyAssert(
|
|
1450
|
-
|
|
1451
|
-
"requested",
|
|
1452
|
-
"approved",
|
|
1453
|
-
"rejected",
|
|
1454
|
-
"expired",
|
|
1455
|
-
"executed",
|
|
1456
|
-
"cancelled"
|
|
1457
|
-
].includes(request.status),
|
|
1809
|
+
event.state === "failed" === (event.failureCode !== void 0),
|
|
1458
1810
|
"INVALID_CONTRACT",
|
|
1459
|
-
"
|
|
1460
|
-
);
|
|
1461
|
-
economyAssert(
|
|
1462
|
-
parseTokenSubunits(request.amount) > 0n,
|
|
1463
|
-
"INVALID_AMOUNT",
|
|
1464
|
-
"Spend-request amount must be positive"
|
|
1465
|
-
);
|
|
1466
|
-
const requestedAt = parseIsoTimestamp(request.requestedAt);
|
|
1467
|
-
const expiresAt = parseIsoTimestamp(request.expiresAt);
|
|
1468
|
-
economyAssert(
|
|
1469
|
-
expiresAt > requestedAt,
|
|
1470
|
-
"INVALID_TIME_WINDOW",
|
|
1471
|
-
"Spend-request expiry must follow creation"
|
|
1472
|
-
);
|
|
1473
|
-
const decisionRequired = ["approved", "rejected", "executed"].includes(
|
|
1474
|
-
request.status
|
|
1811
|
+
"Only failed command workflows require a failure code"
|
|
1475
1812
|
);
|
|
1813
|
+
if (event.failureCode !== void 0) {
|
|
1814
|
+
assertEconomyIdentifier(event.failureCode, "failureCode");
|
|
1815
|
+
}
|
|
1816
|
+
parseIsoTimestamp(event.occurredAt);
|
|
1817
|
+
}
|
|
1818
|
+
|
|
1819
|
+
// src/wallets.ts
|
|
1820
|
+
function assertWalletOwnerReference(owner) {
|
|
1476
1821
|
economyAssert(
|
|
1477
|
-
|
|
1822
|
+
["account", "household", "system"].includes(owner.ownerType),
|
|
1478
1823
|
"INVALID_CONTRACT",
|
|
1479
|
-
"
|
|
1824
|
+
"Wallet owner reference has an unsupported type"
|
|
1480
1825
|
);
|
|
1481
|
-
if (
|
|
1482
|
-
assertEconomyIdentifier(request.decidedByAccountId, "decidedByAccountId");
|
|
1483
|
-
}
|
|
1484
|
-
if (request.decidedAt !== void 0) {
|
|
1485
|
-
const decidedAt = parseIsoTimestamp(request.decidedAt);
|
|
1486
|
-
economyAssert(
|
|
1487
|
-
decidedAt >= requestedAt && decidedAt <= expiresAt,
|
|
1488
|
-
"INVALID_TIME_WINDOW",
|
|
1489
|
-
"Spend-request decision must occur before expiry"
|
|
1490
|
-
);
|
|
1491
|
-
}
|
|
1492
|
-
if (request.approvalNonceHash !== void 0) {
|
|
1826
|
+
if (owner.ownerType === "system") {
|
|
1493
1827
|
economyAssert(
|
|
1494
|
-
|
|
1495
|
-
"INVALID_CONTRACT",
|
|
1496
|
-
"Approval nonces must be stored as SHA-256 hashes"
|
|
1497
|
-
);
|
|
1498
|
-
}
|
|
1499
|
-
if (request.status === "approved" || request.status === "executed") {
|
|
1500
|
-
economyAssert(
|
|
1501
|
-
request.approvalNonceHash !== void 0,
|
|
1828
|
+
owner.ownerId === "system",
|
|
1502
1829
|
"INVALID_CONTRACT",
|
|
1503
|
-
"
|
|
1504
|
-
);
|
|
1505
|
-
}
|
|
1506
|
-
if (request.resultingTransactionId !== void 0) {
|
|
1507
|
-
assertEconomyIdentifier(
|
|
1508
|
-
request.resultingTransactionId,
|
|
1509
|
-
"resultingTransactionId"
|
|
1830
|
+
"System wallet lookups must use the system owner"
|
|
1510
1831
|
);
|
|
1832
|
+
} else {
|
|
1833
|
+
assertEconomyIdentifier(owner.ownerId, "ownerId");
|
|
1511
1834
|
economyAssert(
|
|
1512
|
-
|
|
1835
|
+
owner.ownerId !== "system",
|
|
1513
1836
|
"INVALID_CONTRACT",
|
|
1514
|
-
"
|
|
1837
|
+
"Account and household wallets cannot use the system owner"
|
|
1515
1838
|
);
|
|
1516
1839
|
}
|
|
1517
1840
|
}
|
|
1518
|
-
|
|
1519
|
-
// src/wallets.ts
|
|
1520
1841
|
function assertWallet(wallet) {
|
|
1521
1842
|
economyAssert(
|
|
1522
1843
|
wallet.schemaVersion === ECONOMY_CONTRACT_VERSION,
|
|
@@ -1662,6 +1983,920 @@ function assertWalletLifetimeTotals(totals) {
|
|
|
1662
1983
|
);
|
|
1663
1984
|
}
|
|
1664
1985
|
}
|
|
1986
|
+
|
|
1987
|
+
// src/projection.ts
|
|
1988
|
+
function assertWalletBalanceProjection(projection) {
|
|
1989
|
+
economyAssert(
|
|
1990
|
+
projection.schemaVersion === ECONOMY_CONTRACT_VERSION,
|
|
1991
|
+
"INVALID_CONTRACT",
|
|
1992
|
+
"Unsupported wallet-balance-projection contract version"
|
|
1993
|
+
);
|
|
1994
|
+
assertEconomyIdentifier(projection.walletId, "walletId");
|
|
1995
|
+
for (const amount of [
|
|
1996
|
+
projection.spendable,
|
|
1997
|
+
projection.reserved,
|
|
1998
|
+
projection.held
|
|
1999
|
+
]) {
|
|
2000
|
+
economyAssert(
|
|
2001
|
+
parseTokenSubunits(amount) >= 0n,
|
|
2002
|
+
"NEGATIVE_PROJECTION",
|
|
2003
|
+
"Wallet projection buckets cannot be negative"
|
|
2004
|
+
);
|
|
2005
|
+
}
|
|
2006
|
+
economyAssert(
|
|
2007
|
+
Number.isSafeInteger(projection.version) && projection.version >= 1,
|
|
2008
|
+
"INVALID_CONTRACT",
|
|
2009
|
+
"Wallet projection version must be a positive safe integer"
|
|
2010
|
+
);
|
|
2011
|
+
if (projection.lastTransactionId !== void 0) {
|
|
2012
|
+
assertEconomyIdentifier(
|
|
2013
|
+
projection.lastTransactionId,
|
|
2014
|
+
"lastTransactionId"
|
|
2015
|
+
);
|
|
2016
|
+
}
|
|
2017
|
+
parseIsoTimestamp(projection.asOf);
|
|
2018
|
+
}
|
|
2019
|
+
function assertWalletBalanceDelta(delta) {
|
|
2020
|
+
economyAssert(
|
|
2021
|
+
delta.schemaVersion === ECONOMY_CONTRACT_VERSION,
|
|
2022
|
+
"INVALID_CONTRACT",
|
|
2023
|
+
"Unsupported wallet-balance-delta contract version"
|
|
2024
|
+
);
|
|
2025
|
+
assertEconomyIdentifier(delta.walletId, "walletId");
|
|
2026
|
+
const values = [
|
|
2027
|
+
parseTokenSubunits(delta.spendableDelta),
|
|
2028
|
+
parseTokenSubunits(delta.reservedDelta),
|
|
2029
|
+
parseTokenSubunits(delta.heldDelta)
|
|
2030
|
+
];
|
|
2031
|
+
economyAssert(
|
|
2032
|
+
values.some((value) => value !== 0n),
|
|
2033
|
+
"INVALID_AMOUNT",
|
|
2034
|
+
"Wallet balance delta must change at least one bucket"
|
|
2035
|
+
);
|
|
2036
|
+
}
|
|
2037
|
+
function assertWalletBalanceDeltaBatch(transactionId, deltas) {
|
|
2038
|
+
assertEconomyIdentifier(transactionId, "transactionId");
|
|
2039
|
+
economyAssert(
|
|
2040
|
+
deltas.length > 0,
|
|
2041
|
+
"INVALID_CONTRACT",
|
|
2042
|
+
"An economic transaction must update at least one wallet projection"
|
|
2043
|
+
);
|
|
2044
|
+
const walletIds = /* @__PURE__ */ new Set();
|
|
2045
|
+
for (const delta of deltas) {
|
|
2046
|
+
assertWalletBalanceDelta(delta);
|
|
2047
|
+
economyAssert(
|
|
2048
|
+
!walletIds.has(delta.walletId),
|
|
2049
|
+
"DUPLICATE_IDENTIFIER",
|
|
2050
|
+
"A balance-delta batch must update each wallet once"
|
|
2051
|
+
);
|
|
2052
|
+
walletIds.add(delta.walletId);
|
|
2053
|
+
}
|
|
2054
|
+
}
|
|
2055
|
+
function applyWalletBalanceDelta(projection, delta, transactionId, asOf) {
|
|
2056
|
+
assertWalletBalanceProjection(projection);
|
|
2057
|
+
assertWalletBalanceDelta(delta);
|
|
2058
|
+
assertEconomyIdentifier(transactionId, "transactionId");
|
|
2059
|
+
economyAssert(
|
|
2060
|
+
projection.walletId === delta.walletId,
|
|
2061
|
+
"INVALID_CONTRACT",
|
|
2062
|
+
"Wallet balance delta must target its projection wallet"
|
|
2063
|
+
);
|
|
2064
|
+
economyAssert(
|
|
2065
|
+
parseIsoTimestamp(asOf) >= parseIsoTimestamp(projection.asOf),
|
|
2066
|
+
"INVALID_TIME_WINDOW",
|
|
2067
|
+
"Wallet balance update cannot precede its projection"
|
|
2068
|
+
);
|
|
2069
|
+
const next = {
|
|
2070
|
+
...projection,
|
|
2071
|
+
spendable: serializeTokenSubunits(
|
|
2072
|
+
parseTokenSubunits(projection.spendable) + parseTokenSubunits(delta.spendableDelta)
|
|
2073
|
+
),
|
|
2074
|
+
reserved: serializeTokenSubunits(
|
|
2075
|
+
parseTokenSubunits(projection.reserved) + parseTokenSubunits(delta.reservedDelta)
|
|
2076
|
+
),
|
|
2077
|
+
held: serializeTokenSubunits(
|
|
2078
|
+
parseTokenSubunits(projection.held) + parseTokenSubunits(delta.heldDelta)
|
|
2079
|
+
),
|
|
2080
|
+
version: projection.version + 1,
|
|
2081
|
+
lastTransactionId: transactionId,
|
|
2082
|
+
asOf
|
|
2083
|
+
};
|
|
2084
|
+
assertWalletBalanceProjection(next);
|
|
2085
|
+
return next;
|
|
2086
|
+
}
|
|
2087
|
+
function createWalletBalanceSummary(projection) {
|
|
2088
|
+
assertWalletBalanceProjection(projection);
|
|
2089
|
+
const spendable = parseTokenSubunits(projection.spendable);
|
|
2090
|
+
const rewardProgress = spendable % 1000n;
|
|
2091
|
+
const summary = {
|
|
2092
|
+
schemaVersion: ECONOMY_CONTRACT_VERSION,
|
|
2093
|
+
walletId: projection.walletId,
|
|
2094
|
+
available: serializeTokenSubunits(spendable - rewardProgress),
|
|
2095
|
+
reserved: projection.reserved,
|
|
2096
|
+
held: projection.held,
|
|
2097
|
+
rewardProgress: serializeTokenSubunits(rewardProgress),
|
|
2098
|
+
version: projection.version,
|
|
2099
|
+
asOf: projection.asOf
|
|
2100
|
+
};
|
|
2101
|
+
assertDeterministicWalletBalanceSummary(summary);
|
|
2102
|
+
return summary;
|
|
2103
|
+
}
|
|
2104
|
+
function assertDeterministicWalletBalanceSummary(summary) {
|
|
2105
|
+
assertWalletBalanceSummary(summary);
|
|
2106
|
+
economyAssert(
|
|
2107
|
+
parseTokenSubunits(summary.available) % 1000n === 0n && parseTokenSubunits(summary.rewardProgress) < 1000n,
|
|
2108
|
+
"INVALID_CONTRACT",
|
|
2109
|
+
"Per-wallet availability must be whole Tokens with sub-Token progress below one Token"
|
|
2110
|
+
);
|
|
2111
|
+
}
|
|
2112
|
+
function deriveWalletBalanceDeltas(transaction, bindings) {
|
|
2113
|
+
assertEconomicJournalTransaction(transaction);
|
|
2114
|
+
const byAccount = /* @__PURE__ */ new Map();
|
|
2115
|
+
for (const binding of bindings) {
|
|
2116
|
+
assertEconomyIdentifier(binding.accountId, "accountId");
|
|
2117
|
+
assertEconomyIdentifier(binding.walletId, "walletId");
|
|
2118
|
+
economyAssert(
|
|
2119
|
+
["spendable", "reserved", "held"].includes(binding.bucket),
|
|
2120
|
+
"INVALID_CONTRACT",
|
|
2121
|
+
"Wallet account binding has an unsupported bucket"
|
|
2122
|
+
);
|
|
2123
|
+
economyAssert(
|
|
2124
|
+
!byAccount.has(binding.accountId),
|
|
2125
|
+
"DUPLICATE_IDENTIFIER",
|
|
2126
|
+
"A ledger account may bind to only one wallet bucket"
|
|
2127
|
+
);
|
|
2128
|
+
byAccount.set(binding.accountId, binding);
|
|
2129
|
+
}
|
|
2130
|
+
const byWallet = /* @__PURE__ */ new Map();
|
|
2131
|
+
for (const posting of transaction.postings) {
|
|
2132
|
+
if (posting.walletId === void 0) {
|
|
2133
|
+
continue;
|
|
2134
|
+
}
|
|
2135
|
+
const binding = byAccount.get(posting.accountId);
|
|
2136
|
+
economyAssert(
|
|
2137
|
+
binding !== void 0 && binding.walletId === posting.walletId,
|
|
2138
|
+
"INVALID_CONTRACT",
|
|
2139
|
+
"Every wallet-linked posting must match an authoritative account binding"
|
|
2140
|
+
);
|
|
2141
|
+
const values = byWallet.get(binding.walletId) ?? [0n, 0n, 0n];
|
|
2142
|
+
const amount = parseTokenSubunits(posting.amount);
|
|
2143
|
+
if (binding.bucket === "spendable") {
|
|
2144
|
+
values[0] += amount;
|
|
2145
|
+
} else if (binding.bucket === "reserved") {
|
|
2146
|
+
values[1] += amount;
|
|
2147
|
+
} else {
|
|
2148
|
+
values[2] += amount;
|
|
2149
|
+
}
|
|
2150
|
+
byWallet.set(binding.walletId, values);
|
|
2151
|
+
}
|
|
2152
|
+
const deltas = [...byWallet.entries()].map(([walletId, values]) => ({
|
|
2153
|
+
schemaVersion: ECONOMY_CONTRACT_VERSION,
|
|
2154
|
+
walletId,
|
|
2155
|
+
spendableDelta: serializeTokenSubunits(values[0]),
|
|
2156
|
+
reservedDelta: serializeTokenSubunits(values[1]),
|
|
2157
|
+
heldDelta: serializeTokenSubunits(values[2])
|
|
2158
|
+
})).filter(
|
|
2159
|
+
(delta) => delta.spendableDelta !== "0" || delta.reservedDelta !== "0" || delta.heldDelta !== "0"
|
|
2160
|
+
).sort((left, right) => left.walletId.localeCompare(right.walletId));
|
|
2161
|
+
assertWalletBalanceDeltaBatch(transaction.transactionId, deltas);
|
|
2162
|
+
return deltas;
|
|
2163
|
+
}
|
|
2164
|
+
var EARNED_ACTIVITY_TYPES = /* @__PURE__ */ new Set([
|
|
2165
|
+
"rewarded-ad",
|
|
2166
|
+
"offerwall",
|
|
2167
|
+
"event",
|
|
2168
|
+
"competition"
|
|
2169
|
+
]);
|
|
2170
|
+
function deriveWalletLifetimeDeltas(transaction) {
|
|
2171
|
+
assertEconomicJournalTransaction(transaction);
|
|
2172
|
+
if (transaction.status !== "settled") {
|
|
2173
|
+
return [];
|
|
2174
|
+
}
|
|
2175
|
+
const netByWallet = /* @__PURE__ */ new Map();
|
|
2176
|
+
for (const posting of transaction.postings) {
|
|
2177
|
+
if (posting.walletId !== void 0) {
|
|
2178
|
+
netByWallet.set(
|
|
2179
|
+
posting.walletId,
|
|
2180
|
+
(netByWallet.get(posting.walletId) ?? 0n) + parseTokenSubunits(posting.amount)
|
|
2181
|
+
);
|
|
2182
|
+
}
|
|
2183
|
+
}
|
|
2184
|
+
const deltas = [];
|
|
2185
|
+
for (const [walletId, net] of [...netByWallet.entries()].sort(
|
|
2186
|
+
([left], [right]) => left.localeCompare(right)
|
|
2187
|
+
)) {
|
|
2188
|
+
let bucket;
|
|
2189
|
+
let amount = 0n;
|
|
2190
|
+
if (net > 0n && (transaction.activityType === "purchase" || transaction.activityType === "subscription")) {
|
|
2191
|
+
bucket = "bought";
|
|
2192
|
+
amount = net;
|
|
2193
|
+
} else if (net > 0n && EARNED_ACTIVITY_TYPES.has(transaction.activityType)) {
|
|
2194
|
+
bucket = "earned";
|
|
2195
|
+
amount = net;
|
|
2196
|
+
} else if (net < 0n && (transaction.activityType === "allocation" || transaction.activityType === "boost")) {
|
|
2197
|
+
bucket = "allocated";
|
|
2198
|
+
amount = -net;
|
|
2199
|
+
} else if (net > 0n && transaction.activityType === "reclaim") {
|
|
2200
|
+
bucket = "reclaimed";
|
|
2201
|
+
amount = net;
|
|
2202
|
+
} else if (net < 0n && transaction.activityType === "spend") {
|
|
2203
|
+
bucket = "spent";
|
|
2204
|
+
amount = -net;
|
|
2205
|
+
} else if (net < 0n && (transaction.activityType === "refund" || transaction.activityType === "chargeback" || transaction.activityType === "reversal")) {
|
|
2206
|
+
bucket = "reversed";
|
|
2207
|
+
amount = -net;
|
|
2208
|
+
}
|
|
2209
|
+
if (bucket !== void 0) {
|
|
2210
|
+
deltas.push({
|
|
2211
|
+
schemaVersion: ECONOMY_CONTRACT_VERSION,
|
|
2212
|
+
walletId,
|
|
2213
|
+
bucket,
|
|
2214
|
+
amount: serializeTokenSubunits(amount)
|
|
2215
|
+
});
|
|
2216
|
+
}
|
|
2217
|
+
}
|
|
2218
|
+
return deltas;
|
|
2219
|
+
}
|
|
2220
|
+
function applyWalletLifetimeDeltas(current, walletId, deltas) {
|
|
2221
|
+
assertWalletLifetimeTotals(current);
|
|
2222
|
+
assertEconomyIdentifier(walletId, "walletId");
|
|
2223
|
+
const next = {
|
|
2224
|
+
bought: parseTokenSubunits(current.bought),
|
|
2225
|
+
earned: parseTokenSubunits(current.earned),
|
|
2226
|
+
allocated: parseTokenSubunits(current.allocated),
|
|
2227
|
+
reclaimed: parseTokenSubunits(current.reclaimed),
|
|
2228
|
+
spent: parseTokenSubunits(current.spent),
|
|
2229
|
+
reversed: parseTokenSubunits(current.reversed)
|
|
2230
|
+
};
|
|
2231
|
+
for (const delta of deltas) {
|
|
2232
|
+
economyAssert(
|
|
2233
|
+
delta.schemaVersion === ECONOMY_CONTRACT_VERSION && delta.walletId === walletId && [
|
|
2234
|
+
"bought",
|
|
2235
|
+
"earned",
|
|
2236
|
+
"allocated",
|
|
2237
|
+
"reclaimed",
|
|
2238
|
+
"spent",
|
|
2239
|
+
"reversed"
|
|
2240
|
+
].includes(delta.bucket),
|
|
2241
|
+
"INVALID_CONTRACT",
|
|
2242
|
+
"Lifetime delta must target the requested wallet and a known bucket"
|
|
2243
|
+
);
|
|
2244
|
+
const amount = parseTokenSubunits(delta.amount);
|
|
2245
|
+
economyAssert(
|
|
2246
|
+
amount > 0n,
|
|
2247
|
+
"INVALID_AMOUNT",
|
|
2248
|
+
"Lifetime deltas must be positive"
|
|
2249
|
+
);
|
|
2250
|
+
next[delta.bucket] += amount;
|
|
2251
|
+
}
|
|
2252
|
+
const totals = {
|
|
2253
|
+
schemaVersion: ECONOMY_CONTRACT_VERSION,
|
|
2254
|
+
bought: serializeTokenSubunits(next.bought),
|
|
2255
|
+
earned: serializeTokenSubunits(next.earned),
|
|
2256
|
+
allocated: serializeTokenSubunits(next.allocated),
|
|
2257
|
+
reclaimed: serializeTokenSubunits(next.reclaimed),
|
|
2258
|
+
spent: serializeTokenSubunits(next.spent),
|
|
2259
|
+
reversed: serializeTokenSubunits(next.reversed)
|
|
2260
|
+
};
|
|
2261
|
+
assertWalletLifetimeTotals(totals);
|
|
2262
|
+
return totals;
|
|
2263
|
+
}
|
|
2264
|
+
function applyTransactionToProjection(current, transaction) {
|
|
2265
|
+
assertBalancedTransaction(transaction);
|
|
2266
|
+
const next = { ...current };
|
|
2267
|
+
for (const posting of transaction.postings) {
|
|
2268
|
+
const prior = parseTokenSubunits(next[posting.accountId] ?? "0");
|
|
2269
|
+
next[posting.accountId] = serializeTokenSubunits(
|
|
2270
|
+
prior + parseTokenSubunits(posting.amount)
|
|
2271
|
+
);
|
|
2272
|
+
}
|
|
2273
|
+
return next;
|
|
2274
|
+
}
|
|
2275
|
+
function rebuildBalanceProjection(transactions) {
|
|
2276
|
+
let projection = {};
|
|
2277
|
+
const transactionIds = /* @__PURE__ */ new Set();
|
|
2278
|
+
const idempotencyKeys = /* @__PURE__ */ new Set();
|
|
2279
|
+
const providerEvents = /* @__PURE__ */ new Set();
|
|
2280
|
+
for (const transaction of transactions) {
|
|
2281
|
+
economyAssert(
|
|
2282
|
+
!transactionIds.has(transaction.transactionId),
|
|
2283
|
+
"DUPLICATE_TRANSACTION",
|
|
2284
|
+
"Transaction identifiers must be unique"
|
|
2285
|
+
);
|
|
2286
|
+
economyAssert(
|
|
2287
|
+
!idempotencyKeys.has(transaction.idempotencyKey),
|
|
2288
|
+
"DUPLICATE_TRANSACTION",
|
|
2289
|
+
"Idempotency keys must be unique"
|
|
2290
|
+
);
|
|
2291
|
+
if (transaction.providerEventId !== void 0) {
|
|
2292
|
+
economyAssert(
|
|
2293
|
+
!providerEvents.has(transaction.providerEventId),
|
|
2294
|
+
"DUPLICATE_TRANSACTION",
|
|
2295
|
+
"Provider event identifiers must be unique"
|
|
2296
|
+
);
|
|
2297
|
+
providerEvents.add(transaction.providerEventId);
|
|
2298
|
+
}
|
|
2299
|
+
transactionIds.add(transaction.transactionId);
|
|
2300
|
+
idempotencyKeys.add(transaction.idempotencyKey);
|
|
2301
|
+
projection = applyTransactionToProjection(projection, transaction);
|
|
2302
|
+
}
|
|
2303
|
+
return projection;
|
|
2304
|
+
}
|
|
2305
|
+
function assertNonNegativeAccounts(projection, protectedAccountIds) {
|
|
2306
|
+
for (const accountId of protectedAccountIds) {
|
|
2307
|
+
economyAssert(
|
|
2308
|
+
parseTokenSubunits(projection[accountId] ?? "0") >= 0n,
|
|
2309
|
+
"NEGATIVE_PROJECTION",
|
|
2310
|
+
"A protected economy account would become negative"
|
|
2311
|
+
);
|
|
2312
|
+
}
|
|
2313
|
+
}
|
|
2314
|
+
function createProjectionSnapshot(transactions, rebuiltAt) {
|
|
2315
|
+
parseIsoTimestamp(rebuiltAt);
|
|
2316
|
+
const last = transactions.at(-1);
|
|
2317
|
+
return {
|
|
2318
|
+
schemaVersion: ECONOMY_CONTRACT_VERSION,
|
|
2319
|
+
balances: rebuildBalanceProjection(transactions),
|
|
2320
|
+
...last === void 0 ? {} : { lastTransactionId: last.transactionId },
|
|
2321
|
+
rebuiltAt
|
|
2322
|
+
};
|
|
2323
|
+
}
|
|
2324
|
+
|
|
2325
|
+
// src/ports/query.ts
|
|
2326
|
+
var ACTIVITY_TYPES2 = /* @__PURE__ */ new Set([
|
|
2327
|
+
"purchase",
|
|
2328
|
+
"subscription",
|
|
2329
|
+
"rewarded-ad",
|
|
2330
|
+
"offerwall",
|
|
2331
|
+
"allocation",
|
|
2332
|
+
"boost",
|
|
2333
|
+
"reclaim",
|
|
2334
|
+
"spend",
|
|
2335
|
+
"hold",
|
|
2336
|
+
"refund",
|
|
2337
|
+
"chargeback",
|
|
2338
|
+
"adjustment",
|
|
2339
|
+
"reversal",
|
|
2340
|
+
"event",
|
|
2341
|
+
"competition"
|
|
2342
|
+
]);
|
|
2343
|
+
var ACTIVITY_STATUSES2 = /* @__PURE__ */ new Set([
|
|
2344
|
+
"pending",
|
|
2345
|
+
"held",
|
|
2346
|
+
"settled",
|
|
2347
|
+
"reversed",
|
|
2348
|
+
"failed"
|
|
2349
|
+
]);
|
|
2350
|
+
var ACTIVITY_SOURCES2 = /* @__PURE__ */ new Set([
|
|
2351
|
+
"shopify",
|
|
2352
|
+
"ayet",
|
|
2353
|
+
"bitlabs",
|
|
2354
|
+
"subscription",
|
|
2355
|
+
"event",
|
|
2356
|
+
"competition",
|
|
2357
|
+
"adjustment"
|
|
2358
|
+
]);
|
|
2359
|
+
var PORTFOLIO_ROLES = /* @__PURE__ */ new Set([
|
|
2360
|
+
"household-treasury",
|
|
2361
|
+
"personal",
|
|
2362
|
+
"gameplay-allocation",
|
|
2363
|
+
"hold"
|
|
2364
|
+
]);
|
|
2365
|
+
function hasControlCharacters2(value) {
|
|
2366
|
+
return [...value].some((character) => {
|
|
2367
|
+
const code = character.charCodeAt(0);
|
|
2368
|
+
return code <= 31 || code === 127;
|
|
2369
|
+
});
|
|
2370
|
+
}
|
|
2371
|
+
function assertSafeDisplayString(value, label) {
|
|
2372
|
+
economyAssert(
|
|
2373
|
+
typeof value === "string" && value.trim().length > 0 && value.length <= 128 && !hasControlCharacters2(value),
|
|
2374
|
+
"INVALID_CONTRACT",
|
|
2375
|
+
`${label} must be bounded and safe for display`
|
|
2376
|
+
);
|
|
2377
|
+
}
|
|
2378
|
+
function assertOpaqueCursor(value) {
|
|
2379
|
+
economyAssert(
|
|
2380
|
+
typeof value === "string" && value.length > 0 && value.length <= 512 && !hasControlCharacters2(value),
|
|
2381
|
+
"INVALID_CONTRACT",
|
|
2382
|
+
"Activity cursor must be a bounded opaque value"
|
|
2383
|
+
);
|
|
2384
|
+
}
|
|
2385
|
+
function assertUniqueValues(values, allowed, label) {
|
|
2386
|
+
const unique = new Set(values);
|
|
2387
|
+
economyAssert(
|
|
2388
|
+
values.length > 0 && values.length === unique.size && values.every((value) => allowed.has(value)),
|
|
2389
|
+
"INVALID_CONTRACT",
|
|
2390
|
+
`${label} must contain unique supported values`
|
|
2391
|
+
);
|
|
2392
|
+
}
|
|
2393
|
+
function assertWalletPortfolioReadScope(scope) {
|
|
2394
|
+
economyAssert(
|
|
2395
|
+
scope.schemaVersion === ECONOMY_CONTRACT_VERSION,
|
|
2396
|
+
"INVALID_CONTRACT",
|
|
2397
|
+
"Unsupported wallet-portfolio scope contract version"
|
|
2398
|
+
);
|
|
2399
|
+
assertEconomyIdentifier(scope.portfolioId, "portfolioId");
|
|
2400
|
+
assertEconomyIdentifier(scope.subjectAccountId, "subjectAccountId");
|
|
2401
|
+
economyAssert(
|
|
2402
|
+
scope.components.length > 0 && scope.components.length <= 100,
|
|
2403
|
+
"INVALID_CONTRACT",
|
|
2404
|
+
"Wallet portfolio must contain between one and 100 components"
|
|
2405
|
+
);
|
|
2406
|
+
const walletIds = /* @__PURE__ */ new Set();
|
|
2407
|
+
for (const component of scope.components) {
|
|
2408
|
+
assertEconomyIdentifier(component.walletId, "walletId");
|
|
2409
|
+
economyAssert(
|
|
2410
|
+
PORTFOLIO_ROLES.has(component.role),
|
|
2411
|
+
"INVALID_CONTRACT",
|
|
2412
|
+
"Wallet portfolio component has an unsupported role"
|
|
2413
|
+
);
|
|
2414
|
+
economyAssert(
|
|
2415
|
+
!walletIds.has(component.walletId),
|
|
2416
|
+
"DUPLICATE_IDENTIFIER",
|
|
2417
|
+
"Wallet portfolio cannot repeat a wallet"
|
|
2418
|
+
);
|
|
2419
|
+
walletIds.add(component.walletId);
|
|
2420
|
+
if (component.beneficiaryAccountId !== void 0) {
|
|
2421
|
+
assertEconomyIdentifier(
|
|
2422
|
+
component.beneficiaryAccountId,
|
|
2423
|
+
"beneficiaryAccountId"
|
|
2424
|
+
);
|
|
2425
|
+
}
|
|
2426
|
+
economyAssert(
|
|
2427
|
+
component.role !== "gameplay-allocation" || component.beneficiaryAccountId !== void 0,
|
|
2428
|
+
"INVALID_CONTRACT",
|
|
2429
|
+
"Gameplay-allocation portfolio components require a beneficiary"
|
|
2430
|
+
);
|
|
2431
|
+
}
|
|
2432
|
+
}
|
|
2433
|
+
function assertWalletLifetimeSnapshot(snapshot) {
|
|
2434
|
+
economyAssert(
|
|
2435
|
+
snapshot.schemaVersion === ECONOMY_CONTRACT_VERSION,
|
|
2436
|
+
"INVALID_CONTRACT",
|
|
2437
|
+
"Unsupported wallet-lifetime-snapshot contract version"
|
|
2438
|
+
);
|
|
2439
|
+
assertEconomyIdentifier(snapshot.walletId, "walletId");
|
|
2440
|
+
assertWalletLifetimeTotals(snapshot.totals);
|
|
2441
|
+
economyAssert(
|
|
2442
|
+
Number.isSafeInteger(snapshot.version) && snapshot.version >= 1,
|
|
2443
|
+
"INVALID_CONTRACT",
|
|
2444
|
+
"Wallet lifetime version must be a positive safe integer"
|
|
2445
|
+
);
|
|
2446
|
+
parseIsoTimestamp(snapshot.asOf);
|
|
2447
|
+
}
|
|
2448
|
+
function assertPortfolioComponentMatch(expected, actual) {
|
|
2449
|
+
economyAssert(
|
|
2450
|
+
expected.walletId === actual.walletId && expected.role === actual.role && expected.beneficiaryAccountId === actual.beneficiaryAccountId,
|
|
2451
|
+
"INVALID_CONTRACT",
|
|
2452
|
+
"Portfolio result component does not match its authorized read scope"
|
|
2453
|
+
);
|
|
2454
|
+
}
|
|
2455
|
+
function createWalletPortfolioSummary(scope, components, asOf) {
|
|
2456
|
+
assertWalletPortfolioReadScope(scope);
|
|
2457
|
+
parseIsoTimestamp(asOf);
|
|
2458
|
+
economyAssert(
|
|
2459
|
+
components.length === scope.components.length,
|
|
2460
|
+
"INVALID_CONTRACT",
|
|
2461
|
+
"Portfolio balance must include every authorized component exactly once"
|
|
2462
|
+
);
|
|
2463
|
+
const byWallet = new Map(components.map((item) => [item.walletId, item]));
|
|
2464
|
+
economyAssert(
|
|
2465
|
+
byWallet.size === components.length,
|
|
2466
|
+
"DUPLICATE_IDENTIFIER",
|
|
2467
|
+
"Portfolio balance cannot repeat a wallet"
|
|
2468
|
+
);
|
|
2469
|
+
const ordered = scope.components.map((expected) => {
|
|
2470
|
+
const actual = byWallet.get(expected.walletId);
|
|
2471
|
+
economyAssert(
|
|
2472
|
+
actual !== void 0,
|
|
2473
|
+
"INVALID_CONTRACT",
|
|
2474
|
+
"Portfolio balance is missing an authorized wallet"
|
|
2475
|
+
);
|
|
2476
|
+
assertPortfolioComponentMatch(expected, actual);
|
|
2477
|
+
assertDeterministicWalletBalanceSummary(actual.summary);
|
|
2478
|
+
economyAssert(
|
|
2479
|
+
actual.summary.walletId === actual.walletId && actual.summary.asOf === asOf,
|
|
2480
|
+
"INVALID_CONTRACT",
|
|
2481
|
+
"Portfolio component summary must identify its wallet and shared snapshot time"
|
|
2482
|
+
);
|
|
2483
|
+
return actual;
|
|
2484
|
+
});
|
|
2485
|
+
const totals = {
|
|
2486
|
+
available: serializeTokenSubunits(
|
|
2487
|
+
ordered.reduce(
|
|
2488
|
+
(sum, item) => sum + parseTokenSubunits(item.summary.available),
|
|
2489
|
+
0n
|
|
2490
|
+
)
|
|
2491
|
+
),
|
|
2492
|
+
reserved: serializeTokenSubunits(
|
|
2493
|
+
ordered.reduce(
|
|
2494
|
+
(sum, item) => sum + parseTokenSubunits(item.summary.reserved),
|
|
2495
|
+
0n
|
|
2496
|
+
)
|
|
2497
|
+
),
|
|
2498
|
+
held: serializeTokenSubunits(
|
|
2499
|
+
ordered.reduce(
|
|
2500
|
+
(sum, item) => sum + parseTokenSubunits(item.summary.held),
|
|
2501
|
+
0n
|
|
2502
|
+
)
|
|
2503
|
+
),
|
|
2504
|
+
rewardProgress: serializeTokenSubunits(
|
|
2505
|
+
ordered.reduce(
|
|
2506
|
+
(sum, item) => sum + parseTokenSubunits(item.summary.rewardProgress),
|
|
2507
|
+
0n
|
|
2508
|
+
)
|
|
2509
|
+
)
|
|
2510
|
+
};
|
|
2511
|
+
return {
|
|
2512
|
+
schemaVersion: ECONOMY_CONTRACT_VERSION,
|
|
2513
|
+
portfolioId: scope.portfolioId,
|
|
2514
|
+
subjectAccountId: scope.subjectAccountId,
|
|
2515
|
+
components: ordered,
|
|
2516
|
+
totals,
|
|
2517
|
+
asOf
|
|
2518
|
+
};
|
|
2519
|
+
}
|
|
2520
|
+
function assertWalletPortfolioSummary(summary) {
|
|
2521
|
+
const scope = {
|
|
2522
|
+
schemaVersion: summary.schemaVersion,
|
|
2523
|
+
portfolioId: summary.portfolioId,
|
|
2524
|
+
subjectAccountId: summary.subjectAccountId,
|
|
2525
|
+
components: summary.components.map(
|
|
2526
|
+
({ walletId, role, beneficiaryAccountId }) => ({
|
|
2527
|
+
walletId,
|
|
2528
|
+
role,
|
|
2529
|
+
...beneficiaryAccountId === void 0 ? {} : { beneficiaryAccountId }
|
|
2530
|
+
})
|
|
2531
|
+
)
|
|
2532
|
+
};
|
|
2533
|
+
const rebuilt = createWalletPortfolioSummary(
|
|
2534
|
+
scope,
|
|
2535
|
+
summary.components,
|
|
2536
|
+
summary.asOf
|
|
2537
|
+
);
|
|
2538
|
+
for (const field of [
|
|
2539
|
+
"available",
|
|
2540
|
+
"reserved",
|
|
2541
|
+
"held",
|
|
2542
|
+
"rewardProgress"
|
|
2543
|
+
]) {
|
|
2544
|
+
economyAssert(
|
|
2545
|
+
summary.totals[field] === rebuilt.totals[field],
|
|
2546
|
+
"INVALID_CONTRACT",
|
|
2547
|
+
"Portfolio balance totals must equal their component column sums"
|
|
2548
|
+
);
|
|
2549
|
+
}
|
|
2550
|
+
}
|
|
2551
|
+
function createWalletPortfolioLifetime(scope, components, asOf) {
|
|
2552
|
+
assertWalletPortfolioReadScope(scope);
|
|
2553
|
+
parseIsoTimestamp(asOf);
|
|
2554
|
+
economyAssert(
|
|
2555
|
+
components.length === scope.components.length,
|
|
2556
|
+
"INVALID_CONTRACT",
|
|
2557
|
+
"Portfolio lifetime must include every authorized component exactly once"
|
|
2558
|
+
);
|
|
2559
|
+
const byWallet = new Map(components.map((item) => [item.walletId, item]));
|
|
2560
|
+
economyAssert(
|
|
2561
|
+
byWallet.size === components.length,
|
|
2562
|
+
"DUPLICATE_IDENTIFIER",
|
|
2563
|
+
"Portfolio lifetime cannot repeat a wallet"
|
|
2564
|
+
);
|
|
2565
|
+
const ordered = scope.components.map((expected) => {
|
|
2566
|
+
const actual = byWallet.get(expected.walletId);
|
|
2567
|
+
economyAssert(
|
|
2568
|
+
actual !== void 0,
|
|
2569
|
+
"INVALID_CONTRACT",
|
|
2570
|
+
"Portfolio lifetime is missing an authorized wallet"
|
|
2571
|
+
);
|
|
2572
|
+
assertPortfolioComponentMatch(expected, actual);
|
|
2573
|
+
assertWalletLifetimeSnapshot(actual.snapshot);
|
|
2574
|
+
economyAssert(
|
|
2575
|
+
actual.snapshot.walletId === actual.walletId && actual.snapshot.asOf === asOf,
|
|
2576
|
+
"INVALID_CONTRACT",
|
|
2577
|
+
"Portfolio lifetime component must identify its wallet and shared snapshot time"
|
|
2578
|
+
);
|
|
2579
|
+
return actual;
|
|
2580
|
+
});
|
|
2581
|
+
const sumField = (field) => serializeTokenSubunits(
|
|
2582
|
+
ordered.reduce(
|
|
2583
|
+
(sum, item) => sum + parseTokenSubunits(item.snapshot.totals[field]),
|
|
2584
|
+
0n
|
|
2585
|
+
)
|
|
2586
|
+
);
|
|
2587
|
+
const totals = {
|
|
2588
|
+
schemaVersion: ECONOMY_CONTRACT_VERSION,
|
|
2589
|
+
bought: sumField("bought"),
|
|
2590
|
+
earned: sumField("earned"),
|
|
2591
|
+
allocated: sumField("allocated"),
|
|
2592
|
+
reclaimed: sumField("reclaimed"),
|
|
2593
|
+
spent: sumField("spent"),
|
|
2594
|
+
reversed: sumField("reversed")
|
|
2595
|
+
};
|
|
2596
|
+
assertWalletLifetimeTotals(totals);
|
|
2597
|
+
return {
|
|
2598
|
+
schemaVersion: ECONOMY_CONTRACT_VERSION,
|
|
2599
|
+
portfolioId: scope.portfolioId,
|
|
2600
|
+
subjectAccountId: scope.subjectAccountId,
|
|
2601
|
+
components: ordered,
|
|
2602
|
+
totals,
|
|
2603
|
+
asOf
|
|
2604
|
+
};
|
|
2605
|
+
}
|
|
2606
|
+
function assertWalletPortfolioLifetime(lifetime) {
|
|
2607
|
+
const scope = {
|
|
2608
|
+
schemaVersion: lifetime.schemaVersion,
|
|
2609
|
+
portfolioId: lifetime.portfolioId,
|
|
2610
|
+
subjectAccountId: lifetime.subjectAccountId,
|
|
2611
|
+
components: lifetime.components.map(
|
|
2612
|
+
({ walletId, role, beneficiaryAccountId }) => ({
|
|
2613
|
+
walletId,
|
|
2614
|
+
role,
|
|
2615
|
+
...beneficiaryAccountId === void 0 ? {} : { beneficiaryAccountId }
|
|
2616
|
+
})
|
|
2617
|
+
)
|
|
2618
|
+
};
|
|
2619
|
+
const rebuilt = createWalletPortfolioLifetime(
|
|
2620
|
+
scope,
|
|
2621
|
+
lifetime.components,
|
|
2622
|
+
lifetime.asOf
|
|
2623
|
+
);
|
|
2624
|
+
for (const field of [
|
|
2625
|
+
"bought",
|
|
2626
|
+
"earned",
|
|
2627
|
+
"allocated",
|
|
2628
|
+
"reclaimed",
|
|
2629
|
+
"spent",
|
|
2630
|
+
"reversed"
|
|
2631
|
+
]) {
|
|
2632
|
+
economyAssert(
|
|
2633
|
+
lifetime.totals[field] === rebuilt.totals[field],
|
|
2634
|
+
"INVALID_CONTRACT",
|
|
2635
|
+
"Portfolio lifetime totals must equal their component column sums"
|
|
2636
|
+
);
|
|
2637
|
+
}
|
|
2638
|
+
}
|
|
2639
|
+
function assertWalletActivityEntry(entry) {
|
|
2640
|
+
economyAssert(
|
|
2641
|
+
entry.schemaVersion === ECONOMY_CONTRACT_VERSION,
|
|
2642
|
+
"INVALID_CONTRACT",
|
|
2643
|
+
"Unsupported wallet-activity contract version"
|
|
2644
|
+
);
|
|
2645
|
+
assertEconomyIdentifier(entry.activityId, "activityId");
|
|
2646
|
+
assertEconomyIdentifier(entry.walletId, "walletId");
|
|
2647
|
+
economyAssert(
|
|
2648
|
+
ACTIVITY_TYPES2.has(entry.activityType) && ACTIVITY_STATUSES2.has(entry.status) && ACTIVITY_SOURCES2.has(entry.source),
|
|
2649
|
+
"INVALID_CONTRACT",
|
|
2650
|
+
"Wallet activity type, status, or source is unsupported"
|
|
2651
|
+
);
|
|
2652
|
+
parseIsoTimestamp(entry.occurredAt);
|
|
2653
|
+
economyAssert(
|
|
2654
|
+
parseTokenSubunits(entry.amount) !== 0n,
|
|
2655
|
+
"INVALID_AMOUNT",
|
|
2656
|
+
"Wallet activity amount cannot be zero"
|
|
2657
|
+
);
|
|
2658
|
+
if (entry.beneficiaryAccountId !== void 0) {
|
|
2659
|
+
assertEconomyIdentifier(
|
|
2660
|
+
entry.beneficiaryAccountId,
|
|
2661
|
+
"beneficiaryAccountId"
|
|
2662
|
+
);
|
|
2663
|
+
}
|
|
2664
|
+
if (entry.maskedReference !== void 0) {
|
|
2665
|
+
assertSafeDisplayString(entry.maskedReference, "Masked reference");
|
|
2666
|
+
}
|
|
2667
|
+
assertSafeDisplayString(entry.sourceLabel, "Source label");
|
|
2668
|
+
economyAssert(
|
|
2669
|
+
entry.entryKind === "economic" || entry.entryKind === "workflow",
|
|
2670
|
+
"INVALID_CONTRACT",
|
|
2671
|
+
"Wallet activity has an unsupported entry kind"
|
|
2672
|
+
);
|
|
2673
|
+
if (entry.entryKind === "economic") {
|
|
2674
|
+
assertEconomyIdentifier(entry.transactionId, "transactionId");
|
|
2675
|
+
economyAssert(
|
|
2676
|
+
entry.status === "held" || entry.status === "settled" || entry.status === "reversed",
|
|
2677
|
+
"INVALID_CONTRACT",
|
|
2678
|
+
"Economic activity cannot use a workflow status"
|
|
2679
|
+
);
|
|
2680
|
+
} else {
|
|
2681
|
+
assertEconomyIdentifier(entry.commandId, "commandId");
|
|
2682
|
+
economyAssert(
|
|
2683
|
+
entry.status === "pending" || entry.status === "failed",
|
|
2684
|
+
"INVALID_CONTRACT",
|
|
2685
|
+
"Workflow activity cannot use an economic status"
|
|
2686
|
+
);
|
|
2687
|
+
}
|
|
2688
|
+
}
|
|
2689
|
+
function assertWalletActivityPageRequest(request) {
|
|
2690
|
+
economyAssert(
|
|
2691
|
+
Number.isSafeInteger(request.limit) && request.limit >= 1 && request.limit <= 100,
|
|
2692
|
+
"INVALID_CONTRACT",
|
|
2693
|
+
"Activity page limit must be between one and 100"
|
|
2694
|
+
);
|
|
2695
|
+
if (request.cursor !== void 0) {
|
|
2696
|
+
assertOpaqueCursor(request.cursor);
|
|
2697
|
+
}
|
|
2698
|
+
const filter = request.filter;
|
|
2699
|
+
if (filter === void 0) {
|
|
2700
|
+
return;
|
|
2701
|
+
}
|
|
2702
|
+
if (filter.activityTypes !== void 0) {
|
|
2703
|
+
assertUniqueValues(filter.activityTypes, ACTIVITY_TYPES2, "Activity types");
|
|
2704
|
+
}
|
|
2705
|
+
if (filter.statuses !== void 0) {
|
|
2706
|
+
assertUniqueValues(filter.statuses, ACTIVITY_STATUSES2, "Activity statuses");
|
|
2707
|
+
}
|
|
2708
|
+
if (filter.sources !== void 0) {
|
|
2709
|
+
assertUniqueValues(filter.sources, ACTIVITY_SOURCES2, "Activity sources");
|
|
2710
|
+
}
|
|
2711
|
+
if (filter.beneficiaryAccountId !== void 0) {
|
|
2712
|
+
assertEconomyIdentifier(
|
|
2713
|
+
filter.beneficiaryAccountId,
|
|
2714
|
+
"beneficiaryAccountId"
|
|
2715
|
+
);
|
|
2716
|
+
}
|
|
2717
|
+
const from = filter.fromInclusive === void 0 ? void 0 : parseIsoTimestamp(filter.fromInclusive);
|
|
2718
|
+
const to = filter.toExclusive === void 0 ? void 0 : parseIsoTimestamp(filter.toExclusive);
|
|
2719
|
+
economyAssert(
|
|
2720
|
+
from === void 0 || to === void 0 || from < to,
|
|
2721
|
+
"INVALID_TIME_WINDOW",
|
|
2722
|
+
"Activity filter start must precede its exclusive end"
|
|
2723
|
+
);
|
|
2724
|
+
}
|
|
2725
|
+
function assertWalletActivityPage(page) {
|
|
2726
|
+
economyAssert(
|
|
2727
|
+
page.schemaVersion === ECONOMY_CONTRACT_VERSION && typeof page.hasMore === "boolean",
|
|
2728
|
+
"INVALID_CONTRACT",
|
|
2729
|
+
"Unsupported wallet-activity-page contract"
|
|
2730
|
+
);
|
|
2731
|
+
economyAssert(
|
|
2732
|
+
page.hasMore === (page.nextCursor !== void 0),
|
|
2733
|
+
"INVALID_CONTRACT",
|
|
2734
|
+
"Activity page cursor must be present exactly when more rows exist"
|
|
2735
|
+
);
|
|
2736
|
+
economyAssert(
|
|
2737
|
+
page.entries.length <= 100 && (!page.hasMore || page.entries.length > 0),
|
|
2738
|
+
"INVALID_CONTRACT",
|
|
2739
|
+
"Activity pages are bounded and cannot advertise more rows from an empty page"
|
|
2740
|
+
);
|
|
2741
|
+
if (page.nextCursor !== void 0) {
|
|
2742
|
+
assertOpaqueCursor(page.nextCursor);
|
|
2743
|
+
}
|
|
2744
|
+
const activityIds = /* @__PURE__ */ new Set();
|
|
2745
|
+
let previous;
|
|
2746
|
+
for (const entry of page.entries) {
|
|
2747
|
+
assertWalletActivityEntry(entry);
|
|
2748
|
+
economyAssert(
|
|
2749
|
+
!activityIds.has(entry.activityId),
|
|
2750
|
+
"DUPLICATE_IDENTIFIER",
|
|
2751
|
+
"Activity page cannot repeat an activity ID"
|
|
2752
|
+
);
|
|
2753
|
+
activityIds.add(entry.activityId);
|
|
2754
|
+
if (previous !== void 0) {
|
|
2755
|
+
const previousTime = parseIsoTimestamp(previous.occurredAt);
|
|
2756
|
+
const currentTime = parseIsoTimestamp(entry.occurredAt);
|
|
2757
|
+
economyAssert(
|
|
2758
|
+
previousTime > currentTime || previousTime === currentTime && previous.activityId.localeCompare(entry.activityId) > 0,
|
|
2759
|
+
"INVALID_CONTRACT",
|
|
2760
|
+
"Activity page must use stable descending time and ID order"
|
|
2761
|
+
);
|
|
2762
|
+
}
|
|
2763
|
+
previous = entry;
|
|
2764
|
+
}
|
|
2765
|
+
}
|
|
2766
|
+
function assertWalletActivityPageForPortfolio(page, scope) {
|
|
2767
|
+
assertWalletActivityPage(page);
|
|
2768
|
+
assertWalletPortfolioReadScope(scope);
|
|
2769
|
+
const walletIds = new Set(scope.components.map((item) => item.walletId));
|
|
2770
|
+
economyAssert(
|
|
2771
|
+
page.entries.every((entry) => walletIds.has(entry.walletId)),
|
|
2772
|
+
"INVALID_CONTRACT",
|
|
2773
|
+
"Portfolio activity contains a wallet outside its authorized read scope"
|
|
2774
|
+
);
|
|
2775
|
+
}
|
|
2776
|
+
|
|
2777
|
+
// src/recurrence.ts
|
|
2778
|
+
var BASELINE_MONTHLY_SUBSCRIPTION_PLAN = Object.freeze({
|
|
2779
|
+
schemaVersion: ECONOMY_CONTRACT_VERSION,
|
|
2780
|
+
planId: "monthly-10-100-v1",
|
|
2781
|
+
catalogVersion: "gbp-subscriptions-v1",
|
|
2782
|
+
interval: "monthly",
|
|
2783
|
+
priceMinorUnits: "1000",
|
|
2784
|
+
currency: "GBP",
|
|
2785
|
+
grantAmount: serializeTokenSubunits(100000n),
|
|
2786
|
+
enabled: false,
|
|
2787
|
+
effectiveFrom: "2026-07-15T00:00:00.000Z"
|
|
2788
|
+
});
|
|
2789
|
+
function assertTokenSubscriptionPlan(plan) {
|
|
2790
|
+
economyAssert(
|
|
2791
|
+
plan.schemaVersion === ECONOMY_CONTRACT_VERSION,
|
|
2792
|
+
"INVALID_CONTRACT",
|
|
2793
|
+
"Unsupported subscription-plan contract version"
|
|
2794
|
+
);
|
|
2795
|
+
assertEconomyIdentifier(plan.planId, "planId");
|
|
2796
|
+
assertEconomyIdentifier(plan.catalogVersion, "catalogVersion");
|
|
2797
|
+
economyAssert(
|
|
2798
|
+
["daily", "weekly", "monthly"].includes(plan.interval) && plan.currency === "GBP" && typeof plan.enabled === "boolean",
|
|
2799
|
+
"INVALID_CONTRACT",
|
|
2800
|
+
"Subscription recurrence, currency, or enabled state is invalid"
|
|
2801
|
+
);
|
|
2802
|
+
economyAssert(
|
|
2803
|
+
/^(?:0|[1-9][0-9]*)$/u.test(plan.priceMinorUnits) && BigInt(plan.priceMinorUnits) > 0n,
|
|
2804
|
+
"INVALID_AMOUNT",
|
|
2805
|
+
"Subscription price must be a positive integer minor-unit string"
|
|
2806
|
+
);
|
|
2807
|
+
const grantAmount = parseTokenSubunits(plan.grantAmount);
|
|
2808
|
+
economyAssert(
|
|
2809
|
+
grantAmount > 0n && isWholeTokenAmount(grantAmount),
|
|
2810
|
+
"AMOUNT_NOT_WHOLE_TOKEN",
|
|
2811
|
+
"Subscription grants must contain whole Tokens"
|
|
2812
|
+
);
|
|
2813
|
+
parseIsoTimestamp(plan.effectiveFrom);
|
|
2814
|
+
}
|
|
2815
|
+
|
|
2816
|
+
// src/spend-requests.ts
|
|
2817
|
+
function assertSpendRequest(request) {
|
|
2818
|
+
economyAssert(
|
|
2819
|
+
request.schemaVersion === ECONOMY_CONTRACT_VERSION,
|
|
2820
|
+
"INVALID_CONTRACT",
|
|
2821
|
+
"Unsupported spend-request contract version"
|
|
2822
|
+
);
|
|
2823
|
+
assertEconomyIdentifier(request.requestId, "requestId");
|
|
2824
|
+
assertEconomyIdentifier(request.childAccountId, "childAccountId");
|
|
2825
|
+
assertEconomyIdentifier(request.allocationId, "allocationId");
|
|
2826
|
+
economyAssert(
|
|
2827
|
+
request.purpose === "gameplay-conversion" || request.purpose === "non-gameplay",
|
|
2828
|
+
"INVALID_CONTRACT",
|
|
2829
|
+
"Spend request has an unsupported purpose"
|
|
2830
|
+
);
|
|
2831
|
+
economyAssert(
|
|
2832
|
+
[
|
|
2833
|
+
"requested",
|
|
2834
|
+
"approved",
|
|
2835
|
+
"rejected",
|
|
2836
|
+
"expired",
|
|
2837
|
+
"executed",
|
|
2838
|
+
"cancelled"
|
|
2839
|
+
].includes(request.status),
|
|
2840
|
+
"INVALID_CONTRACT",
|
|
2841
|
+
"Spend request has an unsupported status"
|
|
2842
|
+
);
|
|
2843
|
+
economyAssert(
|
|
2844
|
+
parseTokenSubunits(request.amount) > 0n,
|
|
2845
|
+
"INVALID_AMOUNT",
|
|
2846
|
+
"Spend-request amount must be positive"
|
|
2847
|
+
);
|
|
2848
|
+
const requestedAt = parseIsoTimestamp(request.requestedAt);
|
|
2849
|
+
const expiresAt = parseIsoTimestamp(request.expiresAt);
|
|
2850
|
+
economyAssert(
|
|
2851
|
+
expiresAt > requestedAt,
|
|
2852
|
+
"INVALID_TIME_WINDOW",
|
|
2853
|
+
"Spend-request expiry must follow creation"
|
|
2854
|
+
);
|
|
2855
|
+
const decisionRequired = ["approved", "rejected", "executed"].includes(
|
|
2856
|
+
request.status
|
|
2857
|
+
);
|
|
2858
|
+
economyAssert(
|
|
2859
|
+
!decisionRequired || request.decidedByAccountId !== void 0 && request.decidedAt !== void 0,
|
|
2860
|
+
"INVALID_CONTRACT",
|
|
2861
|
+
"Decided spend requests require a guardian actor and timestamp"
|
|
2862
|
+
);
|
|
2863
|
+
if (request.decidedByAccountId !== void 0) {
|
|
2864
|
+
assertEconomyIdentifier(request.decidedByAccountId, "decidedByAccountId");
|
|
2865
|
+
}
|
|
2866
|
+
if (request.decidedAt !== void 0) {
|
|
2867
|
+
const decidedAt = parseIsoTimestamp(request.decidedAt);
|
|
2868
|
+
economyAssert(
|
|
2869
|
+
decidedAt >= requestedAt && decidedAt <= expiresAt,
|
|
2870
|
+
"INVALID_TIME_WINDOW",
|
|
2871
|
+
"Spend-request decision must occur before expiry"
|
|
2872
|
+
);
|
|
2873
|
+
}
|
|
2874
|
+
if (request.approvalNonceHash !== void 0) {
|
|
2875
|
+
economyAssert(
|
|
2876
|
+
/^sha256:[a-f0-9]{64}$/u.test(request.approvalNonceHash),
|
|
2877
|
+
"INVALID_CONTRACT",
|
|
2878
|
+
"Approval nonces must be stored as SHA-256 hashes"
|
|
2879
|
+
);
|
|
2880
|
+
}
|
|
2881
|
+
if (request.status === "approved" || request.status === "executed") {
|
|
2882
|
+
economyAssert(
|
|
2883
|
+
request.approvalNonceHash !== void 0,
|
|
2884
|
+
"INVALID_CONTRACT",
|
|
2885
|
+
"Approved spend requests require a one-time approval nonce hash"
|
|
2886
|
+
);
|
|
2887
|
+
}
|
|
2888
|
+
if (request.resultingTransactionId !== void 0) {
|
|
2889
|
+
assertEconomyIdentifier(
|
|
2890
|
+
request.resultingTransactionId,
|
|
2891
|
+
"resultingTransactionId"
|
|
2892
|
+
);
|
|
2893
|
+
economyAssert(
|
|
2894
|
+
request.status === "executed",
|
|
2895
|
+
"INVALID_CONTRACT",
|
|
2896
|
+
"Only executed spend requests may reference a ledger transaction"
|
|
2897
|
+
);
|
|
2898
|
+
}
|
|
2899
|
+
}
|
|
1665
2900
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1666
2901
|
0 && (module.exports = {
|
|
1667
2902
|
BASELINE_GBP_CATALOG_VERSION,
|
|
@@ -1669,20 +2904,31 @@ function assertWalletLifetimeTotals(totals) {
|
|
|
1669
2904
|
BASELINE_GBP_TOKEN_PACKS,
|
|
1670
2905
|
BASELINE_MONTHLY_SUBSCRIPTION_PLAN,
|
|
1671
2906
|
BASELINE_PURCHASE_LIMIT_POLICY,
|
|
2907
|
+
EARLY_BACKER_SETTLEMENT_POLICY_V2,
|
|
1672
2908
|
ECONOMY_CONTRACT_VERSION,
|
|
1673
2909
|
EconomyError,
|
|
1674
2910
|
PRE_UTILITY_BACKER_COHORT,
|
|
1675
2911
|
TOKEN_SUBUNITS_MAX,
|
|
1676
2912
|
TOKEN_SUBUNITS_MIN,
|
|
1677
2913
|
TOKEN_SUBUNITS_PER_TOKEN,
|
|
2914
|
+
advanceJournalChainHead,
|
|
2915
|
+
applySourceLotMovement,
|
|
1678
2916
|
applyTransactionToProjection,
|
|
2917
|
+
applyWalletBalanceDelta,
|
|
2918
|
+
applyWalletLifetimeDeltas,
|
|
1679
2919
|
assertActivityEntry,
|
|
1680
2920
|
assertBalancedTransaction,
|
|
2921
|
+
assertDeterministicWalletBalanceSummary,
|
|
1681
2922
|
assertDistinctAdjustmentApproval,
|
|
2923
|
+
assertEconomicJournalTransaction,
|
|
2924
|
+
assertEconomyCommandEnvelope,
|
|
2925
|
+
assertEconomyCommandWorkflowEvent,
|
|
2926
|
+
assertEconomyIdempotencyScope,
|
|
1682
2927
|
assertEconomyIdentifier,
|
|
1683
2928
|
assertFlatTokenCatalog,
|
|
1684
2929
|
assertFxSnapshot,
|
|
1685
2930
|
assertGameplayAllocation,
|
|
2931
|
+
assertJournalChainHead,
|
|
1686
2932
|
assertNonNegativeAccounts,
|
|
1687
2933
|
assertOpenPurchaseIntent,
|
|
1688
2934
|
assertPurchaseIntentBinding,
|
|
@@ -1690,24 +2936,46 @@ function assertWalletLifetimeTotals(totals) {
|
|
|
1690
2936
|
assertReversalAvailable,
|
|
1691
2937
|
assertRewardConversion,
|
|
1692
2938
|
assertRewardRate,
|
|
2939
|
+
assertScopedPersistedIdempotencyResult,
|
|
1693
2940
|
assertSourceLot,
|
|
2941
|
+
assertSourceLotMovement,
|
|
1694
2942
|
assertSpendRequest,
|
|
1695
2943
|
assertTokenPack,
|
|
1696
2944
|
assertTokenReferenceRate,
|
|
1697
2945
|
assertTokenSubscriptionPlan,
|
|
2946
|
+
assertVersionedSourceLot,
|
|
1698
2947
|
assertWallet,
|
|
2948
|
+
assertWalletActivityEntry,
|
|
2949
|
+
assertWalletActivityPage,
|
|
2950
|
+
assertWalletActivityPageForPortfolio,
|
|
2951
|
+
assertWalletActivityPageRequest,
|
|
2952
|
+
assertWalletBalanceDelta,
|
|
2953
|
+
assertWalletBalanceDeltaBatch,
|
|
2954
|
+
assertWalletBalanceProjection,
|
|
1699
2955
|
assertWalletBalanceSummary,
|
|
2956
|
+
assertWalletLifetimeSnapshot,
|
|
1700
2957
|
assertWalletLifetimeTotals,
|
|
2958
|
+
assertWalletOwnerReference,
|
|
2959
|
+
assertWalletPortfolioLifetime,
|
|
2960
|
+
assertWalletPortfolioReadScope,
|
|
2961
|
+
assertWalletPortfolioSummary,
|
|
1701
2962
|
availableSourceLotAmount,
|
|
1702
2963
|
boostGameplayAllocation,
|
|
1703
2964
|
canUseSourceLot,
|
|
1704
2965
|
canonicalTransactionPayload,
|
|
1705
2966
|
convertRewardPayout,
|
|
1706
2967
|
createGameplayAllocation,
|
|
2968
|
+
createInitialSourceLotSnapshot,
|
|
1707
2969
|
createProjectionSnapshot,
|
|
1708
2970
|
createReversalTransaction,
|
|
2971
|
+
createWalletBalanceSummary,
|
|
2972
|
+
createWalletPortfolioLifetime,
|
|
2973
|
+
createWalletPortfolioSummary,
|
|
2974
|
+
deriveWalletBalanceDeltas,
|
|
2975
|
+
deriveWalletLifetimeDeltas,
|
|
1709
2976
|
economyAssert,
|
|
1710
2977
|
evaluateEarlyBacker,
|
|
2978
|
+
evaluateEarlyBackerBySettlementV2,
|
|
1711
2979
|
isWholeTokenAmount,
|
|
1712
2980
|
mapBitLabsConversionState,
|
|
1713
2981
|
parseIsoTimestamp,
|