@ocap/tx-protocols 1.30.25 → 1.31.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/execute.d.mts +7 -0
- package/esm/execute.mjs +196 -71
- package/esm/gas-security-guard.d.mts +374 -0
- package/esm/gas-security-guard.mjs +1101 -0
- package/esm/index.d.mts +35 -5
- package/esm/index.mjs +3 -1
- package/esm/pipes/ensure-cost.mjs +314 -58
- package/esm/protocols/account/delegate.mjs +64 -16
- package/esm/protocols/account/migrate.d.mts +3 -0
- package/esm/protocols/account/migrate.mjs +235 -35
- package/esm/protocols/account/revoke-delegate.d.mts +3 -3
- package/esm/protocols/account/revoke-delegate.mjs +70 -38
- package/esm/protocols/asset/acquire-v2.mjs +29 -12
- package/esm/protocols/asset/acquire-v3.mjs +78 -22
- package/esm/protocols/asset/calls/transfer-token.mjs +2 -1
- package/esm/protocols/asset/calls/transfer.mjs +1 -0
- package/esm/protocols/asset/consume.mjs +12 -4
- package/esm/protocols/asset/create.mjs +18 -5
- package/esm/protocols/asset/mint.mjs +23 -1
- package/esm/protocols/asset/pipes/exec-mint-hook.d.mts +21 -1
- package/esm/protocols/asset/pipes/exec-mint-hook.mjs +115 -1
- package/esm/protocols/asset/pipes/extract-factory-tokens.mjs +14 -8
- package/esm/protocols/asset/pipes/verify-itx-address.mjs +9 -0
- package/esm/protocols/asset/pipes/verify-itx-assets.mjs +21 -5
- package/esm/protocols/asset/update.d.mts +13 -2
- package/esm/protocols/asset/update.mjs +133 -14
- package/esm/protocols/factory/create.mjs +14 -6
- package/esm/protocols/governance/claim-stake.mjs +92 -10
- package/esm/protocols/governance/return-stake.mjs +44 -12
- package/esm/protocols/governance/revoke-stake.mjs +17 -5
- package/esm/protocols/governance/slash-stake.mjs +64 -25
- package/esm/protocols/governance/stake.mjs +63 -13
- package/esm/protocols/rollup/claim-reward.mjs +39 -20
- package/esm/protocols/rollup/close.mjs +1 -1
- package/esm/protocols/rollup/create-block.mjs +8 -7
- package/esm/protocols/rollup/create.mjs +17 -6
- package/esm/protocols/rollup/join.mjs +3 -3
- package/esm/protocols/rollup/leave.mjs +1 -1
- package/esm/protocols/rollup/migrate.mjs +7 -4
- package/esm/protocols/rollup/pipes/verify-evidence.mjs +12 -4
- package/esm/protocols/rollup/update.mjs +6 -1
- package/esm/protocols/token/create.mjs +23 -12
- package/esm/protocols/token/deposit-v2.d.mts +5 -1
- package/esm/protocols/token/deposit-v2.mjs +81 -29
- package/esm/protocols/token/withdraw-v2.mjs +33 -16
- package/esm/protocols/token-factory/burn.mjs +43 -10
- package/esm/protocols/token-factory/create.mjs +23 -14
- package/esm/protocols/token-factory/mint.mjs +54 -21
- package/esm/protocols/token-factory/pipes/verify-icon.mjs +1 -1
- package/esm/protocols/token-factory/update.mjs +16 -6
- package/esm/protocols/trade/exchange-v2.d.mts +4 -0
- package/esm/protocols/trade/exchange-v2.mjs +78 -22
- package/esm/protocols/trade/transfer-v2.mjs +44 -13
- package/esm/protocols/trade/transfer-v3.mjs +72 -31
- package/esm/util.d.mts +3 -1
- package/esm/util.mjs +26 -17
- package/lib/execute.cjs +193 -67
- package/lib/execute.d.cts +7 -0
- package/lib/gas-security-guard.cjs +1140 -0
- package/lib/gas-security-guard.d.cts +374 -0
- package/lib/index.cjs +122 -1
- package/lib/index.d.cts +36 -6
- package/lib/pipes/ensure-cost.cjs +313 -57
- package/lib/protocols/account/delegate.cjs +64 -16
- package/lib/protocols/account/migrate.cjs +235 -35
- package/lib/protocols/account/migrate.d.cts +3 -0
- package/lib/protocols/account/revoke-delegate.cjs +71 -39
- package/lib/protocols/account/revoke-delegate.d.cts +3 -3
- package/lib/protocols/asset/acquire-v2.cjs +28 -11
- package/lib/protocols/asset/acquire-v3.cjs +77 -20
- package/lib/protocols/asset/calls/transfer-token.cjs +3 -2
- package/lib/protocols/asset/calls/transfer.cjs +1 -0
- package/lib/protocols/asset/consume.cjs +11 -3
- package/lib/protocols/asset/create.cjs +19 -6
- package/lib/protocols/asset/mint.cjs +23 -1
- package/lib/protocols/asset/pipes/exec-mint-hook.cjs +117 -1
- package/lib/protocols/asset/pipes/exec-mint-hook.d.cts +21 -1
- package/lib/protocols/asset/pipes/extract-factory-tokens.cjs +14 -9
- package/lib/protocols/asset/pipes/verify-itx-address.cjs +9 -0
- package/lib/protocols/asset/pipes/verify-itx-assets.cjs +21 -5
- package/lib/protocols/asset/update.cjs +131 -12
- package/lib/protocols/asset/update.d.cts +13 -2
- package/lib/protocols/factory/create.cjs +14 -6
- package/lib/protocols/governance/claim-stake.cjs +92 -9
- package/lib/protocols/governance/claim-stake.d.cts +1 -1
- package/lib/protocols/governance/return-stake.cjs +44 -11
- package/lib/protocols/governance/return-stake.d.cts +1 -1
- package/lib/protocols/governance/revoke-stake.cjs +17 -5
- package/lib/protocols/governance/revoke-stake.d.cts +1 -1
- package/lib/protocols/governance/slash-stake.cjs +64 -24
- package/lib/protocols/governance/slash-stake.d.cts +1 -1
- package/lib/protocols/governance/stake.cjs +65 -14
- package/lib/protocols/rollup/claim-reward.cjs +39 -20
- package/lib/protocols/rollup/close.cjs +1 -1
- package/lib/protocols/rollup/create-block.cjs +9 -8
- package/lib/protocols/rollup/create-block.d.cts +1 -1
- package/lib/protocols/rollup/create.cjs +18 -7
- package/lib/protocols/rollup/join.cjs +3 -3
- package/lib/protocols/rollup/leave.cjs +1 -1
- package/lib/protocols/rollup/migrate.cjs +7 -4
- package/lib/protocols/rollup/pipes/verify-evidence.cjs +11 -3
- package/lib/protocols/rollup/update.cjs +6 -1
- package/lib/protocols/token/create.cjs +24 -13
- package/lib/protocols/token/deposit-v2.cjs +80 -28
- package/lib/protocols/token/deposit-v2.d.cts +5 -1
- package/lib/protocols/token/withdraw-v2.cjs +32 -15
- package/lib/protocols/token-factory/burn.cjs +42 -9
- package/lib/protocols/token-factory/create.cjs +24 -15
- package/lib/protocols/token-factory/mint.cjs +53 -20
- package/lib/protocols/token-factory/pipes/verify-icon.cjs +1 -1
- package/lib/protocols/token-factory/update.cjs +16 -6
- package/lib/protocols/trade/exchange-v2.cjs +76 -20
- package/lib/protocols/trade/exchange-v2.d.cts +4 -0
- package/lib/protocols/trade/transfer-v2.cjs +45 -14
- package/lib/protocols/trade/transfer-v3.cjs +72 -30
- package/lib/util.cjs +26 -17
- package/lib/util.d.cts +4 -2
- package/package.json +16 -16
|
@@ -13,13 +13,16 @@ let _ocap_state = require("@ocap/state");
|
|
|
13
13
|
let _ocap_tx_pipeline = require("@ocap/tx-pipeline");
|
|
14
14
|
let _ocap_util = require("@ocap/util");
|
|
15
15
|
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
16
|
+
let _ocap_util_lib_get_list_field = require("@ocap/util/lib/get-list-field");
|
|
17
|
+
let _ocap_util_lib_get_related_addr = require("@ocap/util/lib/get-related-addr");
|
|
16
18
|
let debug = require("debug");
|
|
17
19
|
debug = require_rolldown_runtime.__toESM(debug);
|
|
18
|
-
let _ocap_util_lib_get_list_field = require("@ocap/util/lib/get-list-field");
|
|
19
20
|
let _arcblock_validator = require("@arcblock/validator");
|
|
20
21
|
let node_util = require("node:util");
|
|
21
22
|
let lodash_isEqual = require("lodash/isEqual");
|
|
22
23
|
lodash_isEqual = require_rolldown_runtime.__toESM(lodash_isEqual);
|
|
24
|
+
let lodash_uniqBy = require("lodash/uniqBy");
|
|
25
|
+
lodash_uniqBy = require_rolldown_runtime.__toESM(lodash_uniqBy);
|
|
23
26
|
|
|
24
27
|
//#region src/protocols/asset/acquire-v3.ts
|
|
25
28
|
const debug$1 = (0, debug.default)("@ocap/tx-protocols:acquire-asset-v3");
|
|
@@ -167,21 +170,65 @@ runner.use(_ocap_tx_pipeline.pipes.VerifyAccountMigration({
|
|
|
167
170
|
stateKey: "senderState",
|
|
168
171
|
addressKey: "tx.from"
|
|
169
172
|
}));
|
|
170
|
-
runner.use(_ocap_tx_pipeline.pipes.VerifyBlocked({ stateKeys: [
|
|
173
|
+
runner.use(_ocap_tx_pipeline.pipes.VerifyBlocked({ stateKeys: [
|
|
174
|
+
"senderState",
|
|
175
|
+
"signerStates",
|
|
176
|
+
"ownerState",
|
|
177
|
+
"itx.owner",
|
|
178
|
+
"factoryOwnerState",
|
|
179
|
+
"issuerState"
|
|
180
|
+
] }));
|
|
181
|
+
/**
|
|
182
|
+
* Attribute the factory payment legs (factory / mint hook recipients) to the matched inputs.
|
|
183
|
+
* Legs are consumed in input order across ALL inputs so the attribution is deterministic; each matched
|
|
184
|
+
* input only gets the slices it funds, each pinned to a single payee.
|
|
185
|
+
*/
|
|
186
|
+
function allocatePayLegs(ctx, matched) {
|
|
187
|
+
const { inputs = [], factoryState, config } = ctx;
|
|
188
|
+
const legs = require_protocols_asset_pipes_exec_mint_hook.getMintTokenPayLegs(factoryState, ctx.factoryTokens || [], config?.token?.address).map((l) => ({
|
|
189
|
+
...l,
|
|
190
|
+
left: new _ocap_util.BN(l.value)
|
|
191
|
+
}));
|
|
192
|
+
const result = [];
|
|
193
|
+
const matchedSet = new Set(matched);
|
|
194
|
+
const factoryTo = factoryState?.address ? [factoryState.address] : [];
|
|
195
|
+
for (const input of inputs) for (const t of (0, _ocap_util_lib_get_list_field.getListField)(input, "tokens") || []) {
|
|
196
|
+
let need = new _ocap_util.BN(t.value || "0");
|
|
197
|
+
for (const leg of legs) {
|
|
198
|
+
if (need.lte(new _ocap_util.BN(0))) break;
|
|
199
|
+
if (leg.address !== t.address || leg.left.lte(new _ocap_util.BN(0))) continue;
|
|
200
|
+
const take = _ocap_util.BN.min(need, leg.left);
|
|
201
|
+
leg.left = leg.left.sub(take);
|
|
202
|
+
need = need.sub(take);
|
|
203
|
+
if (matchedSet.has(input)) result.push({
|
|
204
|
+
address: t.address,
|
|
205
|
+
value: take.toString(10),
|
|
206
|
+
to: leg.to
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
if (need.gt(new _ocap_util.BN(0)) && matchedSet.has(input)) result.push({
|
|
210
|
+
address: t.address,
|
|
211
|
+
value: need.toString(10),
|
|
212
|
+
to: factoryTo
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
return result;
|
|
216
|
+
}
|
|
171
217
|
const getAcquireAssetV3Requirements = (ctx, index, signature) => {
|
|
172
|
-
const { tx, itx, inputs = [] } = ctx;
|
|
218
|
+
const { tx, itx, inputs = [], senderState, signerStates = [] } = ctx;
|
|
173
219
|
const signatures = (0, _ocap_util_lib_get_list_field.getListField)(tx, "signatures");
|
|
220
|
+
const isMultiSigCall = typeof index === "number" || Boolean(signature);
|
|
174
221
|
const targetOwner = signature?.signer || (typeof index === "number" ? signatures?.[index]?.signer : tx?.from);
|
|
175
222
|
if (!targetOwner) return [];
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
const
|
|
179
|
-
const
|
|
180
|
-
return [
|
|
223
|
+
if (isMultiSigCall && tx?.delegator && signature?.delegator && (0, _ocap_util.isSameDid)(targetOwner, tx.from) && (0, _ocap_util.isSameDid)(signature.delegator, tx.delegator)) return [];
|
|
224
|
+
const targetOwnerState = ((0, _ocap_util.isSameDid)(targetOwner, tx?.from) ? senderState : void 0) || signerStates.find((s) => (0, _ocap_util_lib_get_related_addr.getRelatedAddresses)(s).some((addr) => (0, _ocap_util.isSameDid)(addr, targetOwner)));
|
|
225
|
+
const ownerRelated = targetOwnerState ? (0, _ocap_util_lib_get_related_addr.getRelatedAddresses)(targetOwnerState) : [targetOwner];
|
|
226
|
+
const matchedInputs = inputs.filter((x) => ownerRelated.some((addr) => (0, _ocap_util.isSameDid)(x.owner, addr)));
|
|
227
|
+
if (matchedInputs.length === 0) return isMultiSigCall ? [] : require_util.getDelegationRequirements(ctx);
|
|
228
|
+
const assetsList = matchedInputs.flatMap((inp) => (0, _ocap_util_lib_get_list_field.getListField)(inp, "assets") || []);
|
|
229
|
+
return [...allocatePayLegs(ctx, matchedInputs).map((x) => ({
|
|
181
230
|
type: "token",
|
|
182
|
-
|
|
183
|
-
value: t.value,
|
|
184
|
-
to: itx?.factory ? [itx.factory] : []
|
|
231
|
+
...x
|
|
185
232
|
})), ...assetsList.map((a) => ({
|
|
186
233
|
type: "asset",
|
|
187
234
|
address: a,
|
|
@@ -208,6 +255,11 @@ runner.use(_ocap_tx_pipeline.pipes.ExtractSigner({
|
|
|
208
255
|
signerKey: "msSignerStates",
|
|
209
256
|
delegatorKey: "delegatorStates"
|
|
210
257
|
}));
|
|
258
|
+
runner.use(_ocap_tx_pipeline.pipes.VerifyBlocked({ stateKeys: [
|
|
259
|
+
"delegatorState",
|
|
260
|
+
"msSignerStates",
|
|
261
|
+
"delegatorStates"
|
|
262
|
+
] }));
|
|
211
263
|
runner.use(_ocap_tx_pipeline.pipes.VerifyDelegation({
|
|
212
264
|
type: "multisig",
|
|
213
265
|
signerKey: "msSignerStates",
|
|
@@ -243,7 +295,7 @@ runner.use(async (context, next) => {
|
|
|
243
295
|
const { owner } = input;
|
|
244
296
|
const assetsList = (0, _ocap_util_lib_get_list_field.getListField)(input, "assets");
|
|
245
297
|
const states = assetStates.filter((x) => assetsList.includes(x.address));
|
|
246
|
-
const signer = signerStates.find((x) => x.
|
|
298
|
+
const signer = signerStates.find((x) => (0, _ocap_util_lib_get_related_addr.getRelatedAddresses)(x).includes(owner));
|
|
247
299
|
try {
|
|
248
300
|
await verifyAssetOwner({
|
|
249
301
|
assets: states,
|
|
@@ -268,6 +320,7 @@ runner.use(async (context, next) => {
|
|
|
268
320
|
const owner = ownerState ? ownerState.address : itx.owner;
|
|
269
321
|
return next(await (0, _ocap_tx_pipeline.verifyAssetHierarchy)([itx.address], owner, context, void 0, assets) || void 0);
|
|
270
322
|
});
|
|
323
|
+
runner.use(require_protocols_asset_pipes_exec_mint_hook.verifyMintHook);
|
|
271
324
|
runner.use(require_pipes_ensure_gas.default((context) => {
|
|
272
325
|
const result = {
|
|
273
326
|
create: 1,
|
|
@@ -283,13 +336,17 @@ runner.use(require_pipes_ensure_cost.default({ attachSenderChanges: true }));
|
|
|
283
336
|
runner.use(_ocap_tx_pipeline.pipes.TakeStateSnapshot());
|
|
284
337
|
runner.use(async (context, next) => {
|
|
285
338
|
const { tx, itx, txTime, inputs = [], statedb, senderState, ownerState, factoryState, factoryTokens = [], mintedAsset, mintedAddress, signerStates = [], senderChange, updateVaults, assetStates = [], delegationState, delegationStates = [] } = context;
|
|
339
|
+
const sender = senderState ? senderState.address : tx.from;
|
|
286
340
|
const signerUpdates = {};
|
|
287
341
|
inputs.forEach((x) => {
|
|
288
342
|
const { owner: owner$1 } = x;
|
|
289
|
-
|
|
290
|
-
|
|
343
|
+
const tokensList = (0, _ocap_util_lib_get_list_field.getListField)(x, "tokens");
|
|
344
|
+
const signerState = signerStates.find((s) => (0, _ocap_util_lib_get_related_addr.getRelatedAddresses)(s).includes(owner$1));
|
|
345
|
+
const canonicalOwner = signerState ? signerState.address : owner$1;
|
|
346
|
+
signerUpdates[canonicalOwner] = require_util.applyTokenUpdates(tokensList, signerUpdates[canonicalOwner] || signerState, "sub");
|
|
291
347
|
});
|
|
292
|
-
|
|
348
|
+
if (senderChange && signerUpdates[senderChange.address]) signerUpdates[senderChange.address] = require_util.applyTokenChange(signerUpdates[senderChange.address], senderChange);
|
|
349
|
+
const isAlsoSigner = !!signerUpdates[sender];
|
|
293
350
|
const owner = ownerState ? ownerState.address : itx.owner;
|
|
294
351
|
const factoryUpdates = { numMinted: factoryState.numMinted + 1 };
|
|
295
352
|
if (factoryState.settlement !== "instant") {
|
|
@@ -299,17 +356,17 @@ runner.use(async (context, next) => {
|
|
|
299
356
|
const existingSenderState = senderState ? await statedb.account.get(senderState.address, context) : null;
|
|
300
357
|
const [newSenderState, newReceiverState, assetState, newSignerStates, newFactoryState, newAssetStates, newDelegationState, newDelegationStates] = await Promise.all([
|
|
301
358
|
statedb.account.updateOrCreate(existingSenderState ?? null, _ocap_state.account.updateOrCreate(existingSenderState ?? null, Object.assign({
|
|
302
|
-
address:
|
|
359
|
+
address: sender,
|
|
303
360
|
nonce: tx.nonce,
|
|
304
361
|
pk: tx.delegator ? senderState?.pk : tx.pk
|
|
305
|
-
}, signerUpdates[
|
|
306
|
-
ownerState || owner ===
|
|
362
|
+
}, signerUpdates[sender] || (senderChange ? require_util.applyTokenChange(senderState, senderChange) : {})), context), context),
|
|
363
|
+
ownerState || owner === sender ? Promise.resolve(ownerState) : statedb.account.create(owner, _ocap_state.account.create({ address: owner }, context), context),
|
|
307
364
|
statedb.asset.create(itx.address, _ocap_state.asset.create({
|
|
308
365
|
...mintedAsset,
|
|
309
366
|
owner,
|
|
310
367
|
address: mintedAddress
|
|
311
368
|
}, context), context),
|
|
312
|
-
Promise.all(signerStates.filter((x) => x.address !==
|
|
369
|
+
Promise.all((0, lodash_uniqBy.default)(signerStates, "address").filter((x) => x.address !== sender).map(async (x) => {
|
|
313
370
|
const existing = await statedb.account.get(x.address, context);
|
|
314
371
|
return statedb.account.updateOrCreate(existing ?? null, _ocap_state.account.updateOrCreate(existing ?? null, {
|
|
315
372
|
address: x.address,
|
|
@@ -319,7 +376,7 @@ runner.use(async (context, next) => {
|
|
|
319
376
|
statedb.factory.update(factoryState.address, _ocap_state.factory.update(factoryState, factoryUpdates, context), context),
|
|
320
377
|
Promise.all(assetStates.map((x) => statedb.asset.update(x.address, _ocap_state.asset.update(x, { consumedTime: txTime }, context), context))),
|
|
321
378
|
context.isDelegationChanged && delegationState ? statedb.delegation.update(delegationState.address, _ocap_state.delegation.update(delegationState, {}, context), context) : delegationState,
|
|
322
|
-
context.isDelegationChanged && delegationStates.length ? Promise.all(delegationStates.map((x) => statedb.delegation.update(x.address, _ocap_state.delegation.update(x, {}, context), context))) : delegationStates
|
|
379
|
+
context.isDelegationChanged && delegationStates.length ? Promise.all((0, lodash_uniqBy.default)(delegationStates, "address").filter((x) => !delegationState || !(0, _ocap_util.isSameDid)(x.address, delegationState.address)).map((x) => statedb.delegation.update(x.address, _ocap_state.delegation.update(x, {}, context), context))) : delegationStates
|
|
323
380
|
]);
|
|
324
381
|
if (updateVaults) await updateVaults();
|
|
325
382
|
context.senderState = newSenderState;
|
|
@@ -2,10 +2,10 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
2
2
|
const require_rolldown_runtime = require('../../../_virtual/rolldown_runtime.cjs');
|
|
3
3
|
let _ocap_tx_pipeline = require("@ocap/tx-pipeline");
|
|
4
4
|
let _ocap_util = require("@ocap/util");
|
|
5
|
-
let debug = require("debug");
|
|
6
|
-
debug = require_rolldown_runtime.__toESM(debug);
|
|
7
5
|
let lodash_cloneDeep = require("lodash/cloneDeep");
|
|
8
6
|
lodash_cloneDeep = require_rolldown_runtime.__toESM(lodash_cloneDeep);
|
|
7
|
+
let debug = require("debug");
|
|
8
|
+
debug = require_rolldown_runtime.__toESM(debug);
|
|
9
9
|
|
|
10
10
|
//#region src/protocols/asset/calls/transfer-token.ts
|
|
11
11
|
const debug$1 = (0, debug.default)("@ocap/tx-protocols:acquire-asset:transfer-token");
|
|
@@ -15,6 +15,7 @@ runner.use(_ocap_tx_pipeline.pipes.ExtractState({
|
|
|
15
15
|
to: "receiverState",
|
|
16
16
|
status: "INVALID_RECEIVER_STATE"
|
|
17
17
|
}));
|
|
18
|
+
runner.use(_ocap_tx_pipeline.pipes.VerifyBlocked({ stateKeys: ["receiverState"] }));
|
|
18
19
|
runner.use((context, next) => {
|
|
19
20
|
const { receiverState, args } = context;
|
|
20
21
|
const receiver = receiverState;
|
|
@@ -13,6 +13,7 @@ runner.use(_ocap_tx_pipeline.pipes.ExtractState({
|
|
|
13
13
|
to: "receiverState",
|
|
14
14
|
status: "INVALID_RECEIVER_STATE"
|
|
15
15
|
}));
|
|
16
|
+
runner.use(_ocap_tx_pipeline.pipes.VerifyBlocked({ stateKeys: ["receiverState"] }));
|
|
16
17
|
runner.use((context, next) => {
|
|
17
18
|
const { receiverState, args } = context;
|
|
18
19
|
const receiver = receiverState;
|
|
@@ -34,6 +34,7 @@ runner.use(_ocap_tx_pipeline.pipes.ExtractState({
|
|
|
34
34
|
runner.use((context, next) => {
|
|
35
35
|
const { assetState } = context;
|
|
36
36
|
if (!assetState) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "Asset state not found"));
|
|
37
|
+
if (_ocap_state.token.isTokenCharterAsset(assetState)) return next(new _ocap_util_lib_error.CustomError("FORBIDDEN", "Token charter asset cannot be consumed"));
|
|
37
38
|
const { issuer, consumedTime } = assetState;
|
|
38
39
|
if (!issuer) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "Asset without issuer can not be consumed"));
|
|
39
40
|
if (consumedTime) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", `Asset already consumed on ${consumedTime}`));
|
|
@@ -62,12 +63,19 @@ runner.use(_ocap_tx_pipeline.pipes.ExtractState({
|
|
|
62
63
|
table: "account"
|
|
63
64
|
}));
|
|
64
65
|
runner.use((context, next) => {
|
|
65
|
-
|
|
66
|
-
context.
|
|
66
|
+
const isSameOwnerIssuer = context.ownerState.address === context.issuerState.address;
|
|
67
|
+
context.signerStates = isSameOwnerIssuer ? [context.ownerState] : [context.ownerState, context.issuerState];
|
|
68
|
+
context.signers = isSameOwnerIssuer ? [context.ownerState.address] : [context.ownerState.address, context.issuerState.address];
|
|
67
69
|
return next();
|
|
68
70
|
});
|
|
69
71
|
runner.use(_ocap_tx_pipeline.pipes.VerifyMultiSigV2({ signersKey: "signers" }));
|
|
70
72
|
runner.use(_ocap_tx_pipeline.pipes.VerifyAccountMigration({ signerKey: "signerStates" }));
|
|
73
|
+
runner.use(_ocap_tx_pipeline.pipes.VerifyBlocked({ stateKeys: [
|
|
74
|
+
"senderState",
|
|
75
|
+
"signerStates",
|
|
76
|
+
"ownerState",
|
|
77
|
+
"issuerState"
|
|
78
|
+
] }));
|
|
71
79
|
runner.use(require_pipes_ensure_gas.default(() => ({
|
|
72
80
|
create: 0,
|
|
73
81
|
update: 2,
|
|
@@ -79,7 +87,7 @@ runner.use(async (context, next) => {
|
|
|
79
87
|
const { tx, itx, txTime, statedb, senderState, senderUpdates, assetState, updateVaults } = context;
|
|
80
88
|
const [newSenderState, newAssetState] = await Promise.all([statedb.account.update(senderState.address, _ocap_state.account.update(senderState, {
|
|
81
89
|
nonce: tx.nonce,
|
|
82
|
-
pk: tx.pk,
|
|
90
|
+
pk: tx.delegator ? senderState.pk || tx.pk : tx.pk,
|
|
83
91
|
...senderUpdates
|
|
84
92
|
}, context), context), statedb.asset.update(itx.address, _ocap_state.asset.update(assetState, { consumedTime: txTime }, context), context)]);
|
|
85
93
|
if (updateVaults) await updateVaults();
|
|
@@ -3,16 +3,16 @@ const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
|
|
|
3
3
|
const require_util = require('../../util.cjs');
|
|
4
4
|
const require_pipes_ensure_cost = require('../../pipes/ensure-cost.cjs');
|
|
5
5
|
const require_pipes_ensure_gas = require('../../pipes/ensure-gas.cjs');
|
|
6
|
+
let _arcblock_did_util = require("@arcblock/did-util");
|
|
6
7
|
let _ocap_state = require("@ocap/state");
|
|
7
8
|
let _ocap_tx_pipeline = require("@ocap/tx-pipeline");
|
|
8
9
|
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
9
|
-
let _arcblock_did_util = require("@arcblock/did-util");
|
|
10
|
-
let debug = require("debug");
|
|
11
|
-
debug = require_rolldown_runtime.__toESM(debug);
|
|
12
|
-
let _ocap_message = require("@ocap/message");
|
|
13
10
|
let _ocap_util_lib_get_list_field = require("@ocap/util/lib/get-list-field");
|
|
14
11
|
let lodash_cloneDeep = require("lodash/cloneDeep");
|
|
15
12
|
lodash_cloneDeep = require_rolldown_runtime.__toESM(lodash_cloneDeep);
|
|
13
|
+
let debug = require("debug");
|
|
14
|
+
debug = require_rolldown_runtime.__toESM(debug);
|
|
15
|
+
let _ocap_message = require("@ocap/message");
|
|
16
16
|
let _arcblock_validator = require("@arcblock/validator");
|
|
17
17
|
|
|
18
18
|
//#region src/protocols/asset/create.ts
|
|
@@ -55,7 +55,11 @@ runner.use(_ocap_tx_pipeline.pipes.ExtractState({
|
|
|
55
55
|
status: "INVALID_ISSUER_STATE",
|
|
56
56
|
table: "account"
|
|
57
57
|
}));
|
|
58
|
-
runner.use(_ocap_tx_pipeline.pipes.VerifyAccountMigration({
|
|
58
|
+
runner.use(_ocap_tx_pipeline.pipes.VerifyAccountMigration({
|
|
59
|
+
stateKey: "issuerState",
|
|
60
|
+
addressKey: "itx.issuer",
|
|
61
|
+
signerKey: "issuerState"
|
|
62
|
+
}));
|
|
59
63
|
runner.use(_ocap_tx_pipeline.pipes.ExtractState({
|
|
60
64
|
from: "tx.from",
|
|
61
65
|
to: "senderState",
|
|
@@ -82,6 +86,11 @@ runner.use(_ocap_tx_pipeline.pipes.VerifyDelegation({
|
|
|
82
86
|
delegatorKey: "delegatorState",
|
|
83
87
|
getRequirements: require_util.getDelegationRequirements
|
|
84
88
|
}));
|
|
89
|
+
runner.use(_ocap_tx_pipeline.pipes.VerifyBlocked({ stateKeys: [
|
|
90
|
+
"senderState",
|
|
91
|
+
"delegatorState",
|
|
92
|
+
"issuerState"
|
|
93
|
+
] }));
|
|
85
94
|
runner.use(_ocap_tx_pipeline.pipes.ExtractState({
|
|
86
95
|
from: "itx.parent",
|
|
87
96
|
to: "factoryState",
|
|
@@ -103,6 +112,10 @@ runner.use(_ocap_tx_pipeline.pipes.ExtractState({
|
|
|
103
112
|
status: "INVALID_ASSET",
|
|
104
113
|
table: "asset"
|
|
105
114
|
}));
|
|
115
|
+
runner.use((context, next) => {
|
|
116
|
+
if (context.parentAsset?.consumedTime) return next(new _ocap_util_lib_error.CustomError("INVALID_ASSET", "Parent asset is already consumed"));
|
|
117
|
+
return next();
|
|
118
|
+
});
|
|
106
119
|
runner.use(require_pipes_ensure_gas.default(() => ({
|
|
107
120
|
create: 1,
|
|
108
121
|
update: 2,
|
|
@@ -116,7 +129,7 @@ runner.use(async (context, next) => {
|
|
|
116
129
|
const [newSenderState, assetState, newDelegationState] = await Promise.all([
|
|
117
130
|
statedb.account.update(senderState.address, _ocap_state.account.update(senderState, {
|
|
118
131
|
nonce: tx.nonce,
|
|
119
|
-
pk: tx.pk,
|
|
132
|
+
pk: tx.delegator ? senderState.pk : tx.pk,
|
|
120
133
|
...senderUpdates
|
|
121
134
|
}, context), context),
|
|
122
135
|
statedb.asset.create(itx.address, _ocap_state.asset.create({
|
|
@@ -8,7 +8,9 @@ const require_protocols_asset_pipes_verify_itx_variables = require('./pipes/veri
|
|
|
8
8
|
const require_protocols_asset_pipes_verify_mint_limit = require('./pipes/verify-mint-limit.cjs');
|
|
9
9
|
let _ocap_state = require("@ocap/state");
|
|
10
10
|
let _ocap_tx_pipeline = require("@ocap/tx-pipeline");
|
|
11
|
+
let _ocap_util = require("@ocap/util");
|
|
11
12
|
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
13
|
+
let _ocap_util_lib_get_related_addr = require("@ocap/util/lib/get-related-addr");
|
|
12
14
|
let debug = require("debug");
|
|
13
15
|
debug = require_rolldown_runtime.__toESM(debug);
|
|
14
16
|
let _arcblock_validator = require("@arcblock/validator");
|
|
@@ -87,7 +89,13 @@ runner.use(_ocap_tx_pipeline.pipes.ExtractState({
|
|
|
87
89
|
status: "OK",
|
|
88
90
|
table: "account"
|
|
89
91
|
}));
|
|
90
|
-
runner.use(_ocap_tx_pipeline.pipes.VerifyBlocked({ stateKeys: [
|
|
92
|
+
runner.use(_ocap_tx_pipeline.pipes.VerifyBlocked({ stateKeys: [
|
|
93
|
+
"senderState",
|
|
94
|
+
"factoryOwnerState",
|
|
95
|
+
"issuerStates",
|
|
96
|
+
"ownerState",
|
|
97
|
+
"itx.owner"
|
|
98
|
+
] }));
|
|
91
99
|
runner.use(_ocap_tx_pipeline.pipes.VerifyInfo([{
|
|
92
100
|
error: "FORBIDDEN",
|
|
93
101
|
message: "Only factory owner and trusted issuers are allowed to mint",
|
|
@@ -119,6 +127,20 @@ runner.use(_ocap_tx_pipeline.pipes.VerifyUpdater({
|
|
|
119
127
|
ownerKey: "ownerState"
|
|
120
128
|
}));
|
|
121
129
|
runner.use(require_protocols_asset_pipes_verify_itx_assets.default);
|
|
130
|
+
runner.use((context, next) => {
|
|
131
|
+
const { itx, senderState, factoryOwnerState, assetStates = [], expectedFactoryStates = [], ownerAddress } = context;
|
|
132
|
+
const targetOwner = ownerAddress || itx.owner;
|
|
133
|
+
if (assetStates.length > 0 && senderState && !(0, _ocap_util.isSameDid)(targetOwner, senderState.address)) {
|
|
134
|
+
const allowedIssuers = new Set([...(0, _ocap_util_lib_get_related_addr.getRelatedAddresses)(senderState), ...factoryOwnerState ? (0, _ocap_util_lib_get_related_addr.getRelatedAddresses)(factoryOwnerState) : []]);
|
|
135
|
+
for (const st of assetStates) {
|
|
136
|
+
if (allowedIssuers.has(st.owner)) continue;
|
|
137
|
+
const isIssuedByFactoryEcosystem = Boolean(st.issuer && allowedIssuers.has(st.issuer));
|
|
138
|
+
const isParentedByFactoryEcosystem = Boolean(st.parent && expectedFactoryStates.some((f) => f.address === st.parent && allowedIssuers.has(f.owner)));
|
|
139
|
+
if (!isIssuedByFactoryEcosystem && !isParentedByFactoryEcosystem) return next(new _ocap_util_lib_error.CustomError("FORBIDDEN", "Cannot consume third-party input asset not issued by this factory or its trusted issuers without owner signature", { persist: true }));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return next();
|
|
143
|
+
});
|
|
122
144
|
runner.use(async (context, next) => {
|
|
123
145
|
const { itx, ownerAddress, assetStates = [] } = context;
|
|
124
146
|
const owner = ownerAddress || itx.owner;
|
|
@@ -2,6 +2,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
2
2
|
const require_rolldown_runtime = require('../../../_virtual/rolldown_runtime.cjs');
|
|
3
3
|
const require_protocols_asset_calls_transfer = require('../calls/transfer.cjs');
|
|
4
4
|
const require_protocols_asset_calls_transfer_token = require('../calls/transfer-token.cjs');
|
|
5
|
+
let _ocap_util = require("@ocap/util");
|
|
6
|
+
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
5
7
|
let lodash_cloneDeep = require("lodash/cloneDeep");
|
|
6
8
|
lodash_cloneDeep = require_rolldown_runtime.__toESM(lodash_cloneDeep);
|
|
7
9
|
|
|
@@ -10,6 +12,97 @@ const contractCalls = {
|
|
|
10
12
|
transfer: require_protocols_asset_calls_transfer.default,
|
|
11
13
|
transferToken: require_protocols_asset_calls_transfer_token.default
|
|
12
14
|
};
|
|
15
|
+
/**
|
|
16
|
+
* Break the tokens paid into a factory into payee legs, for delegation requirements.
|
|
17
|
+
*
|
|
18
|
+
* - non-instant settlement: all tokens are credited to the factory itself
|
|
19
|
+
* - instant settlement with a contract mint hook: each hook call credits its own `to` account
|
|
20
|
+
* (one leg per call, exact amount); whatever is not forwarded by the hook is attributed to the factory
|
|
21
|
+
*
|
|
22
|
+
* One leg per payee (instead of one requirement with a union payee set) keeps delegation `limit.to`
|
|
23
|
+
* pinning strict: a delegation pinned to payee X can not be used to also pay Y.
|
|
24
|
+
*/
|
|
25
|
+
function getMintTokenPayLegs(factory, factoryTokens, officialTokenAddress) {
|
|
26
|
+
const factoryTo = factory?.address ? [factory.address] : [];
|
|
27
|
+
const hook = factory?.settlement === "instant" ? (factory.hooks || []).find((x) => x.name === "mint" && x.type === "contract") : void 0;
|
|
28
|
+
const legs = [];
|
|
29
|
+
const forwarded = /* @__PURE__ */ new Map();
|
|
30
|
+
for (const x of hook?.compiled || []) {
|
|
31
|
+
const tokenAddr = x.call === "transfer" ? officialTokenAddress : x.args?.tokenAddress;
|
|
32
|
+
const to = String(x.args?.to || "");
|
|
33
|
+
let amount = new _ocap_util.BN(0);
|
|
34
|
+
try {
|
|
35
|
+
amount = new _ocap_util.BN(String(x.args?.amount || "0"));
|
|
36
|
+
} catch {}
|
|
37
|
+
if (!tokenAddr || !to || amount.lte(new _ocap_util.BN(0))) continue;
|
|
38
|
+
legs.push({
|
|
39
|
+
address: tokenAddr,
|
|
40
|
+
value: amount.toString(10),
|
|
41
|
+
to: [to]
|
|
42
|
+
});
|
|
43
|
+
forwarded.set(tokenAddr, (forwarded.get(tokenAddr) || new _ocap_util.BN(0)).add(amount));
|
|
44
|
+
}
|
|
45
|
+
for (const ft of factoryTokens) {
|
|
46
|
+
const rest = new _ocap_util.BN(String(ft.value || "0")).sub(forwarded.get(ft.address) || new _ocap_util.BN(0));
|
|
47
|
+
if (rest.gt(new _ocap_util.BN(0))) legs.push({
|
|
48
|
+
address: ft.address,
|
|
49
|
+
value: rest.toString(10),
|
|
50
|
+
to: factoryTo
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
return legs;
|
|
54
|
+
}
|
|
55
|
+
const verifyMintHook = async (context, next) => {
|
|
56
|
+
const { statedb, factoryState, config } = context;
|
|
57
|
+
const factory = factoryState;
|
|
58
|
+
if (!factory || factory.settlement !== "instant") return next();
|
|
59
|
+
const mintHook = (factory.hooks || []).find((x) => x.name === "mint");
|
|
60
|
+
if (!mintHook || mintHook.type !== "contract") return next();
|
|
61
|
+
const clone = (0, lodash_cloneDeep.default)(mintHook.compiled || []);
|
|
62
|
+
const factoryTokens = context.factoryTokens;
|
|
63
|
+
const hookTotals = /* @__PURE__ */ new Map();
|
|
64
|
+
for (const x of clone) {
|
|
65
|
+
const tokenAddr = x.call === "transfer" ? config.token.address : x.args.tokenAddress;
|
|
66
|
+
if (tokenAddr) {
|
|
67
|
+
const prev = hookTotals.get(tokenAddr) || new _ocap_util.BN(0);
|
|
68
|
+
hookTotals.set(tokenAddr, prev.add(new _ocap_util.BN(String(x.args.amount || "0"))));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (Array.isArray(factoryTokens)) {
|
|
72
|
+
const paidTotals = /* @__PURE__ */ new Map();
|
|
73
|
+
for (const ft of factoryTokens) {
|
|
74
|
+
const prev = paidTotals.get(ft.address) || new _ocap_util.BN(0);
|
|
75
|
+
paidTotals.set(ft.address, prev.add(new _ocap_util.BN(String(ft.value || "0"))));
|
|
76
|
+
}
|
|
77
|
+
for (const [tokenAddr, totalOut] of hookTotals.entries()) {
|
|
78
|
+
const paid = paidTotals.get(tokenAddr) || new _ocap_util.BN(0);
|
|
79
|
+
if (totalOut.gt(paid)) return next(new _ocap_util_lib_error.CustomError("INVALID_FACTORY_HOOK", `Mint hook outflow for ${tokenAddr} exceeds paid factory token input`, { persist: true }));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
const filter = context.filter;
|
|
83
|
+
const ownerAddress = context.ownerAddress;
|
|
84
|
+
for (const x of clone) {
|
|
85
|
+
const toAddr = String(x.args.to || "");
|
|
86
|
+
if (filter && typeof filter.isBlocked === "function" && toAddr && await filter.isBlocked(toAddr)) return next(new _ocap_util_lib_error.CustomError("FORBIDDEN", `Account blocked: ${toAddr}`, { persist: true }));
|
|
87
|
+
if (toAddr && toAddr !== ownerAddress) {
|
|
88
|
+
if (x.call === "transfer") {
|
|
89
|
+
x.call = "transferToken";
|
|
90
|
+
x.args.tokenAddress = config.token.address;
|
|
91
|
+
}
|
|
92
|
+
await contractCalls[x.call].runAsync({
|
|
93
|
+
states: context.states,
|
|
94
|
+
statedb,
|
|
95
|
+
txn: context.txn,
|
|
96
|
+
txTime: context.txTime,
|
|
97
|
+
txHash: context.txHash,
|
|
98
|
+
logger: context.logger,
|
|
99
|
+
filter,
|
|
100
|
+
args: x.args
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return next();
|
|
105
|
+
};
|
|
13
106
|
const execMintHook = async (context, next) => {
|
|
14
107
|
const { statedb, factoryState, tokenUpdates, config } = context;
|
|
15
108
|
const factory = factoryState;
|
|
@@ -18,6 +111,26 @@ const execMintHook = async (context, next) => {
|
|
|
18
111
|
if (mintHook && mintHook.type === "contract") {
|
|
19
112
|
context.updatedAccounts = context.updatedAccounts || [];
|
|
20
113
|
const clone = (0, lodash_cloneDeep.default)(mintHook.compiled);
|
|
114
|
+
const factoryTokens = context.factoryTokens;
|
|
115
|
+
const hookTotals = /* @__PURE__ */ new Map();
|
|
116
|
+
for (const x of clone) {
|
|
117
|
+
const tokenAddr = x.call === "transfer" ? config.token.address : x.args.tokenAddress;
|
|
118
|
+
if (tokenAddr) {
|
|
119
|
+
const prev = hookTotals.get(tokenAddr) || new _ocap_util.BN(0);
|
|
120
|
+
hookTotals.set(tokenAddr, prev.add(new _ocap_util.BN(String(x.args.amount || "0"))));
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (Array.isArray(factoryTokens)) {
|
|
124
|
+
const paidTotals = /* @__PURE__ */ new Map();
|
|
125
|
+
for (const ft of factoryTokens) {
|
|
126
|
+
const prev = paidTotals.get(ft.address) || new _ocap_util.BN(0);
|
|
127
|
+
paidTotals.set(ft.address, prev.add(new _ocap_util.BN(String(ft.value || "0"))));
|
|
128
|
+
}
|
|
129
|
+
for (const [tokenAddr, totalOut] of hookTotals.entries()) {
|
|
130
|
+
const paid = paidTotals.get(tokenAddr) || new _ocap_util.BN(0);
|
|
131
|
+
if (totalOut.gt(paid)) return next(new _ocap_util_lib_error.CustomError("INVALID_FACTORY_HOOK", `Mint hook outflow for ${tokenAddr} exceeds paid factory token input`, { persist: true }));
|
|
132
|
+
}
|
|
133
|
+
}
|
|
21
134
|
const mintHookStates = {};
|
|
22
135
|
for (const x of clone) {
|
|
23
136
|
if (x.call === "transfer") {
|
|
@@ -31,6 +144,7 @@ const execMintHook = async (context, next) => {
|
|
|
31
144
|
txTime: context.txTime,
|
|
32
145
|
txHash: context.txHash,
|
|
33
146
|
logger: context.logger,
|
|
147
|
+
filter: context.filter,
|
|
34
148
|
args: x.args
|
|
35
149
|
});
|
|
36
150
|
mintHookStates[updateSet.address] = await statedb.account.update(updateSet.address, updateSet.updates, context);
|
|
@@ -54,4 +168,6 @@ const execMintHook = async (context, next) => {
|
|
|
54
168
|
var exec_mint_hook_default = execMintHook;
|
|
55
169
|
|
|
56
170
|
//#endregion
|
|
57
|
-
exports.default = exec_mint_hook_default;
|
|
171
|
+
exports.default = exec_mint_hook_default;
|
|
172
|
+
exports.getMintTokenPayLegs = getMintTokenPayLegs;
|
|
173
|
+
exports.verifyMintHook = verifyMintHook;
|
|
@@ -19,6 +19,26 @@ type ExecMintHookContext = IDecodedContext & {
|
|
|
19
19
|
states?: unknown;
|
|
20
20
|
txn?: unknown;
|
|
21
21
|
};
|
|
22
|
+
interface TokenPayLeg {
|
|
23
|
+
address: string;
|
|
24
|
+
value: string;
|
|
25
|
+
to: string[];
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Break the tokens paid into a factory into payee legs, for delegation requirements.
|
|
29
|
+
*
|
|
30
|
+
* - non-instant settlement: all tokens are credited to the factory itself
|
|
31
|
+
* - instant settlement with a contract mint hook: each hook call credits its own `to` account
|
|
32
|
+
* (one leg per call, exact amount); whatever is not forwarded by the hook is attributed to the factory
|
|
33
|
+
*
|
|
34
|
+
* One leg per payee (instead of one requirement with a union payee set) keeps delegation `limit.to`
|
|
35
|
+
* pinning strict: a delegation pinned to payee X can not be used to also pay Y.
|
|
36
|
+
*/
|
|
37
|
+
declare function getMintTokenPayLegs(factory: IAssetFactoryState | undefined, factoryTokens: Array<{
|
|
38
|
+
address: string;
|
|
39
|
+
value: string;
|
|
40
|
+
}>, officialTokenAddress?: string): TokenPayLeg[];
|
|
41
|
+
declare const verifyMintHook: PipeFunction<ExecMintHookContext>;
|
|
22
42
|
declare const execMintHook: PipeFunction<ExecMintHookContext>;
|
|
23
43
|
//#endregion
|
|
24
|
-
export { execMintHook as default };
|
|
44
|
+
export { TokenPayLeg, execMintHook as default, getMintTokenPayLegs, verifyMintHook };
|
|
@@ -2,20 +2,25 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
2
2
|
const require_rolldown_runtime = require('../../../_virtual/rolldown_runtime.cjs');
|
|
3
3
|
let _ocap_util = require("@ocap/util");
|
|
4
4
|
let _ocap_util_lib_create_sorted_list = require("@ocap/util/lib/create-sorted-list");
|
|
5
|
-
let lodash_uniqBy = require("lodash/uniqBy");
|
|
6
|
-
lodash_uniqBy = require_rolldown_runtime.__toESM(lodash_uniqBy);
|
|
7
5
|
|
|
8
6
|
//#region src/protocols/asset/pipes/extract-factory-tokens.ts
|
|
9
7
|
const extract_factory_tokens = (context, next) => {
|
|
10
8
|
const { factoryState, config } = context;
|
|
11
9
|
const input = factoryState.input;
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
address
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
10
|
+
const merged = /* @__PURE__ */ new Map();
|
|
11
|
+
for (const item of input.tokens || []) {
|
|
12
|
+
const prev = merged.get(item.address) || new _ocap_util.BN(0);
|
|
13
|
+
merged.set(item.address, prev.add(new _ocap_util.BN(item.value || "0")));
|
|
14
|
+
}
|
|
15
|
+
if (new _ocap_util.BN(input.value || "0").gt(new _ocap_util.BN(0))) {
|
|
16
|
+
const primaryAddr = config.token.address;
|
|
17
|
+
const prev = merged.get(primaryAddr) || new _ocap_util.BN(0);
|
|
18
|
+
merged.set(primaryAddr, prev.add(new _ocap_util.BN(input.value)));
|
|
19
|
+
}
|
|
20
|
+
context.factoryTokens = Array.from(merged.entries()).map(([address, val]) => ({
|
|
21
|
+
address,
|
|
22
|
+
value: val.toString()
|
|
23
|
+
}));
|
|
19
24
|
context.tokenAddress = (0, _ocap_util_lib_create_sorted_list.createSortedList)((context.factoryTokens || []).map((x) => x.address));
|
|
20
25
|
next();
|
|
21
26
|
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
2
|
const require_rolldown_runtime = require('../../../_virtual/rolldown_runtime.cjs');
|
|
3
|
+
let _arcblock_did = require("@arcblock/did");
|
|
3
4
|
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
4
5
|
let debug = require("debug");
|
|
5
6
|
debug = require_rolldown_runtime.__toESM(debug);
|
|
@@ -29,6 +30,14 @@ const verify_itx_address = (mode) => (context, next) => {
|
|
|
29
30
|
name: sender.moniker
|
|
30
31
|
};
|
|
31
32
|
}
|
|
33
|
+
if (mode === "acquire-v2" || mode === "acquire-v3") {
|
|
34
|
+
const issuerState = context.issuerState;
|
|
35
|
+
const rawIssuer = itx.issuer;
|
|
36
|
+
const rawPk = typeof rawIssuer?.pk === "string" ? rawIssuer.pk.trim() : rawIssuer?.pk;
|
|
37
|
+
if (issuerState && !(rawPk === void 0 || rawPk === null || rawPk === "" || rawPk === "z" || rawPk === "0x")) {
|
|
38
|
+
if (!(issuerState.pk ? (0, _arcblock_did.normalizePublicKeyHex)(rawIssuer.pk) === (0, _arcblock_did.normalizePublicKeyHex)(issuerState.pk) : (0, _arcblock_did.isFromPublicKey)(issuerState.address, rawIssuer.pk))) return next(new _ocap_util_lib_error.CustomError("INVALID_ASSET", "Invalid itx.address / itx.issuer.pk: does not match with minted asset address (check that itx.issuer.id/itx.issuer.pk match the trusted issuer's actual on-chain public key — a stale or wrong issuer.pk is the most common cause)"));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
32
41
|
const minted = (0, _ocap_asset.mintFromFactory)({
|
|
33
42
|
factory,
|
|
34
43
|
inputs,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
2
|
const require_rolldown_runtime = require('../../../_virtual/rolldown_runtime.cjs');
|
|
3
|
+
let _ocap_state = require("@ocap/state");
|
|
3
4
|
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
4
5
|
let _ocap_util_lib_get_list_field = require("@ocap/util/lib/get-list-field");
|
|
5
6
|
|
|
@@ -8,19 +9,34 @@ const verify_itx_assets = (context, next) => {
|
|
|
8
9
|
const { factoryState, expectedFactoryStates = [], assetStates: actualAssetStates = [], itx } = context;
|
|
9
10
|
const expectedAssets = factoryState.input.assets;
|
|
10
11
|
const assetStates = actualAssetStates;
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
const rawInputAssets = (0, _ocap_util_lib_get_list_field.getListField)(itx, "assets") || [];
|
|
13
|
+
const inputAssets = rawInputAssets.length > 0 ? rawInputAssets : context.assets || [];
|
|
14
|
+
if (new Set(inputAssets).size !== inputAssets.length) return next(new _ocap_util_lib_error.CustomError("INVALID_ASSET", "Duplicate input assets are not allowed"));
|
|
15
|
+
if (inputAssets.length !== assetStates.length) return next(new _ocap_util_lib_error.CustomError("INVALID_ASSET", "Input asset does not exist on chain (asset not found)"));
|
|
16
|
+
if (assetStates.some((x) => _ocap_state.token.isTokenCharterAsset(x))) return next(new _ocap_util_lib_error.CustomError("FORBIDDEN", "Token charter asset cannot be consumed as factory input"));
|
|
17
|
+
if (assetStates.some((x) => Boolean(x.consumedTime))) return next(new _ocap_util_lib_error.CustomError("INVALID_ASSET", "Some input asset already consumed"));
|
|
18
|
+
if (expectedAssets.length === 0) {
|
|
19
|
+
if (inputAssets.length > 0 || assetStates.length > 0) return next(new _ocap_util_lib_error.CustomError("INVALID_ASSET", "Factory does not accept input assets"));
|
|
20
|
+
return next();
|
|
21
|
+
}
|
|
13
22
|
if (expectedFactoryStates.length === expectedAssets.length) {
|
|
23
|
+
if (assetStates.length === 0) return next(new _ocap_util_lib_error.CustomError("INVALID_ASSET", "Input asset is required by factory.input.assets"));
|
|
14
24
|
if (assetStates.every((x) => x.parent && expectedAssets.includes(x.parent)) === false) return next(new _ocap_util_lib_error.CustomError("INVALID_ASSET", "Input asset not minted from any of factory.input.assets"));
|
|
15
|
-
if (assetStates.every((x) => !x.consumedTime) === false) return next(new _ocap_util_lib_error.CustomError("INVALID_ASSET", "Some input asset already consumed"));
|
|
16
25
|
return next();
|
|
17
26
|
}
|
|
27
|
+
const usedAssetAddresses = /* @__PURE__ */ new Set();
|
|
18
28
|
for (const address of expectedAssets) {
|
|
19
|
-
const plainAsset = assetStates.find((x) => x.address === address);
|
|
29
|
+
const plainAsset = assetStates.find((x) => x.address === address && !usedAssetAddresses.has(x.address));
|
|
20
30
|
if (plainAsset) {
|
|
21
31
|
if (plainAsset.consumedTime) return next(new _ocap_util_lib_error.CustomError("INVALID_ASSET", `Input asset ${address} already consumed`));
|
|
22
|
-
|
|
32
|
+
usedAssetAddresses.add(plainAsset.address);
|
|
33
|
+
} else {
|
|
34
|
+
const childAsset = assetStates.find((x) => x.parent === address && !x.consumedTime && !usedAssetAddresses.has(x.address));
|
|
35
|
+
if (!childAsset) return next(new _ocap_util_lib_error.CustomError("INVALID_ASSET", `Input asset not found ${address}`));
|
|
36
|
+
usedAssetAddresses.add(childAsset.address);
|
|
37
|
+
}
|
|
23
38
|
}
|
|
39
|
+
if (assetStates.length !== expectedAssets.length) return next(new _ocap_util_lib_error.CustomError("INVALID_ASSET", "Input assets count does not match factory.input.assets"));
|
|
24
40
|
return next();
|
|
25
41
|
};
|
|
26
42
|
var verify_itx_assets_default = verify_itx_assets;
|