@ocap/state 1.18.150 → 1.18.152

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 (2) hide show
  1. package/lib/states/tx.js +127 -14
  2. package/package.json +9 -9
package/lib/states/tx.js CHANGED
@@ -5,7 +5,7 @@ const groupBy = require('lodash/groupBy');
5
5
  const upperFirst = require('lodash/upperFirst');
6
6
  const camelCase = require('lodash/camelCase');
7
7
  const { fromTypeUrl, formatMessage, decodeAny } = require('@ocap/message');
8
- const { toBase64, BN, isBN } = require('@ocap/util');
8
+ const { toBase64, BN, isBN, fromTokenToUnit } = require('@ocap/util');
9
9
  const { getListField } = require('@ocap/util/lib/get-list-field');
10
10
  const { CustomError: Error } = require('@ocap/util/lib/error');
11
11
  const { getRelatedAddresses } = require('@ocap/util/lib/get-related-addr');
@@ -14,6 +14,10 @@ const { toTypeInfo, types } = require('@arcblock/did');
14
14
  const ZERO = new BN(0);
15
15
  const { RoleType } = types;
16
16
 
17
+ const FORGE_TOKEN_HOLDER = 'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz';
18
+ const FORGE_FEE_RECEIVER = 'z1EJUBdbPYqMiWHfRZvSAvbYeWcEHB19Xyfc';
19
+ const QLDB_MIGRATION_TIME = new Date('2021-05-03T12:46:56.245Z');
20
+
17
21
  // Receiver = Whose asset have net gain
18
22
  const getTxReceiver = ({ tx, itx, typeUrl }) => {
19
23
  switch (typeUrl) {
@@ -281,12 +285,93 @@ const getDepositTokenReceipts = (tx) => [
281
285
  changes: [{ target: '', action: 'mint', value: tx.itxJson.value }],
282
286
  },
283
287
  ];
284
- const getWithdrawTokenReceipts = (tx) => [
285
- {
286
- address: tx.from,
287
- changes: [{ target: '', action: 'burn', value: `-${tx.itxJson.value}` }],
288
- },
289
- ];
288
+
289
+ const getWithdrawFee = (value) => {
290
+ let fee = new BN(value).abs().mul(new BN('1')).div(new BN('1000')); // 0.1% fee
291
+ fee = BN.min(fee, new BN('100000000000000000000')); // max 100 ABT
292
+ fee = BN.max(fee, new BN('1000000000000000000')); // min 1 ABT
293
+ return fee;
294
+ };
295
+
296
+ const getWithdrawTokenReceipts = (tx) => {
297
+ const fee = getWithdrawFee(tx.itxJson.value);
298
+
299
+ return [
300
+ {
301
+ address: tx.from,
302
+ changes: [
303
+ { target: '', action: 'lock', value: `-${tx.itxJson.value}` },
304
+ { target: '', action: 'fee', value: `-${fee.toString()}` },
305
+ ],
306
+ },
307
+ {
308
+ address: FORGE_TOKEN_HOLDER,
309
+ changes: [
310
+ { target: '', action: 'lock', value: tx.itxJson.value },
311
+ { target: '', action: 'fee', value: fee.toString() },
312
+ ],
313
+ },
314
+ ];
315
+ };
316
+
317
+ const getRevokeWithdrawReceipts = (tx, { withdrawTx }) => {
318
+ if (!withdrawTx?.tx?.from) return [];
319
+ if (withdrawTx.hash !== tx.itxJson.withdraw_tx_hash) return [];
320
+
321
+ const account = withdrawTx.tx.from;
322
+ const withdrawReceipt = withdrawTx.receipts?.find((x) => x.address === account);
323
+ const lockChange = withdrawReceipt?.changes?.find((x) => x.action === 'lock');
324
+
325
+ if (!lockChange) return [];
326
+
327
+ const value = new BN(lockChange.value).abs();
328
+ const fee = getWithdrawFee(value.toString());
329
+ const revokeFee = fee.mul(new BN('5')).div(new BN('100')); // 5% fee
330
+
331
+ return [
332
+ {
333
+ address: account,
334
+ changes: [
335
+ { target: '', action: 'unlock', value: value.toString() },
336
+ { target: '', action: 'fee', value: fee.sub(revokeFee).toString() },
337
+ ],
338
+ },
339
+ {
340
+ address: FORGE_TOKEN_HOLDER,
341
+ changes: [
342
+ { target: '', action: 'unlock', value: `-${value.toString()}` },
343
+ { target: '', action: 'fee', value: `-${fee.toString()}` },
344
+ ],
345
+ },
346
+ {
347
+ address: FORGE_FEE_RECEIVER,
348
+ changes: [{ target: '', action: 'fee', value: revokeFee.toString() }],
349
+ },
350
+ ];
351
+ };
352
+
353
+ const getApproveWithdrawReceipts = (tx, { withdrawTx }) => {
354
+ if (!withdrawTx?.tx?.itxJson?.value) return [];
355
+ if (withdrawTx.hash !== tx.itxJson.withdraw_tx_hash) return [];
356
+
357
+ const { value } = withdrawTx.tx.itxJson;
358
+ const fee = getWithdrawFee(value);
359
+
360
+ return [
361
+ {
362
+ address: FORGE_TOKEN_HOLDER,
363
+ changes: [
364
+ { target: '', action: 'burn', value: `-${value}` },
365
+ { target: '', action: 'fee', value: `-${fee.toString()}` },
366
+ ],
367
+ },
368
+ {
369
+ address: FORGE_FEE_RECEIVER,
370
+ changes: [{ target: '', action: 'fee', value: fee.toString() }],
371
+ },
372
+ ];
373
+ };
374
+
290
375
  const getSetupSwapReceipts = (tx) => [
291
376
  {
292
377
  address: tx.from,
@@ -313,17 +398,36 @@ const getRetrieveSwapReceipts = (tx) => [
313
398
  ].filter(Boolean),
314
399
  },
315
400
  ];
316
- const getRevokeWithdrawReceipts = (tx, { withdrawTx }) => {
317
- const receipt = withdrawTx?.receipts?.find((x) => x.address === withdrawTx.tx.from);
318
- const change = receipt?.changes?.find((x) => x.action === 'burn');
319
401
 
320
- if (!change) return [];
321
- if (tx.itxJson?.withdraw_tx_hash !== withdrawTx.hash) return [];
402
+ const getMigrateReceipts = (tx, { fromState }) => {
403
+ if (!fromState?.tokens) return [];
404
+ if (!tx.itxJson?.address) return [];
322
405
 
323
406
  return [
324
407
  {
325
- address: withdrawTx.tx.from,
326
- changes: [{ target: change.target, action: 'mint', value: new BN(change.value).abs().toString() }],
408
+ address: tx.itxJson.address,
409
+ changes: fromState.tokens.map((x) => ({ target: x.address, action: 'migrate', value: x.value })),
410
+ },
411
+ ];
412
+ };
413
+
414
+ const getDeclareReceipts = (tx, { config, time }) => {
415
+ const txTime = new Date(time);
416
+
417
+ if (!tx.itxJson?.issuer) return [];
418
+ if (!config?.token) return [];
419
+ if (txTime >= QLDB_MIGRATION_TIME) return [];
420
+
421
+ const gas = fromTokenToUnit('0.5', config.token.decimal);
422
+
423
+ return [
424
+ {
425
+ address: tx.itxJson?.issuer,
426
+ changes: [{ target: config.token.address, action: 'gas', value: `-${gas.toString()}` }],
427
+ },
428
+ {
429
+ address: FORGE_FEE_RECEIVER,
430
+ changes: [{ target: config.token.address, action: 'gas', value: gas.toString() }],
327
431
  },
328
432
  ];
329
433
  };
@@ -400,6 +504,8 @@ const verifyTxReceipts = (receipts, typeUrl, ctx = {}) => {
400
504
  CreateAssetTx: ['create'],
401
505
  CreateTokenTx: ['mint'],
402
506
  CreateRollupBlockTx: ['burn', 'mint'],
507
+ AccountMigrateTx: ['migrate'],
508
+ ApproveWithdrawTx: ['burn'],
403
509
  }[typeUrl] || [];
404
510
 
405
511
  for (const [target, changes] of Object.entries(targets)) {
@@ -496,6 +602,12 @@ const getTxReceipts = ({ tx, code }, ctx = {}) => {
496
602
  receipts = receipts.concat(getClaimStakeReceipts(tx, ctx, 'stake'));
497
603
  } else if (['RevokeWithdrawTx'].includes(typeUrl)) {
498
604
  receipts = receipts.concat(getRevokeWithdrawReceipts(tx, ctx));
605
+ } else if (['ApproveWithdrawTx'].includes(typeUrl)) {
606
+ receipts = receipts.concat(getApproveWithdrawReceipts(tx, ctx));
607
+ } else if (['AccountMigrateTx'].includes(typeUrl)) {
608
+ receipts = receipts.concat(getMigrateReceipts(tx, ctx));
609
+ } else if (['DeclareTx'].includes(typeUrl)) {
610
+ receipts = receipts.concat(getDeclareReceipts(tx, ctx));
499
611
  }
500
612
 
501
613
  receipts = receipts.concat(getReceiptsFromContext(ctx));
@@ -607,4 +719,5 @@ module.exports = {
607
719
  mergeTxReceipts,
608
720
  attachPaidTxGas,
609
721
  verifyTxReceipts,
722
+ FORGE_TOKEN_HOLDER,
610
723
  };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.18.150",
6
+ "version": "1.18.152",
7
7
  "description": "",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -18,13 +18,13 @@
18
18
  "coverage": "start-server-and-test start http://127.0.0.1:4001 test:ci"
19
19
  },
20
20
  "dependencies": {
21
- "@arcblock/did": "1.18.150",
22
- "@arcblock/validator": "1.18.150",
23
- "@ocap/contract": "1.18.150",
24
- "@ocap/mcrypto": "1.18.150",
25
- "@ocap/message": "1.18.150",
26
- "@ocap/util": "1.18.150",
27
- "@ocap/wallet": "1.18.150",
21
+ "@arcblock/did": "1.18.152",
22
+ "@arcblock/validator": "1.18.152",
23
+ "@ocap/contract": "1.18.152",
24
+ "@ocap/mcrypto": "1.18.152",
25
+ "@ocap/message": "1.18.152",
26
+ "@ocap/util": "1.18.152",
27
+ "@ocap/wallet": "1.18.152",
28
28
  "bloom-filters": "^1.3.9",
29
29
  "lodash": "^4.17.21"
30
30
  },
@@ -35,5 +35,5 @@
35
35
  "keywords": [],
36
36
  "author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
37
37
  "license": "MIT",
38
- "gitHead": "700ffd83b7fbb781d68ef761aa9b1b004a961d9f"
38
+ "gitHead": "4f31d06aa2a15b57a7d00fdec6241376f10ee873"
39
39
  }