@subwallet/extension-base 1.3.61-0 → 1.3.63-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 (73) hide show
  1. package/cjs/koni/api/staking/bonding/utils.js +1 -1
  2. package/cjs/packageInfo.js +1 -1
  3. package/cjs/services/balance-service/transfer/smart-contract.js +14 -10
  4. package/cjs/services/balance-service/transfer/xcm/acrossBridge/index.js +0 -6
  5. package/cjs/services/balance-service/transfer/xcm/utils.js +2 -0
  6. package/cjs/services/chain-service/constants.js +16 -0
  7. package/cjs/services/chain-service/utils/index.js +24 -4
  8. package/cjs/services/chain-service/utils/patch.js +2 -2
  9. package/cjs/services/earning-service/constants/chains.js +8 -3
  10. package/cjs/services/earning-service/handlers/native-staking/base-para.js +6 -3
  11. package/cjs/services/earning-service/handlers/native-staking/relay-chain.js +4 -1
  12. package/cjs/services/earning-service/handlers/native-staking/tanssi.js +496 -0
  13. package/cjs/services/earning-service/service.js +4 -0
  14. package/cjs/services/earning-service/utils/index.js +2 -0
  15. package/cjs/services/fee-service/utils/tokenPayFee.js +25 -2
  16. package/cjs/services/migration-service/scripts/DeleteEarningData20251010.js +21 -0
  17. package/cjs/services/migration-service/scripts/index.js +3 -1
  18. package/cjs/services/swap-service/handler/base-handler.js +4 -1
  19. package/cjs/services/swap-service/handler/chainflip-handler.js +1 -17
  20. package/cjs/services/swap-service/handler/optimex-handler.js +421 -0
  21. package/cjs/services/swap-service/handler/simpleswap-handler.js +4 -2
  22. package/cjs/services/swap-service/index.js +38 -140
  23. package/cjs/services/swap-service/utils.js +16 -157
  24. package/cjs/services/transaction-service/helpers/index.js +2 -1
  25. package/cjs/types/service-base.js +0 -1
  26. package/cjs/types/swap/index.js +5 -8
  27. package/cjs/utils/account/common.js +1 -2
  28. package/cjs/utils/fee/transfer.js +9 -1
  29. package/koni/api/staking/bonding/utils.js +2 -2
  30. package/package.json +21 -6
  31. package/packageInfo.js +1 -1
  32. package/services/balance-service/transfer/smart-contract.d.ts +3 -2
  33. package/services/balance-service/transfer/smart-contract.js +35 -29
  34. package/services/balance-service/transfer/xcm/acrossBridge/index.d.ts +0 -4
  35. package/services/balance-service/transfer/xcm/acrossBridge/index.js +0 -4
  36. package/services/balance-service/transfer/xcm/utils.js +2 -0
  37. package/services/chain-service/constants.js +16 -0
  38. package/services/chain-service/utils/index.d.ts +3 -2
  39. package/services/chain-service/utils/index.js +20 -1
  40. package/services/chain-service/utils/patch.d.ts +1 -1
  41. package/services/chain-service/utils/patch.js +2 -2
  42. package/services/earning-service/constants/chains.d.ts +2 -0
  43. package/services/earning-service/constants/chains.js +6 -2
  44. package/services/earning-service/handlers/native-staking/base-para.js +6 -3
  45. package/services/earning-service/handlers/native-staking/relay-chain.js +4 -1
  46. package/services/earning-service/handlers/native-staking/tanssi.d.ts +16 -0
  47. package/services/earning-service/handlers/native-staking/tanssi.js +478 -0
  48. package/services/earning-service/service.js +4 -0
  49. package/services/earning-service/utils/index.js +2 -0
  50. package/services/fee-service/utils/tokenPayFee.js +25 -2
  51. package/services/migration-service/scripts/DeleteEarningData20251010.d.ts +4 -0
  52. package/services/migration-service/scripts/DeleteEarningData20251010.js +13 -0
  53. package/services/migration-service/scripts/index.js +3 -1
  54. package/services/swap-service/handler/base-handler.js +6 -3
  55. package/services/swap-service/handler/chainflip-handler.d.ts +0 -2
  56. package/services/swap-service/handler/chainflip-handler.js +2 -18
  57. package/services/swap-service/handler/optimex-handler.d.ts +43 -0
  58. package/services/swap-service/handler/optimex-handler.js +410 -0
  59. package/services/swap-service/handler/simpleswap-handler.js +5 -3
  60. package/services/swap-service/index.d.ts +0 -1
  61. package/services/swap-service/index.js +21 -123
  62. package/services/swap-service/utils.d.ts +6 -12
  63. package/services/swap-service/utils.js +8 -138
  64. package/services/transaction-service/helpers/index.js +2 -1
  65. package/types/balance/transfer.d.ts +1 -0
  66. package/types/service-base.d.ts +3 -4
  67. package/types/service-base.js +0 -2
  68. package/types/swap/index.d.ts +3 -1
  69. package/types/swap/index.js +7 -6
  70. package/types/yield/info/account/info.d.ts +5 -0
  71. package/utils/account/common.js +2 -3
  72. package/utils/fee/transfer.js +9 -1
  73. package/utils/staticData/assetHubStaking.json +6 -1
@@ -5,19 +5,18 @@ Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
7
  exports.SwapService = void 0;
8
- var _chainList = require("@subwallet/chain-list");
9
- var _types = require("@subwallet/chain-list/types");
10
8
  var _SwapError = require("@subwallet/extension-base/background/errors/SwapError");
11
9
  var _TransactionError = require("@subwallet/extension-base/background/errors/TransactionError");
12
10
  var _KoniTypes = require("@subwallet/extension-base/background/KoniTypes");
13
11
  var _constants = require("@subwallet/extension-base/constants");
14
- var _types2 = require("@subwallet/extension-base/services/base/types");
12
+ var _types = require("@subwallet/extension-base/services/base/types");
15
13
  var _utils = require("@subwallet/extension-base/services/chain-service/utils");
16
14
  var _assetHub = require("@subwallet/extension-base/services/swap-service/handler/asset-hub");
17
15
  var _chainflipHandler = require("@subwallet/extension-base/services/swap-service/handler/chainflip-handler");
18
16
  var _hydradxHandler = require("@subwallet/extension-base/services/swap-service/handler/hydradx-handler");
17
+ var _optimexHandler = require("@subwallet/extension-base/services/swap-service/handler/optimex-handler");
19
18
  var _utils2 = require("@subwallet/extension-base/services/swap-service/utils");
20
- var _types3 = require("@subwallet/extension-base/types");
19
+ var _types2 = require("@subwallet/extension-base/types");
21
20
  var _serviceBase = require("@subwallet/extension-base/types/service-base");
22
21
  var _swap = require("@subwallet/extension-base/types/swap");
23
22
  var _utils3 = require("@subwallet/extension-base/utils");
@@ -36,7 +35,7 @@ class SwapService {
36
35
  handlers = {};
37
36
  startPromiseHandler = (0, _utils3.createPromiseHandler)();
38
37
  stopPromiseHandler = (0, _utils3.createPromiseHandler)();
39
- status = _types2.ServiceStatus.NOT_INITIALIZED;
38
+ status = _types.ServiceStatus.NOT_INITIALIZED;
40
39
  constructor(state) {
41
40
  this.state = state;
42
41
  this.eventService = state.eventService;
@@ -84,7 +83,7 @@ class SwapService {
84
83
  steps: [_serviceBase.DEFAULT_FIRST_STEP],
85
84
  path: []
86
85
  };
87
- const swapPairInfo = params.path.find(action => action.action === _types3.DynamicSwapType.SWAP);
86
+ const swapPairInfo = params.path.find(action => action.action === _types2.DynamicSwapType.SWAP);
88
87
  if (!swapPairInfo) {
89
88
  console.error('Swap pair is not found');
90
89
  return result;
@@ -114,6 +113,7 @@ class SwapService {
114
113
  const providerId = ((_params$request$curre = params.request.currentQuote) === null || _params$request$curre === void 0 ? void 0 : _params$request$curre.id) || params.selectedQuote.provider.id;
115
114
  const handler = this.handlers[providerId];
116
115
  if (handler) {
116
+ // todo: handle error response from generateOptimalProcess
117
117
  return handler.generateOptimalProcessV2(params);
118
118
  } else {
119
119
  return this.getDefaultProcessV2(params);
@@ -151,6 +151,12 @@ class SwapService {
151
151
  } catch (e) {
152
152
  throw new Error(e.message);
153
153
  }
154
+
155
+ // override fee for quote because some cases need estimate network fee on Extension (i.e. Optimex)
156
+ if (swapQuoteResponse.optimalQuote) {
157
+ const swapIndex = optimalProcess.steps.findIndex(step => step.type === _swap.SwapStepType.SWAP);
158
+ swapQuoteResponse.optimalQuote.feeInfo.feeComponent = optimalProcess.totalFee[swapIndex].feeComponent;
159
+ }
154
160
  if (swapQuoteResponse.error) {
155
161
  return {
156
162
  process: optimalProcess,
@@ -167,115 +173,6 @@ class SwapService {
167
173
  quote: swapQuoteResponse
168
174
  };
169
175
  }
170
-
171
- // todo: rewrite this function
172
- getAvailablePath(request) {
173
- const {
174
- address,
175
- pair
176
- } = request;
177
- // todo: control provider tighter
178
- const supportSwapChains = (0, _utils2.getSupportedSwapChains)();
179
- const fromToken = this.chainService.getAssetBySlug(pair.from);
180
- const toToken = this.chainService.getAssetBySlug(pair.to);
181
- const fromChain = (0, _utils._getAssetOriginChain)(fromToken);
182
- const toChain = (0, _utils._getAssetOriginChain)(toToken);
183
- const toChainInfo = this.chainService.getChainInfoByKey(toChain);
184
- const assetRefMap = this.chainService.getAssetRefMap();
185
- let process = [];
186
- if (!fromToken || !toToken) {
187
- throw Error('Token not found');
188
- }
189
- if (!fromChain || !toChain) {
190
- throw Error('Token metadata error');
191
- }
192
- const directXcmRef = Object.values(assetRefMap).find(assetRef => assetRef.path === _types._AssetRefPath.XCM && assetRef.srcAsset === fromToken.slug && assetRef.destAsset === toToken.slug);
193
- if (directXcmRef) {
194
- return [[], undefined];
195
- }
196
-
197
- // SWAP: 2 tokens in the same chain and chain has dex
198
- if ((0, _utils2.isChainsHasSameProvider)(fromChain, toChain)) {
199
- // there's a dex that can support direct swapping
200
- process.push((0, _utils2.getSwapStep)(fromToken.slug, toToken.slug));
201
- return [process, request];
202
- }
203
-
204
- // ------------------------
205
- // BRIDGE -> SWAP: Try to find a token in dest chain that can bridge from fromToken
206
- const bridgeTransit = (0, _utils2.findBridgeTransitDestination)(assetRefMap, fromToken, toToken);
207
- if (bridgeTransit && supportSwapChains.includes(toChain)) {
208
- const swapStep = (0, _utils2.getSwapStep)(bridgeTransit, toToken.slug);
209
- process.push((0, _utils2.getBridgeStep)(fromToken.slug, bridgeTransit));
210
- process.push(swapStep);
211
- return [process, {
212
- ...request,
213
- address: (0, _utils3.reformatAddress)(address, (0, _utils._getChainSubstrateAddressPrefix)(toChainInfo)),
214
- pair: swapStep.pair
215
- }];
216
- }
217
-
218
- // ------------------------
219
- // SWAP -> BRIDGE: Try to find a token in from chain that can bridge to toToken
220
- const swapTransit = (0, _utils2.findSwapTransitDestination)(assetRefMap, fromToken, toToken);
221
- if (swapTransit && supportSwapChains.includes(fromChain)) {
222
- const swapStep = (0, _utils2.getSwapStep)(fromToken.slug, swapTransit);
223
- process.push(swapStep);
224
- process.push((0, _utils2.getBridgeStep)(swapTransit, toToken.slug));
225
- return [process, {
226
- ...request,
227
- pair: swapStep.pair
228
- }];
229
- }
230
-
231
- // ------------------------
232
- // BRIDGE -> SWAP -> BRIDGE: Try to find a tri-step path to swap
233
- const processList = [];
234
- const swapPairList = [];
235
- const allBridgeDestinations = (0, _utils2.findAllBridgeDestinations)(assetRefMap, fromToken);
236
-
237
- // currently find first path. Todo: return all paths or best path.
238
- for (const bridgeTransit of allBridgeDestinations) {
239
- process = [];
240
- const bridgeDestinationInfo = this.chainService.getAssetBySlug(bridgeTransit);
241
- const swapTransit = (0, _utils2.findSwapTransitDestination)(assetRefMap, bridgeDestinationInfo, toToken);
242
- if (bridgeTransit === swapTransit) {
243
- continue;
244
- }
245
- if (swapTransit && supportSwapChains.includes(bridgeDestinationInfo.originChain)) {
246
- const swapStep = (0, _utils2.getSwapStep)(bridgeTransit, swapTransit);
247
- process.push((0, _utils2.getBridgeStep)(fromToken.slug, bridgeTransit));
248
- process.push(swapStep);
249
- process.push((0, _utils2.getBridgeStep)(swapTransit, toToken.slug));
250
-
251
- // set the highest priority to hydration provider
252
- if (bridgeDestinationInfo.originChain === _chainList.COMMON_CHAIN_SLUGS.HYDRADX) {
253
- return [process, {
254
- ...request,
255
- address: (0, _utils3._reformatAddressWithChain)(address, this.chainService.getChainInfoByKey(_chainList.COMMON_CHAIN_SLUGS.HYDRADX)),
256
- pair: swapStep.pair
257
- }];
258
- }
259
- processList.push(process);
260
- swapPairList.push(swapStep.pair);
261
- }
262
- }
263
-
264
- // get first process
265
- if (processList.length && swapPairList.length) {
266
- const [firstProcess, firstSwapPair] = [processList[0], swapPairList[0]];
267
- const chainSwap = this.chainService.getAssetBySlug(firstSwapPair.from).originChain;
268
- return [firstProcess, {
269
- ...request,
270
- address: (0, _utils3._reformatAddressWithChain)(address, this.chainService.getChainInfoByKey(chainSwap)),
271
- pair: firstSwapPair
272
- }];
273
- }
274
-
275
- // todo: encapsulate each route type to function
276
-
277
- return [[], undefined];
278
- }
279
176
  async getLatestQuoteFromSwapRequest(request) {
280
177
  let availablePath;
281
178
  try {
@@ -296,7 +193,7 @@ class SwapService {
296
193
  const {
297
194
  path
298
195
  } = availablePath;
299
- const swapAction = path.find(step => step.action === _types3.DynamicSwapType.SWAP);
196
+ const swapAction = path.find(step => step.action === _types2.DynamicSwapType.SWAP);
300
197
  const directSwapRequest = swapAction ? {
301
198
  ...request,
302
199
  address: (0, _utils3._reformatAddressWithChain)(request.address, this.chainService.getChainInfoByKey((0, _utils._getAssetOriginChain)(this.chainService.getAssetBySlug(swapAction.pair.from)))),
@@ -305,7 +202,7 @@ class SwapService {
305
202
  if (!directSwapRequest) {
306
203
  throw Error('Swap pair is not found');
307
204
  }
308
- if (path.length > 1 && path.map(action => action.action).includes(_types3.DynamicSwapType.BRIDGE)) {
205
+ if (path.length > 1 && path.map(action => action.action).includes(_types2.DynamicSwapType.BRIDGE)) {
309
206
  directSwapRequest.isCrossChain = true;
310
207
  }
311
208
  const swapQuoteResponse = await this.getLatestDirectQuotes(directSwapRequest);
@@ -375,9 +272,6 @@ class SwapService {
375
272
  case _swap.SwapProviderId.CHAIN_FLIP_MAINNET:
376
273
  this.handlers[providerId] = new _chainflipHandler.ChainflipSwapHandler(this.chainService, this.state.balanceService, this.state.feeService, false);
377
274
  break;
378
- case _swap.SwapProviderId.HYDRADX_TESTNET:
379
- this.handlers[providerId] = new _hydradxHandler.HydradxHandler(this.chainService, this.state.balanceService, this.state.feeService);
380
- break;
381
275
  case _swap.SwapProviderId.HYDRADX_MAINNET:
382
276
  this.handlers[providerId] = new _hydradxHandler.HydradxHandler(this.chainService, this.state.balanceService, this.state.feeService, false);
383
277
  break;
@@ -387,12 +281,6 @@ class SwapService {
387
281
  case _swap.SwapProviderId.KUSAMA_ASSET_HUB:
388
282
  this.handlers[providerId] = new _assetHub.AssetHubSwapHandler(this.chainService, this.state.balanceService, this.state.feeService, 'statemine');
389
283
  break;
390
- // case SwapProviderId.ROCOCO_ASSET_HUB:
391
- // this.handlers[providerId] = new AssetHubSwapHandler(this.chainService, this.state.balanceService, this.state.feeService, 'rococo_assethub');
392
- // break;
393
- case _swap.SwapProviderId.WESTEND_ASSET_HUB:
394
- this.handlers[providerId] = new _assetHub.AssetHubSwapHandler(this.chainService, this.state.balanceService, this.state.feeService, 'westend_assethub');
395
- break;
396
284
  case _swap.SwapProviderId.SIMPLE_SWAP:
397
285
  this.handlers[providerId] = new _simpleswapHandler.SimpleSwapHandler(this.chainService, this.state.balanceService, this.state.feeService);
398
286
  break;
@@ -402,26 +290,32 @@ class SwapService {
402
290
  case _swap.SwapProviderId.KYBER:
403
291
  this.handlers[providerId] = new _kyberHandler.KyberHandler(this.chainService, this.state.balanceService, this.state.transactionService, this.state.feeService);
404
292
  break;
293
+ case _swap.SwapProviderId.OPTIMEX:
294
+ this.handlers[providerId] = new _optimexHandler.OptimexHandler(this.chainService, this.state.balanceService, this.state.feeService, false);
295
+ break;
296
+ case _swap.SwapProviderId.OPTIMEX_TESTNET:
297
+ this.handlers[providerId] = new _optimexHandler.OptimexHandler(this.chainService, this.state.balanceService, this.state.feeService, true);
298
+ break;
405
299
  default:
406
300
  throw new Error('Unsupported provider');
407
301
  }
408
302
  });
409
303
  }
410
304
  async init() {
411
- this.status = _types2.ServiceStatus.INITIALIZING;
305
+ this.status = _types.ServiceStatus.INITIALIZING;
412
306
  this.eventService.emit('swap.ready', true);
413
- this.status = _types2.ServiceStatus.INITIALIZED;
307
+ this.status = _types.ServiceStatus.INITIALIZED;
414
308
  this.initHandlers();
415
309
  await this.start();
416
310
  }
417
311
  async start() {
418
- if (this.status === _types2.ServiceStatus.STOPPING) {
312
+ if (this.status === _types.ServiceStatus.STOPPING) {
419
313
  await this.waitForStopped();
420
314
  }
421
- if (this.status === _types2.ServiceStatus.STARTED || this.status === _types2.ServiceStatus.STARTING) {
315
+ if (this.status === _types.ServiceStatus.STARTED || this.status === _types.ServiceStatus.STARTING) {
422
316
  return this.waitForStarted();
423
317
  }
424
- this.status = _types2.ServiceStatus.STARTING;
318
+ this.status = _types.ServiceStatus.STARTING;
425
319
 
426
320
  // todo: start the service jobs, subscribe data,...
427
321
 
@@ -430,13 +324,13 @@ class SwapService {
430
324
  // Update promise handler
431
325
  this.startPromiseHandler.resolve();
432
326
  this.stopPromiseHandler = (0, _utils3.createPromiseHandler)();
433
- this.status = _types2.ServiceStatus.STARTED;
327
+ this.status = _types.ServiceStatus.STARTED;
434
328
  }
435
329
  async stop() {
436
- if (this.status === _types2.ServiceStatus.STARTING) {
330
+ if (this.status === _types.ServiceStatus.STARTING) {
437
331
  await this.waitForStarted();
438
332
  }
439
- if (this.status === _types2.ServiceStatus.STOPPED || this.status === _types2.ServiceStatus.STOPPING) {
333
+ if (this.status === _types.ServiceStatus.STOPPED || this.status === _types.ServiceStatus.STOPPING) {
440
334
  return this.waitForStopped();
441
335
  }
442
336
 
@@ -444,7 +338,7 @@ class SwapService {
444
338
 
445
339
  this.stopPromiseHandler.resolve();
446
340
  this.startPromiseHandler = (0, _utils3.createPromiseHandler)();
447
- this.status = _types2.ServiceStatus.STOPPED;
341
+ this.status = _types.ServiceStatus.STOPPED;
448
342
  }
449
343
  waitForStarted() {
450
344
  return this.startPromiseHandler.promise;
@@ -452,6 +346,8 @@ class SwapService {
452
346
  waitForStopped() {
453
347
  return this.stopPromiseHandler.promise;
454
348
  }
349
+
350
+ // todo: deprecated
455
351
  getSwapPairs() {
456
352
  return Object.entries(this.chainService.swapRefMap).map(_ref => {
457
353
  let [slug, assetRef] = _ref;
@@ -478,7 +374,7 @@ class SwapService {
478
374
  const blockedActionsFeaturesMaps = await (0, _constants.fetchLatestBlockedActionsAndFeatures)(passBlockedConfigId);
479
375
  const originSwapPairInfo = (0, _utils2.getTokenPairFromStep)(params.process.steps);
480
376
  if (!originSwapPairInfo) {
481
- return [new _TransactionError.TransactionError(_types3.BasicTxErrorType.INTERNAL_ERROR)];
377
+ return [new _TransactionError.TransactionError(_types2.BasicTxErrorType.INTERNAL_ERROR)];
482
378
  }
483
379
  const currentAction = `${_KoniTypes.ExtrinsicType.SWAP}___${originSwapPairInfo.slug}___${params.selectedQuote.provider.id}`;
484
380
  for (const blockedActionsFeaturesMap of blockedActionsFeaturesMaps) {
@@ -486,27 +382,29 @@ class SwapService {
486
382
  blockedActionsMap
487
383
  } = blockedActionsFeaturesMap;
488
384
  if (blockedActionsMap.swap.includes(currentAction)) {
489
- return [new _TransactionError.TransactionError(_types3.BasicTxErrorType.UNSUPPORTED, (0, _i18next.t)('bg.SWAP.services.service.swap.featureUnderMaintenance'))];
385
+ return [new _TransactionError.TransactionError(_types2.BasicTxErrorType.UNSUPPORTED, (0, _i18next.t)('bg.SWAP.services.service.swap.featureUnderMaintenance'))];
490
386
  }
491
387
  }
492
388
  if (handler) {
493
389
  return handler.validateSwapProcessV2(params);
494
390
  } else {
495
- return [new _TransactionError.TransactionError(_types3.BasicTxErrorType.INTERNAL_ERROR)];
391
+ return [new _TransactionError.TransactionError(_types2.BasicTxErrorType.INTERNAL_ERROR)];
496
392
  }
497
393
  }
498
394
  async handleSwapProcess(params) {
499
395
  const handler = this.handlers[params.quote.provider.id];
500
396
  if (params.process.steps.length === 1) {
501
397
  // todo: do better to handle error generating steps
502
- return Promise.reject(new _TransactionError.TransactionError(_types3.BasicTxErrorType.INTERNAL_ERROR, 'Please check your network and try again'));
398
+ return Promise.reject(new _TransactionError.TransactionError(_types2.BasicTxErrorType.INTERNAL_ERROR, 'Please check your network and try again'));
503
399
  }
504
400
  if (handler) {
505
401
  return handler.handleSwapProcess(params);
506
402
  } else {
507
- return Promise.reject(new _TransactionError.TransactionError(_types3.BasicTxErrorType.INTERNAL_ERROR));
403
+ return Promise.reject(new _TransactionError.TransactionError(_types2.BasicTxErrorType.INTERNAL_ERROR));
508
404
  }
509
405
  }
406
+
407
+ // todo: deprecated
510
408
  subscribeSwapPairs(callback) {
511
409
  return this.chainService.subscribeSwapRefMap().subscribe(refMap => {
512
410
  const latestData = Object.entries(refMap).map(_ref2 => {
@@ -4,29 +4,18 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
4
4
  Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
- exports._PROVIDER_TO_SUPPORTED_PAIR_MAP = exports.SWAP_QUOTE_TIMEOUT_MAP = exports.SIMPLE_SWAP_SUPPORTED_TESTNET_ASSET_MAPPING = exports.SIMPLE_SWAP_EXPLORER = exports.FEE_RATE_MULTIPLIER = exports.DEFAULT_EXCESS_AMOUNT_WEIGHT = exports.CHAIN_FLIP_TESTNET_EXPLORER = exports.CHAIN_FLIP_MAINNET_EXPLORER = void 0;
8
- exports.calculateSwapRate = calculateSwapRate;
9
- exports.convertSwapRate = convertSwapRate;
10
- exports.findAllBridgeDestinations = findAllBridgeDestinations;
11
- exports.findBridgeTransitDestination = findBridgeTransitDestination;
12
- exports.findSwapTransitDestination = findSwapTransitDestination;
7
+ exports.SWAP_QUOTE_TIMEOUT_MAP = exports.SIMPLE_SWAP_EXPLORER = exports.FEE_RATE_MULTIPLIER = exports.DetectedGenOptimalProcessErrMsg = exports.DEFAULT_EXCESS_AMOUNT_WEIGHT = exports.CHAIN_FLIP_TESTNET_EXPLORER = exports.CHAIN_FLIP_MAINNET_EXPLORER = void 0;
13
8
  exports.getAmountAfterSlippage = getAmountAfterSlippage;
14
- exports.getBridgeStep = getBridgeStep;
15
9
  exports.getChainRouteFromSteps = getChainRouteFromSteps;
16
10
  exports.getFirstAmountFromSteps = getFirstAmountFromSteps;
17
11
  exports.getLastAmountFromSteps = getLastAmountFromSteps;
18
- exports.getSupportedSwapChains = getSupportedSwapChains;
19
12
  exports.getSwapAltToken = getSwapAltToken;
20
13
  exports.getSwapAlternativeAsset = getSwapAlternativeAsset;
21
14
  exports.getSwapChainsFromPath = getSwapChainsFromPath;
22
- exports.getSwapStep = getSwapStep;
23
15
  exports.getTokenPairFromStep = getTokenPairFromStep;
24
- exports.isChainsHasSameProvider = isChainsHasSameProvider;
25
16
  exports.processStepsToPathActions = processStepsToPathActions;
26
- var _chainList = require("@subwallet/chain-list");
27
- var _types = require("@subwallet/chain-list/types");
28
17
  var _utils = require("@subwallet/extension-base/services/chain-service/utils");
29
- var _types2 = require("@subwallet/extension-base/types");
18
+ var _types = require("@subwallet/extension-base/types");
30
19
  var _swap = require("@subwallet/extension-base/types/swap");
31
20
  var _bignumber = _interopRequireDefault(require("bignumber.js"));
32
21
  // Copyright 2019-2022 @subwallet/extension-base
@@ -37,17 +26,7 @@ exports.CHAIN_FLIP_TESTNET_EXPLORER = CHAIN_FLIP_TESTNET_EXPLORER;
37
26
  const CHAIN_FLIP_MAINNET_EXPLORER = 'https://scan.chainflip.io';
38
27
  exports.CHAIN_FLIP_MAINNET_EXPLORER = CHAIN_FLIP_MAINNET_EXPLORER;
39
28
  const SIMPLE_SWAP_EXPLORER = 'https://simpleswap.io';
40
-
41
- // Deprecated
42
29
  exports.SIMPLE_SWAP_EXPLORER = SIMPLE_SWAP_EXPLORER;
43
- const SIMPLE_SWAP_SUPPORTED_TESTNET_ASSET_MAPPING = {
44
- 'bittensor-NATIVE-TAO': 'tao',
45
- [_chainList.COMMON_ASSETS.ETH]: 'eth',
46
- [_chainList.COMMON_ASSETS.DOT]: 'dot',
47
- [_chainList.COMMON_ASSETS.USDC_ETHEREUM]: 'usdc',
48
- [_chainList.COMMON_ASSETS.USDT_ETHEREUM]: 'usdterc20'
49
- };
50
- exports.SIMPLE_SWAP_SUPPORTED_TESTNET_ASSET_MAPPING = SIMPLE_SWAP_SUPPORTED_TESTNET_ASSET_MAPPING;
51
30
  const SWAP_QUOTE_TIMEOUT_MAP = {
52
31
  // in milliseconds
53
32
  default: 90000,
@@ -55,32 +34,13 @@ const SWAP_QUOTE_TIMEOUT_MAP = {
55
34
  [_swap.SwapProviderId.CHAIN_FLIP_MAINNET]: 30000,
56
35
  error: 10000
57
36
  };
58
-
59
- // deprecated
60
37
  exports.SWAP_QUOTE_TIMEOUT_MAP = SWAP_QUOTE_TIMEOUT_MAP;
61
- const _PROVIDER_TO_SUPPORTED_PAIR_MAP = {
62
- [_swap.SwapProviderId.HYDRADX_MAINNET]: [_chainList.COMMON_CHAIN_SLUGS.HYDRADX],
63
- [_swap.SwapProviderId.CHAIN_FLIP_MAINNET]: [_chainList.COMMON_CHAIN_SLUGS.POLKADOT, _chainList.COMMON_CHAIN_SLUGS.ETHEREUM, _chainList.COMMON_CHAIN_SLUGS.ARBITRUM],
64
- [_swap.SwapProviderId.POLKADOT_ASSET_HUB]: [_chainList.COMMON_CHAIN_SLUGS.POLKADOT_ASSET_HUB],
65
- [_swap.SwapProviderId.KUSAMA_ASSET_HUB]: [_chainList.COMMON_CHAIN_SLUGS.KUSAMA_ASSET_HUB],
66
- [_swap.SwapProviderId.SIMPLE_SWAP]: ['bittensor', _chainList.COMMON_CHAIN_SLUGS.ETHEREUM, _chainList.COMMON_CHAIN_SLUGS.POLKADOT],
67
- [_swap.SwapProviderId.UNISWAP]: [_chainList.COMMON_CHAIN_SLUGS.ETHEREUM, _chainList.COMMON_CHAIN_SLUGS.ARBITRUM],
68
- // testnet
69
- [_swap.SwapProviderId.CHAIN_FLIP_TESTNET]: [_chainList.COMMON_CHAIN_SLUGS.CHAINFLIP_POLKADOT, _chainList.COMMON_CHAIN_SLUGS.ETHEREUM_SEPOLIA],
70
- [_swap.SwapProviderId.HYDRADX_TESTNET]: [_chainList.COMMON_CHAIN_SLUGS.HYDRADX_TESTNET],
71
- [_swap.SwapProviderId.ROCOCO_ASSET_HUB]: [_chainList.COMMON_CHAIN_SLUGS.ROCOCO_ASSET_HUB],
72
- [_swap.SwapProviderId.WESTEND_ASSET_HUB]: ['westend_assethub']
73
- };
74
- exports._PROVIDER_TO_SUPPORTED_PAIR_MAP = _PROVIDER_TO_SUPPORTED_PAIR_MAP;
75
38
  const FEE_RATE_MULTIPLIER = {
76
39
  default: 1,
77
40
  medium: 1.2,
78
41
  high: 2
79
42
  };
80
43
  exports.FEE_RATE_MULTIPLIER = FEE_RATE_MULTIPLIER;
81
- function getSupportedSwapChains() {
82
- return [...new Set(Object.values(_PROVIDER_TO_SUPPORTED_PAIR_MAP).flat())];
83
- }
84
44
  function getSwapAlternativeAsset(swapPair) {
85
45
  var _swapPair$metadata;
86
46
  return swapPair === null || swapPair === void 0 ? void 0 : (_swapPair$metadata = swapPair.metadata) === null || _swapPair$metadata === void 0 ? void 0 : _swapPair$metadata.alternativeAsset;
@@ -89,116 +49,9 @@ function getSwapAltToken(chainAsset) {
89
49
  var _chainAsset$metadata;
90
50
  return (_chainAsset$metadata = chainAsset.metadata) === null || _chainAsset$metadata === void 0 ? void 0 : _chainAsset$metadata.alternativeSwapAsset;
91
51
  }
92
- function calculateSwapRate(fromAmount, toAmount, fromAsset, toAsset) {
93
- const bnFromAmount = (0, _bignumber.default)(fromAmount);
94
- const bnToAmount = (0, _bignumber.default)(toAmount);
95
- const decimalDiff = (0, _utils._getAssetDecimals)(toAsset) - (0, _utils._getAssetDecimals)(fromAsset);
96
- const bnRate = bnFromAmount.div(bnToAmount);
97
- return 1 / bnRate.times(10 ** decimalDiff).toNumber();
98
- }
99
- function convertSwapRate(rate, fromAsset, toAsset) {
100
- const decimalDiff = (0, _utils._getAssetDecimals)(toAsset) - (0, _utils._getAssetDecimals)(fromAsset);
101
- const bnRate = (0, _bignumber.default)(rate);
102
- return bnRate.times(10 ** decimalDiff).pow(-1).toNumber();
103
- }
104
-
105
- // export function getChainflipOptions (isTestnet: boolean) {
106
- // if (isTestnet) {
107
- // return {
108
- // network: getChainflipNetwork(isTestnet)
109
- // };
110
- // }
111
-
112
- // return {
113
- // network: getChainflipNetwork(isTestnet),
114
- // broker: getChainflipBroker(isTestnet)
115
- // };
116
- // }
117
-
118
- // function getChainflipNetwork (isTestnet: boolean) {
119
- // return isTestnet ? 'perseverance' : 'mainnet';
120
- // }
121
-
122
- // export function getChainflipBroker (isTestnet: boolean) { // noted: currently not use testnet broker
123
- // if (isTestnet) {
124
- // return {
125
- // url: `https://perseverance.chainflip-broker.io/rpc/${CHAINFLIP_BROKER_API}`
126
- // };
127
- // } else {
128
- // return {
129
- // url: `https://chainflip-broker.io/rpc/${CHAINFLIP_BROKER_API}`
130
- // };
131
- // }
132
- // }
133
-
134
- // export function getChainflipSwap (isTestnet: boolean) {
135
- // if (isTestnet) {
136
- // return `https://perseverance.chainflip-broker.io/swap?apikey=${CHAINFLIP_BROKER_API}`;
137
- // } else {
138
- // return `https://chainflip-broker.io/swap?apikey=${CHAINFLIP_BROKER_API}`;
139
- // }
140
- // }
141
-
142
- // export function getAssetsUrl (isTestnet: boolean) {
143
- // if (isTestnet) {
144
- // return 'https://perseverance.chainflip-broker.io/assets';
145
- // } else {
146
- // return 'https://chainflip-broker.io/assets';
147
- // }
148
- // }
149
-
150
- function getBridgeStep(from, to) {
151
- return {
152
- action: _types2.DynamicSwapType.BRIDGE,
153
- pair: {
154
- slug: `${from}___${to}`,
155
- // todo: recheck with assetRef format from chain list
156
- from,
157
- to
158
- }
159
- };
160
- }
161
- function getSwapStep(from, to) {
162
- return {
163
- action: _types2.DynamicSwapType.SWAP,
164
- pair: {
165
- slug: `${from}___${to}`,
166
- // todo: recheck with assetRef format from chain list
167
- from,
168
- to
169
- }
170
- };
171
- }
172
- function findBridgeTransitDestination(assetRefMap, fromToken, toToken) {
173
- const foundAssetRef = Object.values(assetRefMap).find(assetRef => assetRef.srcAsset === fromToken.slug && assetRef.destChain === (0, _utils._getAssetOriginChain)(toToken) && assetRef.path === _types._AssetRefPath.XCM);
174
- if (foundAssetRef) {
175
- return foundAssetRef.destAsset;
176
- }
177
- return undefined;
178
- }
179
- function findSwapTransitDestination(assetRefMap, fromToken, toToken) {
180
- const foundAssetRef = Object.values(assetRefMap).find(assetRef => assetRef.destAsset === toToken.slug && assetRef.srcChain === (0, _utils._getAssetOriginChain)(fromToken) && assetRef.path === _types._AssetRefPath.XCM);
181
- if (foundAssetRef) {
182
- return foundAssetRef.srcAsset;
183
- }
184
- return undefined;
185
- }
186
- function findAllBridgeDestinations(assetRefMap, fromToken) {
187
- const foundAssetRefs = Object.values(assetRefMap).filter(assetRef => assetRef.srcAsset === fromToken.slug && assetRef.path === _types._AssetRefPath.XCM);
188
- return foundAssetRefs.map(assetRef => assetRef.destAsset);
189
- }
190
52
  function getAmountAfterSlippage(amount, slippage) {
191
53
  return (0, _bignumber.default)(amount).multipliedBy((0, _bignumber.default)(1).minus((0, _bignumber.default)(slippage))).integerValue(_bignumber.default.ROUND_DOWN).toString();
192
54
  }
193
- function isChainsHasSameProvider(fromChain, toChain) {
194
- // todo: a provider may support multiple chains but not cross-chain swaps
195
- for (const group of Object.values(_PROVIDER_TO_SUPPORTED_PAIR_MAP)) {
196
- if (group.includes(fromChain) && group.includes(toChain)) {
197
- return true;
198
- }
199
- }
200
- return false;
201
- }
202
55
  function getLastAmountFromSteps(steps) {
203
56
  var _lastStep$metadata;
204
57
  const lastStep = steps[steps.length - 1]; // last step
@@ -213,7 +66,7 @@ function getFirstAmountFromSteps(steps) {
213
66
  }
214
67
  function getChainRouteFromSteps(steps) {
215
68
  // todo: handle metadata for other providers than hydra & pah. Also add validate metadata.
216
- const mainSteps = steps.filter(step => step.type !== _types2.CommonStepType.DEFAULT);
69
+ const mainSteps = steps.filter(step => step.type !== _types.CommonStepType.DEFAULT);
217
70
  return mainSteps.reduce((chainRoute, currentStep, currentIndex) => {
218
71
  const metadata = currentStep.metadata;
219
72
  if (!metadata) {
@@ -233,13 +86,13 @@ function getChainRouteFromSteps(steps) {
233
86
  // note: this function may return undefined if metadata version is < 2 or does not exist
234
87
  function getTokenPairFromStep(steps) {
235
88
  // todo: review this
236
- const mainSteps = steps.filter(step => step.type !== _types2.CommonStepType.DEFAULT && step.type !== _types2.CommonStepType.TOKEN_APPROVAL && step.type !== _types2.SwapStepType.PERMIT);
89
+ const mainSteps = steps.filter(step => step.type !== _types.CommonStepType.DEFAULT && step.type !== _types.CommonStepType.TOKEN_APPROVAL && step.type !== _types.SwapStepType.PERMIT);
237
90
  if (!mainSteps.length) {
238
91
  return undefined;
239
92
  }
240
93
  const isStepValidIfSwap = step => {
241
94
  const metadata = step.metadata;
242
- return step.type !== _types2.SwapStepType.SWAP || !!(metadata !== null && metadata !== void 0 && metadata.version) && (metadata === null || metadata === void 0 ? void 0 : metadata.version) >= 2;
95
+ return step.type !== _types.SwapStepType.SWAP || !!(metadata !== null && metadata !== void 0 && metadata.version) && (metadata === null || metadata === void 0 ? void 0 : metadata.version) >= 2;
243
96
  };
244
97
  if (mainSteps.length === 1) {
245
98
  if (!isStepValidIfSwap(mainSteps[0])) {
@@ -288,14 +141,20 @@ function getSwapChainsFromPath(path) {
288
141
  function processStepsToPathActions(steps) {
289
142
  const path = [];
290
143
  for (const step of steps) {
291
- if (step.type === _types2.CommonStepType.XCM) {
292
- path.push(_types2.DynamicSwapType.BRIDGE);
144
+ if (step.type === _types.CommonStepType.XCM) {
145
+ path.push(_types.DynamicSwapType.BRIDGE);
293
146
  }
294
- if (step.type === _types2.SwapStepType.SWAP) {
295
- path.push(_types2.DynamicSwapType.SWAP);
147
+ if (step.type === _types.SwapStepType.SWAP) {
148
+ path.push(_types.DynamicSwapType.SWAP);
296
149
  }
297
150
  }
298
151
  return path;
299
152
  }
300
153
  const DEFAULT_EXCESS_AMOUNT_WEIGHT = 1.04; // add 2%
301
- exports.DEFAULT_EXCESS_AMOUNT_WEIGHT = DEFAULT_EXCESS_AMOUNT_WEIGHT;
154
+ exports.DEFAULT_EXCESS_AMOUNT_WEIGHT = DEFAULT_EXCESS_AMOUNT_WEIGHT;
155
+ const DetectedGenOptimalProcessErrMsg = {
156
+ AMOUNT_TOO_LOW: 'amount too low',
157
+ AMOUNT_TOO_HIGH: 'amount too high',
158
+ NOT_ENOUGHT_BITCOIN: 'for the transaction. lower your btc amount and try again' // todo: improve handle msg error
159
+ };
160
+ exports.DetectedGenOptimalProcessErrMsg = DetectedGenOptimalProcessErrMsg;
@@ -36,7 +36,8 @@ const isCardanoTransaction = tx => {
36
36
  // TODO: [Review] this function
37
37
  exports.isCardanoTransaction = isCardanoTransaction;
38
38
  const isBitcoinTransaction = tx => {
39
- return 'data' in tx && Array.isArray(tx.data.inputs);
39
+ var _data;
40
+ return 'data' in tx && Array.isArray((_data = tx.data) === null || _data === void 0 ? void 0 : _data.inputs);
40
41
  };
41
42
  exports.isBitcoinTransaction = isBitcoinTransaction;
42
43
  const typeName = type => {
@@ -6,7 +6,6 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.MOCK_STEP_FEE = exports.DEFAULT_FIRST_STEP = exports.CommonStepType = void 0;
7
7
  // Copyright 2019-2022 @subwallet/extension-base
8
8
  // SPDX-License-Identifier: Apache-2.0
9
- /* ServiceWithProcess */
10
9
  let CommonStepType;
11
10
  exports.CommonStepType = CommonStepType;
12
11
  (function (CommonStepType) {
@@ -23,13 +23,13 @@ exports.SwapErrorType = SwapErrorType;
23
23
  SwapErrorType["AMOUNT_CANNOT_BE_ZERO"] = "AMOUNT_CANNOT_BE_ZERO";
24
24
  SwapErrorType["NOT_MEET_MIN_EXPECTED"] = "NOT_MEET_MIN_EXPECTED";
25
25
  })(SwapErrorType || (exports.SwapErrorType = SwapErrorType = {}));
26
- let SwapStepType;
26
+ let SwapStepType; // todo: export this to use from sdk
27
27
  exports.SwapStepType = SwapStepType;
28
28
  (function (SwapStepType) {
29
29
  SwapStepType["SWAP"] = "SWAP";
30
30
  SwapStepType["PERMIT"] = "PERMIT";
31
31
  })(SwapStepType || (exports.SwapStepType = SwapStepType = {}));
32
- let SwapProviderId;
32
+ let SwapProviderId; // todo: export this to use from sdk
33
33
  exports.SwapProviderId = SwapProviderId;
34
34
  (function (SwapProviderId) {
35
35
  SwapProviderId["CHAIN_FLIP_TESTNET"] = "CHAIN_FLIP_TESTNET";
@@ -43,13 +43,10 @@ exports.SwapProviderId = SwapProviderId;
43
43
  SwapProviderId["SIMPLE_SWAP"] = "SIMPLE_SWAP";
44
44
  SwapProviderId["UNISWAP"] = "UNISWAP";
45
45
  SwapProviderId["KYBER"] = "KYBER";
46
+ SwapProviderId["OPTIMEX"] = "OPTIMEX";
47
+ SwapProviderId["OPTIMEX_TESTNET"] = "OPTIMEX_TESTNET";
46
48
  })(SwapProviderId || (exports.SwapProviderId = SwapProviderId = {}));
47
- const _SUPPORTED_SWAP_PROVIDERS = [SwapProviderId.CHAIN_FLIP_TESTNET, SwapProviderId.CHAIN_FLIP_MAINNET, SwapProviderId.HYDRADX_MAINNET,
48
- // SwapProviderId.HYDRADX_TESTNET,
49
- SwapProviderId.POLKADOT_ASSET_HUB, SwapProviderId.KUSAMA_ASSET_HUB,
50
- // SwapProviderId.ROCOCO_ASSET_HUB,
51
- // SwapProviderId.WESTEND_ASSET_HUB,
52
- SwapProviderId.SIMPLE_SWAP, SwapProviderId.UNISWAP, SwapProviderId.KYBER];
49
+ const _SUPPORTED_SWAP_PROVIDERS = [SwapProviderId.CHAIN_FLIP_TESTNET, SwapProviderId.CHAIN_FLIP_MAINNET, SwapProviderId.HYDRADX_MAINNET, SwapProviderId.POLKADOT_ASSET_HUB, SwapProviderId.KUSAMA_ASSET_HUB, SwapProviderId.SIMPLE_SWAP, SwapProviderId.UNISWAP, SwapProviderId.KYBER, SwapProviderId.OPTIMEX, SwapProviderId.OPTIMEX_TESTNET];
53
50
  exports._SUPPORTED_SWAP_PROVIDERS = _SUPPORTED_SWAP_PROVIDERS;
54
51
  // process handling
55
52
  let SwapFeeType;
@@ -56,8 +56,7 @@ function reformatAddress(address) {
56
56
  }
57
57
  }
58
58
  const _reformatAddressWithChain = (address, chainInfo) => {
59
- // todo: check for cardano
60
- const chainType = (0, _utils._chainInfoToChainType)(chainInfo);
59
+ const chainType = (0, _utils._chainInfoToAccountChainType)(chainInfo);
61
60
  if (chainType === _types.AccountChainType.SUBSTRATE) {
62
61
  return reformatAddress(address, (0, _utils._getChainSubstrateAddressPrefix)(chainInfo));
63
62
  } else if (chainType === _types.AccountChainType.TON || chainType === _types.AccountChainType.CARDANO) {