@subwallet/extension-base 1.1.53-beta.0 → 1.1.55-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 (44) hide show
  1. package/background/KoniTypes.d.ts +12 -0
  2. package/background/types.d.ts +2 -0
  3. package/cjs/koni/background/handlers/Extension.js +129 -102
  4. package/cjs/koni/background/handlers/State.js +3 -20
  5. package/cjs/packageInfo.js +1 -1
  6. package/cjs/services/chain-service/index.js +5 -9
  7. package/cjs/services/chain-service/utils/index.js +8 -2
  8. package/cjs/services/chain-service/utils/patch.js +1 -1
  9. package/cjs/services/migration-service/scripts/MigrateTransactionHistoryBySymbol.js +4 -17
  10. package/cjs/services/migration-service/scripts/databases/MigrateAssetSetting.js +4 -17
  11. package/cjs/services/migration-service/scripts/index.js +3 -3
  12. package/cjs/services/swap-service/handler/base-handler.js +20 -2
  13. package/cjs/services/swap-service/handler/hydradx-handler.js +106 -19
  14. package/cjs/services/swap-service/index.js +1 -0
  15. package/koni/background/handlers/Extension.d.ts +1 -0
  16. package/koni/background/handlers/Extension.js +26 -0
  17. package/koni/background/handlers/State.js +4 -21
  18. package/package.json +10 -10
  19. package/packageInfo.js +1 -1
  20. package/services/chain-service/index.d.ts +1 -1
  21. package/services/chain-service/index.js +6 -10
  22. package/services/chain-service/utils/index.d.ts +1 -0
  23. package/services/chain-service/utils/index.js +4 -0
  24. package/services/chain-service/utils/patch.js +1 -1
  25. package/services/migration-service/scripts/MigrateTransactionHistoryBySymbol.js +4 -17
  26. package/services/migration-service/scripts/databases/MigrateAssetSetting.js +4 -17
  27. package/services/migration-service/scripts/index.js +3 -3
  28. package/services/swap-service/handler/base-handler.js +20 -2
  29. package/services/swap-service/handler/hydradx-handler.d.ts +2 -2
  30. package/services/swap-service/handler/hydradx-handler.js +106 -19
  31. package/services/swap-service/index.js +1 -0
  32. package/types/swap/index.d.ts +2 -0
  33. package/cjs/koni/api/dotsama/balance.js +0 -464
  34. package/cjs/koni/api/nft/ordinal_nft/utils.js +0 -41
  35. package/cjs/services/balance-service/helpers/subscribe/balance.js +0 -140
  36. package/cjs/services/chain-service/utils.js +0 -506
  37. package/cjs/services/migration-service/scripts/MigrateEthProvider.js +0 -17
  38. package/cjs/services/migration-service/scripts/MigratePioneerProvider.js +0 -17
  39. package/cjs/services/migration-service/scripts/MigrateProvider.js +0 -29
  40. package/cjs/services/storage-service/index.js +0 -241
  41. package/cjs/types/balance.js +0 -1
  42. package/cjs/types.js +0 -1
  43. package/cjs/utils/address.js +0 -34
  44. package/cjs/utils/keyring.js +0 -57
@@ -9,7 +9,7 @@ import { MantaPrivateHandler } from '@subwallet/extension-base/services/chain-se
9
9
  import { SubstrateChainHandler } from '@subwallet/extension-base/services/chain-service/handler/SubstrateChainHandler';
10
10
  import { _CHAIN_VALIDATION_ERROR } from '@subwallet/extension-base/services/chain-service/handler/types';
11
11
  import { _ChainConnectionStatus, _CUSTOM_PREFIX, _NFT_CONTRACT_STANDARDS, _SMART_CONTRACT_STANDARDS } from '@subwallet/extension-base/services/chain-service/types';
12
- import { _isAssetAutoEnable, _isAssetFungibleToken, _isChainEnabled, _isCustomAsset, _isCustomChain, _isCustomProvider, _isEqualContractAddress, _isEqualSmartContractAsset, _isMantaZkAsset, _isPureEvmChain, _isPureSubstrateChain, _parseAssetRefKey, fetchPatchData, randomizeProvider, updateLatestChainInfo } from '@subwallet/extension-base/services/chain-service/utils';
12
+ import { _isAssetAutoEnable, _isAssetCanPayTxFee, _isAssetFungibleToken, _isChainEnabled, _isCustomAsset, _isCustomChain, _isCustomProvider, _isEqualContractAddress, _isEqualSmartContractAsset, _isMantaZkAsset, _isPureEvmChain, _isPureSubstrateChain, _parseAssetRefKey, fetchPatchData, randomizeProvider, updateLatestChainInfo } from '@subwallet/extension-base/services/chain-service/utils';
13
13
  import AssetSettingStore from '@subwallet/extension-base/stores/AssetSetting';
14
14
  import { addLazy, fetchStaticData, filterAssetsByChainAndType, MODULE_SUPPORT } from '@subwallet/extension-base/utils';
15
15
  import { BehaviorSubject, Subject } from 'rxjs';
@@ -609,15 +609,6 @@ export class ChainService {
609
609
  }
610
610
  }));
611
611
  }
612
- async initSingleApi(slug) {
613
- const chainInfoMap = this.getChainInfoMap();
614
- const chainStateMap = this.getChainStateMap();
615
- if (!chainStateMap[slug].active) {
616
- return false;
617
- }
618
- await this.initApiForChain(chainInfoMap[slug]);
619
- return true;
620
- }
621
612
  async initApiForChain(chainInfo) {
622
613
  const {
623
614
  endpoint,
@@ -1634,4 +1625,9 @@ export class ChainService {
1634
1625
  }
1635
1626
  return result;
1636
1627
  }
1628
+ getFeeTokensByChain(chainSlug) {
1629
+ return Object.values(this.getAssetRegistry()).filter(chainAsset => {
1630
+ return chainAsset.originChain === chainSlug && (chainAsset.assetType === _AssetType.NATIVE || _isAssetCanPayTxFee(chainAsset));
1631
+ }).map(chainAsset => chainAsset.slug);
1632
+ }
1637
1633
  }
@@ -75,6 +75,7 @@ export declare function randomizeProvider(providers: Record<string, string>, exc
75
75
  providerKey: string;
76
76
  providerValue: string;
77
77
  };
78
+ export declare function _isAssetCanPayTxFee(chainAsset: _ChainAsset): boolean;
78
79
  export declare function updateLatestChainInfo(currentDataMap: _DataMap, latestChainInfoList: _ChainInfo[]): {
79
80
  storedChainInfoList: IChain[];
80
81
  needUpdateChainApiList: _ChainInfo[];
@@ -398,6 +398,10 @@ export function randomizeProvider(providers, excludedKeys) {
398
398
  providerValue: selectedProviderValue
399
399
  };
400
400
  }
401
+ export function _isAssetCanPayTxFee(chainAsset) {
402
+ var _ref, _chainAsset$metadata;
403
+ return (_ref = (_chainAsset$metadata = chainAsset.metadata) === null || _chainAsset$metadata === void 0 ? void 0 : _chainAsset$metadata.canPayTxFee) !== null && _ref !== void 0 ? _ref : false;
404
+ }
401
405
  export function updateLatestChainInfo(currentDataMap, latestChainInfoList) {
402
406
  const currentChainInfoMap = currentDataMap.chainInfoMap;
403
407
  const currentChainStateMap = currentDataMap.chainStateMap;
@@ -5,7 +5,7 @@ import fetch from 'cross-fetch';
5
5
  const PRODUCTION_BRANCHES = ['master', 'webapp', 'webapp-dev'];
6
6
  const branchName = process.env.BRANCH_NAME || 'subwallet-dev';
7
7
  const fetchDomain = PRODUCTION_BRANCHES.indexOf(branchName) > -1 ? 'https://chain-list-assets.subwallet.app' : 'https://dev.sw-chain-list-assets.pages.dev';
8
- const ChainListVersion = '0.2.55-beta.3';
8
+ const ChainListVersion = '0.2.55';
9
9
  export async function fetchPatchData(slug) {
10
10
  try {
11
11
  const fetchPromise = fetch(`${fetchDomain}/patch/${ChainListVersion}/${slug}`);
@@ -7,23 +7,10 @@ export default class MigrateTransactionHistoryBySymbol extends BaseMigrationJob
7
7
  const state = this.state;
8
8
  try {
9
9
  const changeSlugsMap = {
10
- 'ethereum-ERC20-WFTM-0x4E15361FD6b4BB609Fa63C81A2be19d873717870': 'ethereum-ERC20-FTM-0x4E15361FD6b4BB609Fa63C81A2be19d873717870',
11
- 'moonbeam-ERC20-CSG-0x2Dfc76901bB2ac2A5fA5fc479590A490BBB10a5F': 'moonbeam-ERC20-CGS-0x2Dfc76901bB2ac2A5fA5fc479590A490BBB10a5F',
12
- 'astar-LOCAL-aUSD': 'astar-LOCAL-aSEED',
13
- 'astarEvm-ERC20-aUSD-0xfFFFFfFF00000000000000010000000000000001': 'astarEvm-ERC20-aSEED-0xfFFFFfFF00000000000000010000000000000001',
14
- 'moonriver-LOCAL-xcaUSD': 'moonriver-LOCAL-xcaSeed',
15
- 'moonriver-LOCAL-xckBTC': 'moonriver-LOCAL-xcKBTC',
16
- 'bifrost-LOCAL-aUSD': 'bifrost-LOCAL-KUSD',
17
- 'calamari-LOCAL-aUSD': 'calamari-LOCAL-AUSD',
18
- 'shiden-LOCAL-aUSD': 'shiden-LOCAL-aSEED',
19
- 'shidenEvm-ERC20-aUSD-0xfFFfFFfF00000000000000010000000000000000': 'shidenEvm-ERC20-aSEED-0xfFFfFFfF00000000000000010000000000000000',
20
- 'ethereum_goerli-NATIVE-GoerliETH': 'ethereum_goerli-NATIVE-ETH',
21
- 'binance_test-NATIVE-BNB': 'binance_test-NATIVE-tBNB',
22
- 'pangolin-LOCAL-CKTON': 'pangolin-LOCAL-PKTON',
23
- 'zeta_test-NATIVE-aZETA': 'zeta_test-NATIVE-ZETA',
24
- 'origintrail-NATIVE-OTP': 'origintrail-NATIVE-NEURO',
25
- 'moonbeam-LOCAL-xciBTC': 'moonbeam-LOCAL-xcIBTC',
26
- 'tomochain-NATIVE-TOMO': 'tomochain-NATIVE-VIC'
10
+ 'moonbeam-LOCAL-xcaUSD': 'moonbeam-LOCAL-xcaSEED',
11
+ 'calamari-LOCAL-AUSD': 'calamari-LOCAL-aSEED',
12
+ 'moonriver-LOCAL-xcaSeed': 'moonriver-LOCAL-xcaSEED',
13
+ 'bifrost-LOCAL-KUSD': 'bifrost-LOCAL-aSEED'
27
14
  };
28
15
  const allTxs = [];
29
16
  await Promise.all(Object.entries(changeSlugsMap).map(async ([oldSlug, newSlug], i) => {
@@ -6,23 +6,10 @@ export default class MigrateAssetSetting extends BaseMigrationJob {
6
6
  async run() {
7
7
  try {
8
8
  const changeSlugsMap = {
9
- 'ethereum-ERC20-WFTM-0x4E15361FD6b4BB609Fa63C81A2be19d873717870': 'ethereum-ERC20-FTM-0x4E15361FD6b4BB609Fa63C81A2be19d873717870',
10
- 'moonbeam-ERC20-CSG-0x2Dfc76901bB2ac2A5fA5fc479590A490BBB10a5F': 'moonbeam-ERC20-CGS-0x2Dfc76901bB2ac2A5fA5fc479590A490BBB10a5F',
11
- 'astar-LOCAL-aUSD': 'astar-LOCAL-aSEED',
12
- 'astarEvm-ERC20-aUSD-0xfFFFFfFF00000000000000010000000000000001': 'astarEvm-ERC20-aSEED-0xfFFFFfFF00000000000000010000000000000001',
13
- 'moonriver-LOCAL-xcaUSD': 'moonriver-LOCAL-xcaSeed',
14
- 'moonriver-LOCAL-xckBTC': 'moonriver-LOCAL-xcKBTC',
15
- 'bifrost-LOCAL-aUSD': 'bifrost-LOCAL-KUSD',
16
- 'calamari-LOCAL-aUSD': 'calamari-LOCAL-AUSD',
17
- 'shiden-LOCAL-aUSD': 'shiden-LOCAL-aSEED',
18
- 'shidenEvm-ERC20-aUSD-0xfFFfFFfF00000000000000010000000000000000': 'shidenEvm-ERC20-aSEED-0xfFFfFFfF00000000000000010000000000000000',
19
- 'ethereum_goerli-NATIVE-GoerliETH': 'ethereum_goerli-NATIVE-ETH',
20
- 'binance_test-NATIVE-BNB': 'binance_test-NATIVE-tBNB',
21
- 'pangolin-LOCAL-CKTON': 'pangolin-LOCAL-PKTON',
22
- 'zeta_test-NATIVE-aZETA': 'zeta_test-NATIVE-ZETA',
23
- 'origintrail-NATIVE-OTP': 'origintrail-NATIVE-NEURO',
24
- 'moonbeam-LOCAL-xciBTC': 'moonbeam-LOCAL-xcIBTC',
25
- 'tomochain-NATIVE-TOMO': 'tomochain-NATIVE-VIC'
9
+ 'moonbeam-LOCAL-xcaUSD': 'moonbeam-LOCAL-xcaSEED',
10
+ 'calamari-LOCAL-AUSD': 'calamari-LOCAL-aSEED',
11
+ 'moonriver-LOCAL-xcaSeed': 'moonriver-LOCAL-xcaSEED',
12
+ 'bifrost-LOCAL-KUSD': 'bifrost-LOCAL-aSEED'
26
13
  };
27
14
  const assetSetting = await this.state.chainService.getAssetSettings();
28
15
  const migratedAssetSetting = {};
@@ -47,9 +47,9 @@ export default {
47
47
  '1.1.26-01': MigratePolygonUSDCProvider,
48
48
  '1.1.28-01': MigrateEarningVersion,
49
49
  '1.1.41-01': DeleteChainStaking,
50
- '1.1.44-01': MigrateAssetSetting,
51
- '1.1.45-01': MigrateTransactionHistoryBySymbol,
52
- '1.1.46-01': AutoEnableSomeTokens
50
+ '1.1.46-01': AutoEnableSomeTokens,
51
+ '1.1.53-01': MigrateAssetSetting,
52
+ '1.1.53-02': MigrateTransactionHistoryBySymbol
53
53
  // [`${EVERYTIME}-1.1.42-02`]: MigrateTransactionHistoryBySymbol
54
54
  // [`${EVERYTIME}-1`]: AutoEnableChainsTokens
55
55
  };
@@ -96,7 +96,19 @@ export class SwapBaseHandler {
96
96
  return Promise.resolve([]);
97
97
  }
98
98
  async validateSetFeeTokenStep(params, stepIndex) {
99
- return Promise.resolve([]);
99
+ if (!params.selectedQuote) {
100
+ return Promise.resolve([new TransactionError(BasicTxErrorType.INTERNAL_ERROR)]);
101
+ }
102
+ const feeInfo = params.process.totalFee[stepIndex];
103
+ const feeAmount = feeInfo.feeComponent[0];
104
+ const feeTokenInfo = this.chainService.getAssetBySlug(feeInfo.defaultFeeToken);
105
+ const feeTokenBalance = await this.balanceService.getTokenFreeBalance(params.address, feeTokenInfo.originChain, feeTokenInfo.slug);
106
+ const bnFeeTokenBalance = new BigNumber(feeTokenBalance.value);
107
+ const bnFeeAmount = new BigNumber(feeAmount.amount);
108
+ if (bnFeeAmount.gte(bnFeeTokenBalance)) {
109
+ return Promise.resolve([new TransactionError(BasicTxErrorType.NOT_ENOUGH_BALANCE)]);
110
+ }
111
+ return [];
100
112
  }
101
113
  async validateSwapStep(params, isXcmOk, stepIndex) {
102
114
  if (!params.selectedQuote) {
@@ -119,9 +131,15 @@ export class SwapBaseHandler {
119
131
  const [feeTokenBalance, fromAssetBalance] = await Promise.all([this.balanceService.getTokenFreeBalance(params.address, feeTokenInfo.originChain, feeTokenInfo.slug), this.balanceService.getTokenFreeBalance(params.address, fromAsset.originChain, fromAsset.slug)]);
120
132
  const bnFeeTokenBalance = new BigNumber(feeTokenBalance.value);
121
133
  const bnFromAssetBalance = new BigNumber(fromAssetBalance.value);
122
- if (bnFeeTokenBalance.lte(new BigNumber(networkFee.amount))) {
134
+ const bnFeeAmount = new BigNumber(networkFee.amount);
135
+ if (bnFeeTokenBalance.lte(bnFeeAmount)) {
123
136
  return Promise.resolve([new TransactionError(BasicTxErrorType.NOT_ENOUGH_BALANCE, `You don't have enough ${feeTokenInfo.symbol} (${feeTokenChain.name}) to pay transaction fee`)]);
124
137
  }
138
+ if (fromAsset.slug === feeTokenInfo.slug) {
139
+ if (bnFromAssetBalance.lte(bnFeeAmount.plus(bnAmount))) {
140
+ return Promise.resolve([new TransactionError(BasicTxErrorType.NOT_ENOUGH_BALANCE, `Insufficient balance. Deposit ${fromAsset.symbol} and try again.`)]);
141
+ }
142
+ }
125
143
  if (params.selectedQuote.minSwap) {
126
144
  const minProtocolSwap = new BigNumber(params.selectedQuote.minSwap);
127
145
  if (!isXcmOk && bnFromAssetBalance.lte(minProtocolSwap)) {
@@ -1,4 +1,3 @@
1
- import { COMMON_CHAIN_SLUGS } from '@subwallet/chain-list';
2
1
  import { SwapError } from '@subwallet/extension-base/background/errors/SwapError';
3
2
  import { TransactionError } from '@subwallet/extension-base/background/errors/TransactionError';
4
3
  import { BalanceService } from '@subwallet/extension-base/services/balance-service';
@@ -13,7 +12,7 @@ export declare class HydradxHandler implements SwapBaseInterface {
13
12
  isReady: boolean;
14
13
  constructor(chainService: ChainService, balanceService: BalanceService, isTestnet?: boolean);
15
14
  init(): Promise<void>;
16
- get chain(): COMMON_CHAIN_SLUGS.HYDRADX | COMMON_CHAIN_SLUGS.HYDRADX_TESTNET;
15
+ chain: () => string;
17
16
  get chainService(): ChainService;
18
17
  get balanceService(): BalanceService;
19
18
  get providerInfo(): import("@subwallet/extension-base/types/swap").SwapProvider;
@@ -27,6 +26,7 @@ export declare class HydradxHandler implements SwapBaseInterface {
27
26
  private parseSwapPath;
28
27
  getSwapQuote(request: SwapRequest): Promise<SwapQuote | SwapError>;
29
28
  handleXcmStep(params: SwapSubmitParams): Promise<SwapSubmitStepData>;
29
+ handleSetFeeStep(params: SwapSubmitParams): Promise<SwapSubmitStepData>;
30
30
  handleSubmitStep(params: SwapSubmitParams): Promise<SwapSubmitStepData>;
31
31
  handleSwapProcess(params: SwapSubmitParams): Promise<SwapSubmitStepData>;
32
32
  validateSwapProcess(params: ValidateSwapProcessParams): Promise<TransactionError[]>;
@@ -19,9 +19,9 @@ const HYDRADX_SUBWALLET_REFERRAL_ACCOUNT = '7PCsCpkgsHdNaZhv79wCCQ5z97uxVbSeSCtD
19
19
  const HYDRADX_TESTNET_SUBWALLET_REFERRAL_CODE = 'ASSETHUB';
20
20
  const HYDRADX_TESTNET_SUBWALLET_REFERRAL_ACCOUNT = '7LCt6dFqtxzdKVB2648jWW9d85doiFfLSbZJDNAMVJNxh5rJ';
21
21
  export class HydradxHandler {
22
+ isTestnet = true;
22
23
  isReady = false;
23
24
  constructor(chainService, balanceService, isTestnet = true) {
24
- // todo: pass in baseHandler from service
25
25
  this.swapBaseHandler = new SwapBaseHandler({
26
26
  balanceService,
27
27
  chainService,
@@ -31,24 +31,24 @@ export class HydradxHandler {
31
31
  this.isTestnet = isTestnet;
32
32
  }
33
33
  async init() {
34
- const chainState = this.chainService.getChainStateByKey(this.chain);
34
+ const chainState = this.chainService.getChainStateByKey(this.chain());
35
35
  if (!chainState.active) {
36
- await this.chainService.enableChain(this.chain);
36
+ await this.chainService.enableChain(this.chain());
37
37
  }
38
- const substrateApi = this.chainService.getSubstrateApi(this.chain);
38
+ const substrateApi = this.chainService.getSubstrateApi(this.chain());
39
39
  await substrateApi.api.isReady;
40
40
  const poolService = new PoolService(substrateApi.api);
41
41
  this.tradeRouter = new TradeRouter(poolService);
42
42
  this.isReady = true;
43
43
  }
44
- get chain() {
44
+ chain = () => {
45
45
  // TODO: check origin chain of tokens in swap pair to determine support
46
46
  if (!this.isTestnet) {
47
47
  return COMMON_CHAIN_SLUGS.HYDRADX;
48
48
  } else {
49
49
  return COMMON_CHAIN_SLUGS.HYDRADX_TESTNET;
50
50
  }
51
- }
51
+ };
52
52
  get chainService() {
53
53
  return this.swapBaseHandler.chainService;
54
54
  }
@@ -120,7 +120,44 @@ export class HydradxHandler {
120
120
  }
121
121
  }
122
122
  async getFeeOptionStep(params) {
123
- return Promise.resolve(undefined);
123
+ if (!params.selectedQuote) {
124
+ return Promise.resolve(undefined);
125
+ }
126
+ const selectedFeeToken = params.selectedQuote.feeInfo.selectedFeeToken;
127
+ if (!selectedFeeToken) {
128
+ return undefined;
129
+ }
130
+ const feeStep = {
131
+ name: 'Set fee token',
132
+ type: SwapStepType.SET_FEE_TOKEN
133
+ };
134
+ try {
135
+ const substrateApi = this.chainService.getSubstrateApi(this.chain());
136
+ const chainApi = await substrateApi.isReady;
137
+ const _currentFeeAssetId = await chainApi.api.query.multiTransactionPayment.accountCurrencyMap(params.request.address);
138
+ const currentFeeAssetId = _currentFeeAssetId.toString();
139
+ const selectedFeeAsset = this.chainService.getAssetBySlug(selectedFeeToken);
140
+ const assetId = _getTokenOnChainAssetId(selectedFeeAsset);
141
+ if (currentFeeAssetId === assetId) {
142
+ return;
143
+ }
144
+ const setFeeTx = chainApi.api.tx.multiTransactionPayment.setCurrency(assetId);
145
+ const _txFee = await setFeeTx.paymentInfo(params.request.address);
146
+ const txFee = _txFee.toPrimitive();
147
+ const fee = {
148
+ feeComponent: [{
149
+ feeType: SwapFeeType.NETWORK_FEE,
150
+ amount: Math.round(txFee.partialFee).toString(),
151
+ tokenSlug: selectedFeeAsset.slug
152
+ }],
153
+ selectedFeeToken: selectedFeeAsset.slug,
154
+ defaultFeeToken: selectedFeeAsset.slug,
155
+ feeOptions: [selectedFeeAsset.slug]
156
+ };
157
+ return [feeStep, fee];
158
+ } catch (e) {
159
+ return undefined;
160
+ }
124
161
  }
125
162
  async getSubmitStep(params) {
126
163
  if (params.selectedQuote) {
@@ -133,7 +170,9 @@ export class HydradxHandler {
133
170
  return Promise.resolve(undefined);
134
171
  }
135
172
  generateOptimalProcess(params) {
136
- return this.swapBaseHandler.generateOptimalProcess(params, [this.getXcmStep, this.getFeeOptionStep, this.getSubmitStep]);
173
+ return this.swapBaseHandler.generateOptimalProcess(params, [this.getXcmStep,
174
+ // this.getFeeOptionStep.bind(this),
175
+ this.getSubmitStep]);
137
176
  }
138
177
  getSwapPathErrors(swapList) {
139
178
  return swapList.reduce((prev, current) => {
@@ -141,7 +180,7 @@ export class HydradxHandler {
141
180
  }, []);
142
181
  }
143
182
  parseSwapPath(swapList) {
144
- const swapAssets = this.chainService.getAssetByChainAndType(this.chain, [_AssetType.NATIVE, _AssetType.LOCAL]);
183
+ const swapAssets = this.chainService.getAssetByChainAndType(this.chain(), [_AssetType.NATIVE, _AssetType.LOCAL]);
145
184
  const swapAssetIdMap = Object.values(swapAssets).reduce((accumulator, asset) => {
146
185
  return {
147
186
  ...accumulator,
@@ -185,7 +224,7 @@ export class HydradxHandler {
185
224
  const toAmount = quoteResponse.amountOut;
186
225
  const minReceive = toAmount.times(1 - request.slippage).integerValue();
187
226
  const txHex = quoteResponse.toTx(minReceive).hex;
188
- const substrateApi = this.chainService.getSubstrateApi(this.chain);
227
+ const substrateApi = this.chainService.getSubstrateApi(this.chain());
189
228
  const extrinsic = substrateApi.api.tx(txHex);
190
229
  const paymentInfo = await extrinsic.paymentInfo(request.address);
191
230
  const networkFee = {
@@ -217,6 +256,16 @@ export class HydradxHandler {
217
256
  return new SwapError(SwapErrorType.NOT_ENOUGH_LIQUIDITY);
218
257
  }
219
258
  }
259
+
260
+ // const feeTokenOptions = this.chainService.getFeeTokensByChain(this.chain());
261
+ const feeTokenOptions = [fromChainNativeTokenSlug];
262
+
263
+ // if (request.feeToken && !feeTokenOptions.includes(request.feeToken)) {
264
+ // return new SwapError(SwapErrorType.UNKNOWN);
265
+ // }
266
+
267
+ // const selectedFeeToken = request.feeToken || fromChainNativeTokenSlug;
268
+ const selectedFeeToken = fromChainNativeTokenSlug;
220
269
  return {
221
270
  pair: request.pair,
222
271
  fromAmount: request.fromAmount,
@@ -227,9 +276,10 @@ export class HydradxHandler {
227
276
  feeInfo: {
228
277
  feeComponent: [networkFee, tradeFee],
229
278
  defaultFeeToken: fromChainNativeTokenSlug,
230
- feeOptions: [fromChainNativeTokenSlug] // todo: parse fee options
279
+ feeOptions: feeTokenOptions,
280
+ // TODO: enable fee options
281
+ selectedFeeToken
231
282
  },
232
-
233
283
  isLowLiquidity: Math.abs(quoteResponse.priceImpactPct) >= HYDRADX_LOW_LIQUIDITY_THRESHOLD,
234
284
  route: swapRoute,
235
285
  metadata: txHex
@@ -281,10 +331,34 @@ export class HydradxHandler {
281
331
  txData: xcmData
282
332
  };
283
333
  }
334
+ async handleSetFeeStep(params) {
335
+ var _swapFeeInfo$selected;
336
+ const substrateApi = this.chainService.getSubstrateApi(this.chain());
337
+ const chainApi = await substrateApi.isReady;
338
+ const swapStepIndex = params.process.steps.findIndex(step => step.type === SwapStepType.SWAP);
339
+ if (swapStepIndex <= -1) {
340
+ return Promise.reject(new TransactionError(BasicTxErrorType.INTERNAL_ERROR));
341
+ }
342
+ const swapFeeInfo = params.process.totalFee[swapStepIndex];
343
+ const selectedFeeTokenSlug = (_swapFeeInfo$selected = swapFeeInfo.selectedFeeToken) !== null && _swapFeeInfo$selected !== void 0 ? _swapFeeInfo$selected : swapFeeInfo.defaultFeeToken;
344
+ const selectedFeeAsset = this.chainService.getAssetBySlug(selectedFeeTokenSlug);
345
+ const extrinsic = chainApi.api.tx.multiTransactionPayment.setCurrency(_getTokenOnChainAssetId(selectedFeeAsset));
346
+ const txData = {
347
+ selectedFeeToken: selectedFeeTokenSlug
348
+ };
349
+ return {
350
+ txChain: this.chain(),
351
+ extrinsic,
352
+ // extrinsicType: ExtrinsicType.SET_FEE_TOKEN,
353
+ extrinsicType: ExtrinsicType.SWAP,
354
+ chainType: ChainType.SUBSTRATE,
355
+ txData
356
+ };
357
+ }
284
358
  async handleSubmitStep(params) {
285
359
  const txHex = params.quote.metadata;
286
360
  const fromAsset = this.chainService.getAssetBySlug(params.quote.pair.from);
287
- const substrateApi = this.chainService.getSubstrateApi(this.chain);
361
+ const substrateApi = this.chainService.getSubstrateApi(this.chain());
288
362
  const chainApi = await substrateApi.isReady;
289
363
  const txData = {
290
364
  provider: this.providerInfo,
@@ -295,12 +369,25 @@ export class HydradxHandler {
295
369
  process: params.process
296
370
  };
297
371
  let extrinsic;
372
+ const txList = [];
373
+ const swapTx = chainApi.api.tx(txHex);
298
374
  const _referral = await chainApi.api.query.referrals.linkedAccounts(params.address);
299
375
  const referral = _referral === null || _referral === void 0 ? void 0 : _referral.toString();
300
- if (!referral || referral === '') {
301
- extrinsic = chainApi.api.tx.utility.batchAll([chainApi.api.tx.referrals.linkCode(this.referralCode), chainApi.api.tx(txHex)]);
376
+ const needSetReferral = !referral || referral === '';
377
+ const steps = params.process.steps.map(step => step.type);
378
+ const needSetFeeToken = steps.includes(SwapStepType.SET_FEE_TOKEN);
379
+ if (!needSetReferral && !needSetFeeToken) {
380
+ extrinsic = swapTx;
302
381
  } else {
303
- extrinsic = chainApi.api.tx(txHex);
382
+ if (needSetReferral) {
383
+ txList.push(chainApi.api.tx.referrals.linkCode(this.referralCode));
384
+ }
385
+ if (needSetFeeToken) {
386
+ const nativeTokenInfo = this.chainService.getNativeTokenInfo(this.chain());
387
+ txList.push(chainApi.api.tx.multiTransactionPayment.setCurrency(_getTokenOnChainAssetId(nativeTokenInfo)));
388
+ }
389
+ txList.push(swapTx);
390
+ extrinsic = chainApi.api.tx.utility.batchAll(txList);
304
391
  }
305
392
  return {
306
393
  txChain: fromAsset.originChain,
@@ -324,7 +411,7 @@ export class HydradxHandler {
324
411
  case SwapStepType.XCM:
325
412
  return this.handleXcmStep(params);
326
413
  case SwapStepType.SET_FEE_TOKEN:
327
- return Promise.reject(new TransactionError(BasicTxErrorType.UNSUPPORTED));
414
+ return this.handleSetFeeStep(params);
328
415
  case SwapStepType.SWAP:
329
416
  return this.handleSubmitStep(params);
330
417
  default:
@@ -393,7 +480,7 @@ export class HydradxHandler {
393
480
  error: SwapErrorType.ASSET_NOT_SUPPORTED
394
481
  };
395
482
  }
396
- if (!(fromAsset.originChain === this.chain && toAsset.originChain === this.chain)) {
483
+ if (!(fromAsset.originChain === this.chain() && toAsset.originChain === this.chain())) {
397
484
  return {
398
485
  error: SwapErrorType.ASSET_NOT_SUPPORTED
399
486
  };
@@ -411,7 +498,7 @@ export class HydradxHandler {
411
498
  }
412
499
  return {
413
500
  metadata: {
414
- chain: this.chainService.getChainInfoByKey(this.chain)
501
+ chain: this.chainService.getChainInfoByKey(this.chain())
415
502
  }
416
503
  };
417
504
  } catch (e) {
@@ -84,6 +84,7 @@ export class SwapService {
84
84
  request,
85
85
  selectedQuote: swapQuoteResponse.optimalQuote
86
86
  });
87
+ console.log('optimalProcess', optimalProcess);
87
88
  return {
88
89
  process: optimalProcess,
89
90
  quote: swapQuoteResponse
@@ -74,6 +74,7 @@ export interface SwapFeeInfo {
74
74
  feeComponent: SwapFeeComponent[];
75
75
  defaultFeeToken: string;
76
76
  feeOptions: string[];
77
+ selectedFeeToken?: string;
77
78
  }
78
79
  export interface SwapStepDetail extends BaseStepDetail {
79
80
  id: number;
@@ -119,6 +120,7 @@ export interface SwapRequest {
119
120
  fromAmount: string;
120
121
  slippage: number;
121
122
  recipient?: string;
123
+ feeToken?: string;
122
124
  }
123
125
  export interface SwapRequestResult {
124
126
  process: OptimalSwapPath;