@subwallet/extension-base 1.1.22-0 → 1.1.24-0

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 (58) hide show
  1. package/background/KoniTypes.d.ts +21 -2
  2. package/cjs/koni/api/dotsama/balance.js +49 -0
  3. package/cjs/koni/api/dotsama/crowdloan.js +29 -3
  4. package/cjs/koni/api/dotsama/transfer.js +10 -3
  5. package/cjs/koni/api/staking/bonding/astar.js +9 -5
  6. package/cjs/koni/api/xcm/index.js +6 -1
  7. package/cjs/koni/api/xcm/polkadotXcm.js +1 -3
  8. package/cjs/koni/api/xcm/xTokens.js +1 -1
  9. package/cjs/koni/background/handlers/Extension.js +123 -96
  10. package/cjs/koni/background/handlers/Mobile.js +32 -0
  11. package/cjs/koni/background/handlers/State.js +15 -7
  12. package/cjs/koni/background/subscription.js +1 -1
  13. package/cjs/packageInfo.js +1 -1
  14. package/cjs/services/chain-service/constants.js +5 -2
  15. package/cjs/services/chain-service/index.js +18 -0
  16. package/cjs/services/history-service/helpers/subscan-extrinsic-parser-helper.js +53 -0
  17. package/cjs/services/history-service/index.js +78 -21
  18. package/cjs/services/history-service/subscan-history.js +107 -0
  19. package/cjs/services/storage-service/DatabaseService.js +40 -0
  20. package/cjs/services/subscan-service/index.js +109 -4
  21. package/cjs/services/subscan-service/subscan-chain-map.js +82 -8
  22. package/cjs/utils/index.js +10 -1
  23. package/koni/api/dotsama/balance.js +49 -0
  24. package/koni/api/dotsama/crowdloan.d.ts +2 -2
  25. package/koni/api/dotsama/crowdloan.js +29 -2
  26. package/koni/api/dotsama/transfer.js +10 -3
  27. package/koni/api/staking/bonding/astar.js +9 -5
  28. package/koni/api/xcm/index.js +6 -1
  29. package/koni/api/xcm/polkadotXcm.js +2 -4
  30. package/koni/api/xcm/xTokens.js +1 -1
  31. package/koni/background/handlers/Extension.d.ts +1 -0
  32. package/koni/background/handlers/Extension.js +26 -0
  33. package/koni/background/handlers/Mobile.d.ts +5 -1
  34. package/koni/background/handlers/Mobile.js +31 -0
  35. package/koni/background/handlers/State.d.ts +1 -0
  36. package/koni/background/handlers/State.js +17 -9
  37. package/koni/background/subscription.js +1 -1
  38. package/package.json +17 -6
  39. package/packageInfo.js +1 -1
  40. package/services/chain-service/constants.d.ts +2 -0
  41. package/services/chain-service/constants.js +5 -3
  42. package/services/chain-service/index.d.ts +1 -0
  43. package/services/chain-service/index.js +18 -0
  44. package/services/history-service/helpers/subscan-extrinsic-parser-helper.d.ts +6 -0
  45. package/services/history-service/helpers/subscan-extrinsic-parser-helper.js +44 -0
  46. package/services/history-service/index.d.ts +10 -6
  47. package/services/history-service/index.js +79 -22
  48. package/services/history-service/subscan-history.d.ts +5 -0
  49. package/services/history-service/subscan-history.js +100 -0
  50. package/services/storage-service/DatabaseService.d.ts +3 -0
  51. package/services/storage-service/DatabaseService.js +40 -0
  52. package/services/subscan-service/index.d.ts +10 -2
  53. package/services/subscan-service/index.js +105 -4
  54. package/services/subscan-service/subscan-chain-map.d.ts +9 -3
  55. package/services/subscan-service/subscan-chain-map.js +78 -5
  56. package/services/subscan-service/types.d.ts +146 -0
  57. package/utils/index.d.ts +1 -0
  58. package/utils/index.js +7 -0
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
+ var _promise = require("@subwallet/extension-base/utils/promise");
7
8
  // Copyright 2019-2022 @subwallet/extension-koni authors & contributors
8
9
  // SPDX-License-Identifier: Apache-2.0
9
10
 
@@ -26,6 +27,7 @@ const DEFAULT_SERVICE_MAP = {
26
27
  class Mobile {
27
28
  // @ts-ignore
28
29
 
30
+ restoreHandler = (0, _promise.createPromiseHandler)();
29
31
  constructor(state) {
30
32
  this.state = state;
31
33
  }
@@ -102,6 +104,32 @@ class Mobile {
102
104
  restartSubscriptionServices(services) {
103
105
  console.log('restartSubscriptionServices');
104
106
  }
107
+ async mobileBackup() {
108
+ const indexedDB = await this.state.dbService.exportDB();
109
+ return {
110
+ storage: JSON.stringify(localStorage),
111
+ indexedDB
112
+ };
113
+ }
114
+ async mobileRestore(_ref5) {
115
+ let {
116
+ indexedDB,
117
+ storage
118
+ } = _ref5;
119
+ if (storage) {
120
+ const storageData = JSON.parse(storage);
121
+ for (const key in storageData) {
122
+ localStorage.setItem(key, JSON.stringify(storageData[key]));
123
+ }
124
+ }
125
+ if (indexedDB) {
126
+ await this.state.dbService.importDB(indexedDB);
127
+ }
128
+ this.restoreHandler.resolve();
129
+ }
130
+ waitRestore() {
131
+ return this.restoreHandler.promise;
132
+ }
105
133
 
106
134
  // eslint-disable-next-line @typescript-eslint/require-await
107
135
  async handle(id, type, request, port) {
@@ -130,6 +158,10 @@ class Mobile {
130
158
  return this.stopSubscriptionServices(request);
131
159
  case 'mobile(subscription.restart)':
132
160
  return this.restartSubscriptionServices(request);
161
+ case 'mobile(storage.restore)':
162
+ return this.mobileRestore(request);
163
+ case 'mobile(storage.backup)':
164
+ return this.mobileBackup();
133
165
  default:
134
166
  throw new Error(`Unable to handle message of type ${type}`);
135
167
  }
@@ -91,14 +91,14 @@ class KoniState {
91
91
  this.eventService = new _eventService.EventService();
92
92
  this.dbService = new _DatabaseService.default(this.eventService);
93
93
  this.keyringService = new _keyringService.KeyringService(this.eventService);
94
- this.subscanService = new _subscanService.SubscanService();
95
94
  this.notificationService = new _NotificationService.default();
96
95
  this.chainService = new _chainService.ChainService(this.dbService, this.eventService);
96
+ this.subscanService = new _subscanService.SubscanService(_subscanChainMap.SUBSCAN_API_CHAIN_MAP);
97
97
  this.settingService = new _SettingService.default();
98
98
  this.requestService = new _requestService.default(this.chainService, this.settingService, this.keyringService);
99
99
  this.priceService = new _priceService.PriceService(this.dbService, this.eventService, this.chainService);
100
100
  this.balanceService = new _balanceService.BalanceService(this.chainService);
101
- this.historyService = new _historyService.HistoryService(this.dbService, this.chainService, this.eventService, this.keyringService);
101
+ this.historyService = new _historyService.HistoryService(this.dbService, this.chainService, this.eventService, this.keyringService, this.subscanService);
102
102
  this.transactionService = new _transactionService.default(this.chainService, this.eventService, this.requestService, this.balanceService, this.historyService, this.notificationService, this.dbService);
103
103
  this.walletConnectService = new _walletConnectService.default(this, this.requestService);
104
104
  this.migrationService = new _migrationService.default(this, this.eventService);
@@ -109,7 +109,9 @@ class KoniState {
109
109
  this.logger = (0, _util.logger)('State');
110
110
 
111
111
  // Init state
112
- this.init().catch(console.error);
112
+ if (_utils2.TARGET_ENV !== 'mobile') {
113
+ this.init().catch(console.error);
114
+ }
113
115
  }
114
116
 
115
117
  // Clone from polkadot.js
@@ -206,9 +208,13 @@ class KoniState {
206
208
  });
207
209
  return balanceMap;
208
210
  }
211
+ afterChainServiceInit() {
212
+ this.subscanService.setSubscanChainMap(this.chainService.getSubscanChainMap());
213
+ }
209
214
  async init() {
210
215
  await this.eventService.waitCryptoReady;
211
216
  await this.chainService.init();
217
+ this.afterChainServiceInit();
212
218
  await this.migrationService.run();
213
219
  this.eventService.emit('chain.ready', true);
214
220
  this.onReady();
@@ -1494,7 +1500,7 @@ class KoniState {
1494
1500
  network,
1495
1501
  symbol
1496
1502
  } = _ref11;
1497
- const chain = _subscanChainMap.SUBSCAN_CHAIN_MAP_REVERSE[network];
1503
+ const chain = _subscanChainMap.SUBSCAN_BALANCE_CHAIN_MAP_REVERSE[network];
1498
1504
  const chainInfo = chain ? chainMap[chain] : null;
1499
1505
  const balanceIsEmpty = (!balance || balance === '0') && (!locked || locked === '0') && (!bonded || bonded === '0');
1500
1506
 
@@ -1503,10 +1509,11 @@ class KoniState {
1503
1509
  return;
1504
1510
  }
1505
1511
  const tokenKey = `${chain}-${category === 'native' ? 'NATIVE' : 'LOCAL'}-${symbol.toUpperCase()}`;
1506
- if (assetMap[tokenKey] && !((_currentAssetSettings = currentAssetSettings[tokenKey]) !== null && _currentAssetSettings !== void 0 && _currentAssetSettings.visible)) {
1512
+ const existedKey = Object.keys(assetMap).find(v => v.toLowerCase() === tokenKey.toLowerCase());
1513
+ if (existedKey && !((_currentAssetSettings = currentAssetSettings[existedKey]) !== null && _currentAssetSettings !== void 0 && _currentAssetSettings.visible)) {
1507
1514
  needEnableChains.push(chain);
1508
- needActiveTokens.push(tokenKey);
1509
- currentAssetSettings[tokenKey] = {
1515
+ needActiveTokens.push(existedKey);
1516
+ currentAssetSettings[existedKey] = {
1510
1517
  visible: true
1511
1518
  };
1512
1519
  }
@@ -1578,6 +1585,7 @@ class KoniState {
1578
1585
  this.chainService.resetWallet(resetAll);
1579
1586
  await this.walletConnectService.resetWallet(resetAll);
1580
1587
  await this.chainService.init();
1588
+ this.afterChainServiceInit();
1581
1589
  }
1582
1590
  async enableMantaPay(updateStore, address, password, seedPhrase) {
1583
1591
  var _this$chainService3, _this$chainService3$m, _this$chainService4, _this$chainService4$m, _this$chainService4$m2, _this$chainService11, _this$chainService11$, _this$chainService11$2;
@@ -169,7 +169,7 @@ class KoniSubscription {
169
169
  initCrowdloanSubscription(addresses, substrateApiMap, onlyRunOnFirstTime) {
170
170
  const subscriptionPromise = (0, _crowdloan.subscribeCrowdloan)(addresses, substrateApiMap, (networkKey, rs) => {
171
171
  this.state.setCrowdloanItem(networkKey, rs);
172
- }, this.state.getChainInfoMap());
172
+ });
173
173
  if (onlyRunOnFirstTime) {
174
174
  subscriptionPromise.then(unsub => {
175
175
  unsub && unsub();
@@ -13,6 +13,6 @@ const packageInfo = {
13
13
  name: '@subwallet/extension-base',
14
14
  path: typeof __dirname === 'string' ? __dirname : 'auto',
15
15
  type: 'cjs',
16
- version: '1.1.22-0'
16
+ version: '1.1.24-0'
17
17
  };
18
18
  exports.packageInfo = packageInfo;
@@ -41,7 +41,9 @@ const _BALANCE_CHAIN_GROUP = {
41
41
  equilibrium_parachain: ['equilibrium_parachain'],
42
42
  bifrost: ['bifrost', 'acala', 'karura', 'acala_testnet', 'pioneer', 'bitcountry', 'bifrost_dot', 'hydradx_main', 'pendulum', 'amplitude'],
43
43
  statemine: ['statemine', 'astar', 'shiden', 'statemint', 'moonbeam', 'moonbase', 'moonriver', 'crabParachain', 'darwinia2', 'parallel', 'calamari'],
44
- kusama: ['kusama', 'kintsugi', 'kintsugi_test', 'interlay', 'acala', 'statemint', 'karura', 'bifrost'] // perhaps there are some runtime updates
44
+ kusama: ['kusama', 'kintsugi', 'kintsugi_test', 'interlay', 'acala', 'statemint', 'karura', 'bifrost'],
45
+ // perhaps there are some runtime updates
46
+ centrifuge: ['centrifuge']
45
47
  };
46
48
  exports._BALANCE_CHAIN_GROUP = _BALANCE_CHAIN_GROUP;
47
49
  const _BALANCE_TOKEN_GROUP = {
@@ -211,7 +213,8 @@ const _TRANSFER_CHAIN_GROUP = {
211
213
  riochain: ['riochain'],
212
214
  sora_substrate: ['sora_substrate'],
213
215
  avail: ['kate', 'goldberg_testnet'],
214
- pendulum: ['pendulum', 'amplitude', 'amplitude_test']
216
+ pendulum: ['pendulum', 'amplitude', 'amplitude_test'],
217
+ centrifuge: ['centrifuge']
215
218
  };
216
219
  exports._TRANSFER_CHAIN_GROUP = _TRANSFER_CHAIN_GROUP;
217
220
  const _BALANCE_PARSING_CHAIN_GROUP = {
@@ -1399,5 +1399,23 @@ class ChainService {
1399
1399
  getMetadataByHash(hash) {
1400
1400
  return this.dbService.stores.metadata.getMetadataByGenesisHash(hash);
1401
1401
  }
1402
+ getSubscanChainMap(reverse) {
1403
+ const result = {};
1404
+ const chainInfoMap = this.getChainInfoMap();
1405
+ Object.values(chainInfoMap).forEach(i => {
1406
+ var _i$extraInfo;
1407
+ if (!((_i$extraInfo = i.extraInfo) !== null && _i$extraInfo !== void 0 && _i$extraInfo.subscanSlug)) {
1408
+ return;
1409
+ }
1410
+ if (!reverse) {
1411
+ var _i$extraInfo2;
1412
+ result[i.slug] = (_i$extraInfo2 = i.extraInfo) === null || _i$extraInfo2 === void 0 ? void 0 : _i$extraInfo2.subscanSlug;
1413
+ } else {
1414
+ var _i$extraInfo3;
1415
+ result[(_i$extraInfo3 = i.extraInfo) === null || _i$extraInfo3 === void 0 ? void 0 : _i$extraInfo3.subscanSlug] = i.slug;
1416
+ }
1417
+ });
1418
+ return result;
1419
+ }
1402
1420
  }
1403
1421
  exports.ChainService = ChainService;
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getExtrinsicParserKey = getExtrinsicParserKey;
7
+ exports.supportedExtrinsicParser = exports.subscanExtrinsicParserMap = void 0;
8
+ var _KoniTypes = require("@subwallet/extension-base/background/KoniTypes");
9
+ var _utilCrypto = require("@polkadot/util-crypto");
10
+ // Copyright 2019-2022 @subwallet/extension-koni authors & contributors
11
+ // SPDX-License-Identifier: Apache-2.0
12
+
13
+ function getExtrinsicParserKey(extrinsicItem) {
14
+ return `${extrinsicItem.call_module}.${extrinsicItem.call_module_function}`;
15
+ }
16
+ function paramJsonParse(item) {
17
+ try {
18
+ return JSON.parse(item.data || '[]');
19
+ } catch (e) {
20
+ return [];
21
+ }
22
+ }
23
+ function balanceTransferParserFunction(item) {
24
+ const params = paramJsonParse(item);
25
+ params.forEach(p => {
26
+ if (p.name === 'dest') {
27
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
28
+ const toPublicKey = p.value.id || p.value.Id;
29
+ if (toPublicKey) {
30
+ item.to = (0, _utilCrypto.encodeAddress)(toPublicKey, 42);
31
+ }
32
+ } else if (p.name === 'value') {
33
+ if (item.amount) {
34
+ item.amount.value = p.value;
35
+ }
36
+ }
37
+ });
38
+ item.type = _KoniTypes.ExtrinsicType.TRANSFER_BALANCE;
39
+ if (!item.to) {
40
+ return null;
41
+ }
42
+ return item;
43
+ }
44
+
45
+ // todo: will support other type later
46
+ const subscanExtrinsicParserMap = {
47
+ 'balances.transfer': balanceTransferParserFunction,
48
+ 'balances.transfer_keep_alive': balanceTransferParserFunction,
49
+ 'balances.transfer_allow_death': balanceTransferParserFunction
50
+ };
51
+ exports.subscanExtrinsicParserMap = subscanExtrinsicParserMap;
52
+ const supportedExtrinsicParser = Object.keys(subscanExtrinsicParserMap);
53
+ exports.supportedExtrinsicParser = supportedExtrinsicParser;
@@ -8,24 +8,32 @@ var _KoniTypes = require("@subwallet/extension-base/background/KoniTypes");
8
8
  var _constants = require("@subwallet/extension-base/constants");
9
9
  var _types = require("@subwallet/extension-base/services/base/types");
10
10
  var _recoverHistoryStatus = require("@subwallet/extension-base/services/history-service/helpers/recoverHistoryStatus");
11
+ var _subscanExtrinsicParserHelper = require("@subwallet/extension-base/services/history-service/helpers/subscan-extrinsic-parser-helper");
12
+ var _subscanHistory = require("@subwallet/extension-base/services/history-service/subscan-history");
13
+ var _utils = require("@subwallet/extension-base/utils");
11
14
  var _promise = require("@subwallet/extension-base/utils/promise");
12
15
  var _uiKeyring = require("@subwallet/ui-keyring");
13
16
  var _rxjs = require("rxjs");
14
17
  // Copyright 2019-2022 @subwallet/extension-base
15
18
  // SPDX-License-Identifier: Apache-2.0
16
19
 
20
+ function filterHistoryItemByAddressAndChain(chain, address) {
21
+ return item => {
22
+ return item.chain === chain && item.address === address;
23
+ };
24
+ }
17
25
  class HistoryService {
18
26
  historySubject = new _rxjs.BehaviorSubject([]);
19
27
  #needRecoveryHistories = {};
20
- constructor(dbService, chainService, eventService, keyringService) {
28
+ constructor(dbService, chainService, eventService, keyringService, subscanService) {
21
29
  this.dbService = dbService;
22
30
  this.chainService = chainService;
23
31
  this.eventService = eventService;
24
32
  this.keyringService = keyringService;
33
+ this.subscanService = subscanService;
25
34
  this.init().catch(console.error);
26
35
  }
27
36
  fetchPromise = null;
28
- interval = undefined;
29
37
  recoverInterval = undefined;
30
38
  async fetchAndLoadHistories(addresses) {
31
39
  if (!addresses || addresses.length === 0) {
@@ -67,6 +75,74 @@ class HistoryService {
67
75
  await this.getHistories();
68
76
  return this.historySubject;
69
77
  }
78
+ fetchSubscanTransactionHistory(chain, address) {
79
+ if (!this.subscanService.checkSupportedSubscanChain(chain)) {
80
+ return;
81
+ }
82
+ const chainInfo = this.chainService.getChainInfoByKey(chain);
83
+ const excludeExtrinsicParserKeys = ['balances.transfer_all'];
84
+
85
+ // Note: fetchAllPossibleExtrinsicItems and fetchAllPossibleTransferItems-receive can run parallelly
86
+ // Hover, fetchAllPossibleTransferItems-sent must run after fetchAllPossibleExtrinsicItems,
87
+ // to avoid "duplicate Extrinsic Hash between items" problem
88
+
89
+ this.subscanService.fetchAllPossibleExtrinsicItems(chain, address, extrinsicItems => {
90
+ const result = [];
91
+ extrinsicItems.forEach(x => {
92
+ const item = (0, _subscanHistory.parseSubscanExtrinsicData)(address, x, chainInfo);
93
+ if (item) {
94
+ result.push(item);
95
+ }
96
+ });
97
+ this.addHistoryItems(result).catch(e => {
98
+ console.log('addHistoryItems in fetchAllPossibleExtrinsicItems error', e);
99
+ });
100
+ }).then(extrinsicItems => {
101
+ const excludeTransferExtrinsicHash = [];
102
+ extrinsicItems.forEach(x => {
103
+ if (!excludeExtrinsicParserKeys.includes((0, _subscanExtrinsicParserHelper.getExtrinsicParserKey)(x))) {
104
+ excludeTransferExtrinsicHash.push(x.extrinsic_hash);
105
+ }
106
+ });
107
+ this.subscanService.fetchAllPossibleTransferItems(chain, address, 'sent', transferItems => {
108
+ const result = [];
109
+ transferItems.forEach(t => {
110
+ if (!excludeTransferExtrinsicHash.includes(t.hash)) {
111
+ result.push((0, _subscanHistory.parseSubscanTransferData)(address, t, chainInfo));
112
+ }
113
+ });
114
+ this.addHistoryItems(result).catch(e => {
115
+ console.log('addHistoryItems in fetchAllPossibleTransferItems-sent error', e);
116
+ });
117
+ }).catch(e => {
118
+ console.log('fetchAllPossibleTransferItems-sent error', e);
119
+ });
120
+ }).catch(e => {
121
+ console.log('fetchAllPossibleExtrinsicItems error', e);
122
+ });
123
+ this.subscanService.fetchAllPossibleTransferItems(chain, address, 'received', transferItems => {
124
+ const result = [];
125
+ transferItems.forEach(t => {
126
+ result.push((0, _subscanHistory.parseSubscanTransferData)(address, t, chainInfo));
127
+ });
128
+ this.addHistoryItems(result).catch(e => {
129
+ console.log('addHistoryItems in fetchAllPossibleTransferItems-receive error', e);
130
+ });
131
+ }).catch(e => {
132
+ console.log('fetchAllPossibleTransferItems-receive error', e);
133
+ });
134
+ }
135
+ subscribeHistories(chain, address, cb) {
136
+ const _address = (0, _utils.reformatAddress)(address);
137
+ const subscription = this.historySubject.subscribe(items => {
138
+ cb(items.filter(filterHistoryItemByAddressAndChain(chain, _address)));
139
+ });
140
+ this.fetchSubscanTransactionHistory(chain, _address);
141
+ return {
142
+ unsubscribe: subscription.unsubscribe,
143
+ value: this.historySubject.getValue().filter(filterHistoryItemByAddressAndChain(chain, _address))
144
+ };
145
+ }
70
146
  async updateHistories(chain, extrinsicHash, updateData) {
71
147
  const existedRecords = await this.dbService.getHistories({
72
148
  chain,
@@ -117,17 +193,6 @@ class HistoryService {
117
193
  async persistData() {
118
194
  await this.dbService.upsertHistory(this.historySubject.value);
119
195
  }
120
- async startCron() {
121
- await this.getHistories();
122
- this.interval = setInterval(() => {
123
- this.getHistories().catch(console.error);
124
- }, _constants.CRON_REFRESH_HISTORY_INTERVAL);
125
- }
126
- stopCron() {
127
- clearTimeout(this.interval);
128
- this.fetchPromise = null;
129
- return Promise.resolve();
130
- }
131
196
  async startRecoverHistories() {
132
197
  await this.recoverHistories();
133
198
  this.recoverInterval = setInterval(() => {
@@ -182,12 +247,6 @@ class HistoryService {
182
247
  Promise.all([this.eventService.waitKeyringReady, this.eventService.waitChainReady]).then(() => {
183
248
  this.getHistories().catch(console.log);
184
249
  this.recoverProcessingHistory().catch(console.error);
185
- this.eventService.on('account.add', () => {
186
- (async () => {
187
- await this.stopCron();
188
- await this.startCron();
189
- })().catch(console.error);
190
- });
191
250
  this.eventService.on('account.remove', address => {
192
251
  this.removeHistoryByAddress(address).catch(console.error);
193
252
  });
@@ -216,7 +275,6 @@ class HistoryService {
216
275
  await Promise.all([this.eventService.waitKeyringReady, this.eventService.waitChainReady]);
217
276
  this.startPromiseHandler = (0, _promise.createPromiseHandler)();
218
277
  this.status = _types.ServiceStatus.STARTING;
219
- await this.startCron();
220
278
  this.status = _types.ServiceStatus.STARTED;
221
279
  this.startPromiseHandler.resolve();
222
280
  } catch (e) {
@@ -232,7 +290,6 @@ class HistoryService {
232
290
  this.stopPromiseHandler = (0, _promise.createPromiseHandler)();
233
291
  this.status = _types.ServiceStatus.STOPPING;
234
292
  await this.persistData();
235
- await this.stopCron();
236
293
  await this.stopRecoverHistories();
237
294
  this.stopPromiseHandler.resolve();
238
295
  this.status = _types.ServiceStatus.STOPPED;
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.parseSubscanExtrinsicData = parseSubscanExtrinsicData;
7
+ exports.parseSubscanTransferData = parseSubscanTransferData;
8
+ var _KoniTypes = require("@subwallet/extension-base/background/KoniTypes");
9
+ var _subscanExtrinsicParserHelper = require("@subwallet/extension-base/services/history-service/helpers/subscan-extrinsic-parser-helper");
10
+ var _utilCrypto = require("@polkadot/util-crypto");
11
+ // Copyright 2019-2022 @subwallet/extension-koni authors & contributors
12
+ // SPDX-License-Identifier: Apache-2.0
13
+
14
+ function autoFormatAddress(address) {
15
+ try {
16
+ if ((0, _utilCrypto.isEthereumAddress)(address)) {
17
+ return address;
18
+ } else {
19
+ const decoded = (0, _utilCrypto.decodeAddress)(address);
20
+ return (0, _utilCrypto.encodeAddress)(decoded, 42);
21
+ }
22
+ } catch (e) {
23
+ return '';
24
+ }
25
+ }
26
+ function parseSubscanExtrinsicData(address, extrinsicItem, chainInfo) {
27
+ var _chainInfo$substrateI, _chainInfo$evmInfo, _chainInfo$substrateI2, _chainInfo$evmInfo2;
28
+ const extrinsicParserKey = (0, _subscanExtrinsicParserHelper.getExtrinsicParserKey)(extrinsicItem);
29
+ if (!_subscanExtrinsicParserHelper.supportedExtrinsicParser.includes(extrinsicParserKey)) {
30
+ return null;
31
+ }
32
+ const chainType = chainInfo.substrateInfo ? _KoniTypes.ChainType.SUBSTRATE : _KoniTypes.ChainType.EVM;
33
+ const nativeDecimals = ((_chainInfo$substrateI = chainInfo.substrateInfo) === null || _chainInfo$substrateI === void 0 ? void 0 : _chainInfo$substrateI.decimals) || ((_chainInfo$evmInfo = chainInfo.evmInfo) === null || _chainInfo$evmInfo === void 0 ? void 0 : _chainInfo$evmInfo.decimals) || 18;
34
+ const nativeSymbol = ((_chainInfo$substrateI2 = chainInfo.substrateInfo) === null || _chainInfo$substrateI2 === void 0 ? void 0 : _chainInfo$substrateI2.symbol) || ((_chainInfo$evmInfo2 = chainInfo.evmInfo) === null || _chainInfo$evmInfo2 === void 0 ? void 0 : _chainInfo$evmInfo2.symbol) || '';
35
+ const initData = {
36
+ address,
37
+ origin: 'subscan',
38
+ time: extrinsicItem.block_timestamp * 1000,
39
+ chainType,
40
+ from: address,
41
+ signature: extrinsicItem.signature,
42
+ fromName: undefined,
43
+ direction: _KoniTypes.TransactionDirection.SEND,
44
+ blockNumber: extrinsicItem.block_num,
45
+ blockHash: '',
46
+ chain: chainInfo.slug,
47
+ type: _KoniTypes.ExtrinsicType.UNKNOWN,
48
+ to: '',
49
+ toName: undefined,
50
+ extrinsicHash: extrinsicItem.extrinsic_hash,
51
+ amount: {
52
+ value: '0',
53
+ decimals: nativeDecimals,
54
+ symbol: nativeSymbol
55
+ },
56
+ data: extrinsicItem.params,
57
+ fee: {
58
+ value: extrinsicItem.fee,
59
+ decimals: nativeDecimals,
60
+ symbol: nativeSymbol
61
+ },
62
+ status: extrinsicItem.success ? _KoniTypes.ExtrinsicStatus.SUCCESS : _KoniTypes.ExtrinsicStatus.FAIL,
63
+ nonce: extrinsicItem.nonce
64
+ };
65
+ try {
66
+ return _subscanExtrinsicParserHelper.subscanExtrinsicParserMap[extrinsicParserKey](initData);
67
+ } catch (e) {
68
+ console.log('parseSubscanExtrinsicData error:', e, initData);
69
+ return null;
70
+ }
71
+ }
72
+ function parseSubscanTransferData(address, transferItem, chainInfo) {
73
+ var _chainInfo$substrateI3, _chainInfo$evmInfo3, _chainInfo$substrateI4, _chainInfo$evmInfo4;
74
+ const chainType = chainInfo.substrateInfo ? _KoniTypes.ChainType.SUBSTRATE : _KoniTypes.ChainType.EVM;
75
+ const nativeDecimals = ((_chainInfo$substrateI3 = chainInfo.substrateInfo) === null || _chainInfo$substrateI3 === void 0 ? void 0 : _chainInfo$substrateI3.decimals) || ((_chainInfo$evmInfo3 = chainInfo.evmInfo) === null || _chainInfo$evmInfo3 === void 0 ? void 0 : _chainInfo$evmInfo3.decimals) || 18;
76
+ const nativeSymbol = ((_chainInfo$substrateI4 = chainInfo.substrateInfo) === null || _chainInfo$substrateI4 === void 0 ? void 0 : _chainInfo$substrateI4.symbol) || ((_chainInfo$evmInfo4 = chainInfo.evmInfo) === null || _chainInfo$evmInfo4 === void 0 ? void 0 : _chainInfo$evmInfo4.symbol) || '';
77
+ const from = autoFormatAddress(transferItem.from);
78
+ const to = autoFormatAddress(transferItem.to);
79
+ return {
80
+ address,
81
+ origin: 'subscan',
82
+ time: transferItem.block_timestamp * 1000,
83
+ chainType,
84
+ from,
85
+ fromName: transferItem.from_account_display.display,
86
+ direction: address === from ? _KoniTypes.TransactionDirection.SEND : _KoniTypes.TransactionDirection.RECEIVED,
87
+ blockNumber: transferItem.block_num,
88
+ blockHash: '',
89
+ chain: chainInfo.slug,
90
+ type: _KoniTypes.ExtrinsicType.TRANSFER_BALANCE,
91
+ to,
92
+ toName: transferItem.to_account_display.display,
93
+ extrinsicHash: transferItem.hash,
94
+ amount: {
95
+ value: transferItem.amount,
96
+ decimals: 0,
97
+ symbol: transferItem.asset_symbol
98
+ },
99
+ fee: {
100
+ value: transferItem.fee,
101
+ decimals: nativeDecimals,
102
+ symbol: nativeSymbol
103
+ },
104
+ status: transferItem.success ? _KoniTypes.ExtrinsicStatus.SUCCESS : _KoniTypes.ExtrinsicStatus.FAIL,
105
+ nonce: transferItem.nonce
106
+ };
107
+ }
@@ -13,10 +13,12 @@ var _ChainStakingMetadata = _interopRequireDefault(require("@subwallet/extension
13
13
  var _MantaPay = _interopRequireDefault(require("@subwallet/extension-base/services/storage-service/db-stores/MantaPay"));
14
14
  var _NominatorMetadata = _interopRequireDefault(require("@subwallet/extension-base/services/storage-service/db-stores/NominatorMetadata"));
15
15
  var _utils = require("@subwallet/extension-base/utils");
16
+ var _dexieExportImport = require("dexie-export-import");
16
17
  var _util = require("@polkadot/util");
17
18
  // Copyright 2019-2022 @subwallet/extension-base authors & contributors
18
19
  // SPDX-License-Identifier: Apache-2.0
19
20
 
21
+ const EXPORT_EXCLUDE_TABLES = ['metadata'];
20
22
  class DatabaseService {
21
23
  // TODO: might remove this
22
24
 
@@ -287,5 +289,43 @@ class DatabaseService {
287
289
  upsertCampaign(campaign) {
288
290
  return this.stores.campaign.upsertCampaign(campaign);
289
291
  }
292
+ async exportDB() {
293
+ const blob = await (0, _dexieExportImport.exportDB)(this._db, {
294
+ filter: (table, value, key) => {
295
+ if (EXPORT_EXCLUDE_TABLES.indexOf(table) >= 0) {
296
+ return false;
297
+ }
298
+ return true;
299
+ }
300
+ });
301
+ return await blob.text();
302
+ }
303
+ async importDB(data) {
304
+ try {
305
+ const blob = new Blob([data], {
306
+ type: 'application/json'
307
+ });
308
+ await this._db.import(blob, {
309
+ overwriteValues: true,
310
+ acceptMissingTables: true,
311
+ acceptVersionDiff: true
312
+ });
313
+ return true;
314
+ } catch (e) {
315
+ this.logger.error(e);
316
+ return false;
317
+ }
318
+ }
319
+ async checkImportMetadata(data) {
320
+ try {
321
+ const blob = new Blob([data], {
322
+ type: 'application/json'
323
+ });
324
+ return await (0, _dexieExportImport.peakImportFile)(blob);
325
+ } catch (e) {
326
+ this.logger.error(e);
327
+ return null;
328
+ }
329
+ }
290
330
  }
291
331
  exports.default = DatabaseService;