@ocap/state 1.18.151 → 1.18.153

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 +41 -2
  2. package/package.json +9 -9
package/lib/states/tx.js CHANGED
@@ -62,6 +62,12 @@ const getTxSender = ({ tx, typeUrl }) => {
62
62
  }
63
63
  };
64
64
 
65
+ const getReceiptChange = (receipts, { address, action }) => {
66
+ const receipt = receipts.filter((x) => !address || x.address === address);
67
+ const changes = receipt.map((x) => x.changes.filter((c) => !action || c.action === action)).flat();
68
+ return changes;
69
+ };
70
+
65
71
  const getTransferReceipts = (tx) => {
66
72
  const senderReceipt = { address: tx.from, changes: [] };
67
73
  const receiverReceipt = { address: tx.itxJson.to, changes: [] };
@@ -118,6 +124,34 @@ const getReceiptsFromContext = (ctx) => {
118
124
  return [];
119
125
  };
120
126
 
127
+ /**
128
+ * Add receipts for tx that have gasPaid but no receipts
129
+ * This is used to handle legacy data that gas receipts were not properly recorded
130
+ */
131
+ const getReceiptsFromGasPaid = (tx, { config, typeUrl }) => {
132
+ const gasReceipt = getReceiptChange(tx.receipts || [], { action: 'gas' });
133
+ const gasPaid = new BN(tx.gasPaid || 0);
134
+
135
+ // For new transactions being created, gas receipts are usually attached by getReceiptsFromContext before,
136
+ // so we don't add duplicate gas receipts here
137
+ if (gasReceipt.length) return [];
138
+ if (gasPaid.lte(ZERO)) return [];
139
+
140
+ const gasPayerMap = {
141
+ AccountMigrateTx: tx.itxJson?.address,
142
+ };
143
+ const gasPayer = gasPayerMap[typeUrl] || tx.from;
144
+ // Cannot determine who received the gas fee in legacy data
145
+ const gasReceiver = config?.vaults?.txGas?.[0] || FORGE_FEE_RECEIVER;
146
+
147
+ if (!gasPayer) return [];
148
+
149
+ return [
150
+ { address: gasPayer, changes: [{ target: '', action: 'gas', value: gasPaid.neg().toString() }] },
151
+ { address: gasReceiver, changes: [{ target: '', action: 'gas', value: gasPaid.toString() }] },
152
+ ];
153
+ };
154
+
121
155
  const getTransferV3Receipts = (tx) => [
122
156
  ...getReceiptsFromTxInput(tx.itxJson.inputs, '-', 'transfer'),
123
157
  ...getReceiptsFromTxInput(tx.itxJson.outputs, '', 'transfer'),
@@ -506,6 +540,7 @@ const verifyTxReceipts = (receipts, typeUrl, ctx = {}) => {
506
540
  CreateRollupBlockTx: ['burn', 'mint'],
507
541
  AccountMigrateTx: ['migrate'],
508
542
  ApproveWithdrawTx: ['burn'],
543
+ SetupSwapTx: ['swap'],
509
544
  }[typeUrl] || [];
510
545
 
511
546
  for (const [target, changes] of Object.entries(targets)) {
@@ -561,12 +596,15 @@ const verifyTxReceipts = (receipts, typeUrl, ctx = {}) => {
561
596
  * @return {Array} list of receipts
562
597
  */
563
598
  const getTxReceipts = ({ tx, code }, ctx = {}) => {
599
+ const typeUrl = upperFirst(camelCase(tx?.itxJson?._type || ''));
600
+
564
601
  if (code !== 'OK') {
565
- return ctx.gasPaid ? getReceiptsFromContext(ctx) : [];
602
+ const receiptsFromContext = ctx.gasPaid ? getReceiptsFromContext(ctx) : [];
603
+ const receiptsFromGas = getReceiptsFromGasPaid(tx, { ...ctx, typeUrl });
604
+ return receiptsFromContext.concat(receiptsFromGas);
566
605
  }
567
606
 
568
607
  let receipts = Array.isArray(ctx.receipts) ? ctx.receipts : [];
569
- const typeUrl = upperFirst(camelCase(tx.itxJson._type));
570
608
 
571
609
  if (['TransferTx', 'TransferV2Tx'].includes(typeUrl)) {
572
610
  receipts = receipts.concat(getTransferReceipts(tx, ctx));
@@ -611,6 +649,7 @@ const getTxReceipts = ({ tx, code }, ctx = {}) => {
611
649
  }
612
650
 
613
651
  receipts = receipts.concat(getReceiptsFromContext(ctx));
652
+ receipts = receipts.concat(getReceiptsFromGasPaid(tx, { ...ctx, typeUrl }));
614
653
 
615
654
  const merged = mergeTxReceipts(receipts).filter((x) => x.address && Array.isArray(x.changes) && x.changes.length);
616
655
 
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.18.151",
6
+ "version": "1.18.153",
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.151",
22
- "@arcblock/validator": "1.18.151",
23
- "@ocap/contract": "1.18.151",
24
- "@ocap/mcrypto": "1.18.151",
25
- "@ocap/message": "1.18.151",
26
- "@ocap/util": "1.18.151",
27
- "@ocap/wallet": "1.18.151",
21
+ "@arcblock/did": "1.18.153",
22
+ "@arcblock/validator": "1.18.153",
23
+ "@ocap/contract": "1.18.153",
24
+ "@ocap/mcrypto": "1.18.153",
25
+ "@ocap/message": "1.18.153",
26
+ "@ocap/util": "1.18.153",
27
+ "@ocap/wallet": "1.18.153",
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": "ad9f0a1c3913c304a716247de283be2146b7105e"
38
+ "gitHead": "ad18c565becef73d6ee782502c3f4858de43b68b"
39
39
  }