@subwallet/extension-base 1.1.51-2 → 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/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 +8 -71
- 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 +277 -75
- 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 +1 -9
- 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/storage-service/DatabaseService.js +10 -1
- 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 +1 -12
- package/koni/background/handlers/State.js +9 -72
- package/koni/background/subscription.d.ts +1 -5
- package/koni/background/subscription.js +1 -60
- package/package.json +23 -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 +64 -6
- package/services/balance-service/index.js +279 -77
- 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.js +2 -10
- package/services/chain-service/utils/patch.js +1 -1
- package/services/earning-service/handlers/liquid-staking/stella-swap.js +2 -2
- package/services/storage-service/DatabaseService.js +7 -1
- 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
|
@@ -7,10 +7,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
7
7
|
exports.BalanceService = void 0;
|
|
8
8
|
var _BalanceError = require("@subwallet/extension-base/background/errors/BalanceError");
|
|
9
9
|
var _KoniTypes = require("@subwallet/extension-base/background/KoniTypes");
|
|
10
|
-
var
|
|
11
|
-
var
|
|
12
|
-
var _substrate = require("@subwallet/extension-base/services/balance-service/helpers/subscribe/substrate");
|
|
13
|
-
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");
|
|
14
12
|
var _utils = require("@subwallet/extension-base/services/chain-service/utils");
|
|
15
13
|
var _DetectAccountBalance = _interopRequireDefault(require("@subwallet/extension-base/stores/DetectAccountBalance"));
|
|
16
14
|
var _utils2 = require("@subwallet/extension-base/utils");
|
|
@@ -18,6 +16,8 @@ var _uiKeyring = _interopRequireDefault(require("@subwallet/ui-keyring"));
|
|
|
18
16
|
var _i18next = require("i18next");
|
|
19
17
|
var _rxjs = require("rxjs");
|
|
20
18
|
var _util = require("@polkadot/util");
|
|
19
|
+
var _BalanceMapImpl = require("./BalanceMapImpl");
|
|
20
|
+
var _helpers = require("./helpers");
|
|
21
21
|
// Copyright 2019-2022 @subwallet/extension-base authors & contributors
|
|
22
22
|
// SPDX-License-Identifier: Apache-2.0
|
|
23
23
|
|
|
@@ -26,56 +26,127 @@ var _util = require("@polkadot/util");
|
|
|
26
26
|
* @class
|
|
27
27
|
*/
|
|
28
28
|
class BalanceService {
|
|
29
|
+
balanceUpdateCache = [];
|
|
30
|
+
startPromiseHandler = (0, _utils2.createPromiseHandler)();
|
|
31
|
+
stopPromiseHandler = (0, _utils2.createPromiseHandler)();
|
|
32
|
+
status = _types.ServiceStatus.NOT_INITIALIZED;
|
|
33
|
+
isReload = false;
|
|
29
34
|
detectAccountBalanceStore = new _DetectAccountBalance.default();
|
|
30
35
|
balanceDetectSubject = new _rxjs.BehaviorSubject({});
|
|
31
36
|
intervalTime = 3 * 60 * 1000;
|
|
32
37
|
cacheTime = 15 * 60 * 1000;
|
|
38
|
+
|
|
33
39
|
/**
|
|
34
40
|
* @constructor
|
|
35
41
|
* @param {KoniState} state - The state of extension.
|
|
36
42
|
*/
|
|
37
43
|
constructor(state) {
|
|
38
44
|
this.state = state;
|
|
39
|
-
|
|
40
|
-
// Todo: Start subscribe balance and data
|
|
41
|
-
// Todo: Listen change and apply to balanceSubject
|
|
42
|
-
// Todo: Active/Chain
|
|
43
|
-
// Todo: Add/remove account
|
|
44
|
-
// Todo: Add new account
|
|
45
|
-
// Todo: Optimize get balance for single account and chain with cache
|
|
46
|
-
// Todo: Move everything of fetching balance to this service
|
|
47
|
-
|
|
48
|
-
this.startHandler = (0, _utils2.createPromiseHandler)();
|
|
49
|
-
const updateBalanceDetectCache = value => {
|
|
50
|
-
this.startHandler.resolve();
|
|
51
|
-
this.balanceDetectSubject.next(value || {});
|
|
52
|
-
};
|
|
53
|
-
this.getBalanceDetectCache(updateBalanceDetectCache);
|
|
54
|
-
this.detectAccountBalanceStore.getSubject().subscribe({
|
|
55
|
-
next: updateBalanceDetectCache
|
|
56
|
-
});
|
|
57
|
-
this.startDetectBalance().catch(console.error);
|
|
45
|
+
this.balanceMap = new _BalanceMapImpl.BalanceMapImpl();
|
|
58
46
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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;
|
|
69
131
|
}
|
|
70
|
-
|
|
71
|
-
|
|
132
|
+
}
|
|
133
|
+
events.forEach(event => {
|
|
134
|
+
if (event.type === 'account.remove') {
|
|
135
|
+
removedAddresses.push(event.data[0]);
|
|
136
|
+
lazyTime = 1000;
|
|
72
137
|
}
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
|
+
}
|
|
79
150
|
}
|
|
80
151
|
getBalanceDetectCache(update) {
|
|
81
152
|
this.detectAccountBalanceStore.get('DetectBalanceCache', value => {
|
|
@@ -94,7 +165,7 @@ class BalanceService {
|
|
|
94
165
|
});
|
|
95
166
|
}
|
|
96
167
|
|
|
97
|
-
|
|
168
|
+
/** Subscribe token free balance of a address on chain */
|
|
98
169
|
async subscribeTokenFreeBalance(address, chain, tokenSlug, callback) {
|
|
99
170
|
const chainInfo = this.state.chainService.getChainInfoByKey(chain);
|
|
100
171
|
const chainState = this.state.chainService.getChainStateByKey(chain);
|
|
@@ -116,7 +187,12 @@ class BalanceService {
|
|
|
116
187
|
}
|
|
117
188
|
return new Promise((resolve, reject) => {
|
|
118
189
|
let hasError = true;
|
|
119
|
-
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];
|
|
120
196
|
if (rs.tokenSlug === tSlug) {
|
|
121
197
|
hasError = false;
|
|
122
198
|
const balance = {
|
|
@@ -156,45 +232,131 @@ class BalanceService {
|
|
|
156
232
|
const [, balance] = await this.subscribeTokenFreeBalance(address, chain, tokenSlug);
|
|
157
233
|
return balance;
|
|
158
234
|
}
|
|
159
|
-
subscribeBalance(addresses, chains, _callback) {
|
|
160
|
-
const [substrateAddresses, evmAddresses] = (0, _utils2.categoryAddresses)(addresses);
|
|
161
|
-
const chainInfoMap = this.state.chainService.getChainInfoMap();
|
|
162
|
-
const chainStateMap = this.state.chainService.getChainStateMap();
|
|
163
|
-
const substrateApiMap = this.state.chainService.getSubstrateApiMap();
|
|
164
|
-
const evmApiMap = this.state.chainService.getEvmApiMap();
|
|
165
235
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
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
|
|
174
253
|
};
|
|
254
|
+
}
|
|
175
255
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
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);
|
|
186
281
|
}
|
|
187
|
-
|
|
188
|
-
|
|
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();
|
|
323
|
+
const chainInfoMap = this.state.chainService.getChainInfoMap();
|
|
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);
|
|
189
334
|
});
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
});
|
|
335
|
+
const unsub2 = this.state.subscribeMantaPayBalance();
|
|
336
|
+
this._unsubscribeBalance = () => {
|
|
337
|
+
cancel = true;
|
|
338
|
+
unsub && unsub();
|
|
339
|
+
unsub2 && unsub2();
|
|
196
340
|
};
|
|
197
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
|
+
}
|
|
357
|
+
|
|
358
|
+
/** Subscribe area */
|
|
359
|
+
|
|
198
360
|
async autoEnableChains(addresses) {
|
|
199
361
|
this.setBalanceDetectCache(addresses);
|
|
200
362
|
const assetMap = this.state.chainService.getAssetRegistry();
|
|
@@ -263,5 +425,45 @@ class BalanceService {
|
|
|
263
425
|
});
|
|
264
426
|
}
|
|
265
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 || {});
|
|
437
|
+
};
|
|
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
|
+
}
|
|
452
|
+
}
|
|
453
|
+
if (needDetectAddresses.length) {
|
|
454
|
+
this.autoEnableChains(needDetectAddresses).finally(_util.noop);
|
|
455
|
+
}
|
|
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;
|
|
467
|
+
}
|
|
266
468
|
}
|
|
267
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(),
|
|
@@ -286,13 +286,7 @@ class ChainService {
|
|
|
286
286
|
return destinationTokenInfo;
|
|
287
287
|
}
|
|
288
288
|
getAssetByChainAndType(chainSlug, assetTypes) {
|
|
289
|
-
|
|
290
|
-
Object.values(this.getAssetRegistry()).forEach(assetInfo => {
|
|
291
|
-
if (assetTypes.includes(assetInfo.assetType) && assetInfo.originChain === chainSlug) {
|
|
292
|
-
result[assetInfo.slug] = assetInfo;
|
|
293
|
-
}
|
|
294
|
-
});
|
|
295
|
-
return result;
|
|
289
|
+
return (0, _utils2.filterAssetsByChainAndType)(this.getAssetRegistry(), chainSlug, assetTypes);
|
|
296
290
|
}
|
|
297
291
|
getSmartContractNfts() {
|
|
298
292
|
const result = [];
|
|
@@ -578,8 +572,6 @@ class ChainService {
|
|
|
578
572
|
const chainState = chainStateMap[originChain];
|
|
579
573
|
if (!assetState) {
|
|
580
574
|
// If this asset not has asset setting, this token is not enabled before (not turned off before)
|
|
581
|
-
// @ts-ignore
|
|
582
|
-
// TODO: Merge issue detect balance to define manualTurnOff props
|
|
583
575
|
if (!chainState || !chainState.manualTurnOff) {
|
|
584
576
|
await this.updateAssetSetting(assetSlug, {
|
|
585
577
|
visible: true
|
|
@@ -12,7 +12,7 @@ var _crossFetch = _interopRequireDefault(require("cross-fetch"));
|
|
|
12
12
|
const PRODUCTION_BRANCHES = ['master', 'webapp', 'webapp-dev'];
|
|
13
13
|
const branchName = process.env.BRANCH_NAME || 'subwallet-dev';
|
|
14
14
|
const fetchDomain = PRODUCTION_BRANCHES.indexOf(branchName) > -1 ? 'https://chain-list-assets.subwallet.app' : 'https://dev.sw-chain-list-assets.pages.dev';
|
|
15
|
-
const ChainListVersion = '0.2.
|
|
15
|
+
const ChainListVersion = '0.2.54';
|
|
16
16
|
async function fetchPatchData(slug) {
|
|
17
17
|
try {
|
|
18
18
|
const fetchPromise = (0, _crossFetch.default)(`${fetchDomain}/patch/${ChainListVersion}/${slug}`);
|
|
@@ -201,7 +201,7 @@ class StellaSwapLiquidStakingPoolHandler extends _base.default {
|
|
|
201
201
|
const derivativeTokenInfo = this.state.getAssetBySlug(derivativeTokenSlug);
|
|
202
202
|
const inputTokenSlug = this.inputAsset;
|
|
203
203
|
const inputTokenInfo = this.state.getAssetBySlug(inputTokenSlug);
|
|
204
|
-
const inputTokenContract = (0, _web.getERC20Contract)(
|
|
204
|
+
const inputTokenContract = (0, _web.getERC20Contract)((0, _utils._getContractAddressOfToken)(inputTokenInfo), evmApi);
|
|
205
205
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment
|
|
206
206
|
const allowanceCall = inputTokenContract.methods.allowance(params.address, (0, _utils._getContractAddressOfToken)(derivativeTokenInfo));
|
|
207
207
|
const [allowance, gasPrice] = await Promise.all([
|
|
@@ -266,7 +266,7 @@ class StellaSwapLiquidStakingPoolHandler extends _base.default {
|
|
|
266
266
|
const derivativeTokenInfo = this.state.getAssetBySlug(this.derivativeAssets[0]);
|
|
267
267
|
const derivativeTokenContractAddress = (0, _utils._getContractAddressOfToken)(derivativeTokenInfo);
|
|
268
268
|
const evmApi = this.evmApi;
|
|
269
|
-
const inputTokenContract = (0, _web.getERC20Contract)(
|
|
269
|
+
const inputTokenContract = (0, _web.getERC20Contract)((0, _utils._getContractAddressOfToken)(inputTokenInfo), evmApi);
|
|
270
270
|
|
|
271
271
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment
|
|
272
272
|
const approveCall = inputTokenContract.methods.approve(derivativeTokenContractAddress, MAX_INT); // TODO: need test
|
|
@@ -15,6 +15,7 @@ var _NominatorMetadata = _interopRequireDefault(require("@subwallet/extension-ba
|
|
|
15
15
|
var _YieldPoolStore = _interopRequireDefault(require("@subwallet/extension-base/services/storage-service/db-stores/YieldPoolStore"));
|
|
16
16
|
var _YieldPositionStore = _interopRequireDefault(require("@subwallet/extension-base/services/storage-service/db-stores/YieldPositionStore"));
|
|
17
17
|
var _utils = require("@subwallet/extension-base/utils");
|
|
18
|
+
var _uiKeyring = _interopRequireDefault(require("@subwallet/ui-keyring"));
|
|
18
19
|
var _dexieExportImport = require("dexie-export-import");
|
|
19
20
|
var _util = require("@polkadot/util");
|
|
20
21
|
// Copyright 2019-2022 @subwallet/extension-base authors & contributors
|
|
@@ -71,7 +72,15 @@ class DatabaseService {
|
|
|
71
72
|
|
|
72
73
|
// Balance
|
|
73
74
|
async getStoredBalance() {
|
|
74
|
-
|
|
75
|
+
const addresses = _uiKeyring.default.getPairs().map(_ref => {
|
|
76
|
+
let {
|
|
77
|
+
address
|
|
78
|
+
} = _ref;
|
|
79
|
+
return address;
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// Filter not exist address
|
|
83
|
+
return this.stores.balance.table.filter(obj => addresses.includes(obj.address)).toArray();
|
|
75
84
|
}
|
|
76
85
|
async updateBalanceStore(item) {
|
|
77
86
|
if (item.state === _KoniTypes.APIItemState.READY) {
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.filterAssetsByChainAndType = void 0;
|
|
7
|
+
// Copyright 2019-2022 @subwallet/extension-base
|
|
8
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
9
|
+
|
|
10
|
+
const filterAssetsByChainAndType = (chainAssetMap, chain, assetTypes) => {
|
|
11
|
+
const result = {};
|
|
12
|
+
Object.values(chainAssetMap).forEach(assetInfo => {
|
|
13
|
+
if (assetTypes.includes(assetInfo.assetType) && assetInfo.originChain === chain) {
|
|
14
|
+
result[assetInfo.slug] = assetInfo;
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
return result;
|
|
18
|
+
};
|
|
19
|
+
exports.filterAssetsByChainAndType = filterAssetsByChainAndType;
|
package/cjs/utils/index.js
CHANGED
|
@@ -104,6 +104,18 @@ Object.keys(_array).forEach(function (key) {
|
|
|
104
104
|
}
|
|
105
105
|
});
|
|
106
106
|
});
|
|
107
|
+
var _asset = require("./asset");
|
|
108
|
+
Object.keys(_asset).forEach(function (key) {
|
|
109
|
+
if (key === "default" || key === "__esModule") return;
|
|
110
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
111
|
+
if (key in exports && exports[key] === _asset[key]) return;
|
|
112
|
+
Object.defineProperty(exports, key, {
|
|
113
|
+
enumerable: true,
|
|
114
|
+
get: function () {
|
|
115
|
+
return _asset[key];
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
});
|
|
107
119
|
var _environment = require("./environment");
|
|
108
120
|
Object.keys(_environment).forEach(function (key) {
|
|
109
121
|
if (key === "default" || key === "__esModule") return;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { _EvmApi } from '@subwallet/extension-base/services/chain-service/types';
|
|
2
|
-
export declare function getEVMBalance(networkKey: string, addresses: string[],
|
|
2
|
+
export declare function getEVMBalance(networkKey: string, addresses: string[], web3Api: _EvmApi): Promise<string[]>;
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
// Copyright 2019-2022 @subwallet/extension-base
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
|
-
export async function getEVMBalance(networkKey, addresses,
|
|
5
|
-
const web3Api = evmApiMap[networkKey];
|
|
4
|
+
export async function getEVMBalance(networkKey, addresses, web3Api) {
|
|
6
5
|
return await Promise.all(addresses.map(async address => {
|
|
7
6
|
try {
|
|
8
7
|
return await web3Api.api.eth.getBalance(address);
|
|
@@ -11,7 +11,7 @@ interface HandleTransferBalanceResultProps {
|
|
|
11
11
|
updateState?: (promise: Partial<ExternalRequestPromise>) => void;
|
|
12
12
|
}
|
|
13
13
|
export declare const handleTransferBalanceResult: ({ callback, changeValue, networkKey, receipt, response, updateState }: HandleTransferBalanceResultProps) => void;
|
|
14
|
-
export declare function getEVMTransactionObject(chainInfo: _ChainInfo, from: string, to: string, value: string, transferAll: boolean,
|
|
15
|
-
export declare function getERC20TransactionObject(assetAddress: string, chainInfo: _ChainInfo, from: string, to: string, value: string, transferAll: boolean,
|
|
14
|
+
export declare function getEVMTransactionObject(chainInfo: _ChainInfo, from: string, to: string, value: string, transferAll: boolean, web3Api: _EvmApi): Promise<[TransactionConfig, string]>;
|
|
15
|
+
export declare function getERC20TransactionObject(assetAddress: string, chainInfo: _ChainInfo, from: string, to: string, value: string, transferAll: boolean, evmApi: _EvmApi): Promise<[TransactionConfig, string]>;
|
|
16
16
|
export declare function getERC721Transaction(web3Api: _EvmApi, chain: string, contractAddress: string, senderAddress: string, recipientAddress: string, tokenId: string): Promise<TransactionConfig>;
|
|
17
17
|
export {};
|
|
@@ -34,10 +34,9 @@ export const handleTransferBalanceResult = ({
|
|
|
34
34
|
});
|
|
35
35
|
callback(response);
|
|
36
36
|
};
|
|
37
|
-
export async function getEVMTransactionObject(chainInfo, from, to, value, transferAll,
|
|
37
|
+
export async function getEVMTransactionObject(chainInfo, from, to, value, transferAll, web3Api) {
|
|
38
38
|
var _priority$maxFeePerGa, _priority$maxPriority;
|
|
39
39
|
const networkKey = chainInfo.slug;
|
|
40
|
-
const web3Api = evmApiMap[networkKey];
|
|
41
40
|
const priority = await calculateGasFeeParams(web3Api, networkKey);
|
|
42
41
|
const transactionObject = {
|
|
43
42
|
to: to,
|
|
@@ -63,11 +62,10 @@ export async function getEVMTransactionObject(chainInfo, from, to, value, transf
|
|
|
63
62
|
}
|
|
64
63
|
return [transactionObject, transactionObject.value.toString()];
|
|
65
64
|
}
|
|
66
|
-
export async function getERC20TransactionObject(assetAddress, chainInfo, from, to, value, transferAll,
|
|
65
|
+
export async function getERC20TransactionObject(assetAddress, chainInfo, from, to, value, transferAll, evmApi) {
|
|
67
66
|
var _priority$maxFeePerGa2, _priority$maxPriority2;
|
|
68
67
|
const networkKey = chainInfo.slug;
|
|
69
|
-
const
|
|
70
|
-
const erc20Contract = getERC20Contract(networkKey, assetAddress, evmApiMap);
|
|
68
|
+
const erc20Contract = getERC20Contract(assetAddress, evmApi);
|
|
71
69
|
let freeAmount = new BigN(0);
|
|
72
70
|
let transferValue = value;
|
|
73
71
|
if (transferAll) {
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { _EvmApi } from '@subwallet/extension-base/services/chain-service/types';
|
|
2
2
|
import { Contract } from 'web3-eth-contract';
|
|
3
|
-
export declare const getERC20Contract: (
|
|
3
|
+
export declare const getERC20Contract: (assetAddress: string, evmApi: _EvmApi, options?: {}) => Contract;
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
4
|
import { _ERC20_ABI } from '@subwallet/extension-base/services/chain-service/helper';
|
|
5
|
-
export const getERC20Contract = (
|
|
5
|
+
export const getERC20Contract = (assetAddress, evmApi, options = {}) => {
|
|
6
6
|
// @ts-ignore
|
|
7
7
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument,@typescript-eslint/no-unsafe-member-access
|
|
8
|
-
return new
|
|
8
|
+
return new evmApi.api.eth.Contract(_ERC20_ABI, assetAddress, options);
|
|
9
9
|
};
|