@subwallet/extension-base 1.3.17-0 → 1.3.18-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 (127) hide show
  1. package/background/KoniTypes.d.ts +7 -2
  2. package/cjs/constants/index.js +6 -3
  3. package/cjs/core/logic-validation/request.js +26 -19
  4. package/cjs/core/logic-validation/transfer.js +18 -17
  5. package/cjs/koni/api/contract-handler/evm/web3.js +3 -3
  6. package/cjs/koni/background/handlers/Extension.js +269 -123
  7. package/cjs/koni/background/handlers/State.js +1 -8
  8. package/cjs/packageInfo.js +1 -1
  9. package/cjs/services/balance-service/helpers/subscribe/substrate/index.js +1 -1
  10. package/cjs/services/balance-service/index.js +13 -0
  11. package/cjs/services/balance-service/transfer/smart-contract.js +47 -33
  12. package/cjs/services/balance-service/transfer/token.js +5 -4
  13. package/cjs/services/balance-service/transfer/xcm/availBridge.js +14 -18
  14. package/cjs/services/balance-service/transfer/xcm/index.js +30 -61
  15. package/cjs/services/balance-service/transfer/xcm/polygonBridge.js +13 -17
  16. package/cjs/services/balance-service/transfer/xcm/posBridge.js +20 -21
  17. package/cjs/services/balance-service/transfer/xcm/snowBridge.js +7 -7
  18. package/cjs/services/balance-service/transfer/xcm/utils.js +2 -2
  19. package/cjs/services/chain-service/constants.js +3 -3
  20. package/cjs/services/earning-service/handlers/liquid-staking/stella-swap.js +7 -12
  21. package/cjs/services/earning-service/handlers/special.js +15 -4
  22. package/cjs/services/fee-service/interfaces.js +1 -0
  23. package/cjs/services/fee-service/service.js +111 -0
  24. package/cjs/services/fee-service/utils/index.js +99 -113
  25. package/cjs/services/storage-service/db-stores/Balance.js +3 -0
  26. package/cjs/services/swap-service/handler/asset-hub/handler.js +21 -6
  27. package/cjs/services/swap-service/handler/asset-hub/router.js +1 -1
  28. package/cjs/services/swap-service/handler/asset-hub/utils.js +4 -4
  29. package/cjs/services/swap-service/handler/base-handler.js +2 -0
  30. package/cjs/services/swap-service/handler/chainflip-handler.js +25 -4
  31. package/cjs/services/swap-service/handler/hydradx-handler.js +20 -6
  32. package/cjs/services/swap-service/handler/simpleswap-handler.js +23 -3
  33. package/cjs/services/swap-service/index.js +8 -8
  34. package/cjs/services/transaction-service/index.js +23 -3
  35. package/cjs/types/balance/transfer.js +1 -0
  36. package/cjs/types/fee/base.js +1 -0
  37. package/cjs/types/fee/evm.js +16 -1
  38. package/cjs/types/fee/index.js +37 -4
  39. package/cjs/types/fee/option.js +1 -0
  40. package/cjs/types/fee/subscription.js +1 -0
  41. package/cjs/types/fee/substrate.js +1 -0
  42. package/cjs/types/fee/ton.js +1 -0
  43. package/cjs/utils/fee/combine.js +50 -0
  44. package/cjs/utils/fee/index.js +27 -0
  45. package/cjs/utils/fee/transfer.js +374 -0
  46. package/cjs/utils/index.js +12 -0
  47. package/constants/index.d.ts +1 -0
  48. package/constants/index.js +1 -0
  49. package/core/logic-validation/request.js +17 -10
  50. package/core/logic-validation/transfer.d.ts +5 -5
  51. package/core/logic-validation/transfer.js +20 -19
  52. package/core/substrate/xcm-parser.d.ts +1 -1
  53. package/koni/api/contract-handler/evm/web3.js +3 -3
  54. package/koni/background/handlers/Extension.d.ts +3 -3
  55. package/koni/background/handlers/Extension.js +253 -107
  56. package/koni/background/handlers/State.js +1 -8
  57. package/package.json +55 -5
  58. package/packageInfo.js +1 -1
  59. package/services/balance-service/helpers/subscribe/substrate/index.js +1 -1
  60. package/services/balance-service/index.d.ts +1 -0
  61. package/services/balance-service/index.js +13 -0
  62. package/services/balance-service/transfer/smart-contract.d.ts +24 -4
  63. package/services/balance-service/transfer/smart-contract.js +45 -33
  64. package/services/balance-service/transfer/token.js +6 -5
  65. package/services/balance-service/transfer/xcm/availBridge.d.ts +3 -2
  66. package/services/balance-service/transfer/xcm/availBridge.js +11 -15
  67. package/services/balance-service/transfer/xcm/index.d.ts +12 -11
  68. package/services/balance-service/transfer/xcm/index.js +29 -58
  69. package/services/balance-service/transfer/xcm/polygonBridge.d.ts +4 -3
  70. package/services/balance-service/transfer/xcm/polygonBridge.js +13 -17
  71. package/services/balance-service/transfer/xcm/posBridge.d.ts +4 -3
  72. package/services/balance-service/transfer/xcm/posBridge.js +18 -19
  73. package/services/balance-service/transfer/xcm/snowBridge.d.ts +2 -1
  74. package/services/balance-service/transfer/xcm/snowBridge.js +7 -7
  75. package/services/balance-service/transfer/xcm/utils.js +2 -2
  76. package/services/chain-service/constants.js +3 -3
  77. package/services/earning-service/handlers/liquid-staking/stella-swap.js +7 -12
  78. package/services/earning-service/handlers/special.js +15 -4
  79. package/services/fee-service/interfaces.d.ts +5 -0
  80. package/services/fee-service/interfaces.js +1 -0
  81. package/services/fee-service/service.d.ts +4 -1
  82. package/services/fee-service/service.js +111 -0
  83. package/services/fee-service/utils/index.d.ts +8 -2
  84. package/services/fee-service/utils/index.js +92 -108
  85. package/services/storage-service/db-stores/Balance.d.ts +1 -0
  86. package/services/storage-service/db-stores/Balance.js +3 -0
  87. package/services/swap-service/handler/asset-hub/handler.d.ts +2 -1
  88. package/services/swap-service/handler/asset-hub/handler.js +21 -6
  89. package/services/swap-service/handler/asset-hub/router.js +2 -2
  90. package/services/swap-service/handler/asset-hub/utils.d.ts +1 -1
  91. package/services/swap-service/handler/asset-hub/utils.js +2 -2
  92. package/services/swap-service/handler/base-handler.d.ts +4 -1
  93. package/services/swap-service/handler/base-handler.js +2 -0
  94. package/services/swap-service/handler/chainflip-handler.d.ts +2 -1
  95. package/services/swap-service/handler/chainflip-handler.js +24 -3
  96. package/services/swap-service/handler/hydradx-handler.d.ts +2 -1
  97. package/services/swap-service/handler/hydradx-handler.js +19 -5
  98. package/services/swap-service/handler/simpleswap-handler.d.ts +2 -1
  99. package/services/swap-service/handler/simpleswap-handler.js +23 -3
  100. package/services/swap-service/index.js +8 -8
  101. package/services/transaction-service/index.js +23 -3
  102. package/services/transaction-service/types.d.ts +5 -4
  103. package/types/balance/transfer.d.ts +25 -0
  104. package/types/balance/transfer.js +1 -0
  105. package/types/fee/base.d.ts +8 -0
  106. package/types/fee/base.js +1 -0
  107. package/types/fee/evm.d.ts +46 -16
  108. package/types/fee/evm.js +10 -1
  109. package/types/fee/index.d.ts +4 -1
  110. package/types/fee/index.js +4 -1
  111. package/types/fee/option.d.ts +8 -0
  112. package/types/fee/option.js +1 -0
  113. package/types/fee/subscription.d.ts +12 -0
  114. package/types/fee/subscription.js +1 -0
  115. package/types/fee/substrate.d.ts +15 -0
  116. package/types/fee/substrate.js +1 -0
  117. package/types/fee/ton.d.ts +18 -0
  118. package/types/fee/ton.js +1 -0
  119. package/types/transaction/request.d.ts +13 -3
  120. package/utils/fee/combine.d.ts +12 -0
  121. package/utils/fee/combine.js +42 -0
  122. package/utils/fee/index.d.ts +2 -0
  123. package/utils/fee/index.js +5 -0
  124. package/utils/fee/transfer.d.ts +22 -0
  125. package/utils/fee/transfer.js +363 -0
  126. package/utils/index.d.ts +1 -0
  127. package/utils/index.js +1 -0
@@ -15,6 +15,7 @@ import { calculateSwapRate, CHAIN_FLIP_SUPPORTED_MAINNET_ASSET_MAPPING, CHAIN_FL
15
15
  import { BasicTxErrorType } from '@subwallet/extension-base/types';
16
16
  import { CommonStepType } from '@subwallet/extension-base/types/service-base';
17
17
  import { SwapErrorType, SwapFeeType, SwapProviderId, SwapStepType } from '@subwallet/extension-base/types/swap';
18
+ import { getId } from '@subwallet/extension-base/utils/getId';
18
19
  import BigNumber from 'bignumber.js';
19
20
  var ChainflipFeeType;
20
21
  (function (ChainflipFeeType) {
@@ -34,10 +35,11 @@ var CHAINFLIP_QUOTE_ERROR;
34
35
  CHAINFLIP_QUOTE_ERROR["InsufficientIngress"] = "amount is lower than estimated ingress fee";
35
36
  })(CHAINFLIP_QUOTE_ERROR || (CHAINFLIP_QUOTE_ERROR = {}));
36
37
  export class ChainflipSwapHandler {
37
- constructor(chainService, balanceService, isTestnet = true) {
38
+ constructor(chainService, balanceService, feeService, isTestnet = true) {
38
39
  this.swapBaseHandler = new SwapBaseHandler({
39
40
  chainService,
40
41
  balanceService,
42
+ feeService,
41
43
  providerName: isTestnet ? 'Chainflip Testnet' : 'Chainflip',
42
44
  providerSlug: isTestnet ? SwapProviderId.CHAIN_FLIP_TESTNET : SwapProviderId.CHAIN_FLIP_MAINNET
43
45
  });
@@ -388,11 +390,30 @@ export class ChainflipSwapHandler {
388
390
  });
389
391
  extrinsic = submittableExtrinsic;
390
392
  } else {
393
+ const id = getId();
394
+ const feeInfo = await this.swapBaseHandler.feeService.subscribeChainFee(id, chainInfo.slug, 'evm');
391
395
  if (_isNativeToken(fromAsset)) {
392
- const [transactionConfig] = await getEVMTransactionObject(chainInfo, address, depositAddressResponse.depositAddress, quote.fromAmount, false, this.chainService.getEvmApi(chainInfo.slug));
396
+ const [transactionConfig] = await getEVMTransactionObject({
397
+ chain: chainInfo.slug,
398
+ evmApi: this.chainService.getEvmApi(chainInfo.slug),
399
+ from: address,
400
+ to: depositAddressResponse.depositAddress,
401
+ value: quote.fromAmount,
402
+ feeInfo,
403
+ transferAll: false
404
+ });
393
405
  extrinsic = transactionConfig;
394
406
  } else {
395
- const [transactionConfig] = await getERC20TransactionObject(_getContractAddressOfToken(fromAsset), chainInfo, address, depositAddressResponse.depositAddress, quote.fromAmount, false, this.chainService.getEvmApi(chainInfo.slug));
407
+ const [transactionConfig] = await getERC20TransactionObject({
408
+ assetAddress: _getContractAddressOfToken(fromAsset),
409
+ chain: chainInfo.slug,
410
+ evmApi: this.chainService.getEvmApi(chainInfo.slug),
411
+ from: address,
412
+ to: depositAddressResponse.depositAddress,
413
+ value: quote.fromAmount,
414
+ feeInfo,
415
+ transferAll: false
416
+ });
396
417
  extrinsic = transactionConfig;
397
418
  }
398
419
  }
@@ -2,6 +2,7 @@ import { SwapError } from '@subwallet/extension-base/background/errors/SwapError
2
2
  import { TransactionError } from '@subwallet/extension-base/background/errors/TransactionError';
3
3
  import { BalanceService } from '@subwallet/extension-base/services/balance-service';
4
4
  import { ChainService } from '@subwallet/extension-base/services/chain-service';
5
+ import FeeService from '@subwallet/extension-base/services/fee-service/service';
5
6
  import { SwapBaseInterface } from '@subwallet/extension-base/services/swap-service/handler/base-handler';
6
7
  import { BaseStepDetail, CommonOptimalPath, CommonStepFeeInfo } from '@subwallet/extension-base/types/service-base';
7
8
  import { OptimalSwapPathParams, SwapEarlyValidation, SwapProviderId, SwapQuote, SwapRequest, SwapSubmitParams, SwapSubmitStepData, ValidateSwapProcessParams } from '@subwallet/extension-base/types/swap';
@@ -11,7 +12,7 @@ export declare class HydradxHandler implements SwapBaseInterface {
11
12
  private readonly isTestnet;
12
13
  isReady: boolean;
13
14
  providerSlug: SwapProviderId;
14
- constructor(chainService: ChainService, balanceService: BalanceService, isTestnet?: boolean);
15
+ constructor(chainService: ChainService, balanceService: BalanceService, feeService: FeeService, isTestnet?: boolean);
15
16
  init(): Promise<void>;
16
17
  chain: () => string;
17
18
  get chainService(): ChainService;
@@ -15,6 +15,7 @@ import { calculateSwapRate, getSwapAlternativeAsset, SWAP_QUOTE_TIMEOUT_MAP } fr
15
15
  import { BasicTxErrorType } from '@subwallet/extension-base/types';
16
16
  import { CommonStepType } from '@subwallet/extension-base/types/service-base';
17
17
  import { SwapErrorType, SwapFeeType, SwapProviderId, SwapStepType } from '@subwallet/extension-base/types/swap';
18
+ import { getId } from '@subwallet/extension-base/utils/getId';
18
19
  import BigNumber from 'bignumber.js';
19
20
  const HYDRADX_LOW_LIQUIDITY_THRESHOLD = 0.15;
20
21
  const HYDRADX_SUBWALLET_REFERRAL_CODE = 'WALLET';
@@ -24,10 +25,11 @@ const HYDRADX_TESTNET_SUBWALLET_REFERRAL_ACCOUNT = '7LCt6dFqtxzdKVB2648jWW9d85do
24
25
  export class HydradxHandler {
25
26
  isTestnet = true;
26
27
  isReady = false;
27
- constructor(chainService, balanceService, isTestnet = true) {
28
+ constructor(chainService, balanceService, feeService, isTestnet = true) {
28
29
  this.swapBaseHandler = new SwapBaseHandler({
29
30
  balanceService,
30
31
  chainService,
32
+ feeService,
31
33
  providerName: isTestnet ? 'Hydration Testnet' : 'Hydration',
32
34
  providerSlug: isTestnet ? SwapProviderId.HYDRADX_TESTNET : SwapProviderId.HYDRADX_MAINNET
33
35
  });
@@ -89,6 +91,7 @@ export class HydradxHandler {
89
91
  }
90
92
  try {
91
93
  const alternativeChainInfo = this.chainService.getChainInfoByKey(alternativeAsset.originChain);
94
+ const destChainInfo = this.chainService.getChainInfoByKey(this.chain());
92
95
  const step = {
93
96
  metadata: {
94
97
  sendingValue: bnAmount.toString(),
@@ -99,13 +102,18 @@ export class HydradxHandler {
99
102
  type: CommonStepType.XCM
100
103
  };
101
104
  const xcmOriginSubstrateApi = await this.chainService.getSubstrateApi(alternativeAsset.originChain).isReady;
105
+ const id = getId();
106
+ const feeInfo = await this.swapBaseHandler.feeService.subscribeChainFee(id, alternativeAsset.originChain, 'substrate');
102
107
  const xcmTransfer = await createXcmExtrinsic({
103
108
  originTokenInfo: alternativeAsset,
104
109
  destinationTokenInfo: fromAsset,
105
110
  sendingValue: bnAmount.toString(),
106
111
  recipient: params.request.address,
107
- chainInfoMap: this.chainService.getChainInfoMap(),
108
- substrateApi: xcmOriginSubstrateApi
112
+ substrateApi: xcmOriginSubstrateApi,
113
+ sender: params.request.address,
114
+ destinationChain: destChainInfo,
115
+ originChain: alternativeChainInfo,
116
+ feeInfo
109
117
  });
110
118
  const _xcmFeeInfo = await xcmTransfer.paymentInfo(params.request.address);
111
119
  const xcmFeeInfo = _xcmFeeInfo.toPrimitive();
@@ -297,6 +305,8 @@ export class HydradxHandler {
297
305
  const alternativeAssetSlug = getSwapAlternativeAsset(pair);
298
306
  const originAsset = this.chainService.getAssetBySlug(alternativeAssetSlug);
299
307
  const destinationAsset = this.chainService.getAssetBySlug(pair.from);
308
+ const originChain = this.chainService.getChainInfoByKey(originAsset.originChain);
309
+ const destinationChain = this.chainService.getChainInfoByKey(destinationAsset.originChain);
300
310
  const substrateApi = this.chainService.getSubstrateApi(originAsset.originChain);
301
311
  const chainApi = await substrateApi.isReady;
302
312
  const destinationAssetBalance = await this.balanceService.getTransferableBalance(params.address, destinationAsset.originChain, destinationAsset.slug);
@@ -309,13 +319,17 @@ export class HydradxHandler {
309
319
 
310
320
  bnTotalAmount = bnTotalAmount.plus(bnXcmFee);
311
321
  }
322
+ const feeInfo = await this.swapBaseHandler.feeService.subscribeChainFee(getId(), originAsset.originChain, 'substrate');
312
323
  const xcmTransfer = await createXcmExtrinsic({
313
324
  originTokenInfo: originAsset,
314
325
  destinationTokenInfo: destinationAsset,
315
326
  sendingValue: bnTotalAmount.toString(),
316
327
  recipient: params.address,
317
- chainInfoMap: this.chainService.getChainInfoMap(),
318
- substrateApi: chainApi
328
+ substrateApi: chainApi,
329
+ sender: params.address,
330
+ destinationChain,
331
+ originChain,
332
+ feeInfo
319
333
  });
320
334
  const xcmData = {
321
335
  originNetworkKey: originAsset.originChain,
@@ -1,5 +1,6 @@
1
1
  import { SwapError } from '@subwallet/extension-base/background/errors/SwapError';
2
2
  import { TransactionError } from '@subwallet/extension-base/background/errors/TransactionError';
3
+ import FeeService from '@subwallet/extension-base/services/fee-service/service';
3
4
  import { BaseStepDetail, CommonOptimalPath, CommonStepFeeInfo, OptimalSwapPathParams, SwapEarlyValidation, SwapProviderId, SwapQuote, SwapRequest, SwapSubmitParams, SwapSubmitStepData, ValidateSwapProcessParams } from '@subwallet/extension-base/types';
4
5
  import { BalanceService } from '../../balance-service';
5
6
  import { ChainService } from '../../chain-service';
@@ -8,7 +9,7 @@ export declare const simpleSwapApiKey: string;
8
9
  export declare class SimpleSwapHandler implements SwapBaseInterface {
9
10
  private swapBaseHandler;
10
11
  providerSlug: SwapProviderId;
11
- constructor(chainService: ChainService, balanceService: BalanceService);
12
+ constructor(chainService: ChainService, balanceService: BalanceService, feeService: FeeService);
12
13
  validateSwapProcess(params: ValidateSwapProcessParams): Promise<TransactionError[]>;
13
14
  get chainService(): ChainService;
14
15
  get balanceService(): BalanceService;
@@ -8,6 +8,7 @@ import { _getSimpleSwapEarlyValidationError } from '@subwallet/extension-base/co
8
8
  import { _getAssetDecimals, _getChainNativeTokenSlug, _getContractAddressOfToken, _isChainSubstrateCompatible, _isNativeToken } from '@subwallet/extension-base/services/chain-service/utils';
9
9
  import { BasicTxErrorType, CommonStepType, SwapErrorType, SwapFeeType, SwapProviderId, SwapStepType } from '@subwallet/extension-base/types';
10
10
  import { _reformatAddressWithChain, formatNumber } from '@subwallet/extension-base/utils';
11
+ import { getId } from '@subwallet/extension-base/utils/getId';
11
12
  import BigN, { BigNumber } from 'bignumber.js';
12
13
  import { getERC20TransactionObject, getEVMTransactionObject } from "../../balance-service/transfer/smart-contract.js";
13
14
  import { createTransferExtrinsic, getTransferMockTxFee } from "../../balance-service/transfer/token.js";
@@ -116,10 +117,11 @@ const createSwapRequest = async params => {
116
117
  };
117
118
  };
118
119
  export class SimpleSwapHandler {
119
- constructor(chainService, balanceService) {
120
+ constructor(chainService, balanceService, feeService) {
120
121
  this.swapBaseHandler = new SwapBaseHandler({
121
122
  chainService,
122
123
  balanceService,
124
+ feeService,
123
125
  providerName: 'SimpleSwap',
124
126
  providerSlug: SwapProviderId.SIMPLE_SWAP
125
127
  });
@@ -414,11 +416,29 @@ export class SimpleSwapHandler {
414
416
  });
415
417
  extrinsic = submittableExtrinsic;
416
418
  } else {
419
+ const feeInfo = await this.swapBaseHandler.feeService.subscribeChainFee(getId(), chainInfo.slug, 'evm');
417
420
  if (_isNativeToken(fromAsset)) {
418
- const [transactionConfig] = await getEVMTransactionObject(chainInfo, address, addressFrom, quote.fromAmount, false, this.chainService.getEvmApi(chainInfo.slug));
421
+ const [transactionConfig] = await getEVMTransactionObject({
422
+ evmApi: this.chainService.getEvmApi(chainInfo.slug),
423
+ transferAll: false,
424
+ value: quote.fromAmount,
425
+ from: address,
426
+ to: addressFrom,
427
+ chain: chainInfo.slug,
428
+ feeInfo
429
+ });
419
430
  extrinsic = transactionConfig;
420
431
  } else {
421
- const [transactionConfig] = await getERC20TransactionObject(_getContractAddressOfToken(fromAsset), chainInfo, address, addressFrom, quote.fromAmount, false, this.chainService.getEvmApi(chainInfo.slug));
432
+ const [transactionConfig] = await getERC20TransactionObject({
433
+ assetAddress: _getContractAddressOfToken(fromAsset),
434
+ chain: chainInfo.slug,
435
+ evmApi: this.chainService.getEvmApi(chainInfo.slug),
436
+ feeInfo,
437
+ from: address,
438
+ to: addressFrom,
439
+ value: quote.fromAmount,
440
+ transferAll: false
441
+ });
422
442
  extrinsic = transactionConfig;
423
443
  }
424
444
  }
@@ -138,28 +138,28 @@ export class SwapService {
138
138
  _SUPPORTED_SWAP_PROVIDERS.forEach(providerId => {
139
139
  switch (providerId) {
140
140
  case SwapProviderId.CHAIN_FLIP_TESTNET:
141
- this.handlers[providerId] = new ChainflipSwapHandler(this.chainService, this.state.balanceService);
141
+ this.handlers[providerId] = new ChainflipSwapHandler(this.chainService, this.state.balanceService, this.state.feeService);
142
142
  break;
143
143
  case SwapProviderId.CHAIN_FLIP_MAINNET:
144
- this.handlers[providerId] = new ChainflipSwapHandler(this.chainService, this.state.balanceService, false);
144
+ this.handlers[providerId] = new ChainflipSwapHandler(this.chainService, this.state.balanceService, this.state.feeService, false);
145
145
  break;
146
146
  case SwapProviderId.HYDRADX_TESTNET:
147
- this.handlers[providerId] = new HydradxHandler(this.chainService, this.state.balanceService);
147
+ this.handlers[providerId] = new HydradxHandler(this.chainService, this.state.balanceService, this.state.feeService);
148
148
  break;
149
149
  case SwapProviderId.HYDRADX_MAINNET:
150
- this.handlers[providerId] = new HydradxHandler(this.chainService, this.state.balanceService, false);
150
+ this.handlers[providerId] = new HydradxHandler(this.chainService, this.state.balanceService, this.state.feeService, false);
151
151
  break;
152
152
  case SwapProviderId.POLKADOT_ASSET_HUB:
153
- this.handlers[providerId] = new AssetHubSwapHandler(this.chainService, this.state.balanceService, 'statemint');
153
+ this.handlers[providerId] = new AssetHubSwapHandler(this.chainService, this.state.balanceService, this.state.feeService, 'statemint');
154
154
  break;
155
155
  case SwapProviderId.KUSAMA_ASSET_HUB:
156
- this.handlers[providerId] = new AssetHubSwapHandler(this.chainService, this.state.balanceService, 'statemine');
156
+ this.handlers[providerId] = new AssetHubSwapHandler(this.chainService, this.state.balanceService, this.state.feeService, 'statemine');
157
157
  break;
158
158
  case SwapProviderId.ROCOCO_ASSET_HUB:
159
- this.handlers[providerId] = new AssetHubSwapHandler(this.chainService, this.state.balanceService, 'rococo_assethub');
159
+ this.handlers[providerId] = new AssetHubSwapHandler(this.chainService, this.state.balanceService, this.state.feeService, 'rococo_assethub');
160
160
  break;
161
161
  case SwapProviderId.SIMPLE_SWAP:
162
- this.handlers[providerId] = new SimpleSwapHandler(this.chainService, this.state.balanceService);
162
+ this.handlers[providerId] = new SimpleSwapHandler(this.chainService, this.state.balanceService, this.state.feeService);
163
163
  break;
164
164
  default:
165
165
  throw new Error('Unsupported provider');
@@ -18,8 +18,10 @@ import { BasicTxErrorType, YieldPoolType } from '@subwallet/extension-base/types
18
18
  import { anyNumberToBN, pairToAccount, reformatAddress } from '@subwallet/extension-base/utils';
19
19
  import { mergeTransactionAndSignature } from '@subwallet/extension-base/utils/eth/mergeTransactionAndSignature';
20
20
  import { isContractAddress, parseContractInput } from '@subwallet/extension-base/utils/eth/parseTransaction';
21
+ import { getId } from '@subwallet/extension-base/utils/getId';
21
22
  import { BN_ZERO } from '@subwallet/extension-base/utils/number';
22
23
  import keyring from '@subwallet/ui-keyring';
24
+ import BigN from 'bignumber.js';
23
25
  import { addHexPrefix } from 'ethereumjs-util';
24
26
  import { ethers } from 'ethers';
25
27
  import EventEmitter from 'eventemitter3';
@@ -83,6 +85,8 @@ export default class TransactionService {
83
85
  checkSupportForAction(validationResponse, blockedActionsMap);
84
86
  }
85
87
  const transaction = transactionInput.transaction;
88
+ const nativeTokenInfo = this.state.chainService.getNativeTokenInfo(chain);
89
+ const tokenPayFeeInfo = transactionInput.nonNativeTokenPayFeeSlug ? this.chainService.getAssetBySlug(transactionInput.nonNativeTokenPayFeeSlug) : undefined;
86
90
 
87
91
  // Check duplicated transaction
88
92
  validationResponse.errors.push(...this.checkDuplicate(transactionInput));
@@ -92,6 +96,7 @@ export default class TransactionService {
92
96
  if (!chainInfo) {
93
97
  validationResponse.errors.push(new TransactionError(BasicTxErrorType.INTERNAL_ERROR, t('Cannot find network')));
94
98
  }
99
+ const substrateApi = this.state.chainService.getSubstrateApi(chainInfo.slug);
95
100
  const evmApi = this.state.chainService.getEvmApi(chainInfo.slug);
96
101
  const tonApi = this.state.chainService.getTonApi(chainInfo.slug);
97
102
  const isNoEvmApi = transaction && !isSubstrateTransaction(transaction) && !isTonTransaction(transaction) && !evmApi; // todo: should split isEvmTx && isNoEvmApi. Because other chains type also has no Evm Api
@@ -101,12 +106,13 @@ export default class TransactionService {
101
106
  }
102
107
 
103
108
  // Estimate fee for transaction
104
- validationResponse.estimateFee = await estimateFeeForTransaction(validationResponse, transaction, chainInfo, evmApi);
109
+ const id = getId();
110
+ const feeInfo = await this.state.feeService.subscribeChainFee(id, chain, 'evm');
111
+ validationResponse.estimateFee = await estimateFeeForTransaction(validationResponse, transaction, chainInfo, evmApi, substrateApi, feeInfo, nativeTokenInfo, tokenPayFeeInfo, transactionInput.isTransferLocalTokenAndPayThatTokenAsFee);
105
112
  const chainInfoMap = this.state.chainService.getChainInfoMap();
106
113
 
107
114
  // Check account signing transaction
108
115
  checkSigningAccountForTransaction(validationResponse, chainInfoMap);
109
- const nativeTokenInfo = this.state.chainService.getNativeTokenInfo(chain);
110
116
  const nativeTokenAvailable = await this.state.balanceService.getTransferableBalance(address, chain, nativeTokenInfo.slug, extrinsicType);
111
117
 
112
118
  // Check available balance against transaction fee
@@ -898,6 +904,13 @@ export default class TransactionService {
898
904
  if (!payload.from) {
899
905
  payload.from = address;
900
906
  }
907
+ if (!payload.estimateGas) {
908
+ if (payload.maxFeePerGas) {
909
+ payload.estimateGas = new BigN(anyNumberToBN(payload.maxFeePerGas).toNumber()).multipliedBy(payload.gas || '0').toFixed(0);
910
+ } else {
911
+ payload.estimateGas = new BigN(anyNumberToBN(payload.gasPrice).toNumber()).multipliedBy(payload.gas || '0').toFixed(0);
912
+ }
913
+ }
901
914
  const isExternal = !!account.isExternal;
902
915
  const isInjected = !!account.isInjected;
903
916
  if (!hasError) {
@@ -1050,10 +1063,15 @@ export default class TransactionService {
1050
1063
  signAndSendSubstrateTransaction({
1051
1064
  address,
1052
1065
  chain,
1066
+ feeCustom,
1053
1067
  id,
1068
+ nonNativeTokenPayFeeSlug,
1054
1069
  transaction,
1055
1070
  url
1056
1071
  }) {
1072
+ var _this$state$chainServ;
1073
+ const tip = (feeCustom === null || feeCustom === void 0 ? void 0 : feeCustom.tip) || '0';
1074
+ const feeAssetId = nonNativeTokenPayFeeSlug ? (_this$state$chainServ = this.state.chainService.getAssetBySlug(nonNativeTokenPayFeeSlug).metadata) === null || _this$state$chainServ === void 0 ? void 0 : _this$state$chainServ.multilocation : undefined;
1057
1075
  const emitter = new EventEmitter();
1058
1076
  const eventData = {
1059
1077
  id,
@@ -1079,7 +1097,9 @@ export default class TransactionService {
1079
1097
  };
1080
1098
  }
1081
1099
  },
1082
- withSignedTransaction: true
1100
+ tip,
1101
+ withSignedTransaction: true,
1102
+ assetId: feeAssetId
1083
1103
  };
1084
1104
 
1085
1105
  // if (_isRuntimeUpdated(signedExtensions)) {
@@ -1,11 +1,11 @@
1
1
  import { ChainType, ExtrinsicDataTypeMap, ExtrinsicStatus, ExtrinsicType, FeeData, ValidateTransactionResponse } from '@subwallet/extension-base/background/KoniTypes';
2
2
  import { TonTransactionConfig } from '@subwallet/extension-base/services/balance-service/transfer/ton-transfer';
3
- import { BaseRequestSign } from '@subwallet/extension-base/types';
3
+ import { BaseRequestSign, TransactionFee } from '@subwallet/extension-base/types';
4
4
  import EventEmitter from 'eventemitter3';
5
5
  import { TransactionConfig } from 'web3-core';
6
6
  import { SubmittableExtrinsic } from '@polkadot/api/promise/types';
7
7
  import { EventRecord } from '@polkadot/types/interfaces';
8
- export interface SWTransaction extends ValidateTransactionResponse, Partial<Pick<BaseRequestSign, 'ignoreWarnings'>> {
8
+ export interface SWTransaction extends ValidateTransactionResponse, Partial<Pick<BaseRequestSign, 'ignoreWarnings'>>, TransactionFee {
9
9
  id: string;
10
10
  url?: string;
11
11
  isInternal: boolean;
@@ -25,17 +25,18 @@ export interface SWTransaction extends ValidateTransactionResponse, Partial<Pick
25
25
  }
26
26
  export declare type SWTransactionResult = Omit<SWTransaction, 'transaction' | 'additionalValidator' | 'eventsHandler'>;
27
27
  declare type SwInputBase = Pick<SWTransaction, 'address' | 'url' | 'data' | 'extrinsicType' | 'chain' | 'chainType' | 'ignoreWarnings' | 'transferNativeAmount'> & Partial<Pick<SWTransaction, 'additionalValidator' | 'eventsHandler'>>;
28
- export interface SWTransactionInput extends SwInputBase, Partial<Pick<SWTransaction, 'estimateFee'>> {
28
+ export interface SWTransactionInput extends SwInputBase, Partial<Pick<SWTransaction, 'estimateFee'>>, TransactionFee {
29
29
  id?: string;
30
30
  transaction?: SWTransaction['transaction'] | null;
31
31
  warnings?: SWTransaction['warnings'];
32
32
  errors?: SWTransaction['errors'];
33
33
  edAsWarning?: boolean;
34
34
  isTransferAll?: boolean;
35
+ isTransferLocalTokenAndPayThatTokenAsFee?: boolean;
35
36
  resolveOnDone?: boolean;
36
37
  skipFeeValidation?: boolean;
37
38
  }
38
- export declare type SWTransactionResponse = SwInputBase & Pick<SWTransaction, 'warnings' | 'errors'> & Partial<Pick<SWTransaction, 'id' | 'extrinsicHash' | 'status' | 'estimateFee'>>;
39
+ export declare type SWTransactionResponse = SwInputBase & Pick<SWTransaction, 'warnings' | 'errors'> & Partial<Pick<SWTransaction, 'id' | 'extrinsicHash' | 'status' | 'estimateFee'>> & TransactionFee;
39
40
  export declare type ValidateTransactionResponseInput = SWTransactionInput;
40
41
  export declare type TransactionEmitter = EventEmitter<TransactionEventMap>;
41
42
  export interface TransactionEventResponse extends ValidateTransactionResponse {
@@ -0,0 +1,25 @@
1
+ import { BaseRequestSign } from '@subwallet/extension-base/types';
2
+ import { FeeChainType, FeeDetail, TransactionFee } from '../fee';
3
+ export interface RequestSubscribeTransfer extends TransactionFee {
4
+ address: string;
5
+ chain: string;
6
+ token: string;
7
+ destChain: string;
8
+ }
9
+ export interface ResponseSubscribeTransfer {
10
+ id: string;
11
+ maxTransferable: string;
12
+ feeOptions: FeeDetail;
13
+ feeType: FeeChainType;
14
+ feePercentageSpecialCase?: number;
15
+ error?: string;
16
+ }
17
+ export interface RequestSubmitTransfer extends BaseRequestSign, TransactionFee {
18
+ chain: string;
19
+ from: string;
20
+ to: string;
21
+ tokenSlug: string;
22
+ transferAll: boolean;
23
+ value: string;
24
+ transferBounceable?: boolean;
25
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ export declare type FeeChainType = 'evm' | 'substrate' | 'ton';
2
+ export interface BaseFeeInfo {
3
+ busyNetwork: boolean;
4
+ type: FeeChainType;
5
+ }
6
+ export interface BaseFeeDetail {
7
+ estimatedFee: string;
8
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,33 +1,61 @@
1
- import BigN from 'bignumber.js';
2
- interface BaseFeeInfo {
3
- busyNetwork: boolean;
4
- }
1
+ import { BaseFeeDetail, BaseFeeInfo, FeeDefaultOption } from '@subwallet/extension-base/types';
5
2
  export interface EvmLegacyFeeInfo extends BaseFeeInfo {
3
+ type: 'evm';
6
4
  gasPrice: string;
7
- maxFeePerGas: undefined;
8
- maxPriorityFeePerGas: undefined;
9
5
  baseGasFee: undefined;
6
+ options: undefined;
7
+ }
8
+ export interface EvmEIP1559FeeOption {
9
+ maxFeePerGas: string;
10
+ maxPriorityFeePerGas: string;
11
+ minWaitTimeEstimate?: number;
12
+ maxWaitTimeEstimate?: number;
13
+ }
14
+ export declare enum FeeOptionKey {
15
+ SLOW = "slow",
16
+ AVERAGE = "average",
17
+ FAST = "fast",
18
+ DEFAULT = "default"
10
19
  }
11
- export interface EvmEIP1995FeeInfo extends BaseFeeInfo {
20
+ export interface EvmEIP1559FeeInfo extends BaseFeeInfo {
21
+ type: 'evm';
12
22
  gasPrice: undefined;
13
- maxFeePerGas: BigN;
14
- maxPriorityFeePerGas: BigN;
15
- baseGasFee: BigN;
23
+ baseGasFee: string;
24
+ options: {
25
+ [FeeOptionKey.SLOW]: EvmEIP1559FeeOption;
26
+ [FeeOptionKey.AVERAGE]: EvmEIP1559FeeOption;
27
+ [FeeOptionKey.FAST]: EvmEIP1559FeeOption;
28
+ [FeeOptionKey.DEFAULT]: FeeDefaultOption;
29
+ };
16
30
  }
17
- export declare type EvmFeeInfo = EvmLegacyFeeInfo | EvmEIP1995FeeInfo;
31
+ export declare type EvmFeeInfo = EvmLegacyFeeInfo | EvmEIP1559FeeInfo;
18
32
  export interface EvmLegacyFeeInfoCache extends BaseFeeInfo {
33
+ type: 'evm';
19
34
  gasPrice: string;
20
35
  maxFeePerGas: undefined;
21
36
  maxPriorityFeePerGas: undefined;
22
37
  baseGasFee: undefined;
38
+ options: undefined;
23
39
  }
24
- export interface EvmEIP1995FeeInfoCache extends BaseFeeInfo {
40
+ export interface EvmEIP1559FeeInfoCache extends BaseFeeInfo {
41
+ type: 'evm';
25
42
  gasPrice: undefined;
26
- maxFeePerGas: string;
27
- maxPriorityFeePerGas: string;
28
43
  baseGasFee: string;
44
+ options: {
45
+ [FeeOptionKey.SLOW]: EvmEIP1559FeeOption;
46
+ [FeeOptionKey.AVERAGE]: EvmEIP1559FeeOption;
47
+ [FeeOptionKey.FAST]: EvmEIP1559FeeOption;
48
+ [FeeOptionKey.DEFAULT]: FeeDefaultOption;
49
+ };
29
50
  }
30
- export declare type EvmFeeInfoCache = EvmLegacyFeeInfoCache | EvmEIP1995FeeInfoCache;
51
+ export interface EvmLegacyFeeDetail extends EvmLegacyFeeInfo, BaseFeeDetail {
52
+ gasLimit: string;
53
+ }
54
+ export interface EvmEIP1559FeeDetail extends EvmEIP1559FeeInfo, BaseFeeDetail {
55
+ gasLimit: string;
56
+ }
57
+ export declare type EvmFeeInfoCache = EvmLegacyFeeInfoCache | EvmEIP1559FeeInfoCache;
58
+ export declare type EvmFeeDetail = EvmLegacyFeeDetail | EvmEIP1559FeeDetail;
31
59
  export interface InfuraFeeDetail {
32
60
  suggestedMaxPriorityFeePerGas: string;
33
61
  suggestedMaxFeePerGas: string;
@@ -46,4 +74,6 @@ export interface InfuraFeeInfo {
46
74
  priorityFeeTrend: 'down' | 'up';
47
75
  baseFeeTrend: 'down' | 'up';
48
76
  }
49
- export {};
77
+ export interface InfuraThresholdInfo {
78
+ busyThreshold: string;
79
+ }
package/types/fee/evm.js CHANGED
@@ -1 +1,10 @@
1
- export {};
1
+ // Copyright 2019-2022 @subwallet/extension-base
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export let FeeOptionKey;
5
+ (function (FeeOptionKey) {
6
+ FeeOptionKey["SLOW"] = "slow";
7
+ FeeOptionKey["AVERAGE"] = "average";
8
+ FeeOptionKey["FAST"] = "fast";
9
+ FeeOptionKey["DEFAULT"] = "default";
10
+ })(FeeOptionKey || (FeeOptionKey = {}));
@@ -1,2 +1,5 @@
1
+ export * from './base';
1
2
  export * from './evm';
2
- export * from './fee';
3
+ export * from './option';
4
+ export * from './subscription';
5
+ export * from './substrate';
@@ -1,5 +1,8 @@
1
1
  // Copyright 2019-2022 @subwallet/extension-base
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
+ export * from "./base.js";
4
5
  export * from "./evm.js";
5
- export * from "./fee.js";
6
+ export * from "./option.js";
7
+ export * from "./subscription.js";
8
+ export * from "./substrate.js";
@@ -0,0 +1,8 @@
1
+ import { FeeCustom } from '@subwallet/extension-base/types';
2
+ export declare type FeeDefaultOption = 'slow' | 'average' | 'fast';
3
+ export declare type FeeOption = FeeDefaultOption | 'custom';
4
+ export declare type TransactionFee = {
5
+ feeOption?: FeeOption;
6
+ feeCustom?: FeeCustom;
7
+ nonNativeTokenPayFeeSlug?: string;
8
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,12 @@
1
+ import { BehaviorSubject } from 'rxjs';
2
+ import { EvmEIP1559FeeOption, EvmFeeDetail, EvmFeeInfo } from './evm';
3
+ import { SubstrateFeeDetail, SubstrateFeeInfo, SubstrateTipInfo } from './substrate';
4
+ import { TonFeeDetail, TonFeeInfo, TonTipInfo } from './ton';
5
+ export declare type FeeInfo = EvmFeeInfo | SubstrateFeeInfo | TonFeeInfo;
6
+ export declare type FeeDetail = EvmFeeDetail | SubstrateFeeDetail | TonFeeDetail;
7
+ export declare type FeeCustom = EvmEIP1559FeeOption | SubstrateTipInfo | TonTipInfo;
8
+ export interface FeeSubscription {
9
+ observer: BehaviorSubject<FeeInfo | undefined>;
10
+ subscription: Record<string, VoidFunction>;
11
+ unsubscribe: VoidFunction;
12
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,15 @@
1
+ import { BaseFeeDetail, BaseFeeInfo } from './base';
2
+ import { FeeDefaultOption } from './option';
3
+ export interface SubstrateTipInfo {
4
+ tip: string;
5
+ }
6
+ export interface SubstrateFeeInfo extends BaseFeeInfo {
7
+ type: 'substrate';
8
+ options: {
9
+ slow: SubstrateTipInfo;
10
+ average: SubstrateTipInfo;
11
+ fast: SubstrateTipInfo;
12
+ default: FeeDefaultOption;
13
+ };
14
+ }
15
+ export declare type SubstrateFeeDetail = SubstrateFeeInfo & BaseFeeDetail;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,18 @@
1
+ import { BaseFeeDetail, BaseFeeInfo } from './base';
2
+ import { FeeDefaultOption } from './option';
3
+ /** @deprecated */
4
+ export interface TonTipInfo {
5
+ tip: string;
6
+ }
7
+ /** @deprecated */
8
+ export interface TonFeeInfo extends BaseFeeInfo {
9
+ type: 'ton';
10
+ options: {
11
+ slow: TonTipInfo;
12
+ average: TonTipInfo;
13
+ fast: TonTipInfo;
14
+ default: FeeDefaultOption;
15
+ };
16
+ }
17
+ /** @deprecated */
18
+ export declare type TonFeeDetail = TonFeeInfo & BaseFeeDetail;
@@ -0,0 +1 @@
1
+ export {};
@@ -1,4 +1,4 @@
1
- import { TransactionWarningType } from '@subwallet/extension-base/types';
1
+ import { TransactionFee, TransactionWarningType } from '@subwallet/extension-base/types';
2
2
  export declare type BaseRequestSign = {
3
3
  ignoreWarnings?: TransactionWarningType[];
4
4
  };
@@ -11,14 +11,24 @@ export interface RequestBaseTransfer {
11
11
  transferAll?: boolean;
12
12
  transferBounceable?: boolean;
13
13
  }
14
- export interface RequestCheckTransfer extends RequestBaseTransfer {
14
+ export interface RequestCheckTransfer extends RequestBaseTransfer, TransactionFee {
15
15
  networkKey: string;
16
16
  }
17
17
  export declare type RequestTransfer = InternalRequestSign<RequestCheckTransfer>;
18
- export interface RequestCheckCrossChainTransfer extends RequestBaseTransfer {
18
+ export interface RequestCheckCrossChainTransfer extends RequestBaseTransfer, TransactionFee {
19
19
  value: string;
20
20
  originNetworkKey: string;
21
21
  destinationNetworkKey: string;
22
22
  showExtraWarning?: boolean;
23
23
  }
24
24
  export declare type RequestCrossChainTransfer = InternalRequestSign<RequestCheckCrossChainTransfer>;
25
+ export interface RequestGetTokensCanPayFee {
26
+ address: string;
27
+ chain: string;
28
+ feeAmount?: string;
29
+ }
30
+ export interface RequestGetAmountForPair {
31
+ nativeTokenFeeAmount: string;
32
+ nativeTokenSlug: string;
33
+ toTokenSlug: string;
34
+ }