@rango-dev/widget-embedded 0.22.4-next.1 → 0.22.4-next.11

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 (203) hide show
  1. package/dist/QueueManager.d.ts.map +1 -1
  2. package/dist/components/AppRoutes.d.ts.map +1 -1
  3. package/dist/components/BlockchainList/BlockchainList.styles.d.ts +3 -0
  4. package/dist/components/BlockchainList/BlockchainList.styles.d.ts.map +1 -1
  5. package/dist/components/BlockchainsSection/BlockchainsSection.styles.d.ts +1 -0
  6. package/dist/components/BlockchainsSection/BlockchainsSection.styles.d.ts.map +1 -1
  7. package/dist/components/ConfirmWalletsModal/ConfirmWallets.helpers.d.ts +2 -2
  8. package/dist/components/ConfirmWalletsModal/ConfirmWallets.helpers.d.ts.map +1 -1
  9. package/dist/components/ConfirmWalletsModal/ConfirmWallets.styles.d.ts +15 -0
  10. package/dist/components/ConfirmWalletsModal/ConfirmWallets.styles.d.ts.map +1 -1
  11. package/dist/components/ConfirmWalletsModal/ConfirmWalletsModal.d.ts.map +1 -1
  12. package/dist/components/ConfirmWalletsModal/WalletList.d.ts.map +1 -1
  13. package/dist/components/ConfirmWalletsModal/WalletList.styles.d.ts +3 -0
  14. package/dist/components/ConfirmWalletsModal/WalletList.styles.d.ts.map +1 -1
  15. package/dist/components/CustomCollapsible/CustomCollapsible.styles.d.ts +4 -0
  16. package/dist/components/CustomCollapsible/CustomCollapsible.styles.d.ts.map +1 -1
  17. package/dist/components/HeaderButtons/HeaderButtons.d.ts +4 -0
  18. package/dist/components/HeaderButtons/HeaderButtons.d.ts.map +1 -0
  19. package/dist/components/HeaderButtons/HeaderButtons.styles.d.ts +5 -0
  20. package/dist/components/HeaderButtons/HeaderButtons.styles.d.ts.map +1 -1
  21. package/dist/components/HeaderButtons/HeaderButtons.types.d.ts +3 -2
  22. package/dist/components/HeaderButtons/HeaderButtons.types.d.ts.map +1 -1
  23. package/dist/components/HeaderButtons/index.d.ts +1 -1
  24. package/dist/components/HeaderButtons/index.d.ts.map +1 -1
  25. package/dist/components/HistoryGroupedList/HistoryGroupedList.styles.d.ts +6 -0
  26. package/dist/components/HistoryGroupedList/HistoryGroupedList.styles.d.ts.map +1 -1
  27. package/dist/components/Layout/Layout.d.ts.map +1 -1
  28. package/dist/components/Layout/Layout.styles.d.ts +3 -0
  29. package/dist/components/Layout/Layout.styles.d.ts.map +1 -1
  30. package/dist/components/Layout/Layout.types.d.ts +1 -0
  31. package/dist/components/Layout/Layout.types.d.ts.map +1 -1
  32. package/dist/components/Layout/PageContainer.d.ts +1 -0
  33. package/dist/components/Layout/PageContainer.d.ts.map +1 -1
  34. package/dist/components/Layout/ScrollableArea.d.ts +1 -0
  35. package/dist/components/Layout/ScrollableArea.d.ts.map +1 -1
  36. package/dist/components/LoadingLiquiditySourceList/LoadingLiquiditySourceList.styles.d.ts +1 -0
  37. package/dist/components/LoadingLiquiditySourceList/LoadingLiquiditySourceList.styles.d.ts.map +1 -1
  38. package/dist/components/LoadingSwapDetails/LoadingSwapDetails.styles.d.ts +14 -0
  39. package/dist/components/LoadingSwapDetails/LoadingSwapDetails.styles.d.ts.map +1 -1
  40. package/dist/components/NoResult/NoResult.styles.d.ts +3 -0
  41. package/dist/components/NoResult/NoResult.styles.d.ts.map +1 -1
  42. package/dist/components/NotificationContent/NotificationContent.styles.d.ts +4 -0
  43. package/dist/components/NotificationContent/NotificationContent.styles.d.ts.map +1 -1
  44. package/dist/components/Quote/Quote.d.ts.map +1 -1
  45. package/dist/components/Quote/Quote.styles.d.ts +1136 -25
  46. package/dist/components/Quote/Quote.styles.d.ts.map +1 -1
  47. package/dist/components/Quote/Quote.types.d.ts +12 -7
  48. package/dist/components/Quote/Quote.types.d.ts.map +1 -1
  49. package/dist/components/Quote/QuoteCostDetails.d.ts.map +1 -1
  50. package/dist/components/Quote/QuoteCostDetails.styles.d.ts +5 -0
  51. package/dist/components/Quote/QuoteCostDetails.styles.d.ts.map +1 -1
  52. package/dist/components/Quote/QuoteSummary.styles.d.ts +2 -0
  53. package/dist/components/Quote/QuoteSummary.styles.d.ts.map +1 -1
  54. package/dist/components/Quote/QuoteTrigger .d.ts.map +1 -1
  55. package/dist/components/QuoteSkeleton/QuoteSkeleton.styles.d.ts +4 -0
  56. package/dist/components/QuoteSkeleton/QuoteSkeleton.styles.d.ts.map +1 -1
  57. package/dist/components/QuoteSkeleton/QuoteSkeleton.types.d.ts +1 -1
  58. package/dist/components/QuoteSkeleton/QuoteSkeleton.types.d.ts.map +1 -1
  59. package/dist/components/QuoteSkeleton/QuoteSummarySkeleton.d.ts.map +1 -1
  60. package/dist/components/QuoteSkeleton/QuoteSummarySkeleton.styles.d.ts +20 -10
  61. package/dist/components/QuoteSkeleton/QuoteSummarySkeleton.styles.d.ts.map +1 -1
  62. package/dist/components/QuoteSkeleton/QuoteSummarySkeleton.types.d.ts +1 -1
  63. package/dist/components/QuoteSkeleton/QuoteSummarySkeleton.types.d.ts.map +1 -1
  64. package/dist/components/QuoteSkeleton/StepSkeleton.styles.d.ts +5 -0
  65. package/dist/components/QuoteSkeleton/StepSkeleton.styles.d.ts.map +1 -1
  66. package/dist/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.styles.d.ts +4 -0
  67. package/dist/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.styles.d.ts.map +1 -1
  68. package/dist/components/SearchInput/SearchInput.styles.d.ts +1 -0
  69. package/dist/components/SearchInput/SearchInput.styles.d.ts.map +1 -1
  70. package/dist/components/SettingsContainer/SettingsContainer.style.d.ts +3 -0
  71. package/dist/components/SettingsContainer/SettingsContainer.style.d.ts.map +1 -1
  72. package/dist/components/Slippage/Slippage.styles.d.ts +5 -0
  73. package/dist/components/Slippage/Slippage.styles.d.ts.map +1 -1
  74. package/dist/components/SwapDetails/SwapDetails.styles.d.ts +13 -0
  75. package/dist/components/SwapDetails/SwapDetails.styles.d.ts.map +1 -1
  76. package/dist/components/SwapDetailsAlerts/SwapDetailsAlerts.styles.d.ts +1 -0
  77. package/dist/components/SwapDetailsAlerts/SwapDetailsAlerts.styles.d.ts.map +1 -1
  78. package/dist/components/SwapDetailsAlerts/SwapDetailsAlerts.types.d.ts +2 -1
  79. package/dist/components/SwapDetailsAlerts/SwapDetailsAlerts.types.d.ts.map +1 -1
  80. package/dist/components/SwapDetailsModal/SwapDetailsCompleteModal.d.ts.map +1 -1
  81. package/dist/components/SwapDetailsModal/SwapDetailsModal.Cancel.d.ts.map +1 -1
  82. package/dist/components/SwapDetailsModal/SwapDetailsModal.Delete.d.ts.map +1 -1
  83. package/dist/components/SwapDetailsModal/SwapDetailsModal.styles.d.ts +1 -0
  84. package/dist/components/SwapDetailsModal/SwapDetailsModal.styles.d.ts.map +1 -1
  85. package/dist/components/SwapDetailsModal/index.d.ts +1 -1
  86. package/dist/components/SwapDetailsModal/index.d.ts.map +1 -1
  87. package/dist/components/TokenList/TokenList.styles.d.ts +17 -0
  88. package/dist/components/TokenList/TokenList.styles.d.ts.map +1 -1
  89. package/dist/components/WalletModal/WalletModalContent.styles.d.ts +4 -0
  90. package/dist/components/WalletModal/WalletModalContent.styles.d.ts.map +1 -1
  91. package/dist/constants/languages.d.ts.map +1 -1
  92. package/dist/constants/navigationRoutes.d.ts +1 -0
  93. package/dist/constants/navigationRoutes.d.ts.map +1 -1
  94. package/dist/constants/quote.d.ts +5 -1
  95. package/dist/constants/quote.d.ts.map +1 -1
  96. package/dist/containers/App/App.styles.d.ts +1 -0
  97. package/dist/containers/App/App.styles.d.ts.map +1 -1
  98. package/dist/containers/QuoteInfo/QuoteInfo.d.ts.map +1 -1
  99. package/dist/containers/QuoteInfo/QuoteInfo.styles.d.ts +3 -0
  100. package/dist/containers/QuoteInfo/QuoteInfo.styles.d.ts.map +1 -1
  101. package/dist/containers/QuoteInfo/QuoteInfo.types.d.ts +6 -3
  102. package/dist/containers/QuoteInfo/QuoteInfo.types.d.ts.map +1 -1
  103. package/dist/containers/Wallets/Wallets.d.ts.map +1 -1
  104. package/dist/hooks/useConfirmSwap/useConfirmSwap.d.ts.map +1 -1
  105. package/dist/hooks/useConfirmSwap/useConfirmSwap.helpers.d.ts +5 -4
  106. package/dist/hooks/useConfirmSwap/useConfirmSwap.helpers.d.ts.map +1 -1
  107. package/dist/hooks/useConfirmSwap/useConfirmSwap.types.d.ts +1 -1
  108. package/dist/hooks/useConfirmSwap/useConfirmSwap.types.d.ts.map +1 -1
  109. package/dist/hooks/useFetch.d.ts +12 -0
  110. package/dist/hooks/useFetch.d.ts.map +1 -0
  111. package/dist/hooks/useFetchAllQuotes.d.ts +4 -0
  112. package/dist/hooks/useFetchAllQuotes.d.ts.map +1 -0
  113. package/dist/hooks/useFetchConfirmQuote.d.ts +4 -0
  114. package/dist/hooks/useFetchConfirmQuote.d.ts.map +1 -0
  115. package/dist/hooks/useSwapInput.d.ts +5 -5
  116. package/dist/hooks/useSwapInput.d.ts.map +1 -1
  117. package/dist/hooks/useWalletList.d.ts +1 -0
  118. package/dist/hooks/useWalletList.d.ts.map +1 -1
  119. package/dist/index.js +2 -2
  120. package/dist/index.js.map +4 -4
  121. package/dist/pages/ConfirmSwapPage.d.ts.map +1 -1
  122. package/dist/pages/Home.d.ts.map +1 -1
  123. package/dist/pages/LiquiditySourcePage.d.ts.map +1 -1
  124. package/dist/pages/Routes.d.ts +3 -0
  125. package/dist/pages/Routes.d.ts.map +1 -0
  126. package/dist/pages/SettingsPage.d.ts.map +1 -1
  127. package/dist/pages/WalletsPage.d.ts.map +1 -1
  128. package/dist/store/AppStore.d.ts +2 -2
  129. package/dist/store/app.d.ts +2 -2
  130. package/dist/store/quote.d.ts +32 -10
  131. package/dist/store/quote.d.ts.map +1 -1
  132. package/dist/store/wallets.d.ts +0 -2
  133. package/dist/store/wallets.d.ts.map +1 -1
  134. package/dist/types/quote.d.ts +19 -1
  135. package/dist/types/quote.d.ts.map +1 -1
  136. package/dist/utils/quote.d.ts +12 -9
  137. package/dist/utils/quote.d.ts.map +1 -1
  138. package/dist/utils/settings.d.ts +2 -3
  139. package/dist/utils/settings.d.ts.map +1 -1
  140. package/dist/utils/swap.d.ts +18 -18
  141. package/dist/utils/swap.d.ts.map +1 -1
  142. package/dist/utils/wallets.d.ts +4 -3
  143. package/dist/utils/wallets.d.ts.map +1 -1
  144. package/package.json +10 -10
  145. package/src/QueueManager.tsx +0 -3
  146. package/src/components/AppRoutes.tsx +5 -0
  147. package/src/components/ConfirmWalletsModal/ConfirmWallets.helpers.ts +3 -3
  148. package/src/components/ConfirmWalletsModal/ConfirmWalletsModal.tsx +29 -21
  149. package/src/components/ConfirmWalletsModal/WalletList.tsx +24 -18
  150. package/src/components/HeaderButtons/{HomeButtons.tsx → HeaderButtons.tsx} +39 -26
  151. package/src/components/HeaderButtons/HeaderButtons.types.ts +3 -2
  152. package/src/components/HeaderButtons/index.ts +1 -1
  153. package/src/components/Layout/Layout.tsx +13 -1
  154. package/src/components/Layout/Layout.types.ts +1 -0
  155. package/src/components/Quote/Quote.styles.ts +133 -38
  156. package/src/components/Quote/Quote.tsx +77 -18
  157. package/src/components/Quote/Quote.types.ts +13 -7
  158. package/src/components/Quote/QuoteCostDetails.tsx +9 -4
  159. package/src/components/Quote/QuoteTrigger .tsx +7 -3
  160. package/src/components/QuoteSkeleton/QuoteSkeleton.tsx +2 -2
  161. package/src/components/QuoteSkeleton/QuoteSkeleton.types.ts +1 -1
  162. package/src/components/QuoteSkeleton/QuoteSummarySkeleton.styles.ts +10 -12
  163. package/src/components/QuoteSkeleton/QuoteSummarySkeleton.tsx +28 -9
  164. package/src/components/QuoteSkeleton/QuoteSummarySkeleton.types.ts +1 -1
  165. package/src/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.helpers.ts +19 -19
  166. package/src/components/SwapDetails/SwapDetails.tsx +1 -1
  167. package/src/components/SwapDetailsAlerts/SwapDetailsAlerts.types.ts +1 -1
  168. package/src/components/SwapDetailsModal/SwapDetailsCompleteModal.tsx +1 -3
  169. package/src/components/SwapDetailsModal/SwapDetailsModal.Cancel.tsx +3 -7
  170. package/src/components/SwapDetailsModal/SwapDetailsModal.Delete.tsx +1 -3
  171. package/src/components/SwapDetailsModal/index.ts +1 -1
  172. package/src/constants/languages.ts +2 -1
  173. package/src/constants/navigationRoutes.ts +1 -0
  174. package/src/constants/quote.ts +17 -1
  175. package/src/containers/QuoteInfo/QuoteInfo.tsx +32 -7
  176. package/src/containers/QuoteInfo/QuoteInfo.types.ts +6 -3
  177. package/src/containers/Wallets/Wallets.tsx +5 -11
  178. package/src/hooks/useConfirmSwap/useConfirmSwap.helpers.ts +36 -20
  179. package/src/hooks/useConfirmSwap/useConfirmSwap.ts +76 -78
  180. package/src/hooks/useConfirmSwap/useConfirmSwap.types.ts +1 -1
  181. package/src/hooks/useFetch.ts +48 -0
  182. package/src/hooks/useFetchAllQuotes.ts +21 -0
  183. package/src/hooks/useFetchConfirmQuote.ts +21 -0
  184. package/src/hooks/useSwapInput.ts +59 -31
  185. package/src/hooks/useWalletList.ts +5 -3
  186. package/src/pages/ConfirmSwapPage.tsx +12 -9
  187. package/src/pages/Home.tsx +27 -19
  188. package/src/pages/LiquiditySourcePage.tsx +3 -5
  189. package/src/pages/Routes.tsx +131 -0
  190. package/src/pages/SettingsPage.tsx +11 -13
  191. package/src/pages/WalletsPage.tsx +24 -18
  192. package/src/store/quote.ts +60 -22
  193. package/src/store/wallets.ts +0 -2
  194. package/src/types/quote.ts +27 -1
  195. package/src/utils/quote.ts +129 -67
  196. package/src/utils/settings.ts +2 -4
  197. package/src/utils/swap.ts +37 -77
  198. package/src/utils/wallets.ts +14 -9
  199. package/dist/components/HeaderButtons/HomeButtons.d.ts +0 -4
  200. package/dist/components/HeaderButtons/HomeButtons.d.ts.map +0 -1
  201. package/dist/hooks/useFetchQuote.d.ts +0 -9
  202. package/dist/hooks/useFetchQuote.d.ts.map +0 -1
  203. package/src/hooks/useFetchQuote.ts +0 -38
@@ -1,43 +1,68 @@
1
1
  import type { PropTypes } from './QuoteInfo.types';
2
2
 
3
+ import BigNumber from 'bignumber.js';
3
4
  import React from 'react';
4
5
 
5
6
  import { Quote } from '../../components/Quote';
6
7
  import { QuoteSkeleton } from '../../components/QuoteSkeleton';
7
8
  import { useQuoteStore } from '../../store/quote';
8
9
  import { QuoteErrorType } from '../../types';
10
+ import { getQuoteToTokenUsdPrice } from '../../utils/quote';
11
+ import { calcOutputUsdValue } from '../../utils/swap';
9
12
 
10
13
  import { QuoteContainer } from './QuoteInfo.styles';
11
14
 
12
15
  export function QuoteInfo(props: PropTypes) {
13
- const { quote, type, loading, error, warning, expanded = false } = props;
14
- const { inputAmount, inputUsdValue, outputUsdValue, outputAmount } =
15
- useQuoteStore();
16
+ const {
17
+ quote,
18
+ type,
19
+ loading,
20
+ error,
21
+ warning,
22
+ expanded = false,
23
+ tagHidden,
24
+ onClick,
25
+ selected,
26
+ onClickAllRoutes,
27
+ } = props;
28
+ const { inputAmount, inputUsdValue, toToken } = useQuoteStore();
29
+
30
+ const outputAmount = !!quote?.outputAmount
31
+ ? new BigNumber(quote?.outputAmount)
32
+ : null;
33
+ const outputUsdValue = !!quote?.outputAmount
34
+ ? calcOutputUsdValue(
35
+ quote?.outputAmount,
36
+ getQuoteToTokenUsdPrice(quote) || toToken?.usdPrice
37
+ )
38
+ : null;
16
39
 
17
40
  const noResult =
18
41
  error &&
19
42
  (error.type === QuoteErrorType.NO_RESULT ||
20
43
  error.type === QuoteErrorType.REQUEST_FAILED);
21
44
 
22
- const showQuote = !noResult && quote && quote.result && !loading;
45
+ const showQuote = !noResult && quote && !loading;
23
46
 
24
47
  if (loading) {
25
48
  return (
26
49
  <QuoteContainer>
27
- <QuoteSkeleton type={type} expanded={expanded} />
50
+ <QuoteSkeleton tagHidden={tagHidden} type={type} expanded={expanded} />
28
51
  </QuoteContainer>
29
52
  );
30
53
  }
31
54
 
32
55
  return showQuote ? (
33
- <QuoteContainer>
56
+ <QuoteContainer onClick={() => onClick?.(quote)}>
34
57
  <Quote
35
58
  quote={quote}
36
59
  error={error}
37
60
  warning={warning}
61
+ tagHidden={tagHidden}
62
+ selected={selected}
38
63
  type={type}
39
64
  expanded={expanded}
40
- recommended={true}
65
+ onClickAllRoutes={onClickAllRoutes}
41
66
  input={{
42
67
  value: inputAmount,
43
68
  usdValue: inputUsdValue?.toString() ?? '',
@@ -1,13 +1,16 @@
1
1
  import type { QuoteProps } from '../../components/Quote/Quote.types';
2
- import type { QuoteError, QuoteWarning } from '../../types';
3
- import type { BestRouteResponse } from 'rango-sdk';
2
+ import type { QuoteError, QuoteWarning, SelectedQuote } from '../../types';
4
3
 
5
4
  export type PropTypes = {
6
- quote: BestRouteResponse | null;
5
+ quote: SelectedQuote | null;
7
6
  type: QuoteProps['type'];
7
+ onClick?: (quote: SelectedQuote) => void;
8
8
  loading: boolean;
9
+ tagHidden?: boolean;
9
10
  error: QuoteError | null;
10
11
  warning: QuoteWarning | null;
11
12
  autoScrollOnExpanding?: boolean;
12
13
  expanded?: boolean;
14
+ onClickAllRoutes?: () => void;
15
+ selected?: boolean;
13
16
  };
@@ -3,15 +3,14 @@ import type {
3
3
  PropTypes,
4
4
  WidgetContextInterface,
5
5
  } from './Wallets.types';
6
- import type { Wallet } from '../../types/wallets';
7
6
  import type { ProvidersOptions } from '../../utils/providers';
8
7
  import type { EventHandler } from '@rango-dev/wallets-react';
9
- import type { Network } from '@rango-dev/wallets-shared';
10
8
  import type { PropsWithChildren } from 'react';
11
9
 
12
10
  import { Events, Provider } from '@rango-dev/wallets-react';
11
+ import { type Network } from '@rango-dev/wallets-shared';
13
12
  import { isEvmBlockchain } from 'rango-sdk';
14
- import React, { createContext, useEffect, useRef, useState } from 'react';
13
+ import React, { createContext, useEffect, useRef } from 'react';
15
14
 
16
15
  import { useWalletProviders } from '../../hooks/useWalletProviders';
17
16
  import { AppStoreProvider, useAppStore } from '../../store/AppStore';
@@ -35,7 +34,6 @@ function Main(props: PropsWithChildren<PropTypes>) {
35
34
  const walletOptions: ProvidersOptions = {
36
35
  walletConnectProjectId: props.config?.walletConnectProjectId,
37
36
  };
38
- const [accounts, setAccounts] = useState<Wallet[]>([]);
39
37
  const { providers } = useWalletProviders(props.config.wallets, walletOptions);
40
38
  const { connectWallet, disconnectWallet } = useWalletsStore();
41
39
  const onConnectWalletHandler = useRef<OnConnectHandler>();
@@ -67,7 +65,9 @@ function Main(props: PropsWithChildren<PropTypes>) {
67
65
  supportedChainNames,
68
66
  meta.isContractWallet
69
67
  );
70
- setAccounts(data);
68
+ if (data.length) {
69
+ connectWallet(data, tokens);
70
+ }
71
71
  } else {
72
72
  disconnectWallet(type);
73
73
  }
@@ -102,12 +102,6 @@ function Main(props: PropsWithChildren<PropTypes>) {
102
102
  };
103
103
  const isActiveTab = useUiStore.use.isActiveTab();
104
104
 
105
- useEffect(() => {
106
- if (accounts.length) {
107
- connectWallet(accounts, tokens);
108
- }
109
- }, [accounts, tokens]);
110
-
111
105
  return (
112
106
  <WidgetContext.Provider
113
107
  // eslint-disable-next-line react/jsx-no-constructed-context-values
@@ -1,6 +1,12 @@
1
1
  import type { ConfirmSwapFetchResult } from './useConfirmSwap.types';
2
- import type { ConfirmSwapWarnings, Wallet } from '../../types';
3
- import type { BestRouteResponse, BlockchainMeta, Token } from 'rango-sdk';
2
+ import type {
3
+ ConfirmSwapWarnings,
4
+ QuoteErrorResponse,
5
+ QuoteResponse,
6
+ SelectedQuote,
7
+ Wallet,
8
+ } from '../../types';
9
+ import type { BlockchainMeta, SwapResult, Token } from 'rango-sdk';
4
10
 
5
11
  import { errorMessages } from '../../constants/errors';
6
12
  import { QuoteErrorType, QuoteUpdateType } from '../../types';
@@ -26,8 +32,8 @@ import {
26
32
  /**
27
33
  * A request can be successful but in body of the response, it can be some case which is considered as failed.
28
34
  */
29
- export function throwErrorIfResponseIsNotValid(response: BestRouteResponse) {
30
- if (!response.result) {
35
+ export function throwErrorIfResponseIsNotValid(response: QuoteResponse) {
36
+ if (!response.swaps) {
31
37
  throw new Error(errorMessages().noResultError.title, {
32
38
  cause: {
33
39
  type: QuoteErrorType.NO_RESULT,
@@ -35,40 +41,49 @@ export function throwErrorIfResponseIsNotValid(response: BestRouteResponse) {
35
41
  },
36
42
  });
37
43
  }
38
- const limitError = hasLimitError(response);
44
+ const quoteError = getQuoteError(response.swaps);
45
+ if (quoteError) {
46
+ throw new Error(quoteError.message, {
47
+ cause: quoteError.options,
48
+ });
49
+ }
50
+ }
51
+
52
+ export function getQuoteError(swaps: SwapResult[]): QuoteErrorResponse | null {
53
+ const limitError = hasLimitError(swaps);
39
54
  if (limitError) {
40
55
  const { swap, recommendation, fromAmountRangeError } =
41
- getLimitErrorMessage(response);
42
-
43
- throw new Error('bridge limit error', {
44
- cause: {
56
+ getLimitErrorMessage(swaps);
57
+ return {
58
+ message: 'bridge limit error',
59
+ options: {
45
60
  type: QuoteErrorType.BRIDGE_LIMIT,
46
- swap: swap,
61
+ swap,
47
62
  recommendation,
48
63
  fromAmountRangeError,
49
64
  },
50
- });
65
+ };
51
66
  }
52
67
 
53
- const recommendedSlippages = checkSlippageErrors(response);
68
+ const recommendedSlippages = checkSlippageErrors(swaps);
54
69
 
55
70
  if (recommendedSlippages) {
56
- const minRequiredSlippage = getMinRequiredSlippage(response);
57
- throw new Error('', {
58
- cause: {
71
+ const minRequiredSlippage = getMinRequiredSlippage(swaps);
72
+ return {
73
+ message: '',
74
+ options: {
59
75
  type: QuoteErrorType.INSUFFICIENT_SLIPPAGE,
60
76
  recommendedSlippages,
61
77
  minRequiredSlippage,
62
78
  },
63
- });
79
+ };
64
80
  }
65
-
66
- return response;
81
+ return null;
67
82
  }
68
83
 
69
84
  export function generateWarnings(
70
- previousQuote: BestRouteResponse | undefined,
71
- currentQuote: BestRouteResponse,
85
+ previousQuote: SelectedQuote | undefined,
86
+ currentQuote: SelectedQuote,
72
87
  params: {
73
88
  fromToken: Token;
74
89
  toToken: Token;
@@ -132,6 +147,7 @@ export function generateWarnings(
132
147
  params.selectedWallets,
133
148
  params.meta.blockchains
134
149
  );
150
+
135
151
  const enoughBalance = balanceWarnings.length === 0;
136
152
 
137
153
  if (!enoughBalance) {
@@ -1,16 +1,14 @@
1
1
  import type { ConfirmSwap, Params } from './useConfirmSwap.types';
2
- import type { PendingSwapSettings } from '../../types';
3
- import type { BestRouteResponse } from 'rango-sdk';
2
+ import type { PendingSwapSettings, SelectedQuote } from '../../types';
3
+ import type { ConfirmRouteRequest } from 'rango-sdk';
4
4
 
5
5
  import { calculatePendingSwap } from '@rango-dev/queue-manager-rango-preset';
6
6
  import { useEffect } from 'react';
7
7
 
8
8
  import { useAppStore } from '../../store/AppStore';
9
9
  import { useQuoteStore } from '../../store/quote';
10
- import { useWalletsStore } from '../../store/wallets';
11
- import { isFeatureEnabled } from '../../utils/settings';
12
- import { createQuoteRequestBody, getWalletsForNewSwap } from '../../utils/swap';
13
- import { useFetchQuote } from '../useFetchQuote';
10
+ import { getWalletsForNewSwap } from '../../utils/swap';
11
+ import { useFetchConfirmQuote } from '../useFetchConfirmQuote';
14
12
 
15
13
  import {
16
14
  generateWarnings,
@@ -23,29 +21,21 @@ export function useConfirmSwap(): ConfirmSwap {
23
21
  fromToken,
24
22
  toToken,
25
23
  inputAmount,
26
- setQuote,
27
- quote: initialQuote,
24
+ setSelectedQuote,
25
+ selectedQuote: initialQuote,
28
26
  customDestination: customDestinationFromStore,
29
27
  setQuoteWarningsConfirmed,
28
+ resetAlerts,
30
29
  } = useQuoteStore();
31
30
 
32
- const {
33
- slippage,
34
- customSlippage,
35
- affiliatePercent,
36
- affiliateRef,
37
- affiliateWallets,
38
- } = useAppStore();
31
+ const { slippage, customSlippage } = useAppStore();
39
32
  const disabledLiquiditySources = useAppStore().getDisabledLiquiditySources();
40
- const { connectedWallets } = useWalletsStore();
41
33
  const blockchains = useAppStore().blockchains();
42
34
  const tokens = useAppStore().tokens();
43
- const { features } = useAppStore().config;
44
- const { enableNewLiquiditySources } = useAppStore().config;
45
35
 
46
36
  const userSlippage = customSlippage || slippage;
47
37
 
48
- const { fetch: fetchQuote, cancelFetch, loading } = useFetchQuote();
38
+ const { fetch: fetchQuote, cancelFetch, loading } = useFetchConfirmQuote();
49
39
 
50
40
  useEffect(() => cancelFetch, []);
51
41
 
@@ -63,70 +53,78 @@ export function useConfirmSwap(): ConfirmSwap {
63
53
  };
64
54
  }
65
55
 
66
- const requestBody = createQuoteRequestBody({
67
- fromToken,
68
- toToken,
69
- inputAmount,
70
- wallets: connectedWallets,
71
- selectedWallets,
72
- excludeLiquiditySources: enableNewLiquiditySources,
73
- disabledLiquiditySources,
74
- slippage: userSlippage,
75
- affiliateRef,
76
- affiliatePercent,
77
- affiliateWallets,
78
- initialQuote: initialQuote ?? undefined,
79
- destination: customDestination,
80
- });
81
- if (isFeatureEnabled('experimentalRoute', features)) {
82
- requestBody.experimental = true;
83
- }
84
- let currentQuote: BestRouteResponse;
56
+ const wallets = selectedWallets.reduce(
57
+ (acc: Record<string, string>, item) => {
58
+ acc[item.chain] = item.address;
59
+ return acc;
60
+ },
61
+ {}
62
+ );
63
+ const requestBody: ConfirmRouteRequest = {
64
+ requestId: initialQuote?.requestId || '',
65
+ selectedWallets: wallets,
66
+ destination: customDestination || undefined,
67
+ };
68
+
85
69
  try {
86
- currentQuote = await fetchQuote(requestBody).then((response) =>
87
- throwErrorIfResponseIsNotValid(response)
88
- );
89
- setQuote(currentQuote);
70
+ return await fetchQuote(requestBody).then((response) => {
71
+ const { result } = response;
72
+
73
+ throwErrorIfResponseIsNotValid({
74
+ diagnosisMessages: result.diagnosisMessages,
75
+ requestId: result.requestId,
76
+ swaps: result.result?.swaps,
77
+ });
78
+
79
+ const currentQuote: SelectedQuote = {
80
+ outputAmount: result.result?.outputAmount,
81
+ requestId: result.requestId,
82
+ resultType: result.result?.resultType,
83
+ swaps: result.result?.swaps || [],
84
+ validationStatus: result.validationStatus,
85
+ requestAmount: result.requestAmount,
86
+ };
87
+ setSelectedQuote(currentQuote);
88
+ const swapSettings: PendingSwapSettings = {
89
+ slippage: userSlippage.toString(),
90
+ disabledSwappersGroups: disabledLiquiditySources,
91
+ };
92
+ const swap = calculatePendingSwap(
93
+ inputAmount.toString(),
94
+ result,
95
+ getWalletsForNewSwap(selectedWallets),
96
+ swapSettings,
97
+ false,
98
+ { blockchains, tokens }
99
+ );
100
+
101
+ const confirmSwapWarnings = generateWarnings(
102
+ initialQuote ?? undefined,
103
+ currentQuote,
104
+ {
105
+ fromToken,
106
+ toToken,
107
+ meta: { blockchains, tokens },
108
+ selectedWallets,
109
+ userSlippage,
110
+ }
111
+ );
112
+ if (confirmSwapWarnings.quoteUpdate) {
113
+ setQuoteWarningsConfirmed(false);
114
+ }
115
+ resetAlerts();
116
+
117
+ return {
118
+ quote: currentQuote,
119
+ swap,
120
+ error: null,
121
+ warnings: confirmSwapWarnings,
122
+ };
123
+ });
90
124
  } catch (error: any) {
91
125
  const confirmSwapResult = handleQuoteErrors(error);
92
126
  return confirmSwapResult;
93
127
  }
94
-
95
- const swapSettings: PendingSwapSettings = {
96
- slippage: userSlippage.toString(),
97
- disabledSwappersGroups: disabledLiquiditySources,
98
- };
99
- const swap = calculatePendingSwap(
100
- inputAmount.toString(),
101
- currentQuote,
102
- getWalletsForNewSwap(selectedWallets),
103
- swapSettings,
104
- false,
105
- { blockchains, tokens }
106
- );
107
-
108
- const confirmSwapWarnings = generateWarnings(
109
- initialQuote ?? undefined,
110
- currentQuote,
111
- {
112
- fromToken,
113
- toToken,
114
- meta: { blockchains, tokens },
115
- selectedWallets,
116
- userSlippage,
117
- }
118
- );
119
-
120
- if (confirmSwapWarnings.quoteUpdate) {
121
- setQuoteWarningsConfirmed(false);
122
- }
123
-
124
- return {
125
- quote: currentQuote,
126
- swap,
127
- error: null,
128
- warnings: confirmSwapWarnings,
129
- };
130
128
  };
131
129
 
132
130
  return {
@@ -15,5 +15,5 @@ export type ConfirmSwap = {
15
15
 
16
16
  export type Params = {
17
17
  selectedWallets: Wallet[];
18
- customDestination?: string;
18
+ customDestination: string | null;
19
19
  };
@@ -0,0 +1,48 @@
1
+ import type { RequestOptions } from 'rango-sdk';
2
+
3
+ import { useRef, useState } from 'react';
4
+
5
+ type UseFetchOptions<TRequest, TResponse> = {
6
+ request: (
7
+ requestBody: TRequest,
8
+ options?: RequestOptions
9
+ ) => Promise<TResponse>;
10
+ };
11
+ export type UseFetchResult<TRequest, TResponse> = {
12
+ fetch: (requestBody: TRequest) => Promise<TResponse>;
13
+ cancelFetch: () => void;
14
+ loading: boolean;
15
+ };
16
+
17
+ export function useFetch<TRequest, TResponse>({
18
+ request,
19
+ }: UseFetchOptions<TRequest, TResponse>): UseFetchResult<TRequest, TResponse> {
20
+ const [loading, setLoading] = useState(false);
21
+ const abortController = useRef<AbortController | null>(null);
22
+
23
+ const cancelFetch = () => abortController.current?.abort();
24
+
25
+ const fetch: UseFetchResult<TRequest, TResponse>['fetch'] = async (
26
+ requestBody
27
+ ) => {
28
+ cancelFetch();
29
+ abortController.current = new AbortController();
30
+
31
+ setLoading(true);
32
+ try {
33
+ const res = await request(requestBody, {
34
+ signal: abortController.current.signal,
35
+ });
36
+
37
+ abortController.current = null;
38
+ return res;
39
+ // eslint-disable-next-line no-useless-catch
40
+ } catch (error) {
41
+ throw error;
42
+ } finally {
43
+ setLoading(false);
44
+ }
45
+ };
46
+
47
+ return { fetch, loading, cancelFetch };
48
+ }
@@ -0,0 +1,21 @@
1
+ import type { UseFetchResult } from './useFetch';
2
+ import type { BestRouteRequest, MultiRouteResponse } from 'rango-sdk';
3
+
4
+ import { httpService } from '../services/httpService';
5
+
6
+ import { useFetch } from './useFetch';
7
+
8
+ export function useFetchAllQuotes(): UseFetchResult<
9
+ BestRouteRequest,
10
+ MultiRouteResponse
11
+ > {
12
+ const { fetch, loading, cancelFetch } = useFetch<
13
+ BestRouteRequest,
14
+ MultiRouteResponse
15
+ >({
16
+ request: async (requestBody, options) =>
17
+ await httpService().getAllRoutes(requestBody, options),
18
+ });
19
+
20
+ return { fetch, loading, cancelFetch };
21
+ }
@@ -0,0 +1,21 @@
1
+ import type { UseFetchResult } from './useFetch';
2
+ import type { ConfirmRouteRequest, ConfirmRouteResponse } from 'rango-sdk';
3
+
4
+ import { httpService } from '../services/httpService';
5
+
6
+ import { useFetch } from './useFetch';
7
+
8
+ export function useFetchConfirmQuote(): UseFetchResult<
9
+ ConfirmRouteRequest,
10
+ ConfirmRouteResponse
11
+ > {
12
+ const { fetch, loading, cancelFetch } = useFetch<
13
+ ConfirmRouteRequest,
14
+ ConfirmRouteResponse
15
+ >({
16
+ request: async (requestBody, options) =>
17
+ await httpService().confirmRouteRequest(requestBody, options),
18
+ });
19
+
20
+ return { fetch, loading, cancelFetch };
21
+ }