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

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 (81) hide show
  1. package/background/KoniTypes.d.ts +1 -25
  2. package/cjs/constants/index.js +9 -3
  3. package/cjs/koni/api/staking/bonding/index.js +1 -0
  4. package/cjs/koni/api/tokens/evm/balance.js +5 -1
  5. package/cjs/koni/api/tokens/evm/transfer.js +8 -4
  6. package/cjs/koni/background/cron.js +2 -2
  7. package/cjs/koni/background/handlers/Extension.js +5 -5
  8. package/cjs/koni/background/handlers/Mobile.js +1 -1
  9. package/cjs/koni/background/handlers/State.js +123 -44
  10. package/cjs/koni/background/subscription.js +2 -2
  11. package/cjs/packageInfo.js +1 -1
  12. package/cjs/services/balance-service/helpers/group.js +53 -0
  13. package/cjs/services/balance-service/helpers/subscribe/balance.js +111 -0
  14. package/cjs/services/balance-service/helpers/subscribe/evm.js +95 -0
  15. package/cjs/services/balance-service/helpers/subscribe/substrate/equilibrium.js +113 -0
  16. package/cjs/services/balance-service/helpers/subscribe/substrate/index.js +324 -0
  17. package/cjs/services/balance-service/index.js +41 -16
  18. package/cjs/services/history-service/helpers/subscan-extrinsic-parser-helper.js +7 -1
  19. package/cjs/services/history-service/index.js +12 -8
  20. package/cjs/services/migration-service/scripts/MigrateProvider.js +1 -1
  21. package/cjs/services/storage-service/DatabaseService.js +7 -2
  22. package/cjs/services/storage-service/db-stores/Balance.js +9 -9
  23. package/cjs/services/subscan-service/index.js +66 -22
  24. package/cjs/services/transaction-service/index.js +4 -3
  25. package/cjs/types/balance.js +1 -0
  26. package/cjs/types/index.js +11 -0
  27. package/cjs/utils/{address.js → account.js} +32 -2
  28. package/cjs/utils/eth.js +7 -2
  29. package/cjs/utils/index.js +12 -0
  30. package/constants/index.d.ts +2 -0
  31. package/constants/index.js +2 -0
  32. package/koni/api/staking/bonding/index.js +1 -0
  33. package/koni/api/tokens/evm/balance.js +5 -1
  34. package/koni/api/tokens/evm/transfer.d.ts +1 -1
  35. package/koni/api/tokens/evm/transfer.js +8 -4
  36. package/koni/background/cron.js +3 -3
  37. package/koni/background/handlers/Extension.js +5 -5
  38. package/koni/background/handlers/Mobile.js +1 -1
  39. package/koni/background/handlers/State.d.ts +6 -4
  40. package/koni/background/handlers/State.js +111 -33
  41. package/koni/background/subscription.js +2 -2
  42. package/package.json +39 -14
  43. package/packageInfo.js +1 -1
  44. package/services/balance-service/helpers/group.d.ts +9 -0
  45. package/services/balance-service/helpers/group.js +46 -0
  46. package/services/balance-service/helpers/subscribe/balance.d.ts +4 -0
  47. package/services/balance-service/helpers/subscribe/balance.js +103 -0
  48. package/services/balance-service/helpers/subscribe/evm.d.ts +5 -0
  49. package/services/balance-service/helpers/subscribe/evm.js +87 -0
  50. package/services/balance-service/helpers/subscribe/substrate/equilibrium.d.ts +4 -0
  51. package/services/balance-service/helpers/subscribe/substrate/equilibrium.js +105 -0
  52. package/services/balance-service/helpers/subscribe/substrate/index.d.ts +4 -0
  53. package/services/balance-service/helpers/subscribe/substrate/index.js +316 -0
  54. package/services/balance-service/index.d.ts +24 -5
  55. package/services/balance-service/index.js +40 -14
  56. package/services/history-service/helpers/subscan-extrinsic-parser-helper.js +7 -1
  57. package/services/history-service/index.js +12 -8
  58. package/services/migration-service/scripts/MigrateProvider.js +1 -1
  59. package/services/storage-service/DatabaseService.d.ts +4 -2
  60. package/services/storage-service/DatabaseService.js +7 -2
  61. package/services/storage-service/databases/index.d.ts +2 -1
  62. package/services/storage-service/db-stores/Balance.d.ts +2 -2
  63. package/services/storage-service/db-stores/Balance.js +9 -9
  64. package/services/subscan-service/index.d.ts +11 -5
  65. package/services/subscan-service/index.js +66 -26
  66. package/services/subscan-service/types.d.ts +4 -0
  67. package/services/transaction-service/index.js +5 -4
  68. package/types/balance.d.ts +40 -0
  69. package/types/balance.js +1 -0
  70. package/types/index.d.ts +1 -0
  71. package/types/index.js +1 -0
  72. package/utils/account.d.ts +15 -0
  73. package/utils/{address.js → account.js} +28 -0
  74. package/utils/eth.d.ts +1 -0
  75. package/utils/eth.js +4 -0
  76. package/utils/index.d.ts +1 -0
  77. package/utils/index.js +1 -0
  78. package/cjs/koni/api/dotsama/balance.js +0 -464
  79. package/koni/api/dotsama/balance.d.ts +0 -6
  80. package/koni/api/dotsama/balance.js +0 -451
  81. package/utils/address.d.ts +0 -5
@@ -28,9 +28,8 @@ import { EXTENSION_REQUEST_URL } from '@subwallet/extension-base/services/reques
28
28
  import { DEFAULT_AUTO_LOCK_TIME } from '@subwallet/extension-base/services/setting-service/constants';
29
29
  import { WALLET_CONNECT_EIP155_NAMESPACE } from '@subwallet/extension-base/services/wallet-connect-service/constants';
30
30
  import { isProposalExpired, isSupportWalletConnectChain, isSupportWalletConnectNamespace } from '@subwallet/extension-base/services/wallet-connect-service/helpers';
31
- import { isSameAddress, reformatAddress, uniqueStringArray } from '@subwallet/extension-base/utils';
32
- import { convertSubjectInfoToAddresses } from '@subwallet/extension-base/utils/address';
33
- import { createTransactionFromRLP, signatureToHex } from '@subwallet/extension-base/utils/eth';
31
+ import { convertSubjectInfoToAddresses, isSameAddress, reformatAddress, uniqueStringArray } from '@subwallet/extension-base/utils';
32
+ import { createTransactionFromRLP, recalculateGasPrice, signatureToHex } from '@subwallet/extension-base/utils/eth';
34
33
  import { parseContractInput, parseEvmRlp } from '@subwallet/extension-base/utils/eth/parseTransaction';
35
34
  import { balanceFormatter, formatNumber } from '@subwallet/extension-base/utils/number';
36
35
  import { createPair } from '@subwallet/keyring';
@@ -1725,7 +1724,7 @@ export default class KoniExtension {
1725
1724
  } = inputData;
1726
1725
  const contractAddress = params.contractAddress;
1727
1726
  const tokenId = params.tokenId;
1728
- const transaction = await getERC721Transaction(this.#koniState.getEvmApi(networkKey), contractAddress, senderAddress, recipientAddress, tokenId);
1727
+ const transaction = await getERC721Transaction(this.#koniState.getEvmApi(networkKey), networkKey, contractAddress, senderAddress, recipientAddress, tokenId);
1729
1728
 
1730
1729
  // this.addContact(recipientAddress);
1731
1730
 
@@ -1872,7 +1871,8 @@ export default class KoniExtension {
1872
1871
  // null address
1873
1872
  from: address
1874
1873
  };
1875
- const gasPrice = await web3.api.eth.getGasPrice();
1874
+ const _price = await web3.api.eth.getGasPrice();
1875
+ const gasPrice = recalculateGasPrice(_price, networkKey);
1876
1876
  const gasLimit = await web3.api.eth.estimateGas(transaction);
1877
1877
  estimatedFee = (gasLimit * parseInt(gasPrice)).toString();
1878
1878
  } else {
@@ -108,7 +108,7 @@ export default class Mobile {
108
108
  if (storage) {
109
109
  const storageData = JSON.parse(storage);
110
110
  for (const key in storageData) {
111
- localStorage.setItem(key, JSON.stringify(storageData[key]));
111
+ localStorage.setItem(key, storageData[key]);
112
112
  }
113
113
  }
114
114
  if (indexedDB) {
@@ -1,6 +1,6 @@
1
1
  /// <reference types="chrome" />
2
2
  import { _AssetRef, _AssetType, _ChainAsset, _ChainInfo, _MultiChainAsset } from '@subwallet/chain-list/types';
3
- import { AddTokenRequestExternal, AmountData, ApiMap, AuthRequestV2, BalanceItem, BalanceJson, ChainStakingMetadata, ConfirmationsQueue, CrowdloanItem, CrowdloanJson, CurrentAccountInfo, EvmSendTransactionParams, ExternalRequestPromise, MantaPayConfig, MantaPaySyncState, NftCollection, NftItem, NftJson, NominatorMetadata, RequestAccountExportPrivateKey, RequestCheckPublicAndSecretKey, RequestConfirmationComplete, RequestSettingsType, ResponseAccountExportPrivateKey, ResponseCheckPublicAndSecretKey, ServiceInfo, SingleModeJson, StakingItem, StakingJson, StakingRewardItem, StakingRewardJson, StakingType, UiSettings } from '@subwallet/extension-base/background/KoniTypes';
3
+ import { AddTokenRequestExternal, AmountData, ApiMap, AuthRequestV2, ChainStakingMetadata, ConfirmationsQueue, CrowdloanItem, CrowdloanJson, CurrentAccountInfo, EvmSendTransactionParams, ExternalRequestPromise, MantaPayConfig, MantaPaySyncState, NftCollection, NftItem, NftJson, NominatorMetadata, RequestAccountExportPrivateKey, RequestCheckPublicAndSecretKey, RequestConfirmationComplete, RequestSettingsType, ResponseAccountExportPrivateKey, ResponseCheckPublicAndSecretKey, ServiceInfo, SingleModeJson, StakingItem, StakingJson, StakingRewardItem, StakingRewardJson, StakingType, UiSettings } from '@subwallet/extension-base/background/KoniTypes';
4
4
  import { AccountJson, RequestAuthorizeTab, RequestRpcSend, RequestRpcSubscribe, RequestRpcUnsubscribe, RequestSign, ResponseRpcListProviders, ResponseSigning } from '@subwallet/extension-base/background/types';
5
5
  import { BalanceService } from '@subwallet/extension-base/services/balance-service';
6
6
  import BuyService from '@subwallet/extension-base/services/buy-service';
@@ -20,6 +20,7 @@ import DatabaseService from '@subwallet/extension-base/services/storage-service/
20
20
  import { SubscanService } from '@subwallet/extension-base/services/subscan-service';
21
21
  import TransactionService from '@subwallet/extension-base/services/transaction-service';
22
22
  import WalletConnectService from '@subwallet/extension-base/services/wallet-connect-service';
23
+ import { BalanceItem, BalanceJson, BalanceMap } from '@subwallet/extension-base/types';
23
24
  import { MetadataDef, ProviderMeta } from '@subwallet/extension-inject/types';
24
25
  import SimpleKeyring from 'eth-simple-keyring';
25
26
  import { BehaviorSubject, Subject } from 'rxjs';
@@ -83,7 +84,7 @@ export default class KoniState {
83
84
  saveMetadata(meta: MetadataDef): void;
84
85
  sign(url: string, request: RequestSign, account: AccountJson): Promise<ResponseSigning>;
85
86
  get authSubjectV2(): BehaviorSubject<import("@subwallet/extension-base/background/types").AuthorizeRequest[]>;
86
- generateDefaultBalanceMap(): Record<string, BalanceItem>;
87
+ generateDefaultBalanceMap(): BalanceMap;
87
88
  private afterChainServiceInit;
88
89
  init(): Promise<void>;
89
90
  initMantaPay(password: string): Promise<void>;
@@ -151,12 +152,13 @@ export default class KoniState {
151
152
  getAllAddresses(): string[];
152
153
  private removeInactiveChainBalances;
153
154
  getBalance(reset?: boolean): BalanceJson;
154
- getStoredBalance(address: string): Promise<Record<string, BalanceItem>>;
155
+ getStoredBalance(address: string): Promise<BalanceMap>;
155
156
  handleSwitchAccount(newAddress: string): Promise<void>;
156
157
  resetBalanceMap(newAddress: string): Promise<void>;
157
158
  resetCrowdloanMap(newAddress: string): Promise<void>;
158
159
  resetStaking(newAddress: string): void;
159
- setBalanceItem(tokenSlug: string, item: BalanceItem): void;
160
+ /** Note: items must be same tokenSlug */
161
+ setBalanceItem(items: BalanceItem[]): void;
160
162
  private updateBalanceStore;
161
163
  subscribeBalance(): Subject<BalanceJson>;
162
164
  getCrowdloan(reset?: boolean): CrowdloanJson;
@@ -7,6 +7,7 @@ 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 { groupBalance } from '@subwallet/extension-base/services/balance-service/helpers/group';
10
11
  import { ServiceStatus } from '@subwallet/extension-base/services/base/types';
11
12
  import BuyService from '@subwallet/extension-base/services/buy-service';
12
13
  import CampaignService from '@subwallet/extension-base/services/campaign-service';
@@ -27,7 +28,8 @@ import { SUBSCAN_API_CHAIN_MAP, SUBSCAN_BALANCE_CHAIN_MAP_REVERSE } from '@subwa
27
28
  import TransactionService from '@subwallet/extension-base/services/transaction-service';
28
29
  import WalletConnectService from '@subwallet/extension-base/services/wallet-connect-service';
29
30
  import AccountRefStore from '@subwallet/extension-base/stores/AccountRef';
30
- import { stripUrl, TARGET_ENV } from '@subwallet/extension-base/utils';
31
+ import { isAccountAll, isSameAddress, stripUrl, TARGET_ENV } from '@subwallet/extension-base/utils';
32
+ import { recalculateGasPrice } from '@subwallet/extension-base/utils/eth';
31
33
  import { isContractAddress, parseContractInput } from '@subwallet/extension-base/utils/eth/parseTransaction';
32
34
  import { createPromiseHandler } from '@subwallet/extension-base/utils/promise';
33
35
  import { decodePair } from '@subwallet/keyring/pair/decode';
@@ -90,7 +92,7 @@ export default class KoniState {
90
92
  this.settingService = new SettingService();
91
93
  this.requestService = new RequestService(this.chainService, this.settingService, this.keyringService);
92
94
  this.priceService = new PriceService(this.dbService, this.eventService, this.chainService);
93
- this.balanceService = new BalanceService(this.chainService);
95
+ this.balanceService = new BalanceService(this);
94
96
  this.historyService = new HistoryService(this.dbService, this.chainService, this.eventService, this.keyringService, this.subscanService);
95
97
  this.transactionService = new TransactionService(this.chainService, this.eventService, this.requestService, this.balanceService, this.historyService, this.notificationService, this.dbService);
96
98
  this.walletConnectService = new WalletConnectService(this, this.requestService);
@@ -187,16 +189,23 @@ export default class KoniState {
187
189
  generateDefaultBalanceMap() {
188
190
  const balanceMap = {};
189
191
  const activeChains = this.chainService.getActiveChainInfoMap();
190
- Object.values(activeChains).forEach(chainInfo => {
191
- const chainAssetMap = this.chainService.getFungibleTokensByChain(chainInfo.slug);
192
- Object.keys(chainAssetMap).forEach(assetSlug => {
193
- balanceMap[assetSlug] = {
194
- tokenSlug: assetSlug,
195
- free: '',
196
- locked: '',
197
- state: APIItemState.PENDING
198
- };
192
+ const isAllAccount = isAccountAll(this.keyringService.currentAccount.address);
193
+ const addresses = isAllAccount ? Object.keys(this.keyringService.accounts) : [this.keyringService.currentAccount.address];
194
+ addresses.forEach(address => {
195
+ const temp = {};
196
+ Object.values(activeChains).forEach(chainInfo => {
197
+ const chainAssetMap = this.chainService.getFungibleTokensByChain(chainInfo.slug);
198
+ Object.keys(chainAssetMap).forEach(assetSlug => {
199
+ temp[assetSlug] = {
200
+ address,
201
+ tokenSlug: assetSlug,
202
+ free: '',
203
+ locked: '',
204
+ state: APIItemState.PENDING
205
+ };
206
+ });
199
207
  });
208
+ balanceMap[address] = temp;
200
209
  });
201
210
  return balanceMap;
202
211
  }
@@ -666,14 +675,17 @@ export default class KoniState {
666
675
  }
667
676
  removeInactiveChainBalances(balanceMap) {
668
677
  const activeBalanceMap = {};
669
- Object.entries(balanceMap).forEach(([tokenSlug, balanceItem]) => {
670
- const tokenInfo = this.chainService.getAssetBySlug(tokenSlug);
671
- if (tokenInfo) {
672
- const chainInfo = this.chainService.getChainInfoByKey(tokenInfo.originChain);
673
- if (chainInfo && this.getChainStateByKey(chainInfo.slug).active) {
674
- activeBalanceMap[tokenSlug] = balanceItem;
678
+ Object.entries(balanceMap).forEach(([address, balances]) => {
679
+ activeBalanceMap[address] = {};
680
+ Object.entries(balances).forEach(([tokenSlug, item]) => {
681
+ const tokenInfo = this.chainService.getAssetBySlug(tokenSlug);
682
+ if (tokenInfo) {
683
+ const chainInfo = this.chainService.getChainInfoByKey(tokenInfo.originChain);
684
+ if (chainInfo && this.getChainStateByKey(chainInfo.slug).active) {
685
+ activeBalanceMap[address][tokenSlug] = item;
686
+ }
675
687
  }
676
- }
688
+ });
677
689
  });
678
690
  return activeBalanceMap;
679
691
  }
@@ -685,7 +697,7 @@ export default class KoniState {
685
697
  };
686
698
  }
687
699
  async getStoredBalance(address) {
688
- const items = await this.dbService.stores.balance.getBalanceMapByAddress(address);
700
+ const items = await this.dbService.stores.balance.getBalanceMapByAddresses(address);
689
701
  return items || {};
690
702
  }
691
703
  async handleSwitchAccount(newAddress) {
@@ -695,6 +707,31 @@ export default class KoniState {
695
707
  const defaultData = this.generateDefaultBalanceMap();
696
708
  let storedData = await this.getStoredBalance(newAddress);
697
709
  storedData = this.removeInactiveChainBalances(storedData);
710
+ const result = {};
711
+ for (const [address, balanceInfo] of Object.entries(defaultData)) {
712
+ result[address] = {
713
+ ...balanceInfo
714
+ };
715
+ }
716
+ for (const [address, balanceInfo] of Object.entries(storedData)) {
717
+ if (!result[address]) {
718
+ result[address] = {
719
+ ...balanceInfo
720
+ };
721
+ } else {
722
+ const temp = {
723
+ ...result[address]
724
+ };
725
+ for (const [slug, item] of Object.entries(balanceInfo)) {
726
+ temp[slug] = {
727
+ ...item
728
+ };
729
+ }
730
+ result[address] = {
731
+ ...temp
732
+ };
733
+ }
734
+ }
698
735
  this.balanceMap = {
699
736
  ...defaultData,
700
737
  ...storedData
@@ -722,19 +759,59 @@ export default class KoniState {
722
759
  });
723
760
  });
724
761
  }
725
- setBalanceItem(tokenSlug, item) {
726
- this.balanceMap[tokenSlug] = {
727
- timestamp: +new Date(),
728
- ...item
729
- };
730
- this.updateBalanceStore(item);
731
- this.lazyNext('setBalanceItem', () => {
732
- this.publishBalance();
733
- });
762
+
763
+ /** Note: items must be same tokenSlug */
764
+ setBalanceItem(items) {
765
+ if (items.length) {
766
+ const tokens = [];
767
+ const updates = [];
768
+ for (const item of items) {
769
+ const address = item.address;
770
+ const tokenSlug = item.tokenSlug;
771
+ if (!tokens.includes(tokenSlug)) {
772
+ tokens.push(tokenSlug);
773
+ }
774
+ if (!this.balanceMap[address]) {
775
+ this.balanceMap[address] = {};
776
+ }
777
+ const data = {
778
+ timestamp: +new Date(),
779
+ ...item
780
+ };
781
+ this.balanceMap[address][tokenSlug] = data;
782
+ updates.push(data);
783
+ }
784
+ const isAllAccount = isAccountAll(this.keyringService.currentAccount.address);
785
+ if (isAllAccount) {
786
+ const address = ALL_ACCOUNT_KEY;
787
+ for (const token of tokens) {
788
+ const items = [];
789
+ for (const [_adr, balanceInfo] of Object.entries(this.balanceMap)) {
790
+ if (!isSameAddress(_adr, address)) {
791
+ const item = balanceInfo[token];
792
+ item && items.push(item);
793
+ }
794
+ }
795
+ const _balance = groupBalance(items, address, token);
796
+ const balance = {
797
+ timestamp: +new Date(),
798
+ ..._balance
799
+ };
800
+ if (!this.balanceMap[address]) {
801
+ this.balanceMap[address] = {};
802
+ }
803
+ this.balanceMap[address][token] = balance;
804
+ updates.push(balance);
805
+ }
806
+ }
807
+ this.updateBalanceStore(updates);
808
+ this.lazyNext('setBalanceItem', () => {
809
+ this.publishBalance();
810
+ });
811
+ }
734
812
  }
735
- updateBalanceStore(item) {
736
- const currentAccountInfo = this.keyringService.currentAccount;
737
- this.dbService.updateBalanceStore(currentAccountInfo.address, item).catch(e => this.logger.warn(e));
813
+ updateBalanceStore(items) {
814
+ this.dbService.updateBulkBalanceStore(items).catch(e => this.logger.warn(e));
738
815
  }
739
816
  subscribeBalance() {
740
817
  return this.balanceSubject;
@@ -1230,7 +1307,8 @@ export default class KoniState {
1230
1307
  // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
1231
1308
  throw new EvmProviderError(EvmProviderErrorType.INVALID_PARAMS, e === null || e === void 0 ? void 0 : e.message);
1232
1309
  }
1233
- const gasPrice = await web3.eth.getGasPrice();
1310
+ const _price = await web3.eth.getGasPrice();
1311
+ const gasPrice = recalculateGasPrice(_price, networkKey);
1234
1312
  transaction.gasPrice = gasPrice;
1235
1313
  const estimateGas = new BN(gasPrice.toString()).mul(new BN(transaction.gas)).toString();
1236
1314
 
@@ -1663,7 +1741,7 @@ export default class KoniState {
1663
1741
  };
1664
1742
  balanceItem.free = ((_zkBalances$i = zkBalances[i]) === null || _zkBalances$i === void 0 ? void 0 : _zkBalances$i.toString()) || '0';
1665
1743
  balanceItem.state = APIItemState.READY;
1666
- this.setBalanceItem(balanceItem.tokenSlug, balanceItem);
1744
+ this.setBalanceItem([balanceItem]);
1667
1745
  }
1668
1746
  }).catch(console.warn);
1669
1747
  }
@@ -1,12 +1,12 @@
1
1
  // Copyright 2019-2022 @subwallet/extension-koni authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import { subscribeBalance } from '@subwallet/extension-base/koni/api/dotsama/balance';
5
4
  import { subscribeCrowdloan } from '@subwallet/extension-base/koni/api/dotsama/crowdloan';
6
5
  import { getNominationStakingRewardData, getPoolingStakingRewardData, stakingOnChainApi } from '@subwallet/extension-base/koni/api/staking';
7
6
  import { subscribeEssentialChainStakingMetadata } from '@subwallet/extension-base/koni/api/staking/bonding';
8
7
  import { getAmplitudeUnclaimedStakingReward } from '@subwallet/extension-base/koni/api/staking/paraChain';
9
8
  import { nftHandler } from '@subwallet/extension-base/koni/background/handlers';
9
+ import { subscribeBalance } from '@subwallet/extension-base/services/balance-service/helpers/subscribe/balance';
10
10
  import { _isChainEnabled, _isChainSupportSubstrateStaking } from '@subwallet/extension-base/services/chain-service/utils';
11
11
  import { COMMON_RELOAD_EVENTS } from '@subwallet/extension-base/services/event-service/types';
12
12
  import { waitTimeout } from '@subwallet/extension-base/utils';
@@ -147,7 +147,7 @@ export class KoniSubscription {
147
147
  }
148
148
  });
149
149
  const unsub = subscribeBalance(addresses, filteredChainInfoMap, substrateApiMap, evmApiMap, result => {
150
- this.state.setBalanceItem(result.tokenSlug, result);
150
+ this.state.setBalanceItem(result);
151
151
  });
152
152
  const unsub2 = this.state.subscribeMantaPayBalance();
153
153
  if (onlyRunOnFirstTime) {
package/package.json CHANGED
@@ -17,7 +17,7 @@
17
17
  "./cjs/detectPackage.js"
18
18
  ],
19
19
  "type": "module",
20
- "version": "1.1.24-0",
20
+ "version": "1.1.24-1",
21
21
  "main": "./cjs/index.js",
22
22
  "module": "./index.js",
23
23
  "types": "./index.d.ts",
@@ -164,11 +164,6 @@
164
164
  "require": "./cjs/koni/api/donate.js",
165
165
  "default": "./koni/api/donate.js"
166
166
  },
167
- "./koni/api/dotsama/balance": {
168
- "types": "./koni/api/dotsama/balance.d.ts",
169
- "require": "./cjs/koni/api/dotsama/balance.js",
170
- "default": "./koni/api/dotsama/balance.js"
171
- },
172
167
  "./koni/api/dotsama/crowdloan": {
173
168
  "types": "./koni/api/dotsama/crowdloan.d.ts",
174
169
  "require": "./cjs/koni/api/dotsama/crowdloan.js",
@@ -515,6 +510,31 @@
515
510
  "require": "./cjs/services/balance-service/index.js",
516
511
  "default": "./services/balance-service/index.js"
517
512
  },
513
+ "./services/balance-service/helpers/group": {
514
+ "types": "./services/balance-service/helpers/group.d.ts",
515
+ "require": "./cjs/services/balance-service/helpers/group.js",
516
+ "default": "./services/balance-service/helpers/group.js"
517
+ },
518
+ "./services/balance-service/helpers/subscribe/balance": {
519
+ "types": "./services/balance-service/helpers/subscribe/balance.d.ts",
520
+ "require": "./cjs/services/balance-service/helpers/subscribe/balance.js",
521
+ "default": "./services/balance-service/helpers/subscribe/balance.js"
522
+ },
523
+ "./services/balance-service/helpers/subscribe/evm": {
524
+ "types": "./services/balance-service/helpers/subscribe/evm.d.ts",
525
+ "require": "./cjs/services/balance-service/helpers/subscribe/evm.js",
526
+ "default": "./services/balance-service/helpers/subscribe/evm.js"
527
+ },
528
+ "./services/balance-service/helpers/subscribe/substrate": {
529
+ "types": "./services/balance-service/helpers/subscribe/substrate/index.d.ts",
530
+ "require": "./cjs/services/balance-service/helpers/subscribe/substrate/index.js",
531
+ "default": "./services/balance-service/helpers/subscribe/substrate/index.js"
532
+ },
533
+ "./services/balance-service/helpers/subscribe/substrate/equilibrium": {
534
+ "types": "./services/balance-service/helpers/subscribe/substrate/equilibrium.d.ts",
535
+ "require": "./cjs/services/balance-service/helpers/subscribe/substrate/equilibrium.js",
536
+ "default": "./services/balance-service/helpers/subscribe/substrate/equilibrium.js"
537
+ },
518
538
  "./services/base/types": {
519
539
  "types": "./services/base/types.d.ts",
520
540
  "require": "./cjs/services/base/types.js",
@@ -1203,6 +1223,11 @@
1203
1223
  "require": "./cjs/types/index.js",
1204
1224
  "default": "./types/index.js"
1205
1225
  },
1226
+ "./types/balance": {
1227
+ "types": "./types/balance.d.ts",
1228
+ "require": "./cjs/types/balance.js",
1229
+ "default": "./types/balance.js"
1230
+ },
1206
1231
  "./types/buy": {
1207
1232
  "types": "./types/buy.d.ts",
1208
1233
  "require": "./cjs/types/buy.js",
@@ -1213,10 +1238,10 @@
1213
1238
  "require": "./cjs/utils/index.js",
1214
1239
  "default": "./utils/index.js"
1215
1240
  },
1216
- "./utils/address": {
1217
- "types": "./utils/address.d.ts",
1218
- "require": "./cjs/utils/address.js",
1219
- "default": "./utils/address.js"
1241
+ "./utils/account": {
1242
+ "types": "./utils/account.d.ts",
1243
+ "require": "./cjs/utils/account.js",
1244
+ "default": "./utils/account.js"
1220
1245
  },
1221
1246
  "./utils/array": {
1222
1247
  "types": "./utils/array.d.ts",
@@ -1323,10 +1348,10 @@
1323
1348
  "@sora-substrate/type-definitions": "^1.17.7",
1324
1349
  "@substrate/connect": "^0.7.26",
1325
1350
  "@subwallet/chain-list": "0.2.26",
1326
- "@subwallet/extension-base": "^1.1.24-0",
1327
- "@subwallet/extension-chains": "^1.1.24-0",
1328
- "@subwallet/extension-dapp": "^1.1.24-0",
1329
- "@subwallet/extension-inject": "^1.1.24-0",
1351
+ "@subwallet/extension-base": "^1.1.24-1",
1352
+ "@subwallet/extension-chains": "^1.1.24-1",
1353
+ "@subwallet/extension-dapp": "^1.1.24-1",
1354
+ "@subwallet/extension-inject": "^1.1.24-1",
1330
1355
  "@subwallet/keyring": "^0.1.1",
1331
1356
  "@subwallet/ui-keyring": "^0.1.1",
1332
1357
  "@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.24-0'
10
+ version: '1.1.24-1'
11
11
  };
@@ -0,0 +1,9 @@
1
+ import { BalanceItem } from '@subwallet/extension-base/types';
2
+ /**
3
+ * Group the balance of {token} from {items} into {address}
4
+ * @param {BalanceItem[]} items - List balance want to group
5
+ * @param {string} address - Address will be grouped to
6
+ * @param {string} token - Slug of token will be group balance
7
+ * @return {BalanceItem} - Grouped balance information of token
8
+ */
9
+ export declare const groupBalance: (items: BalanceItem[], address: string, token: string) => BalanceItem;
@@ -0,0 +1,46 @@
1
+ // Copyright 2019-2022 @subwallet/extension-base
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { APIItemState } from '@subwallet/extension-base/background/KoniTypes';
5
+ import { sumBN } from '@subwallet/extension-base/utils';
6
+ import BN from 'bn.js';
7
+
8
+ /**
9
+ * Group the balance of {token} from {items} into {address}
10
+ * @param {BalanceItem[]} items - List balance want to group
11
+ * @param {string} address - Address will be grouped to
12
+ * @param {string} token - Slug of token will be group balance
13
+ * @return {BalanceItem} - Grouped balance information of token
14
+ */
15
+ export const groupBalance = (items, address, token) => {
16
+ const states = items.map(item => item.state);
17
+ const result = {
18
+ address,
19
+ tokenSlug: token,
20
+ free: sumBN(items.map(item => new BN(item.free))).toString(),
21
+ locked: sumBN(items.map(item => new BN(item.locked))).toString(),
22
+ state: states.every(item => item === APIItemState.NOT_SUPPORT) ? APIItemState.NOT_SUPPORT : states.some(item => item === APIItemState.READY) ? APIItemState.READY : APIItemState.PENDING
23
+ };
24
+ for (const item of items) {
25
+ if (item.substrateInfo) {
26
+ if (!result.substrateInfo) {
27
+ result.substrateInfo = {
28
+ ...item.substrateInfo
29
+ };
30
+ } else {
31
+ const old = {
32
+ ...result.substrateInfo
33
+ };
34
+ const _new = {
35
+ ...item.substrateInfo
36
+ };
37
+ result.substrateInfo = {
38
+ reserved: new BN(old.reserved || '0').add(new BN(_new.reserved || '0')).toString(),
39
+ feeFrozen: new BN(old.feeFrozen || '0').add(new BN(_new.feeFrozen || '0')).toString(),
40
+ miscFrozen: new BN(old.miscFrozen || '0').add(new BN(_new.miscFrozen || '0')).toString()
41
+ };
42
+ }
43
+ }
44
+ }
45
+ return result;
46
+ };
@@ -0,0 +1,4 @@
1
+ import { _ChainInfo } from '@subwallet/chain-list/types';
2
+ import { _EvmApi, _SubstrateApi } from '@subwallet/extension-base/services/chain-service/types';
3
+ import { BalanceItem } from '@subwallet/extension-base/types';
4
+ export declare function subscribeBalance(addresses: string[], chainInfoMap: Record<string, _ChainInfo>, substrateApiMap: Record<string, _SubstrateApi>, evmApiMap: Record<string, _EvmApi>, callback: (rs: BalanceItem[]) => void): () => void;
@@ -0,0 +1,103 @@
1
+ // Copyright 2019-2022 @subwallet/extension-base
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { _AssetType } from '@subwallet/chain-list/types';
5
+ import { APIItemState } from '@subwallet/extension-base/background/KoniTypes';
6
+ import { state } from '@subwallet/extension-base/koni/background/handlers';
7
+ import { _getSubstrateGenesisHash, _isChainEvmCompatible, _isPureEvmChain } from '@subwallet/extension-base/services/chain-service/utils';
8
+ import { categoryAddresses } from '@subwallet/extension-base/utils';
9
+ import { getAccountJsonByAddress } from '@subwallet/extension-base/utils/account';
10
+ import { subscribeEVMBalance } from "./evm.js";
11
+ import { subscribeSubstrateBalance } from "./substrate/index.js";
12
+ const filterAddress = (addresses, chainInfo) => {
13
+ const isEvmChain = _isChainEvmCompatible(chainInfo);
14
+ const [substrateAddresses, evmAddresses] = categoryAddresses(addresses);
15
+ if (isEvmChain) {
16
+ return [evmAddresses, []];
17
+ } else {
18
+ const fetchList = [];
19
+ const unfetchList = [];
20
+ substrateAddresses.forEach(address => {
21
+ const account = getAccountJsonByAddress(address);
22
+ if (account) {
23
+ if (account.isHardware) {
24
+ const availGen = account.availableGenesisHashes || [];
25
+ const gen = _getSubstrateGenesisHash(chainInfo);
26
+ if (availGen.includes(gen)) {
27
+ fetchList.push(address);
28
+ } else {
29
+ unfetchList.push(address);
30
+ }
31
+ } else {
32
+ fetchList.push(address);
33
+ }
34
+ } else {
35
+ fetchList.push(address);
36
+ }
37
+ });
38
+ return [fetchList, [...unfetchList, ...evmAddresses]];
39
+ }
40
+ };
41
+
42
+ // main subscription
43
+ export function subscribeBalance(addresses, chainInfoMap, substrateApiMap, evmApiMap, callback) {
44
+ // Looping over each chain
45
+ const unsubList = Object.entries(chainInfoMap).map(async ([chainSlug, chainInfo]) => {
46
+ const [useAddresses, notSupportAddresses] = filterAddress(addresses, chainInfo);
47
+ if (notSupportAddresses.length) {
48
+ const tokens = state.chainService.getAssetByChainAndType(chainSlug, [_AssetType.NATIVE, _AssetType.ERC20, _AssetType.PSP22, _AssetType.LOCAL]);
49
+ const assetSetting = await state.chainService.getAssetSettings();
50
+ const filtered = Object.values(tokens).filter(({
51
+ slug
52
+ }) => {
53
+ var _assetSetting$slug;
54
+ return (_assetSetting$slug = assetSetting[slug]) === null || _assetSetting$slug === void 0 ? void 0 : _assetSetting$slug.visible;
55
+ });
56
+ const now = new Date().getTime();
57
+ notSupportAddresses.forEach(address => {
58
+ const items = filtered.map(token => {
59
+ return {
60
+ address,
61
+ tokenSlug: token.slug,
62
+ free: '0',
63
+ locked: '0',
64
+ state: APIItemState.NOT_SUPPORT,
65
+ timestamp: now
66
+ };
67
+ });
68
+ callback(items);
69
+ });
70
+ }
71
+ if (_isPureEvmChain(chainInfo)) {
72
+ const nativeTokenInfo = state.getNativeTokenInfo(chainSlug);
73
+ return subscribeEVMBalance(chainSlug, useAddresses, evmApiMap, callback, nativeTokenInfo);
74
+ }
75
+
76
+ // if (!useAddresses || useAddresses.length === 0 || _PURE_EVM_CHAINS.indexOf(chainSlug) > -1) {
77
+ // const fungibleTokensByChain = state.chainService.getFungibleTokensByChain(chainSlug, true);
78
+ // const now = new Date().getTime();
79
+ //
80
+ // Object.values(fungibleTokensByChain).map((token) => {
81
+ // return {
82
+ // tokenSlug: token.slug,
83
+ // free: '0',
84
+ // locked: '0',
85
+ // state: APIItemState.READY,
86
+ // timestamp: now
87
+ // } as BalanceItem;
88
+ // }).forEach(callback);
89
+ //
90
+ // return undefined;
91
+ // }
92
+
93
+ const networkAPI = await substrateApiMap[chainSlug].isReady;
94
+ return subscribeSubstrateBalance(useAddresses, chainInfo, chainSlug, networkAPI, evmApiMap, callback);
95
+ });
96
+ return () => {
97
+ unsubList.forEach(subProm => {
98
+ subProm.then(unsub => {
99
+ unsub && unsub();
100
+ }).catch(console.error);
101
+ });
102
+ };
103
+ }
@@ -0,0 +1,5 @@
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;