@ocap/tx-protocols 1.17.23 → 1.18.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.
Files changed (34) hide show
  1. package/lib/pipes/ensure-cost.js +140 -0
  2. package/lib/pipes/ensure-gas.js +53 -0
  3. package/lib/protocols/account/delegate.js +30 -3
  4. package/lib/protocols/account/migrate.js +15 -1
  5. package/lib/protocols/account/revoke-delegate.js +14 -2
  6. package/lib/protocols/asset/acquire-v2.js +29 -2
  7. package/lib/protocols/asset/acquire-v3.js +34 -2
  8. package/lib/protocols/asset/create.js +9 -10
  9. package/lib/protocols/asset/mint.js +28 -2
  10. package/lib/protocols/asset/pipes/exec-mint-hook.js +1 -1
  11. package/lib/protocols/asset/update.js +11 -2
  12. package/lib/protocols/factory/create.js +8 -9
  13. package/lib/protocols/governance/claim-stake.js +41 -4
  14. package/lib/protocols/governance/revoke-stake.js +14 -3
  15. package/lib/protocols/governance/stake.js +87 -5
  16. package/lib/protocols/rollup/claim-reward.js +46 -16
  17. package/lib/protocols/rollup/create-block.js +35 -3
  18. package/lib/protocols/rollup/create.js +8 -18
  19. package/lib/protocols/rollup/join.js +25 -2
  20. package/lib/protocols/rollup/leave.js +15 -2
  21. package/lib/protocols/rollup/migrate-contract.js +13 -2
  22. package/lib/protocols/rollup/migrate-token.js +13 -2
  23. package/lib/protocols/rollup/pause.js +13 -2
  24. package/lib/protocols/rollup/resume.js +13 -2
  25. package/lib/protocols/rollup/update.js +13 -2
  26. package/lib/protocols/token/create.js +19 -9
  27. package/lib/protocols/token/deposit-v2.js +46 -6
  28. package/lib/protocols/token/withdraw-v2.js +45 -6
  29. package/lib/protocols/trade/exchange-v2.js +38 -3
  30. package/lib/protocols/trade/transfer-v2.js +29 -2
  31. package/lib/protocols/trade/transfer-v3.js +34 -1
  32. package/lib/util.js +58 -8
  33. package/package.json +14 -13
  34. package/lib/protocols/rollup/pipes/ensure-service-fee.js +0 -37
package/lib/util.js CHANGED
@@ -1,7 +1,7 @@
1
1
  const groupBy = require('lodash/groupBy');
2
2
  const flattenDeep = require('lodash/flattenDeep');
3
3
  const { CustomError: Error } = require('@ocap/util/lib/error');
4
- const { BN } = require('@ocap/util');
4
+ const { BN, fromTokenToUnit } = require('@ocap/util');
5
5
  const { decodeAny } = require('@ocap/message');
6
6
  const { toStakeAddress } = require('@arcblock/did-util');
7
7
  const cloneDeep = require('lodash/cloneDeep');
@@ -68,7 +68,7 @@ const applyTokenUpdates = (tokens, state, operator) => {
68
68
  const requirement = new BN(value);
69
69
  const balance = new BN(oldTokens[address] || 0);
70
70
  const newBalance = balance[operator](requirement);
71
- if (newBalance.lt(ZERO)) {
71
+ if (newBalance.isNeg()) {
72
72
  throw new Error('NEGATIVE_TOKEN_BALANCE', `Negative token balance when applyTokenUpdates for ${address}`);
73
73
  }
74
74
  newTokens[address] = newBalance.toString(10);
@@ -104,13 +104,13 @@ const getTxFee = ({ amount, feeRate, maxFee, minFee, stringify = true }) => {
104
104
  if (feeRate < 0) {
105
105
  throw new Error('NEGATIVE_FEE_RATE', 'Unexpected negative feeRate when getTxFee, abort!');
106
106
  }
107
- if (userAmount.lt(ZERO)) {
107
+ if (userAmount.isNeg()) {
108
108
  throw new Error('NEGATIVE_AMOUNT', 'Unexpected negative amount when getTxFee, abort!');
109
109
  }
110
- if (maxFeeAmount.lt(ZERO)) {
110
+ if (maxFeeAmount.isNeg()) {
111
111
  throw new Error('NEGATIVE_MAX_FEE', 'Unexpected negative maxFee when getTxFee, abort!');
112
112
  }
113
- if (minFeeAmount.lt(ZERO)) {
113
+ if (minFeeAmount.isNeg()) {
114
114
  throw new Error('NEGATIVE_MIN_FEE', 'Unexpected negative minFee when getTxFee, abort!');
115
115
  }
116
116
 
@@ -143,7 +143,7 @@ const getTxFee = ({ amount, feeRate, maxFee, minFee, stringify = true }) => {
143
143
 
144
144
  const splitTxFee = ({ total, shares = {}, stringify = true }) => {
145
145
  const totalAmount = new BN(total);
146
- if (totalAmount.lt(ZERO)) {
146
+ if (totalAmount.isNeg()) {
147
147
  throw new Error('NEGATIVE_TOTAL_AMOUNT', 'Unexpected negative total when splitTxFee, abort!');
148
148
  }
149
149
  Object.keys(shares).forEach((key) => {
@@ -211,14 +211,14 @@ const ensureBlockReward = (rollupState, minReward, txStates) => {
211
211
 
212
212
  let actualFee = new BN(0);
213
213
  // If totalMissingFee is less than 0, then the tx will be charged for fixedFee
214
- if (totalMissingFee.lt(ZERO)) {
214
+ if (totalMissingFee.isNeg()) {
215
215
  actualFee = defaults.reward;
216
216
  } else {
217
217
  // Else the tx is charged for a portion of totalMissingFee
218
218
  actualFee = totalMissingFee.mul(maxFee).div(totalDynamicFee);
219
219
  }
220
220
 
221
- if (actualFee.lt(ZERO)) {
221
+ if (actualFee.isNeg()) {
222
222
  throw new Error('NEGATIVE_ACTUAL_FEE', 'Got negative actualFee for tx, abort!');
223
223
  }
224
224
 
@@ -313,6 +313,53 @@ const ensureBlockReward = (rollupState, minReward, txStates) => {
313
313
  return result;
314
314
  };
315
315
 
316
+ const isGasStakeAddress = (sender, stakeId) => toStakeAddress(sender, sender) === stakeId;
317
+ const isGasStakeInput = (inputs, token) => {
318
+ if (inputs.length !== 1) {
319
+ return false;
320
+ }
321
+
322
+ const [{ assetsList, tokensList }] = inputs;
323
+ if (assetsList.length > 0) {
324
+ return false;
325
+ }
326
+ if (tokensList.length !== 1) {
327
+ return false;
328
+ }
329
+
330
+ const [tokenInput] = tokensList;
331
+ if (tokenInput.address !== token) {
332
+ return false;
333
+ }
334
+
335
+ return true;
336
+ };
337
+ const isGasStakeValid = (state, config) => {
338
+ if (!state) {
339
+ return false;
340
+ }
341
+
342
+ const { token, transaction } = config;
343
+ const { minStake } = transaction.txGas;
344
+ const expectedMin = fromTokenToUnit(minStake, token.decimal);
345
+
346
+ if (state.tokens) {
347
+ const actualStaked = new BN(state.tokens[token.address] || 0);
348
+ if (actualStaked.gte(expectedMin)) {
349
+ return true;
350
+ }
351
+ }
352
+
353
+ if (state.revokedTokens) {
354
+ const actualRevoked = new BN(state.revokedTokens[token.address] || 0);
355
+ if (actualRevoked.gte(expectedMin)) {
356
+ return true;
357
+ }
358
+ }
359
+
360
+ return false;
361
+ };
362
+
316
363
  module.exports = {
317
364
  decodeAnySafe,
318
365
  applyTokenUpdates,
@@ -324,5 +371,8 @@ module.exports = {
324
371
  ensureBlockReward,
325
372
  getBNSum,
326
373
  isFixedFee,
374
+ isGasStakeAddress,
375
+ isGasStakeInput,
376
+ isGasStakeValid,
327
377
  RATE_BASE,
328
378
  };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.17.23",
6
+ "version": "1.18.1",
7
7
  "description": "Predefined tx pipeline sets to execute certain type of transactions",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -21,17 +21,18 @@
21
21
  "author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
22
22
  "license": "MIT",
23
23
  "dependencies": {
24
- "@arcblock/did": "1.17.23",
25
- "@arcblock/did-util": "1.17.23",
26
- "@arcblock/validator": "1.17.23",
27
- "@ocap/asset": "1.17.23",
28
- "@ocap/mcrypto": "1.17.23",
29
- "@ocap/merkle-tree": "1.17.23",
30
- "@ocap/message": "1.17.23",
31
- "@ocap/state": "1.17.23",
32
- "@ocap/tx-pipeline": "1.17.23",
33
- "@ocap/util": "1.17.23",
34
- "@ocap/wallet": "1.17.23",
24
+ "@arcblock/did": "1.18.1",
25
+ "@arcblock/did-util": "1.18.1",
26
+ "@arcblock/jwt": "1.18.1",
27
+ "@arcblock/validator": "1.18.1",
28
+ "@ocap/asset": "1.18.1",
29
+ "@ocap/mcrypto": "1.18.1",
30
+ "@ocap/merkle-tree": "1.18.1",
31
+ "@ocap/message": "1.18.1",
32
+ "@ocap/state": "1.18.1",
33
+ "@ocap/tx-pipeline": "1.18.1",
34
+ "@ocap/util": "1.18.1",
35
+ "@ocap/wallet": "1.18.1",
35
36
  "debug": "^4.3.4",
36
37
  "deep-diff": "^1.0.2",
37
38
  "empty-value": "^1.0.1",
@@ -46,5 +47,5 @@
46
47
  "jest": "^27.5.1",
47
48
  "start-server-and-test": "^1.14.0"
48
49
  },
49
- "gitHead": "aecdce3fc6aaee32ea60db191d61962963f5e0bf"
50
+ "gitHead": "7735e4243a1ba253eb14d960ca272974eaf94623"
50
51
  }
@@ -1,37 +0,0 @@
1
- const { CustomError: Error } = require('@ocap/util/lib/error');
2
- const { fromTokenToUnit, BN } = require('@ocap/util');
3
-
4
- const { applyTokenUpdates } = require('../../../util');
5
-
6
- module.exports = async (context, next) => {
7
- const { config, statedb, txType, senderState } = context;
8
- const txFee = config.transaction.txFee[txType];
9
- const vaultState = await statedb.account.get(config.vaults.txFee, context);
10
-
11
- if (!txFee) {
12
- context.senderUpdates = {};
13
- context.vaultUpdates = {};
14
- context.vaultState = vaultState;
15
- context.updatedAccounts = [];
16
- return next();
17
- }
18
-
19
- const expected = fromTokenToUnit(txFee, config.token.decimal);
20
- const actual = new BN(senderState.tokens[config.token.address] || 0);
21
- if (actual.lt(expected)) {
22
- return next(new Error('INSUFFICIENT_FUND', `Insufficient fund to pay for tx fee, expected ${txFee}`));
23
- }
24
-
25
- const tokenChange = { address: config.token.address, value: expected.toString(10) };
26
-
27
- context.senderUpdates = applyTokenUpdates([tokenChange], senderState, 'sub');
28
- context.vaultUpdates = applyTokenUpdates([tokenChange], vaultState, 'add');
29
- context.vaultState = vaultState;
30
-
31
- context.updatedAccounts = [
32
- { address: senderState.address, token: config.token.address, delta: `-${tokenChange.value}`, action: 'fee' },
33
- { address: vaultState.address, token: config.token.address, delta: tokenChange.value, action: 'fee' },
34
- ];
35
-
36
- return next();
37
- };