@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
@@ -1,16 +1,25 @@
1
1
  // Copyright 2019-2022 @subwallet/extension-koni authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import { StakingType, UnstakingStatus } from '@subwallet/extension-base/background/KoniTypes';
4
+ import { StakingStatus, StakingType, UnstakingStatus } from '@subwallet/extension-base/background/KoniTypes';
5
5
  import { _KNOWN_CHAIN_INFLATION_PARAMS, _STAKING_CHAIN_GROUP, _SUBSTRATE_DEFAULT_INFLATION_PARAMS } from '@subwallet/extension-base/services/chain-service/constants';
6
6
  import { parseRawNumber, reformatAddress } from '@subwallet/extension-base/utils';
7
- import { BN, BN_BILLION, BN_HUNDRED, BN_MILLION, BN_THOUSAND, BN_ZERO } from '@polkadot/util';
7
+ import { BN, BN_BILLION, BN_HUNDRED, BN_MILLION, BN_THOUSAND, BN_ZERO, bnToU8a, stringToU8a, u8aConcat } from '@polkadot/util';
8
8
  export let PalletParachainStakingRequestType;
9
9
  (function (PalletParachainStakingRequestType) {
10
10
  PalletParachainStakingRequestType["REVOKE"] = "revoke";
11
11
  PalletParachainStakingRequestType["DECREASE"] = "decrease";
12
12
  PalletParachainStakingRequestType["BOND_LESS"] = "bondLess";
13
13
  })(PalletParachainStakingRequestType || (PalletParachainStakingRequestType = {}));
14
+ export function parsePoolStashAddress(api, index, poolId, poolsPalletId) {
15
+ const ModPrefix = stringToU8a('modl');
16
+ const U32Opts = {
17
+ bitLength: 32,
18
+ isLe: true
19
+ };
20
+ const EmptyH256 = new Uint8Array(32);
21
+ return api.registry.createType('AccountId32', u8aConcat(ModPrefix, poolsPalletId, new Uint8Array([index]), bnToU8a(new BN(poolId.toString()), U32Opts), EmptyH256)).toString();
22
+ }
14
23
  export function transformPoolName(input) {
15
24
  return input.replace(/[^\x20-\x7E]/g, '');
16
25
  }
@@ -116,11 +125,8 @@ export function getBondedValidators(nominations) {
116
125
  const bondedValidators = [];
117
126
  let nominationCount = 0;
118
127
  for (const nomination of nominations) {
119
- const bnActiveStake = new BN(nomination.activeStake);
120
- if (bnActiveStake.gt(BN_ZERO)) {
121
- nominationCount += 1;
122
- bondedValidators.push(reformatAddress(nomination.validatorAddress, 0));
123
- }
128
+ nominationCount += 1;
129
+ bondedValidators.push(reformatAddress(nomination.validatorAddress, 0));
124
130
  }
125
131
  return {
126
132
  nominationCount,
@@ -130,7 +136,9 @@ export function getBondedValidators(nominations) {
130
136
  export function isUnstakeAll(selectedValidator, nominations, unstakeAmount) {
131
137
  let isUnstakeAll = false;
132
138
  for (const nomination of nominations) {
133
- if (nomination.validatorAddress === selectedValidator) {
139
+ const parsedValidatorAddress = reformatAddress(nomination.validatorAddress, 0);
140
+ const parsedSelectedValidator = reformatAddress(selectedValidator, 0);
141
+ if (parsedValidatorAddress === parsedSelectedValidator) {
134
142
  if (unstakeAmount === nomination.activeStake) {
135
143
  isUnstakeAll = true;
136
144
  }
@@ -147,13 +155,28 @@ export let StakingAction;
147
155
  StakingAction["CLAIM_REWARD"] = "CLAIM_REWARD";
148
156
  StakingAction["CANCEL_UNSTAKE"] = "CANCEL_UNSTAKE";
149
157
  })(StakingAction || (StakingAction = {}));
150
- export function getStakingAvailableActions(nominatorMetadata) {
158
+ export function getStakingAvailableActionsByChain(chain, type) {
159
+ if (type === StakingType.POOLED) {
160
+ return [StakingAction.STAKE, StakingAction.UNSTAKE, StakingAction.WITHDRAW, StakingAction.CLAIM_REWARD];
161
+ }
162
+ if (_STAKING_CHAIN_GROUP.para.includes(chain)) {
163
+ return [StakingAction.STAKE, StakingAction.UNSTAKE, StakingAction.WITHDRAW, StakingAction.CANCEL_UNSTAKE];
164
+ } else if (_STAKING_CHAIN_GROUP.astar.includes(chain)) {
165
+ return [StakingAction.STAKE, StakingAction.UNSTAKE, StakingAction.WITHDRAW, StakingAction.CLAIM_REWARD];
166
+ } else if (_STAKING_CHAIN_GROUP.amplitude.includes(chain)) {
167
+ return [StakingAction.STAKE, StakingAction.UNSTAKE, StakingAction.WITHDRAW];
168
+ }
169
+ return [StakingAction.STAKE, StakingAction.UNSTAKE, StakingAction.WITHDRAW, StakingAction.CANCEL_UNSTAKE];
170
+ }
171
+ export function getStakingAvailableActionsByNominator(nominatorMetadata, unclaimedReward) {
151
172
  const result = [StakingAction.STAKE];
152
173
  const bnActiveStake = new BN(nominatorMetadata.activeStake);
153
174
  if (nominatorMetadata.activeStake && bnActiveStake.gt(BN_ZERO)) {
154
175
  result.push(StakingAction.UNSTAKE);
155
- const isChainAllowClaimReward = _STAKING_CHAIN_GROUP.amplitude.includes(nominatorMetadata.chain) || _STAKING_CHAIN_GROUP.astar.includes(nominatorMetadata.chain);
156
- if (nominatorMetadata.type === StakingType.POOLED || isChainAllowClaimReward) {
176
+ const isAstarNetwork = _STAKING_CHAIN_GROUP.astar.includes(nominatorMetadata.chain);
177
+ const isAmplitudeNetwork = _STAKING_CHAIN_GROUP.amplitude.includes(nominatorMetadata.chain);
178
+ const bnUnclaimedReward = new BN(unclaimedReward || '0');
179
+ if ((nominatorMetadata.type === StakingType.POOLED || isAmplitudeNetwork) && bnUnclaimedReward.gt(BN_ZERO) || isAstarNetwork) {
157
180
  result.push(StakingAction.CLAIM_REWARD);
158
181
  }
159
182
  }
@@ -195,4 +218,23 @@ export function getWithdrawalInfo(nominatorMetadata) {
195
218
  }
196
219
  }
197
220
  return result;
221
+ }
222
+ export function getStakingStatusByNominations(bnTotalActiveStake, nominationList) {
223
+ let stakingStatus = StakingStatus.EARNING_REWARD;
224
+ if (bnTotalActiveStake.isZero()) {
225
+ stakingStatus = StakingStatus.NOT_EARNING;
226
+ } else {
227
+ let invalidDelegationCount = 0;
228
+ for (const nomination of nominationList) {
229
+ if (nomination.status === StakingStatus.NOT_EARNING) {
230
+ invalidDelegationCount += 1;
231
+ }
232
+ }
233
+ if (invalidDelegationCount > 0 && invalidDelegationCount < nominationList.length) {
234
+ stakingStatus = StakingStatus.PARTIALLY_EARNING;
235
+ } else if (invalidDelegationCount === nominationList.length) {
236
+ stakingStatus = StakingStatus.NOT_EARNING;
237
+ }
238
+ }
239
+ return stakingStatus;
198
240
  }
@@ -1,6 +1,7 @@
1
1
  // Copyright 2019-2022 @subwallet/extension-base
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
+ import { getWasmContractGasLimit } from '@subwallet/extension-base/koni/api/tokens/wasm/utils';
4
5
  import { _PSP22_ABI, _PSP34_ABI } from '@subwallet/extension-base/services/chain-service/helper';
5
6
  import { ContractPromise } from '@polkadot/api-contract';
6
7
  export function getPSP22ContractPromise(apiPromise, contractAddress) {
@@ -14,10 +15,10 @@ export async function getPSP34TransferExtrinsic(networkKey, substrateApi, sender
14
15
  const onChainOption = params.onChainOption;
15
16
  try {
16
17
  const contractPromise = getPSP34ContractPromise(substrateApi.api, contractAddress);
17
- const transferQuery = await contractPromise.query['psp34::transfer'](senderAddress, {
18
- gasLimit: -1
19
- }, recipientAddress, onChainOption, {});
20
- const gasLimit = transferQuery.gasRequired.toString();
18
+ // @ts-ignore
19
+ const gasLimit = await getWasmContractGasLimit(substrateApi.api, senderAddress, 'psp34::transfer', contractPromise, {}, [recipientAddress, onChainOption, {}]);
20
+
21
+ // @ts-ignore
21
22
  return contractPromise.tx['psp34::transfer']({
22
23
  gasLimit
23
24
  }, recipientAddress, onChainOption, {});
@@ -0,0 +1,6 @@
1
+ import { ApiPromise } from '@polkadot/api';
2
+ import { ContractPromise } from '@polkadot/api-contract';
3
+ import { WeightV2 } from '@polkadot/types/interfaces';
4
+ import { Codec } from '@polkadot/types/types';
5
+ export declare function getWasmContractGasLimit(api: ApiPromise, callerAddress: string, message: string, contract: ContractPromise, options?: {}, args?: never[]): Promise<Codec>;
6
+ export declare function getDefaultWeightV2(apiPromise: ApiPromise, isFallback?: boolean): WeightV2;
@@ -0,0 +1,54 @@
1
+ // Copyright 2019-2022 @subwallet/extension-base
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { BN } from '@polkadot/util';
5
+ const MAX_CALL_WEIGHT = '5000000000000';
6
+ const DEFAULT_REF_TIME = '1000000000000';
7
+ const toContractAbiMessage = (contractPromise, message) => {
8
+ const value = contractPromise.abi.messages.find(m => m.method === message);
9
+ if (!value) {
10
+ const messages = contractPromise === null || contractPromise === void 0 ? void 0 : contractPromise.abi.messages.map(m => m.method).join(', ');
11
+ const error = `"${message}" not found in metadata.spec.messages: [${messages}]`;
12
+ console.error(error);
13
+ return {
14
+ ok: false,
15
+ error
16
+ };
17
+ }
18
+ return {
19
+ ok: true,
20
+ value
21
+ };
22
+ };
23
+ export async function getWasmContractGasLimit(api, callerAddress, message, contract, options = {}, args = []) {
24
+ try {
25
+ var _abiMessage$value;
26
+ const abiMessage = toContractAbiMessage(contract, message);
27
+ if (!abiMessage.ok) {
28
+ return getDefaultWeightV2(api, true);
29
+ }
30
+
31
+ // @ts-ignore
32
+ const {
33
+ gasLimit,
34
+ storageDepositLimit,
35
+ value
36
+ } = options;
37
+
38
+ // @ts-ignore
39
+ const {
40
+ gasRequired
41
+ } = await api.call.contractsApi.call(callerAddress, contract.address, value !== null && value !== void 0 ? value : new 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));
42
+ return gasRequired;
43
+ } catch {
44
+ return getDefaultWeightV2(api, true);
45
+ }
46
+ }
47
+ export function getDefaultWeightV2(apiPromise, isFallback) {
48
+ const proofSize = isFallback ? 3407872 : MAX_CALL_WEIGHT; // TODO: handle error better
49
+ const refTime = isFallback ? 32490000000 : DEFAULT_REF_TIME;
50
+ return apiPromise.registry.createType('WeightV2', {
51
+ refTime,
52
+ proofSize
53
+ });
54
+ }
@@ -27,115 +27,4 @@ export const createXcmExtrinsic = async ({
27
27
  }
28
28
  console.log('XCM extrinsic: ', extrinsic.toHex());
29
29
  return extrinsic;
30
- };
31
-
32
- // TODO: add + refine logic for more chains
33
- // export function updateXcmResponseTxResult (
34
- // networkKey: string,
35
- // tokenInfo: _ChainAsset,
36
- // response: TransactionResponse,
37
- // records: EventRecord[]
38
- // ) {
39
- // if (!response.txResult) {
40
- // response.txResult = { change: '0' };
41
- // }
42
- //
43
- // let isFeeUseMainTokenSymbol = true;
44
- //
45
- // for (let index = 0; index < records.length; index++) {
46
- // const record = records[index];
47
- //
48
- // if (_XCM_CHAIN_GROUP.acala.includes(networkKey) && !_isNativeToken(tokenInfo)) {
49
- // if (record.event.section === 'currencies' &&
50
- // record.event.method.toLowerCase() === 'transferred') {
51
- // if (index === 0) {
52
- // // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
53
- // response.txResult.fee = record.event.data[3]?.toString() || '0';
54
- // response.txResult.feeSymbol = tokenInfo.symbol;
55
- //
56
- // isFeeUseMainTokenSymbol = false;
57
- // } else {
58
- // response.txResult.change = record.event.data[3]?.toString() || '0';
59
- // response.txResult.changeSymbol = tokenInfo.symbol;
60
- // }
61
- // }
62
- //
63
- // if (record.event.section === 'tokens' &&
64
- // record.event.method.toLowerCase() === 'withdrawn') {
65
- // response.txResult.change = record.event.data[2]?.toString() || '0';
66
- // response.txResult.changeSymbol = tokenInfo.symbol;
67
- // }
68
- // } else if (_XCM_CHAIN_GROUP.kintsugi.includes(networkKey) && !_isNativeToken(tokenInfo)) {
69
- // if (record.event.section === 'tokens' &&
70
- // record.event.method.toLowerCase() === 'transfer') {
71
- // response.txResult.change = record.event.data[3]?.toString() || '0';
72
- // response.txResult.changeSymbol = tokenInfo.symbol;
73
- // }
74
- // } else if (_XCM_CHAIN_GROUP.genshiro.includes(networkKey) && !_isNativeToken(tokenInfo)) {
75
- // if (record.event.section === 'eqBalances' &&
76
- // record.event.method.toLowerCase() === 'transfer') {
77
- // response.txResult.change = record.event.data[3]?.toString() || '0';
78
- // response.txResult.changeSymbol = tokenInfo.symbol;
79
- // }
80
- // } else if (_XCM_CHAIN_GROUP.bifrost.includes(networkKey) && !_isNativeToken(tokenInfo)) {
81
- // if (record.event.section === 'tokens' &&
82
- // record.event.method.toLowerCase() === 'withdrawn') {
83
- // response.txResult.change = record.event.data[2]?.toString() || '0';
84
- // response.txResult.changeSymbol = tokenInfo.symbol;
85
- // } else if (record.event.section === 'balances' &&
86
- // record.event.method.toLowerCase() === 'transfer') {
87
- // // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
88
- // response.txResult.change = record.event.data[2]?.toString() || '0';
89
- // response.txResult.changeSymbol = tokenInfo.symbol;
90
- // }
91
- // } else if (_XCM_CHAIN_GROUP.astar.includes(networkKey) && !_isNativeToken(tokenInfo)) {
92
- // if (record.event.section === 'assets' &&
93
- // record.event.method.toLowerCase() === 'burned') {
94
- // response.txResult.change = record.event.data[2]?.toString() || '0';
95
- // response.txResult.changeSymbol = tokenInfo.symbol;
96
- // }
97
- // } else if (_XCM_CHAIN_GROUP.moonbeam.includes(networkKey) && !_isNativeToken(tokenInfo)) {
98
- // if (record.event.section === 'assets' &&
99
- // record.event.method.toLowerCase() === 'burned') {
100
- // response.txResult.change = record.event.data[2]?.toString() || '0';
101
- // response.txResult.changeSymbol = tokenInfo.symbol;
102
- // }
103
- // } else if (_XCM_CHAIN_GROUP.statemine.includes(networkKey)) {
104
- // if (!_isNativeToken(tokenInfo)) {
105
- // if (record.event.section === 'assets' &&
106
- // record.event.method.toLowerCase() === 'transferred') {
107
- // response.txResult.change = record.event.data[3]?.toString() || '0';
108
- // response.txResult.changeSymbol = tokenInfo.symbol;
109
- // }
110
- // } else {
111
- // if (record.event.section === 'balances' &&
112
- // record.event.method.toLowerCase() === 'withdraw') {
113
- // response.txResult.change = record.event.data[1]?.toString() || '0';
114
- // response.txResult.changeSymbol = tokenInfo.symbol;
115
- // }
116
- // }
117
- // } else {
118
- // if (record.event.section === 'balances' &&
119
- // record.event.method.toLowerCase() === 'transfer') {
120
- // // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
121
- // response.txResult.change = record.event.data[2]?.toString() || '0';
122
- // } else if (record.event.section === 'xTokens' &&
123
- // record.event.method.toLowerCase() === 'transferred') {
124
- // // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
125
- // response.txResult.change = record.event.data[2]?.toString() || '0';
126
- // }
127
- // }
128
- //
129
- // if (isFeeUseMainTokenSymbol && record.event.section === 'balances' && record.event.method.toLowerCase() === 'withdraw') {
130
- // if (!response.txResult.fee) {
131
- // // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
132
- // response.txResult.fee = record.event.data[1]?.toString() || '0';
133
- // }
134
- // } else if (isFeeUseMainTokenSymbol && record.event.section === 'tokens' && record.event.method.toLowerCase() === 'withdrawn') {
135
- // if (!response.txResult.fee) {
136
- // // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
137
- // response.txResult.fee = record.event.data[2]?.toString() || '0';
138
- // }
139
- // }
140
- // }
141
- // }
30
+ };
@@ -1,7 +1,7 @@
1
1
  // Copyright 2019-2022 @subwallet/extension-base
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import { FOUR_INSTRUCTIONS_WEIGHT, getBeneficiary, NETWORK_USE_UNLIMITED_WEIGHT, POLKADOT_UNLIMITED_WEIGHT } from '@subwallet/extension-base/koni/api/xcm/utils';
4
+ import { getBeneficiary, getDestWeight } from '@subwallet/extension-base/koni/api/xcm/utils';
5
5
  import { _getSubstrateParaId, _getXcmAssetMultilocation, _isSubstrateParaChain, _isSubstrateRelayChain } from '@subwallet/extension-base/services/chain-service/utils';
6
6
  function getDestinationChainLocation(destinationChainInfo) {
7
7
  if (_isSubstrateParaChain(destinationChainInfo)) {
@@ -36,13 +36,13 @@ function getAssetLocation(tokenInfo, sendingValue) {
36
36
  };
37
37
  }
38
38
  export function getExtrinsicByPolkadotXcmPallet(tokenInfo, originChainInfo, destinationChainInfo, recipientAddress, value, api) {
39
- const weightParam = NETWORK_USE_UNLIMITED_WEIGHT.includes(originChainInfo.slug) ? POLKADOT_UNLIMITED_WEIGHT : FOUR_INSTRUCTIONS_WEIGHT;
39
+ const weightParam = getDestWeight();
40
40
  const beneficiary = getBeneficiary(originChainInfo, destinationChainInfo, recipientAddress);
41
41
  const destination = getDestinationChainLocation(destinationChainInfo);
42
42
  const assetLocation = getAssetLocation(tokenInfo, value);
43
43
  let method = 'limitedReserveTransferAssets';
44
44
  if (['astar', 'shiden'].includes(originChainInfo.slug)) {
45
- method = 'reserveWithdrawAssets';
45
+ method = 'limitedReserveWithdrawAssets';
46
46
  } else if (_isSubstrateRelayChain(destinationChainInfo)) {
47
47
  method = 'limitedTeleportAssets';
48
48
  }
@@ -1,16 +1,15 @@
1
1
  import { _ChainInfo } from '@subwallet/chain-list/types';
2
- export declare const FOUR_INSTRUCTIONS_WEIGHT: {
2
+ export declare const FOUR_INSTRUCTIONS_WEIGHT = 5000000000;
3
+ export declare const FOUR_INSTRUCTIONS_LIMITED_WEIGHT: {
3
4
  Limited: number;
4
5
  };
5
- export declare const POLKADOT_LIMITED_WEIGHT = 1000000000;
6
- export declare const POLKADOT_UNLIMITED_WEIGHT = "Unlimited";
7
6
  export declare function getReceiverLocation(originChainInfo: _ChainInfo, destinationChainInfo: _ChainInfo, toAddress: string): Record<string, any>;
8
- export declare function getBeneficiary(originChainInfo: _ChainInfo, destinationChainInfo: _ChainInfo, recipientAddress: string): {
9
- V1: {
7
+ export declare function getBeneficiary(originChainInfo: _ChainInfo, destinationChainInfo: _ChainInfo, recipientAddress: string, version?: string): {
8
+ [x: string]: {
10
9
  parents: number;
11
10
  interior: {
12
11
  X1: Record<string, any>;
13
12
  };
14
13
  };
15
14
  };
16
- export declare const NETWORK_USE_UNLIMITED_WEIGHT: string[];
15
+ export declare function getDestWeight(): string;
@@ -2,20 +2,19 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
4
  import { COMMON_CHAIN_SLUGS } from '@subwallet/chain-list';
5
- import { _getChainSubstrateAddressPrefix, _isChainEvmCompatible } from '@subwallet/extension-base/services/chain-service/utils';
5
+ import { _isChainEvmCompatible } from '@subwallet/extension-base/services/chain-service/utils';
6
6
  import { decodeAddress, evmToAddress } from '@polkadot/util-crypto';
7
- export const FOUR_INSTRUCTIONS_WEIGHT = {
7
+ export const FOUR_INSTRUCTIONS_WEIGHT = 5000000000;
8
+ export const FOUR_INSTRUCTIONS_LIMITED_WEIGHT = {
8
9
  Limited: 5000000000
9
10
  };
10
- export const POLKADOT_LIMITED_WEIGHT = 1000000000;
11
- export const POLKADOT_UNLIMITED_WEIGHT = 'Unlimited';
12
11
 
13
12
  // get multilocation for destination chain from a parachain
14
13
 
15
14
  export function getReceiverLocation(originChainInfo, destinationChainInfo, toAddress) {
16
- // @ts-ignore
17
- if (destinationChainInfo.slug === COMMON_CHAIN_SLUGS.ASTAR_EVM || destinationChainInfo.slug === 'astarEvm') {
18
- const ss58Address = evmToAddress(toAddress, _getChainSubstrateAddressPrefix(destinationChainInfo));
15
+ if (destinationChainInfo.slug === COMMON_CHAIN_SLUGS.ASTAR_EVM) {
16
+ const ss58Address = evmToAddress(toAddress, 2006); // TODO: shouldn't pass addressPrefix directly
17
+
19
18
  return {
20
19
  AccountId32: {
21
20
  network: 'Any',
@@ -38,10 +37,10 @@ export function getReceiverLocation(originChainInfo, destinationChainInfo, toAdd
38
37
  }
39
38
  };
40
39
  }
41
- export function getBeneficiary(originChainInfo, destinationChainInfo, recipientAddress) {
40
+ export function getBeneficiary(originChainInfo, destinationChainInfo, recipientAddress, version = 'V1') {
42
41
  const receiverLocation = getReceiverLocation(originChainInfo, destinationChainInfo, recipientAddress);
43
42
  return {
44
- V1: {
43
+ [version]: {
45
44
  parents: 0,
46
45
  interior: {
47
46
  X1: receiverLocation
@@ -49,4 +48,10 @@ export function getBeneficiary(originChainInfo, destinationChainInfo, recipientA
49
48
  }
50
49
  };
51
50
  }
52
- export const NETWORK_USE_UNLIMITED_WEIGHT = ['acala', 'karura', 'statemint', 'moonriver'];
51
+ export function getDestWeight() {
52
+ return 'Unlimited';
53
+ // return api.tx.xTokens.transfer.meta.args[3].type.toString() ===
54
+ // 'XcmV2WeightLimit'
55
+ // ? 'Unlimited'
56
+ // : FOUR_INSTRUCTIONS_WEIGHT;
57
+ }
@@ -1,11 +1,10 @@
1
1
  // Copyright 2019-2022 @subwallet/extension-base
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import { FOUR_INSTRUCTIONS_WEIGHT, getReceiverLocation, NETWORK_USE_UNLIMITED_WEIGHT, POLKADOT_UNLIMITED_WEIGHT } from '@subwallet/extension-base/koni/api/xcm/utils';
4
+ import { FOUR_INSTRUCTIONS_WEIGHT, getDestWeight, getReceiverLocation } from '@subwallet/extension-base/koni/api/xcm/utils';
5
5
  import { _XCM_TYPE } from '@subwallet/extension-base/services/chain-service/constants';
6
- import { _getSubstrateParaId, _getXcmAssetId, _getXcmAssetMultilocation, _getXcmAssetType, _getXcmTransferType, _isNativeToken } from '@subwallet/extension-base/services/chain-service/utils';
6
+ import { _getSubstrateParaId, _getTokenOnChainInfo, _getXcmAssetId, _getXcmAssetMultilocation, _getXcmAssetType, _getXcmTransferType, _isNativeToken } from '@subwallet/extension-base/services/chain-service/utils';
7
7
  function getCurrencyId(tokenInfo) {
8
- var _tokenInfo$metadata2;
9
8
  if (['acala', 'karura'].includes(tokenInfo.originChain) && _isNativeToken(tokenInfo)) {
10
9
  return _getXcmAssetMultilocation(tokenInfo);
11
10
  } else if (['moonbeam', 'moonbase', 'moonriver'].includes(tokenInfo.originChain)) {
@@ -15,10 +14,9 @@ function getCurrencyId(tokenInfo) {
15
14
  [tokenType]: assetId
16
15
  };
17
16
  } else if (['pioneer'].includes(tokenInfo.originChain)) {
18
- var _tokenInfo$metadata;
19
- return (_tokenInfo$metadata = tokenInfo.metadata) === null || _tokenInfo$metadata === void 0 ? void 0 : _tokenInfo$metadata.multilocation;
17
+ return _getXcmAssetMultilocation(tokenInfo);
20
18
  }
21
- return (_tokenInfo$metadata2 = tokenInfo.metadata) === null || _tokenInfo$metadata2 === void 0 ? void 0 : _tokenInfo$metadata2.onChainInfo;
19
+ return _getTokenOnChainInfo(tokenInfo);
22
20
  }
23
21
  function getMultiLocationForXtokensPallet(originChainInfo, destinationChainInfo, toAddress) {
24
22
  const xcmType = _getXcmTransferType(originChainInfo, destinationChainInfo);
@@ -50,6 +48,6 @@ function getMultiLocationForXtokensPallet(originChainInfo, destinationChainInfo,
50
48
  };
51
49
  }
52
50
  export function getExtrinsicByXtokensPallet(tokenInfo, originChainInfo, destinationChainInfo, recipientAddress, value, api) {
53
- const weightParam = NETWORK_USE_UNLIMITED_WEIGHT.includes(originChainInfo.slug) ? POLKADOT_UNLIMITED_WEIGHT : FOUR_INSTRUCTIONS_WEIGHT;
51
+ const weightParam = ['pioneer'].includes(originChainInfo.slug) ? FOUR_INSTRUCTIONS_WEIGHT : getDestWeight();
54
52
  return api.tx.xTokens.transfer(getCurrencyId(tokenInfo), value, getMultiLocationForXtokensPallet(originChainInfo, destinationChainInfo, recipientAddress), weightParam);
55
53
  }
@@ -1,11 +1,11 @@
1
1
  // Copyright 2019-2022 @subwallet/extension-base
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import { FOUR_INSTRUCTIONS_WEIGHT, getBeneficiary, NETWORK_USE_UNLIMITED_WEIGHT, POLKADOT_UNLIMITED_WEIGHT } from '@subwallet/extension-base/koni/api/xcm/utils';
4
+ import { getBeneficiary, getDestWeight } from '@subwallet/extension-base/koni/api/xcm/utils';
5
5
  import { _getSubstrateParaId } from '@subwallet/extension-base/services/chain-service/utils';
6
- function getDestinationChainLocation(destinationChainInfo) {
6
+ function getDestinationChainLocation(destinationChainInfo, version = 'V1') {
7
7
  return {
8
- V1: {
8
+ [version]: {
9
9
  parents: 0,
10
10
  interior: {
11
11
  X1: {
@@ -15,10 +15,10 @@ function getDestinationChainLocation(destinationChainInfo) {
15
15
  }
16
16
  };
17
17
  }
18
- function getTokenLocation(sendingValue) {
18
+ function getTokenLocation(sendingValue, version = 'V2') {
19
19
  return {
20
20
  // always native token of relaychain
21
- V1: [{
21
+ [version]: [{
22
22
  id: {
23
23
  Concrete: {
24
24
  parents: 0,
@@ -34,10 +34,11 @@ function getTokenLocation(sendingValue) {
34
34
 
35
35
  // this pallet is only used by Relaychains
36
36
  export function getExtrinsicByXcmPalletPallet(tokenInfo, originChainInfo, destinationChainInfo, recipientAddress, value, api) {
37
- const weightParam = NETWORK_USE_UNLIMITED_WEIGHT.includes(originChainInfo.slug) ? POLKADOT_UNLIMITED_WEIGHT : FOUR_INSTRUCTIONS_WEIGHT;
38
- const destination = getDestinationChainLocation(destinationChainInfo);
39
- const beneficiary = getBeneficiary(originChainInfo, destinationChainInfo, recipientAddress);
40
- const tokenLocation = getTokenLocation(value);
37
+ const weightParam = getDestWeight();
38
+ const xcmVer = ['kusama'].includes(originChainInfo.slug) ? 'V2' : 'V1';
39
+ const destination = getDestinationChainLocation(destinationChainInfo, xcmVer);
40
+ const beneficiary = getBeneficiary(originChainInfo, destinationChainInfo, recipientAddress, xcmVer);
41
+ const tokenLocation = getTokenLocation(value, xcmVer);
41
42
  let method = 'limitedReserveTransferAssets';
42
43
  if (['statemint', 'statemine'].includes(destinationChainInfo.slug)) {
43
44
  method = 'limitedTeleportAssets';
@@ -15,15 +15,16 @@ export declare class KoniCron {
15
15
  constructor(state: KoniState, subscriptions: KoniSubscription, dbService: DatabaseService);
16
16
  private cronMap;
17
17
  private subjectMap;
18
+ private eventHandler?;
18
19
  getCron: (name: string) => any;
19
20
  getSubjectMap: (name: string) => any;
20
21
  addCron: (name: string, callback: (param?: any) => void, interval: number, runFirst?: boolean) => void;
21
22
  addSubscribeCron: <T>(name: string, callback: (subject: Subject<T>) => void, interval: number) => void;
22
23
  removeCron: (name: string) => void;
23
24
  removeAllCrons: () => void;
24
- init: () => void;
25
25
  start: () => void;
26
26
  stop: () => void;
27
+ updateApiMapStatus: () => void;
27
28
  recoverApiMap: () => void;
28
29
  refreshNft: (address: string, apiMap: ApiMap, smartContractNfts: _ChainAsset[], chainInfoMap: Record<string, _ChainInfo>) => () => void;
29
30
  resetNft: (newAddress: string) => void;
@@ -34,4 +35,8 @@ export declare class KoniCron {
34
35
  checkNetworkAvailable: (serviceInfo: ServiceInfo) => boolean;
35
36
  updateChainStakingMetadata: (chainInfoMap: Record<string, _ChainInfo>, chainStateMap: Record<string, _ChainState>, substrateApiMap: Record<string, _SubstrateApi>) => () => void;
36
37
  updateNominatorMetadata: (address: string, chainInfoMap: Record<string, _ChainInfo>, chainStateMap: Record<string, _ChainState>, substrateApiMap: Record<string, _SubstrateApi>) => () => void;
38
+ reloadNft(): Promise<boolean>;
39
+ reloadStaking(): Promise<boolean>;
40
+ private needUpdateNft;
41
+ private needUpdateStaking;
37
42
  }