@subwallet/extension-base 1.0.2-2 → 1.0.3-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 (151) hide show
  1. package/background/KoniTypes.d.ts +56 -40
  2. package/background/KoniTypes.js +11 -9
  3. package/background/errors/TransactionError.js +25 -1
  4. package/background/types.d.ts +10 -5
  5. package/cjs/background/KoniTypes.js +11 -9
  6. package/cjs/background/errors/TransactionError.js +24 -0
  7. package/cjs/constants/index.js +8 -26
  8. package/cjs/koni/api/dotsama/balance.js +49 -224
  9. package/cjs/koni/api/dotsama/transfer.js +34 -39
  10. package/cjs/koni/api/nft/acala_nft/index.js +7 -7
  11. package/cjs/koni/api/nft/bit.country/index.js +7 -6
  12. package/cjs/koni/api/nft/evm_nft/index.js +8 -3
  13. package/cjs/koni/api/nft/index.js +3 -6
  14. package/cjs/koni/api/nft/karura_nft/index.js +7 -6
  15. package/cjs/koni/api/nft/rmrk_nft/index.js +11 -1
  16. package/cjs/koni/api/nft/statemine_nft/index.js +7 -6
  17. package/cjs/koni/api/nft/unique_nft/index.js +5 -1
  18. package/cjs/koni/api/nft/wasm_nft/index.js +170 -111
  19. package/cjs/koni/api/nft/wasm_nft/utils.js +11 -7
  20. package/cjs/koni/api/staking/bonding/amplitude.js +13 -9
  21. package/cjs/koni/api/staking/bonding/astar.js +15 -13
  22. package/cjs/koni/api/staking/bonding/index.js +22 -10
  23. package/cjs/koni/api/staking/bonding/paraChain.js +85 -2
  24. package/cjs/koni/api/staking/bonding/relayChain.js +122 -16
  25. package/cjs/koni/api/staking/bonding/utils.js +27 -8
  26. package/cjs/koni/api/tokens/wasm/index.js +5 -4
  27. package/cjs/koni/api/tokens/wasm/utils.js +63 -0
  28. package/cjs/koni/api/xcm/polkadotXcm.js +1 -1
  29. package/cjs/koni/api/xcm/utils.js +18 -13
  30. package/cjs/koni/api/xcm/xTokens.js +1 -1
  31. package/cjs/koni/api/xcm/xcmPallet.js +9 -6
  32. package/cjs/koni/background/cron.js +171 -61
  33. package/cjs/koni/background/handlers/Extension.js +391 -207
  34. package/cjs/koni/background/handlers/State.js +49 -34
  35. package/cjs/koni/background/handlers/Tabs.js +50 -17
  36. package/cjs/koni/background/subscription.js +53 -28
  37. package/cjs/packageInfo.js +1 -1
  38. package/cjs/services/base/types.js +20 -0
  39. package/cjs/services/chain-service/handler/SubstrateChainHandler.js +13 -8
  40. package/cjs/services/chain-service/index.js +73 -49
  41. package/cjs/services/event-service/index.js +5 -1
  42. package/cjs/services/event-service/types.js +11 -1
  43. package/cjs/services/history-service/index.js +101 -50
  44. package/cjs/services/history-service/subsquid-multi-chain-history.js +13 -10
  45. package/cjs/services/keyring-service/index.js +11 -13
  46. package/cjs/services/migration-service/scripts/MigrateImportedToken.js +2 -1
  47. package/cjs/services/price-service/coingecko.js +0 -1
  48. package/cjs/services/price-service/index.js +71 -24
  49. package/cjs/services/request-service/handler/AuthRequestHandler.js +13 -7
  50. package/cjs/services/request-service/handler/EvmRequestHandler.js +8 -12
  51. package/cjs/services/request-service/index.js +14 -5
  52. package/cjs/services/storage-service/DatabaseService.js +66 -34
  53. package/cjs/services/storage-service/db-stores/Nft.js +7 -15
  54. package/cjs/services/storage-service/db-stores/Transaction.js +6 -10
  55. package/cjs/services/transaction-service/event-parser/index.js +20 -48
  56. package/cjs/services/transaction-service/index.js +104 -48
  57. package/cjs/services/transaction-service/utils.js +10 -8
  58. package/cjs/utils/address.js +10 -1
  59. package/cjs/utils/index.js +9 -15
  60. package/cjs/utils/promise.js +26 -0
  61. package/constants/index.d.ts +7 -13
  62. package/constants/index.js +7 -13
  63. package/koni/api/dotsama/balance.d.ts +0 -1
  64. package/koni/api/dotsama/balance.js +22 -197
  65. package/koni/api/dotsama/transfer.js +11 -16
  66. package/koni/api/nft/acala_nft/index.js +7 -7
  67. package/koni/api/nft/bit.country/index.js +7 -6
  68. package/koni/api/nft/evm_nft/index.js +7 -3
  69. package/koni/api/nft/index.d.ts +1 -2
  70. package/koni/api/nft/index.js +3 -6
  71. package/koni/api/nft/karura_nft/index.js +7 -6
  72. package/koni/api/nft/nft.d.ts +1 -0
  73. package/koni/api/nft/rmrk_nft/index.js +11 -1
  74. package/koni/api/nft/statemine_nft/index.js +7 -6
  75. package/koni/api/nft/unique_nft/index.js +5 -1
  76. package/koni/api/nft/wasm_nft/index.d.ts +0 -2
  77. package/koni/api/nft/wasm_nft/index.js +168 -109
  78. package/koni/api/nft/wasm_nft/utils.d.ts +7 -5
  79. package/koni/api/nft/wasm_nft/utils.js +7 -5
  80. package/koni/api/staking/bonding/amplitude.d.ts +0 -1
  81. package/koni/api/staking/bonding/amplitude.js +15 -10
  82. package/koni/api/staking/bonding/astar.js +8 -6
  83. package/koni/api/staking/bonding/index.d.ts +4 -1
  84. package/koni/api/staking/bonding/index.js +23 -13
  85. package/koni/api/staking/bonding/paraChain.d.ts +3 -0
  86. package/koni/api/staking/bonding/paraChain.js +86 -5
  87. package/koni/api/staking/bonding/relayChain.d.ts +5 -1
  88. package/koni/api/staking/bonding/relayChain.js +121 -18
  89. package/koni/api/staking/bonding/utils.d.ts +3 -2
  90. package/koni/api/staking/bonding/utils.js +27 -9
  91. package/koni/api/tokens/wasm/index.js +5 -4
  92. package/koni/api/tokens/wasm/utils.d.ts +6 -0
  93. package/koni/api/tokens/wasm/utils.js +54 -0
  94. package/koni/api/xcm/polkadotXcm.js +2 -2
  95. package/koni/api/xcm/utils.d.ts +5 -6
  96. package/koni/api/xcm/utils.js +15 -10
  97. package/koni/api/xcm/xTokens.js +2 -2
  98. package/koni/api/xcm/xcmPallet.js +10 -9
  99. package/koni/background/cron.d.ts +6 -1
  100. package/koni/background/cron.js +172 -62
  101. package/koni/background/handlers/Extension.d.ts +9 -3
  102. package/koni/background/handlers/Extension.js +306 -126
  103. package/koni/background/handlers/State.d.ts +5 -6
  104. package/koni/background/handlers/State.js +51 -34
  105. package/koni/background/handlers/Tabs.js +50 -17
  106. package/koni/background/subscription.d.ts +2 -0
  107. package/koni/background/subscription.js +51 -29
  108. package/package.json +29 -14
  109. package/packageInfo.js +1 -1
  110. package/services/base/types.d.ts +34 -0
  111. package/services/base/types.js +15 -0
  112. package/services/chain-service/handler/SubstrateChainHandler.js +14 -9
  113. package/services/chain-service/helper/psp22_abi.json +1041 -881
  114. package/services/chain-service/helper/psp34_abi.json +2963 -1807
  115. package/services/chain-service/index.d.ts +5 -2
  116. package/services/chain-service/index.js +68 -45
  117. package/services/chain-service/types.d.ts +1 -0
  118. package/services/event-service/index.js +5 -1
  119. package/services/event-service/types.d.ts +5 -9
  120. package/services/event-service/types.js +4 -1
  121. package/services/history-service/index.d.ts +28 -7
  122. package/services/history-service/index.js +101 -50
  123. package/services/history-service/subsquid-multi-chain-history.js +16 -12
  124. package/services/keyring-service/index.d.ts +4 -2
  125. package/services/keyring-service/index.js +11 -13
  126. package/services/migration-service/scripts/MigrateImportedToken.js +2 -1
  127. package/services/price-service/coingecko.js +0 -1
  128. package/services/price-service/index.d.ts +22 -1
  129. package/services/price-service/index.js +71 -24
  130. package/services/request-service/handler/AuthRequestHandler.d.ts +3 -1
  131. package/services/request-service/handler/AuthRequestHandler.js +13 -7
  132. package/services/request-service/handler/EvmRequestHandler.js +8 -12
  133. package/services/request-service/index.d.ts +3 -1
  134. package/services/request-service/index.js +14 -5
  135. package/services/storage-service/DatabaseService.d.ts +2 -0
  136. package/services/storage-service/DatabaseService.js +66 -34
  137. package/services/storage-service/db-stores/Nft.d.ts +2 -2
  138. package/services/storage-service/db-stores/Nft.js +7 -14
  139. package/services/storage-service/db-stores/Transaction.d.ts +2 -0
  140. package/services/storage-service/db-stores/Transaction.js +6 -10
  141. package/services/transaction-service/event-parser/index.js +21 -49
  142. package/services/transaction-service/index.d.ts +2 -0
  143. package/services/transaction-service/index.js +86 -32
  144. package/services/transaction-service/types.d.ts +2 -0
  145. package/services/transaction-service/utils.js +10 -8
  146. package/utils/address.d.ts +3 -0
  147. package/utils/address.js +8 -1
  148. package/utils/index.d.ts +2 -2
  149. package/utils/index.js +7 -13
  150. package/utils/promise.d.ts +6 -0
  151. package/utils/promise.js +20 -0
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.getPSP22ContractPromise = getPSP22ContractPromise;
7
7
  exports.getPSP34ContractPromise = getPSP34ContractPromise;
8
8
  exports.getPSP34TransferExtrinsic = getPSP34TransferExtrinsic;
9
+ var _utils = require("@subwallet/extension-base/koni/api/tokens/wasm/utils");
9
10
  var _helper = require("@subwallet/extension-base/services/chain-service/helper");
10
11
  var _apiContract = require("@polkadot/api-contract");
11
12
  // Copyright 2019-2022 @subwallet/extension-base
@@ -22,10 +23,10 @@ async function getPSP34TransferExtrinsic(networkKey, substrateApi, senderAddress
22
23
  const onChainOption = params.onChainOption;
23
24
  try {
24
25
  const contractPromise = getPSP34ContractPromise(substrateApi.api, contractAddress);
25
- const transferQuery = await contractPromise.query['psp34::transfer'](senderAddress, {
26
- gasLimit: -1
27
- }, recipientAddress, onChainOption, {});
28
- const gasLimit = transferQuery.gasRequired.toString();
26
+ // @ts-ignore
27
+ const gasLimit = await (0, _utils.getWasmContractGasLimit)(substrateApi.api, senderAddress, 'psp34::transfer', contractPromise, {}, [recipientAddress, onChainOption, {}]);
28
+
29
+ // @ts-ignore
29
30
  return contractPromise.tx['psp34::transfer']({
30
31
  gasLimit
31
32
  }, recipientAddress, onChainOption, {});
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getDefaultWeightV2 = getDefaultWeightV2;
7
+ exports.getWasmContractGasLimit = getWasmContractGasLimit;
8
+ var _util = require("@polkadot/util");
9
+ // Copyright 2019-2022 @subwallet/extension-base
10
+ // SPDX-License-Identifier: Apache-2.0
11
+
12
+ const MAX_CALL_WEIGHT = '5000000000000';
13
+ const DEFAULT_REF_TIME = '1000000000000';
14
+ const toContractAbiMessage = (contractPromise, message) => {
15
+ const value = contractPromise.abi.messages.find(m => m.method === message);
16
+ if (!value) {
17
+ const messages = contractPromise === null || contractPromise === void 0 ? void 0 : contractPromise.abi.messages.map(m => m.method).join(', ');
18
+ const error = `"${message}" not found in metadata.spec.messages: [${messages}]`;
19
+ console.error(error);
20
+ return {
21
+ ok: false,
22
+ error
23
+ };
24
+ }
25
+ return {
26
+ ok: true,
27
+ value
28
+ };
29
+ };
30
+ async function getWasmContractGasLimit(api, callerAddress, message, contract) {
31
+ let options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
32
+ let args = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : [];
33
+ try {
34
+ var _abiMessage$value;
35
+ const abiMessage = toContractAbiMessage(contract, message);
36
+ if (!abiMessage.ok) {
37
+ return getDefaultWeightV2(api, true);
38
+ }
39
+
40
+ // @ts-ignore
41
+ const {
42
+ gasLimit,
43
+ storageDepositLimit,
44
+ value
45
+ } = options;
46
+
47
+ // @ts-ignore
48
+ const {
49
+ gasRequired
50
+ } = await api.call.contractsApi.call(callerAddress, contract.address, value !== null && value !== void 0 ? value : new _util.BN(0), gasLimit !== null && gasLimit !== void 0 ? gasLimit : null, storageDepositLimit !== null && storageDepositLimit !== void 0 ? storageDepositLimit : null, abiMessage === null || abiMessage === void 0 ? void 0 : (_abiMessage$value = abiMessage.value) === null || _abiMessage$value === void 0 ? void 0 : _abiMessage$value.toU8a(args));
51
+ return gasRequired;
52
+ } catch {
53
+ return getDefaultWeightV2(api, true);
54
+ }
55
+ }
56
+ function getDefaultWeightV2(apiPromise, isFallback) {
57
+ const proofSize = isFallback ? 3407872 : MAX_CALL_WEIGHT; // TODO: handle error better
58
+ const refTime = isFallback ? 32490000000 : DEFAULT_REF_TIME;
59
+ return apiPromise.registry.createType('WeightV2', {
60
+ refTime,
61
+ proofSize
62
+ });
63
+ }
@@ -42,7 +42,7 @@ function getAssetLocation(tokenInfo, sendingValue) {
42
42
  };
43
43
  }
44
44
  function getExtrinsicByPolkadotXcmPallet(tokenInfo, originChainInfo, destinationChainInfo, recipientAddress, value, api) {
45
- const weightParam = _utils.NETWORK_USE_UNLIMITED_WEIGHT.includes(originChainInfo.slug) ? _utils.POLKADOT_UNLIMITED_WEIGHT : _utils.FOUR_INSTRUCTIONS_WEIGHT;
45
+ const weightParam = (0, _utils.getDestWeight)();
46
46
  const beneficiary = (0, _utils.getBeneficiary)(originChainInfo, destinationChainInfo, recipientAddress);
47
47
  const destination = getDestinationChainLocation(destinationChainInfo);
48
48
  const assetLocation = getAssetLocation(tokenInfo, value);
@@ -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
+ }
@@ -54,6 +54,6 @@ function getMultiLocationForXtokensPallet(originChainInfo, destinationChainInfo,
54
54
  };
55
55
  }
56
56
  function getExtrinsicByXtokensPallet(tokenInfo, originChainInfo, destinationChainInfo, recipientAddress, value, api) {
57
- 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)();
58
58
  return api.tx.xTokens.transfer(getCurrencyId(tokenInfo), value, getMultiLocationForXtokensPallet(originChainInfo, destinationChainInfo, recipientAddress), weightParam);
59
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,8 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.KoniCron = void 0;
7
7
  var _constants = require("@subwallet/extension-base/constants");
8
+ var _utils = require("@subwallet/extension-base/services/chain-service/utils");
9
+ var _utils2 = require("@subwallet/extension-base/utils");
8
10
  var _rxjs = require("rxjs");
9
11
  var _util = require("@polkadot/util");
10
12
  // Copyright 2019-2022 @subwallet/extension-koni authors & contributors
@@ -58,33 +60,95 @@ class KoniCron {
58
60
  delete this.cronMap[key];
59
61
  });
60
62
  };
61
- init = () => {
62
- const currentAccountInfo = this.state.keyringService.currentAccount;
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
- };
63
+
64
+ // init = () => {
65
+ // const currentAccountInfo = this.state.keyringService.currentAccount;
66
+ //
67
+ // if (!currentAccountInfo?.address) {
68
+ // return;
69
+ // }
70
+ //
71
+ // if (Object.keys(this.state.getSubstrateApiMap()).length !== 0 || Object.keys(this.state.getEvmApiMap()).length !== 0) {
72
+ // this.refreshNft(currentAccountInfo.address, this.state.getApiMap(), this.state.getSmartContractNfts(), this.state.getActiveChainInfoMap());
73
+ // this.updateApiMapStatus();
74
+ // this.refreshStakingReward(currentAccountInfo.address);
75
+ // this.refreshStakingRewardFastInterval(currentAccountInfo.address);
76
+ // // this.updateChainStakingMetadata(this.state.getChainInfoMap(), this.state.getChainStateMap(), this.state.getSubstrateApiMap());
77
+ // this.updateNominatorMetadata(currentAccountInfo.address, this.state.getChainInfoMap(), this.state.getChainStateMap(), this.state.getSubstrateApiMap());
78
+ // } else {
79
+ // this.setStakingRewardReady();
80
+ // }
81
+ // };
82
+
76
83
  start = () => {
77
84
  if (this.status === 'running') {
78
85
  return;
79
86
  }
80
- this.logger.log('Starting cron jobs');
81
87
  const currentAccountInfo = this.state.keyringService.currentAccount;
88
+ const commonReloadEvents = ['account.add', 'account.remove', 'account.updateCurrent', 'chain.add', 'asset.updateState'];
89
+ this.eventHandler = (events, eventTypes) => {
90
+ var _serviceInfo$currentA;
91
+ const serviceInfo = this.state.getServiceInfo();
92
+ const commonReload = eventTypes.some(eventType => commonReloadEvents.includes(eventType));
93
+ const chainUpdated = eventTypes.includes('chain.updateState');
94
+ const stakingSubmitted = eventTypes.includes('transaction.submitStaking');
95
+ const updatedChains = [];
96
+ if (chainUpdated) {
97
+ events.forEach(event => {
98
+ if (event.type === 'chain.updateState') {
99
+ const updatedData = event.data;
100
+ updatedChains.push(updatedData[0]);
101
+ }
102
+ });
103
+ }
104
+ if (!commonReload && !chainUpdated && !stakingSubmitted) {
105
+ return;
106
+ }
107
+ this.logger.log('ServiceInfo updated, Cron restarting...', eventTypes);
108
+ const address = (_serviceInfo$currentA = serviceInfo.currentAccountInfo) === null || _serviceInfo$currentA === void 0 ? void 0 : _serviceInfo$currentA.address;
109
+ if (!address) {
110
+ return;
111
+ }
112
+ const chainInfoMap = serviceInfo.chainInfoMap;
113
+ const needUpdateNft = this.needUpdateNft(chainInfoMap, updatedChains);
114
+ const needUpdateStaking = this.needUpdateStaking(chainInfoMap, updatedChains);
115
+
116
+ // NFT
117
+ (commonReload || needUpdateNft) && this.resetNft(address);
118
+ (commonReload || needUpdateNft) && this.removeCron('refreshNft');
119
+
120
+ // Staking
121
+ (commonReload || needUpdateStaking || stakingSubmitted) && this.resetStakingReward();
122
+ (commonReload || needUpdateStaking || stakingSubmitted) && this.removeCron('refreshStakingReward');
123
+ (commonReload || needUpdateStaking || stakingSubmitted) && this.removeCron('refreshPoolingStakingReward');
124
+ (commonReload || needUpdateStaking || stakingSubmitted) && this.removeCron('updateNominatorMetadata');
125
+ needUpdateStaking && this.removeCron('updateChainStakingMetadata');
126
+
127
+ // Chains
128
+ chainUpdated && this.removeCron('checkStatusApiMap');
129
+ chainUpdated && this.removeCron('recoverApiMap');
130
+ if (this.checkNetworkAvailable(serviceInfo)) {
131
+ // only add cron job if there's at least 1 active network
132
+ (commonReload || needUpdateNft) && this.addCron('refreshNft', this.refreshNft(address, serviceInfo.chainApiMap, this.state.getSmartContractNfts(), this.state.getActiveChainInfoMap()), _constants.CRON_REFRESH_NFT_INTERVAL);
133
+ chainUpdated && this.addCron('checkStatusApiMap', this.updateApiMapStatus, _constants.CRON_GET_API_MAP_STATUS, false);
134
+ chainUpdated && this.addCron('recoverApiMap', this.recoverApiMap, _constants.CRON_AUTO_RECOVER_DOTSAMA_INTERVAL, false);
135
+ (commonReload || needUpdateStaking || stakingSubmitted) && this.addCron('refreshStakingReward', this.refreshStakingReward(address), _constants.CRON_REFRESH_STAKING_REWARD_INTERVAL);
136
+ (commonReload || needUpdateStaking || stakingSubmitted) && this.addCron('refreshPoolingStakingReward', this.refreshStakingRewardFastInterval(address), _constants.CRON_REFRESH_STAKING_REWARD_FAST_INTERVAL);
137
+ (commonReload || needUpdateStaking || stakingSubmitted) && this.addCron('updateNominatorMetadata', this.updateNominatorMetadata(address, serviceInfo.chainInfoMap, serviceInfo.chainStateMap, serviceInfo.chainApiMap.substrate), _constants.CRON_REFRESH_CHAIN_NOMINATOR_METADATA);
138
+ needUpdateStaking && this.addCron('updateChainStakingMetadata', this.updateChainStakingMetadata(serviceInfo.chainInfoMap, serviceInfo.chainStateMap, serviceInfo.chainApiMap.substrate), _constants.CRON_REFRESH_CHAIN_STAKING_METADATA);
139
+ } else {
140
+ this.setStakingRewardReady();
141
+ }
142
+ };
143
+ this.state.eventService.onLazy(this.eventHandler);
82
144
  if (!(currentAccountInfo !== null && currentAccountInfo !== void 0 && currentAccountInfo.address)) {
83
145
  return;
84
146
  }
147
+ this.logger.log('Starting cron jobs');
85
148
  if (Object.keys(this.state.getSubstrateApiMap()).length !== 0 || Object.keys(this.state.getEvmApiMap()).length !== 0) {
86
149
  this.resetNft(currentAccountInfo.address);
87
150
  this.addCron('refreshNft', this.refreshNft(currentAccountInfo.address, this.state.getApiMap(), this.state.getSmartContractNfts(), this.state.getActiveChainInfoMap()), _constants.CRON_REFRESH_NFT_INTERVAL);
151
+ this.addCron('checkStatusApiMap', this.updateApiMapStatus, _constants.CRON_GET_API_MAP_STATUS);
88
152
  this.addCron('recoverApiMap', this.recoverApiMap, _constants.CRON_AUTO_RECOVER_DOTSAMA_INTERVAL, false);
89
153
  this.addCron('refreshStakingReward', this.refreshStakingReward(currentAccountInfo.address), _constants.CRON_REFRESH_STAKING_REWARD_INTERVAL);
90
154
  this.addCron('refreshPoolingStakingReward', this.refreshStakingRewardFastInterval(currentAccountInfo.address), _constants.CRON_REFRESH_STAKING_REWARD_FAST_INTERVAL);
@@ -93,46 +157,18 @@ class KoniCron {
93
157
  } else {
94
158
  this.setStakingRewardReady();
95
159
  }
96
- const reloadEvents = ['account.add', 'account.remove', 'account.updateCurrent', 'chain.add', 'chain.update', 'chain.enable', 'asset.update', 'asset.enable', 'transaction.done', 'transaction.failed'];
97
- this.state.eventService.onLazy((events, eventTypes) => {
98
- var _serviceInfo$currentA;
99
- const serviceInfo = this.state.getServiceInfo();
100
- const needReload = eventTypes.some(eT => reloadEvents.includes(eT));
101
- if (!needReload) {
102
- return;
103
- }
104
- this.logger.log('ServiceInfo updated, restarting...');
105
- const address = (_serviceInfo$currentA = serviceInfo.currentAccountInfo) === null || _serviceInfo$currentA === void 0 ? void 0 : _serviceInfo$currentA.address;
106
- if (!address) {
107
- return;
108
- }
109
- this.resetStakingReward();
110
- this.resetNft(address);
111
- this.removeCron('refreshNft');
112
- this.removeCron('refreshStakingReward');
113
- this.removeCron('refreshPoolingStakingReward');
114
- this.removeCron('checkStatusApiMap');
115
- this.removeCron('recoverApiMap');
116
- this.removeCron('updateChainStakingMetadata');
117
- this.removeCron('updateNominatorMetadata');
118
- if (this.checkNetworkAvailable(serviceInfo)) {
119
- // only add cron job if there's at least 1 active network
120
- this.addCron('refreshNft', this.refreshNft(address, serviceInfo.chainApiMap, this.state.getSmartContractNfts(), this.state.getActiveChainInfoMap()), _constants.CRON_REFRESH_NFT_INTERVAL);
121
- this.addCron('recoverApiMap', this.recoverApiMap, _constants.CRON_AUTO_RECOVER_DOTSAMA_INTERVAL, false);
122
- this.addCron('refreshStakingReward', this.refreshStakingReward(address), _constants.CRON_REFRESH_STAKING_REWARD_INTERVAL);
123
- this.addCron('refreshPoolingStakingReward', this.refreshStakingRewardFastInterval(address), _constants.CRON_REFRESH_STAKING_REWARD_FAST_INTERVAL);
124
- this.addCron('updateChainStakingMetadata', this.updateChainStakingMetadata(serviceInfo.chainInfoMap, serviceInfo.chainStateMap, serviceInfo.chainApiMap.substrate), _constants.CRON_REFRESH_CHAIN_STAKING_METADATA);
125
- this.addCron('updateNominatorMetadata', this.updateNominatorMetadata(address, serviceInfo.chainInfoMap, serviceInfo.chainStateMap, serviceInfo.chainApiMap.substrate), _constants.CRON_REFRESH_CHAIN_NOMINATOR_METADATA);
126
- } else {
127
- this.setStakingRewardReady();
128
- }
129
- });
130
160
  this.status = 'running';
131
161
  };
132
162
  stop = () => {
133
163
  if (this.status === 'stopped') {
134
164
  return;
135
165
  }
166
+
167
+ // Unsubscribe events
168
+ if (this.eventHandler) {
169
+ this.state.eventService.offLazy(this.eventHandler);
170
+ this.eventHandler = undefined;
171
+ }
136
172
  if (this.serviceSubscription) {
137
173
  this.serviceSubscription.unsubscribe();
138
174
  this.serviceSubscription = undefined;
@@ -141,6 +177,44 @@ class KoniCron {
141
177
  this.removeAllCrons();
142
178
  this.status = 'stopped';
143
179
  };
180
+ updateApiMapStatus = () => {
181
+ this.state.chainService.updateApiMapStatus().catch(console.error);
182
+ // const apiMap = this.state.getApiMap();
183
+ // const networkMap = this.state.getChainStateMap();
184
+ //
185
+ // for (const [key, substrateApi] of Object.entries(apiMap.substrate)) {
186
+ // let status: _ChainConnectionStatus = _ChainConnectionStatus.CONNECTING;
187
+ //
188
+ // if (substrateApi.isApiConnected) {
189
+ // status = _ChainConnectionStatus.CONNECTED;
190
+ // }
191
+ //
192
+ // if (!networkMap[key].connectionStatus) {
193
+ // this.state.updateChainConnectionStatus(key, status);
194
+ // } else if (networkMap[key].connectionStatus && networkMap[key].connectionStatus !== status) {
195
+ // this.state.updateChainConnectionStatus(key, status);
196
+ // }
197
+ // }
198
+ //
199
+ // for (const [key, evmApi] of Object.entries(apiMap.evm)) {
200
+ // evmApi.api.eth.net.isListening()
201
+ // .then(() => {
202
+ // if (!networkMap[key].connectionStatus) {
203
+ // this.state.updateChainConnectionStatus(key, _ChainConnectionStatus.CONNECTED);
204
+ // } else if (networkMap[key].connectionStatus && networkMap[key].connectionStatus !== _ChainConnectionStatus.CONNECTED) {
205
+ // this.state.updateChainConnectionStatus(key, _ChainConnectionStatus.CONNECTED);
206
+ // }
207
+ // })
208
+ // .catch(() => {
209
+ // if (!networkMap[key].connectionStatus) {
210
+ // this.state.updateChainConnectionStatus(key, _ChainConnectionStatus.CONNECTING);
211
+ // } else if (networkMap[key].connectionStatus && networkMap[key].connectionStatus !== _ChainConnectionStatus.CONNECTING) {
212
+ // this.state.updateChainConnectionStatus(key, _ChainConnectionStatus.CONNECTING);
213
+ // }
214
+ // });
215
+ // }
216
+ };
217
+
144
218
  recoverApiMap = () => {
145
219
  var _this$subscriptions;
146
220
  const apiMap = this.state.getApiMap();
@@ -161,28 +235,26 @@ class KoniCron {
161
235
  };
162
236
  refreshNft = (address, apiMap, smartContractNfts, chainInfoMap) => {
163
237
  return () => {
164
- this.logger.log('Refresh Nft state');
238
+ console.debug('Refresh NFT state');
165
239
  this.subscriptions.subscribeNft(address, apiMap.substrate, apiMap.evm, smartContractNfts, chainInfoMap);
166
240
  };
167
241
  };
168
242
  resetNft = newAddress => {
169
- this.logger.log('Reset Nft state');
170
243
  this.state.resetNft(newAddress);
171
244
  };
172
245
  resetStakingReward = () => {
173
- this.logger.log('Reset Staking Reward State');
174
246
  this.state.resetStakingReward();
175
247
  };
176
248
  refreshStakingReward = address => {
177
249
  return () => {
178
- this.logger.log('Fetching staking reward data');
179
- this.subscriptions.subscribeStakingReward(address).then(() => this.logger.log('Refresh staking reward state')).catch(this.logger.error);
250
+ console.debug('Refresh staking reward state');
251
+ this.subscriptions.subscribeStakingReward(address).catch(this.logger.error);
180
252
  };
181
253
  };
182
254
  refreshStakingRewardFastInterval = address => {
183
255
  return () => {
184
- this.logger.log('Fetching staking reward data with fast interval');
185
- this.subscriptions.subscribeStakingRewardFastInterval(address).then(() => this.logger.log('Refresh staking reward state with fast interval')).catch(this.logger.error);
256
+ console.debug('Refresh staking reward data with fast interval');
257
+ this.subscriptions.subscribeStakingRewardFastInterval(address).catch(this.logger.error);
186
258
  };
187
259
  };
188
260
  setStakingRewardReady = () => {
@@ -193,15 +265,53 @@ class KoniCron {
193
265
  };
194
266
  updateChainStakingMetadata = (chainInfoMap, chainStateMap, substrateApiMap) => {
195
267
  return () => {
196
- this.logger.log('Fetching chain staking metadata');
197
- this.subscriptions.fetchChainStakingMetadata(chainInfoMap, chainStateMap, substrateApiMap).then(() => this.logger.log('Updated chain staking metadata')).catch(this.logger.error);
268
+ console.debug('Fetching chain staking metadata');
269
+ this.subscriptions.fetchChainStakingMetadata(chainInfoMap, chainStateMap, substrateApiMap).catch(this.logger.error);
198
270
  };
199
271
  };
200
272
  updateNominatorMetadata = (address, chainInfoMap, chainStateMap, substrateApiMap) => {
201
273
  return () => {
202
- this.logger.log('Fetching nominator data', address);
203
- this.subscriptions.fetchNominatorMetadata(address, chainInfoMap, chainStateMap, substrateApiMap).then(() => this.logger.log('Updated nominator data', address)).catch(this.logger.error);
274
+ console.debug('Fetching nominator data for', address);
275
+ this.subscriptions.fetchNominatorMetadata(address, chainInfoMap, chainStateMap, substrateApiMap).catch(this.logger.error);
204
276
  };
205
277
  };
278
+ async reloadNft() {
279
+ const address = this.state.keyringService.currentAccount.address;
280
+ const serviceInfo = this.state.getServiceInfo();
281
+ console.debug('Hard refresh NFT for', address);
282
+ this.resetNft(address);
283
+ this.removeCron('refreshNft');
284
+ this.addCron('refreshNft', this.refreshNft(address, serviceInfo.chainApiMap, this.state.getSmartContractNfts(), this.state.getActiveChainInfoMap()), _constants.CRON_REFRESH_NFT_INTERVAL);
285
+ await (0, _utils2.waitTimeout)(1800);
286
+ return true;
287
+ }
288
+ async reloadStaking() {
289
+ const address = this.state.keyringService.currentAccount.address;
290
+ console.debug('Hard refresh staking meta for', address);
291
+ this.resetStakingReward();
292
+ this.removeCron('refreshStakingReward');
293
+ this.removeCron('refreshPoolingStakingReward');
294
+ this.removeCron('updateNominatorMetadata');
295
+ this.addCron('refreshStakingReward', this.refreshStakingReward(address), _constants.CRON_REFRESH_STAKING_REWARD_INTERVAL);
296
+ this.addCron('refreshPoolingStakingReward', this.refreshStakingRewardFastInterval(address), _constants.CRON_REFRESH_STAKING_REWARD_FAST_INTERVAL);
297
+ this.addCron('updateNominatorMetadata', this.updateNominatorMetadata(address, this.state.getChainInfoMap(), this.state.getChainStateMap(), this.state.getSubstrateApiMap()), _constants.CRON_REFRESH_CHAIN_NOMINATOR_METADATA);
298
+ await (0, _utils2.waitTimeout)(1800);
299
+ return true;
300
+ }
301
+ needUpdateNft(chainInfoMap, updatedChains) {
302
+ if (updatedChains && updatedChains.length > 0) {
303
+ return updatedChains.some(updatedChain => {
304
+ const chainInfo = chainInfoMap[updatedChain];
305
+ return (0, _utils._isChainSupportNativeNft)(chainInfo) || (0, _utils._isChainSupportEvmNft)(chainInfo) || (0, _utils._isChainSupportWasmNft)(chainInfo);
306
+ });
307
+ }
308
+ return false;
309
+ }
310
+ needUpdateStaking(chainInfoMap, updatedChains) {
311
+ if (updatedChains && updatedChains.length > 0) {
312
+ return updatedChains.some(updatedChain => (0, _utils._isChainSupportSubstrateStaking)(chainInfoMap[updatedChain]));
313
+ }
314
+ return false;
315
+ }
206
316
  }
207
317
  exports.KoniCron = KoniCron;