@ocap/resolver 1.13.16 → 1.13.17

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/index.js +64 -87
  2. package/package.json +10 -10
package/lib/index.js CHANGED
@@ -13,7 +13,7 @@ const md5 = require('@ocap/util/lib/md5');
13
13
  const CustomError = require('@ocap/util/lib/error');
14
14
  const { types } = require('@ocap/mcrypto');
15
15
  const { fromTypeUrl } = require('@ocap/message');
16
- const { fromPublicKey, toTypeInfo } = require('@arcblock/did');
16
+ const { fromPublicKey, toTypeInfo, isValid: isValidDid } = require('@arcblock/did');
17
17
  const { toBN, fromTokenToUnit } = require('@ocap/util');
18
18
  const { DEFAULT_TOKEN_DECIMAL } = require('@ocap/util/lib/constant');
19
19
  const { createExecutor } = require('@ocap/tx-protocols');
@@ -87,7 +87,6 @@ const extractTokenMeta = (address, tokenStates) => {
87
87
  const tokenState = tokenStates.find((t) => t.address === address);
88
88
 
89
89
  return {
90
- key: address, // TODO: 这里是为了测试时的兼容
91
90
  address,
92
91
  decimal: typeof tokenState.decimal === 'undefined' ? DEFAULT_TOKEN_DECIMAL : tokenState.decimal,
93
92
  unit: tokenState.unit,
@@ -263,7 +262,6 @@ module.exports = class OCAPResolver {
263
262
  if (state.tokens) {
264
263
  state.tokens = await this.formatTokenMap(state.tokens);
265
264
  }
266
- return this.restoreStateBalance(state, ctx);
267
265
  }
268
266
 
269
267
  return state;
@@ -314,7 +312,7 @@ module.exports = class OCAPResolver {
314
312
 
315
313
  const blockHeight = Number(height || 0);
316
314
  if (blockHeight > 0 && rollupAddress) {
317
- const { blocks } = await this._doSearch(
315
+ const { blocks } = await this._doPaginatedSearch(
318
316
  'listRollupBlocks',
319
317
  { height: blockHeight, rollupAddress },
320
318
  'blocks',
@@ -350,15 +348,11 @@ module.exports = class OCAPResolver {
350
348
 
351
349
  state.output.data = formatData(state.output.data);
352
350
 
353
- if (!get(ctx, 'featureSwitch.consistentTokens')) {
354
- const index = state.input.tokens.findIndex((x) => x.address === this.tokenItx.address);
355
- if (index > -1) {
356
- state.input.value = state.input.tokens[index].value;
357
- state.input.tokens.splice(index, 1);
358
- }
351
+ const index = state.input.tokens.findIndex((x) => x.address === this.tokenItx.address);
352
+ if (index > -1) {
353
+ state.input.value = state.input.tokens[index].value;
354
+ state.input.tokens.splice(index, 1);
359
355
  }
360
-
361
- return this.restoreStateBalance(state, ctx);
362
356
  }
363
357
 
364
358
  return state;
@@ -394,8 +388,6 @@ module.exports = class OCAPResolver {
394
388
  state = await this._getState({ table: 'account', id: address, dataKey: 'data', expandContext: false, ctx });
395
389
  }
396
390
 
397
- state = this.restoreStateBalance(state, ctx);
398
-
399
391
  if (!state || !state.tokens) {
400
392
  return [];
401
393
  }
@@ -491,51 +483,74 @@ module.exports = class OCAPResolver {
491
483
  }
492
484
 
493
485
  listTransactions(args, ctx) {
494
- return this._doSearch('listTransactions', args, 'transactions', 'tx.itxJson.data', ctx);
486
+ return this._doPaginatedSearch('listTransactions', args, 'transactions', 'tx.itxJson.data', ctx);
495
487
  }
496
488
 
497
489
  listAssets(args, ctx) {
498
- return this._doSearch('listAssets', args, 'assets', 'data', ctx);
490
+ return this._doPaginatedSearch('listAssets', args, 'assets', 'data', ctx);
499
491
  }
500
492
 
501
493
  listAssetTransactions(args, ctx) {
502
- return this._doSearch('listAssetTransactions', args, 'transactions', 'tx.itxJson.data', ctx);
494
+ return this._doPaginatedSearch('listAssetTransactions', args, 'transactions', 'tx.itxJson.data', ctx);
503
495
  }
504
496
 
505
497
  async listFactories(args, ctx) {
506
- const result = await this._doSearch('listFactories', args, 'factories', 'data', ctx);
498
+ const result = await this._doPaginatedSearch('listFactories', args, 'factories', 'data', ctx);
507
499
  result.factories = result.factories.map((x) => {
508
500
  x.output.data = formatData(x.output.data);
509
- return this.restoreStateBalance(x, ctx);
501
+ return x;
510
502
  });
511
503
 
512
504
  return result;
513
505
  }
514
506
 
515
- async listTopAccounts(args, ctx) {
507
+ listTopAccounts(args) {
516
508
  if (!args.tokenAddress) {
517
509
  args.tokenAddress = this.tokenItx.address;
518
510
  }
519
511
 
520
- const result = await this._doSearch('listTopAccounts', args, 'accounts');
521
- result.accounts = result.accounts.map((x) => this.restoreStateBalance(x, ctx));
522
- return result;
512
+ return this._doPaginatedSearch('listTopAccounts', args, 'accounts');
523
513
  }
524
514
 
525
515
  listTokens(args, ctx) {
526
- return this._doSearch('listTokens', args, 'tokens', 'data', ctx);
516
+ return this._doPaginatedSearch('listTokens', args, 'tokens', 'data', ctx);
527
517
  }
528
518
 
529
519
  listStakes(args, ctx) {
530
- return this._doSearch('listStakes', args, 'stakes', 'data', ctx);
520
+ return this._doPaginatedSearch('listStakes', args, 'stakes', 'data', ctx);
531
521
  }
532
522
 
533
523
  listRollups(args, ctx) {
534
- return this._doSearch('listRollups', args, 'rollups', 'data', ctx);
524
+ return this._doPaginatedSearch('listRollups', args, 'rollups', 'data', ctx);
535
525
  }
536
526
 
537
527
  listRollupBlocks(args, ctx) {
538
- return this._doSearch('listRollupBlocks', args, 'blocks', 'data', ctx);
528
+ return this._doPaginatedSearch('listRollupBlocks', args, 'blocks', 'data', ctx);
529
+ }
530
+
531
+ async search(args) {
532
+ if (!args.keyword) {
533
+ return { results: [] };
534
+ }
535
+
536
+ const doSearch = async (type) => {
537
+ const result = await this.indexdb[type].get(args.keyword);
538
+ return result ? { type, id: args.keyword } : null;
539
+ };
540
+
541
+ const entitiesByDid = ['account', 'asset', 'delegation', 'factory', 'token', 'stake', 'rollup'];
542
+ if (isValidDid(args.keyword)) {
543
+ const results = await Promise.all(entitiesByDid.map(doSearch));
544
+ return { results: results.filter(Boolean) };
545
+ }
546
+
547
+ const entitiesByHash = ['tx', 'rollupBlock'];
548
+ if (states.Joi.hashRegexp.test(args.keyword)) {
549
+ const results = await Promise.all(entitiesByHash.map(doSearch));
550
+ return { results: results.filter(Boolean) };
551
+ }
552
+
553
+ return { results: [] };
539
554
  }
540
555
 
541
556
  listBlocks() {
@@ -726,7 +741,7 @@ module.exports = class OCAPResolver {
726
741
  return onRead(state);
727
742
  }
728
743
 
729
- async _doSearch(fn, args, listKey, dataKey, ctx) {
744
+ async _doPaginatedSearch(fn, args, listKey, dataKey, ctx) {
730
745
  const result = await this.indexdb[fn](args);
731
746
  const { [listKey]: items = [], ...rest } = result;
732
747
  let data = items;
@@ -822,14 +837,17 @@ module.exports = class OCAPResolver {
822
837
  tokens.push(...tx.tx.itxJson.receiver.tokens);
823
838
  } else if (typeUrl === 'TransferV2Tx') {
824
839
  tokens.push(...tx.tx.itxJson.tokens);
825
- } else if (['TransferV3Tx', 'AcquireAssetV3Tx'].includes(typeUrl)) {
840
+ } else if (['TransferV3Tx', 'AcquireAssetV3Tx', 'StakeTx'].includes(typeUrl)) {
841
+ tokens.push(
842
+ ...uniqBy(
843
+ tx.tx.itxJson.inputs.reduce((acc, x) => acc.concat(x.tokens), []),
844
+ 'address'
845
+ ).filter((x) => x.address)
846
+ );
847
+ } else if (['RevokeStakeTx'].includes(typeUrl)) {
826
848
  tokens.push(
827
849
  ...uniqBy(
828
- tx.tx.itxJson.inputs.reduce((acc, x) => {
829
- // eslint-disable-next-line no-param-reassign
830
- acc = acc.concat(x.tokens);
831
- return acc;
832
- }, []),
850
+ tx.tx.itxJson.outputs.reduce((acc, x) => acc.concat(x.tokens), []),
833
851
  'address'
834
852
  ).filter((x) => x.address)
835
853
  );
@@ -844,69 +862,28 @@ module.exports = class OCAPResolver {
844
862
  return [];
845
863
  }
846
864
 
847
- fixReceiptTokens(tx, ctx) {
848
- if (get(ctx, 'featureSwitch.consistentTokens')) {
849
- // Auto populate initial token address in tx receipts
850
- tx.receipts.forEach((receipt) =>
851
- receipt.changes.forEach((x) => {
852
- if (x.target === '') {
853
- x.target = this.tokenItx.address;
854
- }
855
- })
856
- );
857
- } else {
858
- // TODO: only exist for backwards compatibility
859
- tx.receipts.forEach((receipt) =>
860
- receipt.changes.forEach((x) => {
861
- if (x.target === this.tokenItx.address) {
862
- x.target = '';
863
- }
864
- })
865
- );
866
- }
865
+ fixReceiptTokens(tx) {
866
+ // Auto populate initial token address in tx receipts
867
+ tx.receipts.forEach((receipt) =>
868
+ receipt.changes.forEach((x) => {
869
+ if (x.target === '') {
870
+ x.target = this.tokenItx.address;
871
+ }
872
+ })
873
+ );
867
874
 
868
875
  return tx;
869
876
  }
870
877
 
871
- fixTokenSymbols(tx, ctx) {
878
+ fixTokenSymbols(tx) {
872
879
  const index = tx.tokenSymbols.findIndex((x) => x.address === this.tokenItx.address);
873
- if (get(ctx, 'featureSwitch.consistentTokens')) {
874
- if (index === -1) {
875
- tx.tokenSymbols.push(extractTokenMeta(this.tokenItx.address, [this.tokenItx]));
876
- }
877
- } else if (index > -1) {
878
- tx.tokenSymbols.splice(index, 1);
880
+ if (index === -1) {
881
+ tx.tokenSymbols.push(extractTokenMeta(this.tokenItx.address, [this.tokenItx]));
879
882
  }
880
883
 
881
884
  return tx;
882
885
  }
883
886
 
884
- // TODO: only exist for backwards compatibility
885
- restoreStateBalance(state, ctx) {
886
- if (!state || !state.tokens) {
887
- return state;
888
- }
889
-
890
- if (get(ctx, 'featureSwitch.consistentTokens')) {
891
- return state;
892
- }
893
-
894
- // For indexdb
895
- if (Array.isArray(state.tokens)) {
896
- const index = state.tokens.findIndex((x) => x.address === this.tokenItx.address);
897
- if (index > -1) {
898
- state.balance = state.tokens[index].value || state.tokens[index].balance;
899
- state.tokens.splice(index, 1);
900
- }
901
- } else if (state.tokens[this.tokenItx.address]) {
902
- // For statedb
903
- state.balance = state.tokens[this.tokenItx.address];
904
- delete state.tokens[this.tokenItx.address];
905
- }
906
-
907
- return state;
908
- }
909
-
910
887
  enrichIndexContext(ctx) {
911
888
  if (Array.isArray(ctx.tokenStates)) {
912
889
  ctx.tokenStates.push(this.tokenItx);
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.13.16",
6
+ "version": "1.13.17",
7
7
  "description": "GraphQL resolver built upon ocap statedb and GQL layer",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -22,16 +22,16 @@
22
22
  "jest": "^26.6.3"
23
23
  },
24
24
  "dependencies": {
25
- "@arcblock/did": "1.13.16",
26
- "@ocap/config": "1.13.16",
27
- "@ocap/indexdb": "1.13.16",
28
- "@ocap/mcrypto": "1.13.16",
29
- "@ocap/message": "1.13.16",
30
- "@ocap/state": "1.13.16",
31
- "@ocap/tx-protocols": "1.13.16",
32
- "@ocap/util": "1.13.16",
25
+ "@arcblock/did": "1.13.17",
26
+ "@ocap/config": "1.13.17",
27
+ "@ocap/indexdb": "1.13.17",
28
+ "@ocap/mcrypto": "1.13.17",
29
+ "@ocap/message": "1.13.17",
30
+ "@ocap/state": "1.13.17",
31
+ "@ocap/tx-protocols": "1.13.17",
32
+ "@ocap/util": "1.13.17",
33
33
  "debug": "^4.3.2",
34
34
  "lodash": "^4.17.21"
35
35
  },
36
- "gitHead": "7b5bdaf7b0404fa5d59c4160ab986e2dc6c565a3"
36
+ "gitHead": "8f5e7a52b90cbe52dda7182ad1780771b03442e7"
37
37
  }