@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.
Files changed (75) hide show
  1. package/background/KoniTypes.d.ts +1 -0
  2. package/cjs/koni/api/tokens/evm/balance.js +1 -2
  3. package/cjs/koni/api/tokens/evm/transfer.js +3 -5
  4. package/cjs/koni/api/tokens/evm/web3.js +3 -3
  5. package/cjs/koni/background/handlers/Extension.js +5 -4
  6. package/cjs/koni/background/handlers/State.js +21 -122
  7. package/cjs/koni/background/subscription.js +3 -63
  8. package/cjs/packageInfo.js +1 -1
  9. package/cjs/services/balance-service/BalanceMapImpl.js +2 -2
  10. package/cjs/services/balance-service/helpers/index.js +27 -0
  11. package/cjs/services/balance-service/helpers/subscribe/evm.js +33 -15
  12. package/cjs/services/balance-service/helpers/subscribe/{balance.js → index.js} +46 -29
  13. package/cjs/services/balance-service/helpers/subscribe/substrate/equilibrium.js +34 -15
  14. package/cjs/services/balance-service/helpers/subscribe/substrate/index.js +156 -72
  15. package/cjs/services/balance-service/index.js +371 -44
  16. package/cjs/services/chain-service/constants.js +2 -1
  17. package/cjs/services/chain-service/health-check/utils/asset-info.js +1 -3
  18. package/cjs/services/chain-service/index.js +60 -26
  19. package/cjs/services/chain-service/utils/index.js +10 -2
  20. package/cjs/services/chain-service/utils/patch.js +1 -1
  21. package/cjs/services/earning-service/handlers/liquid-staking/stella-swap.js +2 -2
  22. package/cjs/services/earning-service/handlers/native-staking/para-chain.js +1 -1
  23. package/cjs/services/migration-service/scripts/AutoEnableChainsTokens.js +1 -1
  24. package/cjs/services/storage-service/DatabaseService.js +10 -1
  25. package/cjs/stores/DetectAccountBalance.js +18 -0
  26. package/cjs/utils/asset.js +19 -0
  27. package/cjs/utils/index.js +12 -0
  28. package/koni/api/tokens/evm/balance.d.ts +1 -1
  29. package/koni/api/tokens/evm/balance.js +1 -2
  30. package/koni/api/tokens/evm/transfer.d.ts +2 -2
  31. package/koni/api/tokens/evm/transfer.js +3 -5
  32. package/koni/api/tokens/evm/web3.d.ts +1 -1
  33. package/koni/api/tokens/evm/web3.js +2 -2
  34. package/koni/background/handlers/Extension.js +5 -4
  35. package/koni/background/handlers/State.d.ts +2 -13
  36. package/koni/background/handlers/State.js +21 -121
  37. package/koni/background/subscription.d.ts +1 -5
  38. package/koni/background/subscription.js +1 -60
  39. package/package.json +28 -13
  40. package/packageInfo.js +1 -1
  41. package/services/balance-service/BalanceMapImpl.js +1 -1
  42. package/services/balance-service/helpers/index.d.ts +2 -0
  43. package/services/balance-service/helpers/index.js +5 -0
  44. package/services/balance-service/helpers/subscribe/evm.d.ts +3 -5
  45. package/services/balance-service/helpers/subscribe/evm.js +31 -14
  46. package/services/balance-service/helpers/subscribe/{balance.d.ts → index.d.ts} +2 -2
  47. package/services/balance-service/helpers/subscribe/{balance.js → index.js} +41 -28
  48. package/services/balance-service/helpers/subscribe/substrate/equilibrium.d.ts +3 -4
  49. package/services/balance-service/helpers/subscribe/substrate/equilibrium.js +29 -13
  50. package/services/balance-service/helpers/subscribe/substrate/index.d.ts +2 -2
  51. package/services/balance-service/helpers/subscribe/substrate/index.js +143 -67
  52. package/services/balance-service/index.d.ts +71 -4
  53. package/services/balance-service/index.js +372 -46
  54. package/services/chain-service/constants.js +2 -1
  55. package/services/chain-service/health-check/utils/asset-info.js +1 -3
  56. package/services/chain-service/index.d.ts +2 -1
  57. package/services/chain-service/index.js +61 -27
  58. package/services/chain-service/types.d.ts +1 -0
  59. package/services/chain-service/utils/index.js +10 -2
  60. package/services/chain-service/utils/patch.js +1 -1
  61. package/services/earning-service/handlers/liquid-staking/stella-swap.js +2 -2
  62. package/services/earning-service/handlers/native-staking/para-chain.js +1 -1
  63. package/services/migration-service/scripts/AutoEnableChainsTokens.js +1 -1
  64. package/services/storage-service/DatabaseService.js +7 -1
  65. package/services/storage-service/databases/index.d.ts +1 -0
  66. package/stores/DetectAccountBalance.d.ts +5 -0
  67. package/stores/DetectAccountBalance.js +10 -0
  68. package/types/{balance.d.ts → balance/index.d.ts} +16 -0
  69. package/types/index.js +1 -1
  70. package/utils/asset.d.ts +2 -0
  71. package/utils/asset.js +12 -0
  72. package/utils/index.d.ts +1 -0
  73. package/utils/index.js +1 -0
  74. /package/cjs/types/{balance.js → balance/index.js} +0 -0
  75. /package/types/{balance.js → balance/index.js} +0 -0
@@ -7,7 +7,6 @@ import { isSubscriptionRunning, unsubscribe } from '@subwallet/extension-base/ba
7
7
  import { APIItemState, BasicTxErrorType, ChainType, EvmProviderErrorType, ExternalRequestPromiseStatus, ExtrinsicType } from '@subwallet/extension-base/background/KoniTypes';
8
8
  import { ALL_ACCOUNT_KEY, ALL_GENESIS_HASH, MANTA_PAY_BALANCE_INTERVAL } from '@subwallet/extension-base/constants';
9
9
  import { BalanceService } from '@subwallet/extension-base/services/balance-service';
10
- import { BalanceMapImpl } from '@subwallet/extension-base/services/balance-service/BalanceMapImpl';
11
10
  import { ServiceStatus } from '@subwallet/extension-base/services/base/types';
12
11
  import BuyService from '@subwallet/extension-base/services/buy-service';
13
12
  import CampaignService from '@subwallet/extension-base/services/campaign-service';
@@ -28,11 +27,11 @@ import RequestService from '@subwallet/extension-base/services/request-service';
28
27
  import SettingService from '@subwallet/extension-base/services/setting-service/SettingService';
29
28
  import DatabaseService from '@subwallet/extension-base/services/storage-service/DatabaseService';
30
29
  import { SubscanService } from '@subwallet/extension-base/services/subscan-service';
31
- import { SUBSCAN_API_CHAIN_MAP, SUBSCAN_BALANCE_CHAIN_MAP_REVERSE } from '@subwallet/extension-base/services/subscan-service/subscan-chain-map';
30
+ import { SUBSCAN_API_CHAIN_MAP } from '@subwallet/extension-base/services/subscan-service/subscan-chain-map';
32
31
  import TransactionService from '@subwallet/extension-base/services/transaction-service';
33
32
  import WalletConnectService from '@subwallet/extension-base/services/wallet-connect-service';
34
33
  import AccountRefStore from '@subwallet/extension-base/stores/AccountRef';
35
- import { addLazy, isAccountAll, stripUrl, TARGET_ENV } from '@subwallet/extension-base/utils';
34
+ import { isAccountAll, stripUrl, TARGET_ENV } from '@subwallet/extension-base/utils';
36
35
  import { isContractAddress, parseContractInput } from '@subwallet/extension-base/utils/eth/parseTransaction';
37
36
  import { createPromiseHandler } from '@subwallet/extension-base/utils/promise';
38
37
  import { decodePair } from '@subwallet/keyring/pair/decode';
@@ -66,7 +65,6 @@ export default class KoniState {
66
65
  unsubscriptionMap = {};
67
66
  accountRefStore = new AccountRefStore();
68
67
  externalRequest = {};
69
- balanceMap = new BalanceMapImpl();
70
68
  crowdloanMap = generateDefaultCrowdloanMap();
71
69
  crowdloanSubject = new Subject();
72
70
  nftSubject = new Subject();
@@ -226,6 +224,7 @@ export default class KoniState {
226
224
  await this.migrationService.run();
227
225
  this.campaignService.init();
228
226
  this.eventService.emit('chain.ready', true);
227
+ await this.balanceService.init();
229
228
  await this.earningService.init();
230
229
  this.onReady();
231
230
  this.onAccountAdd();
@@ -675,35 +674,6 @@ export default class KoniState {
675
674
  getAllAddresses() {
676
675
  return keyring.getAccounts().map(account => account.address);
677
676
  }
678
- async removeInactiveChainBalances() {
679
- const assetSettings = await this.chainService.getAssetSettings();
680
- this.balanceMap.removeBalanceItemByFilter(item => {
681
- return !assetSettings[item.tokenSlug];
682
- });
683
- }
684
- async getBalance(reset) {
685
- await this.removeInactiveChainBalances();
686
- return {
687
- details: this.balanceMap.map,
688
- reset
689
- };
690
- }
691
- async getStoredBalance(address) {
692
- return await this.dbService.stores.balance.getBalanceMapByAddresses(address);
693
- }
694
- isFirstLoad = true;
695
- async handleResetBalance(newAddress, forceRefresh) {
696
- if (this.isFirstLoad) {
697
- const backupBalanceData = await this.dbService.getStoredBalance();
698
- this.balanceMap.updateBalanceItems(backupBalanceData, isAccountAll(newAddress));
699
- this.isFirstLoad = false;
700
- }
701
- if (forceRefresh) {
702
- this.balanceMap.setData({});
703
- await this.dbService.stores.balance.clear();
704
- }
705
- await Promise.all([this.removeInactiveChainBalances()]);
706
- }
707
677
  async resetCrowdloanMap(newAddress) {
708
678
  const defaultData = generateDefaultCrowdloanMap();
709
679
  const storedData = await this.getStoredCrowdloan(newAddress);
@@ -725,36 +695,6 @@ export default class KoniState {
725
695
  });
726
696
  });
727
697
  }
728
- balanceUpdateCache = [];
729
-
730
- /** Note: items must be same tokenSlug */
731
- setBalanceItem(items) {
732
- if (items.length) {
733
- const nowTime = new Date().getTime();
734
- for (const item of items) {
735
- const balance = {
736
- timestamp: nowTime,
737
- ...item
738
- };
739
- this.balanceUpdateCache.push(balance);
740
- }
741
- addLazy('updateBalanceStore', () => {
742
- const isAllAccount = isAccountAll(this.keyringService.currentAccount.address);
743
- this.balanceMap.updateBalanceItems(this.balanceUpdateCache, isAllAccount);
744
- if (isAllAccount) {
745
- this.balanceUpdateCache = [...this.balanceUpdateCache, ...Object.values(this.balanceMap.map[ALL_ACCOUNT_KEY])];
746
- }
747
- this.updateBalanceStore(this.balanceUpdateCache);
748
- this.balanceUpdateCache = [];
749
- }, 300, 1800);
750
- }
751
- }
752
- updateBalanceStore(items) {
753
- this.dbService.updateBulkBalanceStore(items).catch(e => this.logger.warn(e));
754
- }
755
- subscribeBalance() {
756
- return this.balanceMap.mapSubject;
757
- }
758
698
  getCrowdloan(reset) {
759
699
  return {
760
700
  details: this.crowdloanMap,
@@ -1456,7 +1396,7 @@ export default class KoniState {
1456
1396
  // Stopping services
1457
1397
  await Promise.all([this.cron.stop(), this.subscription.stop()]);
1458
1398
  await this.pauseAllNetworks(undefined, 'IDLE mode');
1459
- await Promise.all([this.historyService.stop(), this.priceService.stop(), this.earningService.stop()]);
1399
+ await Promise.all([this.historyService.stop(), this.priceService.stop(), this.balanceService.stop(), this.earningService.stop()]);
1460
1400
 
1461
1401
  // Complete sleeping
1462
1402
  sleeping.resolve();
@@ -1488,7 +1428,7 @@ export default class KoniState {
1488
1428
  }
1489
1429
 
1490
1430
  // Start services
1491
- await Promise.all([this.cron.start(), this.subscription.start(), this.historyService.start(), this.priceService.start(), this.earningService.start()]);
1431
+ await Promise.all([this.cron.start(), this.subscription.start(), this.historyService.start(), this.priceService.start(), this.balanceService.start(), this.earningService.start()]);
1492
1432
 
1493
1433
  // Complete starting
1494
1434
  starting.resolve();
@@ -1511,58 +1451,21 @@ export default class KoniState {
1511
1451
  createUnsubscriptionHandle(id, unsubscribe) {
1512
1452
  this.unsubscriptionMap[id] = unsubscribe;
1513
1453
  }
1514
- async autoEnableChains(addresses) {
1515
- const assetMap = this.chainService.getAssetRegistry();
1516
- const promiseList = addresses.map(address => {
1517
- return this.subscanService.getMultiChainBalance(address).catch(e => {
1518
- console.error(e);
1519
- return null;
1520
- });
1521
- });
1522
- const needEnableChains = [];
1523
- const needActiveTokens = [];
1524
- const currentAssetSettings = await this.chainService.getAssetSettings();
1525
- const chainMap = this.chainService.getChainInfoMap();
1526
- const balanceDataList = await Promise.all(promiseList);
1527
- balanceDataList.forEach(balanceData => {
1528
- balanceData && balanceData.forEach(({
1529
- balance,
1530
- bonded,
1531
- category,
1532
- locked,
1533
- network,
1534
- symbol
1535
- }) => {
1536
- var _currentAssetSettings;
1537
- const chain = SUBSCAN_BALANCE_CHAIN_MAP_REVERSE[network];
1538
- const chainInfo = chain ? chainMap[chain] : null;
1539
- const balanceIsEmpty = (!balance || balance === '0') && (!locked || locked === '0') && (!bonded || bonded === '0');
1540
-
1541
- // Cancel if chain is not supported or is testnet or balance is 0
1542
- if (!chainInfo || chainInfo.isTestnet || balanceIsEmpty) {
1543
- return;
1544
- }
1545
- const tokenKey = `${chain}-${category === 'native' ? 'NATIVE' : 'LOCAL'}-${symbol.toUpperCase()}`;
1546
- const existedKey = Object.keys(assetMap).find(v => v.toLowerCase() === tokenKey.toLowerCase());
1547
- if (existedKey && !((_currentAssetSettings = currentAssetSettings[existedKey]) !== null && _currentAssetSettings !== void 0 && _currentAssetSettings.visible)) {
1548
- needEnableChains.push(chain);
1549
- needActiveTokens.push(existedKey);
1550
- currentAssetSettings[existedKey] = {
1551
- visible: true
1552
- };
1553
- }
1554
- });
1555
- });
1556
- if (needActiveTokens.length) {
1557
- await this.chainService.enableChains(needEnableChains);
1558
- this.chainService.setAssetSettings({
1559
- ...currentAssetSettings
1560
- });
1454
+ get detectBalanceChainSlugMap() {
1455
+ const result = {};
1456
+ const chainInfoMap = this.getChainInfoMap();
1457
+ for (const [key, chainInfo] of Object.entries(chainInfoMap)) {
1458
+ var _chainInfo$extraInfo;
1459
+ const chainBalanceSlug = ((_chainInfo$extraInfo = chainInfo.extraInfo) === null || _chainInfo$extraInfo === void 0 ? void 0 : _chainInfo$extraInfo.chainBalanceSlug) || '';
1460
+ if (chainBalanceSlug) {
1461
+ result[chainBalanceSlug] = key;
1462
+ }
1561
1463
  }
1464
+ return result;
1562
1465
  }
1563
1466
  onAccountAdd() {
1564
1467
  this.eventService.on('account.add', address => {
1565
- this.autoEnableChains([address]).catch(this.logger.error);
1468
+ this.balanceService.autoEnableChains([address]).catch(this.logger.error);
1566
1469
  });
1567
1470
  }
1568
1471
  onAccountRemove() {
@@ -1571,11 +1474,6 @@ export default class KoniState {
1571
1474
 
1572
1475
  const stores = this.dbService.stores;
1573
1476
 
1574
- // Remove Balance
1575
- stores.balance.removeAllByAddress(address).catch(console.error);
1576
- stores.balance.removeAllByAddress(ALL_ACCOUNT_KEY).catch(console.error);
1577
- this.balanceMap.removeBalanceItems([address, ALL_ACCOUNT_KEY]);
1578
-
1579
1477
  // Remove NFT
1580
1478
  stores.nft.deleteNftByAddress([address]).catch(console.error);
1581
1479
 
@@ -1593,7 +1491,7 @@ export default class KoniState {
1593
1491
  return true;
1594
1492
  }
1595
1493
  async reloadBalance() {
1596
- await this.subscription.reloadBalance();
1494
+ await this.balanceService.reloadBalance();
1597
1495
  return true;
1598
1496
  }
1599
1497
  async reloadCrowdloan() {
@@ -1618,9 +1516,11 @@ export default class KoniState {
1618
1516
  }
1619
1517
  async resetWallet(resetAll) {
1620
1518
  await this.keyringService.resetWallet(resetAll);
1519
+ await this.earningService.resetYieldPosition();
1520
+ await this.balanceService.handleResetBalance(true);
1621
1521
  this.requestService.resetWallet();
1622
1522
  this.transactionService.resetWallet();
1623
- await this.handleResetBalance(ALL_ACCOUNT_KEY, true);
1523
+ // await this.handleResetBalance(ALL_ACCOUNT_KEY, true);
1624
1524
  await this.earningService.resetWallet();
1625
1525
  await this.dbService.resetWallet(resetAll);
1626
1526
  this.accountRefStore.set('refList', []);
@@ -1733,7 +1633,7 @@ export default class KoniState {
1733
1633
  };
1734
1634
  balanceItem.free = ((_zkBalances$i = zkBalances[i]) === null || _zkBalances$i === void 0 ? void 0 : _zkBalances$i.toString()) || '0';
1735
1635
  balanceItem.state = APIItemState.READY;
1736
- this.setBalanceItem([balanceItem]);
1636
+ this.balanceService.setBalanceItem([balanceItem]);
1737
1637
  }
1738
1638
  }).catch(console.warn);
1739
1639
  }
@@ -1,5 +1,5 @@
1
1
  import { _ChainAsset, _ChainInfo } from '@subwallet/chain-list/types';
2
- import { _ChainState, _EvmApi, _SubstrateApi } from '@subwallet/extension-base/services/chain-service/types';
2
+ import { _EvmApi, _SubstrateApi } from '@subwallet/extension-base/services/chain-service/types';
3
3
  import DatabaseService from '@subwallet/extension-base/services/storage-service/DatabaseService';
4
4
  import KoniState from './handlers/State';
5
5
  declare type SubscriptionName = 'balance' | 'crowdloan' | 'yieldPoolStats' | 'yieldPosition';
@@ -16,14 +16,10 @@ export declare class KoniSubscription {
16
16
  stopAllSubscription(): void;
17
17
  start(): Promise<void>;
18
18
  stop(): Promise<void>;
19
- subscribeBalances(address: string, chainInfoMap: Record<string, _ChainInfo>, chainStateMap: Record<string, _ChainState>, substrateApiMap: Record<string, _SubstrateApi>, web3ApiMap: Record<string, _EvmApi>, onlyRunOnFirstTime?: boolean): void;
20
19
  subscribeCrowdloans(address: string, substrateApiMap: Record<string, _SubstrateApi>, onlyRunOnFirstTime?: boolean): void;
21
- initBalanceSubscription(addresses: string[], chainInfoMap: Record<string, _ChainInfo>, chainStateMap: Record<string, _ChainState>, substrateApiMap: Record<string, _SubstrateApi>, evmApiMap: Record<string, _EvmApi>, onlyRunOnFirstTime?: boolean): (() => void) | undefined;
22
20
  initCrowdloanSubscription(addresses: string[], substrateApiMap: Record<string, _SubstrateApi>, onlyRunOnFirstTime?: boolean): (() => void) | undefined;
23
21
  subscribeNft(address: string, substrateApiMap: Record<string, _SubstrateApi>, evmApiMap: Record<string, _EvmApi>, smartContractNfts: _ChainAsset[], chainInfoMap: Record<string, _ChainInfo>): void;
24
22
  initNftSubscription(addresses: string[], substrateApiMap: Record<string, _SubstrateApi>, evmApiMap: Record<string, _EvmApi>, smartContractNfts: _ChainAsset[], chainInfoMap: Record<string, _ChainInfo>): void;
25
- subscribeStakingReward(address: string): Promise<void>;
26
- reloadBalance(): Promise<void>;
27
23
  reloadCrowdloan(): Promise<void>;
28
24
  }
29
25
  export {};
@@ -2,10 +2,7 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
4
  import { subscribeCrowdloan } from '@subwallet/extension-base/koni/api/dotsama/crowdloan';
5
- import { getNominationStakingRewardData } from '@subwallet/extension-base/koni/api/staking';
6
5
  import { nftHandler } from '@subwallet/extension-base/koni/background/handlers';
7
- import { subscribeBalance } from '@subwallet/extension-base/services/balance-service/helpers/subscribe/balance';
8
- import { _isChainEnabled, _isChainSupportSubstrateStaking } from '@subwallet/extension-base/services/chain-service/utils';
9
6
  import { COMMON_RELOAD_EVENTS } from '@subwallet/extension-base/services/event-service/types';
10
7
  import { waitTimeout } from '@subwallet/extension-base/utils';
11
8
  import { logger as createLogger } from '@polkadot/util';
@@ -47,7 +44,6 @@ export class KoniSubscription {
47
44
  await Promise.all([this.state.eventService.waitCryptoReady, this.state.eventService.waitKeyringReady, this.state.eventService.waitAssetReady]);
48
45
  const currentAddress = (_this$state$keyringSe = this.state.keyringService.currentAccount) === null || _this$state$keyringSe === void 0 ? void 0 : _this$state$keyringSe.address;
49
46
  if (currentAddress) {
50
- this.subscribeBalances(currentAddress, this.state.getChainInfoMap(), this.state.getChainStateMap(), this.state.getSubstrateApiMap(), this.state.getEvmApiMap());
51
47
  this.subscribeCrowdloans(currentAddress, this.state.getSubstrateApiMap());
52
48
  }
53
49
  this.eventHandler = (events, eventTypes) => {
@@ -61,7 +57,6 @@ export class KoniSubscription {
61
57
  if (!address) {
62
58
  return;
63
59
  }
64
- this.subscribeBalances(address, serviceInfo.chainInfoMap, serviceInfo.chainStateMap, serviceInfo.chainApiMap.substrate, serviceInfo.chainApiMap.evm);
65
60
  this.subscribeCrowdloans(address, serviceInfo.chainApiMap.substrate);
66
61
  };
67
62
  this.state.eventService.onLazy(this.eventHandler.bind(this));
@@ -74,15 +69,6 @@ export class KoniSubscription {
74
69
  this.stopAllSubscription();
75
70
  return Promise.resolve();
76
71
  }
77
- subscribeBalances(address, chainInfoMap, chainStateMap, substrateApiMap, web3ApiMap, onlyRunOnFirstTime) {
78
- const addresses = this.state.getDecodedAddresses(address);
79
- if (!addresses.length) {
80
- return;
81
- }
82
- this.state.handleResetBalance(address).then(() => {
83
- this.updateSubscription('balance', this.initBalanceSubscription(addresses, chainInfoMap, chainStateMap, substrateApiMap, web3ApiMap, onlyRunOnFirstTime));
84
- }).catch(err => this.logger.warn(err));
85
- }
86
72
  subscribeCrowdloans(address, substrateApiMap, onlyRunOnFirstTime) {
87
73
  const addresses = this.state.getDecodedAddresses(address);
88
74
  if (!addresses.length) {
@@ -92,27 +78,6 @@ export class KoniSubscription {
92
78
  this.updateSubscription('crowdloan', this.initCrowdloanSubscription(addresses, substrateApiMap, onlyRunOnFirstTime));
93
79
  }).catch(console.error);
94
80
  }
95
- initBalanceSubscription(addresses, chainInfoMap, chainStateMap, substrateApiMap, evmApiMap, onlyRunOnFirstTime) {
96
- const filteredChainInfoMap = {};
97
- Object.values(chainStateMap).forEach(chainState => {
98
- if (chainState.active) {
99
- filteredChainInfoMap[chainState.slug] = chainInfoMap[chainState.slug];
100
- }
101
- });
102
- const unsub = subscribeBalance(addresses, filteredChainInfoMap, substrateApiMap, evmApiMap, result => {
103
- this.state.setBalanceItem(result);
104
- });
105
- const unsub2 = this.state.subscribeMantaPayBalance();
106
- if (onlyRunOnFirstTime) {
107
- unsub && unsub();
108
- unsub2 && unsub2();
109
- return;
110
- }
111
- return () => {
112
- unsub && unsub();
113
- unsub2 && unsub2();
114
- };
115
- }
116
81
  initCrowdloanSubscription(addresses, substrateApiMap, onlyRunOnFirstTime) {
117
82
  const subscriptionPromise = subscribeCrowdloan(addresses, substrateApiMap, (networkKey, rs) => {
118
83
  this.state.setCrowdloanItem(networkKey, rs);
@@ -139,33 +104,9 @@ export class KoniSubscription {
139
104
  nftHandler.setAddresses(addresses);
140
105
  nftHandler.handleNfts(smartContractNfts, (...args) => this.state.updateNftData(...args), (...args) => this.state.setNftCollection(...args)).catch(this.logger.log);
141
106
  }
142
- async subscribeStakingReward(address) {
143
- const addresses = this.state.getDecodedAddresses(address);
144
- if (!addresses.length) {
145
- return;
146
- }
147
- const chainInfoMap = this.state.getChainInfoMap();
148
- const targetNetworkMap = {};
149
- Object.entries(chainInfoMap).forEach(([key, network]) => {
150
- const chainState = this.state.getChainStateByKey(key);
151
- if (_isChainEnabled(chainState) && _isChainSupportSubstrateStaking(network)) {
152
- targetNetworkMap[key] = network;
153
- }
154
- });
155
- await getNominationStakingRewardData(addresses, targetNetworkMap, rewardItem => {
156
- this.state.updateStakingReward(rewardItem);
157
- });
158
- }
159
- async reloadBalance() {
107
+ async reloadCrowdloan() {
160
108
  var _this$state$keyringSe2;
161
109
  const currentAddress = (_this$state$keyringSe2 = this.state.keyringService.currentAccount) === null || _this$state$keyringSe2 === void 0 ? void 0 : _this$state$keyringSe2.address;
162
- await this.state.handleResetBalance(currentAddress, true);
163
- this.subscribeBalances(currentAddress, this.state.getChainInfoMap(), this.state.getChainStateMap(), this.state.getSubstrateApiMap(), this.state.getEvmApiMap());
164
- await waitTimeout(1800);
165
- }
166
- async reloadCrowdloan() {
167
- var _this$state$keyringSe3;
168
- const currentAddress = (_this$state$keyringSe3 = this.state.keyringService.currentAccount) === null || _this$state$keyringSe3 === void 0 ? void 0 : _this$state$keyringSe3.address;
169
110
  this.subscribeCrowdloans(currentAddress, this.state.getSubstrateApiMap());
170
111
  await waitTimeout(1800);
171
112
  }
package/package.json CHANGED
@@ -17,7 +17,7 @@
17
17
  "./cjs/detectPackage.js"
18
18
  ],
19
19
  "type": "module",
20
- "version": "1.1.51-1",
20
+ "version": "1.1.52-0",
21
21
  "main": "./cjs/index.js",
22
22
  "module": "./index.js",
23
23
  "types": "./index.d.ts",
@@ -535,15 +535,20 @@
535
535
  "require": "./cjs/services/balance-service/BalanceMapImpl.js",
536
536
  "default": "./services/balance-service/BalanceMapImpl.js"
537
537
  },
538
+ "./services/balance-service/helpers": {
539
+ "types": "./services/balance-service/helpers/index.d.ts",
540
+ "require": "./cjs/services/balance-service/helpers/index.js",
541
+ "default": "./services/balance-service/helpers/index.js"
542
+ },
538
543
  "./services/balance-service/helpers/group": {
539
544
  "types": "./services/balance-service/helpers/group.d.ts",
540
545
  "require": "./cjs/services/balance-service/helpers/group.js",
541
546
  "default": "./services/balance-service/helpers/group.js"
542
547
  },
543
- "./services/balance-service/helpers/subscribe/balance": {
544
- "types": "./services/balance-service/helpers/subscribe/balance.d.ts",
545
- "require": "./cjs/services/balance-service/helpers/subscribe/balance.js",
546
- "default": "./services/balance-service/helpers/subscribe/balance.js"
548
+ "./services/balance-service/helpers/subscribe": {
549
+ "types": "./services/balance-service/helpers/subscribe/index.d.ts",
550
+ "require": "./cjs/services/balance-service/helpers/subscribe/index.js",
551
+ "default": "./services/balance-service/helpers/subscribe/index.js"
547
552
  },
548
553
  "./services/balance-service/helpers/subscribe/evm": {
549
554
  "types": "./services/balance-service/helpers/subscribe/evm.d.ts",
@@ -1480,6 +1485,11 @@
1480
1485
  "require": "./cjs/stores/CurrentAccountStore.js",
1481
1486
  "default": "./stores/CurrentAccountStore.js"
1482
1487
  },
1488
+ "./stores/DetectAccountBalance": {
1489
+ "types": "./stores/DetectAccountBalance.d.ts",
1490
+ "require": "./cjs/stores/DetectAccountBalance.js",
1491
+ "default": "./stores/DetectAccountBalance.js"
1492
+ },
1483
1493
  "./stores/Keyring": {
1484
1494
  "types": "./stores/Keyring.d.ts",
1485
1495
  "require": "./cjs/stores/Keyring.js",
@@ -1526,9 +1536,9 @@
1526
1536
  "default": "./types/index.js"
1527
1537
  },
1528
1538
  "./types/balance": {
1529
- "types": "./types/balance.d.ts",
1530
- "require": "./cjs/types/balance.js",
1531
- "default": "./types/balance.js"
1539
+ "types": "./types/balance/index.d.ts",
1540
+ "require": "./cjs/types/balance/index.js",
1541
+ "default": "./types/balance/index.js"
1532
1542
  },
1533
1543
  "./types/buy": {
1534
1544
  "types": "./types/buy.d.ts",
@@ -1665,6 +1675,11 @@
1665
1675
  "require": "./cjs/utils/array.js",
1666
1676
  "default": "./utils/array.js"
1667
1677
  },
1678
+ "./utils/asset": {
1679
+ "types": "./utils/asset.d.ts",
1680
+ "require": "./cjs/utils/asset.js",
1681
+ "default": "./utils/asset.js"
1682
+ },
1668
1683
  "./utils/canDerive": {
1669
1684
  "types": "./utils/canDerive.d.ts",
1670
1685
  "require": "./cjs/utils/canDerive.js",
@@ -1801,11 +1816,11 @@
1801
1816
  "@reduxjs/toolkit": "^1.9.1",
1802
1817
  "@sora-substrate/type-definitions": "^1.17.7",
1803
1818
  "@substrate/connect": "^0.7.26",
1804
- "@subwallet/chain-list": "0.2.53",
1805
- "@subwallet/extension-base": "^1.1.51-1",
1806
- "@subwallet/extension-chains": "^1.1.51-1",
1807
- "@subwallet/extension-dapp": "^1.1.51-1",
1808
- "@subwallet/extension-inject": "^1.1.51-1",
1819
+ "@subwallet/chain-list": "0.2.54",
1820
+ "@subwallet/extension-base": "^1.1.52-0",
1821
+ "@subwallet/extension-chains": "^1.1.52-0",
1822
+ "@subwallet/extension-dapp": "^1.1.52-0",
1823
+ "@subwallet/extension-inject": "^1.1.52-0",
1809
1824
  "@subwallet/keyring": "^0.1.3",
1810
1825
  "@subwallet/ui-keyring": "^0.1.3",
1811
1826
  "@walletconnect/sign-client": "^2.8.4",
package/packageInfo.js CHANGED
@@ -7,5 +7,5 @@ export const packageInfo = {
7
7
  name: '@subwallet/extension-base',
8
8
  path: (import.meta && import.meta.url) ? new URL(import.meta.url).pathname.substring(0, new URL(import.meta.url).pathname.lastIndexOf('/') + 1) : 'auto',
9
9
  type: 'esm',
10
- version: '1.1.51-1'
10
+ version: '1.1.52-0'
11
11
  };
@@ -2,9 +2,9 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
4
  import { ALL_ACCOUNT_KEY } from '@subwallet/extension-base/constants';
5
- import { groupBalance } from '@subwallet/extension-base/services/balance-service/helpers/group';
6
5
  import { isAccountAll } from '@subwallet/extension-base/utils';
7
6
  import { BehaviorSubject } from 'rxjs';
7
+ import { groupBalance } from "./helpers/index.js";
8
8
  export class BalanceMapImpl {
9
9
  constructor(_map = {}) {
10
10
  this._map = _map;
@@ -0,0 +1,2 @@
1
+ export * from './group';
2
+ export * from './subscribe';
@@ -0,0 +1,5 @@
1
+ // Copyright 2019-2022 @subwallet/extension-base
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export * from "./group.js";
5
+ export * from "./subscribe/index.js";
@@ -1,5 +1,3 @@
1
- import { _ChainAsset } from '@subwallet/chain-list/types';
2
- import { _EvmApi } from '@subwallet/extension-base/services/chain-service/types';
3
- import { BalanceItem } from '@subwallet/extension-base/types';
4
- export declare function subscribeERC20Interval(addresses: string[], chain: string, evmApiMap: Record<string, _EvmApi>, callBack: (result: BalanceItem[]) => void): () => void;
5
- export declare function subscribeEVMBalance(chain: string, addresses: string[], evmApiMap: Record<string, _EvmApi>, callback: (rs: BalanceItem[]) => void, tokenInfo: _ChainAsset): () => void;
1
+ import { SubscribeEvmPalletBalance } from '@subwallet/extension-base/types';
2
+ export declare function subscribeERC20Interval({ addresses, assetMap, callback, chainInfo, evmApi }: SubscribeEvmPalletBalance): () => void;
3
+ export declare function subscribeEVMBalance(params: SubscribeEvmPalletBalance): () => void;
@@ -6,12 +6,22 @@ import { APIItemState } from '@subwallet/extension-base/background/KoniTypes';
6
6
  import { ASTAR_REFRESH_BALANCE_INTERVAL, SUB_TOKEN_REFRESH_BALANCE_INTERVAL } from '@subwallet/extension-base/constants';
7
7
  import { getEVMBalance } from '@subwallet/extension-base/koni/api/tokens/evm/balance';
8
8
  import { getERC20Contract } from '@subwallet/extension-base/koni/api/tokens/evm/web3';
9
- import { state } from '@subwallet/extension-base/koni/background/handlers';
10
9
  import { _getContractAddressOfToken } from '@subwallet/extension-base/services/chain-service/utils';
10
+ import { filterAssetsByChainAndType } from '@subwallet/extension-base/utils';
11
11
  import { BN } from '@polkadot/util';
12
- export function subscribeERC20Interval(addresses, chain, evmApiMap, callBack) {
13
- let tokenList = {};
12
+ export function subscribeERC20Interval({
13
+ addresses,
14
+ assetMap,
15
+ callback,
16
+ chainInfo,
17
+ evmApi
18
+ }) {
19
+ const chain = chainInfo.slug;
20
+ const tokenList = filterAssetsByChainAndType(assetMap, chain, [_AssetType.ERC20]);
14
21
  const erc20ContractMap = {};
22
+ Object.entries(tokenList).forEach(([slug, tokenInfo]) => {
23
+ erc20ContractMap[slug] = getERC20Contract(_getContractAddressOfToken(tokenInfo), evmApi);
24
+ });
15
25
  const getTokenBalances = () => {
16
26
  Object.values(tokenList).map(async tokenInfo => {
17
27
  try {
@@ -34,40 +44,47 @@ export function subscribeERC20Interval(addresses, chain, evmApiMap, callBack) {
34
44
  state: APIItemState.READY
35
45
  };
36
46
  });
37
- callBack(items);
47
+ callback(items);
38
48
  } catch (err) {
39
49
  console.log(tokenInfo.slug, err);
40
50
  }
41
51
  });
42
52
  };
43
- tokenList = state.getAssetByChainAndAsset(chain, [_AssetType.ERC20]);
44
- Object.entries(tokenList).forEach(([slug, tokenInfo]) => {
45
- erc20ContractMap[slug] = getERC20Contract(chain, _getContractAddressOfToken(tokenInfo), evmApiMap);
46
- });
47
53
  getTokenBalances();
48
54
  const interval = setInterval(getTokenBalances, SUB_TOKEN_REFRESH_BALANCE_INTERVAL);
49
55
  return () => {
50
56
  clearInterval(interval);
51
57
  };
52
58
  }
53
- export function subscribeEVMBalance(chain, addresses, evmApiMap, callback, tokenInfo) {
59
+ export function subscribeEVMBalance(params) {
60
+ var _Object$values$;
61
+ const {
62
+ addresses,
63
+ assetMap,
64
+ callback,
65
+ chainInfo,
66
+ evmApi
67
+ } = params;
68
+ const chain = chainInfo.slug;
69
+ const nativeTokenInfo = filterAssetsByChainAndType(assetMap, chain, [_AssetType.NATIVE]);
70
+ const nativeTokenSlug = ((_Object$values$ = Object.values(nativeTokenInfo)[0]) === null || _Object$values$ === void 0 ? void 0 : _Object$values$.slug) || '';
54
71
  function getBalance() {
55
- getEVMBalance(chain, addresses, evmApiMap).then(balances => {
72
+ getEVMBalance(chain, addresses, evmApi).then(balances => {
56
73
  return balances.map((balance, index) => {
57
74
  return {
58
75
  address: addresses[index],
59
- tokenSlug: tokenInfo.slug,
76
+ tokenSlug: nativeTokenSlug,
60
77
  state: APIItemState.READY,
61
78
  free: new BN(balance || '0').toString(),
62
79
  locked: '0'
63
80
  };
64
81
  });
65
82
  }).catch(e => {
66
- console.error(`Error on get native balance with token ${tokenInfo.slug}`, e);
83
+ console.error(`Error on get native balance with token ${nativeTokenSlug}`, e);
67
84
  return addresses.map(address => {
68
85
  return {
69
86
  address: address,
70
- tokenSlug: tokenInfo.slug,
87
+ tokenSlug: nativeTokenSlug,
71
88
  state: APIItemState.READY,
72
89
  free: '0',
73
90
  locked: '0'
@@ -79,7 +96,7 @@ export function subscribeEVMBalance(chain, addresses, evmApiMap, callback, token
79
96
  }
80
97
  getBalance();
81
98
  const interval = setInterval(getBalance, ASTAR_REFRESH_BALANCE_INTERVAL);
82
- const unsub2 = subscribeERC20Interval(addresses, chain, evmApiMap, callback);
99
+ const unsub2 = subscribeERC20Interval(params);
83
100
  return () => {
84
101
  clearInterval(interval);
85
102
  unsub2 && unsub2();
@@ -1,4 +1,4 @@
1
- import { _ChainInfo } from '@subwallet/chain-list/types';
1
+ import { _ChainAsset, _ChainInfo } from '@subwallet/chain-list/types';
2
2
  import { AccountJson } from '@subwallet/extension-base/background/types';
3
3
  import { _EvmApi, _SubstrateApi } from '@subwallet/extension-base/services/chain-service/types';
4
4
  import { BalanceItem } from '@subwallet/extension-base/types';
@@ -12,4 +12,4 @@ import { BalanceItem } from '@subwallet/extension-base/types';
12
12
  * @returns {AccountJson|null} - Account info or null if not found
13
13
  */
14
14
  export declare const getAccountJsonByAddress: (address: string) => AccountJson | null;
15
- export declare function subscribeBalance(addresses: string[], chainInfoMap: Record<string, _ChainInfo>, substrateApiMap: Record<string, _SubstrateApi>, evmApiMap: Record<string, _EvmApi>, callback: (rs: BalanceItem[]) => void): () => void;
15
+ export declare function subscribeBalance(addresses: string[], chains: string[], tokens: string[], _chainAssetMap: Record<string, _ChainAsset>, _chainInfoMap: Record<string, _ChainInfo>, substrateApiMap: Record<string, _SubstrateApi>, evmApiMap: Record<string, _EvmApi>, callback: (rs: BalanceItem[]) => void): () => void;