@subwallet/extension-base 1.2.3-0 → 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 (94) hide show
  1. package/background/KoniTypes.d.ts +4 -153
  2. package/background/KoniTypes.js +0 -21
  3. package/cjs/background/KoniTypes.js +1 -22
  4. package/cjs/core/logic-validation/earning.js +47 -0
  5. package/cjs/core/logic-validation/swap.js +99 -0
  6. package/cjs/core/logic-validation/transfer.js +212 -0
  7. package/cjs/core/substrate/nominationpools-pallet.js +22 -0
  8. package/cjs/core/substrate/system-pallet.js +77 -0
  9. package/cjs/core/substrate/xcm-parser.js +190 -0
  10. package/cjs/koni/api/dotsama/transfer.js +58 -122
  11. package/cjs/koni/api/staking/bonding/utils.js +1 -1
  12. package/cjs/koni/api/xcm/index.js +30 -2
  13. package/cjs/koni/api/xcm/polkadotXcm.js +12 -20
  14. package/cjs/koni/api/xcm/utils.js +8 -126
  15. package/cjs/koni/api/xcm/xTokens.js +10 -8
  16. package/cjs/koni/api/xcm/xcmPallet.js +6 -6
  17. package/cjs/koni/background/handlers/Extension.js +223 -356
  18. package/cjs/packageInfo.js +1 -1
  19. package/cjs/services/balance-service/helpers/group.js +4 -27
  20. package/cjs/services/balance-service/helpers/subscribe/index.js +2 -30
  21. package/cjs/services/balance-service/helpers/subscribe/substrate/index.js +46 -82
  22. package/cjs/services/balance-service/index.js +11 -9
  23. package/cjs/services/chain-service/constants.js +2 -2
  24. package/cjs/services/chain-service/index.js +0 -1
  25. package/cjs/services/chain-service/utils/index.js +13 -2
  26. package/cjs/services/earning-service/handlers/base.js +1 -1
  27. package/cjs/services/earning-service/handlers/special.js +11 -12
  28. package/cjs/services/migration-service/scripts/MigrateTransactionHistoryBySymbol.js +2 -1
  29. package/cjs/services/migration-service/scripts/databases/MigrateAssetSetting.js +2 -1
  30. package/cjs/services/migration-service/scripts/index.js +2 -2
  31. package/cjs/services/swap-service/handler/base-handler.js +30 -44
  32. package/cjs/services/swap-service/handler/chainflip-handler.js +23 -21
  33. package/cjs/services/swap-service/handler/hydradx-handler.js +41 -39
  34. package/cjs/services/swap-service/index.js +6 -0
  35. package/cjs/services/swap-service/utils.js +8 -49
  36. package/cjs/services/transaction-service/index.js +66 -155
  37. package/core/logic-validation/earning.d.ts +10 -0
  38. package/core/logic-validation/earning.js +37 -0
  39. package/core/logic-validation/swap.d.ts +8 -0
  40. package/core/logic-validation/swap.js +89 -0
  41. package/core/logic-validation/transfer.d.ts +16 -0
  42. package/core/logic-validation/transfer.js +199 -0
  43. package/core/substrate/nominationpools-pallet.d.ts +10 -0
  44. package/core/substrate/nominationpools-pallet.js +13 -0
  45. package/core/substrate/system-pallet.d.ts +27 -0
  46. package/core/substrate/system-pallet.js +70 -0
  47. package/core/substrate/xcm-parser.d.ts +49 -0
  48. package/core/substrate/xcm-parser.js +181 -0
  49. package/koni/api/dotsama/transfer.d.ts +3 -4
  50. package/koni/api/dotsama/transfer.js +54 -118
  51. package/koni/api/staking/bonding/relayChain.d.ts +2 -1
  52. package/koni/api/staking/bonding/utils.js +1 -1
  53. package/koni/api/xcm/index.d.ts +2 -0
  54. package/koni/api/xcm/index.js +27 -1
  55. package/koni/api/xcm/polkadotXcm.js +14 -22
  56. package/koni/api/xcm/utils.d.ts +3 -48
  57. package/koni/api/xcm/utils.js +5 -114
  58. package/koni/api/xcm/xTokens.js +12 -10
  59. package/koni/api/xcm/xcmPallet.js +7 -7
  60. package/koni/background/handlers/Extension.d.ts +6 -8
  61. package/koni/background/handlers/Extension.js +117 -247
  62. package/package.json +39 -9
  63. package/packageInfo.js +1 -1
  64. package/services/balance-service/helpers/group.js +4 -27
  65. package/services/balance-service/helpers/subscribe/index.d.ts +2 -1
  66. package/services/balance-service/helpers/subscribe/index.js +2 -30
  67. package/services/balance-service/helpers/subscribe/substrate/index.d.ts +2 -1
  68. package/services/balance-service/helpers/subscribe/substrate/index.js +31 -66
  69. package/services/balance-service/index.d.ts +7 -6
  70. package/services/balance-service/index.js +12 -10
  71. package/services/chain-service/constants.js +2 -2
  72. package/services/chain-service/index.js +0 -1
  73. package/services/chain-service/utils/index.d.ts +4 -2
  74. package/services/chain-service/utils/index.js +9 -2
  75. package/services/earning-service/handlers/base.js +1 -1
  76. package/services/earning-service/handlers/nomination-pool/index.d.ts +2 -1
  77. package/services/earning-service/handlers/special.js +11 -12
  78. package/services/migration-service/scripts/MigrateTransactionHistoryBySymbol.js +2 -1
  79. package/services/migration-service/scripts/databases/MigrateAssetSetting.js +2 -1
  80. package/services/migration-service/scripts/index.js +2 -2
  81. package/services/swap-service/handler/base-handler.d.ts +3 -2
  82. package/services/swap-service/handler/base-handler.js +28 -42
  83. package/services/swap-service/handler/chainflip-handler.d.ts +2 -1
  84. package/services/swap-service/handler/chainflip-handler.js +4 -2
  85. package/services/swap-service/handler/hydradx-handler.d.ts +2 -1
  86. package/services/swap-service/handler/hydradx-handler.js +8 -6
  87. package/services/swap-service/index.js +7 -1
  88. package/services/swap-service/utils.d.ts +2 -4
  89. package/services/swap-service/utils.js +7 -47
  90. package/services/transaction-service/index.d.ts +1 -1
  91. package/services/transaction-service/index.js +30 -119
  92. package/services/transaction-service/types.d.ts +1 -0
  93. package/types/balance/index.d.ts +6 -10
  94. package/types/yield/info/pallet.d.ts +0 -6
@@ -3,29 +3,27 @@
3
3
 
4
4
  import { Common } from '@ethereumjs/common';
5
5
  import { LegacyTransaction } from '@ethereumjs/tx';
6
- import { _AssetType } from '@subwallet/chain-list/types';
7
6
  import { TransactionError } from '@subwallet/extension-base/background/errors/TransactionError';
8
7
  import { withErrorLog } from '@subwallet/extension-base/background/handlers/helpers';
9
8
  import { createSubscription } from '@subwallet/extension-base/background/handlers/subscriptions';
10
- import { AccountExternalErrorCode, BasicTxErrorType, BasicTxWarningCode, CampaignDataType, ChainType, ExternalRequestPromiseStatus, ExtrinsicType, MantaPayEnableMessage, StakingTxErrorType, StakingType, TransferTxErrorType } from '@subwallet/extension-base/background/KoniTypes';
11
- import { TransactionWarning } from '@subwallet/extension-base/background/warnings/TransactionWarning';
12
- import { ALL_ACCOUNT_KEY, ALL_GENESIS_HASH, LATEST_SESSION, XCM_FEE_RATIO, XCM_MIN_AMOUNT_RATIO } from '@subwallet/extension-base/constants';
9
+ import { AccountExternalErrorCode, BasicTxErrorType, CampaignDataType, ChainType, ExternalRequestPromiseStatus, ExtrinsicType, MantaPayEnableMessage, StakingTxErrorType, StakingType } from '@subwallet/extension-base/background/KoniTypes';
10
+ import { ALL_ACCOUNT_KEY, ALL_GENESIS_HASH, LATEST_SESSION, XCM_FEE_RATIO } from '@subwallet/extension-base/constants';
11
+ import { additionalValidateTransfer, additionalValidateXcmTransfer, validateTransferRequest, validateXcmTransferRequest } from '@subwallet/extension-base/core/logic-validation/transfer';
13
12
  import { ALLOWED_PATH } from '@subwallet/extension-base/defaults';
14
13
  import { resolveAzeroAddressToDomain, resolveAzeroDomainToAddress } from '@subwallet/extension-base/koni/api/dotsama/domain';
15
14
  import { parseSubstrateTransaction } from '@subwallet/extension-base/koni/api/dotsama/parseTransaction';
16
- import { checkReferenceCount, checkSupportTransfer, createTransferExtrinsic } from '@subwallet/extension-base/koni/api/dotsama/transfer';
15
+ import { createTransferExtrinsic, getTransferMockTxFee } from '@subwallet/extension-base/koni/api/dotsama/transfer';
17
16
  import { getNftTransferExtrinsic, isRecipientSelf } from '@subwallet/extension-base/koni/api/nft/transfer';
18
17
  import { getBondingExtrinsic, getCancelWithdrawalExtrinsic, getClaimRewardExtrinsic, getNominationPoolsInfo, getUnbondingExtrinsic, getValidatorsInfo, validateBondingCondition, validateUnbondingCondition } from '@subwallet/extension-base/koni/api/staking/bonding';
19
18
  import { getTuringCancelCompoundingExtrinsic, getTuringCompoundExtrinsic } from '@subwallet/extension-base/koni/api/staking/bonding/paraChain';
20
19
  import { getPoolingBondingExtrinsic, getPoolingUnbondingExtrinsic, validatePoolBondingCondition, validateRelayUnbondingCondition } from '@subwallet/extension-base/koni/api/staking/bonding/relayChain';
21
20
  import { getERC20TransactionObject, getERC721Transaction, getEVMTransactionObject } from '@subwallet/extension-base/koni/api/tokens/evm/transfer';
22
21
  import { getPSP34TransferExtrinsic } from '@subwallet/extension-base/koni/api/tokens/wasm';
23
- import { createXcmExtrinsic } from '@subwallet/extension-base/koni/api/xcm';
22
+ import { createXcmExtrinsic, getXcmMockTxFee } from '@subwallet/extension-base/koni/api/xcm';
24
23
  import { YIELD_EXTRINSIC_TYPES } from '@subwallet/extension-base/koni/api/yield/helper/utils';
25
24
  import { _API_OPTIONS_CHAIN_GROUP, _DEFAULT_MANTA_ZK_CHAIN, _MANTA_ZK_CHAIN_GROUP, _ZK_ASSET_PREFIX } from '@subwallet/extension-base/services/chain-service/constants';
26
25
  import { _ChainConnectionStatus } from '@subwallet/extension-base/services/chain-service/types';
27
- import { _getChainNativeTokenBasicInfo, _getContractAddressOfToken, _getEvmChainId, _getSubstrateGenesisHash, _getTokenMinAmount, _isAssetSmartContractNft, _isChainEvmCompatible, _isCustomAsset, _isLocalToken, _isMantaZkAsset, _isNativeToken, _isTokenEvmSmartContract, _isTokenTransferredByEvm } from '@subwallet/extension-base/services/chain-service/utils';
28
- import { calculateGasFeeParams } from '@subwallet/extension-base/services/fee-service/utils';
26
+ import { _getAssetDecimals, _getAssetSymbol, _getChainNativeTokenBasicInfo, _getContractAddressOfToken, _getEvmChainId, _getSubstrateGenesisHash, _isAssetSmartContractNft, _isChainEvmCompatible, _isCustomAsset, _isLocalToken, _isMantaZkAsset, _isNativeToken, _isTokenEvmSmartContract, _isTokenTransferredByEvm } from '@subwallet/extension-base/services/chain-service/utils';
29
27
  import { EXTENSION_REQUEST_URL } from '@subwallet/extension-base/services/request-service/constants';
30
28
  import { DEFAULT_AUTO_LOCK_TIME } from '@subwallet/extension-base/services/setting-service/constants';
31
29
  import { WALLET_CONNECT_EIP155_NAMESPACE } from '@subwallet/extension-base/services/wallet-connect-service/constants';
@@ -35,15 +33,15 @@ import { AccountsStore } from '@subwallet/extension-base/stores';
35
33
  import { YieldPoolType } from '@subwallet/extension-base/types';
36
34
  import { BN_ZERO, convertSubjectInfoToAddresses, createTransactionFromRLP, isSameAddress, MODULE_SUPPORT, reformatAddress, signatureToHex, uniqueStringArray } from '@subwallet/extension-base/utils';
37
35
  import { parseContractInput, parseEvmRlp } from '@subwallet/extension-base/utils/eth/parseTransaction';
38
- import { balanceFormatter, formatNumber } from '@subwallet/extension-base/utils/number';
39
36
  import { createPair } from '@subwallet/keyring';
40
37
  import { keyring } from '@subwallet/ui-keyring';
41
38
  import { getSdkError } from '@walletconnect/utils';
42
39
  import BigN from 'bignumber.js';
43
40
  import { t } from 'i18next';
41
+ import { Subject } from 'rxjs';
44
42
  import { TypeRegistry } from '@polkadot/types';
45
- import { assert, BN, hexStripPrefix, hexToU8a, isAscii, isHex, u8aToHex, u8aToString } from '@polkadot/util';
46
- import { addressToEvm, base64Decode, decodeAddress, isAddress, isEthereumAddress, jsonDecrypt, keyExtractSuri, mnemonicGenerate, mnemonicValidate } from '@polkadot/util-crypto';
43
+ import { assert, hexStripPrefix, hexToU8a, isAscii, isHex, u8aToHex, u8aToString } from '@polkadot/util';
44
+ import { base64Decode, decodeAddress, isAddress, isEthereumAddress, jsonDecrypt, keyExtractSuri, mnemonicGenerate, mnemonicValidate } from '@polkadot/util-crypto';
47
45
  const ETH_DERIVE_DEFAULT = '/m/44\'/60\'/0\'/0/0';
48
46
  function getSuri(seed, type) {
49
47
  return type === 'ethereum' ? `${seed}${ETH_DERIVE_DEFAULT}` : seed;
@@ -77,6 +75,7 @@ export default class KoniExtension {
77
75
  #skipAutoLock = false;
78
76
  #firstTime = true;
79
77
  #alwaysLock = false;
78
+ #keyringLockSubject = new Subject();
80
79
  constructor(state) {
81
80
  this.#koniState = state;
82
81
  const updateTimeAutoLock = rs => {
@@ -1542,39 +1541,6 @@ export default class KoniExtension {
1542
1541
  items: subscribeHistoriesResponse.value
1543
1542
  };
1544
1543
  }
1545
-
1546
- // Save address to contact
1547
- // private addContact (to: string) {
1548
- // const toAddress = reformatAddress(to);
1549
- // const account = keyring.getAccount(toAddress);
1550
- // const contact = keyring.getAddress(toAddress);
1551
- //
1552
- // if (!account && (!contact || contact.meta.isRecent)) {
1553
- // keyring.saveAddress(toAddress, {});
1554
- // }
1555
- // }
1556
-
1557
- validateTransfer(tokenSlug, from, to, value, transferAll) {
1558
- const errors = [];
1559
- const keypair = keyring.getPair(from);
1560
- let transferValue;
1561
- if (!transferAll) {
1562
- if (value === undefined) {
1563
- errors.push(new TransactionError(BasicTxErrorType.INVALID_PARAMS, t('Transfer amount is required')));
1564
- }
1565
- if (value) {
1566
- transferValue = new BN(value);
1567
- }
1568
- }
1569
- const tokenInfo = this.#koniState.getAssetBySlug(tokenSlug);
1570
- if (!tokenInfo) {
1571
- errors.push(new TransactionError(BasicTxErrorType.INVALID_PARAMS, t('Not found token from registry')));
1572
- }
1573
- if (isEthereumAddress(from) && isEthereumAddress(to) && _isTokenEvmSmartContract(tokenInfo) && _getContractAddressOfToken(tokenInfo).length === 0) {
1574
- errors.push(new TransactionError(BasicTxErrorType.INVALID_PARAMS, t('Not found ERC20 address for this token')));
1575
- }
1576
- return [errors, keypair, transferValue, tokenInfo];
1577
- }
1578
1544
  async makeTransfer(inputData) {
1579
1545
  const {
1580
1546
  from,
@@ -1584,45 +1550,41 @@ export default class KoniExtension {
1584
1550
  transferAll,
1585
1551
  value
1586
1552
  } = inputData;
1587
- const [errors,,, tokenInfo] = this.validateTransfer(tokenSlug, from, to, value, transferAll);
1553
+ const transferTokenInfo = this.#koniState.chainService.getAssetBySlug(tokenSlug);
1554
+ const [errors,,] = validateTransferRequest(transferTokenInfo, from, to, value, transferAll);
1588
1555
  const warnings = [];
1589
1556
  const evmApiMap = this.#koniState.getEvmApiMap();
1590
1557
  const chainInfo = this.#koniState.getChainInfo(networkKey);
1591
1558
  const nativeTokenInfo = this.#koniState.getNativeTokenInfo(networkKey);
1592
1559
  const nativeTokenSlug = nativeTokenInfo.slug;
1593
1560
  const isTransferNativeToken = nativeTokenSlug === tokenSlug;
1561
+ const extrinsicType = isTransferNativeToken ? ExtrinsicType.TRANSFER_BALANCE : ExtrinsicType.TRANSFER_TOKEN;
1594
1562
  let chainType = ChainType.SUBSTRATE;
1595
- const tokenBaseAmount = {
1596
- value: '0',
1597
- symbol: tokenInfo.symbol,
1598
- decimals: tokenInfo.decimals || 0
1599
- };
1600
1563
  const transferAmount = {
1601
- ...tokenBaseAmount
1564
+ value: '0',
1565
+ symbol: _getAssetSymbol(transferTokenInfo),
1566
+ decimals: _getAssetDecimals(transferTokenInfo)
1602
1567
  };
1603
1568
  let transaction;
1604
-
1605
- // Get native token amount
1606
- const freeBalance = await this.getAddressFreeBalance({
1569
+ const transferTokenAvailable = await this.getAddressTransferableBalance({
1607
1570
  address: from,
1608
1571
  networkKey,
1609
- token: tokenSlug
1572
+ token: tokenSlug,
1573
+ extrinsicType
1610
1574
  });
1611
1575
  try {
1612
- if (isEthereumAddress(from) && isEthereumAddress(to) && _isTokenTransferredByEvm(tokenInfo)) {
1613
- // TODO: review this
1576
+ if (isEthereumAddress(from) && isEthereumAddress(to) && _isTokenTransferredByEvm(transferTokenInfo)) {
1614
1577
  chainType = ChainType.EVM;
1615
- const txVal = transferAll ? freeBalance.value : value || '0';
1578
+ const txVal = transferAll ? transferTokenAvailable.value : value || '0';
1616
1579
  const evmApi = evmApiMap[networkKey];
1617
1580
 
1618
1581
  // Estimate with EVM API
1619
- if (_isTokenEvmSmartContract(tokenInfo) || _isLocalToken(tokenInfo)) {
1620
- [transaction, transferAmount.value] = await getERC20TransactionObject(_getContractAddressOfToken(tokenInfo), chainInfo, from, to, txVal, !!transferAll, evmApi);
1582
+ if (_isTokenEvmSmartContract(transferTokenInfo) || _isLocalToken(transferTokenInfo)) {
1583
+ [transaction, transferAmount.value] = await getERC20TransactionObject(_getContractAddressOfToken(transferTokenInfo), chainInfo, from, to, txVal, !!transferAll, evmApi);
1621
1584
  } else {
1622
1585
  [transaction, transferAmount.value] = await getEVMTransactionObject(chainInfo, from, to, txVal, !!transferAll, evmApi);
1623
1586
  }
1624
- } else if (_isMantaZkAsset(tokenInfo)) {
1625
- // TODO
1587
+ } else if (_isMantaZkAsset(transferTokenInfo)) {
1626
1588
  transaction = undefined;
1627
1589
  transferAmount.value = '0';
1628
1590
  } else {
@@ -1632,7 +1594,7 @@ export default class KoniExtension {
1632
1594
  value: value || '0',
1633
1595
  from: from,
1634
1596
  networkKey,
1635
- tokenInfo,
1597
+ tokenInfo: transferTokenInfo,
1636
1598
  to: to,
1637
1599
  substrateApi
1638
1600
  });
@@ -1645,46 +1607,33 @@ export default class KoniExtension {
1645
1607
  throw error;
1646
1608
  }
1647
1609
  const transferNativeAmount = isTransferNativeToken ? transferAmount.value : '0';
1648
-
1649
- // this.addContact(to);
1650
-
1651
1610
  const additionalValidator = async inputTransaction => {
1652
- const minAmount = tokenInfo.minAmount || '0';
1611
+ let senderTransferTokenTransferable;
1653
1612
 
1654
1613
  // Check ed for sender
1655
1614
  if (!isTransferNativeToken) {
1656
1615
  const {
1657
- value: balance
1658
- } = await this.getAddressFreeBalance({
1616
+ value
1617
+ } = await this.getAddressTransferableBalance({
1659
1618
  address: from,
1660
1619
  networkKey,
1661
- token: tokenSlug
1620
+ token: tokenSlug,
1621
+ extrinsicType
1662
1622
  });
1663
- if (new BigN(balance).minus(transferAmount.value).lt(minAmount)) {
1664
- inputTransaction.warnings.push(new TransactionWarning(BasicTxWarningCode.NOT_ENOUGH_EXISTENTIAL_DEPOSIT));
1665
- }
1623
+ senderTransferTokenTransferable = value;
1666
1624
  }
1667
1625
  const {
1668
- value: receiverBalance
1669
- } = await this.getAddressFreeBalance({
1626
+ value: receiverTransferTokenTransferable
1627
+ } = await this.getAddressTransferableBalance({
1670
1628
  address: to,
1671
1629
  networkKey,
1672
- token: tokenSlug
1673
- });
1630
+ token: tokenSlug,
1631
+ extrinsicType
1632
+ }); // todo: shouldn't be just transferable, locked also counts
1674
1633
 
1675
- // Check ed for receiver
1676
- if (new BigN(receiverBalance).plus(transferAmount.value).lt(minAmount)) {
1677
- const atLeast = new BigN(minAmount).minus(receiverBalance).plus((tokenInfo.decimals || 0) === 0 ? 0 : 1);
1678
- const atLeastStr = formatNumber(atLeast, tokenInfo.decimals || 0, balanceFormatter, {
1679
- maxNumberFormat: tokenInfo.decimals || 6
1680
- });
1681
- inputTransaction.errors.push(new TransactionError(TransferTxErrorType.RECEIVER_NOT_ENOUGH_EXISTENTIAL_DEPOSIT, t('You must transfer at least {{amount}} {{symbol}} to keep the destination account alive', {
1682
- replace: {
1683
- amount: atLeastStr,
1684
- symbol: tokenInfo.symbol
1685
- }
1686
- })));
1687
- }
1634
+ const [warning, error] = additionalValidateTransfer(transferTokenInfo, extrinsicType, receiverTransferTokenTransferable, transferAmount.value, senderTransferTokenTransferable);
1635
+ warning && inputTransaction.warnings.push(warning);
1636
+ error && inputTransaction.errors.push(error);
1688
1637
  };
1689
1638
  return this.#koniState.transactionService.handleTransaction({
1690
1639
  errors,
@@ -1695,24 +1644,13 @@ export default class KoniExtension {
1695
1644
  transferNativeAmount,
1696
1645
  transaction,
1697
1646
  data: inputData,
1698
- extrinsicType: isTransferNativeToken ? ExtrinsicType.TRANSFER_BALANCE : ExtrinsicType.TRANSFER_TOKEN,
1647
+ extrinsicType,
1699
1648
  ignoreWarnings: transferAll,
1700
1649
  isTransferAll: isTransferNativeToken ? transferAll : false,
1701
1650
  edAsWarning: isTransferNativeToken,
1702
1651
  additionalValidator: additionalValidator
1703
1652
  });
1704
1653
  }
1705
- validateCrossChainTransfer(destinationNetworkKey, sendingTokenSlug, sender, sendingValue) {
1706
- const errors = [];
1707
- const keypair = keyring.getPair(sender);
1708
- const transferValue = new BN(sendingValue);
1709
- const originTokenInfo = this.#koniState.getAssetBySlug(sendingTokenSlug);
1710
- const destinationTokenInfo = this.#koniState.getXcmEqualAssetByChain(destinationNetworkKey, sendingTokenSlug);
1711
- if (!destinationTokenInfo) {
1712
- errors.push(new TransactionError(TransferTxErrorType.INVALID_TOKEN, t('Not found token from registry')));
1713
- }
1714
- return [errors, keypair, transferValue, originTokenInfo, destinationTokenInfo];
1715
- }
1716
1654
  async makeCrossChainTransfer(inputData) {
1717
1655
  const {
1718
1656
  destinationNetworkKey,
@@ -1722,7 +1660,9 @@ export default class KoniExtension {
1722
1660
  tokenSlug,
1723
1661
  value
1724
1662
  } = inputData;
1725
- const [errors, fromKeyPair,, originTokenInfo, destinationTokenInfo] = this.validateCrossChainTransfer(destinationNetworkKey, tokenSlug, from, value);
1663
+ const originTokenInfo = this.#koniState.getAssetBySlug(tokenSlug);
1664
+ const destinationTokenInfo = this.#koniState.getXcmEqualAssetByChain(destinationNetworkKey, tokenSlug);
1665
+ const [errors, fromKeyPair] = validateXcmTransferRequest(destinationTokenInfo, from, value);
1726
1666
  let extrinsic = null;
1727
1667
  if (errors.length > 0) {
1728
1668
  return this.#koniState.transactionService.generateBeforeHandleResponseErrors(errors);
@@ -1741,37 +1681,16 @@ export default class KoniExtension {
1741
1681
  substrateApi
1742
1682
  });
1743
1683
  additionalValidator = async inputTransaction => {
1744
- const destMinAmount = destinationTokenInfo.minAmount || '0';
1745
- const atLeast = new BigN(destMinAmount).multipliedBy(XCM_MIN_AMOUNT_RATIO);
1746
-
1747
- // Check ed for receiver
1748
- if (new BigN(value).lt(atLeast)) {
1749
- const atLeastStr = formatNumber(atLeast, destinationTokenInfo.decimals || 0, balanceFormatter, {
1750
- maxNumberFormat: destinationTokenInfo.decimals || 6
1751
- });
1752
- inputTransaction.errors.push(new TransactionError(TransferTxErrorType.RECEIVER_NOT_ENOUGH_EXISTENTIAL_DEPOSIT, t('You must transfer at least {{amount}} {{symbol}} to keep the destination account alive', {
1753
- replace: {
1754
- amount: atLeastStr,
1755
- symbol: originTokenInfo.symbol
1756
- }
1757
- })));
1758
- }
1759
- const srcMinAmount = originTokenInfo.minAmount || '0';
1760
- const isTransferNativeToken = originTokenInfo.assetType === _AssetType.NATIVE;
1761
-
1762
- // Check ed for sender
1763
- if (!isTransferNativeToken) {
1764
- const {
1765
- value: balance
1766
- } = await this.getAddressFreeBalance({
1767
- address: from,
1768
- networkKey: originNetworkKey,
1769
- token: originTokenInfo.slug
1770
- });
1771
- if (new BigN(balance).minus(value).lt(srcMinAmount)) {
1772
- inputTransaction.warnings.push(new TransactionWarning(BasicTxWarningCode.NOT_ENOUGH_EXISTENTIAL_DEPOSIT));
1773
- }
1774
- }
1684
+ const {
1685
+ value: senderTransferable
1686
+ } = await this.getAddressTransferableBalance({
1687
+ address: from,
1688
+ networkKey: originNetworkKey,
1689
+ token: originTokenInfo.slug
1690
+ });
1691
+ const [warning, error] = additionalValidateXcmTransfer(originTokenInfo, destinationTokenInfo, value, senderTransferable);
1692
+ error && inputTransaction.errors.push(error);
1693
+ warning && inputTransaction.warnings.push(warning);
1775
1694
  };
1776
1695
  eventsHandler = eventEmitter => {
1777
1696
  eventEmitter.on('send', () => {
@@ -1790,9 +1709,6 @@ export default class KoniExtension {
1790
1709
  });
1791
1710
  };
1792
1711
  }
1793
-
1794
- // this.addContact(to);
1795
-
1796
1712
  return await this.#koniState.transactionService.handleTransaction({
1797
1713
  url: EXTENSION_REQUEST_URL,
1798
1714
  address: from,
@@ -1896,8 +1812,9 @@ export default class KoniExtension {
1896
1812
  async validateCustomAsset(data) {
1897
1813
  return await this.#koniState.validateCustomAsset(data);
1898
1814
  }
1899
- async getAddressFreeBalance({
1815
+ async getAddressTransferableBalance({
1900
1816
  address,
1817
+ extrinsicType,
1901
1818
  networkKey,
1902
1819
  token
1903
1820
  }) {
@@ -1907,100 +1824,75 @@ export default class KoniExtension {
1907
1824
  return await this.#koniState.getMantaPayZkBalance(address, tokenInfo);
1908
1825
  }
1909
1826
  }
1910
- return await this.#koniState.balanceService.getTokenFreeBalance(address, networkKey, token);
1827
+ return await this.#koniState.balanceService.getTransferableBalance(address, networkKey, token, extrinsicType);
1911
1828
  }
1912
- async transferGetMaxTransferable({
1829
+ async getMaxTransferable({
1913
1830
  address,
1914
1831
  destChain,
1915
1832
  isXcmTransfer,
1916
1833
  networkKey,
1917
1834
  token
1918
1835
  }) {
1919
- const freeBalance = await this.getAddressFreeBalance({
1920
- address,
1921
- networkKey,
1922
- token
1923
- });
1924
1836
  const tokenInfo = token ? this.#koniState.chainService.getAssetBySlug(token) : this.#koniState.chainService.getNativeTokenInfo(networkKey);
1925
1837
  if (!_isNativeToken(tokenInfo)) {
1926
- return freeBalance;
1838
+ return await this.getAddressTransferableBalance({
1839
+ extrinsicType: ExtrinsicType.TRANSFER_TOKEN,
1840
+ address,
1841
+ networkKey,
1842
+ token
1843
+ });
1927
1844
  } else {
1928
- const substrateApi = this.#koniState.chainService.getSubstrateApi(networkKey);
1929
- let estimatedFee;
1930
- let maxTransferable = new BigN(freeBalance.value);
1931
- try {
1932
- if (isXcmTransfer) {
1933
- const chainInfoMap = this.#koniState.chainService.getChainInfoMap();
1934
- const destinationTokenInfo = this.#koniState.getXcmEqualAssetByChain(destChain, tokenInfo.slug);
1935
- if (!destinationTokenInfo) {
1936
- estimatedFee = '0';
1937
- } else {
1938
- maxTransferable = maxTransferable.minus(new BigN(tokenInfo.minAmount || '0').multipliedBy(XCM_MIN_AMOUNT_RATIO));
1939
- const desChainInfo = chainInfoMap[destChain];
1940
- const orgChainInfo = chainInfoMap[networkKey];
1941
- const recipient = !isEthereumAddress(address) && _isChainEvmCompatible(desChainInfo) && !_isChainEvmCompatible(orgChainInfo) ? u8aToHex(addressToEvm(address)) : address;
1942
- const mockTx = await createXcmExtrinsic({
1943
- chainInfoMap,
1944
- destinationTokenInfo,
1945
- originTokenInfo: tokenInfo,
1946
- recipient: recipient,
1947
- sendingValue: '1000000000000000000',
1948
- substrateApi
1949
- });
1950
- try {
1951
- var _paymentInfo$partialF;
1952
- const paymentInfo = await mockTx.paymentInfo(address);
1953
- estimatedFee = (paymentInfo === null || paymentInfo === void 0 ? void 0 : (_paymentInfo$partialF = paymentInfo.partialFee) === null || _paymentInfo$partialF === void 0 ? void 0 : _paymentInfo$partialF.toString()) || '0';
1954
- } catch (e) {
1955
- estimatedFee = tokenInfo.minAmount || '0';
1956
- }
1957
- }
1958
- } else {
1959
- const chainInfo = this.#koniState.chainService.getChainInfoByKey(networkKey);
1960
- if (_isChainEvmCompatible(chainInfo) && _isTokenTransferredByEvm(tokenInfo)) {
1961
- const web3 = this.#koniState.chainService.getEvmApi(networkKey);
1962
- const transaction = {
1963
- value: 0,
1964
- to: '0x0000000000000000000000000000000000000000',
1965
- // null address
1966
- from: address
1967
- };
1968
- const gasLimit = await web3.api.eth.estimateGas(transaction);
1969
- const priority = await calculateGasFeeParams(web3, networkKey);
1970
- if (priority.baseGasFee) {
1971
- const maxFee = priority.maxFeePerGas;
1972
- estimatedFee = maxFee.multipliedBy(gasLimit).toFixed(0);
1973
- } else {
1974
- estimatedFee = new BigN(priority.gasPrice).multipliedBy(gasLimit).toFixed(0);
1975
- }
1976
- } else {
1977
- var _paymentInfo$partialF2;
1978
- const [mockTx] = await createTransferExtrinsic({
1979
- from: address,
1980
- networkKey,
1981
- substrateApi,
1982
- to: address,
1983
- tokenInfo,
1984
- transferAll: true,
1985
- value: '1000000000000000000'
1986
- });
1987
- const paymentInfo = await (mockTx === null || mockTx === void 0 ? void 0 : mockTx.paymentInfo(address));
1988
- estimatedFee = (paymentInfo === null || paymentInfo === void 0 ? void 0 : (_paymentInfo$partialF2 = paymentInfo.partialFee) === null || _paymentInfo$partialF2 === void 0 ? void 0 : _paymentInfo$partialF2.toString()) || '0';
1989
- }
1990
- }
1991
- } catch (e) {
1992
- estimatedFee = '0';
1993
- console.warn('Unable to estimate fee', e);
1845
+ let maxTransferable;
1846
+ if (isXcmTransfer) {
1847
+ maxTransferable = await this.getXcmMaxTransferable(tokenInfo, destChain, address);
1848
+ } else {
1849
+ // regular transfer with native token
1850
+ maxTransferable = await this.getNativeTokenMaxTransferable(tokenInfo, networkKey, address);
1994
1851
  }
1995
- maxTransferable = maxTransferable.minus(new BigN(estimatedFee).multipliedBy(isXcmTransfer ? XCM_FEE_RATIO : 1));
1996
1852
  return {
1997
- ...freeBalance,
1998
- value: maxTransferable.gt(BN_ZERO) ? maxTransferable.toFixed(0) || '0' : '0'
1853
+ value: maxTransferable.gt(BN_ZERO) ? maxTransferable.toFixed(0) || '0' : '0',
1854
+ decimals: tokenInfo.decimals,
1855
+ symbol: tokenInfo.symbol
1999
1856
  };
2000
1857
  }
2001
1858
  }
2002
- async subscribeAddressFreeBalance({
1859
+ async getXcmMaxTransferable(originTokenInfo, destChain, address) {
1860
+ const substrateApi = this.#koniState.chainService.getSubstrateApi(originTokenInfo.originChain);
1861
+ const chainInfoMap = this.#koniState.chainService.getChainInfoMap();
1862
+ const destinationTokenInfo = this.#koniState.getXcmEqualAssetByChain(destChain, originTokenInfo.slug);
1863
+ if (destinationTokenInfo) {
1864
+ const [bnMockFee, {
1865
+ value
1866
+ }] = await Promise.all([getXcmMockTxFee(substrateApi, chainInfoMap, address, originTokenInfo, destinationTokenInfo), this.getAddressTransferableBalance({
1867
+ extrinsicType: ExtrinsicType.TRANSFER_XCM,
1868
+ address,
1869
+ networkKey: originTokenInfo.originChain,
1870
+ token: originTokenInfo.slug
1871
+ })]);
1872
+ const bnMaxTransferable = new BigN(value);
1873
+ const estimatedFee = bnMockFee.multipliedBy(XCM_FEE_RATIO); // multiply by weight to account for destination chain fee
1874
+
1875
+ return bnMaxTransferable.minus(estimatedFee);
1876
+ }
1877
+ return new BigN(0);
1878
+ }
1879
+ async getNativeTokenMaxTransferable(tokenInfo, networkKey, address) {
1880
+ const chainInfo = this.#koniState.chainService.getChainInfoByKey(networkKey);
1881
+ const api = _isChainEvmCompatible(chainInfo) && _isTokenTransferredByEvm(tokenInfo) ? this.#koniState.chainService.getEvmApi(networkKey) : this.#koniState.chainService.getSubstrateApi(networkKey);
1882
+ const [mockTxFee, {
1883
+ value
1884
+ }] = await Promise.all([getTransferMockTxFee(address, chainInfo, tokenInfo, api), this.getAddressTransferableBalance({
1885
+ extrinsicType: ExtrinsicType.TRANSFER_BALANCE,
1886
+ address,
1887
+ networkKey,
1888
+ token: tokenInfo.slug
1889
+ })]);
1890
+ const bnMaxTransferable = new BigN(value);
1891
+ return bnMaxTransferable.minus(mockTxFee);
1892
+ }
1893
+ async subscribeAddressTransferableBalance({
2003
1894
  address,
1895
+ extrinsicType,
2004
1896
  networkKey,
2005
1897
  token
2006
1898
  }, id, port) {
@@ -2015,33 +1907,13 @@ export default class KoniExtension {
2015
1907
  // eslint-disable-next-line node/no-callback-literal
2016
1908
  cb(convertData(data));
2017
1909
  };
2018
- const [unsub, currentFreeBalance] = await this.#koniState.balanceService.subscribeTokenFreeBalance(address, networkKey, token, _cb);
1910
+ const [unsub, currentFreeBalance] = await this.#koniState.balanceService.subscribeTransferableBalance(address, networkKey, token, extrinsicType, _cb);
2019
1911
  this.createUnsubscriptionHandle(id, unsub);
2020
1912
  port.onDisconnect.addListener(() => {
2021
1913
  this.cancelSubscription(id);
2022
1914
  });
2023
1915
  return convertData(currentFreeBalance);
2024
1916
  }
2025
- async transferCheckReferenceCount({
2026
- address,
2027
- networkKey
2028
- }) {
2029
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-return
2030
- return await checkReferenceCount(networkKey, address, this.#koniState.getSubstrateApiMap(), this.#koniState.getChainInfo(networkKey));
2031
- }
2032
- async transferCheckSupporting({
2033
- networkKey,
2034
- tokenSlug
2035
- }) {
2036
- const tokenInfo = this.#koniState.getAssetBySlug(tokenSlug);
2037
- return await checkSupportTransfer(networkKey, tokenInfo, this.#koniState.getSubstrateApiMap(), this.#koniState.getChainInfo(networkKey));
2038
- }
2039
- transferGetExistentialDeposit({
2040
- tokenSlug
2041
- }) {
2042
- const tokenInfo = this.#koniState.getAssetBySlug(tokenSlug);
2043
- return _getTokenMinAmount(tokenInfo);
2044
- }
2045
1917
  async substrateNftSubmitTransaction(inputData) {
2046
1918
  const {
2047
1919
  params,
@@ -2926,8 +2798,12 @@ export default class KoniExtension {
2926
2798
 
2927
2799
  keyringLock() {
2928
2800
  this.#koniState.keyringService.lock();
2801
+ this.#keyringLockSubject.next(true);
2929
2802
  clearTimeout(this.#lockTimeOut);
2930
2803
  }
2804
+ keyringLockSubscribe(cb) {
2805
+ this.#keyringLockSubject.subscribe(cb);
2806
+ }
2931
2807
 
2932
2808
  // Export mnemonic
2933
2809
 
@@ -4407,20 +4283,14 @@ export default class KoniExtension {
4407
4283
  return this.subscribeAssetSetting(id, port);
4408
4284
  case 'pri(assetSetting.update)':
4409
4285
  return await this.updateAssetSetting(request);
4410
- case 'pri(transfer.checkReferenceCount)':
4411
- return await this.transferCheckReferenceCount(request);
4412
- case 'pri(transfer.checkSupporting)':
4413
- return await this.transferCheckSupporting(request);
4414
- case 'pri(transfer.getExistentialDeposit)':
4415
- return this.transferGetExistentialDeposit(request);
4416
4286
  case 'pri(transfer.getMaxTransferable)':
4417
- return this.transferGetMaxTransferable(request);
4287
+ return this.getMaxTransferable(request);
4418
4288
  case 'pri(transfer.subscribeMaxTransferable)':
4419
- return this.transferGetMaxTransferable(request);
4289
+ return this.getMaxTransferable(request);
4420
4290
  case 'pri(freeBalance.get)':
4421
- return this.getAddressFreeBalance(request);
4291
+ return this.getAddressTransferableBalance(request);
4422
4292
  case 'pri(freeBalance.subscribe)':
4423
- return this.subscribeAddressFreeBalance(request, id, port);
4293
+ return this.subscribeAddressTransferableBalance(request, id, port);
4424
4294
  case 'pri(subscription.cancel)':
4425
4295
  return this.cancelSubscription(request);
4426
4296
  case 'pri(chainService.recoverSubstrateApi)':
package/package.json CHANGED
@@ -17,7 +17,7 @@
17
17
  "./cjs/detectPackage.js"
18
18
  ],
19
19
  "type": "module",
20
- "version": "1.2.3-0",
20
+ "version": "1.2.4-0",
21
21
  "main": "./cjs/index.js",
22
22
  "module": "./index.js",
23
23
  "types": "./index.d.ts",
@@ -129,6 +129,36 @@
129
129
  "require": "./cjs/constants/storage.js",
130
130
  "default": "./constants/storage.js"
131
131
  },
132
+ "./core/logic-validation/earning": {
133
+ "types": "./core/logic-validation/earning.d.ts",
134
+ "require": "./cjs/core/logic-validation/earning.js",
135
+ "default": "./core/logic-validation/earning.js"
136
+ },
137
+ "./core/logic-validation/swap": {
138
+ "types": "./core/logic-validation/swap.d.ts",
139
+ "require": "./cjs/core/logic-validation/swap.js",
140
+ "default": "./core/logic-validation/swap.js"
141
+ },
142
+ "./core/logic-validation/transfer": {
143
+ "types": "./core/logic-validation/transfer.d.ts",
144
+ "require": "./cjs/core/logic-validation/transfer.js",
145
+ "default": "./core/logic-validation/transfer.js"
146
+ },
147
+ "./core/substrate/nominationpools-pallet": {
148
+ "types": "./core/substrate/nominationpools-pallet.d.ts",
149
+ "require": "./cjs/core/substrate/nominationpools-pallet.js",
150
+ "default": "./core/substrate/nominationpools-pallet.js"
151
+ },
152
+ "./core/substrate/system-pallet": {
153
+ "types": "./core/substrate/system-pallet.d.ts",
154
+ "require": "./cjs/core/substrate/system-pallet.js",
155
+ "default": "./core/substrate/system-pallet.js"
156
+ },
157
+ "./core/substrate/xcm-parser": {
158
+ "types": "./core/substrate/xcm-parser.d.ts",
159
+ "require": "./cjs/core/substrate/xcm-parser.js",
160
+ "default": "./core/substrate/xcm-parser.js"
161
+ },
132
162
  "./defaults": {
133
163
  "types": "./defaults.d.ts",
134
164
  "require": "./cjs/defaults.js",
@@ -1891,17 +1921,17 @@
1891
1921
  "@reduxjs/toolkit": "^1.9.1",
1892
1922
  "@sora-substrate/type-definitions": "^1.17.7",
1893
1923
  "@substrate/connect": "^0.8.9",
1894
- "@subwallet/chain-list": "0.2.65",
1895
- "@subwallet/extension-base": "^1.2.3-0",
1896
- "@subwallet/extension-chains": "^1.2.3-0",
1897
- "@subwallet/extension-dapp": "^1.2.3-0",
1898
- "@subwallet/extension-inject": "^1.2.3-0",
1924
+ "@subwallet/chain-list": "0.2.67",
1925
+ "@subwallet/extension-base": "^1.2.4-0",
1926
+ "@subwallet/extension-chains": "^1.2.4-0",
1927
+ "@subwallet/extension-dapp": "^1.2.4-0",
1928
+ "@subwallet/extension-inject": "^1.2.4-0",
1899
1929
  "@subwallet/keyring": "^0.1.5",
1900
1930
  "@subwallet/ui-keyring": "^0.1.5",
1901
1931
  "@walletconnect/keyvaluestorage": "^1.1.1",
1902
- "@walletconnect/sign-client": "^2.11.3",
1903
- "@walletconnect/types": "^2.11.3",
1904
- "@walletconnect/utils": "^2.11.3",
1932
+ "@walletconnect/sign-client": "^2.13.1",
1933
+ "@walletconnect/types": "^2.13.1",
1934
+ "@walletconnect/utils": "^2.13.1",
1905
1935
  "avail-js-sdk": "^0.2.12",
1906
1936
  "axios": "^1.6.2",
1907
1937
  "bignumber.js": "^9.1.1",
package/packageInfo.js CHANGED
@@ -7,5 +7,5 @@ export const packageInfo = {
7
7
  name: '@subwallet/extension-base',
8
8
  path: (import.meta && import.meta.url) ? new URL(import.meta.url).pathname.substring(0, new URL(import.meta.url).pathname.lastIndexOf('/') + 1) : 'auto',
9
9
  type: 'esm',
10
- version: '1.2.3-0'
10
+ version: '1.2.4-0'
11
11
  };