@subwallet/extension-base 1.1.24-0 → 1.1.24-2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/background/KoniTypes.d.ts +2 -25
- package/cjs/constants/index.js +9 -3
- package/cjs/koni/api/staking/bonding/amplitude.js +6 -4
- package/cjs/koni/api/staking/bonding/astar.js +4 -0
- package/cjs/koni/api/staking/bonding/index.js +5 -0
- package/cjs/koni/api/staking/bonding/paraChain.js +9 -20
- package/cjs/koni/api/staking/bonding/relayChain.js +41 -23
- package/cjs/koni/api/staking/bonding/utils.js +67 -16
- package/cjs/koni/api/tokens/evm/balance.js +5 -1
- package/cjs/koni/api/tokens/evm/transfer.js +8 -4
- package/cjs/koni/background/cron.js +2 -2
- package/cjs/koni/background/handlers/Extension.js +5 -5
- package/cjs/koni/background/handlers/Mobile.js +1 -1
- package/cjs/koni/background/handlers/State.js +123 -44
- package/cjs/koni/background/subscription.js +2 -2
- package/cjs/packageInfo.js +1 -1
- package/cjs/services/balance-service/helpers/group.js +53 -0
- package/cjs/services/balance-service/helpers/subscribe/balance.js +111 -0
- package/cjs/services/balance-service/helpers/subscribe/evm.js +95 -0
- package/cjs/services/balance-service/helpers/subscribe/substrate/equilibrium.js +113 -0
- package/cjs/services/balance-service/helpers/subscribe/substrate/index.js +324 -0
- package/cjs/services/balance-service/index.js +41 -16
- package/cjs/services/chain-service/constants.js +4 -3
- package/cjs/services/history-service/helpers/subscan-extrinsic-parser-helper.js +7 -1
- package/cjs/services/history-service/index.js +12 -8
- package/cjs/services/migration-service/scripts/MigrateProvider.js +1 -1
- package/cjs/services/storage-service/DatabaseService.js +7 -2
- package/cjs/services/storage-service/db-stores/Balance.js +9 -9
- package/cjs/services/subscan-service/index.js +66 -22
- package/cjs/services/transaction-service/index.js +4 -3
- package/cjs/types/balance.js +1 -0
- package/cjs/types/index.js +11 -0
- package/cjs/utils/{address.js → account.js} +32 -2
- package/cjs/utils/eth.js +7 -2
- package/cjs/utils/index.js +12 -0
- package/constants/index.d.ts +2 -0
- package/constants/index.js +2 -0
- package/koni/api/staking/bonding/amplitude.d.ts +3 -0
- package/koni/api/staking/bonding/amplitude.js +6 -4
- package/koni/api/staking/bonding/astar.d.ts +3 -0
- package/koni/api/staking/bonding/astar.js +4 -0
- package/koni/api/staking/bonding/index.d.ts +3 -0
- package/koni/api/staking/bonding/index.js +5 -0
- package/koni/api/staking/bonding/paraChain.d.ts +3 -0
- package/koni/api/staking/bonding/paraChain.js +9 -20
- package/koni/api/staking/bonding/relayChain.d.ts +3 -0
- package/koni/api/staking/bonding/relayChain.js +41 -24
- package/koni/api/staking/bonding/utils.d.ts +15 -2
- package/koni/api/staking/bonding/utils.js +69 -18
- package/koni/api/tokens/evm/balance.js +5 -1
- package/koni/api/tokens/evm/transfer.d.ts +1 -1
- package/koni/api/tokens/evm/transfer.js +8 -4
- package/koni/background/cron.js +3 -3
- package/koni/background/handlers/Extension.js +5 -5
- package/koni/background/handlers/Mobile.js +1 -1
- package/koni/background/handlers/State.d.ts +6 -4
- package/koni/background/handlers/State.js +111 -33
- package/koni/background/subscription.js +2 -2
- package/package.json +40 -15
- package/packageInfo.js +1 -1
- package/services/balance-service/helpers/group.d.ts +9 -0
- package/services/balance-service/helpers/group.js +46 -0
- package/services/balance-service/helpers/subscribe/balance.d.ts +4 -0
- package/services/balance-service/helpers/subscribe/balance.js +103 -0
- package/services/balance-service/helpers/subscribe/evm.d.ts +5 -0
- package/services/balance-service/helpers/subscribe/evm.js +87 -0
- package/services/balance-service/helpers/subscribe/substrate/equilibrium.d.ts +4 -0
- package/services/balance-service/helpers/subscribe/substrate/equilibrium.js +105 -0
- package/services/balance-service/helpers/subscribe/substrate/index.d.ts +4 -0
- package/services/balance-service/helpers/subscribe/substrate/index.js +316 -0
- package/services/balance-service/index.d.ts +24 -5
- package/services/balance-service/index.js +40 -14
- package/services/chain-service/constants.js +4 -3
- package/services/history-service/helpers/subscan-extrinsic-parser-helper.js +7 -1
- package/services/history-service/index.js +12 -8
- package/services/migration-service/scripts/MigrateProvider.js +1 -1
- package/services/storage-service/DatabaseService.d.ts +4 -2
- package/services/storage-service/DatabaseService.js +7 -2
- package/services/storage-service/databases/index.d.ts +2 -1
- package/services/storage-service/db-stores/Balance.d.ts +2 -2
- package/services/storage-service/db-stores/Balance.js +9 -9
- package/services/subscan-service/index.d.ts +11 -5
- package/services/subscan-service/index.js +66 -26
- package/services/subscan-service/types.d.ts +4 -0
- package/services/transaction-service/index.js +5 -4
- package/types/balance.d.ts +40 -0
- package/types/balance.js +1 -0
- package/types/index.d.ts +1 -0
- package/types/index.js +1 -0
- package/utils/account.d.ts +15 -0
- package/utils/{address.js → account.js} +28 -0
- package/utils/eth.d.ts +1 -0
- package/utils/eth.js +4 -0
- package/utils/index.d.ts +1 -0
- package/utils/index.js +1 -0
- package/cjs/koni/api/dotsama/balance.js +0 -464
- package/koni/api/dotsama/balance.d.ts +0 -6
- package/koni/api/dotsama/balance.js +0 -451
- package/utils/address.d.ts +0 -5
|
@@ -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
|
|
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
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
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(([
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
const
|
|
673
|
-
if (
|
|
674
|
-
|
|
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.
|
|
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
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
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(
|
|
736
|
-
|
|
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
|
|
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
|
|
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
|
|
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-
|
|
20
|
+
"version": "1.1.24-2",
|
|
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/
|
|
1217
|
-
"types": "./utils/
|
|
1218
|
-
"require": "./cjs/utils/
|
|
1219
|
-
"default": "./utils/
|
|
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",
|
|
@@ -1322,11 +1347,11 @@
|
|
|
1322
1347
|
"@reduxjs/toolkit": "^1.9.1",
|
|
1323
1348
|
"@sora-substrate/type-definitions": "^1.17.7",
|
|
1324
1349
|
"@substrate/connect": "^0.7.26",
|
|
1325
|
-
"@subwallet/chain-list": "0.2.
|
|
1326
|
-
"@subwallet/extension-base": "^1.1.24-
|
|
1327
|
-
"@subwallet/extension-chains": "^1.1.24-
|
|
1328
|
-
"@subwallet/extension-dapp": "^1.1.24-
|
|
1329
|
-
"@subwallet/extension-inject": "^1.1.24-
|
|
1350
|
+
"@subwallet/chain-list": "0.2.27",
|
|
1351
|
+
"@subwallet/extension-base": "^1.1.24-2",
|
|
1352
|
+
"@subwallet/extension-chains": "^1.1.24-2",
|
|
1353
|
+
"@subwallet/extension-dapp": "^1.1.24-2",
|
|
1354
|
+
"@subwallet/extension-inject": "^1.1.24-2",
|
|
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-
|
|
10
|
+
version: '1.1.24-2'
|
|
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;
|
|
@@ -0,0 +1,87 @@
|
|
|
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 { ASTAR_REFRESH_BALANCE_INTERVAL, SUB_TOKEN_REFRESH_BALANCE_INTERVAL } from '@subwallet/extension-base/constants';
|
|
7
|
+
import { getEVMBalance } from '@subwallet/extension-base/koni/api/tokens/evm/balance';
|
|
8
|
+
import { getERC20Contract } from '@subwallet/extension-base/koni/api/tokens/evm/web3';
|
|
9
|
+
import { state } from '@subwallet/extension-base/koni/background/handlers';
|
|
10
|
+
import { _getContractAddressOfToken } from '@subwallet/extension-base/services/chain-service/utils';
|
|
11
|
+
import { BN } from '@polkadot/util';
|
|
12
|
+
export function subscribeERC20Interval(addresses, chain, evmApiMap, callBack) {
|
|
13
|
+
let tokenList = {};
|
|
14
|
+
const erc20ContractMap = {};
|
|
15
|
+
const getTokenBalances = () => {
|
|
16
|
+
Object.values(tokenList).map(async tokenInfo => {
|
|
17
|
+
try {
|
|
18
|
+
const contract = erc20ContractMap[tokenInfo.slug];
|
|
19
|
+
const balances = await Promise.all(addresses.map(async address => {
|
|
20
|
+
try {
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
|
|
22
|
+
return await contract.methods.balanceOf(address).call();
|
|
23
|
+
} catch (e) {
|
|
24
|
+
console.error(`Error on get balance of account ${address} for token ${tokenInfo.slug}`, e);
|
|
25
|
+
return '0';
|
|
26
|
+
}
|
|
27
|
+
}));
|
|
28
|
+
const items = balances.map((balance, index) => {
|
|
29
|
+
return {
|
|
30
|
+
address: addresses[index],
|
|
31
|
+
tokenSlug: tokenInfo.slug,
|
|
32
|
+
free: new BN(balance || 0).toString(),
|
|
33
|
+
locked: '0',
|
|
34
|
+
state: APIItemState.READY
|
|
35
|
+
};
|
|
36
|
+
});
|
|
37
|
+
callBack(items);
|
|
38
|
+
} catch (err) {
|
|
39
|
+
console.log(tokenInfo.slug, err);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
tokenList = state.getAssetByChainAndAsset(chain, [_AssetType.ERC20]);
|
|
44
|
+
Object.entries(tokenList).forEach(([slug, tokenInfo]) => {
|
|
45
|
+
erc20ContractMap[slug] = getERC20Contract(chain, _getContractAddressOfToken(tokenInfo), evmApiMap);
|
|
46
|
+
});
|
|
47
|
+
getTokenBalances();
|
|
48
|
+
const interval = setInterval(getTokenBalances, SUB_TOKEN_REFRESH_BALANCE_INTERVAL);
|
|
49
|
+
return () => {
|
|
50
|
+
clearInterval(interval);
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
export function subscribeEVMBalance(chain, addresses, evmApiMap, callback, tokenInfo) {
|
|
54
|
+
function getBalance() {
|
|
55
|
+
getEVMBalance(chain, addresses, evmApiMap).then(balances => {
|
|
56
|
+
return balances.map((balance, index) => {
|
|
57
|
+
return {
|
|
58
|
+
address: addresses[index],
|
|
59
|
+
tokenSlug: tokenInfo.slug,
|
|
60
|
+
state: APIItemState.READY,
|
|
61
|
+
free: new BN(balance || '0').toString(),
|
|
62
|
+
locked: '0'
|
|
63
|
+
};
|
|
64
|
+
});
|
|
65
|
+
}).catch(e => {
|
|
66
|
+
console.error(`Error on get native balance with token ${tokenInfo.slug}`, e);
|
|
67
|
+
return addresses.map(address => {
|
|
68
|
+
return {
|
|
69
|
+
address: address,
|
|
70
|
+
tokenSlug: tokenInfo.slug,
|
|
71
|
+
state: APIItemState.READY,
|
|
72
|
+
free: '0',
|
|
73
|
+
locked: '0'
|
|
74
|
+
};
|
|
75
|
+
});
|
|
76
|
+
}).then(items => {
|
|
77
|
+
callback(items);
|
|
78
|
+
}).catch(console.error);
|
|
79
|
+
}
|
|
80
|
+
getBalance();
|
|
81
|
+
const interval = setInterval(getBalance, ASTAR_REFRESH_BALANCE_INTERVAL);
|
|
82
|
+
const unsub2 = subscribeERC20Interval(addresses, chain, evmApiMap, callback);
|
|
83
|
+
return () => {
|
|
84
|
+
clearInterval(interval);
|
|
85
|
+
unsub2 && unsub2();
|
|
86
|
+
};
|
|
87
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { BalanceItem } from '@subwallet/extension-base/types';
|
|
2
|
+
import { ApiPromise } from '@polkadot/api';
|
|
3
|
+
export declare function subscribeEquilibriumTokenBalance(addresses: string[], chain: string, api: ApiPromise, callBack: (rs: BalanceItem[]) => void, includeNativeToken?: boolean): Promise<() => void>;
|
|
4
|
+
export declare function subscribeEqBalanceAccountPallet(addresses: string[], chain: string, api: ApiPromise, callBack: (rs: BalanceItem[]) => void, includeNativeToken?: boolean): Promise<() => void>;
|