@rango-dev/widget-embedded 0.17.1-next.2 → 0.17.1-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.
Files changed (195) 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/constants/errors.d.ts +4 -4
  54. package/dist/constants/errors.d.ts.map +1 -1
  55. package/dist/constants/messages.d.ts +1 -1
  56. package/dist/constants/warnings.d.ts +2 -2
  57. package/dist/constants/warnings.d.ts.map +1 -1
  58. package/dist/containers/QuoteInfo/QuoteInfo.d.ts +4 -0
  59. package/dist/containers/QuoteInfo/QuoteInfo.d.ts.map +1 -0
  60. package/dist/containers/QuoteInfo/QuoteInfo.styles.d.ts +472 -0
  61. package/dist/containers/QuoteInfo/QuoteInfo.styles.d.ts.map +1 -0
  62. package/dist/containers/QuoteInfo/QuoteInfo.types.d.ts +17 -0
  63. package/dist/containers/QuoteInfo/QuoteInfo.types.d.ts.map +1 -0
  64. package/dist/containers/QuoteInfo/index.d.ts +2 -0
  65. package/dist/containers/QuoteInfo/index.d.ts.map +1 -0
  66. package/dist/hooks/useConfirmSwap/index.d.ts +2 -0
  67. package/dist/hooks/useConfirmSwap/index.d.ts.map +1 -0
  68. package/dist/hooks/useConfirmSwap/useConfirmSwap.d.ts +3 -0
  69. package/dist/hooks/useConfirmSwap/useConfirmSwap.d.ts.map +1 -0
  70. package/dist/hooks/useConfirmSwap/useConfirmSwap.helpers.d.ts +16 -0
  71. package/dist/hooks/useConfirmSwap/useConfirmSwap.helpers.d.ts.map +1 -0
  72. package/dist/hooks/{useConfirmSwap.d.ts → useConfirmSwap/useConfirmSwap.types.d.ts} +5 -7
  73. package/dist/hooks/useConfirmSwap/useConfirmSwap.types.d.ts.map +1 -0
  74. package/dist/hooks/{useFetchBestRoute.d.ts → useFetchQuote.d.ts} +3 -3
  75. package/dist/hooks/useFetchQuote.d.ts.map +1 -0
  76. package/dist/hooks/usePrepareBlockchainList/usePrepareBlockchainList.helpers.d.ts +2 -2
  77. package/dist/hooks/useSwapInput.d.ts +4 -2
  78. package/dist/hooks/useSwapInput.d.ts.map +1 -1
  79. package/dist/index.js +1 -1
  80. package/dist/index.js.map +4 -4
  81. package/dist/pages/ConfirmSwapPage.d.ts.map +1 -1
  82. package/dist/pages/Home.d.ts.map +1 -1
  83. package/dist/store/app.d.ts +2 -2
  84. package/dist/store/{bestRoute.d.ts → quote.d.ts} +25 -20
  85. package/dist/store/quote.d.ts.map +1 -0
  86. package/dist/types/index.d.ts +1 -1
  87. package/dist/types/index.d.ts.map +1 -1
  88. package/dist/types/quote.d.ts +82 -0
  89. package/dist/types/quote.d.ts.map +1 -0
  90. package/dist/utils/numbers.d.ts +1 -1
  91. package/dist/utils/numbers.d.ts.map +1 -1
  92. package/dist/utils/{routing.d.ts → quote.d.ts} +16 -12
  93. package/dist/utils/quote.d.ts.map +1 -0
  94. package/dist/utils/settings.d.ts +1 -1
  95. package/dist/utils/swap.d.ts +22 -20
  96. package/dist/utils/swap.d.ts.map +1 -1
  97. package/dist/utils/wallets.d.ts +1 -1
  98. package/package.json +2 -2
  99. package/src/components/BlockchainsSection/BlockchainsSection.tsx +3 -3
  100. package/src/components/ConfirmWalletsModal/ConfirmWallets.helpers.ts +2 -64
  101. package/src/components/ConfirmWalletsModal/ConfirmWalletsModal.tsx +23 -26
  102. package/src/components/ConfirmWalletsModal/ConfirmWalletsModal.types.ts +1 -1
  103. package/src/components/LoadingSwapDetails/LoadingSwapDetails.styles.ts +3 -3
  104. package/src/components/LoadingSwapDetails/LoadingSwapDetails.tsx +6 -6
  105. package/src/components/{NoRoutes/NoRoutes.helper.ts → NoResult/NoResult.helpers.ts} +16 -9
  106. package/src/components/{NoRoutes/NoRoutes.styles.ts → NoResult/NoResult.styles.ts} +2 -0
  107. package/src/components/{NoRoutes/NoRoutes.tsx → NoResult/NoResult.tsx} +7 -12
  108. package/src/components/{NoRoutes/NoRoutes.types.ts → NoResult/NoResult.types.ts} +3 -2
  109. package/src/components/NoResult/index.ts +1 -0
  110. package/src/components/Quote/Quote.styles.ts +212 -0
  111. package/src/components/Quote/Quote.tsx +347 -0
  112. package/src/components/Quote/Quote.types.ts +16 -0
  113. package/src/components/Quote/QuoteSkeleton.styles.ts +110 -0
  114. package/src/components/Quote/QuoteSkeleton.tsx +108 -0
  115. package/src/components/Quote/QuoteSkeleton.types.ts +5 -0
  116. package/src/components/Quote/QuoteSummary.styles.ts +15 -0
  117. package/src/components/Quote/QuoteSummary.tsx +46 -0
  118. package/src/components/Quote/index.ts +3 -0
  119. package/src/components/QuoteWarningsAndErrors/HighValueLossWarningModal.tsx +107 -0
  120. package/src/components/{RouteErrors/RouteErrorsModalItem.tsx → QuoteWarningsAndErrors/QuoteErrorsModalItem.tsx} +3 -3
  121. package/src/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.helpers.ts +67 -0
  122. package/src/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.tsx +103 -0
  123. package/src/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.types.ts +24 -0
  124. package/src/components/QuoteWarningsAndErrors/SlippageWariningModal.tsx +77 -0
  125. package/src/components/QuoteWarningsAndErrors/UnknownPriceWarningModal.tsx +38 -0
  126. package/src/components/QuoteWarningsAndErrors/index.ts +1 -0
  127. package/src/components/SwapDetails/SwapDetails.Placeholder.tsx +1 -1
  128. package/src/components/SwapDetails/SwapDetails.tsx +13 -16
  129. package/src/components/SwitchFromAndTo.tsx +2 -2
  130. package/src/components/UpdateUrl.tsx +12 -12
  131. package/src/constants/errors.ts +8 -10
  132. package/src/constants/messages.ts +1 -1
  133. package/src/constants/warnings.ts +7 -7
  134. package/src/containers/QuoteInfo/QuoteInfo.styles.ts +15 -0
  135. package/src/containers/QuoteInfo/QuoteInfo.tsx +97 -0
  136. package/src/containers/QuoteInfo/QuoteInfo.types.ts +17 -0
  137. package/src/containers/QuoteInfo/index.ts +1 -0
  138. package/src/hooks/useConfirmSwap/index.ts +1 -0
  139. package/src/hooks/useConfirmSwap/useConfirmSwap.helpers.ts +168 -0
  140. package/src/hooks/useConfirmSwap/useConfirmSwap.ts +136 -0
  141. package/src/hooks/useConfirmSwap/useConfirmSwap.types.ts +19 -0
  142. package/src/hooks/{useFetchBestRoute.ts → useFetchQuote.ts} +3 -3
  143. package/src/hooks/useNavigateBack.ts +3 -3
  144. package/src/hooks/useSwapInput.ts +57 -23
  145. package/src/hooks/useSyncStoresWithConfig.ts +2 -2
  146. package/src/pages/ConfirmSwapPage.tsx +58 -227
  147. package/src/pages/HistoryPage.tsx +1 -1
  148. package/src/pages/Home.tsx +52 -241
  149. package/src/pages/LanguagePage.tsx +1 -1
  150. package/src/pages/LiquiditySourcePage.tsx +1 -1
  151. package/src/pages/SelectBlockchainPage.tsx +3 -3
  152. package/src/pages/SelectSwapItemsPage.tsx +2 -2
  153. package/src/pages/SettingsPage.tsx +1 -1
  154. package/src/pages/ThemePage.tsx +1 -1
  155. package/src/pages/WalletsPage.tsx +1 -1
  156. package/src/store/{bestRoute.ts → quote.ts} +42 -38
  157. package/src/types/index.ts +1 -1
  158. package/src/types/quote.ts +107 -0
  159. package/src/utils/numbers.ts +1 -1
  160. package/src/utils/quote.ts +338 -0
  161. package/src/utils/swap.ts +99 -69
  162. package/src/utils/wallets.ts +2 -2
  163. package/dist/components/NoRoutes/NoRoutes.d.ts +0 -4
  164. package/dist/components/NoRoutes/NoRoutes.d.ts.map +0 -1
  165. package/dist/components/NoRoutes/NoRoutes.helper.d.ts +0 -3
  166. package/dist/components/NoRoutes/NoRoutes.helper.d.ts.map +0 -1
  167. package/dist/components/NoRoutes/NoRoutes.types.d.ts.map +0 -1
  168. package/dist/components/NoRoutes/index.d.ts +0 -2
  169. package/dist/components/NoRoutes/index.d.ts.map +0 -1
  170. package/dist/components/RouteErrors/RouteErrors.d.ts +0 -4
  171. package/dist/components/RouteErrors/RouteErrors.d.ts.map +0 -1
  172. package/dist/components/RouteErrors/RouteErrors.styles.d.ts.map +0 -1
  173. package/dist/components/RouteErrors/RouteErrors.types.d.ts +0 -24
  174. package/dist/components/RouteErrors/RouteErrors.types.d.ts.map +0 -1
  175. package/dist/components/RouteErrors/RouteErrorsModal.d.ts +0 -4
  176. package/dist/components/RouteErrors/RouteErrorsModal.d.ts.map +0 -1
  177. package/dist/components/RouteErrors/RouteErrorsModalItem.d.ts +0 -4
  178. package/dist/components/RouteErrors/RouteErrorsModalItem.d.ts.map +0 -1
  179. package/dist/components/RouteErrors/index.d.ts +0 -2
  180. package/dist/components/RouteErrors/index.d.ts.map +0 -1
  181. package/dist/hooks/useConfirmSwap.d.ts.map +0 -1
  182. package/dist/hooks/useFetchBestRoute.d.ts.map +0 -1
  183. package/dist/store/bestRoute.d.ts.map +0 -1
  184. package/dist/types/routing.d.ts +0 -65
  185. package/dist/types/routing.d.ts.map +0 -1
  186. package/dist/utils/routing.d.ts.map +0 -1
  187. package/src/components/NoRoutes/index.ts +0 -1
  188. package/src/components/RouteErrors/RouteErrors.tsx +0 -79
  189. package/src/components/RouteErrors/RouteErrors.types.ts +0 -29
  190. package/src/components/RouteErrors/RouteErrorsModal.tsx +0 -129
  191. package/src/components/RouteErrors/index.ts +0 -1
  192. package/src/hooks/useConfirmSwap.ts +0 -303
  193. package/src/types/routing.ts +0 -77
  194. package/src/utils/routing.ts +0 -321
  195. /package/src/components/{RouteErrors/RouteErrors.styles.ts → QuoteWarningsAndErrors/QuoteWarningsAndErrors.styles.ts} +0 -0
@@ -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
  }
@@ -1,37 +1,49 @@
1
+ import type { QuoteError, QuoteWarning } from '../types';
2
+
1
3
  import { useEffect, useRef, useState } from 'react';
2
4
 
3
- import { useBestRouteStore } from '../store/bestRoute';
5
+ import { useMetaStore } from '../store/meta';
6
+ import { useQuoteStore } from '../store/quote';
4
7
  import { useSettingsStore } from '../store/settings';
8
+ import { QuoteErrorType } from '../types';
5
9
  import { debounce } from '../utils/common';
6
10
  import { isPositiveNumber } from '../utils/numbers';
7
- import { createBestRouteRequestBody } from '../utils/swap';
11
+ import { generateQuoteWarnings } from '../utils/quote';
12
+ import { createQuoteRequestBody } from '../utils/swap';
8
13
  import { tokensAreEqual } from '../utils/wallets';
9
14
 
10
- import { useFetchBestRoute } from './useFetchBestRoute';
15
+ import {
16
+ handleQuoteErrors,
17
+ throwErrorIfResponseIsNotValid,
18
+ } from './useConfirmSwap/useConfirmSwap.helpers';
19
+ import { useFetchQuote } from './useFetchQuote';
11
20
 
12
21
  const DEBOUNCE_DELAY = 600;
13
22
 
14
23
  type UseSwapInput = {
15
24
  fetch: () => void;
16
25
  loading: boolean;
17
- error: string;
26
+ error: QuoteError | null;
27
+ warning: QuoteWarning | null;
18
28
  };
19
29
 
20
30
  /**
21
- * a hook for fetching best route based on from and to input values
31
+ * a hook for fetching quote based on from and to input values
22
32
  * we use this hook in home page
23
33
  */
24
34
  export function useSwapInput(): UseSwapInput {
25
- const { fetch: fetchBestRoute, cancelFetch } = useFetchBestRoute();
26
- const [loading, setLoading] = useState(false);
35
+ const { fetch: fetchQuote, cancelFetch } = useFetchQuote();
27
36
  const {
28
37
  fromToken,
29
38
  toToken,
30
39
  inputAmount,
31
40
  inputUsdValue,
32
- resetRoute,
33
- setRoute,
34
- } = useBestRouteStore();
41
+ resetQuote,
42
+ setQuote,
43
+ } = useQuoteStore();
44
+ const {
45
+ meta: { tokens },
46
+ } = useMetaStore();
35
47
  const {
36
48
  slippage,
37
49
  customSlippage,
@@ -40,7 +52,9 @@ export function useSwapInput(): UseSwapInput {
40
52
  affiliateWallets,
41
53
  disabledLiquiditySources,
42
54
  } = useSettingsStore();
43
- const [error, setError] = useState('');
55
+ const [loading, setLoading] = useState(false);
56
+ const [error, setError] = useState<QuoteError | null>(null);
57
+ const [warning, setWarning] = useState<QuoteWarning | null>(null);
44
58
  const prevDisabledLiquiditySources = useRef(disabledLiquiditySources);
45
59
  const userSlippage = customSlippage ?? slippage;
46
60
  const hasTokensValue = !fromToken || !toToken;
@@ -49,13 +63,19 @@ export function useSwapInput(): UseSwapInput {
49
63
  tokensAreEqual(fromToken, toToken) ||
50
64
  !isPositiveNumber(inputAmount);
51
65
 
66
+ const resetState = (loading: boolean) => {
67
+ setLoading(loading);
68
+ setError(null);
69
+ setWarning(null);
70
+ };
71
+
52
72
  const fetch: UseSwapInput['fetch'] = () => {
53
73
  if (!loading) {
54
- setLoading(true);
74
+ resetState(true);
55
75
  }
56
76
  if (!shouldSkipRequest) {
57
- resetRoute();
58
- const requestBody = createBestRouteRequestBody({
77
+ resetQuote();
78
+ const requestBody = createQuoteRequestBody({
59
79
  fromToken,
60
80
  toToken,
61
81
  inputAmount,
@@ -65,15 +85,29 @@ export function useSwapInput(): UseSwapInput {
65
85
  affiliatePercent,
66
86
  affiliateWallets,
67
87
  });
68
- fetchBestRoute(requestBody)
88
+ fetchQuote(requestBody)
69
89
  .then((res) => {
70
90
  setLoading(false);
71
- setRoute(res);
91
+ setQuote(res);
92
+ throwErrorIfResponseIsNotValid(res);
93
+ const quoteWarning = generateQuoteWarnings(res, {
94
+ fromToken,
95
+ toToken,
96
+ userSlippage,
97
+ tokens,
98
+ });
99
+ setWarning(quoteWarning);
72
100
  })
73
101
  .catch((error) => {
74
- resetRoute();
75
- if (error?.code !== 'ERR_CANCELED') {
76
- setError(error.message);
102
+ const { error: quoteError } = handleQuoteErrors(error);
103
+ if (
104
+ quoteError?.type === QuoteErrorType.NO_RESULT ||
105
+ quoteError?.type === QuoteErrorType.REQUEST_FAILED
106
+ ) {
107
+ resetQuote();
108
+ }
109
+ if (quoteError?.type !== QuoteErrorType.REQUEST_CANCELED) {
110
+ setError(quoteError);
77
111
  setLoading(false);
78
112
  }
79
113
  });
@@ -88,15 +122,15 @@ export function useSwapInput(): UseSwapInput {
88
122
 
89
123
  useEffect(() => {
90
124
  if (!isPositiveNumber(inputAmount) || inputUsdValue?.eq(0)) {
91
- setLoading(false);
125
+ resetState(false);
92
126
  cancelFetch();
93
127
  return;
94
128
  }
95
129
  if (shouldSkipRequest) {
96
130
  return;
97
131
  }
98
- resetRoute();
99
- setLoading(true);
132
+ resetQuote();
133
+ resetState(true);
100
134
  debouncedFetch();
101
135
  return cancelFetch;
102
136
  }, [inputAmount, shouldSkipRequest]);
@@ -112,5 +146,5 @@ export function useSwapInput(): UseSwapInput {
112
146
  return cancelFetch;
113
147
  }, [disabledLiquiditySources.length]);
114
148
 
115
- return { fetch, loading, error };
149
+ return { fetch, loading, error, warning };
116
150
  }