@subwallet/extension-base 1.1.38-1 → 1.1.40-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 (54) hide show
  1. package/background/KoniTypes.d.ts +1 -0
  2. package/background/types.d.ts +1 -0
  3. package/cjs/koni/api/nft/unique_network_nft/index.js +4 -0
  4. package/cjs/koni/background/handlers/Extension.js +48 -12
  5. package/cjs/koni/background/handlers/State.js +6 -5
  6. package/cjs/packageInfo.js +1 -1
  7. package/cjs/services/balance-service/helpers/subscribe/balance.js +1 -1
  8. package/cjs/services/chain-service/health-check/constants/index.js +36 -0
  9. package/cjs/services/chain-service/health-check/utils/asset-info.js +282 -0
  10. package/cjs/services/chain-service/health-check/utils/chain-info.js +36 -0
  11. package/cjs/services/chain-service/health-check/utils/index.js +38 -0
  12. package/cjs/services/chain-service/health-check/utils/provider.js +186 -0
  13. package/cjs/services/earning-service/handlers/native-staking/relay-chain.js +42 -26
  14. package/cjs/services/earning-service/service.js +9 -2
  15. package/cjs/services/transaction-service/index.js +6 -4
  16. package/cjs/utils/eth/mergeTransactionAndSignature.js +8 -3
  17. package/cjs/utils/index.js +6 -5
  18. package/cjs/utils/staticData/index.js +5 -5
  19. package/koni/api/nft/unique_network_nft/index.d.ts +1 -0
  20. package/koni/api/nft/unique_network_nft/index.js +5 -1
  21. package/koni/api/staking/bonding/utils.d.ts +0 -1
  22. package/koni/background/handlers/Extension.js +48 -12
  23. package/koni/background/handlers/State.js +2 -1
  24. package/package.json +41 -16
  25. package/packageInfo.js +1 -1
  26. package/services/balance-service/helpers/subscribe/balance.js +1 -1
  27. package/services/chain-service/health-check/constants/index.d.ts +4 -0
  28. package/services/chain-service/health-check/constants/index.js +26 -0
  29. package/services/chain-service/health-check/utils/asset-info.d.ts +14 -0
  30. package/services/chain-service/health-check/utils/asset-info.js +269 -0
  31. package/services/chain-service/health-check/utils/chain-info.d.ts +8 -0
  32. package/services/chain-service/health-check/utils/chain-info.js +28 -0
  33. package/services/chain-service/health-check/utils/index.d.ts +3 -0
  34. package/services/chain-service/health-check/utils/index.js +6 -0
  35. package/services/chain-service/health-check/utils/provider.d.ts +25 -0
  36. package/services/chain-service/health-check/utils/provider.js +172 -0
  37. package/services/earning-service/handlers/native-staking/relay-chain.d.ts +0 -1
  38. package/services/earning-service/handlers/native-staking/relay-chain.js +42 -26
  39. package/services/earning-service/handlers/special.d.ts +0 -1
  40. package/services/earning-service/service.js +10 -3
  41. package/services/transaction-service/index.js +6 -4
  42. package/types/balance.d.ts +0 -1
  43. package/types/yield/info/pallet.d.ts +4 -1
  44. package/utils/eth/mergeTransactionAndSignature.js +8 -3
  45. package/utils/index.d.ts +1 -2
  46. package/utils/index.js +6 -5
  47. package/utils/staticData/buyServiceInfos.json +0 -1
  48. package/utils/staticData/buyTokenConfigs.json +0 -15
  49. package/utils/staticData/chains.json +192 -89
  50. package/utils/staticData/crowdloanFunds.json +433 -58
  51. package/utils/staticData/index.d.ts +1 -2
  52. package/utils/staticData/index.js +4 -3
  53. package/utils/staticData/marketingCampaigns.json +1 -298
  54. package/utils/staticData/termAndCondition.json +1 -1
package/package.json CHANGED
@@ -17,7 +17,7 @@
17
17
  "./cjs/detectPackage.js"
18
18
  ],
19
19
  "type": "module",
20
- "version": "1.1.38-1",
20
+ "version": "1.1.40-0",
21
21
  "main": "./cjs/index.js",
22
22
  "module": "./index.js",
23
23
  "types": "./index.d.ts",
@@ -670,6 +670,31 @@
670
670
  "require": "./cjs/services/chain-service/health-check/index.js",
671
671
  "default": "./services/chain-service/health-check/index.js"
672
672
  },
673
+ "./services/chain-service/health-check/constants": {
674
+ "types": "./services/chain-service/health-check/constants/index.d.ts",
675
+ "require": "./cjs/services/chain-service/health-check/constants/index.js",
676
+ "default": "./services/chain-service/health-check/constants/index.js"
677
+ },
678
+ "./services/chain-service/health-check/utils": {
679
+ "types": "./services/chain-service/health-check/utils/index.d.ts",
680
+ "require": "./cjs/services/chain-service/health-check/utils/index.js",
681
+ "default": "./services/chain-service/health-check/utils/index.js"
682
+ },
683
+ "./services/chain-service/health-check/utils/asset-info": {
684
+ "types": "./services/chain-service/health-check/utils/asset-info.d.ts",
685
+ "require": "./cjs/services/chain-service/health-check/utils/asset-info.js",
686
+ "default": "./services/chain-service/health-check/utils/asset-info.js"
687
+ },
688
+ "./services/chain-service/health-check/utils/chain-info": {
689
+ "types": "./services/chain-service/health-check/utils/chain-info.d.ts",
690
+ "require": "./cjs/services/chain-service/health-check/utils/chain-info.js",
691
+ "default": "./services/chain-service/health-check/utils/chain-info.js"
692
+ },
693
+ "./services/chain-service/health-check/utils/provider": {
694
+ "types": "./services/chain-service/health-check/utils/provider.d.ts",
695
+ "require": "./cjs/services/chain-service/health-check/utils/provider.js",
696
+ "default": "./services/chain-service/health-check/utils/provider.js"
697
+ },
673
698
  "./services/chain-service/helper": {
674
699
  "types": "./services/chain-service/helper/index.d.ts",
675
700
  "require": "./cjs/services/chain-service/helper/index.js",
@@ -1719,28 +1744,28 @@
1719
1744
  "@metamask/safe-event-emitter": "^2.0.0",
1720
1745
  "@metaverse-network-sdk/type-definitions": "^0.0.1-13",
1721
1746
  "@oak-foundation/types": "^0.0.23",
1722
- "@polkadot/api": "^10.11.1",
1747
+ "@polkadot/api": "^10.11.2",
1723
1748
  "@polkadot/api-base": "^10.9.1",
1724
- "@polkadot/api-contract": "^10.11.1",
1725
- "@polkadot/api-derive": "^10.11.1",
1749
+ "@polkadot/api-contract": "^10.11.2",
1750
+ "@polkadot/api-derive": "^10.11.2",
1726
1751
  "@polkadot/apps-config": "^0.133.1",
1727
- "@polkadot/hw-ledger": "^12.6.1",
1728
- "@polkadot/networks": "^12.6.1",
1752
+ "@polkadot/hw-ledger": "^12.6.2",
1753
+ "@polkadot/networks": "^12.6.2",
1729
1754
  "@polkadot/phishing": "^0.22.1",
1730
- "@polkadot/rpc-provider": "^10.11.1",
1731
- "@polkadot/types": "^10.11.1",
1732
- "@polkadot/types-augment": "^10.11.1",
1755
+ "@polkadot/rpc-provider": "^10.11.2",
1756
+ "@polkadot/types": "^10.11.2",
1757
+ "@polkadot/types-augment": "^10.11.2",
1733
1758
  "@polkadot/ui-settings": "^3.6.4",
1734
- "@polkadot/util": "^12.6.1",
1735
- "@polkadot/util-crypto": "^12.6.1",
1759
+ "@polkadot/util": "^12.6.2",
1760
+ "@polkadot/util-crypto": "^12.6.2",
1736
1761
  "@reduxjs/toolkit": "^1.9.1",
1737
1762
  "@sora-substrate/type-definitions": "^1.17.7",
1738
1763
  "@substrate/connect": "^0.7.26",
1739
- "@subwallet/chain-list": "0.2.41",
1740
- "@subwallet/extension-base": "^1.1.38-1",
1741
- "@subwallet/extension-chains": "^1.1.38-1",
1742
- "@subwallet/extension-dapp": "^1.1.38-1",
1743
- "@subwallet/extension-inject": "^1.1.38-1",
1764
+ "@subwallet/chain-list": "0.2.42-beta.0",
1765
+ "@subwallet/extension-base": "^1.1.40-0",
1766
+ "@subwallet/extension-chains": "^1.1.40-0",
1767
+ "@subwallet/extension-dapp": "^1.1.40-0",
1768
+ "@subwallet/extension-inject": "^1.1.40-0",
1744
1769
  "@subwallet/keyring": "^0.1.3",
1745
1770
  "@subwallet/ui-keyring": "^0.1.3",
1746
1771
  "@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.38-1'
10
+ version: '1.1.40-0'
11
11
  };
@@ -40,7 +40,7 @@ const filterAddress = (addresses, chainInfo) => {
40
40
  const isEvmChain = _isChainEvmCompatible(chainInfo);
41
41
  const [substrateAddresses, evmAddresses] = categoryAddresses(addresses);
42
42
  if (isEvmChain) {
43
- return [evmAddresses, []];
43
+ return [evmAddresses, substrateAddresses];
44
44
  } else {
45
45
  const fetchList = [];
46
46
  const unfetchList = [];
@@ -0,0 +1,4 @@
1
+ import BigN from 'bignumber.js';
2
+ export declare const chainProvider: Record<string, number>;
3
+ export declare const chainProviderBackup: Record<string, number>;
4
+ export declare const BIG_TEN: BigN;
@@ -0,0 +1,26 @@
1
+ // Copyright 2019-2022 @subwallet/extension-base authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import BigN from 'bignumber.js';
5
+ export const chainProvider = {
6
+ default: 0,
7
+ ethereum: 1,
8
+ polygon: 2,
9
+ shidenEvm: 2,
10
+ shiden: 2,
11
+ ajunaPolkadot: 1,
12
+ crabParachain: 1,
13
+ astarEvm: 1,
14
+ shibuya: 1,
15
+ shibuyaEvm: 0
16
+ };
17
+ export const chainProviderBackup = {
18
+ default: 1,
19
+ pangolin: 0,
20
+ moonbeam: 0,
21
+ moonbase: 0,
22
+ moonriver: 3,
23
+ darwinia2: 2,
24
+ crabParachain: 1
25
+ };
26
+ export const BIG_TEN = new BigN(10);
@@ -0,0 +1,14 @@
1
+ import { _ChainAsset } from '@subwallet/chain-list/types';
2
+ import { _EvmApi } from '@subwallet/extension-base/services/chain-service/types';
3
+ import { ApiPromise } from '@polkadot/api';
4
+ export interface AssetSpec {
5
+ minAmount: string;
6
+ symbol: string;
7
+ decimals: number;
8
+ }
9
+ export declare const getLocalAssetInfo: (chain: string, asset: _ChainAsset, api: ApiPromise) => Promise<AssetSpec>;
10
+ export declare const getSubstrateNativeInfo: (api: ApiPromise) => Promise<AssetSpec>;
11
+ export declare const getPsp22AssetInfo: (asset: _ChainAsset, api: ApiPromise) => Promise<AssetSpec>;
12
+ export declare const getEvmNativeInfo: (api: _EvmApi) => Promise<AssetSpec>;
13
+ export declare const getErc20AssetInfo: (asset: _ChainAsset, api: _EvmApi) => Promise<AssetSpec>;
14
+ export declare const compareAsset: (assetInfo: AssetSpec, asset: _ChainAsset, errors: string[]) => void;
@@ -0,0 +1,269 @@
1
+ // Copyright 2019-2022 @subwallet/extension-base authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { getERC20Contract } from '@subwallet/extension-base/koni/api/tokens/evm/web3';
5
+ import { getDefaultWeightV2 } from '@subwallet/extension-base/koni/api/tokens/wasm/utils';
6
+ import { _PSP22_ABI } from '@subwallet/extension-base/services/chain-service/helper';
7
+ import { _getContractAddressOfToken, _getTokenOnChainAssetId, _getTokenOnChainInfo } from '@subwallet/extension-base/services/chain-service/utils';
8
+ import BigN from 'bignumber.js';
9
+ import { ContractPromise } from '@polkadot/api-contract';
10
+ const BN_TEN = new BigN(10);
11
+ const getByAssetPallet = async (asset, api) => {
12
+ const [_info, _metadata] = await api.queryMulti([[api.query.assets.asset, _getTokenOnChainAssetId(asset)], [api.query.assets.metadata, _getTokenOnChainAssetId(asset)]]);
13
+ const info = _info.toPrimitive();
14
+ const metadata = _metadata.toPrimitive();
15
+ return {
16
+ decimals: metadata.decimals,
17
+ minAmount: info.minBalance.toString(),
18
+ symbol: metadata.symbol
19
+ };
20
+ };
21
+ const getByAssetRegistryWithChainInfoPallet = async (asset, api) => {
22
+ const raw = _getTokenOnChainInfo(asset);
23
+ let data;
24
+ if ('ForeignAsset' in raw) {
25
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
26
+ data = {
27
+ ForeignAssetId: raw.ForeignAsset
28
+ };
29
+ } else if ('Erc20' in raw) {
30
+ data = raw;
31
+ } else if ('StableAssetPoolToken' in raw) {
32
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
33
+ data = {
34
+ StableAssetId: raw.StableAssetPoolToken
35
+ };
36
+ } else {
37
+ data = {
38
+ NativeAssetId: raw
39
+ };
40
+ }
41
+ const _metadata = await api.query.assetRegistry.assetMetadatas(data);
42
+ const metadata = _metadata.toPrimitive();
43
+ return {
44
+ decimals: metadata.decimals,
45
+ minAmount: metadata.minimalBalance.toString(),
46
+ symbol: metadata.symbol
47
+ };
48
+ };
49
+ const getByAssetRegistryWithChainInfoPalletV2 = async (asset, api) => {
50
+ const data = _getTokenOnChainInfo(asset);
51
+ const _metadata = await api.query.assetRegistry.currencyMetadatas(data);
52
+ const metadata = _metadata.toPrimitive();
53
+ return {
54
+ decimals: metadata.decimals,
55
+ minAmount: metadata.minimalBalance.toString(),
56
+ symbol: metadata.symbol
57
+ };
58
+ };
59
+ const getByAssetRegistryWithChainInfoPalletV3 = async (asset, api) => {
60
+ const data = _getTokenOnChainInfo(asset);
61
+ const _metadata = await api.query.assetRegistry.metadata(data);
62
+ const metadata = _metadata.toPrimitive();
63
+ return {
64
+ decimals: metadata.decimals,
65
+ minAmount: metadata.existentialDeposit.toString(),
66
+ symbol: metadata.symbol
67
+ };
68
+ };
69
+ const getByOrmlAssetRegistryPallet = async (asset, api) => {
70
+ const data = _getTokenOnChainInfo(asset);
71
+ const _metadata = await api.query.ormlAssetRegistry.metadata(data);
72
+ const metadata = _metadata.toPrimitive();
73
+ return {
74
+ decimals: metadata.decimals,
75
+ minAmount: metadata.existentialDeposit.toString(),
76
+ symbol: metadata.symbol
77
+ };
78
+ };
79
+ const getByAssetManagerWithChainInfoPallet = async (asset, api) => {
80
+ const raw = _getTokenOnChainInfo(asset);
81
+ let data;
82
+ if ('ForeignAsset' in raw) {
83
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
84
+ data = {
85
+ ForeignAssetId: raw.ForeignAsset
86
+ };
87
+ } else if ('Erc20' in raw) {
88
+ data = raw;
89
+ } else {
90
+ data = {
91
+ NativeAssetId: raw
92
+ };
93
+ }
94
+ const _metadata = await api.query.assetManager.assetMetadatas(data);
95
+ const metadata = _metadata.toPrimitive();
96
+ return {
97
+ decimals: metadata.decimals,
98
+ minAmount: metadata.minimalBalance.toString(),
99
+ symbol: metadata.symbol
100
+ };
101
+ };
102
+ const getByAssetManagerWithAssetIdPallet = async (asset, api) => {
103
+ const _metadata = await api.query.assetManager.assetIdMetadata(_getTokenOnChainAssetId(asset));
104
+ const metadata = _metadata.toPrimitive();
105
+ return {
106
+ decimals: metadata.metadata.decimals,
107
+ minAmount: metadata.minBalance.toString(),
108
+ symbol: metadata.metadata.symbol
109
+ };
110
+ };
111
+ const getByAssetRegistryWithAssetIdPallet = async (asset, api) => {
112
+ const [_info, _metadata] = await api.queryMulti([[api.query.assetRegistry.assets, _getTokenOnChainAssetId(asset)], [api.query.assetRegistry.assetMetadataMap, _getTokenOnChainAssetId(asset)]]);
113
+ const info = _info.toPrimitive();
114
+ const metadata = _metadata.toPrimitive();
115
+ return {
116
+ decimals: metadata.decimals,
117
+ minAmount: info.existentialDeposit.toString(),
118
+ symbol: metadata.symbol
119
+ };
120
+ };
121
+ export const getLocalAssetInfo = async (chain, asset, api) => {
122
+ if (['astar', 'shiden', 'shibuya', 'statemint', 'statemine', 'moonbeam', 'moonbase', 'moonriver', 'parallel', 'pangolin', 'darwinia2', 'crabParachain'].includes(chain)) {
123
+ return getByAssetPallet(asset, api);
124
+ } else if (['pioneer', 'bitcountry'].includes(chain)) {
125
+ return getByAssetManagerWithChainInfoPallet(asset, api);
126
+ } else if (['acala', 'karura', 'acala_testnet'].includes(chain)) {
127
+ return getByAssetRegistryWithChainInfoPallet(asset, api);
128
+ } else if (['bifrost', 'bifrost_dot', 'bifrost_testnet'].includes(chain)) {
129
+ return getByAssetRegistryWithChainInfoPalletV2(asset, api);
130
+ } else if (['hydradx_main'].includes(chain)) {
131
+ return getByAssetRegistryWithAssetIdPallet(asset, api);
132
+ } else if (['calamari', 'manta_network'].includes(chain)) {
133
+ return getByAssetManagerWithAssetIdPallet(asset, api);
134
+ } else if (['amplitude', 'pendulum'].includes(chain)) {
135
+ return getByAssetRegistryWithChainInfoPalletV3(asset, api);
136
+ } else if (['centrifuge'].includes(chain)) {
137
+ return getByOrmlAssetRegistryPallet(asset, api);
138
+ }
139
+ throw new Error('Fail to get info');
140
+ };
141
+
142
+ // eslint-disable-next-line @typescript-eslint/require-await
143
+ export const getSubstrateNativeInfo = async api => {
144
+ const minAmount = api.consts.balances ? api.consts.balances.existentialDeposit.toString() : api.consts.eqBalances.existentialDeposit.toString();
145
+ const symbol = api.registry.chainTokens[0];
146
+ const decimals = api.registry.chainDecimals[0];
147
+ return {
148
+ decimals,
149
+ minAmount,
150
+ symbol
151
+ };
152
+ };
153
+ export const getPsp22AssetInfo = async (asset, api) => {
154
+ const contractAddress = _getContractAddressOfToken(asset);
155
+ const tokenContract = new ContractPromise(api, _PSP22_ABI, contractAddress);
156
+ const [nameResp, symbolResp, decimalsResp] = await Promise.all([tokenContract.query['psp22Metadata::tokenName'](contractAddress, {
157
+ gasLimit: getDefaultWeightV2(api)
158
+ }),
159
+ // read-only operation so no gas limit
160
+ tokenContract.query['psp22Metadata::tokenSymbol'](contractAddress, {
161
+ gasLimit: getDefaultWeightV2(api)
162
+ }), tokenContract.query['psp22Metadata::tokenDecimals'](contractAddress, {
163
+ gasLimit: getDefaultWeightV2(api)
164
+ })]);
165
+ if (!(nameResp.result.isOk && symbolResp.result.isOk && decimalsResp.result.isOk) || !nameResp.output || !decimalsResp.output || !symbolResp.output) {
166
+ return {
167
+ decimals: -1,
168
+ symbol: '',
169
+ minAmount: '0'
170
+ };
171
+ } else {
172
+ var _symbolResp$output, _decimalsResp$output, _nameResp$output;
173
+ const symbolObj = (_symbolResp$output = symbolResp.output) === null || _symbolResp$output === void 0 ? void 0 : _symbolResp$output.toHuman();
174
+ const decimalsObj = (_decimalsResp$output = decimalsResp.output) === null || _decimalsResp$output === void 0 ? void 0 : _decimalsResp$output.toHuman();
175
+ const nameObj = (_nameResp$output = nameResp.output) === null || _nameResp$output === void 0 ? void 0 : _nameResp$output.toHuman();
176
+ const name = nameResp.output ? nameObj.Ok || nameObj.ok : '';
177
+ const decimals = decimalsResp.output ? new BigN(decimalsObj.Ok || decimalsObj.ok).toNumber() : 0;
178
+ const symbol = decimalsResp.output ? symbolObj.Ok || symbolObj.ok : '';
179
+ if (!name || !symbol || typeof name === 'object' || typeof symbol === 'object') {
180
+ return {
181
+ decimals: -1,
182
+ symbol: '',
183
+ minAmount: '0'
184
+ };
185
+ }
186
+ return {
187
+ decimals: decimals,
188
+ symbol: symbol,
189
+ minAmount: '0'
190
+ };
191
+ }
192
+ };
193
+ export const getEvmNativeInfo = async api => {
194
+ const chainId = await api.api.eth.getChainId();
195
+ const fetchData = () => {
196
+ return new Promise((resolve, reject) => {
197
+ fetch('https://chainid.network/chains.json').then(res => {
198
+ return res.json();
199
+ }).then(json => {
200
+ const rs = json.find(i => i.chainId === chainId);
201
+ resolve(rs);
202
+ }).catch(e => {
203
+ reject(e);
204
+ });
205
+ });
206
+ };
207
+ const data = await fetchData();
208
+ if (data) {
209
+ return {
210
+ decimals: data.nativeCurrency.decimals,
211
+ symbol: data.nativeCurrency.symbol,
212
+ minAmount: '0'
213
+ };
214
+ }
215
+ throw new Error('Cannot get info');
216
+ };
217
+ export const getErc20AssetInfo = async (asset, api) => {
218
+ const contractAddress = _getContractAddressOfToken(asset);
219
+ const tokenContract = getERC20Contract('chain', contractAddress, {
220
+ chain: api
221
+ });
222
+ const [_decimals, _symbol, _name] = await Promise.all([
223
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
224
+ tokenContract.methods.decimals().call(),
225
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
226
+ tokenContract.methods.symbol().call(),
227
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
228
+ tokenContract.methods.name().call()]);
229
+ const name = _name;
230
+ const decimals = new BigN(_decimals).toNumber();
231
+ const symbol = _symbol;
232
+ if (!name || !symbol || typeof name === 'object' || typeof symbol === 'object') {
233
+ return {
234
+ decimals: -1,
235
+ symbol: '',
236
+ minAmount: '0'
237
+ };
238
+ }
239
+ return {
240
+ decimals: decimals,
241
+ symbol: symbol,
242
+ minAmount: '0'
243
+ };
244
+ };
245
+ export const compareAsset = (assetInfo, asset, errors) => {
246
+ const {
247
+ decimals,
248
+ minAmount,
249
+ symbol
250
+ } = assetInfo;
251
+ const _minAmount = asset.minAmount || '0';
252
+ const _decimals = asset.decimals || 0;
253
+ if (minAmount !== _minAmount) {
254
+ var _asset$minAmount;
255
+ const convert = new BigN(minAmount).dividedBy(BN_TEN.pow(decimals)).toFixed();
256
+ const _convert = new BigN(_minAmount).dividedBy(BN_TEN.pow(_decimals)).toFixed();
257
+ errors.push(`Wrong min amount: current - ${(_asset$minAmount = asset.minAmount) !== null && _asset$minAmount !== void 0 ? _asset$minAmount : 'null'} (${_convert}), onChain - ${minAmount} (${convert})`);
258
+ }
259
+ if (symbol !== asset.symbol) {
260
+ const zkSymbol = 'zk' + symbol;
261
+ if (zkSymbol !== asset.symbol) {
262
+ errors.push(`Wrong symbol: current - ${asset.symbol}, onChain - ${symbol}`);
263
+ }
264
+ }
265
+ if (decimals !== _decimals) {
266
+ var _asset$decimals;
267
+ errors.push(`Wrong decimals: current - ${(_asset$decimals = asset.decimals) !== null && _asset$decimals !== void 0 ? _asset$decimals : 'null'}, onChain - ${decimals}`);
268
+ }
269
+ };
@@ -0,0 +1,8 @@
1
+ import { AssetSpec } from '@subwallet/extension-base/services/chain-service/health-check/utils/asset-info';
2
+ interface NativeAssetInfo {
3
+ decimals: number;
4
+ existentialDeposit: string;
5
+ symbol: string;
6
+ }
7
+ export declare const compareNativeAsset: (assetInfo: AssetSpec, nativeAsset: NativeAssetInfo, errors: string[]) => void;
8
+ export {};
@@ -0,0 +1,28 @@
1
+ // Copyright 2019-2022 @subwallet/extension-base authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { BIG_TEN } from '@subwallet/extension-base/services/chain-service/health-check/constants';
5
+ import BigN from 'bignumber.js';
6
+ export const compareNativeAsset = (assetInfo, nativeAsset, errors) => {
7
+ const {
8
+ decimals: _decimals,
9
+ existentialDeposit: _minAmount,
10
+ symbol: _symbol
11
+ } = nativeAsset;
12
+ const {
13
+ decimals,
14
+ minAmount,
15
+ symbol
16
+ } = assetInfo;
17
+ if (minAmount !== _minAmount) {
18
+ const convert = new BigN(minAmount).dividedBy(BIG_TEN.pow(decimals)).toFixed();
19
+ const _convert = new BigN(_minAmount !== null && _minAmount !== void 0 ? _minAmount : '0').dividedBy(BIG_TEN.pow(_decimals)).toFixed();
20
+ errors.push(`Wrong min amount: current - ${_minAmount} (${_convert}), onChain - ${minAmount} (${convert})`);
21
+ }
22
+ if (symbol !== _symbol) {
23
+ errors.push(`Wrong symbol: current - ${_symbol}, onChain - ${symbol}`);
24
+ }
25
+ if (decimals !== _decimals) {
26
+ errors.push(`Wrong decimals: current - ${_decimals}, onChain - ${decimals}`);
27
+ }
28
+ };
@@ -0,0 +1,3 @@
1
+ export * from './asset-info';
2
+ export * from './chain-info';
3
+ export * from './provider';
@@ -0,0 +1,6 @@
1
+ // Copyright 2019-2022 @subwallet/extension-base authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export * from "./asset-info.js";
5
+ export * from "./chain-info.js";
6
+ export * from "./provider.js";
@@ -0,0 +1,25 @@
1
+ import { _EvmApi } from '@subwallet/extension-base/services/chain-service/types';
2
+ import { ApiPromise } from '@polkadot/api';
3
+ export declare const failedMessage = "Connect failed";
4
+ export declare const timeoutMessage = "Connect timeout";
5
+ export declare const substrateHandleConnectChain: (chain: string, key: string, provider: string, hash: string) => Promise<[ApiPromise, string]>;
6
+ export declare const evmHandleConnectChain: (chain: string, key: string, provider: string, chainId: number) => Promise<[_EvmApi | null, string]>;
7
+ interface HandleProviderProp {
8
+ chain: string;
9
+ key: string;
10
+ provider: string;
11
+ onTimeout: () => void;
12
+ awaitDisconnect: boolean;
13
+ onError: (message: string) => Promise<void>;
14
+ }
15
+ interface HandleSubstrateProviderProp extends HandleProviderProp {
16
+ genHash: string;
17
+ onSuccess: (api: ApiPromise) => Promise<void>;
18
+ }
19
+ interface HandleEvmProviderProp extends HandleProviderProp {
20
+ chainId: number;
21
+ onSuccess: (api: _EvmApi) => Promise<void>;
22
+ }
23
+ export declare const handleSubstrateProvider: ({ awaitDisconnect, chain, genHash, key, onError, onSuccess, onTimeout, provider }: HandleSubstrateProviderProp) => Promise<void>;
24
+ export declare const handleEvmProvider: ({ awaitDisconnect, chain, chainId, key, onError, onSuccess, onTimeout, provider }: HandleEvmProviderProp) => Promise<void>;
25
+ export {};
@@ -0,0 +1,172 @@
1
+ // Copyright 2019-2022 @subwallet/extension-base authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { EvmApi } from '@subwallet/extension-base/services/chain-service/handler/EvmApi';
5
+ import { ApiPromise, WsProvider } from '@polkadot/api';
6
+ import { noop } from '@polkadot/util';
7
+ export const failedMessage = 'Connect failed';
8
+ export const timeoutMessage = 'Connect timeout';
9
+ export const substrateHandleConnectChain = async (chain, key, provider, hash) => {
10
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises,no-async-promise-executor
11
+ return new Promise(async resolve => {
12
+ console.log('start', chain, key, provider);
13
+ const _api = new ApiPromise({
14
+ provider: new WsProvider(provider)
15
+ });
16
+ let logFail = true;
17
+ const handlerOnFail = e => {
18
+ if (logFail) {
19
+ console.log('error', chain, key);
20
+ resolve([_api, (e === null || e === void 0 ? void 0 : e.message) || failedMessage]);
21
+ }
22
+ logFail = false;
23
+ };
24
+ const timeout = setTimeout(() => {
25
+ console.log('timeout', chain, key);
26
+ resolve([_api, timeoutMessage]);
27
+ logFail = false;
28
+ _api.disconnect().catch(console.error);
29
+ _api.off('disconnected', handlerOnFail);
30
+ _api.off('error', handlerOnFail);
31
+ }, 30 * 1000);
32
+ _api.on('disconnected', handlerOnFail);
33
+ _api.on('error', handlerOnFail);
34
+ const temp = await _api.isReady;
35
+ logFail = false;
36
+ const tempHash = temp.genesisHash.toHex();
37
+ if (hash && hash !== tempHash) {
38
+ resolve([_api, `Wrong genesisHash: current - ${hash}, onChain - ${tempHash}`]);
39
+ }
40
+ await _api.query.system.number();
41
+ _api.off('disconnected', handlerOnFail);
42
+ _api.off('error', handlerOnFail);
43
+ clearTimeout(timeout);
44
+ resolve([_api, '']);
45
+ });
46
+ };
47
+ export const evmHandleConnectChain = async (chain, key, provider, chainId) => {
48
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises,no-async-promise-executor
49
+ return new Promise(async resolve => {
50
+ console.log('start', chain, key, provider);
51
+ let api = null;
52
+ const _api = new EvmApi(chain, provider, {
53
+ providerName: key
54
+ });
55
+ let logFail = true;
56
+ const handlerOnFail = e => {
57
+ if (logFail) {
58
+ console.log('error', chain, key);
59
+ resolve([api, (e === null || e === void 0 ? void 0 : e.message) || failedMessage]);
60
+ }
61
+ logFail = false;
62
+ };
63
+ const timeout = setTimeout(() => {
64
+ console.log('timeout', chain, key);
65
+ resolve([api, timeoutMessage]);
66
+ logFail = false;
67
+ _api.destroy().catch(console.error);
68
+ }, 60 * 1000);
69
+ try {
70
+ api = await _api.isReady;
71
+ } catch (e) {
72
+ handlerOnFail(e);
73
+ }
74
+ logFail = false;
75
+ clearTimeout(timeout);
76
+ const tempId = await _api.api.eth.getChainId();
77
+ if (tempId !== chainId) {
78
+ resolve([api, `Wrong chain id: current - ${chainId}, onChain - ${tempId}`]);
79
+ }
80
+ resolve([api, '']);
81
+ });
82
+ };
83
+ export const handleSubstrateProvider = ({
84
+ awaitDisconnect,
85
+ chain,
86
+ genHash,
87
+ key,
88
+ onError,
89
+ onSuccess,
90
+ onTimeout,
91
+ provider
92
+ }) => {
93
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises,no-async-promise-executor
94
+ return new Promise(async resolve => {
95
+ try {
96
+ const timeHandler = () => {
97
+ onTimeout();
98
+ resolve();
99
+ };
100
+ const timeout = setTimeout(() => {
101
+ timeHandler();
102
+ }, 2 * 60 * 1000);
103
+ const [api, message] = await substrateHandleConnectChain(chain, key, provider, genHash);
104
+ const disconnectApi = async api => {
105
+ if (awaitDisconnect) {
106
+ await (api === null || api === void 0 ? void 0 : api.disconnect());
107
+ } else {
108
+ api === null || api === void 0 ? void 0 : api.disconnect().finally(noop);
109
+ }
110
+ };
111
+ clearTimeout(timeout);
112
+ if (message) {
113
+ await onError(message);
114
+ await disconnectApi(api);
115
+ resolve();
116
+ }
117
+ await onSuccess(api);
118
+ await disconnectApi(api);
119
+ resolve();
120
+ } catch (e) {
121
+ await onError(e.message);
122
+ resolve();
123
+ }
124
+ });
125
+ };
126
+ export const handleEvmProvider = ({
127
+ awaitDisconnect,
128
+ chain,
129
+ chainId,
130
+ key,
131
+ onError,
132
+ onSuccess,
133
+ onTimeout,
134
+ provider
135
+ }) => {
136
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises,no-async-promise-executor
137
+ return new Promise(async resolve => {
138
+ try {
139
+ const timeHandler = () => {
140
+ onTimeout();
141
+ resolve();
142
+ };
143
+ const timeout = setTimeout(() => {
144
+ timeHandler();
145
+ }, 2 * 60 * 1000);
146
+ const [_api, message] = await evmHandleConnectChain(chain, key, provider, chainId);
147
+ const disconnectApi = async _api => {
148
+ if (awaitDisconnect) {
149
+ await (_api === null || _api === void 0 ? void 0 : _api.destroy());
150
+ } else {
151
+ _api === null || _api === void 0 ? void 0 : _api.destroy().finally(noop);
152
+ }
153
+ };
154
+ console.log('connected', chain, key, provider);
155
+ clearTimeout(timeout);
156
+ if (message) {
157
+ await onError(message);
158
+ await disconnectApi(_api);
159
+ resolve();
160
+ }
161
+
162
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
163
+ const api = _api;
164
+ await onSuccess(api);
165
+ await disconnectApi(_api);
166
+ resolve();
167
+ } catch (e) {
168
+ await onError(e.message);
169
+ resolve();
170
+ }
171
+ });
172
+ };