@rango-dev/queue-manager-rango-preset 0.1.10 → 0.1.11-next.3

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.
@@ -1,5 +1,6 @@
1
1
  import { SignerError, isAPIErrorCode, isSignerErrorCode, SignerErrorCode } from 'rango-types';
2
2
  import { WalletType, Network, getBlockChainNameFromId } from '@rango-dev/wallets-shared';
3
+ import BigNumber from 'bignumber.js';
3
4
  import { readAccountAddress } from '@rango-dev/wallets-core';
4
5
  import { RangoClient, TransactionType } from 'rango-sdk';
5
6
  import { Status, Persistor, DB_NAME } from '@rango-dev/queue-manager-core';
@@ -522,6 +523,34 @@ var prettifyErrorMessage = function prettifyErrorMessage(obj) {
522
523
  };
523
524
  };
524
525
 
526
+ var numberToString = function numberToString(number, minDecimals, maxDecimals) {
527
+ if (minDecimals === void 0) {
528
+ minDecimals = null;
529
+ }
530
+ if (maxDecimals === void 0) {
531
+ maxDecimals = null;
532
+ }
533
+ if (number === null) return '';
534
+ if (number === '') return '';
535
+ var n = new BigNumber(number);
536
+ var roundingMode = 1;
537
+ var maxI = 1000;
538
+ for (var i = 0; i < 60; i++) {
539
+ if (new BigNumber(n.toFixed(i, roundingMode)).eq(n)) {
540
+ maxI = i;
541
+ break;
542
+ }
543
+ }
544
+ if (n.gte(10000)) return n.toFormat(0, roundingMode);
545
+ if (n.gte(1000)) return n.toFormat(Math.min(maxI, Math.min(maxDecimals || 100, Math.max(minDecimals || 0, 1))), roundingMode);
546
+ if (n.gte(100)) return n.toFormat(Math.min(maxI, Math.min(maxDecimals || 100, Math.max(minDecimals || 0, 1))), roundingMode);
547
+ if (n.gte(1)) return n.toFormat(Math.min(maxI, Math.min(maxDecimals || 100, Math.max(minDecimals || 0, 2))), roundingMode);
548
+ if (n.gte(0.01)) return n.toFormat(Math.min(maxI, Math.min(maxDecimals || 100, Math.max(minDecimals || 0, 4))), roundingMode);
549
+ for (var _i = minDecimals || 4; _i < 17; _i++) if (n.gte(Math.pow(10, -_i))) return n.toFormat(Math.min(maxI, Math.min(maxDecimals || 100, Math.max(minDecimals || 0, _i))), roundingMode);
550
+ if (n.isEqualTo(0)) return '0';
551
+ return n.toFormat(Math.min(maxI, Math.min(maxDecimals || 100, Math.max(minDecimals || 0, 8))), roundingMode);
552
+ };
553
+
525
554
  var PendingSwapNetworkStatus;
526
555
  (function (PendingSwapNetworkStatus) {
527
556
  PendingSwapNetworkStatus["WaitingForConnectingWallet"] = "waitingForConnectingWallet";
@@ -613,6 +642,102 @@ function getRelatedWalletOrNull(swap, currentStep) {
613
642
  return null;
614
643
  }
615
644
  }
645
+ var getUsdPrice = function getUsdPrice(blockchain, symbol, address, allTokens) {
646
+ var token = allTokens == null ? void 0 : allTokens.find(function (t) {
647
+ var _t$symbol;
648
+ return t.blockchain === blockchain && ((_t$symbol = t.symbol) == null ? void 0 : _t$symbol.toUpperCase()) === (symbol == null ? void 0 : symbol.toUpperCase()) && t.address === address;
649
+ });
650
+ return (token == null ? void 0 : token.usdPrice) || null;
651
+ };
652
+ function getUsdFeeOfStep(step, allTokens) {
653
+ var totalFeeInUsd = new BigNumber(0);
654
+ for (var i = 0; i < step.fee.length; i++) {
655
+ var fee = step.fee[i];
656
+ if (fee.expenseType === 'DECREASE_FROM_OUTPUT') continue;
657
+ var unitPrice = getUsdPrice(fee.asset.blockchain, fee.asset.symbol, fee.asset.address, allTokens);
658
+ totalFeeInUsd = totalFeeInUsd.plus(new BigNumber(fee.amount).multipliedBy(unitPrice || 0));
659
+ }
660
+ return totalFeeInUsd;
661
+ }
662
+ function calculatePendingSwap(inputAmount, bestRoute, wallets, settings, validateBalanceOrFee, meta) {
663
+ var _bestRoute$result, _bestRoute$result$swa;
664
+ var simulationResult = bestRoute.result;
665
+ if (!simulationResult) throw Error('Simulation result should not be null');
666
+ return {
667
+ creationTime: new Date().getTime().toString(),
668
+ finishTime: null,
669
+ requestId: bestRoute.requestId || '',
670
+ inputAmount: inputAmount,
671
+ wallets: wallets,
672
+ status: 'running',
673
+ isPaused: false,
674
+ extraMessage: null,
675
+ extraMessageSeverity: null,
676
+ extraMessageDetail: null,
677
+ extraMessageErrorCode: null,
678
+ networkStatusExtraMessage: null,
679
+ networkStatusExtraMessageDetail: null,
680
+ lastNotificationTime: null,
681
+ settings: settings,
682
+ simulationResult: simulationResult,
683
+ validateBalanceOrFee: validateBalanceOrFee,
684
+ steps: ((_bestRoute$result = bestRoute.result) == null ? void 0 : (_bestRoute$result$swa = _bestRoute$result.swaps) == null ? void 0 : _bestRoute$result$swa.map(function (swap, index) {
685
+ return {
686
+ id: index + 1,
687
+ // from
688
+ fromBlockchain: swap.from.blockchain,
689
+ fromBlockchainLogo: swap.from.blockchainLogo,
690
+ fromLogo: swap.from.logo,
691
+ fromSymbol: swap.from.symbol,
692
+ fromSymbolAddress: swap.from.address,
693
+ fromDecimals: swap.from.decimals,
694
+ fromAmountPrecision: swap.fromAmountPrecision,
695
+ fromAmountMinValue: swap.fromAmountMinValue,
696
+ fromAmountMaxValue: swap.fromAmountMaxValue,
697
+ fromAmountRestrictionType: swap.fromAmountRestrictionType,
698
+ // to
699
+ toBlockchain: swap.to.blockchain,
700
+ toBlockchainLogo: swap.to.blockchainLogo,
701
+ toSymbol: swap.to.symbol,
702
+ toSymbolAddress: swap.to.address,
703
+ toDecimals: swap.to.decimals,
704
+ toLogo: swap.to.logo,
705
+ // swapper
706
+ swapperId: swap.swapperId,
707
+ swapperLogo: swap.swapperLogo,
708
+ swapperType: swap.swapperType,
709
+ // output, fee, timing
710
+ expectedOutputAmountHumanReadable: swap.toAmount,
711
+ outputAmount: '',
712
+ feeInUsd: meta ? numberToString(getUsdFeeOfStep(swap, meta == null ? void 0 : meta.tokens), null, 8) : null,
713
+ estimatedTimeInSeconds: swap.estimatedTimeInSeconds || null,
714
+ // status, tracking
715
+ status: 'created',
716
+ networkStatus: null,
717
+ startTransactionTime: new Date().getTime(),
718
+ externalTransactionId: null,
719
+ executedTransactionId: null,
720
+ executedTransactionTime: null,
721
+ explorerUrl: null,
722
+ diagnosisUrl: null,
723
+ trackingCode: null,
724
+ internalSteps: null,
725
+ // transactions
726
+ evmTransaction: null,
727
+ evmApprovalTransaction: null,
728
+ starknetTransaction: null,
729
+ starknetApprovalTransaction: null,
730
+ tronTransaction: null,
731
+ tronApprovalTransaction: null,
732
+ cosmosTransaction: null,
733
+ solanaTransaction: null,
734
+ transferTransaction: null,
735
+ // front fields
736
+ hasAlreadyProceededToSign: false
737
+ };
738
+ })) || []
739
+ };
740
+ }
616
741
 
617
742
  var SwapActionTypes;
618
743
  (function (SwapActionTypes) {
@@ -660,7 +785,10 @@ function logRPCError(error, swap, currentStep, walletType) {
660
785
  }
661
786
  }
662
787
 
663
- var httpService = /*#__PURE__*/new RangoClient(RANGO_DAPP_API_KEY || '', RANGO_DAPP_API_BASE_URL);
788
+ // this API key is limited and
789
+ // it is only for test purpose
790
+ var RANGO_PUBLIC_API_KEY = 'c6381a79-2817-4602-83bf-6a641a409e32';
791
+ var httpService = /*#__PURE__*/new RangoClient(RANGO_DAPP_API_KEY || RANGO_PUBLIC_API_KEY, RANGO_DAPP_API_BASE_URL);
664
792
 
665
793
  var swapClaimedBy = null;
666
794
  /**
@@ -2770,5 +2898,5 @@ function useQueueManager(params) {
2770
2898
  }, [params.disconnectedWallet]);
2771
2899
  }
2772
2900
 
2773
- export { MessageSeverity, PendingSwapNetworkStatus, PrettyError, cancelSwap, checkWaitingForNetworkChange, getCurrentBlockchainOfOrNull, getCurrentStep, getEvmProvider, getRelatedWallet, getRelatedWalletOrNull, getRequiredWallet, getRunningSwaps, prettifyErrorMessage, resetRunningSwapNotifsOnPageLoad, splitWalletNetwork, swapQueueDef, updateSwapStatus, useMigration, useQueueManager };
2901
+ export { MessageSeverity, PendingSwapNetworkStatus, PrettyError, calculatePendingSwap, cancelSwap, checkWaitingForNetworkChange, getCurrentBlockchainOfOrNull, getCurrentStep, getEvmProvider, getRelatedWallet, getRelatedWalletOrNull, getRequiredWallet, getRunningSwaps, prettifyErrorMessage, resetRunningSwapNotifsOnPageLoad, splitWalletNetwork, swapQueueDef, updateSwapStatus, useMigration, useQueueManager };
2774
2902
  //# sourceMappingURL=queue-manager-rango-preset.esm.js.map