@rango-dev/widget-embedded 0.17.1-next.2 → 0.17.1-next.4

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 (197) hide show
  1. package/dist/components/ConfirmWalletsModal/ConfirmWallets.helpers.d.ts +1 -3
  2. package/dist/components/ConfirmWalletsModal/ConfirmWallets.helpers.d.ts.map +1 -1
  3. package/dist/components/ConfirmWalletsModal/ConfirmWalletsModal.d.ts.map +1 -1
  4. package/dist/components/ConfirmWalletsModal/ConfirmWalletsModal.types.d.ts +1 -1
  5. package/dist/components/ConfirmWalletsModal/ConfirmWalletsModal.types.d.ts.map +1 -1
  6. package/dist/components/NoResult/NoResult.d.ts +4 -0
  7. package/dist/components/NoResult/NoResult.d.ts.map +1 -0
  8. package/dist/components/NoResult/NoResult.helpers.d.ts +4 -0
  9. package/dist/components/NoResult/NoResult.helpers.d.ts.map +1 -0
  10. package/dist/components/{NoRoutes/NoRoutes.styles.d.ts → NoResult/NoResult.styles.d.ts} +1 -1
  11. package/dist/components/{NoRoutes/NoRoutes.styles.d.ts.map → NoResult/NoResult.styles.d.ts.map} +1 -1
  12. package/dist/components/{NoRoutes/NoRoutes.types.d.ts → NoResult/NoResult.types.d.ts} +3 -3
  13. package/dist/components/NoResult/NoResult.types.d.ts.map +1 -0
  14. package/dist/components/NoResult/index.d.ts +2 -0
  15. package/dist/components/NoResult/index.d.ts.map +1 -0
  16. package/dist/components/Quote/Quote.d.ts +4 -0
  17. package/dist/components/Quote/Quote.d.ts.map +1 -0
  18. package/dist/components/Quote/Quote.styles.d.ts +1429 -0
  19. package/dist/components/Quote/Quote.styles.d.ts.map +1 -0
  20. package/dist/components/Quote/Quote.types.d.ts +22 -0
  21. package/dist/components/Quote/Quote.types.d.ts.map +1 -0
  22. package/dist/components/Quote/QuoteSkeleton.d.ts +4 -0
  23. package/dist/components/Quote/QuoteSkeleton.d.ts.map +1 -0
  24. package/dist/components/Quote/QuoteSkeleton.styles.d.ts +319 -0
  25. package/dist/components/Quote/QuoteSkeleton.styles.d.ts.map +1 -0
  26. package/dist/components/Quote/QuoteSkeleton.types.d.ts +6 -0
  27. package/dist/components/Quote/QuoteSkeleton.types.d.ts.map +1 -0
  28. package/dist/components/Quote/QuoteSummary.d.ts +11 -0
  29. package/dist/components/Quote/QuoteSummary.d.ts.map +1 -0
  30. package/dist/components/Quote/QuoteSummary.styles.d.ts +158 -0
  31. package/dist/components/Quote/QuoteSummary.styles.d.ts.map +1 -0
  32. package/dist/components/Quote/index.d.ts +4 -0
  33. package/dist/components/Quote/index.d.ts.map +1 -0
  34. package/dist/components/QuoteWarningsAndErrors/HighValueLossWarningModal.d.ts +11 -0
  35. package/dist/components/QuoteWarningsAndErrors/HighValueLossWarningModal.d.ts.map +1 -0
  36. package/dist/components/QuoteWarningsAndErrors/QuoteErrorsModalItem.d.ts +4 -0
  37. package/dist/components/QuoteWarningsAndErrors/QuoteErrorsModalItem.d.ts.map +1 -0
  38. package/dist/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.d.ts +4 -0
  39. package/dist/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.d.ts.map +1 -0
  40. package/dist/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.helpers.d.ts +9 -0
  41. package/dist/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.helpers.d.ts.map +1 -0
  42. package/dist/components/{RouteErrors/RouteErrors.styles.d.ts → QuoteWarningsAndErrors/QuoteWarningsAndErrors.styles.d.ts} +1 -1
  43. package/dist/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.styles.d.ts.map +1 -0
  44. package/dist/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.types.d.ts +22 -0
  45. package/dist/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.types.d.ts.map +1 -0
  46. package/dist/components/QuoteWarningsAndErrors/SlippageWariningModal.d.ts +11 -0
  47. package/dist/components/QuoteWarningsAndErrors/SlippageWariningModal.d.ts.map +1 -0
  48. package/dist/components/QuoteWarningsAndErrors/UnknownPriceWarningModal.d.ts +11 -0
  49. package/dist/components/QuoteWarningsAndErrors/UnknownPriceWarningModal.d.ts.map +1 -0
  50. package/dist/components/QuoteWarningsAndErrors/index.d.ts +2 -0
  51. package/dist/components/QuoteWarningsAndErrors/index.d.ts.map +1 -0
  52. package/dist/components/SwapDetails/SwapDetails.d.ts.map +1 -1
  53. package/dist/components/WidgetEvents.d.ts.map +1 -1
  54. package/dist/constants/errors.d.ts +4 -4
  55. package/dist/constants/errors.d.ts.map +1 -1
  56. package/dist/constants/messages.d.ts +1 -1
  57. package/dist/constants/warnings.d.ts +2 -2
  58. package/dist/constants/warnings.d.ts.map +1 -1
  59. package/dist/containers/QuoteInfo/QuoteInfo.d.ts +4 -0
  60. package/dist/containers/QuoteInfo/QuoteInfo.d.ts.map +1 -0
  61. package/dist/containers/QuoteInfo/QuoteInfo.styles.d.ts +472 -0
  62. package/dist/containers/QuoteInfo/QuoteInfo.styles.d.ts.map +1 -0
  63. package/dist/containers/QuoteInfo/QuoteInfo.types.d.ts +17 -0
  64. package/dist/containers/QuoteInfo/QuoteInfo.types.d.ts.map +1 -0
  65. package/dist/containers/QuoteInfo/index.d.ts +2 -0
  66. package/dist/containers/QuoteInfo/index.d.ts.map +1 -0
  67. package/dist/hooks/useConfirmSwap/index.d.ts +2 -0
  68. package/dist/hooks/useConfirmSwap/index.d.ts.map +1 -0
  69. package/dist/hooks/useConfirmSwap/useConfirmSwap.d.ts +3 -0
  70. package/dist/hooks/useConfirmSwap/useConfirmSwap.d.ts.map +1 -0
  71. package/dist/hooks/useConfirmSwap/useConfirmSwap.helpers.d.ts +16 -0
  72. package/dist/hooks/useConfirmSwap/useConfirmSwap.helpers.d.ts.map +1 -0
  73. package/dist/hooks/{useConfirmSwap.d.ts → useConfirmSwap/useConfirmSwap.types.d.ts} +5 -7
  74. package/dist/hooks/useConfirmSwap/useConfirmSwap.types.d.ts.map +1 -0
  75. package/dist/hooks/{useFetchBestRoute.d.ts → useFetchQuote.d.ts} +3 -3
  76. package/dist/hooks/useFetchQuote.d.ts.map +1 -0
  77. package/dist/hooks/usePrepareBlockchainList/usePrepareBlockchainList.helpers.d.ts +2 -2
  78. package/dist/hooks/useSwapInput.d.ts +4 -2
  79. package/dist/hooks/useSwapInput.d.ts.map +1 -1
  80. package/dist/index.js +1 -1
  81. package/dist/index.js.map +4 -4
  82. package/dist/pages/ConfirmSwapPage.d.ts.map +1 -1
  83. package/dist/pages/Home.d.ts.map +1 -1
  84. package/dist/store/app.d.ts +2 -2
  85. package/dist/store/{bestRoute.d.ts → quote.d.ts} +25 -20
  86. package/dist/store/quote.d.ts.map +1 -0
  87. package/dist/types/index.d.ts +1 -1
  88. package/dist/types/index.d.ts.map +1 -1
  89. package/dist/types/quote.d.ts +82 -0
  90. package/dist/types/quote.d.ts.map +1 -0
  91. package/dist/utils/numbers.d.ts +1 -1
  92. package/dist/utils/numbers.d.ts.map +1 -1
  93. package/dist/utils/{routing.d.ts → quote.d.ts} +16 -12
  94. package/dist/utils/quote.d.ts.map +1 -0
  95. package/dist/utils/settings.d.ts +1 -1
  96. package/dist/utils/swap.d.ts +22 -20
  97. package/dist/utils/swap.d.ts.map +1 -1
  98. package/dist/utils/wallets.d.ts +1 -1
  99. package/package.json +3 -3
  100. package/src/components/BlockchainsSection/BlockchainsSection.tsx +3 -3
  101. package/src/components/ConfirmWalletsModal/ConfirmWallets.helpers.ts +2 -64
  102. package/src/components/ConfirmWalletsModal/ConfirmWalletsModal.tsx +23 -26
  103. package/src/components/ConfirmWalletsModal/ConfirmWalletsModal.types.ts +1 -1
  104. package/src/components/LoadingSwapDetails/LoadingSwapDetails.styles.ts +3 -3
  105. package/src/components/LoadingSwapDetails/LoadingSwapDetails.tsx +6 -6
  106. package/src/components/{NoRoutes/NoRoutes.helper.ts → NoResult/NoResult.helpers.ts} +16 -9
  107. package/src/components/{NoRoutes/NoRoutes.styles.ts → NoResult/NoResult.styles.ts} +2 -0
  108. package/src/components/{NoRoutes/NoRoutes.tsx → NoResult/NoResult.tsx} +7 -12
  109. package/src/components/{NoRoutes/NoRoutes.types.ts → NoResult/NoResult.types.ts} +3 -2
  110. package/src/components/NoResult/index.ts +1 -0
  111. package/src/components/Quote/Quote.styles.ts +212 -0
  112. package/src/components/Quote/Quote.tsx +347 -0
  113. package/src/components/Quote/Quote.types.ts +16 -0
  114. package/src/components/Quote/QuoteSkeleton.styles.ts +110 -0
  115. package/src/components/Quote/QuoteSkeleton.tsx +108 -0
  116. package/src/components/Quote/QuoteSkeleton.types.ts +5 -0
  117. package/src/components/Quote/QuoteSummary.styles.ts +15 -0
  118. package/src/components/Quote/QuoteSummary.tsx +46 -0
  119. package/src/components/Quote/index.ts +3 -0
  120. package/src/components/QuoteWarningsAndErrors/HighValueLossWarningModal.tsx +107 -0
  121. package/src/components/{RouteErrors/RouteErrorsModalItem.tsx → QuoteWarningsAndErrors/QuoteErrorsModalItem.tsx} +3 -3
  122. package/src/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.helpers.ts +67 -0
  123. package/src/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.tsx +103 -0
  124. package/src/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.types.ts +24 -0
  125. package/src/components/QuoteWarningsAndErrors/SlippageWariningModal.tsx +77 -0
  126. package/src/components/QuoteWarningsAndErrors/UnknownPriceWarningModal.tsx +38 -0
  127. package/src/components/QuoteWarningsAndErrors/index.ts +1 -0
  128. package/src/components/SwapDetails/SwapDetails.Placeholder.tsx +1 -1
  129. package/src/components/SwapDetails/SwapDetails.tsx +13 -16
  130. package/src/components/SwitchFromAndTo.tsx +2 -2
  131. package/src/components/UpdateUrl.tsx +12 -12
  132. package/src/components/WidgetEvents.tsx +2 -3
  133. package/src/constants/errors.ts +8 -10
  134. package/src/constants/messages.ts +1 -1
  135. package/src/constants/warnings.ts +7 -7
  136. package/src/containers/QuoteInfo/QuoteInfo.styles.ts +15 -0
  137. package/src/containers/QuoteInfo/QuoteInfo.tsx +97 -0
  138. package/src/containers/QuoteInfo/QuoteInfo.types.ts +17 -0
  139. package/src/containers/QuoteInfo/index.ts +1 -0
  140. package/src/hooks/useConfirmSwap/index.ts +1 -0
  141. package/src/hooks/useConfirmSwap/useConfirmSwap.helpers.ts +168 -0
  142. package/src/hooks/useConfirmSwap/useConfirmSwap.ts +136 -0
  143. package/src/hooks/useConfirmSwap/useConfirmSwap.types.ts +19 -0
  144. package/src/hooks/{useFetchBestRoute.ts → useFetchQuote.ts} +3 -3
  145. package/src/hooks/useNavigateBack.ts +3 -3
  146. package/src/hooks/useSwapInput.ts +57 -23
  147. package/src/hooks/useSyncStoresWithConfig.ts +2 -2
  148. package/src/pages/ConfirmSwapPage.tsx +58 -227
  149. package/src/pages/HistoryPage.tsx +1 -1
  150. package/src/pages/Home.tsx +52 -241
  151. package/src/pages/LanguagePage.tsx +1 -1
  152. package/src/pages/LiquiditySourcePage.tsx +1 -1
  153. package/src/pages/SelectBlockchainPage.tsx +3 -3
  154. package/src/pages/SelectSwapItemsPage.tsx +2 -2
  155. package/src/pages/SettingsPage.tsx +1 -1
  156. package/src/pages/ThemePage.tsx +1 -1
  157. package/src/pages/WalletsPage.tsx +1 -1
  158. package/src/store/{bestRoute.ts → quote.ts} +42 -38
  159. package/src/types/index.ts +1 -1
  160. package/src/types/quote.ts +107 -0
  161. package/src/utils/numbers.ts +1 -1
  162. package/src/utils/quote.ts +338 -0
  163. package/src/utils/swap.ts +99 -69
  164. package/src/utils/wallets.ts +2 -2
  165. package/dist/components/NoRoutes/NoRoutes.d.ts +0 -4
  166. package/dist/components/NoRoutes/NoRoutes.d.ts.map +0 -1
  167. package/dist/components/NoRoutes/NoRoutes.helper.d.ts +0 -3
  168. package/dist/components/NoRoutes/NoRoutes.helper.d.ts.map +0 -1
  169. package/dist/components/NoRoutes/NoRoutes.types.d.ts.map +0 -1
  170. package/dist/components/NoRoutes/index.d.ts +0 -2
  171. package/dist/components/NoRoutes/index.d.ts.map +0 -1
  172. package/dist/components/RouteErrors/RouteErrors.d.ts +0 -4
  173. package/dist/components/RouteErrors/RouteErrors.d.ts.map +0 -1
  174. package/dist/components/RouteErrors/RouteErrors.styles.d.ts.map +0 -1
  175. package/dist/components/RouteErrors/RouteErrors.types.d.ts +0 -24
  176. package/dist/components/RouteErrors/RouteErrors.types.d.ts.map +0 -1
  177. package/dist/components/RouteErrors/RouteErrorsModal.d.ts +0 -4
  178. package/dist/components/RouteErrors/RouteErrorsModal.d.ts.map +0 -1
  179. package/dist/components/RouteErrors/RouteErrorsModalItem.d.ts +0 -4
  180. package/dist/components/RouteErrors/RouteErrorsModalItem.d.ts.map +0 -1
  181. package/dist/components/RouteErrors/index.d.ts +0 -2
  182. package/dist/components/RouteErrors/index.d.ts.map +0 -1
  183. package/dist/hooks/useConfirmSwap.d.ts.map +0 -1
  184. package/dist/hooks/useFetchBestRoute.d.ts.map +0 -1
  185. package/dist/store/bestRoute.d.ts.map +0 -1
  186. package/dist/types/routing.d.ts +0 -65
  187. package/dist/types/routing.d.ts.map +0 -1
  188. package/dist/utils/routing.d.ts.map +0 -1
  189. package/src/components/NoRoutes/index.ts +0 -1
  190. package/src/components/RouteErrors/RouteErrors.tsx +0 -79
  191. package/src/components/RouteErrors/RouteErrors.types.ts +0 -29
  192. package/src/components/RouteErrors/RouteErrorsModal.tsx +0 -129
  193. package/src/components/RouteErrors/index.ts +0 -1
  194. package/src/hooks/useConfirmSwap.ts +0 -303
  195. package/src/types/routing.ts +0 -77
  196. package/src/utils/routing.ts +0 -321
  197. /package/src/components/{RouteErrors/RouteErrors.styles.ts → QuoteWarningsAndErrors/QuoteWarningsAndErrors.styles.ts} +0 -0
@@ -5,6 +5,6 @@ export const swapButtonTitles = () => {
5
5
  connectWallet: i18n.t('Connect Wallet'),
6
6
  swap: i18n.t('Swap'),
7
7
  swapAnyway: i18n.t('Swap anyway'),
8
- ethRouteWarning: i18n.t('The route goes through Ethereum. Continue?'),
8
+ ethWarning: i18n.t('The route goes through Ethereum. Continue?'),
9
9
  };
10
10
  };
@@ -1,14 +1,14 @@
1
- import type { RouteWarning } from '../types';
1
+ import type { QuoteUpdateWarning } from '../types';
2
2
 
3
3
  import { i18n } from '@lingui/core';
4
4
 
5
- import { RouteWarningType } from '../types';
5
+ import { QuoteUpdateType } from '../types';
6
6
 
7
- export function getRouteWarningMessage(warning: RouteWarning) {
7
+ export function getQuoteUpdateWarningMessage(warning: QuoteUpdateWarning) {
8
8
  switch (warning.type) {
9
- case RouteWarningType.ROUTE_UPDATED:
9
+ case QuoteUpdateType.QUOTE_UPDATED:
10
10
  return i18n.t('Route has been updated.');
11
- case RouteWarningType.ROUTE_AND_OUTPUT_AMOUNT_UPDATED:
11
+ case QuoteUpdateType.QUOTE_AND_OUTPUT_AMOUNT_UPDATED:
12
12
  return i18n.t(
13
13
  'Output amount changed to {newOutputAmount} ({percentageChange}% change).',
14
14
  {
@@ -16,9 +16,9 @@ export function getRouteWarningMessage(warning: RouteWarning) {
16
16
  percentageChange: warning.percentageChange,
17
17
  }
18
18
  );
19
- case RouteWarningType.ROUTE_SWAPPERS_UPDATED:
19
+ case QuoteUpdateType.QUOTE_SWAPPERS_UPDATED:
20
20
  return i18n.t('Route swappers has been updated.');
21
- case RouteWarningType.ROUTE_COINS_UPDATED:
21
+ case QuoteUpdateType.QUOTE_COINS_UPDATED:
22
22
  return i18n.t('Route internal coins has been updated.');
23
23
  default:
24
24
  return '';
@@ -0,0 +1,15 @@
1
+ import { styled } from '@rango-dev/ui';
2
+
3
+ export const QuoteContainer = styled('div', {
4
+ width: '100%',
5
+ paddingTop: '$2',
6
+ });
7
+
8
+ export const FooterStepAlarm = styled('div', {
9
+ paddingBottom: '$15',
10
+ });
11
+
12
+ export const FooterAlert = styled('div', {
13
+ width: '100%',
14
+ display: 'flex',
15
+ });
@@ -0,0 +1,97 @@
1
+ import type { PropTypes } from './QuoteInfo.types';
2
+
3
+ import { Divider } from '@rango-dev/ui';
4
+ import React from 'react';
5
+
6
+ import { Quote, QuoteSkeleton } from '../../components/Quote';
7
+ import { QuoteWarningsAndErrors } from '../../components/QuoteWarningsAndErrors';
8
+ import {
9
+ TOKEN_AMOUNT_MAX_DECIMALS,
10
+ TOKEN_AMOUNT_MIN_DECIMALS,
11
+ USD_VALUE_MAX_DECIMALS,
12
+ USD_VALUE_MIN_DECIMALS,
13
+ } from '../../constants/routing';
14
+ import { useQuoteStore } from '../../store/quote';
15
+ import { numberToString } from '../../utils/numbers';
16
+
17
+ import { QuoteContainer } from './QuoteInfo.styles';
18
+
19
+ export function QuoteInfo(props: PropTypes) {
20
+ const {
21
+ quote,
22
+ type,
23
+ loading,
24
+ error,
25
+ warning,
26
+ refetchQuote,
27
+ showWarningModal,
28
+ onOpenWarningModal,
29
+ onCloseWarningModal,
30
+ onConfirmWarningModal,
31
+ expanded = false,
32
+ } = props;
33
+ const { inputAmount, inputUsdValue, outputUsdValue, outputAmount } =
34
+ useQuoteStore();
35
+
36
+ const showQuote = quote && quote.result && !loading;
37
+ return (
38
+ <>
39
+ {loading && (
40
+ <QuoteContainer>
41
+ <QuoteSkeleton type={type} expanded={expanded} />
42
+ </QuoteContainer>
43
+ )}
44
+ {showQuote ? (
45
+ <QuoteContainer>
46
+ <Quote
47
+ quote={quote}
48
+ error={error}
49
+ warning={warning}
50
+ type={type}
51
+ expanded={expanded}
52
+ recommended={true}
53
+ input={{
54
+ value: numberToString(
55
+ inputAmount,
56
+ TOKEN_AMOUNT_MIN_DECIMALS,
57
+ TOKEN_AMOUNT_MAX_DECIMALS
58
+ ),
59
+ usdValue: numberToString(
60
+ inputUsdValue,
61
+ USD_VALUE_MIN_DECIMALS,
62
+ USD_VALUE_MAX_DECIMALS
63
+ ),
64
+ }}
65
+ output={{
66
+ value: numberToString(
67
+ outputAmount,
68
+ TOKEN_AMOUNT_MIN_DECIMALS,
69
+ TOKEN_AMOUNT_MAX_DECIMALS
70
+ ),
71
+ usdValue: numberToString(
72
+ outputUsdValue,
73
+ USD_VALUE_MIN_DECIMALS,
74
+ USD_VALUE_MAX_DECIMALS
75
+ ),
76
+ }}
77
+ />
78
+ </QuoteContainer>
79
+ ) : null}
80
+
81
+ {!warning && !error && <Divider size={16} />}
82
+
83
+ {(warning || error) && (
84
+ <QuoteWarningsAndErrors
85
+ warning={warning}
86
+ error={error}
87
+ loading={loading}
88
+ refetchQuote={refetchQuote}
89
+ showWarningModal={showWarningModal}
90
+ onOpenWarningModal={onOpenWarningModal}
91
+ onCloseWarningModal={onCloseWarningModal}
92
+ onConfirmWarningModal={onConfirmWarningModal}
93
+ />
94
+ )}
95
+ </>
96
+ );
97
+ }
@@ -0,0 +1,17 @@
1
+ import type { QuoteProps } from '../../components/Quote/Quote.types';
2
+ import type { QuoteError, QuoteWarning } from '../../types';
3
+ import type { BestRouteResponse } from 'rango-sdk';
4
+
5
+ export type PropTypes = {
6
+ quote: BestRouteResponse | null;
7
+ type: QuoteProps['type'];
8
+ loading: boolean;
9
+ error: QuoteError | null;
10
+ warning: QuoteWarning | null;
11
+ expanded?: boolean;
12
+ refetchQuote: () => void;
13
+ showWarningModal: boolean;
14
+ onOpenWarningModal: () => void;
15
+ onCloseWarningModal: () => void;
16
+ onConfirmWarningModal: () => void;
17
+ };
@@ -0,0 +1 @@
1
+ export { QuoteInfo } from './QuoteInfo';
@@ -0,0 +1 @@
1
+ export { useConfirmSwap } from './useConfirmSwap';
@@ -0,0 +1,168 @@
1
+ import type { ConfirmSwapFetchResult } from './useConfirmSwap.types';
2
+ import type { ConfirmSwapWarnings, Wallet } from '../../types';
3
+ import type { BestRouteResponse, Token } from 'rango-sdk';
4
+
5
+ import { errorMessages } from '../../constants/errors';
6
+ import { QuoteErrorType, QuoteUpdateType } from '../../types';
7
+ import { numberToString } from '../../utils/numbers';
8
+ import {
9
+ generateQuoteWarnings,
10
+ isNumberOfSwapsChanged,
11
+ isQuoteChanged,
12
+ isQuoteInternalCoinsUpdated,
13
+ isQuoteSwappersUpdated,
14
+ } from '../../utils/quote';
15
+ import {
16
+ checkSlippageErrors,
17
+ getBalanceWarnings,
18
+ getLimitErrorMessage,
19
+ getMinRequiredSlippage,
20
+ getPercentageChange,
21
+ getQuoteOutputAmount,
22
+ hasLimitError,
23
+ isOutputAmountChangedALot,
24
+ } from '../../utils/swap';
25
+
26
+ /**
27
+ * A request can be successful but in body of the response, it can be some case which is considered as failed.
28
+ */
29
+ export function throwErrorIfResponseIsNotValid(response: BestRouteResponse) {
30
+ if (!response.result) {
31
+ throw new Error(errorMessages().noResultError.title, {
32
+ cause: {
33
+ type: QuoteErrorType.NO_RESULT,
34
+ diagnosisMessage: response.diagnosisMessages?.[0],
35
+ },
36
+ });
37
+ }
38
+ const limitError = hasLimitError(response);
39
+ if (limitError) {
40
+ const { swap, recommendation, fromAmountRangeError } =
41
+ getLimitErrorMessage(response);
42
+
43
+ throw new Error('bridge limit error', {
44
+ cause: {
45
+ type: QuoteErrorType.BRIDGE_LIMIT,
46
+ swap: swap,
47
+ recommendation,
48
+ fromAmountRangeError,
49
+ },
50
+ });
51
+ }
52
+
53
+ const recommendedSlippages = checkSlippageErrors(response);
54
+
55
+ if (recommendedSlippages) {
56
+ const minRequiredSlippage = getMinRequiredSlippage(response);
57
+ throw new Error('', {
58
+ cause: {
59
+ type: QuoteErrorType.INSUFFICIENT_SLIPPAGE,
60
+ recommendedSlippages,
61
+ minRequiredSlippage,
62
+ },
63
+ });
64
+ }
65
+
66
+ return response;
67
+ }
68
+
69
+ export function generateWarnings(
70
+ previousQuote: BestRouteResponse,
71
+ currentQuote: BestRouteResponse,
72
+ params: {
73
+ fromToken: Token;
74
+ toToken: Token;
75
+ tokens: Token[];
76
+ selectedWallets: Wallet[];
77
+ userSlippage: number;
78
+ }
79
+ ): ConfirmSwapWarnings {
80
+ const quoteChanged = isQuoteChanged(previousQuote, currentQuote);
81
+ const output: ConfirmSwapWarnings = {
82
+ quote: null,
83
+ quoteUpdate: null,
84
+ balance: null,
85
+ };
86
+
87
+ const quoteWarning = generateQuoteWarnings(currentQuote, {
88
+ fromToken: params.fromToken,
89
+ toToken: params.toToken,
90
+ tokens: params.tokens,
91
+ userSlippage: params.userSlippage,
92
+ });
93
+
94
+ if (quoteWarning) {
95
+ output.quote = quoteWarning;
96
+ }
97
+
98
+ if (quoteChanged) {
99
+ if (isOutputAmountChangedALot(previousQuote, currentQuote)) {
100
+ output.quoteUpdate = {
101
+ type: QuoteUpdateType.QUOTE_AND_OUTPUT_AMOUNT_UPDATED,
102
+ newOutputAmount: numberToString(getQuoteOutputAmount(currentQuote)),
103
+ percentageChange: numberToString(
104
+ getPercentageChange(
105
+ getQuoteOutputAmount(previousQuote) ?? '',
106
+ getQuoteOutputAmount(currentQuote) ?? ''
107
+ ),
108
+ null,
109
+ 2
110
+ ),
111
+ };
112
+ } else if (isNumberOfSwapsChanged(previousQuote, currentQuote)) {
113
+ output.quoteUpdate = {
114
+ type: QuoteUpdateType.QUOTE_UPDATED,
115
+ };
116
+ } else if (isQuoteSwappersUpdated(previousQuote, currentQuote)) {
117
+ output.quoteUpdate = {
118
+ type: QuoteUpdateType.QUOTE_SWAPPERS_UPDATED,
119
+ };
120
+ } else if (isQuoteInternalCoinsUpdated(previousQuote, currentQuote)) {
121
+ output.quoteUpdate = {
122
+ type: QuoteUpdateType.QUOTE_COINS_UPDATED,
123
+ };
124
+ }
125
+ }
126
+
127
+ const balanceWarnings = getBalanceWarnings(
128
+ currentQuote,
129
+ params.selectedWallets
130
+ );
131
+ const enoughBalance = balanceWarnings.length === 0;
132
+
133
+ if (!enoughBalance) {
134
+ output.balance = {
135
+ messages: balanceWarnings,
136
+ };
137
+ }
138
+
139
+ return output;
140
+ }
141
+
142
+ export function handleQuoteErrors(error: any): ConfirmSwapFetchResult {
143
+ if (error?.code === 'ERR_CANCELED') {
144
+ return {
145
+ swap: null,
146
+ error: {
147
+ type: QuoteErrorType.REQUEST_CANCELED,
148
+ },
149
+ warnings: null,
150
+ };
151
+ }
152
+
153
+ if (error.cause) {
154
+ return {
155
+ swap: null,
156
+ error: error.cause,
157
+ warnings: null,
158
+ };
159
+ }
160
+
161
+ return {
162
+ swap: null,
163
+ error: {
164
+ type: QuoteErrorType.REQUEST_FAILED,
165
+ },
166
+ warnings: null,
167
+ };
168
+ }
@@ -0,0 +1,136 @@
1
+ import type { ConfirmSwap, Params } from './useConfirmSwap.types';
2
+ import type { PendingSwapSettings } from '../../types';
3
+ import type { BestRouteResponse } from 'rango-sdk';
4
+
5
+ import { calculatePendingSwap } from '@rango-dev/queue-manager-rango-preset';
6
+ import { useEffect } from 'react';
7
+
8
+ import { useMetaStore } from '../../store/meta';
9
+ import { useQuoteStore } from '../../store/quote';
10
+ import { useSettingsStore } from '../../store/settings';
11
+ import { useWalletsStore } from '../../store/wallets';
12
+ import { QuoteErrorType } from '../../types';
13
+ import { createQuoteRequestBody, getWalletsForNewSwap } from '../../utils/swap';
14
+ import { useFetchQuote } from '../useFetchQuote';
15
+
16
+ import {
17
+ generateWarnings,
18
+ handleQuoteErrors,
19
+ throwErrorIfResponseIsNotValid,
20
+ } from './useConfirmSwap.helpers';
21
+
22
+ export function useConfirmSwap(): ConfirmSwap {
23
+ const {
24
+ fromToken,
25
+ toToken,
26
+ inputAmount,
27
+ setQuote,
28
+ resetQuote,
29
+ quote: initialQuote,
30
+ customDestination: customDestinationFromStore,
31
+ setQuoteWarningsConfirmed,
32
+ } = useQuoteStore();
33
+
34
+ const {
35
+ slippage,
36
+ customSlippage,
37
+ affiliatePercent,
38
+ affiliateRef,
39
+ affiliateWallets,
40
+ disabledLiquiditySources,
41
+ } = useSettingsStore();
42
+ const { connectedWallets } = useWalletsStore();
43
+
44
+ const { meta } = useMetaStore();
45
+
46
+ const userSlippage = customSlippage || slippage;
47
+
48
+ const { fetch: fetchQuote, cancelFetch, loading } = useFetchQuote();
49
+
50
+ useEffect(() => cancelFetch, []);
51
+
52
+ const fetch: ConfirmSwap['fetch'] = async (params: Params) => {
53
+ const selectedWallets = params.selectedWallets;
54
+ const customDestination =
55
+ params?.customDestination ?? customDestinationFromStore;
56
+
57
+ if (!fromToken || !toToken || !inputAmount || !initialQuote) {
58
+ return {
59
+ quote: null,
60
+ swap: null,
61
+ error: null,
62
+ warnings: null,
63
+ };
64
+ }
65
+
66
+ const requestBody = createQuoteRequestBody({
67
+ fromToken,
68
+ toToken,
69
+ inputAmount,
70
+ wallets: connectedWallets,
71
+ selectedWallets,
72
+ disabledLiquiditySources,
73
+ slippage: userSlippage,
74
+ affiliateRef,
75
+ affiliatePercent,
76
+ affiliateWallets,
77
+ initialQuote: initialQuote,
78
+ destination: customDestination,
79
+ });
80
+
81
+ let currentQuote: BestRouteResponse;
82
+ try {
83
+ currentQuote = await fetchQuote(requestBody).then((response) =>
84
+ throwErrorIfResponseIsNotValid(response)
85
+ );
86
+ setQuote(currentQuote);
87
+ } catch (error: any) {
88
+ const confirmSwapResult = handleQuoteErrors(error);
89
+ if (
90
+ confirmSwapResult.error?.type === QuoteErrorType.NO_RESULT ||
91
+ confirmSwapResult.error?.type === QuoteErrorType.REQUEST_FAILED
92
+ ) {
93
+ resetQuote();
94
+ }
95
+ return confirmSwapResult;
96
+ }
97
+
98
+ const swapSettings: PendingSwapSettings = {
99
+ slippage: userSlippage.toString(),
100
+ disabledSwappersGroups: disabledLiquiditySources,
101
+ };
102
+ const swap = calculatePendingSwap(
103
+ inputAmount.toString(),
104
+ currentQuote,
105
+ getWalletsForNewSwap(selectedWallets),
106
+ swapSettings,
107
+ false,
108
+ meta
109
+ );
110
+
111
+ const confirmSwapWarnings = generateWarnings(initialQuote, currentQuote, {
112
+ fromToken,
113
+ toToken,
114
+ tokens: meta.tokens,
115
+ selectedWallets,
116
+ userSlippage,
117
+ });
118
+
119
+ if (confirmSwapWarnings.quoteUpdate) {
120
+ setQuoteWarningsConfirmed(false);
121
+ }
122
+
123
+ return {
124
+ quote: currentQuote,
125
+ swap,
126
+ error: null,
127
+ warnings: confirmSwapWarnings,
128
+ };
129
+ };
130
+
131
+ return {
132
+ loading,
133
+ fetch,
134
+ cancelFetch,
135
+ };
136
+ }
@@ -0,0 +1,19 @@
1
+ import type { ConfirmSwapWarnings, QuoteError, Wallet } from '../../types';
2
+ import type { PendingSwap } from 'rango-types/lib';
3
+
4
+ export type ConfirmSwapFetchResult = {
5
+ swap: PendingSwap | null;
6
+ error: QuoteError | null;
7
+ warnings: ConfirmSwapWarnings | null;
8
+ };
9
+
10
+ export type ConfirmSwap = {
11
+ loading: boolean;
12
+ fetch: (params: Params) => Promise<ConfirmSwapFetchResult>;
13
+ cancelFetch: () => void;
14
+ };
15
+
16
+ export type Params = {
17
+ selectedWallets: Wallet[];
18
+ customDestination?: string;
19
+ };
@@ -4,18 +4,18 @@ import { useRef, useState } from 'react';
4
4
 
5
5
  import { httpService } from '../services/httpService';
6
6
 
7
- interface UseFetchBestRoute {
7
+ interface UseFetchQuote {
8
8
  fetch: (requestBody: BestRouteRequest) => Promise<BestRouteResponse>;
9
9
  cancelFetch: () => void;
10
10
  loading: boolean;
11
11
  }
12
12
 
13
- export function useFetchBestRoute(): UseFetchBestRoute {
13
+ export function useFetchQuote(): UseFetchQuote {
14
14
  const [loading, setLoading] = useState(false);
15
15
  const abortController = useRef<AbortController | null>(null);
16
16
  const cancel = () => abortController.current?.abort();
17
17
 
18
- const fetch: UseFetchBestRoute['fetch'] = async (requestBody) => {
18
+ const fetch: UseFetchQuote['fetch'] = async (requestBody) => {
19
19
  cancel();
20
20
  abortController.current = new AbortController();
21
21
 
@@ -1,12 +1,12 @@
1
1
  import { useInRouterContext, useNavigate } from 'react-router-dom';
2
2
 
3
3
  import { navigationRoutes } from '../constants/navigationRoutes';
4
- import { useBestRouteStore } from '../store/bestRoute';
4
+ import { useQuoteStore } from '../store/quote';
5
5
 
6
6
  export function useNavigateBack() {
7
7
  const isRouterInContext = useInRouterContext();
8
8
  const navigate = useNavigate();
9
- const routeWalletsConfirmed = useBestRouteStore.use.routeWalletsConfirmed();
9
+ const quoteWalletsConfirmed = useQuoteStore.use.quoteWalletsConfirmed();
10
10
 
11
11
  navigationRoutes;
12
12
 
@@ -26,7 +26,7 @@ export function useNavigateBack() {
26
26
  [navigationRoutes.settings, navigationRoutes.wallets].includes(
27
27
  currentRoute
28
28
  ) &&
29
- routeWalletsConfirmed
29
+ quoteWalletsConfirmed
30
30
  ) {
31
31
  return navigate('/' + navigationRoutes.confirmSwap, { replace: true });
32
32
  }