@ocap/tx-protocols 1.18.88 → 1.18.90

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.
@@ -1,6 +1,6 @@
1
1
  const noop = require('lodash/noop');
2
2
  const { CustomError: Error } = require('@ocap/util/lib/error');
3
- const { fromTokenToUnit, BN } = require('@ocap/util');
3
+ const { fromTokenToUnit, BN, fromUnitToToken } = require('@ocap/util');
4
4
  const JWT = require('@arcblock/jwt');
5
5
  const { account } = require('@ocap/state');
6
6
  const { toAddress } = require('@arcblock/did');
@@ -11,6 +11,8 @@ const debug = require('debug')(`${require('../../package.json').name}:pipes:ensu
11
11
 
12
12
  const { applyTokenUpdates, isGasStakeValid } = require('../util');
13
13
 
14
+ const ZERO = new BN(0);
15
+
14
16
  // We have a layered transaction cost design
15
17
  // - gas: charged for every tx for creating/updating states on the ledger
16
18
  // - service fee: charged for creating tokens/assets/factories/rollups
@@ -25,22 +27,6 @@ module.exports = function CreateEnsureTxCostPipe({
25
27
  const { config, statedb, txType, senderState, gasEstimate, gasVaultState, totalGas } = context;
26
28
  const feeVaultState = await statedb.account.get(config.vaults.txFee, context);
27
29
 
28
- // Get service fee and gas fee
29
- const changes = {};
30
- let txCost = new BN(0);
31
- const txFee = config.transaction.txFee[txType];
32
- if (!gasOnly && txFee) {
33
- const totalFee = fromTokenToUnit(txFee, config.token.decimal);
34
- txCost = txCost.add(totalFee);
35
- changes.fee = { address: config.token.address, value: totalFee.toString(10) };
36
- context.feeVaultChange = changes.fee;
37
- }
38
- if (totalGas.gt(new BN(0))) {
39
- txCost = txCost.add(totalGas);
40
- changes.gas = { address: config.token.address, value: totalGas.toString(10) };
41
- context.gasVaultChange = changes.gas;
42
- }
43
-
44
30
  // verify gas staking headers
45
31
  const { tx, extra = {}, txHash } = context;
46
32
  const { token, pk } = extra.gasStakeHeaders || {};
@@ -58,6 +44,7 @@ module.exports = function CreateEnsureTxCostPipe({
58
44
  console.warn(`Invalid gas payer header for: ${txHash}`);
59
45
  }
60
46
  }
47
+ // fallback to tx sender
61
48
  if (!gasStake.owner) {
62
49
  gasStake.owner = tx.from;
63
50
  gasStake.stakeId = toStakeAddress(gasStake.owner, gasStake.owner);
@@ -67,13 +54,31 @@ module.exports = function CreateEnsureTxCostPipe({
67
54
  context.gasStake = gasStake;
68
55
  debug('gasStake', gasStake, context.extra);
69
56
 
70
- let isGasCharged = false;
71
- if (senderState && !gasStake.valid) {
57
+ // Get service fee and gas fee
58
+ const changes = {};
59
+ let txCost = new BN(0);
60
+ const txFee = config.transaction.txFee[txType];
61
+ // always charge tx fee
62
+ if (!gasOnly && txFee) {
63
+ const totalFee = fromTokenToUnit(txFee, config.token.decimal);
64
+ txCost = txCost.add(totalFee);
65
+ changes.fee = { address: config.token.address, value: totalFee.toString(10) };
66
+ context.feeVaultChange = changes.fee;
67
+ }
68
+ // only charge gas when gas stake is not valid
69
+ if (!gasStake.valid && totalGas.gt(ZERO)) {
70
+ txCost = txCost.add(totalGas);
71
+ changes.gas = { address: config.token.address, value: totalGas.toString(10) };
72
+ context.gasVaultChange = changes.gas;
73
+ }
74
+
75
+ let isCostCharged = false;
76
+ if (senderState && txCost.gt(ZERO)) {
72
77
  const expected = new BN(gasEstimate.payment || 0).add(txCost);
73
78
  const actual = new BN(senderState.tokens[config.token.address] || 0);
74
79
  // If we have someone with enough balance to pay for this tx
75
80
  if (actual.gte(expected)) {
76
- isGasCharged = true;
81
+ isCostCharged = true;
77
82
 
78
83
  // to be merged into later pipe
79
84
  context.senderUpdates = applyTokenUpdates(
@@ -151,13 +156,16 @@ module.exports = function CreateEnsureTxCostPipe({
151
156
  return next(
152
157
  new Error(
153
158
  'INSUFFICIENT_FUND',
154
- `Insufficient fund to pay for tx cost from ${senderState.address}, expected ${expected}, got ${actual}`
159
+ `Insufficient fund to pay for tx cost from ${senderState.address}, expected ${fromUnitToToken(
160
+ expected,
161
+ config.token.decimal
162
+ )}, got ${fromUnitToToken(actual, config.token.decimal)}`
155
163
  )
156
164
  );
157
165
  }
158
166
  }
159
167
 
160
- if (!isGasCharged) {
168
+ if (!isCostCharged) {
161
169
  context.senderUpdates = {};
162
170
  context.senderChange = null;
163
171
  context.updateVaults = noop;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.18.88",
6
+ "version": "1.18.90",
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.88",
25
- "@arcblock/did-util": "1.18.88",
26
- "@arcblock/jwt": "1.18.88",
27
- "@arcblock/validator": "1.18.88",
28
- "@ocap/asset": "1.18.88",
29
- "@ocap/mcrypto": "1.18.88",
30
- "@ocap/merkle-tree": "1.18.88",
31
- "@ocap/message": "1.18.88",
32
- "@ocap/state": "1.18.88",
33
- "@ocap/tx-pipeline": "1.18.88",
34
- "@ocap/util": "1.18.88",
35
- "@ocap/wallet": "1.18.88",
24
+ "@arcblock/did": "1.18.90",
25
+ "@arcblock/did-util": "1.18.90",
26
+ "@arcblock/jwt": "1.18.90",
27
+ "@arcblock/validator": "1.18.90",
28
+ "@ocap/asset": "1.18.90",
29
+ "@ocap/mcrypto": "1.18.90",
30
+ "@ocap/merkle-tree": "1.18.90",
31
+ "@ocap/message": "1.18.90",
32
+ "@ocap/state": "1.18.90",
33
+ "@ocap/tx-pipeline": "1.18.90",
34
+ "@ocap/util": "1.18.90",
35
+ "@ocap/wallet": "1.18.90",
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": "ab1df2e118a55d3e0350ee45f2927342ae0e17b3"
50
+ "gitHead": "2f6dfa9b33c0b8846dc3971c37e74cb4b60b29d6"
51
51
  }