@subwallet/extension-base 1.1.51-1 → 1.1.52-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.
- package/background/KoniTypes.d.ts +1 -0
- package/cjs/koni/api/tokens/evm/balance.js +1 -2
- package/cjs/koni/api/tokens/evm/transfer.js +3 -5
- package/cjs/koni/api/tokens/evm/web3.js +3 -3
- package/cjs/koni/background/handlers/Extension.js +5 -4
- package/cjs/koni/background/handlers/State.js +21 -122
- package/cjs/koni/background/subscription.js +3 -63
- package/cjs/packageInfo.js +1 -1
- package/cjs/services/balance-service/BalanceMapImpl.js +2 -2
- package/cjs/services/balance-service/helpers/index.js +27 -0
- package/cjs/services/balance-service/helpers/subscribe/evm.js +33 -15
- package/cjs/services/balance-service/helpers/subscribe/{balance.js → index.js} +46 -29
- package/cjs/services/balance-service/helpers/subscribe/substrate/equilibrium.js +34 -15
- package/cjs/services/balance-service/helpers/subscribe/substrate/index.js +156 -72
- package/cjs/services/balance-service/index.js +371 -44
- package/cjs/services/chain-service/constants.js +2 -1
- package/cjs/services/chain-service/health-check/utils/asset-info.js +1 -3
- package/cjs/services/chain-service/index.js +60 -26
- package/cjs/services/chain-service/utils/index.js +10 -2
- package/cjs/services/chain-service/utils/patch.js +1 -1
- package/cjs/services/earning-service/handlers/liquid-staking/stella-swap.js +2 -2
- package/cjs/services/earning-service/handlers/native-staking/para-chain.js +1 -1
- package/cjs/services/migration-service/scripts/AutoEnableChainsTokens.js +1 -1
- package/cjs/services/storage-service/DatabaseService.js +10 -1
- package/cjs/stores/DetectAccountBalance.js +18 -0
- package/cjs/utils/asset.js +19 -0
- package/cjs/utils/index.js +12 -0
- package/koni/api/tokens/evm/balance.d.ts +1 -1
- package/koni/api/tokens/evm/balance.js +1 -2
- package/koni/api/tokens/evm/transfer.d.ts +2 -2
- package/koni/api/tokens/evm/transfer.js +3 -5
- package/koni/api/tokens/evm/web3.d.ts +1 -1
- package/koni/api/tokens/evm/web3.js +2 -2
- package/koni/background/handlers/Extension.js +5 -4
- package/koni/background/handlers/State.d.ts +2 -13
- package/koni/background/handlers/State.js +21 -121
- package/koni/background/subscription.d.ts +1 -5
- package/koni/background/subscription.js +1 -60
- package/package.json +28 -13
- package/packageInfo.js +1 -1
- package/services/balance-service/BalanceMapImpl.js +1 -1
- package/services/balance-service/helpers/index.d.ts +2 -0
- package/services/balance-service/helpers/index.js +5 -0
- package/services/balance-service/helpers/subscribe/evm.d.ts +3 -5
- package/services/balance-service/helpers/subscribe/evm.js +31 -14
- package/services/balance-service/helpers/subscribe/{balance.d.ts → index.d.ts} +2 -2
- package/services/balance-service/helpers/subscribe/{balance.js → index.js} +41 -28
- package/services/balance-service/helpers/subscribe/substrate/equilibrium.d.ts +3 -4
- package/services/balance-service/helpers/subscribe/substrate/equilibrium.js +29 -13
- package/services/balance-service/helpers/subscribe/substrate/index.d.ts +2 -2
- package/services/balance-service/helpers/subscribe/substrate/index.js +143 -67
- package/services/balance-service/index.d.ts +71 -4
- package/services/balance-service/index.js +372 -46
- package/services/chain-service/constants.js +2 -1
- package/services/chain-service/health-check/utils/asset-info.js +1 -3
- package/services/chain-service/index.d.ts +2 -1
- package/services/chain-service/index.js +61 -27
- package/services/chain-service/types.d.ts +1 -0
- package/services/chain-service/utils/index.js +10 -2
- package/services/chain-service/utils/patch.js +1 -1
- package/services/earning-service/handlers/liquid-staking/stella-swap.js +2 -2
- package/services/earning-service/handlers/native-staking/para-chain.js +1 -1
- package/services/migration-service/scripts/AutoEnableChainsTokens.js +1 -1
- package/services/storage-service/DatabaseService.js +7 -1
- package/services/storage-service/databases/index.d.ts +1 -0
- package/stores/DetectAccountBalance.d.ts +5 -0
- package/stores/DetectAccountBalance.js +10 -0
- package/types/{balance.d.ts → balance/index.d.ts} +16 -0
- package/types/index.js +1 -1
- package/utils/asset.d.ts +2 -0
- package/utils/asset.js +12 -0
- package/utils/index.d.ts +1 -0
- package/utils/index.js +1 -0
- /package/cjs/types/{balance.js → balance/index.js} +0 -0
- /package/types/{balance.js → balance/index.js} +0 -0
|
@@ -1,18 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
3
4
|
Object.defineProperty(exports, "__esModule", {
|
|
4
5
|
value: true
|
|
5
6
|
});
|
|
6
7
|
exports.BalanceService = void 0;
|
|
7
8
|
var _BalanceError = require("@subwallet/extension-base/background/errors/BalanceError");
|
|
8
9
|
var _KoniTypes = require("@subwallet/extension-base/background/KoniTypes");
|
|
9
|
-
var
|
|
10
|
-
var
|
|
11
|
-
var _substrate = require("@subwallet/extension-base/services/balance-service/helpers/subscribe/substrate");
|
|
12
|
-
var _constants = require("@subwallet/extension-base/services/chain-service/constants");
|
|
10
|
+
var _constants = require("@subwallet/extension-base/constants");
|
|
11
|
+
var _types = require("@subwallet/extension-base/services/base/types");
|
|
13
12
|
var _utils = require("@subwallet/extension-base/services/chain-service/utils");
|
|
13
|
+
var _DetectAccountBalance = _interopRequireDefault(require("@subwallet/extension-base/stores/DetectAccountBalance"));
|
|
14
14
|
var _utils2 = require("@subwallet/extension-base/utils");
|
|
15
|
+
var _uiKeyring = _interopRequireDefault(require("@subwallet/ui-keyring"));
|
|
15
16
|
var _i18next = require("i18next");
|
|
17
|
+
var _rxjs = require("rxjs");
|
|
18
|
+
var _util = require("@polkadot/util");
|
|
19
|
+
var _BalanceMapImpl = require("./BalanceMapImpl");
|
|
20
|
+
var _helpers = require("./helpers");
|
|
16
21
|
// Copyright 2019-2022 @subwallet/extension-base authors & contributors
|
|
17
22
|
// SPDX-License-Identifier: Apache-2.0
|
|
18
23
|
|
|
@@ -21,23 +26,146 @@ var _i18next = require("i18next");
|
|
|
21
26
|
* @class
|
|
22
27
|
*/
|
|
23
28
|
class BalanceService {
|
|
29
|
+
balanceUpdateCache = [];
|
|
30
|
+
startPromiseHandler = (0, _utils2.createPromiseHandler)();
|
|
31
|
+
stopPromiseHandler = (0, _utils2.createPromiseHandler)();
|
|
32
|
+
status = _types.ServiceStatus.NOT_INITIALIZED;
|
|
33
|
+
isReload = false;
|
|
34
|
+
detectAccountBalanceStore = new _DetectAccountBalance.default();
|
|
35
|
+
balanceDetectSubject = new _rxjs.BehaviorSubject({});
|
|
36
|
+
intervalTime = 3 * 60 * 1000;
|
|
37
|
+
cacheTime = 15 * 60 * 1000;
|
|
38
|
+
|
|
24
39
|
/**
|
|
25
40
|
* @constructor
|
|
26
41
|
* @param {KoniState} state - The state of extension.
|
|
27
42
|
*/
|
|
28
43
|
constructor(state) {
|
|
29
44
|
this.state = state;
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
45
|
+
this.balanceMap = new _BalanceMapImpl.BalanceMapImpl();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Init service */
|
|
49
|
+
async init() {
|
|
50
|
+
this.status = _types.ServiceStatus.INITIALIZING;
|
|
51
|
+
await this.state.eventService.waitChainReady;
|
|
52
|
+
await this.state.eventService.waitAccountReady;
|
|
53
|
+
|
|
54
|
+
// Load data from db to balanceSubject
|
|
55
|
+
await this.loadData();
|
|
56
|
+
this.status = _types.ServiceStatus.INITIALIZED;
|
|
57
|
+
|
|
58
|
+
// Start service
|
|
59
|
+
await this.start();
|
|
60
|
+
|
|
61
|
+
// Handle events
|
|
62
|
+
this.state.eventService.onLazy(this.handleEvents.bind(this));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Restore balance map */
|
|
66
|
+
async loadData() {
|
|
67
|
+
const backupBalanceData = await this.state.dbService.getStoredBalance();
|
|
68
|
+
this.balanceMap.updateBalanceItems(backupBalanceData, true);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Start service */
|
|
72
|
+
async start() {
|
|
73
|
+
if (this.status === _types.ServiceStatus.STOPPING) {
|
|
74
|
+
await this.waitForStopped();
|
|
75
|
+
}
|
|
76
|
+
if (this.status === _types.ServiceStatus.STARTED || this.status === _types.ServiceStatus.STARTING) {
|
|
77
|
+
return await this.waitForStarted();
|
|
78
|
+
}
|
|
79
|
+
this.status = _types.ServiceStatus.STARTING;
|
|
80
|
+
await this.startScanBalance();
|
|
81
|
+
|
|
82
|
+
// Run subscribe balance
|
|
83
|
+
await this.runSubscribeBalances();
|
|
84
|
+
|
|
85
|
+
// Update status
|
|
86
|
+
this.stopPromiseHandler = (0, _utils2.createPromiseHandler)();
|
|
87
|
+
this.status = _types.ServiceStatus.STARTED;
|
|
88
|
+
this.startPromiseHandler.resolve();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Stop service */
|
|
92
|
+
async stop() {
|
|
93
|
+
if (this.status === _types.ServiceStatus.STARTING) {
|
|
94
|
+
await this.waitForStarted();
|
|
95
|
+
}
|
|
96
|
+
if (this.status === _types.ServiceStatus.STOPPED || this.status === _types.ServiceStatus.STOPPING) {
|
|
97
|
+
return await this.waitForStopped();
|
|
98
|
+
}
|
|
99
|
+
this.runUnsubscribeBalances();
|
|
100
|
+
this.stopScanBalance();
|
|
101
|
+
|
|
102
|
+
// Update status
|
|
103
|
+
this.startPromiseHandler = (0, _utils2.createPromiseHandler)();
|
|
104
|
+
this.status = _types.ServiceStatus.STOPPING;
|
|
105
|
+
this.stopPromiseHandler.resolve();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Wait service start */
|
|
109
|
+
waitForStarted() {
|
|
110
|
+
return this.startPromiseHandler.promise;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Wait service stop */
|
|
114
|
+
waitForStopped() {
|
|
115
|
+
return this.stopPromiseHandler.promise;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Handle when data change
|
|
120
|
+
* */
|
|
121
|
+
handleEvents(events, eventTypes) {
|
|
122
|
+
const removedAddresses = [];
|
|
123
|
+
let needReload = false;
|
|
124
|
+
let lazyTime = 2000;
|
|
125
|
+
|
|
126
|
+
// Account changed or chain changed (active or inactive)
|
|
127
|
+
if (eventTypes.includes('account.updateCurrent') || eventTypes.includes('account.add') || eventTypes.includes('chain.updateState') || eventTypes.includes('asset.updateState')) {
|
|
128
|
+
needReload = true;
|
|
129
|
+
if (eventTypes.includes('account.updateCurrent')) {
|
|
130
|
+
lazyTime = 1000;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
events.forEach(event => {
|
|
134
|
+
if (event.type === 'account.remove') {
|
|
135
|
+
removedAddresses.push(event.data[0]);
|
|
136
|
+
lazyTime = 1000;
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
if (removedAddresses.length > 0) {
|
|
140
|
+
this.balanceMap.removeBalanceItems([...removedAddresses, _constants.ALL_ACCOUNT_KEY]); // Add all account key to recalculate all account balances
|
|
141
|
+
needReload = true;
|
|
142
|
+
}
|
|
143
|
+
if (needReload) {
|
|
144
|
+
(0, _utils2.addLazy)('reloadBalanceByEvents', () => {
|
|
145
|
+
if (!this.isReload) {
|
|
146
|
+
this.runSubscribeBalances().catch(console.error);
|
|
147
|
+
}
|
|
148
|
+
}, lazyTime, undefined, true);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
getBalanceDetectCache(update) {
|
|
152
|
+
this.detectAccountBalanceStore.get('DetectBalanceCache', value => {
|
|
153
|
+
update(value);
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
setBalanceDetectCache(addresses) {
|
|
157
|
+
this.detectAccountBalanceStore.get('DetectBalanceCache', value => {
|
|
158
|
+
const rs = {
|
|
159
|
+
...value
|
|
160
|
+
};
|
|
161
|
+
for (const address of addresses) {
|
|
162
|
+
rs[address] = Date.now();
|
|
163
|
+
}
|
|
164
|
+
this.detectAccountBalanceStore.set('DetectBalanceCache', rs);
|
|
165
|
+
});
|
|
38
166
|
}
|
|
39
167
|
|
|
40
|
-
|
|
168
|
+
/** Subscribe token free balance of a address on chain */
|
|
41
169
|
async subscribeTokenFreeBalance(address, chain, tokenSlug, callback) {
|
|
42
170
|
const chainInfo = this.state.chainService.getChainInfoByKey(chain);
|
|
43
171
|
const chainState = this.state.chainService.getChainStateByKey(chain);
|
|
@@ -59,7 +187,12 @@ class BalanceService {
|
|
|
59
187
|
}
|
|
60
188
|
return new Promise((resolve, reject) => {
|
|
61
189
|
let hasError = true;
|
|
62
|
-
const
|
|
190
|
+
const assetMap = this.state.chainService.getAssetRegistry();
|
|
191
|
+
const chainInfoMap = this.state.chainService.getChainInfoMap();
|
|
192
|
+
const evmApiMap = this.state.chainService.getEvmApiMap();
|
|
193
|
+
const substrateApiMap = this.state.chainService.getSubstrateApiMap();
|
|
194
|
+
const unsub = (0, _helpers.subscribeBalance)([address], [chain], [tSlug], assetMap, chainInfoMap, substrateApiMap, evmApiMap, result => {
|
|
195
|
+
const rs = result[0];
|
|
63
196
|
if (rs.tokenSlug === tSlug) {
|
|
64
197
|
hasError = false;
|
|
65
198
|
const balance = {
|
|
@@ -99,44 +232,238 @@ class BalanceService {
|
|
|
99
232
|
const [, balance] = await this.subscribeTokenFreeBalance(address, chain, tokenSlug);
|
|
100
233
|
return balance;
|
|
101
234
|
}
|
|
102
|
-
|
|
103
|
-
|
|
235
|
+
|
|
236
|
+
/** Remove balance from the subject object by addresses */
|
|
237
|
+
removeBalanceByAddresses(addresses) {
|
|
238
|
+
this.balanceMap.removeBalanceItems([...addresses, _constants.ALL_ACCOUNT_KEY]);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** Remove inactive asset from the balance map */
|
|
242
|
+
async removeInactiveChainBalances() {
|
|
243
|
+
const assetSettings = await this.state.chainService.getAssetSettings();
|
|
244
|
+
this.balanceMap.removeBalanceItemByFilter(item => {
|
|
245
|
+
return !assetSettings[item.tokenSlug];
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
async getBalance(reset) {
|
|
249
|
+
await this.removeInactiveChainBalances();
|
|
250
|
+
return {
|
|
251
|
+
details: this.balanceMap.map,
|
|
252
|
+
reset
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** Get stored balance from db */
|
|
257
|
+
async getStoredBalance(address) {
|
|
258
|
+
return await this.state.dbService.stores.balance.getBalanceMapByAddresses(address);
|
|
259
|
+
}
|
|
260
|
+
async handleResetBalance(forceRefresh) {
|
|
261
|
+
if (forceRefresh) {
|
|
262
|
+
this.balanceMap.setData({});
|
|
263
|
+
await this.state.dbService.stores.balance.clear();
|
|
264
|
+
} else {
|
|
265
|
+
await Promise.all([this.removeInactiveChainBalances()]);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Update value for balance map
|
|
271
|
+
* Note: items must be same tokenSlug */
|
|
272
|
+
setBalanceItem(items) {
|
|
273
|
+
if (items.length) {
|
|
274
|
+
const nowTime = new Date().getTime();
|
|
275
|
+
for (const item of items) {
|
|
276
|
+
const balance = {
|
|
277
|
+
timestamp: nowTime,
|
|
278
|
+
...item
|
|
279
|
+
};
|
|
280
|
+
this.balanceUpdateCache.push(balance);
|
|
281
|
+
}
|
|
282
|
+
(0, _utils2.addLazy)('updateBalanceStore', () => {
|
|
283
|
+
const isAllAccount = (0, _utils2.isAccountAll)(this.state.keyringService.currentAccount.address);
|
|
284
|
+
this.balanceMap.updateBalanceItems(this.balanceUpdateCache, isAllAccount);
|
|
285
|
+
if (isAllAccount) {
|
|
286
|
+
this.balanceUpdateCache = [...this.balanceUpdateCache, ...Object.values(this.balanceMap.map[_constants.ALL_ACCOUNT_KEY])];
|
|
287
|
+
}
|
|
288
|
+
this.updateBalanceStore(this.balanceUpdateCache);
|
|
289
|
+
this.balanceUpdateCache = [];
|
|
290
|
+
}, 300, 1800);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Store balance map to db
|
|
296
|
+
* */
|
|
297
|
+
updateBalanceStore(items) {
|
|
298
|
+
this.state.dbService.updateBulkBalanceStore(items).catch(console.warn);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Subscribe balance map with subject object
|
|
303
|
+
* */
|
|
304
|
+
subscribeBalanceMap() {
|
|
305
|
+
return this.balanceMap.mapSubject;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** Subscribe area */
|
|
309
|
+
|
|
310
|
+
/** Subscribe balance subscription */
|
|
311
|
+
async runSubscribeBalances() {
|
|
312
|
+
await Promise.all([this.state.eventService.waitKeyringReady, this.state.eventService.waitChainReady]);
|
|
313
|
+
this.runUnsubscribeBalances();
|
|
314
|
+
const addresses = this.state.getDecodedAddresses();
|
|
315
|
+
if (!addresses.length) {
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// Reset balance before subscribe
|
|
320
|
+
await this.handleResetBalance();
|
|
321
|
+
let cancel = false;
|
|
322
|
+
const assetMap = this.state.chainService.getAssetRegistry();
|
|
104
323
|
const chainInfoMap = this.state.chainService.getChainInfoMap();
|
|
105
|
-
const chainStateMap = this.state.chainService.getChainStateMap();
|
|
106
|
-
const substrateApiMap = this.state.chainService.getSubstrateApiMap();
|
|
107
324
|
const evmApiMap = this.state.chainService.getEvmApiMap();
|
|
325
|
+
const substrateApiMap = this.state.chainService.getSubstrateApiMap();
|
|
326
|
+
const activeChainSlugs = Object.keys(this.state.getActiveChainInfoMap());
|
|
327
|
+
const assetState = this.state.chainService.subscribeAssetSettings().value;
|
|
328
|
+
const assets = Object.values(assetMap).filter(asset => {
|
|
329
|
+
var _assetState$asset$slu;
|
|
330
|
+
return activeChainSlugs.includes(asset.originChain) && ((_assetState$asset$slu = assetState[asset.slug]) === null || _assetState$asset$slu === void 0 ? void 0 : _assetState$asset$slu.visible);
|
|
331
|
+
}).map(asset => asset.slug);
|
|
332
|
+
const unsub = (0, _helpers.subscribeBalance)(addresses, activeChainSlugs, assets, assetMap, chainInfoMap, substrateApiMap, evmApiMap, result => {
|
|
333
|
+
!cancel && this.setBalanceItem(result);
|
|
334
|
+
});
|
|
335
|
+
const unsub2 = this.state.subscribeMantaPayBalance();
|
|
336
|
+
this._unsubscribeBalance = () => {
|
|
337
|
+
cancel = true;
|
|
338
|
+
unsub && unsub();
|
|
339
|
+
unsub2 && unsub2();
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** Unsubscribe balance subscription */
|
|
344
|
+
runUnsubscribeBalances() {
|
|
345
|
+
this._unsubscribeBalance && this._unsubscribeBalance();
|
|
346
|
+
this._unsubscribeBalance = undefined;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** Reload balance subscription */
|
|
350
|
+
async reloadBalance() {
|
|
351
|
+
this.isReload = true;
|
|
352
|
+
await this.handleResetBalance(true);
|
|
353
|
+
await this.runSubscribeBalances();
|
|
354
|
+
await (0, _utils2.waitTimeout)(1800);
|
|
355
|
+
this.isReload = false;
|
|
356
|
+
}
|
|
108
357
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
358
|
+
/** Subscribe area */
|
|
359
|
+
|
|
360
|
+
async autoEnableChains(addresses) {
|
|
361
|
+
this.setBalanceDetectCache(addresses);
|
|
362
|
+
const assetMap = this.state.chainService.getAssetRegistry();
|
|
363
|
+
const promiseList = addresses.map(address => {
|
|
364
|
+
return this.state.subscanService.getMultiChainBalance(address).catch(e => {
|
|
365
|
+
console.error(e);
|
|
366
|
+
return null;
|
|
367
|
+
});
|
|
368
|
+
});
|
|
369
|
+
const needEnableChains = [];
|
|
370
|
+
const needActiveTokens = [];
|
|
371
|
+
const balanceDataList = await Promise.all(promiseList);
|
|
372
|
+
const currentAssetSettings = await this.state.chainService.getAssetSettings();
|
|
373
|
+
const chainInfoMap = this.state.chainService.getChainInfoMap();
|
|
374
|
+
const detectBalanceChainSlugMap = this.state.chainService.detectBalanceChainSlugMap;
|
|
375
|
+
for (const balanceData of balanceDataList) {
|
|
376
|
+
if (balanceData) {
|
|
377
|
+
for (const balanceDatum of balanceData) {
|
|
378
|
+
var _currentAssetSettings;
|
|
379
|
+
const {
|
|
380
|
+
balance,
|
|
381
|
+
bonded,
|
|
382
|
+
category,
|
|
383
|
+
locked,
|
|
384
|
+
network,
|
|
385
|
+
symbol
|
|
386
|
+
} = balanceDatum;
|
|
387
|
+
const chain = detectBalanceChainSlugMap[network];
|
|
388
|
+
const chainInfo = chain ? chainInfoMap[chain] : null;
|
|
389
|
+
const chainState = this.state.chainService.getChainStateByKey(chain);
|
|
390
|
+
const balanceIsEmpty = (!balance || balance === '0') && (!locked || locked === '0') && (!bonded || bonded === '0');
|
|
391
|
+
const tokenKey = `${chain}-${category === 'native' ? 'NATIVE' : 'LOCAL'}-${symbol.toUpperCase()}`;
|
|
392
|
+
const existedKey = Object.keys(assetMap).find(v => v.toLowerCase() === tokenKey.toLowerCase());
|
|
393
|
+
|
|
394
|
+
// Cancel if chain is not supported or is testnet
|
|
395
|
+
if (!chainInfo || chainInfo.isTestnet) {
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// Cancel is balance is 0
|
|
400
|
+
if (balanceIsEmpty) {
|
|
401
|
+
continue;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// Cancel is chain is turned off by user
|
|
405
|
+
if (chainState && chainState.manualTurnOff) {
|
|
406
|
+
continue;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// const a = this.state.chainService.getChainStateByKey(chain);
|
|
410
|
+
|
|
411
|
+
if (existedKey && !((_currentAssetSettings = currentAssetSettings[existedKey]) !== null && _currentAssetSettings !== void 0 && _currentAssetSettings.visible)) {
|
|
412
|
+
needEnableChains.push(chain);
|
|
413
|
+
needActiveTokens.push(existedKey);
|
|
414
|
+
currentAssetSettings[existedKey] = {
|
|
415
|
+
visible: true
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
}
|
|
116
419
|
}
|
|
420
|
+
}
|
|
421
|
+
if (needActiveTokens.length) {
|
|
422
|
+
await this.state.chainService.enableChains(needEnableChains);
|
|
423
|
+
this.state.chainService.setAssetSettings({
|
|
424
|
+
...currentAssetSettings
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
async startScanBalance() {
|
|
429
|
+
var _this$startBalanceDet2;
|
|
430
|
+
await Promise.all([this.state.eventService.waitAccountReady, this.state.eventService.waitChainReady]);
|
|
431
|
+
this.stopScanBalance();
|
|
432
|
+
this.startBalanceDetectCache = (0, _utils2.createPromiseHandler)();
|
|
433
|
+
const updateBalanceDetectCache = value => {
|
|
434
|
+
var _this$startBalanceDet;
|
|
435
|
+
(_this$startBalanceDet = this.startBalanceDetectCache) === null || _this$startBalanceDet === void 0 ? void 0 : _this$startBalanceDet.resolve();
|
|
436
|
+
this.balanceDetectSubject.next(value || {});
|
|
117
437
|
};
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
438
|
+
this.getBalanceDetectCache(updateBalanceDetectCache);
|
|
439
|
+
const subscription = this.detectAccountBalanceStore.getSubject().subscribe({
|
|
440
|
+
next: updateBalanceDetectCache
|
|
441
|
+
});
|
|
442
|
+
this._unsubscribeBalanceDetectCache = subscription.unsubscribe;
|
|
443
|
+
const scanBalance = () => {
|
|
444
|
+
const addresses = _uiKeyring.default.getPairs().map(account => account.address);
|
|
445
|
+
const cache = this.balanceDetectSubject.value;
|
|
446
|
+
const now = Date.now();
|
|
447
|
+
const needDetectAddresses = [];
|
|
448
|
+
for (const address of addresses) {
|
|
449
|
+
if (!cache[address] || now - cache[address] > this.cacheTime) {
|
|
450
|
+
needDetectAddresses.push(address);
|
|
451
|
+
}
|
|
126
452
|
}
|
|
127
|
-
if (
|
|
128
|
-
|
|
453
|
+
if (needDetectAddresses.length) {
|
|
454
|
+
this.autoEnableChains(needDetectAddresses).finally(_util.noop);
|
|
129
455
|
}
|
|
130
|
-
const networkAPI = await substrateApiMap[chainSlug].isReady;
|
|
131
|
-
return (0, _substrate.subscribeSubstrateBalance)(useAddresses, chainInfo, chainSlug, networkAPI, evmApiMap, callback);
|
|
132
|
-
});
|
|
133
|
-
return () => {
|
|
134
|
-
unsubList.forEach(subProm => {
|
|
135
|
-
subProm.then(unsub => {
|
|
136
|
-
unsub && unsub();
|
|
137
|
-
}).catch(console.error);
|
|
138
|
-
});
|
|
139
456
|
};
|
|
457
|
+
await ((_this$startBalanceDet2 = this.startBalanceDetectCache) === null || _this$startBalanceDet2 === void 0 ? void 0 : _this$startBalanceDet2.promise);
|
|
458
|
+
scanBalance();
|
|
459
|
+
this._intervalScan = setInterval(scanBalance, this.intervalTime);
|
|
460
|
+
}
|
|
461
|
+
stopScanBalance() {
|
|
462
|
+
this._intervalScan && clearInterval(this._intervalScan);
|
|
463
|
+
this._unsubscribeBalanceDetectCache && this._unsubscribeBalanceDetectCache();
|
|
464
|
+
this._intervalScan = undefined;
|
|
465
|
+
this._unsubscribeBalanceDetectCache = undefined;
|
|
466
|
+
this.startBalanceDetectCache = undefined;
|
|
140
467
|
}
|
|
141
468
|
}
|
|
142
469
|
exports.BalanceService = BalanceService;
|
|
@@ -263,7 +263,8 @@ exports._DEFAULT_ACTIVE_CHAINS = _DEFAULT_ACTIVE_CHAINS;
|
|
|
263
263
|
const EVM_PASS_CONNECT_STATUS = {
|
|
264
264
|
arbitrum_one: ['*'],
|
|
265
265
|
okxTest: ['*'],
|
|
266
|
-
astarZkEvm: ['*']
|
|
266
|
+
astarZkEvm: ['*'],
|
|
267
|
+
xlayer: ['*']
|
|
267
268
|
};
|
|
268
269
|
exports.EVM_PASS_CONNECT_STATUS = EVM_PASS_CONNECT_STATUS;
|
|
269
270
|
const EVM_REFORMAT_DECIMALS = {
|
|
@@ -227,9 +227,7 @@ const getEvmNativeInfo = async api => {
|
|
|
227
227
|
exports.getEvmNativeInfo = getEvmNativeInfo;
|
|
228
228
|
const getErc20AssetInfo = async (asset, api) => {
|
|
229
229
|
const contractAddress = (0, _utils2._getContractAddressOfToken)(asset);
|
|
230
|
-
const tokenContract = (0, _web.getERC20Contract)(
|
|
231
|
-
chain: api
|
|
232
|
-
});
|
|
230
|
+
const tokenContract = (0, _web.getERC20Contract)(contractAddress, api);
|
|
233
231
|
const [_decimals, _symbol, _name] = await Promise.all([
|
|
234
232
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
|
|
235
233
|
tokenContract.methods.decimals().call(),
|