@ocap/tx-protocols 1.18.42 → 1.18.44

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/lib/index.js CHANGED
@@ -9,6 +9,7 @@ const delegate = require('./protocols/account/delegate');
9
9
  const revokeDelegate = require('./protocols/account/revoke-delegate');
10
10
  const createAsset = require('./protocols/asset/create');
11
11
  const updateAsset = require('./protocols/asset/update');
12
+ const consumeAsset = require('./protocols/asset/consume');
12
13
  const createFactory = require('./protocols/factory/create');
13
14
  const acquireAssetV2 = require('./protocols/asset/acquire-v2');
14
15
  const acquireAssetV3 = require('./protocols/asset/acquire-v3');
@@ -50,6 +51,7 @@ const createExecutor = ({ filter, runAsLambda }) => {
50
51
  // asset
51
52
  createAsset,
52
53
  updateAsset,
54
+ consumeAsset,
53
55
  acquireAssetV2,
54
56
  acquireAssetV3,
55
57
  mintAsset,
@@ -0,0 +1,86 @@
1
+ const { CustomError: Error } = require('@ocap/util/lib/error');
2
+ const { Joi } = require('@arcblock/validator');
3
+ const { Runner, pipes } = require('@ocap/tx-pipeline');
4
+ const { account, asset } = require('@ocap/state');
5
+
6
+ // eslint-disable-next-line global-require
7
+ const debug = require('debug')(`${require('../../../package.json').name}:consume-asset`);
8
+
9
+ const EnsureTxGas = require('../../pipes/ensure-gas');
10
+ const EnsureTxCost = require('../../pipes/ensure-cost');
11
+
12
+ const runner = new Runner();
13
+
14
+ // Verify itx
15
+ const schema = Joi.object({
16
+ address: Joi.DID().prefix().role('ROLE_ASSET').required(),
17
+ data: Joi.any().optional(),
18
+ }).options({ stripUnknown: true, noDefaults: false });
19
+ runner.use((context, next) => {
20
+ const { itx } = context;
21
+ const { error } = schema.validate(itx);
22
+ if (error) {
23
+ return next(new Error('INVALID_TX', `Invalid itx: ${error.message}`));
24
+ }
25
+
26
+ return next();
27
+ });
28
+
29
+ // Ensure asset exist
30
+ runner.use(pipes.ExtractState({ from: 'itx.address', to: 'assetState', status: 'INVALID_ASSET', table: 'asset' }));
31
+
32
+ // Ensure asset can be consumed
33
+ runner.use((context, next) => {
34
+ const { issuer, owner, consumedTime } = context.assetState;
35
+ if (!issuer) {
36
+ return next(new Error('INVALID_TX', 'Asset without issuer can not be consumed'));
37
+ }
38
+ if (consumedTime) {
39
+ return next(new Error('INVALID_TX', `Asset already consumed on ${consumedTime}`));
40
+ }
41
+
42
+ context.signers = [owner, issuer];
43
+ return next();
44
+ });
45
+
46
+ // Ensure owner and issuer signed
47
+ runner.use(pipes.VerifyMultiSigV2({ signersKey: 'signers' }));
48
+
49
+ // Ensure sender exist
50
+ runner.use(pipes.ExtractState({ from: 'tx.from', to: 'senderState', status: 'INVALID_SENDER_STATE', table: 'account' })); // prettier-ignore
51
+ runner.use(pipes.VerifyAccountMigration({ senderKey: 'senderState' }));
52
+
53
+ // Ensure tx fee and gas
54
+ runner.use(EnsureTxGas(() => ({ create: 0, update: 2, payment: 0 })));
55
+ runner.use(EnsureTxCost({ attachSenderChanges: true }));
56
+
57
+ // Update asset state
58
+ runner.use(
59
+ async (context, next) => {
60
+ const { tx, itx, txTime, statedb, senderState, senderUpdates, assetState, updateVaults } = context;
61
+
62
+ const [newSenderState, newAssetState] = await Promise.all([
63
+ // update sender state
64
+ statedb.account.update(
65
+ senderState.address,
66
+ account.update(senderState, { nonce: tx.nonce, pk: tx.pk, ...senderUpdates }, context),
67
+ context
68
+ ),
69
+
70
+ // update asset state
71
+ statedb.asset.update(itx.address, asset.update(assetState, { consumedTime: txTime }, context), context),
72
+
73
+ updateVaults(),
74
+ ]);
75
+
76
+ context.senderState = newSenderState;
77
+ context.assetState = newAssetState;
78
+
79
+ debug('consume', newAssetState);
80
+
81
+ next();
82
+ },
83
+ { persistError: true }
84
+ );
85
+
86
+ module.exports = runner;
@@ -21,11 +21,14 @@ module.exports = (context, next) => {
21
21
  if (expectedFactoryStates.length === expectedAssets.length) {
22
22
  // Just ensure that input assets are minted from any of the factory
23
23
  // They do not need to match on number
24
- if (actualAssetStates.every((x) => x.parent && expectedAssets.includes(x.parent) && !x.consumedTime)) {
25
- return next();
24
+ if (actualAssetStates.every((x) => x.parent && expectedAssets.includes(x.parent)) === false) {
25
+ return next(new Error('INVALID_ASSET', 'Input asset not minted from any of factory.input.assets'));
26
+ }
27
+ if (actualAssetStates.every((x) => !x.consumedTime) === false) {
28
+ return next(new Error('INVALID_ASSET', 'Some input asset already consumed'));
26
29
  }
27
30
 
28
- return next(new Error('INVALID_ASSET', 'Input asset not minted from any of factory.input.assets'));
31
+ return next();
29
32
  }
30
33
 
31
34
  // For specific assets, they must not be consumed
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.18.42",
6
+ "version": "1.18.44",
7
7
  "description": "Predefined tx pipeline sets to execute certain type of transactions",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -21,18 +21,18 @@
21
21
  "author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
22
22
  "license": "MIT",
23
23
  "dependencies": {
24
- "@arcblock/did": "1.18.42",
25
- "@arcblock/did-util": "1.18.42",
26
- "@arcblock/jwt": "1.18.42",
27
- "@arcblock/validator": "1.18.42",
28
- "@ocap/asset": "1.18.42",
29
- "@ocap/mcrypto": "1.18.42",
30
- "@ocap/merkle-tree": "1.18.42",
31
- "@ocap/message": "1.18.42",
32
- "@ocap/state": "1.18.42",
33
- "@ocap/tx-pipeline": "1.18.42",
34
- "@ocap/util": "1.18.42",
35
- "@ocap/wallet": "1.18.42",
24
+ "@arcblock/did": "1.18.44",
25
+ "@arcblock/did-util": "1.18.44",
26
+ "@arcblock/jwt": "1.18.44",
27
+ "@arcblock/validator": "1.18.44",
28
+ "@ocap/asset": "1.18.44",
29
+ "@ocap/mcrypto": "1.18.44",
30
+ "@ocap/merkle-tree": "1.18.44",
31
+ "@ocap/message": "1.18.44",
32
+ "@ocap/state": "1.18.44",
33
+ "@ocap/tx-pipeline": "1.18.44",
34
+ "@ocap/util": "1.18.44",
35
+ "@ocap/wallet": "1.18.44",
36
36
  "debug": "^4.3.4",
37
37
  "deep-diff": "^1.0.2",
38
38
  "empty-value": "^1.0.1",
@@ -47,5 +47,5 @@
47
47
  "jest": "^27.5.1",
48
48
  "start-server-and-test": "^1.14.0"
49
49
  },
50
- "gitHead": "e4c81bf4891fd7b1b1ce3b8e1ccf7c4b7c004593"
50
+ "gitHead": "563803a77461e59d630e283f0c00e975c47f6ebf"
51
51
  }