@subwallet/extension-base 1.0.2-2 → 1.0.2-3

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 (134) hide show
  1. package/background/KoniTypes.d.ts +46 -38
  2. package/background/KoniTypes.js +8 -8
  3. package/background/errors/TransactionError.js +25 -1
  4. package/background/types.d.ts +10 -5
  5. package/cjs/background/KoniTypes.js +8 -8
  6. package/cjs/background/errors/TransactionError.js +24 -0
  7. package/cjs/constants/index.js +8 -26
  8. package/cjs/koni/api/dotsama/balance.js +49 -224
  9. package/cjs/koni/api/dotsama/transfer.js +34 -39
  10. package/cjs/koni/api/nft/acala_nft/index.js +7 -7
  11. package/cjs/koni/api/nft/bit.country/index.js +7 -6
  12. package/cjs/koni/api/nft/evm_nft/index.js +8 -3
  13. package/cjs/koni/api/nft/index.js +3 -6
  14. package/cjs/koni/api/nft/karura_nft/index.js +7 -6
  15. package/cjs/koni/api/nft/rmrk_nft/index.js +11 -1
  16. package/cjs/koni/api/nft/statemine_nft/index.js +7 -6
  17. package/cjs/koni/api/nft/unique_nft/index.js +5 -1
  18. package/cjs/koni/api/nft/wasm_nft/index.js +170 -111
  19. package/cjs/koni/api/nft/wasm_nft/utils.js +11 -7
  20. package/cjs/koni/api/staking/bonding/amplitude.js +13 -9
  21. package/cjs/koni/api/staking/bonding/astar.js +15 -13
  22. package/cjs/koni/api/staking/bonding/index.js +22 -10
  23. package/cjs/koni/api/staking/bonding/paraChain.js +85 -2
  24. package/cjs/koni/api/staking/bonding/relayChain.js +122 -16
  25. package/cjs/koni/api/staking/bonding/utils.js +27 -8
  26. package/cjs/koni/api/tokens/wasm/index.js +5 -4
  27. package/cjs/koni/api/tokens/wasm/utils.js +63 -0
  28. package/cjs/koni/api/xcm/polkadotXcm.js +1 -1
  29. package/cjs/koni/api/xcm/utils.js +18 -13
  30. package/cjs/koni/api/xcm/xTokens.js +1 -1
  31. package/cjs/koni/api/xcm/xcmPallet.js +9 -6
  32. package/cjs/koni/background/cron.js +171 -61
  33. package/cjs/koni/background/handlers/Extension.js +382 -207
  34. package/cjs/koni/background/handlers/State.js +41 -33
  35. package/cjs/koni/background/handlers/Tabs.js +50 -17
  36. package/cjs/koni/background/subscription.js +32 -29
  37. package/cjs/packageInfo.js +1 -1
  38. package/cjs/services/chain-service/handler/SubstrateChainHandler.js +13 -8
  39. package/cjs/services/chain-service/index.js +59 -45
  40. package/cjs/services/event-service/index.js +5 -1
  41. package/cjs/services/event-service/types.js +11 -1
  42. package/cjs/services/history-service/index.js +19 -13
  43. package/cjs/services/history-service/subsquid-multi-chain-history.js +13 -10
  44. package/cjs/services/keyring-service/index.js +11 -13
  45. package/cjs/services/price-service/coingecko.js +0 -1
  46. package/cjs/services/price-service/index.js +2 -3
  47. package/cjs/services/request-service/handler/AuthRequestHandler.js +13 -7
  48. package/cjs/services/request-service/handler/EvmRequestHandler.js +8 -12
  49. package/cjs/services/request-service/index.js +14 -5
  50. package/cjs/services/storage-service/DatabaseService.js +56 -34
  51. package/cjs/services/storage-service/db-stores/Nft.js +7 -15
  52. package/cjs/services/transaction-service/event-parser/index.js +20 -48
  53. package/cjs/services/transaction-service/index.js +26 -15
  54. package/cjs/utils/address.js +10 -1
  55. package/cjs/utils/index.js +9 -15
  56. package/constants/index.d.ts +7 -13
  57. package/constants/index.js +7 -13
  58. package/koni/api/dotsama/balance.d.ts +0 -1
  59. package/koni/api/dotsama/balance.js +22 -197
  60. package/koni/api/dotsama/transfer.js +11 -16
  61. package/koni/api/nft/acala_nft/index.js +7 -7
  62. package/koni/api/nft/bit.country/index.js +7 -6
  63. package/koni/api/nft/evm_nft/index.js +7 -3
  64. package/koni/api/nft/index.d.ts +1 -2
  65. package/koni/api/nft/index.js +3 -6
  66. package/koni/api/nft/karura_nft/index.js +7 -6
  67. package/koni/api/nft/nft.d.ts +1 -0
  68. package/koni/api/nft/rmrk_nft/index.js +11 -1
  69. package/koni/api/nft/statemine_nft/index.js +7 -6
  70. package/koni/api/nft/unique_nft/index.js +5 -1
  71. package/koni/api/nft/wasm_nft/index.d.ts +0 -2
  72. package/koni/api/nft/wasm_nft/index.js +168 -109
  73. package/koni/api/nft/wasm_nft/utils.d.ts +7 -5
  74. package/koni/api/nft/wasm_nft/utils.js +7 -5
  75. package/koni/api/staking/bonding/amplitude.d.ts +0 -1
  76. package/koni/api/staking/bonding/amplitude.js +15 -10
  77. package/koni/api/staking/bonding/astar.js +8 -6
  78. package/koni/api/staking/bonding/index.d.ts +4 -1
  79. package/koni/api/staking/bonding/index.js +23 -13
  80. package/koni/api/staking/bonding/paraChain.d.ts +3 -0
  81. package/koni/api/staking/bonding/paraChain.js +86 -5
  82. package/koni/api/staking/bonding/relayChain.d.ts +5 -1
  83. package/koni/api/staking/bonding/relayChain.js +121 -18
  84. package/koni/api/staking/bonding/utils.d.ts +3 -2
  85. package/koni/api/staking/bonding/utils.js +27 -9
  86. package/koni/api/tokens/wasm/index.js +5 -4
  87. package/koni/api/tokens/wasm/utils.d.ts +6 -0
  88. package/koni/api/tokens/wasm/utils.js +54 -0
  89. package/koni/api/xcm/polkadotXcm.js +2 -2
  90. package/koni/api/xcm/utils.d.ts +5 -6
  91. package/koni/api/xcm/utils.js +15 -10
  92. package/koni/api/xcm/xTokens.js +2 -2
  93. package/koni/api/xcm/xcmPallet.js +10 -9
  94. package/koni/background/cron.d.ts +6 -1
  95. package/koni/background/cron.js +172 -62
  96. package/koni/background/handlers/Extension.d.ts +8 -3
  97. package/koni/background/handlers/Extension.js +297 -126
  98. package/koni/background/handlers/State.d.ts +5 -6
  99. package/koni/background/handlers/State.js +43 -33
  100. package/koni/background/handlers/Tabs.js +50 -17
  101. package/koni/background/subscription.js +31 -30
  102. package/package.json +18 -13
  103. package/packageInfo.js +1 -1
  104. package/services/chain-service/handler/SubstrateChainHandler.js +14 -9
  105. package/services/chain-service/helper/psp22_abi.json +1041 -881
  106. package/services/chain-service/helper/psp34_abi.json +2963 -1807
  107. package/services/chain-service/index.d.ts +3 -2
  108. package/services/chain-service/index.js +53 -40
  109. package/services/chain-service/types.d.ts +1 -0
  110. package/services/event-service/index.js +5 -1
  111. package/services/event-service/types.d.ts +5 -9
  112. package/services/event-service/types.js +4 -1
  113. package/services/history-service/index.d.ts +4 -2
  114. package/services/history-service/index.js +19 -13
  115. package/services/history-service/subsquid-multi-chain-history.js +16 -12
  116. package/services/keyring-service/index.d.ts +4 -2
  117. package/services/keyring-service/index.js +11 -13
  118. package/services/price-service/coingecko.js +0 -1
  119. package/services/price-service/index.js +2 -3
  120. package/services/request-service/handler/AuthRequestHandler.d.ts +3 -1
  121. package/services/request-service/handler/AuthRequestHandler.js +13 -7
  122. package/services/request-service/handler/EvmRequestHandler.js +8 -12
  123. package/services/request-service/index.d.ts +3 -1
  124. package/services/request-service/index.js +14 -5
  125. package/services/storage-service/DatabaseService.d.ts +1 -0
  126. package/services/storage-service/DatabaseService.js +56 -34
  127. package/services/storage-service/db-stores/Nft.d.ts +2 -2
  128. package/services/storage-service/db-stores/Nft.js +7 -14
  129. package/services/transaction-service/event-parser/index.js +21 -49
  130. package/services/transaction-service/index.js +26 -15
  131. package/utils/address.d.ts +3 -0
  132. package/utils/address.js +8 -1
  133. package/utils/index.d.ts +2 -2
  134. package/utils/index.js +7 -13
@@ -19,7 +19,6 @@ export declare class ChainService {
19
19
  private store;
20
20
  private assetSettingSubject;
21
21
  private logger;
22
- private refreshChainStateTimeout;
23
22
  constructor(dbService: DatabaseService, eventService: EventService);
24
23
  getXcmRefMap(): Record<string, _AssetRef>;
25
24
  getEvmApi(slug: string): _EvmApi;
@@ -92,8 +91,10 @@ export declare class ChainService {
92
91
  refreshEvmApi(slug: string): void;
93
92
  stopAllChainApis(): Promise<void[]>;
94
93
  resumeAllChainApis(): Promise<void[]>;
94
+ private refreshChainStateTimeout;
95
+ private refreshChainStateTimes;
95
96
  private refreshChainStateInterval;
96
- private updateApiMapStatus;
97
+ updateApiMapStatus(): Promise<void>;
97
98
  initAssetSettings(): Promise<void>;
98
99
  setAssetSettings(assetSettings: Record<string, AssetSetting>, emitEvent?: boolean): void;
99
100
  getStoreAssetSettings(): Promise<Record<string, AssetSetting>>;
@@ -3,7 +3,6 @@
3
3
 
4
4
  import { AssetRefMap, ChainAssetMap, ChainInfoMap, MultiChainAssetMap } from '@subwallet/chain-list';
5
5
  import { _AssetRefPath, _AssetType, _ChainStatus, _SubstrateChainType } from '@subwallet/chain-list/types';
6
- import { CRON_GET_API_MAP_STATUS } from '@subwallet/extension-base/constants';
7
6
  import { _DEFAULT_ACTIVE_CHAINS } from '@subwallet/extension-base/services/chain-service/constants';
8
7
  import { EvmChainHandler } from '@subwallet/extension-base/services/chain-service/handler/EvmChainHandler';
9
8
  import { SubstrateChainHandler } from '@subwallet/extension-base/services/chain-service/handler/SubstrateChainHandler';
@@ -44,7 +43,7 @@ export class ChainService {
44
43
  this.multiChainAssetMapSubject.next(MultiChainAssetMap);
45
44
  this.xcmRefMapSubject.next(this.getXcmRefMap());
46
45
  this.logger = createLogger('chain-service');
47
- this.refreshChainStateInterval(3000);
46
+ this.refreshChainStateInterval(3000, 6);
48
47
  }
49
48
 
50
49
  // Getter
@@ -277,7 +276,7 @@ export class ChainService {
277
276
  this.dbService.removeFromChainStore([slug]).catch(console.error);
278
277
  this.updateChainSubscription();
279
278
  this.lockChainInfoMap = false;
280
- this.eventService.emit('chain.remove', slug);
279
+ this.eventService.emit('chain.updateState', slug);
281
280
  return true;
282
281
  }
283
282
  resetChainInfoMap(excludedChains) {
@@ -343,7 +342,7 @@ export class ChainService {
343
342
  this.dbService.removeFromAssetStore(targetAssets).catch(e => this.logger.error(e));
344
343
  this.assetRegistrySubject.next(assetRegistry);
345
344
  targetAssets.forEach(assetSlug => {
346
- this.eventService.emit('asset.remove', assetSlug);
345
+ this.eventService.emit('asset.updateState', assetSlug);
347
346
  });
348
347
  }
349
348
 
@@ -407,14 +406,14 @@ export class ChainService {
407
406
  this.lockChainInfoMap = true;
408
407
  chainStateMap[chainSlug].active = true;
409
408
  this.initApiForChain(chainInfo);
410
- this.refreshChainStateInterval(1000);
409
+ this.refreshChainStateInterval(3000, 6);
411
410
  this.dbService.updateChainStore({
412
411
  ...chainInfo,
413
412
  active: true,
414
413
  currentProvider: chainStateMap[chainSlug].currentProvider
415
414
  }).catch(console.error);
416
415
  this.lockChainInfoMap = false;
417
- this.eventService.emit('chain.enable', chainSlug);
416
+ this.eventService.emit('chain.updateState', chainSlug);
418
417
  return true;
419
418
  }
420
419
  enableChain(chainSlug) {
@@ -449,7 +448,7 @@ export class ChainService {
449
448
  }).catch(console.error);
450
449
  this.updateChainStateMapSubscription();
451
450
  this.lockChainInfoMap = false;
452
- this.eventService.emit('chain.disable', chainSlug);
451
+ this.eventService.emit('chain.updateState', chainSlug);
453
452
  return true;
454
453
  }
455
454
  checkExistedPredefinedChain(genesisHash, evmChainId) {
@@ -672,7 +671,7 @@ export class ChainService {
672
671
  active: targetChainState.active,
673
672
  currentProvider: targetChainState.currentProvider
674
673
  }).then(() => {
675
- this.eventService.emit('chain.update', chainSlug);
674
+ this.eventService.emit('chain.updateState', chainSlug);
676
675
  }).catch(e => this.logger.error(e));
677
676
  }
678
677
  insertChain(params) {
@@ -961,21 +960,23 @@ export class ChainService {
961
960
  }
962
961
  async validateCustomToken(data) {
963
962
  const assetRegistry = this.getSmartContractTokens();
964
- let isExist = false;
963
+ let existedToken;
965
964
  for (const token of Object.values(assetRegistry)) {
966
965
  var _token$metadata2;
967
966
  const contractAddress = token === null || token === void 0 ? void 0 : (_token$metadata2 = token.metadata) === null || _token$metadata2 === void 0 ? void 0 : _token$metadata2.contractAddress;
968
967
  if (_isEqualContractAddress(contractAddress, data.contractAddress) && token.assetType === data.type && token.originChain === data.originChain) {
969
- isExist = true;
968
+ existedToken = token;
970
969
  break;
971
970
  }
972
971
  }
973
- if (isExist) {
972
+ if (existedToken) {
973
+ var _existedToken;
974
974
  return {
975
- decimals: -1,
976
- name: '',
977
- symbol: '',
978
- isExist,
975
+ decimals: existedToken.decimals || 0,
976
+ name: existedToken.name,
977
+ symbol: existedToken.symbol,
978
+ isExist: !!existedToken,
979
+ existedSlug: (_existedToken = existedToken) === null || _existedToken === void 0 ? void 0 : _existedToken.slug,
979
980
  contractError: false
980
981
  };
981
982
  }
@@ -989,7 +990,7 @@ export class ChainService {
989
990
  name,
990
991
  decimals,
991
992
  symbol,
992
- isExist,
993
+ isExist: !!existedToken,
993
994
  contractError
994
995
  };
995
996
  }
@@ -1039,19 +1040,26 @@ export class ChainService {
1039
1040
 
1040
1041
  return this.substrateChainHandler.resumeAllApis();
1041
1042
  }
1042
-
1043
- // Interval update api status
1044
- refreshChainStateInterval(delay = 0) {
1043
+ refreshChainStateTimeout = undefined;
1044
+ refreshChainStateTimes = 0;
1045
+ refreshChainStateInterval(delay = 0, times) {
1045
1046
  clearTimeout(this.refreshChainStateTimeout);
1046
1047
  setTimeout(() => {
1047
- this.updateApiMapStatus();
1048
+ if (times) {
1049
+ this.refreshChainStateTimes = times;
1050
+ }
1051
+ this.refreshChainStateTimes -= 1;
1052
+ if (this.refreshChainStateTimes < 0) {
1053
+ return;
1054
+ }
1055
+ this.updateApiMapStatus().catch(console.error);
1048
1056
  this.refreshChainStateTimeout = setTimeout(() => {
1049
- this.updateApiMapStatus();
1050
- this.refreshChainStateInterval();
1051
- }, CRON_GET_API_MAP_STATUS);
1057
+ this.updateApiMapStatus().catch(console.error);
1058
+ this.refreshChainStateInterval(0);
1059
+ }, 3000);
1052
1060
  }, delay);
1053
1061
  }
1054
- updateApiMapStatus() {
1062
+ async updateApiMapStatus() {
1055
1063
  const substrateApiMap = this.getSubstrateApiMap();
1056
1064
  const evmApiMap = this.getEvmApiMap();
1057
1065
  const chainStateMap = this.getChainStateMap();
@@ -1062,24 +1070,30 @@ export class ChainService {
1062
1070
  update = true;
1063
1071
  }
1064
1072
  }
1065
- Object.entries(chainStateMap).forEach(([chain, chainState]) => {
1066
- if (chainState.active) {
1067
- if (substrateApiMap[chain]) {
1068
- const api = substrateApiMap[chain];
1069
- if (api) {
1070
- updateState(chainState, _ChainConnectionStatus.CONNECTED);
1071
- return;
1072
- }
1073
- } else if (evmApiMap[chain]) {
1074
- const api = evmApiMap[chain];
1075
- if (api) {
1076
- updateState(chainState, _ChainConnectionStatus.CONNECTED);
1077
- return;
1073
+ const promiseList = Object.entries(chainStateMap).map(async ([chain, chainState]) => {
1074
+ try {
1075
+ if (chainState.active) {
1076
+ if (substrateApiMap[chain]) {
1077
+ const api = substrateApiMap[chain];
1078
+ if (api.isApiConnected) {
1079
+ updateState(chainState, _ChainConnectionStatus.CONNECTED);
1080
+ return;
1081
+ }
1082
+ } else if (evmApiMap[chain]) {
1083
+ var _api$api;
1084
+ const api = evmApiMap[chain];
1085
+ if (await (api === null || api === void 0 ? void 0 : (_api$api = api.api) === null || _api$api === void 0 ? void 0 : _api$api.eth.net.isListening())) {
1086
+ updateState(chainState, _ChainConnectionStatus.CONNECTED);
1087
+ return;
1088
+ }
1078
1089
  }
1079
1090
  }
1091
+ updateState(chainState, _ChainConnectionStatus.DISCONNECTED);
1092
+ } catch (e) {
1093
+ updateState(chainState, _ChainConnectionStatus.DISCONNECTED);
1080
1094
  }
1081
- updateState(chainState, _ChainConnectionStatus.DISCONNECTED);
1082
1095
  });
1096
+ await Promise.all(promiseList);
1083
1097
  if (update) {
1084
1098
  console.log('Update chain connection state');
1085
1099
  this.chainStateMapSubject.next(chainStateMap);
@@ -1105,7 +1119,6 @@ export class ChainService {
1105
1119
  this.setAssetSettings(assetSettings, false);
1106
1120
  }
1107
1121
  this.eventService.emit('asset.ready', true);
1108
- console.log('Done init asset settings');
1109
1122
  }
1110
1123
  setAssetSettings(assetSettings, emitEvent = true) {
1111
1124
  const updateAssets = [];
@@ -1119,7 +1132,7 @@ export class ChainService {
1119
1132
  }
1120
1133
  this.assetSettingSubject.next(assetSettings);
1121
1134
  updateAssets.forEach(slug => {
1122
- this.eventService.emit(assetSettings[slug].visible ? 'asset.enable' : 'asset.disable', slug);
1135
+ this.eventService.emit('asset.updateState', slug);
1123
1136
  });
1124
1137
  this.store.set('AssetSetting', assetSettings);
1125
1138
  }
@@ -104,6 +104,7 @@ export interface _SmartContractTokenInfo {
104
104
  }
105
105
  export interface _ValidateCustomAssetResponse extends _SmartContractTokenInfo {
106
106
  isExist: boolean;
107
+ existedSlug?: string;
107
108
  }
108
109
  export declare const _FUNGIBLE_CONTRACT_STANDARDS: _AssetType[];
109
110
  export declare const _NFT_CONTRACT_STANDARDS: _AssetType[];
@@ -34,7 +34,11 @@ export class EventService extends EventEmitter {
34
34
  }, this.lazyTime);
35
35
  }
36
36
  emitLazy() {
37
- this.lazyEmitter.emit('lazy', this.pendingEvents, this.pendingEvents.map(e => e.type));
37
+ try {
38
+ this.lazyEmitter.emit('lazy', this.pendingEvents, this.pendingEvents.map(e => e.type));
39
+ } catch (e) {
40
+ console.error('Get error in some listener of lazy event', e);
41
+ }
38
42
  this.pendingEvents = [];
39
43
  this.timeoutId = null;
40
44
  }
@@ -9,20 +9,16 @@ export interface EventRegistry {
9
9
  'account.remove': [string];
10
10
  'chain.ready': [boolean];
11
11
  'chain.add': [string];
12
- 'chain.update': [string];
13
- 'chain.disable': [string];
14
- 'chain.enable': [string];
15
- 'chain.remove': [string];
12
+ 'chain.updateState': [string];
16
13
  'asset.ready': [boolean];
17
- 'asset.add': [string];
18
- 'asset.update': [string];
19
- 'asset.enable': [string];
20
- 'asset.disable': [string];
21
- 'asset.remove': [string];
14
+ 'asset.updateState': [string];
22
15
  'transaction.done': [SWTransaction];
23
16
  'transaction.failed': [SWTransaction | undefined];
17
+ 'transaction.submitStaking': [string];
18
+ 'transaction.transferNft': [SWTransaction | undefined];
24
19
  }
25
20
  export declare type EventType = keyof EventRegistry;
21
+ export declare const COMMON_RELOAD_EVENTS: EventType[];
26
22
  export interface EventItem<T extends EventType> {
27
23
  type: T;
28
24
  data: EventRegistry[T];
@@ -1 +1,4 @@
1
- export {};
1
+ // Copyright 2019-2022 @subwallet/extension-base
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export const COMMON_RELOAD_EVENTS = ['account.updateCurrent', 'asset.updateState', 'account.add', 'chain.updateState', 'account.remove', 'chain.add'];
@@ -1,14 +1,16 @@
1
1
  import { TransactionHistoryItem } from '@subwallet/extension-base/background/KoniTypes';
2
2
  import { ChainService } from '@subwallet/extension-base/services/chain-service';
3
3
  import { EventService } from '@subwallet/extension-base/services/event-service';
4
+ import { KeyringService } from '@subwallet/extension-base/services/keyring-service';
4
5
  import DatabaseService from '@subwallet/extension-base/services/storage-service/DatabaseService';
5
6
  import { BehaviorSubject } from 'rxjs';
6
7
  export declare class HistoryService {
7
8
  private dbService;
8
9
  private chainService;
9
10
  private eventService;
11
+ private keyringService;
10
12
  private historySubject;
11
- constructor(dbService: DatabaseService, chainService: ChainService, eventService: EventService);
13
+ constructor(dbService: DatabaseService, chainService: ChainService, eventService: EventService, keyringService: KeyringService);
12
14
  private fetchPromise;
13
15
  private nextFetch;
14
16
  private fetchAndLoadHistories;
@@ -17,8 +19,8 @@ export declare class HistoryService {
17
19
  refreshHistoryInterval(): void;
18
20
  getHistories(): Promise<TransactionHistoryItem<import("@subwallet/extension-base/background/KoniTypes").ExtrinsicType.TRANSFER_BALANCE>[]>;
19
21
  getHistorySubject(): Promise<BehaviorSubject<TransactionHistoryItem<import("@subwallet/extension-base/background/KoniTypes").ExtrinsicType.TRANSFER_BALANCE>[]>>;
20
- insertHistories(historyItems: TransactionHistoryItem[]): Promise<void>;
21
22
  updateHistories(chain: string, extrinsicHash: string, updateData: Partial<TransactionHistoryItem>): Promise<void>;
23
+ insertHistories(historyItems: TransactionHistoryItem[]): Promise<void>;
22
24
  addHistoryItems(historyItems: TransactionHistoryItem[]): Promise<void>;
23
25
  removeHistoryByAddress(address: string): Promise<void>;
24
26
  }
@@ -2,17 +2,16 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
4
  import { CRON_REFRESH_HISTORY_INTERVAL } from '@subwallet/extension-base/constants';
5
- import { quickFormatAddressToCompare } from '@subwallet/extension-base/utils/address';
6
5
  import { keyring } from '@subwallet/ui-keyring';
7
- import { accounts } from '@subwallet/ui-keyring/observable/accounts';
8
6
  import { BehaviorSubject } from 'rxjs';
9
7
  import { fetchMultiChainHistories } from "./subsquid-multi-chain-history.js";
10
8
  export class HistoryService {
11
9
  historySubject = new BehaviorSubject([]);
12
- constructor(dbService, chainService, eventService) {
10
+ constructor(dbService, chainService, eventService, keyringService) {
13
11
  this.dbService = dbService;
14
12
  this.chainService = chainService;
15
13
  this.eventService = eventService;
14
+ this.keyringService = keyringService;
16
15
  // Load history from database
17
16
  this.dbService.getHistories().then(histories => {
18
17
  this.historySubject.next(histories);
@@ -41,7 +40,7 @@ export class HistoryService {
41
40
  const historyRecords = await fetchMultiChainHistories(addresses, chainMap);
42
41
 
43
42
  // Fill additional info
44
- const accountMap = Object.entries(accounts.subject.value).reduce((map, [address, account]) => {
43
+ const accountMap = Object.entries(this.keyringService.accounts).reduce((map, [address, account]) => {
45
44
  map[address.toLowerCase()] = account.json.meta.name || address;
46
45
  return map;
47
46
  }, {});
@@ -50,7 +49,7 @@ export class HistoryService {
50
49
  record.fromName = accountMap[(_record$from = record.from) === null || _record$from === void 0 ? void 0 : _record$from.toLowerCase()];
51
50
  record.toName = accountMap[(_record$to = record.to) === null || _record$to === void 0 ? void 0 : _record$to.toLowerCase()];
52
51
  });
53
- this.dbService.upsertHistory(historyRecords).catch(console.error);
52
+ await this.addHistoryItems(historyRecords);
54
53
  return historyRecords;
55
54
  }
56
55
  async fetchHistories(addresses) {
@@ -75,8 +74,8 @@ export class HistoryService {
75
74
  const addressList = keyring.getAccounts().map(a => a.address);
76
75
  const currentHistories = this.historySubject.value;
77
76
  if (!this.fetchPromise || currentHistories.length === 0) {
78
- const historyRecords = await this.fetchHistories(addressList);
79
- this.historySubject.next(historyRecords);
77
+ await this.fetchHistories(addressList);
78
+ this.historySubject.next(await this.dbService.getHistories());
80
79
  }
81
80
  return this.historySubject.getValue();
82
81
  }
@@ -84,10 +83,6 @@ export class HistoryService {
84
83
  await this.getHistories();
85
84
  return this.historySubject;
86
85
  }
87
- async insertHistories(historyItems) {
88
- await this.dbService.upsertHistory(historyItems);
89
- this.historySubject.next(await this.dbService.getHistories());
90
- }
91
86
  async updateHistories(chain, extrinsicHash, updateData) {
92
87
  const existedRecords = await this.dbService.getHistories({
93
88
  chain,
@@ -101,14 +96,25 @@ export class HistoryService {
101
96
  });
102
97
  await this.addHistoryItems(updatedRecords);
103
98
  }
99
+
100
+ // Insert history without check override origin 'app'
101
+ async insertHistories(historyItems) {
102
+ await this.dbService.upsertHistory(historyItems);
103
+ this.historySubject.next(await this.dbService.getHistories());
104
+ }
105
+
106
+ // Insert history with check override origin 'app'
104
107
  async addHistoryItems(historyItems) {
105
108
  // Prevent override record with original is 'app'
106
109
  const appRecords = this.historySubject.value.filter(item => item.origin === 'app');
107
110
  const excludeKeys = appRecords.map(item => {
108
- return `${item.chain}-${quickFormatAddressToCompare(item.address) || ''}-${item.extrinsicHash}`;
111
+ return `${item.chain}-${item.extrinsicHash}`;
109
112
  });
110
113
  const updateRecords = historyItems.filter(item => {
111
- const key = `${item.chain}-${quickFormatAddressToCompare(item.address) || ''}-${item.extrinsicHash}`;
114
+ const key = `${item.chain}-${item.extrinsicHash}`;
115
+
116
+ // !excludeKeys.includes(key) && console.log('Cancel update', key);
117
+
112
118
  return item.origin === 'app' || !excludeKeys.includes(key);
113
119
  });
114
120
  await this.dbService.upsertHistory(updateRecords);
@@ -57,11 +57,15 @@ function autoFormatAddress(address) {
57
57
  return '';
58
58
  }
59
59
  }
60
- function generateSignature({
61
- r,
62
- s,
63
- v
64
- }) {
60
+ function generateSignature(input) {
61
+ if (!input) {
62
+ return '';
63
+ }
64
+ const {
65
+ r,
66
+ s,
67
+ v
68
+ } = input;
65
69
  const rHex = r.startsWith('0x') ? r.slice(2) : r;
66
70
  const sHex = s.startsWith('0x') ? s.slice(2) : s;
67
71
  const vHex = parseInt(v).toString(16);
@@ -107,7 +111,7 @@ export function parseSubsquidTransactionData(address, type, historyItem, chainIn
107
111
  const transaction = data.call.data.args.transaction.value;
108
112
  to = autoFormatAddress(parsedArgs.to);
109
113
  from = autoFormatAddress(parsedArgs.from);
110
- extrinsicHash = parsedArgs.transactionHash;
114
+ extrinsicHash = parsedArgs.transactionHash || extrinsic.hash;
111
115
  amount = transaction.value || '0';
112
116
  fee = (parseInt(transaction.gasPrice) * parseInt(transaction.gasLimit)).toString();
113
117
  signature = generateSignature(transaction.signature);
@@ -243,14 +247,14 @@ export async function fetchMultiChainHistories(addresses, chainMap, maxPage = 25
243
247
  console.warn(`Not found chain info for chain id: ${chainId}`);
244
248
  return;
245
249
  }
246
- try {
247
- usedAddresses.forEach(address => {
250
+ usedAddresses.forEach(address => {
251
+ try {
248
252
  const transactionData = parseSubsquidTransactionData(address, name, historyItem, chainInfo, parseData(args), parseData(_data));
249
253
  histories.push(transactionData);
250
- });
251
- } catch (e) {
252
- console.warn('Parse transaction data failed', e);
253
- }
254
+ } catch (e) {
255
+ console.warn('Parse transaction data failed', address, e);
256
+ }
257
+ });
254
258
  });
255
259
  return histories;
256
260
  }
@@ -3,17 +3,19 @@ import { EventService } from '@subwallet/extension-base/services/event-service';
3
3
  import { SubjectInfo } from '@subwallet/ui-keyring/observable/types';
4
4
  import { BehaviorSubject } from 'rxjs';
5
5
  export declare class KeyringService {
6
+ private eventService;
6
7
  private readonly currentAccountStore;
7
8
  readonly currentAccountSubject: BehaviorSubject<CurrentAccountInfo>;
8
- readonly accountsSubject: BehaviorSubject<SubjectInfo>;
9
+ readonly addressesSubject: BehaviorSubject<SubjectInfo>;
10
+ readonly accountSubject: BehaviorSubject<SubjectInfo>;
9
11
  private beforeAccount;
10
12
  readonly keyringStateSubject: BehaviorSubject<KeyringState>;
11
- private eventService;
12
13
  constructor(eventService: EventService);
13
14
  private subscribeAccounts;
14
15
  get keyringState(): KeyringState;
15
16
  updateKeyringState(isReady?: boolean): void;
16
17
  get accounts(): SubjectInfo;
18
+ get addresses(): SubjectInfo;
17
19
  get currentAccount(): CurrentAccountInfo;
18
20
  setCurrentAccount(currentAccountData: CurrentAccountInfo): void;
19
21
  }
@@ -10,8 +10,9 @@ export class KeyringService {
10
10
  address: '',
11
11
  currentGenesisHash: null
12
12
  });
13
- accountsSubject = keyring.accounts.subject;
14
- beforeAccount = keyring.accounts.subject.value;
13
+ addressesSubject = keyring.addresses.subject;
14
+ accountSubject = keyring.accounts.subject;
15
+ beforeAccount = this.accountSubject.value;
15
16
  keyringStateSubject = new BehaviorSubject({
16
17
  isReady: false,
17
18
  hasMasterPassword: false,
@@ -20,23 +21,17 @@ export class KeyringService {
20
21
  constructor(eventService) {
21
22
  this.eventService = eventService;
22
23
  this.currentAccountStore.get('CurrentAccountInfo', rs => {
23
- this.currentAccountSubject.next(rs);
24
+ rs && this.currentAccountSubject.next(rs);
24
25
  });
25
26
  this.subscribeAccounts().catch(console.error);
26
27
  }
27
28
  async subscribeAccounts() {
28
29
  // Wait until account ready
29
- await new Promise(resolve => {
30
- const onReady = () => {
31
- this.eventService.off('account.ready', onReady);
32
- resolve(true);
33
- };
34
- this.eventService.on('account.ready', onReady);
35
- });
30
+ await this.eventService.waitAccountReady;
36
31
  this.beforeAccount = {
37
- ...this.accountsSubject.value
32
+ ...this.accountSubject.value
38
33
  };
39
- this.accountsSubject.subscribe(subjectInfo => {
34
+ this.accountSubject.subscribe(subjectInfo => {
40
35
  // Check if accounts changed
41
36
  const beforeAddresses = Object.keys(this.beforeAccount);
42
37
  const afterAddresses = Object.keys(subjectInfo);
@@ -80,7 +75,10 @@ export class KeyringService {
80
75
  });
81
76
  }
82
77
  get accounts() {
83
- return this.accountsSubject.value;
78
+ return this.accountSubject.value;
79
+ }
80
+ get addresses() {
81
+ return this.addressesSubject.value;
84
82
  }
85
83
  get currentAccount() {
86
84
  return this.currentAccountSubject.value;
@@ -16,7 +16,6 @@ export const getTokenPrice = async (priceIds, currency = 'usd') => {
16
16
  }
17
17
  }
18
18
  if (useBackupApi || ((_res = res) === null || _res === void 0 ? void 0 : _res.status) !== 200) {
19
- console.log('Use backup api for price');
20
19
  useBackupApi = true;
21
20
  res = await axios.get(`https://chain-data.subwallet.app/api/price/get?ids=${idStr}`);
22
21
  }
@@ -31,8 +31,8 @@ export class PriceService {
31
31
  };
32
32
  this.eventService.waitAssetReady.then(() => {
33
33
  this.refreshPriceData();
34
- this.eventService.on('asset.enable', eventHandler);
35
- this.eventService.on('asset.update', eventHandler);
34
+ this.eventService.on('asset.updateState', eventHandler);
35
+ this.eventService.on('asset.updateState', eventHandler);
36
36
  }).catch(console.error);
37
37
  }
38
38
  async getPrice() {
@@ -62,7 +62,6 @@ export class PriceService {
62
62
  ready: true
63
63
  });
64
64
  this.dbService.updatePriceStore(rs).catch(console.error);
65
- console.log('Get Token Price From CoinGecko');
66
65
  }).catch(console.error);
67
66
  this.refreshTimeout = setTimeout(this.refreshPriceData.bind(this), CRON_REFRESH_PRICE_INTERVAL);
68
67
  }
@@ -2,17 +2,19 @@ import { _ChainInfo } from '@subwallet/chain-list/types';
2
2
  import { AuthRequestV2 } from '@subwallet/extension-base/background/KoniTypes';
3
3
  import { AccountAuthType, AuthorizeRequest, RequestAuthorizeTab } from '@subwallet/extension-base/background/types';
4
4
  import { ChainService } from '@subwallet/extension-base/services/chain-service';
5
+ import { KeyringService } from '@subwallet/extension-base/services/keyring-service';
5
6
  import RequestService from '@subwallet/extension-base/services/request-service';
6
7
  import { AuthUrls } from '@subwallet/extension-base/services/request-service/types';
7
8
  import { BehaviorSubject, Subject } from 'rxjs';
8
9
  export default class AuthRequestHandler {
9
10
  #private;
11
+ private keyringService;
10
12
  private readonly authorizeStore;
11
13
  private authorizeCached;
12
14
  private readonly authorizeUrlSubject;
13
15
  private readonly evmChainSubject;
14
16
  readonly authSubjectV2: BehaviorSubject<AuthorizeRequest[]>;
15
- constructor(requestService: RequestService, chainService: ChainService);
17
+ constructor(requestService: RequestService, chainService: ChainService, keyringService: KeyringService);
16
18
  private stripUrl;
17
19
  private getAddressList;
18
20
  get numAuthRequestsV2(): number;
@@ -6,7 +6,6 @@ import { PREDEFINED_CHAIN_DAPP_CHAIN_MAP } from '@subwallet/extension-base/servi
6
6
  import AuthorizeStore from '@subwallet/extension-base/stores/Authorize';
7
7
  import { getDomainFromUrl } from '@subwallet/extension-base/utils';
8
8
  import { getId } from '@subwallet/extension-base/utils/getId';
9
- import { accounts } from '@subwallet/ui-keyring/observable/accounts';
10
9
  import { BehaviorSubject, Subject } from 'rxjs';
11
10
  import { assert } from '@polkadot/util';
12
11
  import { isEthereumAddress } from '@polkadot/util-crypto';
@@ -20,7 +19,8 @@ export default class AuthRequestHandler {
20
19
  authorizeUrlSubject = new Subject();
21
20
  evmChainSubject = new Subject();
22
21
  authSubjectV2 = new BehaviorSubject([]);
23
- constructor(requestService, chainService) {
22
+ constructor(requestService, chainService, keyringService) {
23
+ this.keyringService = keyringService;
24
24
  this.#requestService = requestService;
25
25
  this.#chainService = chainService;
26
26
  }
@@ -30,7 +30,7 @@ export default class AuthRequestHandler {
30
30
  return parts[2];
31
31
  }
32
32
  getAddressList(value = false) {
33
- const addressList = Object.keys(accounts.subject.value);
33
+ const addressList = Object.keys(this.keyringService.accounts);
34
34
  return addressList.reduce((addressList, v) => ({
35
35
  ...addressList,
36
36
  [v]: value
@@ -84,11 +84,11 @@ export default class AuthRequestHandler {
84
84
  const chainInfoMaps = this.#chainService.getChainInfoMap();
85
85
  const chainStateMap = this.#chainService.getChainStateMap();
86
86
  let defaultChain = options.defaultChain;
87
- const needEnableChains = [];
87
+ let needEnableChains = [];
88
88
  if (options.url) {
89
89
  const domain = getDomainFromUrl(options.url);
90
90
  const predefinedSupportChains = PREDEFINED_CHAIN_DAPP_CHAIN_MAP[domain];
91
- if (predefinedSupportChains) {
91
+ if (!defaultChain && predefinedSupportChains) {
92
92
  defaultChain = predefinedSupportChains[0];
93
93
  options.autoActive && needEnableChains.push(...predefinedSupportChains);
94
94
  }
@@ -101,11 +101,17 @@ export default class AuthRequestHandler {
101
101
  return (_chainStateMap$chain$ = chainStateMap[chain.slug]) === null || _chainStateMap$chain$ === void 0 ? void 0 : _chainStateMap$chain$.active;
102
102
  })) || evmChains[0];
103
103
  if (options.autoActive) {
104
- if (!needEnableChains.includes(chainInfo.slug)) {
105
- needEnableChains.push(chainInfo.slug);
104
+ var _chainInfo;
105
+ if (!needEnableChains.includes((_chainInfo = chainInfo) === null || _chainInfo === void 0 ? void 0 : _chainInfo.slug)) {
106
+ var _chainInfo2;
107
+ needEnableChains.push((_chainInfo2 = chainInfo) === null || _chainInfo2 === void 0 ? void 0 : _chainInfo2.slug);
106
108
  }
107
109
  }
108
110
  }
111
+ needEnableChains = needEnableChains.filter(slug => {
112
+ var _chainStateMap$slug;
113
+ return !((_chainStateMap$slug = chainStateMap[slug]) !== null && _chainStateMap$slug !== void 0 && _chainStateMap$slug.active);
114
+ });
109
115
  needEnableChains.length > 0 && this.#chainService.enableChains(needEnableChains);
110
116
  return chainInfo;
111
117
  }