@ocap/util 1.30.26 → 1.31.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/esm/amm.d.mts +158 -0
- package/esm/amm.mjs +269 -0
- package/esm/arcade.d.mts +217 -0
- package/esm/arcade.mjs +484 -0
- package/esm/bonding-reserve-aup.d.mts +125 -0
- package/esm/bonding-reserve-aup.mjs +230 -0
- package/esm/composable-container.d.mts +141 -0
- package/esm/composable-container.mjs +264 -0
- package/esm/conditional-market.d.mts +151 -0
- package/esm/conditional-market.mjs +376 -0
- package/esm/curve.d.mts +43 -5
- package/esm/curve.mjs +71 -9
- package/esm/foreign-receipt.d.mts +126 -0
- package/esm/foreign-receipt.mjs +388 -0
- package/esm/index.d.mts +8 -1
- package/esm/index.mjs +8 -1
- package/esm/node_modules/@noble/hashes/_md.mjs +168 -0
- package/esm/node_modules/@noble/hashes/sha2.mjs +185 -0
- package/esm/node_modules/@noble/hashes/utils.mjs +175 -0
- package/lib/amm.cjs +279 -0
- package/lib/amm.d.cts +158 -0
- package/lib/arcade.cjs +491 -0
- package/lib/arcade.d.cts +217 -0
- package/lib/bonding-reserve-aup.cjs +232 -0
- package/lib/bonding-reserve-aup.d.cts +125 -0
- package/lib/composable-container.cjs +270 -0
- package/lib/composable-container.d.cts +141 -0
- package/lib/conditional-market.cjs +377 -0
- package/lib/conditional-market.d.cts +151 -0
- package/lib/curve.cjs +73 -9
- package/lib/curve.d.cts +43 -5
- package/lib/foreign-receipt.cjs +396 -0
- package/lib/foreign-receipt.d.cts +126 -0
- package/lib/index.cjs +57 -1
- package/lib/index.d.cts +8 -1
- package/lib/node_modules/@noble/hashes/_md.cjs +171 -0
- package/lib/node_modules/@noble/hashes/sha2.cjs +185 -0
- package/lib/node_modules/@noble/hashes/utils.cjs +183 -0
- package/package.json +2 -2
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/composable-container.ts
|
|
3
|
+
function parseNonNegativeBigInt(value, label) {
|
|
4
|
+
let parsed;
|
|
5
|
+
if (typeof value === "bigint") parsed = value;
|
|
6
|
+
else if (typeof value === "number") {
|
|
7
|
+
if (!Number.isSafeInteger(value)) throw new Error(`Invalid ${label}: must be a safe integer`);
|
|
8
|
+
parsed = BigInt(value);
|
|
9
|
+
} else if (typeof value === "string") {
|
|
10
|
+
const trimmed = value.trim();
|
|
11
|
+
if (!/^-?\d+$/.test(trimmed)) throw new Error(`Invalid ${label}: must be a base-10 integer string`);
|
|
12
|
+
parsed = BigInt(trimmed);
|
|
13
|
+
} else throw new Error(`Invalid ${label}: must be an integer`);
|
|
14
|
+
if (parsed < 0n) throw new Error(`Invalid ${label}: must be non-negative`);
|
|
15
|
+
return parsed;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Creates a new ComposableAssetNode with verified initial floor reserve invariants.
|
|
19
|
+
*/
|
|
20
|
+
function createComposableAssetNode(params) {
|
|
21
|
+
if (!params.did || !params.did.trim()) throw new Error("Asset did is required");
|
|
22
|
+
if (!params.ownerDid || !params.ownerDid.trim()) throw new Error("Asset ownerDid is required");
|
|
23
|
+
if (!params.category || !params.category.trim()) throw new Error("Asset category is required");
|
|
24
|
+
const baseFloor = parseNonNegativeBigInt(params.baseFloorReserveValue, "baseFloorReserveValue");
|
|
25
|
+
const normalizedSlots = [];
|
|
26
|
+
if (params.slotDefinitions) {
|
|
27
|
+
const seenSlotKeys = /* @__PURE__ */ new Set();
|
|
28
|
+
for (const def of params.slotDefinitions) {
|
|
29
|
+
const slotKey = def.slotKey?.trim();
|
|
30
|
+
if (!slotKey) throw new Error("Slot definition requires a non-empty slotKey");
|
|
31
|
+
if (seenSlotKeys.has(slotKey)) throw new Error(`DUPLICATE_SLOT_KEY: duplicate slot definition ${slotKey}`);
|
|
32
|
+
seenSlotKeys.add(slotKey);
|
|
33
|
+
if (!Array.isArray(def.allowedCategories) || def.allowedCategories.length === 0) throw new Error(`Slot ${slotKey} requires at least one allowedCategory`);
|
|
34
|
+
normalizedSlots.push({
|
|
35
|
+
...def,
|
|
36
|
+
slotKey,
|
|
37
|
+
allowedCategories: def.allowedCategories.map((c) => c.trim()).filter(Boolean)
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
did: params.did.trim(),
|
|
43
|
+
ownerDid: params.ownerDid.trim(),
|
|
44
|
+
category: params.category.trim(),
|
|
45
|
+
baseFloorReserveValue: baseFloor,
|
|
46
|
+
aggregatedFloorReserveValue: baseFloor,
|
|
47
|
+
mountedInParentDid: null,
|
|
48
|
+
independentRedemptionFrozen: false,
|
|
49
|
+
slotDefinitions: normalizedSlots,
|
|
50
|
+
mountedSlots: {}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Checks ancestry and subtree depth in a registry of ComposableAssetNodes to prevent
|
|
55
|
+
* circular container mounting (`A -> B -> A`) and enforce `maxDepth` in both top-down
|
|
56
|
+
* and bottom-up mounting orders.
|
|
57
|
+
*/
|
|
58
|
+
function assertNoContainerCycle(parentDid, childDid, assetRegistry, maxDepth = 8) {
|
|
59
|
+
if (!Number.isSafeInteger(maxDepth) || maxDepth < 1) throw new Error("Invalid maxDepth: must be a positive integer");
|
|
60
|
+
if (parentDid === childDid) throw new Error(`CONTAINER_CYCLE_DETECTED: asset ${parentDid} cannot be mounted into itself`);
|
|
61
|
+
let cursor = parentDid;
|
|
62
|
+
let upwardDepth = 0;
|
|
63
|
+
const visited = new Set([childDid]);
|
|
64
|
+
while (cursor) {
|
|
65
|
+
if (visited.has(cursor)) throw new Error(`CONTAINER_CYCLE_DETECTED: mounting ${childDid} into ${parentDid} creates a cycle at ${cursor}`);
|
|
66
|
+
visited.add(cursor);
|
|
67
|
+
upwardDepth += 1;
|
|
68
|
+
if (upwardDepth > maxDepth) throw new Error(`MAX_CONTAINER_DEPTH_EXCEEDED: container hierarchy exceeds maxDepth ${maxDepth}`);
|
|
69
|
+
cursor = Object.hasOwn(assetRegistry, cursor) ? assetRegistry[cursor]?.mountedInParentDid ?? null : null;
|
|
70
|
+
}
|
|
71
|
+
const measureSubtreeHeight = (did, pathSeen) => {
|
|
72
|
+
const node = Object.hasOwn(assetRegistry, did) ? assetRegistry[did] : void 0;
|
|
73
|
+
if (!node?.mountedSlots) return 0;
|
|
74
|
+
let maxChildHeight = 0;
|
|
75
|
+
for (const slot of Object.values(node.mountedSlots)) {
|
|
76
|
+
if (pathSeen.has(slot.childDid)) throw new Error(`CONTAINER_CYCLE_DETECTED: cycle detected in child subtree at ${slot.childDid}`);
|
|
77
|
+
const nextSeen = new Set(pathSeen);
|
|
78
|
+
nextSeen.add(slot.childDid);
|
|
79
|
+
const h = 1 + measureSubtreeHeight(slot.childDid, nextSeen);
|
|
80
|
+
if (h > maxChildHeight) maxChildHeight = h;
|
|
81
|
+
}
|
|
82
|
+
return maxChildHeight;
|
|
83
|
+
};
|
|
84
|
+
const childSubtreeHeight = measureSubtreeHeight(childDid, visited);
|
|
85
|
+
if (upwardDepth + childSubtreeHeight > maxDepth) throw new Error(`MAX_CONTAINER_DEPTH_EXCEEDED: container hierarchy exceeds maxDepth ${maxDepth}`);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Mounts a child NFT (e.g. a mecha part, furniture item, slot machine, or card game table)
|
|
89
|
+
* into a parent container NFT slot in either `OWNED` or `LEASED` mode.
|
|
90
|
+
*
|
|
91
|
+
* Security & Economic Invariants:
|
|
92
|
+
* - Enforces bottom-up mounting: `parent` itself must be unmounted (`!parent.mountedInParentDid && !parent.independentRedemptionFrozen`)
|
|
93
|
+
* so ancestor `aggregatedFloorReserveValue` can never be desynchronized.
|
|
94
|
+
* - `OWNED` mode: `parent.ownerDid === child.ownerDid === callerDid`. Freezes child's independent
|
|
95
|
+
* redemption and adds `child.aggregatedFloorReserveValue` into `parent.aggregatedFloorReserveValue`.
|
|
96
|
+
* Forbids mounting a child container that holds `LEASED` slots inside an outer container.
|
|
97
|
+
* - `LEASED` mode: `parent.ownerDid === leaseTerms.lesseeHostDid` and `child.ownerDid === leaseTerms.lessorDid`.
|
|
98
|
+
* Freezes child's independent floor burn while mounted, but does NOT merge the lessor's floor
|
|
99
|
+
* reserve into the host's container floor value (so the island host can never strip or burn
|
|
100
|
+
* a leased machine's bankroll).
|
|
101
|
+
*/
|
|
102
|
+
function mountContainerSlotItem(params) {
|
|
103
|
+
const { parent, child, slotKey, mountMode, callerDid, timestamp } = params;
|
|
104
|
+
if (!Number.isSafeInteger(timestamp) || timestamp < 0) throw new Error("timestamp must be a non-negative integer");
|
|
105
|
+
const registry = {
|
|
106
|
+
...params.assetRegistry ?? {},
|
|
107
|
+
[parent.did]: parent,
|
|
108
|
+
[child.did]: child
|
|
109
|
+
};
|
|
110
|
+
assertNoContainerCycle(parent.did, child.did, registry, params.maxDepth ?? 8);
|
|
111
|
+
if (parent.mountedInParentDid || parent.independentRedemptionFrozen) throw new Error(`PARENT_ALREADY_MOUNTED: container ${parent.did} is currently mounted in ${parent.mountedInParentDid ?? "another container"} and cannot mount child items until unmounted`);
|
|
112
|
+
if (child.mountedInParentDid || child.independentRedemptionFrozen) throw new Error(`CHILD_ALREADY_MOUNTED: asset ${child.did} is already mounted in ${child.mountedInParentDid}`);
|
|
113
|
+
for (const childSlot of Object.values(child.mountedSlots)) if (childSlot.mountMode === "LEASED") throw new Error(`CHILD_HAS_LEASED_SLOTS: child container ${child.did} holds leased item ${childSlot.childDid} and cannot be mounted into ${parent.did}`);
|
|
114
|
+
const slotDef = parent.slotDefinitions.find((s) => s.slotKey === slotKey);
|
|
115
|
+
if (!slotDef) throw new Error(`SLOT_NOT_FOUND: slot ${slotKey} is not defined on container ${parent.did}`);
|
|
116
|
+
if (Object.hasOwn(parent.mountedSlots, slotKey)) throw new Error(`SLOT_OCCUPIED: slot ${slotKey} on container ${parent.did} is already occupied`);
|
|
117
|
+
if (!slotDef.allowedCategories.includes(child.category)) throw new Error(`SLOT_CATEGORY_MISMATCH: child category ${child.category} is not allowed in slot ${slotKey} (expected ${slotDef.allowedCategories.join(", ")})`);
|
|
118
|
+
if (!(slotDef.allowedMountModes ?? ["OWNED", "LEASED"]).includes(mountMode)) throw new Error(`SLOT_MOUNT_MODE_FORBIDDEN: mountMode ${mountMode} is not permitted on slot ${slotKey}`);
|
|
119
|
+
if (mountMode === "OWNED") {
|
|
120
|
+
if (parent.ownerDid !== callerDid || child.ownerDid !== callerDid) throw new Error("OWNERSHIP_MISMATCH: OWNED mount requires callerDid to own both parent container and child item");
|
|
121
|
+
} else {
|
|
122
|
+
const terms = params.leaseTerms;
|
|
123
|
+
if (!terms) throw new Error("LEASE_TERMS_REQUIRED: LEASED mount requires leaseTerms");
|
|
124
|
+
if (terms.lesseeHostDid !== parent.ownerDid || terms.lessorDid !== child.ownerDid) throw new Error("LEASE_PARTY_MISMATCH: lesseeHostDid must own parent container and lessorDid must own child NFT");
|
|
125
|
+
if (callerDid !== terms.lesseeHostDid && callerDid !== terms.lessorDid) throw new Error("LEASE_UNAUTHORIZED: callerDid must be either lessorDid or lesseeHostDid");
|
|
126
|
+
if (!Number.isSafeInteger(terms.hostBps) || terms.hostBps < 0 || terms.hostBps > 1e4) throw new Error("Invalid leaseTerms.hostBps: must be an integer between 0 and 10000");
|
|
127
|
+
if (!Number.isSafeInteger(terms.leaseExpiresAt) || terms.leaseExpiresAt <= timestamp) throw new Error("Invalid leaseTerms.leaseExpiresAt: must be greater than mount timestamp");
|
|
128
|
+
}
|
|
129
|
+
const assignment = {
|
|
130
|
+
slotKey,
|
|
131
|
+
childDid: child.did,
|
|
132
|
+
childCategory: child.category,
|
|
133
|
+
mountMode,
|
|
134
|
+
childFloorReserveValue: child.aggregatedFloorReserveValue,
|
|
135
|
+
mountedAt: timestamp,
|
|
136
|
+
leaseTerms: mountMode === "LEASED" ? { ...params.leaseTerms } : void 0
|
|
137
|
+
};
|
|
138
|
+
const nextParentAggregatedFloor = mountMode === "OWNED" ? parent.aggregatedFloorReserveValue + child.aggregatedFloorReserveValue : parent.aggregatedFloorReserveValue;
|
|
139
|
+
return {
|
|
140
|
+
parent: {
|
|
141
|
+
...parent,
|
|
142
|
+
aggregatedFloorReserveValue: nextParentAggregatedFloor,
|
|
143
|
+
mountedSlots: {
|
|
144
|
+
...parent.mountedSlots,
|
|
145
|
+
[slotKey]: assignment
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
child: {
|
|
149
|
+
...child,
|
|
150
|
+
mountedInParentDid: parent.did,
|
|
151
|
+
independentRedemptionFrozen: true
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Unmounts a child NFT from a parent container slot and restores its independent redemption status.
|
|
157
|
+
*
|
|
158
|
+
* - Enforces top-down unmounting: `parent` itself must be unmounted (`!parent.mountedInParentDid && !parent.independentRedemptionFrozen`).
|
|
159
|
+
* - For `OWNED` mounts: only `parent.ownerDid` can unmount, and `childFloorReserveValue` is subtracted
|
|
160
|
+
* from `parent.aggregatedFloorReserveValue`.
|
|
161
|
+
* - For `LEASED` mounts: `lesseeHostDid` can unmount at any time; `lessorDid` can unmount once
|
|
162
|
+
* `timestamp >= leaseExpiresAt` (or anytime if `allowEarlyLessorRecall` is true).
|
|
163
|
+
*/
|
|
164
|
+
function unmountContainerSlotItem(params) {
|
|
165
|
+
const { parent, child, slotKey, callerDid, timestamp } = params;
|
|
166
|
+
if (!Number.isSafeInteger(timestamp) || timestamp < 0) throw new Error("timestamp must be a non-negative integer");
|
|
167
|
+
if (parent.mountedInParentDid || parent.independentRedemptionFrozen) throw new Error(`PARENT_ALREADY_MOUNTED: container ${parent.did} is currently mounted in ${parent.mountedInParentDid ?? "another container"} and cannot unmount child items until unmounted first`);
|
|
168
|
+
const assignment = Object.hasOwn(parent.mountedSlots, slotKey) ? parent.mountedSlots[slotKey] : void 0;
|
|
169
|
+
if (!assignment || assignment.childDid !== child.did) throw new Error(`SLOT_CHILD_MISMATCH: child ${child.did} is not mounted in slot ${slotKey} of ${parent.did}`);
|
|
170
|
+
if (assignment.mountMode === "OWNED") {
|
|
171
|
+
if (callerDid !== parent.ownerDid) throw new Error("UNMOUNT_UNAUTHORIZED: only parent container owner can unmount OWNED slot item");
|
|
172
|
+
} else {
|
|
173
|
+
const terms = assignment.leaseTerms;
|
|
174
|
+
const isLessee = callerDid === terms.lesseeHostDid;
|
|
175
|
+
const isLessor = callerDid === terms.lessorDid;
|
|
176
|
+
if (!isLessee && !isLessor) throw new Error("UNMOUNT_UNAUTHORIZED: only lessor or lessee host can unmount LEASED slot item");
|
|
177
|
+
if (isLessor && !isLessee && !terms.allowEarlyLessorRecall && !(timestamp >= terms.leaseExpiresAt)) throw new Error(`LEASE_NOT_EXPIRED: lessor ${callerDid} cannot recall leased machine before ${terms.leaseExpiresAt}`);
|
|
178
|
+
}
|
|
179
|
+
const nextMountedSlots = { ...parent.mountedSlots };
|
|
180
|
+
delete nextMountedSlots[slotKey];
|
|
181
|
+
const nextParentAggregatedFloor = assignment.mountMode === "OWNED" ? parent.aggregatedFloorReserveValue - assignment.childFloorReserveValue : parent.aggregatedFloorReserveValue;
|
|
182
|
+
return {
|
|
183
|
+
parent: {
|
|
184
|
+
...parent,
|
|
185
|
+
aggregatedFloorReserveValue: nextParentAggregatedFloor,
|
|
186
|
+
mountedSlots: nextMountedSlots
|
|
187
|
+
},
|
|
188
|
+
child: {
|
|
189
|
+
...child,
|
|
190
|
+
mountedInParentDid: null,
|
|
191
|
+
independentRedemptionFrozen: false
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Verifies whether an asset node is eligible for floor-burn redemption or transfer:
|
|
197
|
+
* - Mounted children (`independentRedemptionFrozen === true`) cannot be independently burned or transferred.
|
|
198
|
+
* - A parent container holding a `LEASED` child cannot be burned or transferred until all leased
|
|
199
|
+
* machines are unmounted and returned to their lessors.
|
|
200
|
+
*/
|
|
201
|
+
function assertAssetRedeemableOrTransferable(asset, operation) {
|
|
202
|
+
if (asset.independentRedemptionFrozen || asset.mountedInParentDid) throw new Error(`ASSET_MOUNTED_FROZEN: asset ${asset.did} is currently mounted in ${asset.mountedInParentDid} and cannot perform ${operation}`);
|
|
203
|
+
for (const assignment of Object.values(asset.mountedSlots)) if (assignment.mountMode === "LEASED") throw new Error(`CONTAINER_HAS_LEASED_SLOTS: container ${asset.did} has leased item ${assignment.childDid} in slot ${assignment.slotKey} and cannot perform ${operation}`);
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Executes a deterministic N-to-1 consumable alchemy synthesis (`synthesizeContainerRecipe`):
|
|
207
|
+
* - Verifies all ingredient assets belong to `callerDid`, are unmounted, and have no mounted slots.
|
|
208
|
+
* - Matches ingredient categories against `recipe.requirements` (rejecting duplicate category rules).
|
|
209
|
+
* - Conserves underlying floor reserve value:
|
|
210
|
+
* `netOutputFloorReserve + protocolFeeReserve === sum(input.aggregatedFloorReserveValue) + extraCatalystReserveAdded`.
|
|
211
|
+
*/
|
|
212
|
+
function synthesizeContainerRecipe(params) {
|
|
213
|
+
const { recipe, ingredients, outputDid, callerDid } = params;
|
|
214
|
+
if (!Array.isArray(ingredients) || ingredients.length === 0) throw new Error("Synthesis requires at least one ingredient asset");
|
|
215
|
+
if (!Array.isArray(recipe.requirements) || recipe.requirements.length === 0) throw new Error("Synthesis recipe requires at least one requirement");
|
|
216
|
+
const extraReserve = params.extraCatalystReserveAdded !== void 0 ? parseNonNegativeBigInt(params.extraCatalystReserveAdded, "extraCatalystReserveAdded") : 0n;
|
|
217
|
+
const requiredExtra = recipe.extraCatalystFeeReserve !== void 0 ? parseNonNegativeBigInt(recipe.extraCatalystFeeReserve, "recipe.extraCatalystFeeReserve") : 0n;
|
|
218
|
+
if (extraReserve < requiredExtra) throw new Error(`INSUFFICIENT_CATALYST_RESERVE: recipe requires at least ${requiredExtra.toString()} extra reserve`);
|
|
219
|
+
const seenDids = /* @__PURE__ */ new Set();
|
|
220
|
+
const categoryCounts = Object.create(null);
|
|
221
|
+
let totalInputReserve = 0n;
|
|
222
|
+
for (const item of ingredients) {
|
|
223
|
+
if (seenDids.has(item.did)) throw new Error(`DUPLICATE_INGREDIENT: asset ${item.did} supplied multiple times`);
|
|
224
|
+
seenDids.add(item.did);
|
|
225
|
+
if (item.ownerDid !== callerDid) throw new Error(`INGREDIENT_OWNER_MISMATCH: caller ${callerDid} does not own ingredient ${item.did}`);
|
|
226
|
+
assertAssetRedeemableOrTransferable(item, "REDEEM_BURN");
|
|
227
|
+
if (Object.keys(item.mountedSlots).length > 0) throw new Error(`INGREDIENT_HAS_MOUNTED_SLOTS: ingredient ${item.did} has mounted items and must be emptied before synthesis`);
|
|
228
|
+
categoryCounts[item.category] = (categoryCounts[item.category] ?? 0) + 1;
|
|
229
|
+
totalInputReserve += item.aggregatedFloorReserveValue;
|
|
230
|
+
}
|
|
231
|
+
if (outputDid && seenDids.has(outputDid.trim())) throw new Error(`OUTPUT_DID_COLLISION: outputDid ${outputDid} collides with an input ingredient DID`);
|
|
232
|
+
let totalRequiredCount = 0;
|
|
233
|
+
const seenCategories = /* @__PURE__ */ new Set();
|
|
234
|
+
for (const req of recipe.requirements) {
|
|
235
|
+
const category = req.category?.trim();
|
|
236
|
+
if (!category) throw new Error("Requirement category is required");
|
|
237
|
+
if (seenCategories.has(category)) throw new Error(`DUPLICATE_RECIPE_CATEGORY: duplicate requirement category ${category}`);
|
|
238
|
+
seenCategories.add(category);
|
|
239
|
+
if (!Number.isSafeInteger(req.count) || req.count <= 0) throw new Error(`Invalid requirement count for category ${category}`);
|
|
240
|
+
totalRequiredCount += req.count;
|
|
241
|
+
if ((categoryCounts[category] ?? 0) !== req.count) throw new Error(`RECIPE_REQUIREMENT_MISMATCH: expected ${req.count} of ${category}, got ${categoryCounts[category] ?? 0}`);
|
|
242
|
+
}
|
|
243
|
+
if (ingredients.length !== totalRequiredCount) throw new Error("RECIPE_EXTRA_INGREDIENTS: unexpected ingredient categories supplied");
|
|
244
|
+
const protocolFeeBps = recipe.protocolFeeBps ?? 0;
|
|
245
|
+
if (!Number.isSafeInteger(protocolFeeBps) || protocolFeeBps < 0 || protocolFeeBps > 5e3) throw new Error("Invalid recipe.protocolFeeBps: must be an integer between 0 and 5000");
|
|
246
|
+
const combinedReserve = totalInputReserve + extraReserve;
|
|
247
|
+
const protocolFeeReserve = combinedReserve * BigInt(protocolFeeBps) / 10000n;
|
|
248
|
+
const netOutputFloorReserve = combinedReserve - protocolFeeReserve;
|
|
249
|
+
return {
|
|
250
|
+
outputAsset: createComposableAssetNode({
|
|
251
|
+
did: outputDid,
|
|
252
|
+
ownerDid: callerDid,
|
|
253
|
+
category: recipe.outputCategory,
|
|
254
|
+
baseFloorReserveValue: netOutputFloorReserve,
|
|
255
|
+
slotDefinitions: params.outputSlotDefinitions
|
|
256
|
+
}),
|
|
257
|
+
burnedInputDids: ingredients.map((i) => i.did),
|
|
258
|
+
totalInputReserveConsumed: totalInputReserve,
|
|
259
|
+
protocolFeeReserve,
|
|
260
|
+
netOutputFloorReserve
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
//#endregion
|
|
265
|
+
exports.assertAssetRedeemableOrTransferable = assertAssetRedeemableOrTransferable;
|
|
266
|
+
exports.assertNoContainerCycle = assertNoContainerCycle;
|
|
267
|
+
exports.createComposableAssetNode = createComposableAssetNode;
|
|
268
|
+
exports.mountContainerSlotItem = mountContainerSlotItem;
|
|
269
|
+
exports.synthesizeContainerRecipe = synthesizeContainerRecipe;
|
|
270
|
+
exports.unmountContainerSlotItem = unmountContainerSlotItem;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
//#region src/composable-container.d.ts
|
|
2
|
+
type SlotMountMode = 'OWNED' | 'LEASED';
|
|
3
|
+
interface ContainerSlotDefinition {
|
|
4
|
+
slotKey: string;
|
|
5
|
+
allowedCategories: readonly string[];
|
|
6
|
+
allowedMountModes?: readonly SlotMountMode[];
|
|
7
|
+
required?: boolean;
|
|
8
|
+
}
|
|
9
|
+
interface LeaseMountTerms {
|
|
10
|
+
lessorDid: string;
|
|
11
|
+
lesseeHostDid: string;
|
|
12
|
+
hostBps: number;
|
|
13
|
+
leaseExpiresAt: number;
|
|
14
|
+
allowEarlyLessorRecall?: boolean;
|
|
15
|
+
}
|
|
16
|
+
interface MountedSlotAssignment {
|
|
17
|
+
slotKey: string;
|
|
18
|
+
childDid: string;
|
|
19
|
+
childCategory: string;
|
|
20
|
+
mountMode: SlotMountMode;
|
|
21
|
+
childFloorReserveValue: bigint;
|
|
22
|
+
mountedAt: number;
|
|
23
|
+
leaseTerms?: LeaseMountTerms;
|
|
24
|
+
}
|
|
25
|
+
interface ComposableAssetNode {
|
|
26
|
+
did: string;
|
|
27
|
+
ownerDid: string;
|
|
28
|
+
category: string;
|
|
29
|
+
baseFloorReserveValue: bigint;
|
|
30
|
+
aggregatedFloorReserveValue: bigint;
|
|
31
|
+
mountedInParentDid?: string | null;
|
|
32
|
+
independentRedemptionFrozen: boolean;
|
|
33
|
+
slotDefinitions: readonly ContainerSlotDefinition[];
|
|
34
|
+
mountedSlots: Record<string, MountedSlotAssignment>;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Creates a new ComposableAssetNode with verified initial floor reserve invariants.
|
|
38
|
+
*/
|
|
39
|
+
declare function createComposableAssetNode(params: {
|
|
40
|
+
did: string;
|
|
41
|
+
ownerDid: string;
|
|
42
|
+
category: string;
|
|
43
|
+
baseFloorReserveValue: bigint | number | string;
|
|
44
|
+
slotDefinitions?: readonly ContainerSlotDefinition[];
|
|
45
|
+
}): ComposableAssetNode;
|
|
46
|
+
/**
|
|
47
|
+
* Checks ancestry and subtree depth in a registry of ComposableAssetNodes to prevent
|
|
48
|
+
* circular container mounting (`A -> B -> A`) and enforce `maxDepth` in both top-down
|
|
49
|
+
* and bottom-up mounting orders.
|
|
50
|
+
*/
|
|
51
|
+
declare function assertNoContainerCycle(parentDid: string, childDid: string, assetRegistry: Record<string, Pick<ComposableAssetNode, 'did' | 'mountedInParentDid'> & Partial<Pick<ComposableAssetNode, 'mountedSlots'>>>, maxDepth?: number): void;
|
|
52
|
+
/**
|
|
53
|
+
* Mounts a child NFT (e.g. a mecha part, furniture item, slot machine, or card game table)
|
|
54
|
+
* into a parent container NFT slot in either `OWNED` or `LEASED` mode.
|
|
55
|
+
*
|
|
56
|
+
* Security & Economic Invariants:
|
|
57
|
+
* - Enforces bottom-up mounting: `parent` itself must be unmounted (`!parent.mountedInParentDid && !parent.independentRedemptionFrozen`)
|
|
58
|
+
* so ancestor `aggregatedFloorReserveValue` can never be desynchronized.
|
|
59
|
+
* - `OWNED` mode: `parent.ownerDid === child.ownerDid === callerDid`. Freezes child's independent
|
|
60
|
+
* redemption and adds `child.aggregatedFloorReserveValue` into `parent.aggregatedFloorReserveValue`.
|
|
61
|
+
* Forbids mounting a child container that holds `LEASED` slots inside an outer container.
|
|
62
|
+
* - `LEASED` mode: `parent.ownerDid === leaseTerms.lesseeHostDid` and `child.ownerDid === leaseTerms.lessorDid`.
|
|
63
|
+
* Freezes child's independent floor burn while mounted, but does NOT merge the lessor's floor
|
|
64
|
+
* reserve into the host's container floor value (so the island host can never strip or burn
|
|
65
|
+
* a leased machine's bankroll).
|
|
66
|
+
*/
|
|
67
|
+
declare function mountContainerSlotItem(params: {
|
|
68
|
+
parent: ComposableAssetNode;
|
|
69
|
+
child: ComposableAssetNode;
|
|
70
|
+
slotKey: string;
|
|
71
|
+
mountMode: SlotMountMode;
|
|
72
|
+
callerDid: string;
|
|
73
|
+
timestamp: number;
|
|
74
|
+
leaseTerms?: LeaseMountTerms;
|
|
75
|
+
assetRegistry?: Record<string, Pick<ComposableAssetNode, 'did' | 'mountedInParentDid'> & Partial<Pick<ComposableAssetNode, 'mountedSlots'>>>;
|
|
76
|
+
maxDepth?: number;
|
|
77
|
+
}): {
|
|
78
|
+
parent: ComposableAssetNode;
|
|
79
|
+
child: ComposableAssetNode;
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Unmounts a child NFT from a parent container slot and restores its independent redemption status.
|
|
83
|
+
*
|
|
84
|
+
* - Enforces top-down unmounting: `parent` itself must be unmounted (`!parent.mountedInParentDid && !parent.independentRedemptionFrozen`).
|
|
85
|
+
* - For `OWNED` mounts: only `parent.ownerDid` can unmount, and `childFloorReserveValue` is subtracted
|
|
86
|
+
* from `parent.aggregatedFloorReserveValue`.
|
|
87
|
+
* - For `LEASED` mounts: `lesseeHostDid` can unmount at any time; `lessorDid` can unmount once
|
|
88
|
+
* `timestamp >= leaseExpiresAt` (or anytime if `allowEarlyLessorRecall` is true).
|
|
89
|
+
*/
|
|
90
|
+
declare function unmountContainerSlotItem(params: {
|
|
91
|
+
parent: ComposableAssetNode;
|
|
92
|
+
child: ComposableAssetNode;
|
|
93
|
+
slotKey: string;
|
|
94
|
+
callerDid: string;
|
|
95
|
+
timestamp: number;
|
|
96
|
+
}): {
|
|
97
|
+
parent: ComposableAssetNode;
|
|
98
|
+
child: ComposableAssetNode;
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* Verifies whether an asset node is eligible for floor-burn redemption or transfer:
|
|
102
|
+
* - Mounted children (`independentRedemptionFrozen === true`) cannot be independently burned or transferred.
|
|
103
|
+
* - A parent container holding a `LEASED` child cannot be burned or transferred until all leased
|
|
104
|
+
* machines are unmounted and returned to their lessors.
|
|
105
|
+
*/
|
|
106
|
+
declare function assertAssetRedeemableOrTransferable(asset: ComposableAssetNode, operation: 'REDEEM_BURN' | 'TRANSFER'): void;
|
|
107
|
+
interface SynthesisRecipeRequirement {
|
|
108
|
+
category: string;
|
|
109
|
+
count: number;
|
|
110
|
+
}
|
|
111
|
+
interface SynthesisRecipe {
|
|
112
|
+
recipeId: string;
|
|
113
|
+
outputCategory: string;
|
|
114
|
+
requirements: readonly SynthesisRecipeRequirement[];
|
|
115
|
+
extraCatalystFeeReserve?: bigint;
|
|
116
|
+
protocolFeeBps?: number;
|
|
117
|
+
}
|
|
118
|
+
interface SynthesisExecutionResult {
|
|
119
|
+
outputAsset: ComposableAssetNode;
|
|
120
|
+
burnedInputDids: string[];
|
|
121
|
+
totalInputReserveConsumed: bigint;
|
|
122
|
+
protocolFeeReserve: bigint;
|
|
123
|
+
netOutputFloorReserve: bigint;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Executes a deterministic N-to-1 consumable alchemy synthesis (`synthesizeContainerRecipe`):
|
|
127
|
+
* - Verifies all ingredient assets belong to `callerDid`, are unmounted, and have no mounted slots.
|
|
128
|
+
* - Matches ingredient categories against `recipe.requirements` (rejecting duplicate category rules).
|
|
129
|
+
* - Conserves underlying floor reserve value:
|
|
130
|
+
* `netOutputFloorReserve + protocolFeeReserve === sum(input.aggregatedFloorReserveValue) + extraCatalystReserveAdded`.
|
|
131
|
+
*/
|
|
132
|
+
declare function synthesizeContainerRecipe(params: {
|
|
133
|
+
recipe: SynthesisRecipe;
|
|
134
|
+
ingredients: readonly ComposableAssetNode[];
|
|
135
|
+
outputDid: string;
|
|
136
|
+
callerDid: string;
|
|
137
|
+
extraCatalystReserveAdded?: bigint | number | string;
|
|
138
|
+
outputSlotDefinitions?: readonly ContainerSlotDefinition[];
|
|
139
|
+
}): SynthesisExecutionResult;
|
|
140
|
+
//#endregion
|
|
141
|
+
export { ComposableAssetNode, ContainerSlotDefinition, LeaseMountTerms, MountedSlotAssignment, SlotMountMode, SynthesisExecutionResult, SynthesisRecipe, SynthesisRecipeRequirement, assertAssetRedeemableOrTransferable, assertNoContainerCycle, createComposableAssetNode, mountContainerSlotItem, synthesizeContainerRecipe, unmountContainerSlotItem };
|