@subwallet/extension-base 1.2.3-1 → 1.2.4-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 (53) hide show
  1. package/background/KoniTypes.d.ts +0 -153
  2. package/background/KoniTypes.js +0 -21
  3. package/cjs/background/KoniTypes.js +1 -22
  4. package/cjs/core/logic-validation/earning.js +2 -2
  5. package/cjs/core/logic-validation/transfer.js +0 -7
  6. package/cjs/core/substrate/nominationpools-pallet.js +11 -1
  7. package/cjs/core/substrate/system-pallet.js +2 -3
  8. package/cjs/core/substrate/xcm-parser.js +190 -0
  9. package/cjs/koni/api/dotsama/transfer.js +10 -119
  10. package/cjs/koni/api/xcm/polkadotXcm.js +12 -20
  11. package/cjs/koni/api/xcm/utils.js +8 -126
  12. package/cjs/koni/api/xcm/xTokens.js +10 -8
  13. package/cjs/koni/api/xcm/xcmPallet.js +6 -6
  14. package/cjs/koni/background/handlers/Extension.js +90 -113
  15. package/cjs/packageInfo.js +1 -1
  16. package/cjs/services/balance-service/helpers/subscribe/substrate/index.js +6 -3
  17. package/cjs/services/chain-service/constants.js +2 -2
  18. package/cjs/services/chain-service/utils/index.js +7 -2
  19. package/cjs/services/earning-service/handlers/special.js +3 -3
  20. package/cjs/services/migration-service/scripts/MigrateTransactionHistoryBySymbol.js +2 -1
  21. package/cjs/services/migration-service/scripts/databases/MigrateAssetSetting.js +2 -1
  22. package/cjs/services/migration-service/scripts/index.js +2 -2
  23. package/cjs/services/swap-service/handler/base-handler.js +5 -3
  24. package/cjs/services/swap-service/handler/hydradx-handler.js +1 -1
  25. package/core/logic-validation/earning.d.ts +1 -1
  26. package/core/logic-validation/earning.js +2 -2
  27. package/core/logic-validation/transfer.js +0 -7
  28. package/core/substrate/nominationpools-pallet.d.ts +4 -1
  29. package/core/substrate/nominationpools-pallet.js +8 -1
  30. package/core/substrate/system-pallet.js +2 -3
  31. package/core/substrate/xcm-parser.d.ts +49 -0
  32. package/core/substrate/xcm-parser.js +181 -0
  33. package/koni/api/dotsama/transfer.d.ts +0 -3
  34. package/koni/api/dotsama/transfer.js +12 -119
  35. package/koni/api/xcm/polkadotXcm.js +14 -22
  36. package/koni/api/xcm/utils.d.ts +3 -48
  37. package/koni/api/xcm/utils.js +5 -114
  38. package/koni/api/xcm/xTokens.js +12 -10
  39. package/koni/api/xcm/xcmPallet.js +7 -7
  40. package/koni/background/handlers/Extension.d.ts +1 -3
  41. package/koni/background/handlers/Extension.js +8 -28
  42. package/package.json +11 -6
  43. package/packageInfo.js +1 -1
  44. package/services/balance-service/helpers/subscribe/substrate/index.js +8 -5
  45. package/services/chain-service/constants.js +2 -2
  46. package/services/chain-service/utils/index.d.ts +3 -2
  47. package/services/chain-service/utils/index.js +5 -2
  48. package/services/earning-service/handlers/special.js +3 -3
  49. package/services/migration-service/scripts/MigrateTransactionHistoryBySymbol.js +2 -1
  50. package/services/migration-service/scripts/databases/MigrateAssetSetting.js +2 -1
  51. package/services/migration-service/scripts/index.js +2 -2
  52. package/services/swap-service/handler/base-handler.js +5 -3
  53. package/services/swap-service/handler/hydradx-handler.js +1 -1
@@ -54,6 +54,7 @@ var _exportNames = {
54
54
  _getXcmAssetId: true,
55
55
  _getXcmAssetMultilocation: true,
56
56
  _getXcmTransferType: true,
57
+ _isRelayChain: true,
57
58
  _isSubstrateRelayChain: true,
58
59
  _isSubstrateParaChain: true,
59
60
  _getEvmAbiExplorer: true,
@@ -145,6 +146,7 @@ exports._isNativeTokenBySlug = _isNativeTokenBySlug;
145
146
  exports._isNativeTokenTransferredByEvm = _isNativeTokenTransferredByEvm;
146
147
  exports._isPureEvmChain = _isPureEvmChain;
147
148
  exports._isPureSubstrateChain = _isPureSubstrateChain;
149
+ exports._isRelayChain = _isRelayChain;
148
150
  exports._isSmartContractToken = _isSmartContractToken;
149
151
  exports._isSubstrateChain = _isSubstrateChain;
150
152
  exports._isSubstrateParaChain = _isSubstrateParaChain;
@@ -305,7 +307,7 @@ function _isSubstrateChain(chainInfo) {
305
307
 
306
308
  function _getEvmChainId(chainInfo) {
307
309
  var _chainInfo$evmInfo;
308
- return ((_chainInfo$evmInfo = chainInfo.evmInfo) === null || _chainInfo$evmInfo === void 0 ? void 0 : _chainInfo$evmInfo.evmChainId) || 1; // fallback to Ethereum
310
+ return (_chainInfo$evmInfo = chainInfo.evmInfo) === null || _chainInfo$evmInfo === void 0 ? void 0 : _chainInfo$evmInfo.evmChainId; // fallback to Ethereum
309
311
  }
310
312
 
311
313
  function _getSubstrateParaId(chainInfo) {
@@ -314,7 +316,7 @@ function _getSubstrateParaId(chainInfo) {
314
316
  }
315
317
  function _getSubstrateRelayParent(chainInfo) {
316
318
  var _chainInfo$substrateI2;
317
- return ((_chainInfo$substrateI2 = chainInfo.substrateInfo) === null || _chainInfo$substrateI2 === void 0 ? void 0 : _chainInfo$substrateI2.relaySlug) || '';
319
+ return (_chainInfo$substrateI2 = chainInfo.substrateInfo) === null || _chainInfo$substrateI2 === void 0 ? void 0 : _chainInfo$substrateI2.relaySlug;
318
320
  }
319
321
  function _getSubstrateGenesisHash(chainInfo) {
320
322
  var _chainInfo$substrateI3;
@@ -467,6 +469,9 @@ function _getXcmTransferType(originChainInfo, destinationChainInfo) {
467
469
  var _originChainInfo$subs, _destinationChainInfo;
468
470
  return `${((_originChainInfo$subs = originChainInfo.substrateInfo) === null || _originChainInfo$subs === void 0 ? void 0 : _originChainInfo$subs.chainType) || ''}-${((_destinationChainInfo = destinationChainInfo.substrateInfo) === null || _destinationChainInfo === void 0 ? void 0 : _destinationChainInfo.chainType) || ''}`;
469
471
  }
472
+ function _isRelayChain(chainInfo) {
473
+ return _isSubstrateRelayChain(chainInfo) || _isPureEvmChain(chainInfo);
474
+ }
470
475
  function _isSubstrateRelayChain(chainInfo) {
471
476
  var _chainInfo$substrateI14;
472
477
  return ((_chainInfo$substrateI14 = chainInfo.substrateInfo) === null || _chainInfo$substrateI14 === void 0 ? void 0 : _chainInfo$substrateI14.chainType) === _types._SubstrateChainType.RELAYCHAIN;
@@ -284,11 +284,11 @@ class BaseSpecialStakingPoolHandler extends _base.default {
284
284
  const xcmFee = new _util.BN(path.totalFee[1].amount || '0');
285
285
  const xcmAmount = missingAmount.add(xcmFee);
286
286
  const bnAltInputTokenBalance = new _util.BN(altInputTokenBalance.value || '0');
287
- if (!bnAltInputTokenBalance.sub(xcmAmount).gt(_util.BN_ZERO)) {
287
+ if (!bnAltInputTokenBalance.sub(xcmAmount).sub(xcmFee).gt(_util.BN_ZERO)) {
288
288
  processValidation.failedStep = path.steps[1];
289
289
  processValidation.ok = false;
290
290
  processValidation.status = _types.YieldValidationStatus.NOT_ENOUGH_BALANCE;
291
- const maxBn = bnInputTokenBalance.add(new _util.BN(altInputTokenBalance.value)).sub(xcmFee);
291
+ const maxBn = bnInputTokenBalance.add(new _util.BN(altInputTokenBalance.value)).sub(xcmFee).sub(xcmFee);
292
292
  const maxValue = (0, _utils3.formatNumber)(maxBn.toString(), inputTokenInfo.decimals || 0);
293
293
  const altInputTokenInfo = this.state.getAssetBySlug(altInputTokenSlug);
294
294
  const symbol = altInputTokenInfo.symbol;
@@ -296,7 +296,7 @@ class BaseSpecialStakingPoolHandler extends _base.default {
296
296
  const inputNetworkName = this.chainInfo.name;
297
297
  const altNetworkName = altNetwork.name;
298
298
  const currentValue = (0, _utils3.formatNumber)(bnInputTokenBalance.toString(), inputTokenInfo.decimals || 0);
299
- const bnMaxXCM = new _util.BN(altInputTokenBalance.value).sub(xcmFee);
299
+ const bnMaxXCM = new _util.BN(altInputTokenBalance.value).sub(xcmFee).sub(xcmFee);
300
300
  const maxXCMValue = (0, _utils3.formatNumber)(bnMaxXCM.toString(), inputTokenInfo.decimals || 0);
301
301
  processValidation.message = (0, _i18next.t)('You can only enter a maximum of {{maxValue}} {{symbol}}, which is {{currentValue}} {{symbol}} ({{inputNetworkName}}) and {{maxXCMValue}} {{symbol}} ({{altNetworkName}}). Lower your amount and try again.', {
302
302
  replace: {
@@ -14,7 +14,8 @@ class MigrateTransactionHistoryBySymbol extends _Base.default {
14
14
  const state = this.state;
15
15
  try {
16
16
  const changeSlugsMap = {
17
- 'commune-NATIVE-C': 'commune-NATIVE-COMAI'
17
+ 'bobMainnet-LOCAL-wBTC-0x03C7054BCB39f7b2e5B2c7AcB37583e32D70Cfa3': 'bobMainnet-LOCAL-WBTC-0x03C7054BCB39f7b2e5B2c7AcB37583e32D70Cfa3',
18
+ 'hydradx_main-LOCAL-LRNA': 'hydradx_main-LOCAL-H2O'
18
19
  };
19
20
  const allTxs = [];
20
21
  await Promise.all(Object.entries(changeSlugsMap).map(async (_ref, i) => {
@@ -13,7 +13,8 @@ class MigrateAssetSetting extends _Base.default {
13
13
  async run() {
14
14
  try {
15
15
  const changeSlugsMap = {
16
- 'commune-NATIVE-C': 'commune-NATIVE-COMAI'
16
+ 'bobMainnet-LOCAL-wBTC-0x03C7054BCB39f7b2e5B2c7AcB37583e32D70Cfa3': 'bobMainnet-LOCAL-WBTC-0x03C7054BCB39f7b2e5B2c7AcB37583e32D70Cfa3',
17
+ 'hydradx_main-LOCAL-LRNA': 'hydradx_main-LOCAL-H2O'
17
18
  };
18
19
  const assetSetting = await this.state.chainService.getAssetSettings();
19
20
  const migratedAssetSetting = {};
@@ -57,8 +57,8 @@ var _default = {
57
57
  '1.1.28-01': _MigrateEarningVersion.default,
58
58
  '1.1.41-01': _DeleteChainStaking.default,
59
59
  '1.1.46-01': _AutoEnableSomeTokens.default,
60
- '1.1.62-01': _MigrateAssetSetting.default,
61
- '1.1.62-02': _MigrateTransactionHistoryBySymbol.default,
60
+ '1.1.69-03': _MigrateAssetSetting.default,
61
+ '1.1.69-02': _MigrateTransactionHistoryBySymbol.default,
62
62
  '1.2.69-01': _MigrateRemoveGenesisHash.default
63
63
  // [`${EVERYTIME}-1.1.42-02`]: MigrateTransactionHistoryBySymbol
64
64
  // [`${EVERYTIME}-1`]: AutoEnableChainsTokens
@@ -68,11 +68,13 @@ class SwapBaseHandler {
68
68
  const xcmFeeComponent = params.process.totalFee[stepIndex].feeComponent[0]; // todo: can do better than indexing
69
69
  const xcmFee = new _bignumber.default(xcmFeeComponent.amount || '0');
70
70
  let xcmAmount = bnAmount.minus(bnFromAssetBalance);
71
+ let editedXcmFee = new _bignumber.default(0);
71
72
  if ((0, _utils._isNativeToken)(alternativeAsset)) {
72
73
  xcmAmount = xcmAmount.plus(xcmFee);
74
+ editedXcmFee = xcmFee.times(2);
73
75
  }
74
- if (!bnAlternativeAssetBalance.minus(xcmAmount).gt(0)) {
75
- const maxBn = bnFromAssetBalance.plus(new _bignumber.default(alternativeAssetBalance.value)).minus(xcmFee);
76
+ if (!bnAlternativeAssetBalance.minus((0, _utils._isNativeToken)(alternativeAsset) ? xcmAmount.plus(xcmFee) : xcmFee).gt(0)) {
77
+ const maxBn = bnFromAssetBalance.plus(new _bignumber.default(alternativeAssetBalance.value)).minus((0, _utils._isNativeToken)(alternativeAsset) ? editedXcmFee : xcmFee);
76
78
  const maxValue = (0, _utils3.formatNumber)(maxBn.toString(), fromAsset.decimals || 0);
77
79
  const altInputTokenInfo = this.chainService.getAssetBySlug(alternativeAssetSlug);
78
80
  const symbol = altInputTokenInfo.symbol;
@@ -81,7 +83,7 @@ class SwapBaseHandler {
81
83
  const inputNetworkName = chain.name;
82
84
  const altNetworkName = alternativeChain.name;
83
85
  const currentValue = (0, _utils3.formatNumber)(bnFromAssetBalance.toString(), fromAsset.decimals || 0);
84
- const bnMaxXCM = new _bignumber.default(alternativeAssetBalance.value).minus(xcmFee);
86
+ const bnMaxXCM = new _bignumber.default(alternativeAssetBalance.value).minus((0, _utils._isNativeToken)(alternativeAsset) ? editedXcmFee : xcmFee);
85
87
  const maxXCMValue = (0, _utils3.formatNumber)(bnMaxXCM.toString(), fromAsset.decimals || 0);
86
88
  if (maxBn.lte(0) || bnFromAssetBalance.lte(0) || bnMaxXCM.lte(0)) {
87
89
  return [new _TransactionError.TransactionError(_KoniTypes.BasicTxErrorType.NOT_ENOUGH_BALANCE, (0, _i18next.t)(`Insufficient balance. Deposit ${fromAsset.symbol} and try again.`))];
@@ -34,7 +34,7 @@ class HydradxHandler {
34
34
  this.swapBaseHandler = new _baseHandler.SwapBaseHandler({
35
35
  balanceService,
36
36
  chainService,
37
- providerName: isTestnet ? 'HydraDX Testnet' : 'HydraDX',
37
+ providerName: isTestnet ? 'Hydration Testnet' : 'Hydration',
38
38
  providerSlug: isTestnet ? _swap2.SwapProviderId.HYDRADX_TESTNET : _swap2.SwapProviderId.HYDRADX_MAINNET
39
39
  });
40
40
  this.providerSlug = isTestnet ? _swap2.SwapProviderId.HYDRADX_TESTNET : _swap2.SwapProviderId.HYDRADX_MAINNET;
@@ -4,7 +4,7 @@ export declare function _handleDisplayForEarningError(error: TransactionError):
4
4
  message: string;
5
5
  } | undefined;
6
6
  export declare const INSUFFICIENT_MESSAGES: string[];
7
- export declare function _handleDisplayInsufficientEarningError(error: Error, availableBalance: string, amount: string, minJoinPool: string): {
7
+ export declare function _handleDisplayInsufficientEarningError(error: Error, isXCM: boolean, availableBalance: string, amount: string, minJoinPool: string): {
8
8
  title: string;
9
9
  message: string;
10
10
  } | undefined;
@@ -12,8 +12,8 @@ export function _handleDisplayForEarningError(error) {
12
12
  return undefined;
13
13
  }
14
14
  export const INSUFFICIENT_MESSAGES = ['残高不足', 'Недостаточный баланс', 'Insufficient balance'];
15
- export function _handleDisplayInsufficientEarningError(error, availableBalance, amount, minJoinPool) {
16
- if (!INSUFFICIENT_MESSAGES.some(v => error.message.includes(v))) {
15
+ export function _handleDisplayInsufficientEarningError(error, isXCM, availableBalance, amount, minJoinPool) {
16
+ if (isXCM || !INSUFFICIENT_MESSAGES.some(v => error.message.includes(v))) {
17
17
  return undefined;
18
18
  }
19
19
  const bnAvailableBalance = new BigN(availableBalance);
@@ -170,13 +170,6 @@ export function checkBalanceWithTransactionFee(validationResponse, transactionIn
170
170
  // todo: estimateFee should be must-have, need to refactor interface
171
171
  return;
172
172
  }
173
-
174
- // if (!nativeTokenAvailable.metadata) {
175
- // validationResponse.errors.push(new TransactionError(BasicTxErrorType.INTERNAL_ERROR));
176
- //
177
- // return;
178
- // }
179
-
180
173
  const {
181
174
  edAsWarning,
182
175
  extrinsicType,
@@ -1,7 +1,10 @@
1
+ import BigN from 'bignumber.js';
1
2
  export declare type PalletNominationPoolsPoolMember = {
2
3
  poolId: number;
3
4
  points: number;
4
5
  lastRecordedRewardCounter: number;
5
6
  unbondingEras: Record<string, number>;
6
7
  };
7
- export declare function _getActiveStakeInNominationPool(memberInfo: PalletNominationPoolsPoolMember): string;
8
+ export declare function _getActiveStakeInNominationPool(memberInfo: PalletNominationPoolsPoolMember): BigN;
9
+ export declare function _getUnbondingStakeInNominationPool(memberInfo: PalletNominationPoolsPoolMember): BigN;
10
+ export declare function _getTotalStakeInNominationPool(memberInfo: PalletNominationPoolsPoolMember): BigN;
@@ -1,6 +1,13 @@
1
1
  // Copyright 2019-2022 @subwallet/extension-base
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
+ import BigN from 'bignumber.js';
4
5
  export function _getActiveStakeInNominationPool(memberInfo) {
5
- return memberInfo.points.toString();
6
+ return new BigN(memberInfo.points.toString());
7
+ }
8
+ export function _getUnbondingStakeInNominationPool(memberInfo) {
9
+ return new BigN(Object.values(memberInfo.unbondingEras).reduce((a, b) => a + b, 0));
10
+ }
11
+ export function _getTotalStakeInNominationPool(memberInfo) {
12
+ return _getActiveStakeInNominationPool(memberInfo).plus(_getUnbondingStakeInNominationPool(memberInfo));
6
13
  }
@@ -62,10 +62,9 @@ function _getSystemPalletTotalBalanceV2(accountInfo) {
62
62
  function _getSystemPalletTransferableV1(accountInfo, existentialDeposit, strictMode) {
63
63
  const bnAppliedExistentialDeposit = new BigN(_getAppliedExistentialDeposit(accountInfo, existentialDeposit, strictMode));
64
64
  const bnAppliedFrozen = BigN.max(accountInfo.data.feeFrozen, accountInfo.data.miscFrozen);
65
- const bnTotalBalance = new BigN(_getSystemPalletTotalBalanceV1(accountInfo));
66
- const bnTransferableBalance = bnTotalBalance.minus(BigN.max(bnAppliedFrozen, accountInfo.data.reserved, bnAppliedExistentialDeposit));
65
+ const bnTransferableBalance = new BigN(accountInfo.data.free).minus(BigN.max(bnAppliedFrozen, bnAppliedExistentialDeposit));
67
66
  return BigN.max(bnTransferableBalance, 0).toFixed();
68
67
  }
69
68
  function _getSystemPalletTotalBalanceV1(accountInfo) {
70
- return new BigN(accountInfo.data.free).toFixed();
69
+ return new BigN(accountInfo.data.free).plus(accountInfo.data.reserved).toFixed();
71
70
  }
@@ -0,0 +1,49 @@
1
+ import { _ChainAsset, _ChainInfo } from '@subwallet/chain-list/types';
2
+ import { _Address } from '@subwallet/extension-base/background/KoniTypes';
3
+ export declare function _getXcmDestWeight(originChainInfo: _ChainInfo): "Unlimited" | 5000000000;
4
+ export declare function _getXcmBeneficiary(destChainInfo: _ChainInfo, recipient: _Address, version: number): {
5
+ [x: string]: {
6
+ parents: number;
7
+ interior: {
8
+ X1: {
9
+ AccountId32: {
10
+ network: string | undefined;
11
+ id: Uint8Array;
12
+ };
13
+ AccountKey20?: undefined;
14
+ } | {
15
+ AccountKey20: {
16
+ network: string | undefined;
17
+ key: string;
18
+ };
19
+ AccountId32?: undefined;
20
+ } | ({
21
+ AccountId32: {
22
+ network: string | undefined;
23
+ id: Uint8Array;
24
+ };
25
+ AccountKey20?: undefined;
26
+ } | {
27
+ AccountKey20: {
28
+ network: string | undefined;
29
+ key: string;
30
+ };
31
+ AccountId32?: undefined;
32
+ })[];
33
+ };
34
+ };
35
+ };
36
+ export declare function _getXcmMultiAssets(tokenInfo: _ChainAsset, value: string, version: number): {
37
+ [x: string]: {
38
+ id: Record<string, any>;
39
+ fun: {
40
+ Fungible: string;
41
+ };
42
+ }[];
43
+ };
44
+ export declare function _getXcmMultiLocation(originChainInfo: _ChainInfo, destChainInfo: _ChainInfo, version: number, recipient?: _Address): {
45
+ [x: string]: {
46
+ parents: number;
47
+ interior: unknown;
48
+ };
49
+ };
@@ -0,0 +1,181 @@
1
+ // Copyright 2019-2022 @subwallet/extension-base
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { COMMON_CHAIN_SLUGS } from '@subwallet/chain-list';
5
+ import { _getChainSubstrateAddressPrefix, _getEvmChainId, _getSubstrateParaId, _getSubstrateRelayParent, _getXcmAssetMultilocation, _isChainEvmCompatible, _isSubstrateParaChain } from '@subwallet/extension-base/services/chain-service/utils';
6
+ import { decodeAddress, evmToAddress } from '@polkadot/util-crypto';
7
+ const FOUR_INSTRUCTIONS_WEIGHT = 5000000000;
8
+ const UNLIMITED_WEIGHT = 'Unlimited';
9
+ export function _getXcmDestWeight(originChainInfo) {
10
+ if (['pioneer'].includes(originChainInfo.slug)) {
11
+ return FOUR_INSTRUCTIONS_WEIGHT;
12
+ }
13
+ return UNLIMITED_WEIGHT;
14
+ }
15
+ export function _getXcmBeneficiary(destChainInfo, recipient, version) {
16
+ const receiverLocation = version < 4 // from V4, X1 is also an array
17
+ ? _getRecipientLocation(destChainInfo, recipient, version) : [_getRecipientLocation(destChainInfo, recipient, version)];
18
+ return {
19
+ [`V${version}`]: {
20
+ parents: 0,
21
+ interior: {
22
+ X1: receiverLocation
23
+ }
24
+ }
25
+ };
26
+ }
27
+ export function _getXcmMultiAssets(tokenInfo, value, version) {
28
+ const assetId = _getAssetIdentifier(tokenInfo, version);
29
+ return {
30
+ [`V${version}`]: [{
31
+ id: assetId,
32
+ fun: {
33
+ Fungible: value
34
+ }
35
+ }]
36
+ };
37
+ }
38
+ export function _getXcmMultiLocation(originChainInfo, destChainInfo, version, recipient) {
39
+ const isWithinSameConsensus = _isXcmWithinSameConsensus(originChainInfo, destChainInfo);
40
+ const parents = _getMultiLocationParent(originChainInfo, isWithinSameConsensus);
41
+ const interior = _getMultiLocationInterior(destChainInfo, isWithinSameConsensus, version, recipient);
42
+ return {
43
+ [`V${version}`]: {
44
+ parents,
45
+ interior
46
+ }
47
+ };
48
+ }
49
+
50
+ // ---------------------------------------------------------------------------------------------------------------------
51
+
52
+ function _isXcmWithinSameConsensus(originChainInfo, destChainInfo) {
53
+ return _getSubstrateRelayParent(originChainInfo) === destChainInfo.slug || _getSubstrateRelayParent(destChainInfo) === originChainInfo.slug || _getSubstrateRelayParent(originChainInfo) === _getSubstrateRelayParent(destChainInfo);
54
+ }
55
+ function _getMultiLocationParent(originChainInfo, isWithinSameConsensus) {
56
+ let parent = 0; // how many hops up the hierarchy
57
+
58
+ if (_isSubstrateParaChain(originChainInfo)) {
59
+ parent += 1;
60
+ }
61
+ if (!isWithinSameConsensus) {
62
+ parent += 1;
63
+ }
64
+ return parent;
65
+ }
66
+ function _getMultiLocationInterior(destChainInfo, isWithinSameConsensus, version, recipient) {
67
+ const junctions = [];
68
+ if (isWithinSameConsensus) {
69
+ if (_isSubstrateParaChain(destChainInfo)) {
70
+ junctions.push({
71
+ Parachain: _getSubstrateParaId(destChainInfo)
72
+ });
73
+ }
74
+ } else {
75
+ junctions.push({
76
+ GlobalConsensus: _getGlobalConsensusJunction(destChainInfo, version)
77
+ });
78
+ if (_isSubstrateParaChain(destChainInfo)) {
79
+ junctions.push({
80
+ Parachain: _getSubstrateParaId(destChainInfo)
81
+ });
82
+ }
83
+ }
84
+ if (recipient) {
85
+ junctions.push(_getRecipientLocation(destChainInfo, recipient, version));
86
+ }
87
+ if (junctions.length === 0 && !recipient) {
88
+ return 'Here';
89
+ }
90
+ if (version < 4 && junctions.length === 1) {
91
+ return {
92
+ X1: junctions[0]
93
+ };
94
+ }
95
+ return {
96
+ [`X${junctions.length}`]: junctions
97
+ };
98
+ }
99
+ function _getGlobalConsensusJunction(destChainInfo, version) {
100
+ let chainSlug = destChainInfo.slug;
101
+ let evmChainId;
102
+ if (_isSubstrateParaChain(destChainInfo)) {
103
+ const relaySlug = _getSubstrateRelayParent(destChainInfo);
104
+ if (!relaySlug) {
105
+ throw Error('Parachain must have a parent chainSlug');
106
+ }
107
+ chainSlug = relaySlug;
108
+ } else {
109
+ evmChainId = _getEvmChainId(destChainInfo);
110
+ }
111
+ if (evmChainId) {
112
+ return {
113
+ Ethereum: {
114
+ chainId: evmChainId
115
+ }
116
+ };
117
+ }
118
+ switch (chainSlug) {
119
+ case COMMON_CHAIN_SLUGS.POLKADOT:
120
+ return version < 4 ? {
121
+ Polkadot: null
122
+ } : 'Polkadot';
123
+ case COMMON_CHAIN_SLUGS.KUSAMA:
124
+ return version < 4 ? {
125
+ Kusama: null
126
+ } : 'Kusama';
127
+ default:
128
+ return version < 4 ? {
129
+ Rococo: null
130
+ } : 'Rococo';
131
+ }
132
+ }
133
+ function _getRecipientLocation(destChainInfo, recipient, version) {
134
+ const network = _getNetworkByVersion(version);
135
+ if (destChainInfo.slug === COMMON_CHAIN_SLUGS.ASTAR_EVM) {
136
+ const ss58Address = evmToAddress(recipient, _getChainSubstrateAddressPrefix(destChainInfo)); // TODO: shouldn't pass addressPrefix directly
137
+
138
+ return {
139
+ AccountId32: {
140
+ network,
141
+ id: decodeAddress(ss58Address)
142
+ }
143
+ };
144
+ }
145
+ if (_isChainEvmCompatible(destChainInfo)) {
146
+ return {
147
+ AccountKey20: {
148
+ network,
149
+ key: recipient
150
+ }
151
+ };
152
+ }
153
+ return {
154
+ AccountId32: {
155
+ network,
156
+ id: decodeAddress(recipient)
157
+ }
158
+ };
159
+ }
160
+ function _getAssetIdentifier(tokenInfo, version) {
161
+ const assetIdentifier = _getXcmAssetMultilocation(tokenInfo);
162
+ if (!assetIdentifier) {
163
+ throw new Error('Asset must have multilocation');
164
+ }
165
+ return version >= 4 // from V4, Concrete is removed
166
+ ? assetIdentifier : {
167
+ Concrete: assetIdentifier
168
+ };
169
+ }
170
+ function _getNetworkByVersion(version) {
171
+ switch (version) {
172
+ case 1:
173
+ case 2:
174
+ return 'Any';
175
+ case 3:
176
+ case 4:
177
+ return undefined;
178
+ default:
179
+ return undefined;
180
+ }
181
+ }
@@ -1,10 +1,7 @@
1
1
  import { _ChainAsset, _ChainInfo } from '@subwallet/chain-list/types';
2
- import { SupportTransferResponse } from '@subwallet/extension-base/background/KoniTypes';
3
2
  import { _EvmApi, _SubstrateApi } from '@subwallet/extension-base/services/chain-service/types';
4
3
  import BigN from 'bignumber.js';
5
4
  import { SubmittableExtrinsic } from '@polkadot/api/promise/types';
6
- export declare function checkReferenceCount(networkKey: string, address: string, substrateApiMap: Record<string, _SubstrateApi>, chainInfo: _ChainInfo): Promise<boolean>;
7
- export declare function checkSupportTransfer(networkKey: string, tokenInfo: _ChainAsset, substrateApiMap: Record<string, _SubstrateApi>, chainInfo: _ChainInfo): Promise<SupportTransferResponse>;
8
5
  interface CreateTransferExtrinsicProps {
9
6
  substrateApi: _SubstrateApi;
10
7
  networkKey: string;
@@ -4,112 +4,13 @@
4
4
  import { GearApi } from '@gear-js/api';
5
5
  import { getPSP22ContractPromise } from '@subwallet/extension-base/koni/api/tokens/wasm';
6
6
  import { getWasmContractGasLimit } from '@subwallet/extension-base/koni/api/tokens/wasm/utils';
7
- import { _BALANCE_TOKEN_GROUP, _MANTA_ZK_CHAIN_GROUP, _TRANSFER_CHAIN_GROUP, _TRANSFER_NOT_SUPPORTED_CHAINS, _ZK_ASSET_PREFIX } from '@subwallet/extension-base/services/chain-service/constants';
8
- import { _getContractAddressOfToken, _getTokenOnChainAssetId, _getTokenOnChainInfo, _isChainEvmCompatible, _isNativeToken, _isTokenGearSmartContract, _isTokenTransferredByEvm, _isTokenWasmSmartContract } from '@subwallet/extension-base/services/chain-service/utils';
7
+ import { _TRANSFER_CHAIN_GROUP } from '@subwallet/extension-base/services/chain-service/constants';
8
+ import { _getContractAddressOfToken, _getTokenOnChainAssetId, _getTokenOnChainInfo, _isBridgedToken, _isChainEvmCompatible, _isNativeToken, _isTokenGearSmartContract, _isTokenTransferredByEvm, _isTokenWasmSmartContract } from '@subwallet/extension-base/services/chain-service/utils';
9
9
  import { calculateGasFeeParams } from '@subwallet/extension-base/services/fee-service/utils';
10
10
  import { getGRC20ContractPromise } from '@subwallet/extension-base/utils';
11
11
  import BigN from 'bignumber.js';
12
12
  import { BN, u8aToHex } from '@polkadot/util';
13
13
  import { decodeAddress } from '@polkadot/util-crypto';
14
- function isRefCount(accountInfo) {
15
- return !!accountInfo.refcount;
16
- }
17
- export async function checkReferenceCount(networkKey, address, substrateApiMap, chainInfo) {
18
- const apiProps = await substrateApiMap[networkKey].isReady;
19
- const api = apiProps.api;
20
- if (_isChainEvmCompatible(chainInfo)) {
21
- return false;
22
- }
23
-
24
- // @ts-ignore
25
- const accountInfo = await api.query.system.account(address);
26
- return accountInfo ? isRefCount(accountInfo) ? !accountInfo.refcount.isZero() : !accountInfo.consumers.isZero() : false;
27
- }
28
- export async function checkSupportTransfer(networkKey, tokenInfo, substrateApiMap, chainInfo) {
29
- const substrateApi = await substrateApiMap[networkKey].isReady;
30
- if (!tokenInfo) {
31
- return {
32
- supportTransfer: false,
33
- supportTransferAll: false
34
- };
35
- }
36
- if (_isChainEvmCompatible(chainInfo)) {
37
- return {
38
- supportTransfer: true,
39
- supportTransferAll: true
40
- };
41
- }
42
- if (tokenInfo.symbol.startsWith(_ZK_ASSET_PREFIX) && _MANTA_ZK_CHAIN_GROUP.includes(tokenInfo.originChain)) {
43
- return {
44
- supportTransfer: false,
45
- supportTransferAll: false
46
- };
47
- }
48
- if (_TRANSFER_NOT_SUPPORTED_CHAINS.includes(networkKey)) {
49
- return {
50
- supportTransfer: false,
51
- supportTransferAll: false
52
- };
53
- }
54
- const api = substrateApi.api;
55
- const isTxCurrenciesSupported = !!api && !!api.tx && !!api.tx.currencies;
56
- const isTxBalancesSupported = !!api && !!api.tx && !!api.tx.balances;
57
- const isTxTokensSupported = !!api && !!api.tx && !!api.tx.tokens;
58
- const isTxEqBalancesSupported = !!api && !!api.tx && !!api.tx.eqBalances;
59
- const result = {
60
- supportTransfer: false,
61
- supportTransferAll: false
62
- };
63
- if (!(isTxCurrenciesSupported || isTxBalancesSupported || isTxTokensSupported || isTxEqBalancesSupported)) {
64
- return result;
65
- }
66
- if (_isTokenWasmSmartContract(tokenInfo) && api.query.contracts) {
67
- // for PSP tokens
68
- return {
69
- supportTransfer: true,
70
- supportTransferAll: true
71
- };
72
- }
73
-
74
- // TODO: need review
75
- if (_TRANSFER_CHAIN_GROUP.acala.includes(networkKey) && !_isNativeToken(tokenInfo) && isTxCurrenciesSupported) {
76
- result.supportTransfer = true;
77
- result.supportTransferAll = true;
78
- } else if (_TRANSFER_CHAIN_GROUP.kintsugi.includes(networkKey) && !_isNativeToken(tokenInfo) && isTxTokensSupported) {
79
- result.supportTransfer = true;
80
- result.supportTransferAll = true;
81
- } else if (_TRANSFER_CHAIN_GROUP.genshiro.includes(networkKey)
82
- // && !_isNativeToken(tokenInfo) && isTxEqBalancesSupported
83
- ) {
84
- result.supportTransfer = false;
85
- result.supportTransferAll = false;
86
- // } else if (_TRANSFER_CHAIN_GROUP.crab.includes(networkKey) && _BALANCE_TOKEN_GROUP.crab.includes(tokenInfo.symbol)) {
87
- // result.supportTransfer = true;
88
- // result.supportTransferAll = true;
89
- } else if (isTxBalancesSupported && _isNativeToken(tokenInfo)) {
90
- result.supportTransfer = true;
91
- result.supportTransferAll = true;
92
- } else if (_TRANSFER_CHAIN_GROUP.bitcountry.includes(networkKey) && !_isNativeToken(tokenInfo) && _BALANCE_TOKEN_GROUP.bitcountry.includes(tokenInfo.symbol)) {
93
- result.supportTransfer = true;
94
- result.supportTransferAll = true;
95
- } else if (_TRANSFER_CHAIN_GROUP.statemine.includes(networkKey) && !_isNativeToken(tokenInfo)) {
96
- result.supportTransfer = true;
97
- result.supportTransferAll = true;
98
- } else if (_TRANSFER_CHAIN_GROUP.sora_substrate.includes(networkKey)) {
99
- result.supportTransfer = true;
100
- result.supportTransferAll = true;
101
- // } else if (_TRANSFER_CHAIN_GROUP.riochain.includes(networkKey) && _isNativeToken(tokenInfo)) {
102
- // result.supportTransfer = true;
103
- // result.supportTransferAll = true;
104
- } else if (_TRANSFER_CHAIN_GROUP.avail.includes(networkKey)) {
105
- result.supportTransfer = true;
106
- result.supportTransferAll = true;
107
- } else if (_TRANSFER_CHAIN_GROUP.centrifuge.includes(networkKey)) {
108
- result.supportTransfer = true;
109
- result.supportTransferAll = true;
110
- }
111
- return result;
112
- }
113
14
  export const createTransferExtrinsic = async ({
114
15
  from,
115
16
  networkKey,
@@ -131,11 +32,17 @@ export const createTransferExtrinsic = async ({
131
32
  const isTxCurrenciesSupported = !!api && !!api.tx && !!api.tx.currencies;
132
33
  const isTxBalancesSupported = !!api && !!api.tx && !!api.tx.balances;
133
34
  const isTxTokensSupported = !!api && !!api.tx && !!api.tx.tokens;
134
- // const isTxEqBalancesSupported = !!api && !!api.tx && !!api.tx.eqBalances;
135
35
  const isTxAssetsSupported = !!api && !!api.tx && !!api.tx.assets;
136
36
  let transferAmount; // for PSP-22 tokens, might be deprecated in the future
137
37
 
138
- if (_isTokenWasmSmartContract(tokenInfo) && api.query.contracts) {
38
+ if (_isBridgedToken(tokenInfo) && api.tx.foreignAssets) {
39
+ const onChainInfo = _getTokenOnChainInfo(tokenInfo);
40
+ if (transferAll) {
41
+ transfer = api.tx.foreignAssets.transfer(onChainInfo, to, value);
42
+ } else {
43
+ transfer = api.tx.foreignAssets.transferKeepAlive(onChainInfo, to, value);
44
+ }
45
+ } else if (_isTokenWasmSmartContract(tokenInfo) && api.query.contracts) {
139
46
  const contractPromise = getPSP22ContractPromise(api, _getContractAddressOfToken(tokenInfo));
140
47
  // @ts-ignore
141
48
  const gasLimit = await getWasmContractGasLimit(api, from, 'psp22::transfer', contractPromise, {}, [from, value, {}]);
@@ -177,25 +84,10 @@ export const createTransferExtrinsic = async ({
177
84
  } else if (value) {
178
85
  transfer = api.tx.tokens.transfer(to, _getTokenOnChainInfo(tokenInfo) || _getTokenOnChainAssetId(tokenInfo), new BN(value));
179
86
  }
180
- } else if (_TRANSFER_CHAIN_GROUP.genshiro.includes(networkKey)
181
- // && isTxEqBalancesSupported
182
- ) {
183
- // transfer = api.tx.eqBalances.transfer(_getTokenOnChainAssetId(tokenInfo), to, value);
184
- /* empty */
185
- // } else if (!_isNativeToken(tokenInfo) && (_TRANSFER_CHAIN_GROUP.crab.includes(networkKey) || _BALANCE_TOKEN_GROUP.crab.includes(tokenInfo.symbol))) {
186
- // if (transferAll) {
187
- // transfer = api.tx.kton.transferAll(to, false);
188
- // } else if (value) {
189
- // transfer = api.tx.kton.transfer(to, new BN(value));
190
- // }
191
87
  } else if (_TRANSFER_CHAIN_GROUP.bitcountry.includes(networkKey) && !_isNativeToken(tokenInfo)) {
192
88
  transfer = api.tx.currencies.transfer(to, _getTokenOnChainInfo(tokenInfo), value);
193
89
  } else if (_TRANSFER_CHAIN_GROUP.statemine.includes(networkKey) && !_isNativeToken(tokenInfo)) {
194
90
  transfer = api.tx.assets.transfer(_getTokenOnChainAssetId(tokenInfo), to, value);
195
- // } else if (_TRANSFER_CHAIN_GROUP.riochain.includes(networkKey)) {
196
- // if (_isNativeToken(tokenInfo)) {
197
- // transfer = api.tx.currencies.transferNativeCurrency(to, value);
198
- // }
199
91
  } else if (_TRANSFER_CHAIN_GROUP.sora_substrate.includes(networkKey) && isTxAssetsSupported) {
200
92
  transfer = api.tx.assets.transfer(_getTokenOnChainAssetId(tokenInfo), to, value);
201
93
  } else if (isTxBalancesSupported && _isNativeToken(tokenInfo)) {
@@ -236,10 +128,11 @@ export const getTransferMockTxFee = async (address, chainInfo, tokenInfo, api) =
236
128
  } else {
237
129
  var _paymentInfo$partialF;
238
130
  const substrateApi = api;
131
+ const chainApi = await substrateApi.isReady;
239
132
  const [mockTx] = await createTransferExtrinsic({
240
133
  from: address,
241
134
  networkKey: chainInfo.slug,
242
- substrateApi,
135
+ substrateApi: chainApi,
243
136
  to: address,
244
137
  tokenInfo,
245
138
  transferAll: true,