@subwallet/extension-base 1.0.1 → 1.0.2-1b

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 (220) hide show
  1. package/background/KoniTypes.d.ts +25 -36
  2. package/background/KoniTypes.js +12 -7
  3. package/background/errors/TransactionError.js +21 -1
  4. package/cjs/background/KoniTypes.js +14 -8
  5. package/cjs/background/errors/TransactionError.js +20 -0
  6. package/cjs/constants/index.js +13 -28
  7. package/cjs/koni/api/dotsama/balance.js +60 -224
  8. package/cjs/koni/api/dotsama/transfer.js +30 -29
  9. package/cjs/koni/api/nft/acala_nft/index.js +4 -1
  10. package/cjs/koni/api/nft/bit.country/index.js +4 -1
  11. package/cjs/koni/api/nft/evm_nft/index.js +7 -3
  12. package/cjs/koni/api/nft/index.js +3 -6
  13. package/cjs/koni/api/nft/karura_nft/index.js +4 -1
  14. package/cjs/koni/api/nft/rmrk_nft/index.js +8 -1
  15. package/cjs/koni/api/nft/statemine_nft/index.js +4 -1
  16. package/cjs/koni/api/nft/unique_nft/index.js +6 -1
  17. package/cjs/koni/api/nft/wasm_nft/index.js +169 -111
  18. package/cjs/koni/api/nft/wasm_nft/utils.js +11 -7
  19. package/cjs/koni/api/staking/bonding/amplitude.js +18 -10
  20. package/cjs/koni/api/staking/bonding/astar.js +37 -238
  21. package/cjs/koni/api/staking/bonding/index.js +26 -14
  22. package/cjs/koni/api/staking/bonding/paraChain.js +100 -11
  23. package/cjs/koni/api/staking/bonding/relayChain.js +174 -16
  24. package/cjs/koni/api/staking/bonding/utils.js +55 -10
  25. package/cjs/koni/api/tokens/wasm/index.js +5 -4
  26. package/cjs/koni/api/tokens/wasm/utils.js +63 -0
  27. package/cjs/koni/api/xcm/index.js +0 -111
  28. package/cjs/koni/api/xcm/polkadotXcm.js +2 -2
  29. package/cjs/koni/api/xcm/utils.js +18 -13
  30. package/cjs/koni/api/xcm/xTokens.js +3 -5
  31. package/cjs/koni/api/xcm/xcmPallet.js +9 -6
  32. package/cjs/koni/background/cron.js +179 -77
  33. package/cjs/koni/background/handlers/Extension.js +231 -195
  34. package/cjs/koni/background/handlers/State.js +147 -111
  35. package/cjs/koni/background/handlers/Tabs.js +48 -39
  36. package/cjs/koni/background/subscription.js +64 -56
  37. package/cjs/packageInfo.js +1 -1
  38. package/cjs/page/index.js +5 -0
  39. package/cjs/services/chain-service/constants.js +1 -11
  40. package/cjs/services/chain-service/handler/SubstrateChainHandler.js +13 -8
  41. package/cjs/services/chain-service/handler/light-client/index.js +2 -0
  42. package/cjs/services/chain-service/index.js +39 -6
  43. package/cjs/services/chain-service/utils.js +4 -0
  44. package/cjs/services/event-service/index.js +75 -0
  45. package/cjs/services/event-service/types.js +11 -0
  46. package/cjs/services/history-service/index.js +46 -21
  47. package/cjs/services/history-service/subsquid-multi-chain-history.js +19 -11
  48. package/cjs/services/keyring-service/index.js +101 -0
  49. package/cjs/services/migration-service/index.js +13 -10
  50. package/cjs/{koni/migration/scripts/RemoveWrongCrowdloan.js → services/migration-service/scripts/AutoEnableChainsTokens.js} +11 -6
  51. package/cjs/services/migration-service/scripts/MigrateNetworkSettings.js +39 -19
  52. package/cjs/services/migration-service/scripts/MigrateSettings.js +31 -0
  53. package/cjs/services/migration-service/scripts/MigrateTransactionHistory.js +72 -0
  54. package/cjs/services/migration-service/scripts/index.js +13 -4
  55. package/cjs/services/price-service/coingecko.js +15 -3
  56. package/cjs/services/price-service/index.js +15 -18
  57. package/cjs/services/request-service/constants.js +8 -2
  58. package/cjs/services/request-service/handler/AuthRequestHandler.js +43 -12
  59. package/cjs/services/request-service/index.js +3 -0
  60. package/cjs/services/storage-service/DatabaseService.js +53 -34
  61. package/cjs/services/storage-service/databases/index.js +1 -1
  62. package/cjs/services/storage-service/db-stores/BaseStoreWithAddressAndChain.js +2 -8
  63. package/cjs/services/storage-service/db-stores/Nft.js +8 -18
  64. package/cjs/services/storage-service/db-stores/Transaction.js +10 -7
  65. package/cjs/services/subscan-service/index.js +107 -0
  66. package/cjs/services/subscan-service/subscan-chain-map.js +74 -0
  67. package/cjs/services/subscan-service/types.js +1 -0
  68. package/cjs/services/transaction-service/event-parser/index.js +20 -48
  69. package/cjs/services/transaction-service/index.js +66 -22
  70. package/cjs/services/transaction-service/utils.js +4 -6
  71. package/cjs/utils/address.js +9 -1
  72. package/cjs/utils/index.js +30 -26
  73. package/constants/index.d.ts +8 -13
  74. package/constants/index.js +8 -13
  75. package/koni/api/dotsama/balance.d.ts +0 -1
  76. package/koni/api/dotsama/balance.js +33 -197
  77. package/koni/api/dotsama/transfer.js +5 -4
  78. package/koni/api/nft/acala_nft/index.js +3 -1
  79. package/koni/api/nft/bit.country/index.js +3 -1
  80. package/koni/api/nft/evm_nft/index.js +6 -3
  81. package/koni/api/nft/index.d.ts +1 -2
  82. package/koni/api/nft/index.js +3 -6
  83. package/koni/api/nft/karura_nft/index.js +3 -1
  84. package/koni/api/nft/nft.d.ts +1 -0
  85. package/koni/api/nft/rmrk_nft/index.js +8 -1
  86. package/koni/api/nft/statemine_nft/index.js +3 -1
  87. package/koni/api/nft/unique_nft/index.js +5 -1
  88. package/koni/api/nft/wasm_nft/index.d.ts +0 -2
  89. package/koni/api/nft/wasm_nft/index.js +167 -109
  90. package/koni/api/nft/wasm_nft/utils.d.ts +7 -5
  91. package/koni/api/nft/wasm_nft/utils.js +7 -5
  92. package/koni/api/staking/bonding/amplitude.d.ts +1 -2
  93. package/koni/api/staking/bonding/amplitude.js +22 -13
  94. package/koni/api/staking/bonding/astar.d.ts +2 -11
  95. package/koni/api/staking/bonding/astar.js +39 -231
  96. package/koni/api/staking/bonding/index.d.ts +5 -2
  97. package/koni/api/staking/bonding/index.js +27 -17
  98. package/koni/api/staking/bonding/paraChain.d.ts +4 -1
  99. package/koni/api/staking/bonding/paraChain.js +101 -14
  100. package/koni/api/staking/bonding/relayChain.d.ts +6 -2
  101. package/koni/api/staking/bonding/relayChain.js +172 -17
  102. package/koni/api/staking/bonding/utils.d.ts +15 -2
  103. package/koni/api/staking/bonding/utils.js +53 -11
  104. package/koni/api/tokens/wasm/index.js +5 -4
  105. package/koni/api/tokens/wasm/utils.d.ts +6 -0
  106. package/koni/api/tokens/wasm/utils.js +54 -0
  107. package/koni/api/xcm/index.js +1 -112
  108. package/koni/api/xcm/polkadotXcm.js +3 -3
  109. package/koni/api/xcm/utils.d.ts +5 -6
  110. package/koni/api/xcm/utils.js +15 -10
  111. package/koni/api/xcm/xTokens.js +5 -7
  112. package/koni/api/xcm/xcmPallet.js +10 -9
  113. package/koni/background/cron.d.ts +6 -1
  114. package/koni/background/cron.js +179 -76
  115. package/koni/background/handlers/Extension.d.ts +2 -2
  116. package/koni/background/handlers/Extension.js +161 -125
  117. package/koni/background/handlers/State.d.ts +21 -20
  118. package/koni/background/handlers/State.js +144 -111
  119. package/koni/background/handlers/Tabs.js +30 -20
  120. package/koni/background/subscription.d.ts +1 -1
  121. package/koni/background/subscription.js +64 -58
  122. package/package.json +63 -18
  123. package/packageInfo.js +1 -1
  124. package/page/index.d.ts +2 -0
  125. package/page/index.js +4 -0
  126. package/services/chain-service/constants.d.ts +0 -1
  127. package/services/chain-service/constants.js +0 -9
  128. package/services/chain-service/handler/SubstrateChainHandler.js +14 -9
  129. package/services/chain-service/handler/light-client/index.d.ts +17 -1
  130. package/services/chain-service/handler/light-client/index.js +1 -1
  131. package/services/chain-service/helper/psp22_abi.json +1041 -881
  132. package/services/chain-service/helper/psp34_abi.json +2963 -1807
  133. package/services/chain-service/index.d.ts +6 -4
  134. package/services/chain-service/index.js +40 -9
  135. package/services/chain-service/utils.d.ts +1 -0
  136. package/services/chain-service/utils.js +3 -0
  137. package/services/event-service/index.d.ts +22 -0
  138. package/services/event-service/index.js +63 -0
  139. package/services/event-service/types.d.ts +28 -0
  140. package/services/event-service/types.js +4 -0
  141. package/services/history-service/index.d.ts +5 -3
  142. package/services/history-service/index.js +46 -21
  143. package/services/history-service/subsquid-multi-chain-history.js +22 -13
  144. package/services/keyring-service/index.d.ts +19 -0
  145. package/services/keyring-service/index.js +93 -0
  146. package/services/migration-service/index.js +11 -9
  147. package/services/migration-service/scripts/AutoEnableChainsTokens.d.ts +4 -0
  148. package/services/migration-service/scripts/AutoEnableChainsTokens.js +13 -0
  149. package/services/migration-service/scripts/MigrateNetworkSettings.js +37 -18
  150. package/services/migration-service/scripts/MigrateSettings.d.ts +4 -0
  151. package/services/migration-service/scripts/MigrateSettings.js +23 -0
  152. package/services/migration-service/scripts/MigrateTransactionHistory.d.ts +4 -0
  153. package/services/migration-service/scripts/MigrateTransactionHistory.js +64 -0
  154. package/services/migration-service/scripts/index.d.ts +1 -0
  155. package/services/migration-service/scripts/index.js +10 -3
  156. package/services/price-service/coingecko.js +15 -3
  157. package/services/price-service/index.d.ts +5 -3
  158. package/services/price-service/index.js +15 -18
  159. package/services/request-service/constants.d.ts +1 -0
  160. package/services/request-service/constants.js +6 -1
  161. package/services/request-service/handler/AuthRequestHandler.d.ts +8 -1
  162. package/services/request-service/handler/AuthRequestHandler.js +44 -13
  163. package/services/request-service/index.d.ts +7 -1
  164. package/services/request-service/index.js +3 -0
  165. package/services/storage-service/DatabaseService.d.ts +1 -0
  166. package/services/storage-service/DatabaseService.js +53 -34
  167. package/services/storage-service/databases/index.js +1 -1
  168. package/services/storage-service/db-stores/BaseStoreWithAddressAndChain.d.ts +1 -1
  169. package/services/storage-service/db-stores/BaseStoreWithAddressAndChain.js +2 -8
  170. package/services/storage-service/db-stores/Nft.d.ts +3 -3
  171. package/services/storage-service/db-stores/Nft.js +8 -17
  172. package/services/storage-service/db-stores/Transaction.js +10 -7
  173. package/services/subscan-service/index.d.ts +20 -0
  174. package/services/subscan-service/index.js +99 -0
  175. package/services/subscan-service/subscan-chain-map.d.ts +5 -0
  176. package/services/subscan-service/subscan-chain-map.js +63 -0
  177. package/services/subscan-service/types.d.ts +27 -0
  178. package/services/subscan-service/types.js +1 -0
  179. package/services/transaction-service/event-parser/index.d.ts +2 -2
  180. package/services/transaction-service/event-parser/index.js +21 -49
  181. package/services/transaction-service/index.d.ts +4 -2
  182. package/services/transaction-service/index.js +66 -22
  183. package/services/transaction-service/utils.js +5 -6
  184. package/utils/address.d.ts +1 -0
  185. package/utils/address.js +9 -2
  186. package/utils/index.d.ts +2 -1
  187. package/utils/index.js +26 -23
  188. package/cjs/background/errors/EvmRpcError.js +0 -21
  189. package/cjs/background/errors/SubWalletProviderError.js +0 -17
  190. package/cjs/constants/ethereum.js +0 -19
  191. package/cjs/errors/SubWalletProviderError.js +0 -17
  192. package/cjs/koni/api/xcm/astar.js +0 -160
  193. package/cjs/koni/api/xcm/moonbeamXcm.js +0 -80
  194. package/cjs/koni/api/xcm/statemintXcm.js +0 -197
  195. package/cjs/koni/api/xcm/substrateXcm.js +0 -213
  196. package/cjs/koni/migration/Base.js +0 -20
  197. package/cjs/koni/migration/index.js +0 -45
  198. package/cjs/koni/migration/scripts/ChangeRouteToHome.js +0 -22
  199. package/cjs/koni/migration/scripts/ClearOldStorage.js +0 -24
  200. package/cjs/koni/migration/scripts/RemoveWrongTransactionHistoriesFromStore.js +0 -36
  201. package/cjs/koni/migration/scripts/ResetTransactionHistoryEventIdx.js +0 -21
  202. package/cjs/koni/migration/scripts/index.js +0 -22
  203. package/cjs/koni/page/index.js +0 -16
  204. package/cjs/services/asset-service/index.js +0 -91
  205. package/cjs/services/storage-service/db-stores/ExtraDelegationInfo.js +0 -17
  206. package/cjs/stores/Balance.js +0 -18
  207. package/cjs/stores/Crowdloan.js +0 -18
  208. package/cjs/stores/CustomEvmToken.js +0 -18
  209. package/cjs/stores/NetworkMap.js +0 -18
  210. package/cjs/stores/Nft.js +0 -18
  211. package/cjs/stores/NftCollection.js +0 -18
  212. package/cjs/stores/Price.js +0 -18
  213. package/cjs/stores/Staking.js +0 -18
  214. package/cjs/stores/StakingReward.js +0 -18
  215. package/cjs/utils/eth/parseTransactionData.js +0 -284
  216. package/koni/page/index.d.ts +0 -2
  217. package/koni/page/index.js +0 -9
  218. /package/cjs/{koni/page → page}/SubWalleEvmProvider.js +0 -0
  219. /package/{koni/page → page}/SubWalleEvmProvider.d.ts +0 -0
  220. /package/{koni/page → page}/SubWalleEvmProvider.js +0 -0
@@ -3,8 +3,9 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.POLKADOT_UNLIMITED_WEIGHT = exports.POLKADOT_LIMITED_WEIGHT = exports.NETWORK_USE_UNLIMITED_WEIGHT = exports.FOUR_INSTRUCTIONS_WEIGHT = void 0;
6
+ exports.FOUR_INSTRUCTIONS_WEIGHT = exports.FOUR_INSTRUCTIONS_LIMITED_WEIGHT = void 0;
7
7
  exports.getBeneficiary = getBeneficiary;
8
+ exports.getDestWeight = getDestWeight;
8
9
  exports.getReceiverLocation = getReceiverLocation;
9
10
  var _chainList = require("@subwallet/chain-list");
10
11
  var _utils = require("@subwallet/extension-base/services/chain-service/utils");
@@ -12,20 +13,18 @@ var _utilCrypto = require("@polkadot/util-crypto");
12
13
  // Copyright 2019-2022 @subwallet/extension-base
13
14
  // SPDX-License-Identifier: Apache-2.0
14
15
 
15
- const FOUR_INSTRUCTIONS_WEIGHT = {
16
+ const FOUR_INSTRUCTIONS_WEIGHT = 5000000000;
17
+ exports.FOUR_INSTRUCTIONS_WEIGHT = FOUR_INSTRUCTIONS_WEIGHT;
18
+ const FOUR_INSTRUCTIONS_LIMITED_WEIGHT = {
16
19
  Limited: 5000000000
17
20
  };
18
- exports.FOUR_INSTRUCTIONS_WEIGHT = FOUR_INSTRUCTIONS_WEIGHT;
19
- const POLKADOT_LIMITED_WEIGHT = 1000000000;
20
- exports.POLKADOT_LIMITED_WEIGHT = POLKADOT_LIMITED_WEIGHT;
21
- const POLKADOT_UNLIMITED_WEIGHT = 'Unlimited';
22
21
 
23
22
  // get multilocation for destination chain from a parachain
24
- exports.POLKADOT_UNLIMITED_WEIGHT = POLKADOT_UNLIMITED_WEIGHT;
23
+ exports.FOUR_INSTRUCTIONS_LIMITED_WEIGHT = FOUR_INSTRUCTIONS_LIMITED_WEIGHT;
25
24
  function getReceiverLocation(originChainInfo, destinationChainInfo, toAddress) {
26
- // @ts-ignore
27
- if (destinationChainInfo.slug === _chainList.COMMON_CHAIN_SLUGS.ASTAR_EVM || destinationChainInfo.slug === 'astarEvm') {
28
- const ss58Address = (0, _utilCrypto.evmToAddress)(toAddress, (0, _utils._getChainSubstrateAddressPrefix)(destinationChainInfo));
25
+ if (destinationChainInfo.slug === _chainList.COMMON_CHAIN_SLUGS.ASTAR_EVM) {
26
+ const ss58Address = (0, _utilCrypto.evmToAddress)(toAddress, 2006); // TODO: shouldn't pass addressPrefix directly
27
+
29
28
  return {
30
29
  AccountId32: {
31
30
  network: 'Any',
@@ -49,9 +48,10 @@ function getReceiverLocation(originChainInfo, destinationChainInfo, toAddress) {
49
48
  };
50
49
  }
51
50
  function getBeneficiary(originChainInfo, destinationChainInfo, recipientAddress) {
51
+ let version = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'V1';
52
52
  const receiverLocation = getReceiverLocation(originChainInfo, destinationChainInfo, recipientAddress);
53
53
  return {
54
- V1: {
54
+ [version]: {
55
55
  parents: 0,
56
56
  interior: {
57
57
  X1: receiverLocation
@@ -59,5 +59,10 @@ function getBeneficiary(originChainInfo, destinationChainInfo, recipientAddress)
59
59
  }
60
60
  };
61
61
  }
62
- const NETWORK_USE_UNLIMITED_WEIGHT = ['acala', 'karura', 'statemint', 'moonriver'];
63
- exports.NETWORK_USE_UNLIMITED_WEIGHT = NETWORK_USE_UNLIMITED_WEIGHT;
62
+ function getDestWeight() {
63
+ return 'Unlimited';
64
+ // return api.tx.xTokens.transfer.meta.args[3].type.toString() ===
65
+ // 'XcmV2WeightLimit'
66
+ // ? 'Unlimited'
67
+ // : FOUR_INSTRUCTIONS_WEIGHT;
68
+ }
@@ -11,7 +11,6 @@ var _utils2 = require("@subwallet/extension-base/services/chain-service/utils");
11
11
  // SPDX-License-Identifier: Apache-2.0
12
12
 
13
13
  function getCurrencyId(tokenInfo) {
14
- var _tokenInfo$metadata2;
15
14
  if (['acala', 'karura'].includes(tokenInfo.originChain) && (0, _utils2._isNativeToken)(tokenInfo)) {
16
15
  return (0, _utils2._getXcmAssetMultilocation)(tokenInfo);
17
16
  } else if (['moonbeam', 'moonbase', 'moonriver'].includes(tokenInfo.originChain)) {
@@ -21,10 +20,9 @@ function getCurrencyId(tokenInfo) {
21
20
  [tokenType]: assetId
22
21
  };
23
22
  } else if (['pioneer'].includes(tokenInfo.originChain)) {
24
- var _tokenInfo$metadata;
25
- return (_tokenInfo$metadata = tokenInfo.metadata) === null || _tokenInfo$metadata === void 0 ? void 0 : _tokenInfo$metadata.multilocation;
23
+ return (0, _utils2._getXcmAssetMultilocation)(tokenInfo);
26
24
  }
27
- return (_tokenInfo$metadata2 = tokenInfo.metadata) === null || _tokenInfo$metadata2 === void 0 ? void 0 : _tokenInfo$metadata2.onChainInfo;
25
+ return (0, _utils2._getTokenOnChainInfo)(tokenInfo);
28
26
  }
29
27
  function getMultiLocationForXtokensPallet(originChainInfo, destinationChainInfo, toAddress) {
30
28
  const xcmType = (0, _utils2._getXcmTransferType)(originChainInfo, destinationChainInfo);
@@ -56,6 +54,6 @@ function getMultiLocationForXtokensPallet(originChainInfo, destinationChainInfo,
56
54
  };
57
55
  }
58
56
  function getExtrinsicByXtokensPallet(tokenInfo, originChainInfo, destinationChainInfo, recipientAddress, value, api) {
59
- const weightParam = _utils.NETWORK_USE_UNLIMITED_WEIGHT.includes(originChainInfo.slug) ? _utils.POLKADOT_UNLIMITED_WEIGHT : _utils.FOUR_INSTRUCTIONS_WEIGHT;
57
+ const weightParam = ['pioneer'].includes(originChainInfo.slug) ? _utils.FOUR_INSTRUCTIONS_WEIGHT : (0, _utils.getDestWeight)();
60
58
  return api.tx.xTokens.transfer(getCurrencyId(tokenInfo), value, getMultiLocationForXtokensPallet(originChainInfo, destinationChainInfo, recipientAddress), weightParam);
61
59
  }
@@ -10,8 +10,9 @@ var _utils2 = require("@subwallet/extension-base/services/chain-service/utils");
10
10
  // SPDX-License-Identifier: Apache-2.0
11
11
 
12
12
  function getDestinationChainLocation(destinationChainInfo) {
13
+ let version = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'V1';
13
14
  return {
14
- V1: {
15
+ [version]: {
15
16
  parents: 0,
16
17
  interior: {
17
18
  X1: {
@@ -22,9 +23,10 @@ function getDestinationChainLocation(destinationChainInfo) {
22
23
  };
23
24
  }
24
25
  function getTokenLocation(sendingValue) {
26
+ let version = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'V2';
25
27
  return {
26
28
  // always native token of relaychain
27
- V1: [{
29
+ [version]: [{
28
30
  id: {
29
31
  Concrete: {
30
32
  parents: 0,
@@ -40,10 +42,11 @@ function getTokenLocation(sendingValue) {
40
42
 
41
43
  // this pallet is only used by Relaychains
42
44
  function getExtrinsicByXcmPalletPallet(tokenInfo, originChainInfo, destinationChainInfo, recipientAddress, value, api) {
43
- const weightParam = _utils.NETWORK_USE_UNLIMITED_WEIGHT.includes(originChainInfo.slug) ? _utils.POLKADOT_UNLIMITED_WEIGHT : _utils.FOUR_INSTRUCTIONS_WEIGHT;
44
- const destination = getDestinationChainLocation(destinationChainInfo);
45
- const beneficiary = (0, _utils.getBeneficiary)(originChainInfo, destinationChainInfo, recipientAddress);
46
- const tokenLocation = getTokenLocation(value);
45
+ const weightParam = (0, _utils.getDestWeight)();
46
+ const xcmVer = ['kusama'].includes(originChainInfo.slug) ? 'V2' : 'V1';
47
+ const destination = getDestinationChainLocation(destinationChainInfo, xcmVer);
48
+ const beneficiary = (0, _utils.getBeneficiary)(originChainInfo, destinationChainInfo, recipientAddress, xcmVer);
49
+ const tokenLocation = getTokenLocation(value, xcmVer);
47
50
  let method = 'limitedReserveTransferAssets';
48
51
  if (['statemint', 'statemine'].includes(destinationChainInfo.slug)) {
49
52
  method = 'limitedTeleportAssets';
@@ -5,6 +5,9 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.KoniCron = void 0;
7
7
  var _constants = require("@subwallet/extension-base/constants");
8
+ var _types = require("@subwallet/extension-base/services/chain-service/types");
9
+ var _utils = require("@subwallet/extension-base/services/chain-service/utils");
10
+ var _utils2 = require("@subwallet/extension-base/utils");
8
11
  var _rxjs = require("rxjs");
9
12
  var _util = require("@polkadot/util");
10
13
  // Copyright 2019-2022 @subwallet/extension-koni authors & contributors
@@ -58,80 +61,115 @@ class KoniCron {
58
61
  delete this.cronMap[key];
59
62
  });
60
63
  };
61
- init = () => {
62
- this.state.getCurrentAccount(currentAccountInfo => {
63
- if (!(currentAccountInfo !== null && currentAccountInfo !== void 0 && currentAccountInfo.address)) {
64
- return;
65
- }
66
- if (Object.keys(this.state.getSubstrateApiMap()).length !== 0 || Object.keys(this.state.getEvmApiMap()).length !== 0) {
67
- this.refreshNft(currentAccountInfo.address, this.state.getApiMap(), this.state.getSmartContractNfts(), this.state.getActiveChainInfoMap());
68
- this.refreshStakingReward(currentAccountInfo.address);
69
- this.refreshStakingRewardFastInterval(currentAccountInfo.address);
70
- this.updateChainStakingMetadata(this.state.getChainInfoMap(), this.state.getChainStateMap(), this.state.getSubstrateApiMap());
71
- this.updateNominatorMetadata(currentAccountInfo.address, this.state.getChainInfoMap(), this.state.getChainStateMap(), this.state.getSubstrateApiMap());
72
- } else {
73
- this.setStakingRewardReady();
74
- }
75
- });
76
- };
64
+
65
+ // init = () => {
66
+ // const currentAccountInfo = this.state.keyringService.currentAccount;
67
+ //
68
+ // if (!currentAccountInfo?.address) {
69
+ // return;
70
+ // }
71
+ //
72
+ // if (Object.keys(this.state.getSubstrateApiMap()).length !== 0 || Object.keys(this.state.getEvmApiMap()).length !== 0) {
73
+ // this.refreshNft(currentAccountInfo.address, this.state.getApiMap(), this.state.getSmartContractNfts(), this.state.getActiveChainInfoMap());
74
+ // this.updateApiMapStatus();
75
+ // this.refreshStakingReward(currentAccountInfo.address);
76
+ // this.refreshStakingRewardFastInterval(currentAccountInfo.address);
77
+ // // this.updateChainStakingMetadata(this.state.getChainInfoMap(), this.state.getChainStateMap(), this.state.getSubstrateApiMap());
78
+ // this.updateNominatorMetadata(currentAccountInfo.address, this.state.getChainInfoMap(), this.state.getChainStateMap(), this.state.getSubstrateApiMap());
79
+ // } else {
80
+ // this.setStakingRewardReady();
81
+ // }
82
+ // };
83
+
77
84
  start = () => {
78
85
  if (this.status === 'running') {
79
86
  return;
80
87
  }
81
88
  this.logger.log('Starting cron jobs');
82
- this.state.getCurrentAccount(currentAccountInfo => {
83
- if (!(currentAccountInfo !== null && currentAccountInfo !== void 0 && currentAccountInfo.address)) {
89
+ const currentAccountInfo = this.state.keyringService.currentAccount;
90
+ if (!(currentAccountInfo !== null && currentAccountInfo !== void 0 && currentAccountInfo.address)) {
91
+ return;
92
+ }
93
+ if (Object.keys(this.state.getSubstrateApiMap()).length !== 0 || Object.keys(this.state.getEvmApiMap()).length !== 0) {
94
+ this.resetNft(currentAccountInfo.address);
95
+ this.addCron('refreshNft', this.refreshNft(currentAccountInfo.address, this.state.getApiMap(), this.state.getSmartContractNfts(), this.state.getActiveChainInfoMap()), _constants.CRON_REFRESH_NFT_INTERVAL);
96
+ this.addCron('checkStatusApiMap', this.updateApiMapStatus, _constants.CRON_GET_API_MAP_STATUS);
97
+ this.addCron('recoverApiMap', this.recoverApiMap, _constants.CRON_AUTO_RECOVER_DOTSAMA_INTERVAL, false);
98
+ this.addCron('refreshStakingReward', this.refreshStakingReward(currentAccountInfo.address), _constants.CRON_REFRESH_STAKING_REWARD_INTERVAL);
99
+ this.addCron('refreshPoolingStakingReward', this.refreshStakingRewardFastInterval(currentAccountInfo.address), _constants.CRON_REFRESH_STAKING_REWARD_FAST_INTERVAL);
100
+ this.addCron('updateChainStakingMetadata', this.updateChainStakingMetadata(this.state.getChainInfoMap(), this.state.getChainStateMap(), this.state.getSubstrateApiMap()), _constants.CRON_REFRESH_CHAIN_STAKING_METADATA);
101
+ this.addCron('updateNominatorMetadata', this.updateNominatorMetadata(currentAccountInfo.address, this.state.getChainInfoMap(), this.state.getChainStateMap(), this.state.getSubstrateApiMap()), _constants.CRON_REFRESH_CHAIN_NOMINATOR_METADATA);
102
+ } else {
103
+ this.setStakingRewardReady();
104
+ }
105
+ const commonReloadEvents = ['account.add', 'account.remove', 'account.updateCurrent', 'chain.add', 'asset.updateState'];
106
+ this.eventHandler = (events, eventTypes) => {
107
+ var _serviceInfo$currentA;
108
+ const serviceInfo = this.state.getServiceInfo();
109
+ const commonReload = eventTypes.some(eventType => commonReloadEvents.includes(eventType));
110
+ const chainUpdated = eventTypes.includes('chain.updateState');
111
+ const stakingSubmitted = eventTypes.includes('transaction.submitStaking');
112
+ const updatedChains = [];
113
+ if (chainUpdated) {
114
+ events.forEach(event => {
115
+ if (event.type === 'chain.updateState') {
116
+ const updatedData = event.data;
117
+ updatedChains.push(updatedData[0]);
118
+ }
119
+ });
120
+ }
121
+ if (!commonReload && !chainUpdated && !stakingSubmitted) {
122
+ return;
123
+ }
124
+ this.logger.log('ServiceInfo updated, Cron restarting...', eventTypes);
125
+ const address = (_serviceInfo$currentA = serviceInfo.currentAccountInfo) === null || _serviceInfo$currentA === void 0 ? void 0 : _serviceInfo$currentA.address;
126
+ if (!address) {
84
127
  return;
85
128
  }
86
- if (Object.keys(this.state.getSubstrateApiMap()).length !== 0 || Object.keys(this.state.getEvmApiMap()).length !== 0) {
87
- this.resetNft(currentAccountInfo.address);
88
- this.addCron('refreshNft', this.refreshNft(currentAccountInfo.address, this.state.getApiMap(), this.state.getSmartContractNfts(), this.state.getActiveChainInfoMap()), _constants.CRON_REFRESH_NFT_INTERVAL);
89
- this.addCron('recoverApiMap', this.recoverApiMap, _constants.CRON_AUTO_RECOVER_DOTSAMA_INTERVAL, false);
90
- this.addCron('refreshStakingReward', this.refreshStakingReward(currentAccountInfo.address), _constants.CRON_REFRESH_STAKING_REWARD_INTERVAL);
91
- this.addCron('refreshPoolingStakingReward', this.refreshStakingRewardFastInterval(currentAccountInfo.address), _constants.CRON_REFRESH_STAKING_REWARD_FAST_INTERVAL);
92
- this.addCron('updateChainStakingMetadata', this.updateChainStakingMetadata(this.state.getChainInfoMap(), this.state.getChainStateMap(), this.state.getSubstrateApiMap()), _constants.CRON_REFRESH_CHAIN_STAKING_METADATA);
93
- this.addCron('updateNominatorMetadata', this.updateNominatorMetadata(currentAccountInfo.address, this.state.getChainInfoMap(), this.state.getChainStateMap(), this.state.getSubstrateApiMap()), _constants.CRON_REFRESH_CHAIN_NOMINATOR_METADATA);
129
+ const chainInfoMap = serviceInfo.chainInfoMap;
130
+ const needUpdateNft = this.needUpdateNft(chainInfoMap, updatedChains);
131
+ const needUpdateStaking = this.needUpdateStaking(chainInfoMap, updatedChains);
132
+
133
+ // NFT
134
+ (commonReload || needUpdateNft) && this.resetNft(address);
135
+ (commonReload || needUpdateNft) && this.removeCron('refreshNft');
136
+
137
+ // Staking
138
+ (commonReload || needUpdateStaking || stakingSubmitted) && this.resetStakingReward();
139
+ (commonReload || needUpdateStaking || stakingSubmitted) && this.removeCron('refreshStakingReward');
140
+ (commonReload || needUpdateStaking || stakingSubmitted) && this.removeCron('refreshPoolingStakingReward');
141
+ (commonReload || needUpdateStaking || stakingSubmitted) && this.removeCron('updateNominatorMetadata');
142
+ needUpdateStaking && this.removeCron('updateChainStakingMetadata');
143
+
144
+ // Chains
145
+ chainUpdated && this.removeCron('checkStatusApiMap');
146
+ chainUpdated && this.removeCron('recoverApiMap');
147
+ if (this.checkNetworkAvailable(serviceInfo)) {
148
+ // only add cron job if there's at least 1 active network
149
+ (commonReload || needUpdateNft) && this.addCron('refreshNft', this.refreshNft(address, serviceInfo.chainApiMap, this.state.getSmartContractNfts(), this.state.getActiveChainInfoMap()), _constants.CRON_REFRESH_NFT_INTERVAL);
150
+ chainUpdated && this.addCron('checkStatusApiMap', this.updateApiMapStatus, _constants.CRON_GET_API_MAP_STATUS);
151
+ chainUpdated && this.addCron('recoverApiMap', this.recoverApiMap, _constants.CRON_AUTO_RECOVER_DOTSAMA_INTERVAL, false);
152
+ (commonReload || needUpdateStaking || stakingSubmitted) && this.addCron('refreshStakingReward', this.refreshStakingReward(address), _constants.CRON_REFRESH_STAKING_REWARD_INTERVAL);
153
+ (commonReload || needUpdateStaking || stakingSubmitted) && this.addCron('refreshPoolingStakingReward', this.refreshStakingRewardFastInterval(address), _constants.CRON_REFRESH_STAKING_REWARD_FAST_INTERVAL);
154
+ (commonReload || needUpdateStaking || stakingSubmitted) && this.addCron('updateNominatorMetadata', this.updateNominatorMetadata(address, serviceInfo.chainInfoMap, serviceInfo.chainStateMap, serviceInfo.chainApiMap.substrate), _constants.CRON_REFRESH_CHAIN_NOMINATOR_METADATA);
155
+ needUpdateStaking && this.addCron('updateChainStakingMetadata', this.updateChainStakingMetadata(serviceInfo.chainInfoMap, serviceInfo.chainStateMap, serviceInfo.chainApiMap.substrate), _constants.CRON_REFRESH_CHAIN_STAKING_METADATA);
94
156
  } else {
95
157
  this.setStakingRewardReady();
96
158
  }
97
- });
98
- this.serviceSubscription = this.state.subscribeServiceInfo().subscribe({
99
- next: serviceInfo => {
100
- var _serviceInfo$currentA;
101
- this.logger.log('ServiceInfo updated, restarting...');
102
- const address = (_serviceInfo$currentA = serviceInfo.currentAccountInfo) === null || _serviceInfo$currentA === void 0 ? void 0 : _serviceInfo$currentA.address;
103
- if (!address) {
104
- return;
105
- }
106
- this.resetStakingReward();
107
- this.resetNft(address);
108
- this.removeCron('refreshNft');
109
- this.removeCron('refreshStakingReward');
110
- this.removeCron('refreshPoolingStakingReward');
111
- this.removeCron('refreshPrice');
112
- this.removeCron('checkStatusApiMap');
113
- this.removeCron('recoverApiMap');
114
- this.removeCron('updateChainStakingMetadata');
115
- this.removeCron('updateNominatorMetadata');
116
- if (this.checkNetworkAvailable(serviceInfo)) {
117
- // only add cron job if there's at least 1 active network
118
- this.addCron('refreshNft', this.refreshNft(address, serviceInfo.chainApiMap, this.state.getSmartContractNfts(), this.state.getActiveChainInfoMap()), _constants.CRON_REFRESH_NFT_INTERVAL);
119
- this.addCron('recoverApiMap', this.recoverApiMap, _constants.CRON_AUTO_RECOVER_DOTSAMA_INTERVAL, false);
120
- this.addCron('refreshStakingReward', this.refreshStakingReward(address), _constants.CRON_REFRESH_STAKING_REWARD_INTERVAL);
121
- this.addCron('refreshPoolingStakingReward', this.refreshStakingRewardFastInterval(address), _constants.CRON_REFRESH_STAKING_REWARD_FAST_INTERVAL);
122
- this.addCron('updateChainStakingMetadata', this.updateChainStakingMetadata(serviceInfo.chainInfoMap, serviceInfo.chainStateMap, serviceInfo.chainApiMap.substrate), _constants.CRON_REFRESH_CHAIN_STAKING_METADATA);
123
- this.addCron('updateNominatorMetadata', this.updateNominatorMetadata(address, serviceInfo.chainInfoMap, serviceInfo.chainStateMap, serviceInfo.chainApiMap.substrate), _constants.CRON_REFRESH_CHAIN_NOMINATOR_METADATA);
124
- } else {
125
- this.setStakingRewardReady();
126
- }
127
- }
128
- });
159
+ };
160
+ this.state.eventService.onLazy(this.eventHandler);
129
161
  this.status = 'running';
130
162
  };
131
163
  stop = () => {
132
164
  if (this.status === 'stopped') {
133
165
  return;
134
166
  }
167
+
168
+ // Unsubscribe events
169
+ if (this.eventHandler) {
170
+ this.state.eventService.offLazy(this.eventHandler);
171
+ this.eventHandler = undefined;
172
+ }
135
173
  if (this.serviceSubscription) {
136
174
  this.serviceSubscription.unsubscribe();
137
175
  this.serviceSubscription = undefined;
@@ -140,7 +178,38 @@ class KoniCron {
140
178
  this.removeAllCrons();
141
179
  this.status = 'stopped';
142
180
  };
181
+ updateApiMapStatus = () => {
182
+ const apiMap = this.state.getApiMap();
183
+ const networkMap = this.state.getChainStateMap();
184
+ for (const [key, substrateApi] of Object.entries(apiMap.substrate)) {
185
+ let status = _types._ChainConnectionStatus.CONNECTING;
186
+ if (substrateApi.isApiConnected) {
187
+ status = _types._ChainConnectionStatus.CONNECTED;
188
+ }
189
+ if (!networkMap[key].connectionStatus) {
190
+ this.state.updateChainConnectionStatus(key, status);
191
+ } else if (networkMap[key].connectionStatus && networkMap[key].connectionStatus !== status) {
192
+ this.state.updateChainConnectionStatus(key, status);
193
+ }
194
+ }
195
+ for (const [key, evmApi] of Object.entries(apiMap.evm)) {
196
+ evmApi.api.eth.net.isListening().then(() => {
197
+ if (!networkMap[key].connectionStatus) {
198
+ this.state.updateChainConnectionStatus(key, _types._ChainConnectionStatus.CONNECTED);
199
+ } else if (networkMap[key].connectionStatus && networkMap[key].connectionStatus !== _types._ChainConnectionStatus.CONNECTED) {
200
+ this.state.updateChainConnectionStatus(key, _types._ChainConnectionStatus.CONNECTED);
201
+ }
202
+ }).catch(() => {
203
+ if (!networkMap[key].connectionStatus) {
204
+ this.state.updateChainConnectionStatus(key, _types._ChainConnectionStatus.CONNECTING);
205
+ } else if (networkMap[key].connectionStatus && networkMap[key].connectionStatus !== _types._ChainConnectionStatus.CONNECTING) {
206
+ this.state.updateChainConnectionStatus(key, _types._ChainConnectionStatus.CONNECTING);
207
+ }
208
+ });
209
+ }
210
+ };
143
211
  recoverApiMap = () => {
212
+ var _this$subscriptions;
144
213
  const apiMap = this.state.getApiMap();
145
214
  for (const [networkKey, apiProp] of Object.entries(apiMap.substrate)) {
146
215
  if (!apiProp.isApiConnected) {
@@ -152,38 +221,33 @@ class KoniCron {
152
221
  this.state.refreshWeb3Api(key);
153
222
  });
154
223
  }
155
- this.state.getCurrentAccount(_ref2 => {
156
- var _this$subscriptions;
157
- let {
158
- address
159
- } = _ref2;
160
- ((_this$subscriptions = this.subscriptions) === null || _this$subscriptions === void 0 ? void 0 : _this$subscriptions.subscribeBalancesAndCrowdloans) && this.subscriptions.subscribeBalancesAndCrowdloans(address, this.state.getChainInfoMap(), this.state.getChainStateMap(), this.state.getSubstrateApiMap(), this.state.getEvmApiMap());
161
- });
224
+ const {
225
+ address
226
+ } = this.state.keyringService.currentAccount;
227
+ ((_this$subscriptions = this.subscriptions) === null || _this$subscriptions === void 0 ? void 0 : _this$subscriptions.subscribeBalancesAndCrowdloans) && this.subscriptions.subscribeBalancesAndCrowdloans(address, this.state.getChainInfoMap(), this.state.getChainStateMap(), this.state.getSubstrateApiMap(), this.state.getEvmApiMap());
162
228
  };
163
229
  refreshNft = (address, apiMap, smartContractNfts, chainInfoMap) => {
164
230
  return () => {
165
- this.logger.log('Refresh Nft state');
231
+ console.debug('Refresh NFT state');
166
232
  this.subscriptions.subscribeNft(address, apiMap.substrate, apiMap.evm, smartContractNfts, chainInfoMap);
167
233
  };
168
234
  };
169
235
  resetNft = newAddress => {
170
- this.logger.log('Reset Nft state');
171
- this.state.resetNft(newAddress).catch(e => this.logger.warn(e));
236
+ this.state.resetNft(newAddress);
172
237
  };
173
238
  resetStakingReward = () => {
174
- this.logger.log('Reset Staking Reward State');
175
239
  this.state.resetStakingReward();
176
240
  };
177
241
  refreshStakingReward = address => {
178
242
  return () => {
179
- this.logger.log('Fetching staking reward data');
180
- this.subscriptions.subscribeStakingReward(address).then(() => this.logger.log('Refresh staking reward state')).catch(this.logger.error);
243
+ console.debug('Refresh staking reward state');
244
+ this.subscriptions.subscribeStakingReward(address).catch(this.logger.error);
181
245
  };
182
246
  };
183
247
  refreshStakingRewardFastInterval = address => {
184
248
  return () => {
185
- this.logger.log('Fetching staking reward data with fast interval');
186
- this.subscriptions.subscribeStakingRewardFastInterval(address).then(() => this.logger.log('Refresh staking reward state with fast interval')).catch(this.logger.error);
249
+ console.debug('Refresh staking reward data with fast interval');
250
+ this.subscriptions.subscribeStakingRewardFastInterval(address).catch(this.logger.error);
187
251
  };
188
252
  };
189
253
  setStakingRewardReady = () => {
@@ -194,15 +258,53 @@ class KoniCron {
194
258
  };
195
259
  updateChainStakingMetadata = (chainInfoMap, chainStateMap, substrateApiMap) => {
196
260
  return () => {
197
- this.logger.log('Fetching chain staking metadata');
198
- this.subscriptions.fetchChainStakingMetadata(chainInfoMap, chainStateMap, substrateApiMap).then(() => this.logger.log('Updated chain staking metadata')).catch(this.logger.error);
261
+ console.debug('Fetching chain staking metadata');
262
+ this.subscriptions.fetchChainStakingMetadata(chainInfoMap, chainStateMap, substrateApiMap).catch(this.logger.error);
199
263
  };
200
264
  };
201
265
  updateNominatorMetadata = (address, chainInfoMap, chainStateMap, substrateApiMap) => {
202
266
  return () => {
203
- this.logger.log('Fetching nominator data', address);
204
- this.subscriptions.fetchNominatorMetadata(address, chainInfoMap, chainStateMap, substrateApiMap).then(() => this.logger.log('Updated nominator data', address)).catch(this.logger.error);
267
+ console.debug('Fetching nominator data for', address);
268
+ this.subscriptions.fetchNominatorMetadata(address, chainInfoMap, chainStateMap, substrateApiMap).catch(this.logger.error);
205
269
  };
206
270
  };
271
+ async reloadNft() {
272
+ const address = this.state.keyringService.currentAccount.address;
273
+ const serviceInfo = this.state.getServiceInfo();
274
+ console.debug('Hard refresh NFT for', address);
275
+ this.resetNft(address);
276
+ this.removeCron('refreshNft');
277
+ this.addCron('refreshNft', this.refreshNft(address, serviceInfo.chainApiMap, this.state.getSmartContractNfts(), this.state.getActiveChainInfoMap()), _constants.CRON_REFRESH_NFT_INTERVAL);
278
+ await (0, _utils2.waitTimeout)(1800);
279
+ return true;
280
+ }
281
+ async reloadStaking() {
282
+ const address = this.state.keyringService.currentAccount.address;
283
+ console.debug('Hard refresh staking meta for', address);
284
+ this.resetStakingReward();
285
+ this.removeCron('refreshStakingReward');
286
+ this.removeCron('refreshPoolingStakingReward');
287
+ this.removeCron('updateNominatorMetadata');
288
+ this.addCron('refreshStakingReward', this.refreshStakingReward(address), _constants.CRON_REFRESH_STAKING_REWARD_INTERVAL);
289
+ this.addCron('refreshPoolingStakingReward', this.refreshStakingRewardFastInterval(address), _constants.CRON_REFRESH_STAKING_REWARD_FAST_INTERVAL);
290
+ this.addCron('updateNominatorMetadata', this.updateNominatorMetadata(address, this.state.getChainInfoMap(), this.state.getChainStateMap(), this.state.getSubstrateApiMap()), _constants.CRON_REFRESH_CHAIN_NOMINATOR_METADATA);
291
+ await (0, _utils2.waitTimeout)(1800);
292
+ return true;
293
+ }
294
+ needUpdateNft(chainInfoMap, updatedChains) {
295
+ if (updatedChains && updatedChains.length > 0) {
296
+ return updatedChains.some(updatedChain => {
297
+ const chainInfo = chainInfoMap[updatedChain];
298
+ return (0, _utils._isChainSupportNativeNft)(chainInfo) || (0, _utils._isChainSupportEvmNft)(chainInfo) || (0, _utils._isChainSupportWasmNft)(chainInfo);
299
+ });
300
+ }
301
+ return false;
302
+ }
303
+ needUpdateStaking(chainInfoMap, updatedChains) {
304
+ if (updatedChains && updatedChains.length > 0) {
305
+ return updatedChains.some(updatedChain => (0, _utils._isChainSupportSubstrateStaking)(chainInfoMap[updatedChain]));
306
+ }
307
+ return false;
308
+ }
207
309
  }
208
310
  exports.KoniCron = KoniCron;