@ocap/tx-protocols 1.13.61 → 1.13.62

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.
@@ -235,7 +235,7 @@ runner.use(pipes.ExtractState({ from: 'stakeAddress', to: 'stakeStates', status:
235
235
  runner.use((context, next) => {
236
236
  const { itx, txStates, rollupState } = context;
237
237
  try {
238
- const result = ensureBlockReward(rollupState.address, rollupState.tokenAddress, itx.minReward, txStates);
238
+ const result = ensureBlockReward(rollupState, itx.minReward, txStates);
239
239
  context.senders = Object.keys(result.accountUpdates);
240
240
  Object.assign(context, result);
241
241
  return next();
package/lib/util.js CHANGED
@@ -7,6 +7,7 @@ const { toStakeAddress } = require('@arcblock/did-util');
7
7
  const cloneDeep = require('lodash/cloneDeep');
8
8
 
9
9
  const ZERO = new BN(0);
10
+ const RATE_BASE = new BN(10000);
10
11
 
11
12
  const decodeAnyNested = (encoded) => {
12
13
  if (!encoded) {
@@ -95,12 +96,24 @@ const fixTokenInput = (input, config) => {
95
96
  return input;
96
97
  };
97
98
 
98
- const RATE_BASE = new BN(10000);
99
99
  const getTxFee = ({ amount, feeRate, maxFee, minFee, stringify = true }) => {
100
100
  const userAmount = new BN(amount);
101
101
  const maxFeeAmount = new BN(maxFee);
102
102
  const minFeeAmount = new BN(minFee);
103
103
 
104
+ if (feeRate < 0) {
105
+ throw new Error('FORBIDDEN', 'Unexpected negative feeRate when getTxFee, abort!');
106
+ }
107
+ if (userAmount.lt(ZERO)) {
108
+ throw new Error('FORBIDDEN', 'Unexpected negative amount when getTxFee, abort!');
109
+ }
110
+ if (maxFeeAmount.lt(ZERO)) {
111
+ throw new Error('FORBIDDEN', 'Unexpected negative maxFee when getTxFee, abort!');
112
+ }
113
+ if (minFeeAmount.lt(ZERO)) {
114
+ throw new Error('FORBIDDEN', 'Unexpected negative minFee when getTxFee, abort!');
115
+ }
116
+
104
117
  // total fee
105
118
  let rewardAmount = userAmount.mul(new BN(feeRate)).div(RATE_BASE);
106
119
  if (rewardAmount.lt(minFeeAmount)) {
@@ -130,6 +143,15 @@ const getTxFee = ({ amount, feeRate, maxFee, minFee, stringify = true }) => {
130
143
 
131
144
  const splitTxFee = ({ total, shares = {}, stringify = true }) => {
132
145
  const totalAmount = new BN(total);
146
+ if (totalAmount.lt(ZERO)) {
147
+ throw new Error('FORBIDDEN', 'Unexpected negative total when splitTxFee, abort!');
148
+ }
149
+ Object.keys(shares).forEach((key) => {
150
+ if (shares[key] < 0) {
151
+ throw new Error('FORBIDDEN', `Unexpected negative shares[${key}] when splitTxFee, abort!`);
152
+ }
153
+ });
154
+
133
155
  const rewardShares = Object.keys(shares).reduce((acc, x) => {
134
156
  acc[x] = totalAmount.mul(new BN(shares[x])).div(RATE_BASE);
135
157
  return acc;
@@ -145,8 +167,9 @@ const getRewardLocker = (rollupAddress) => toStakeAddress(rollupAddress, rollupA
145
167
  const getBNSum = (...args) => flattenDeep(args).reduce((sum, x) => sum.add(new BN(x)), new BN(0)).toString(10); // prettier-ignore
146
168
  const isFixedFee = (x) => new BN(x.tx.itxJson.maxFee).isZero();
147
169
 
148
- const ensureBlockReward = (rollupAddress, tokenAddress, minReward, txStates) => {
149
- const locker = toStakeAddress(rollupAddress, rollupAddress);
170
+ const ensureBlockReward = (rollupState, minReward, txStates) => {
171
+ const { address, withdrawFeeRate, minWithdrawFee, maxWithdrawFee, tokenAddress } = rollupState;
172
+ const locker = getRewardLocker(address);
150
173
 
151
174
  const result = {
152
175
  mintedAmount: new BN(0),
@@ -177,7 +200,25 @@ const ensureBlockReward = (rollupAddress, tokenAddress, minReward, txStates) =>
177
200
  const changes = { stake: [], account: [] };
178
201
  dynamicFeeTxs.forEach((x) => {
179
202
  const maxFee = new BN(x.tx.itxJson.maxFee);
180
- const actualFee = totalMissingFee.mul(maxFee).div(totalDynamicFee);
203
+ let actualFee = new BN(0);
204
+ // If totalMissingFee is less than 0, then the tx will be charged for fixedFee
205
+ if (totalMissingFee.lt(ZERO)) {
206
+ const fee = getTxFee({
207
+ amount: x.tx.itxJson.token.value,
208
+ feeRate: withdrawFeeRate,
209
+ maxFee: maxWithdrawFee,
210
+ minFee: minWithdrawFee,
211
+ stringify: false,
212
+ });
213
+ actualFee = fee.reward;
214
+ } else {
215
+ // Else the tx is charged for a portion of totalMissingFee
216
+ actualFee = totalMissingFee.mul(maxFee).div(totalDynamicFee);
217
+ }
218
+
219
+ if (actualFee.lt(ZERO)) {
220
+ throw new Error('FORBIDDEN', 'Got negative actualFee for tx, abort!');
221
+ }
181
222
 
182
223
  // If the actualFee is less than the maxFee, user will have a refund
183
224
  if (actualFee.lt(maxFee)) {
@@ -209,7 +250,7 @@ const ensureBlockReward = (rollupAddress, tokenAddress, minReward, txStates) =>
209
250
 
210
251
  // mint tokens for deposit proposer
211
252
  changes.stake.push({
212
- address: toStakeAddress(x.tx.itxJson.proposer, rollupAddress),
253
+ address: toStakeAddress(x.tx.itxJson.proposer, address),
213
254
  delta: total,
214
255
  action: 'mint',
215
256
  });
@@ -219,7 +260,7 @@ const ensureBlockReward = (rollupAddress, tokenAddress, minReward, txStates) =>
219
260
 
220
261
  // burn tokens from locked withdraws: user amount
221
262
  changes.stake.push({
222
- address: toStakeAddress(x.tx.from, rollupAddress),
263
+ address: toStakeAddress(x.tx.from, address),
223
264
  delta: `-${user}`,
224
265
  action: 'burn',
225
266
  });
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.13.61",
6
+ "version": "1.13.62",
7
7
  "description": "Predefined tx pipeline sets to execute certain type of transactions",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -19,16 +19,16 @@
19
19
  "author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
20
20
  "license": "MIT",
21
21
  "dependencies": {
22
- "@arcblock/did": "1.13.61",
23
- "@arcblock/did-util": "1.13.61",
24
- "@ocap/asset": "1.13.61",
25
- "@ocap/mcrypto": "1.13.61",
26
- "@ocap/merkle-tree": "1.13.61",
27
- "@ocap/message": "1.13.61",
28
- "@ocap/state": "1.13.61",
29
- "@ocap/tx-pipeline": "1.13.61",
30
- "@ocap/util": "1.13.61",
31
- "@ocap/wallet": "1.13.61",
22
+ "@arcblock/did": "1.13.62",
23
+ "@arcblock/did-util": "1.13.62",
24
+ "@ocap/asset": "1.13.62",
25
+ "@ocap/mcrypto": "1.13.62",
26
+ "@ocap/merkle-tree": "1.13.62",
27
+ "@ocap/message": "1.13.62",
28
+ "@ocap/state": "1.13.62",
29
+ "@ocap/tx-pipeline": "1.13.62",
30
+ "@ocap/util": "1.13.62",
31
+ "@ocap/wallet": "1.13.62",
32
32
  "debug": "^4.3.2",
33
33
  "empty-value": "^1.0.1",
34
34
  "lodash": "^4.17.21",
@@ -41,5 +41,5 @@
41
41
  "devDependencies": {
42
42
  "jest": "^27.3.1"
43
43
  },
44
- "gitHead": "79d07bbde4df1d0d37afe76aaa1eaa2f30cc55a7"
44
+ "gitHead": "a14f8d2ade8dc0c58fd9fbf331a4eb9b842de6ed"
45
45
  }