@rango-dev/widget-embedded 0.17.1-next.9 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (271) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/dist/QueueManager.d.ts +1 -2
  3. package/dist/QueueManager.d.ts.map +1 -1
  4. package/dist/Wallets.d.ts +0 -1
  5. package/dist/Wallets.d.ts.map +1 -1
  6. package/dist/Widget.d.ts +1 -1
  7. package/dist/Widget.d.ts.map +1 -1
  8. package/dist/components/AppRouter.d.ts +2 -0
  9. package/dist/components/AppRouter.d.ts.map +1 -1
  10. package/dist/components/AppRoutes.d.ts +6 -1
  11. package/dist/components/AppRoutes.d.ts.map +1 -1
  12. package/dist/components/ConfirmWalletsModal/ConfirmWallets.helpers.d.ts +3 -1
  13. package/dist/components/ConfirmWalletsModal/ConfirmWallets.helpers.d.ts.map +1 -1
  14. package/dist/components/ConfirmWalletsModal/ConfirmWallets.styles.d.ts +1 -1
  15. package/dist/components/ConfirmWalletsModal/ConfirmWalletsModal.d.ts.map +1 -1
  16. package/dist/components/ConfirmWalletsModal/ConfirmWalletsModal.types.d.ts +3 -1
  17. package/dist/components/ConfirmWalletsModal/ConfirmWalletsModal.types.d.ts.map +1 -1
  18. package/dist/components/ConfirmWalletsModal/WalletList.d.ts.map +1 -1
  19. package/dist/components/ConfirmWalletsModal/WalletList.type.d.ts +4 -1
  20. package/dist/components/ConfirmWalletsModal/WalletList.type.d.ts.map +1 -1
  21. package/dist/components/NoRoutes/NoRoutes.d.ts +4 -0
  22. package/dist/components/NoRoutes/NoRoutes.d.ts.map +1 -0
  23. package/dist/components/NoRoutes/NoRoutes.helper.d.ts +3 -0
  24. package/dist/components/NoRoutes/NoRoutes.helper.d.ts.map +1 -0
  25. package/dist/components/{NoResult/NoResult.styles.d.ts → NoRoutes/NoRoutes.styles.d.ts} +1 -1
  26. package/dist/components/{NoResult/NoResult.styles.d.ts.map → NoRoutes/NoRoutes.styles.d.ts.map} +1 -1
  27. package/dist/components/{NoResult/NoResult.types.d.ts → NoRoutes/NoRoutes.types.d.ts} +3 -3
  28. package/dist/components/NoRoutes/NoRoutes.types.d.ts.map +1 -0
  29. package/dist/components/NoRoutes/index.d.ts +2 -0
  30. package/dist/components/NoRoutes/index.d.ts.map +1 -0
  31. package/dist/components/NotificationContent/NotificationContent.d.ts.map +1 -1
  32. package/dist/components/RouteErrors/RouteErrors.d.ts +4 -0
  33. package/dist/components/RouteErrors/RouteErrors.d.ts.map +1 -0
  34. package/dist/components/{QuoteWarningsAndErrors/QuoteWarningsAndErrors.styles.d.ts → RouteErrors/RouteErrors.styles.d.ts} +1 -1
  35. package/dist/components/RouteErrors/RouteErrors.styles.d.ts.map +1 -0
  36. package/dist/components/RouteErrors/RouteErrors.types.d.ts +24 -0
  37. package/dist/components/RouteErrors/RouteErrors.types.d.ts.map +1 -0
  38. package/dist/components/RouteErrors/RouteErrorsModal.d.ts +4 -0
  39. package/dist/components/RouteErrors/RouteErrorsModal.d.ts.map +1 -0
  40. package/dist/components/RouteErrors/RouteErrorsModalItem.d.ts +4 -0
  41. package/dist/components/RouteErrors/RouteErrorsModalItem.d.ts.map +1 -0
  42. package/dist/components/RouteErrors/index.d.ts +2 -0
  43. package/dist/components/RouteErrors/index.d.ts.map +1 -0
  44. package/dist/components/SwapDetails/SwapDetails.d.ts.map +1 -1
  45. package/dist/components/TokenList/TokenList.d.ts.map +1 -1
  46. package/dist/components/UpdateUrl.d.ts +6 -1
  47. package/dist/components/UpdateUrl.d.ts.map +1 -1
  48. package/dist/components/WidgetEvents.d.ts.map +1 -1
  49. package/dist/constants/errors.d.ts +4 -4
  50. package/dist/constants/errors.d.ts.map +1 -1
  51. package/dist/constants/messages.d.ts +1 -1
  52. package/dist/constants/searchParams.d.ts +1 -2
  53. package/dist/constants/searchParams.d.ts.map +1 -1
  54. package/dist/constants/warnings.d.ts +2 -2
  55. package/dist/constants/warnings.d.ts.map +1 -1
  56. package/dist/hooks/{useConfirmSwap/useConfirmSwap.types.d.ts → useConfirmSwap.d.ts} +7 -5
  57. package/dist/hooks/useConfirmSwap.d.ts.map +1 -0
  58. package/dist/hooks/{useFetchQuote.d.ts → useFetchBestRoute.d.ts} +3 -3
  59. package/dist/hooks/useFetchBestRoute.d.ts.map +1 -0
  60. package/dist/hooks/usePrepareBlockchainList/usePrepareBlockchainList.helpers.d.ts +2 -2
  61. package/dist/hooks/useSwapInput.d.ts +2 -4
  62. package/dist/hooks/useSwapInput.d.ts.map +1 -1
  63. package/dist/hooks/useSyncStoresWithConfig.d.ts +2 -1
  64. package/dist/hooks/useSyncStoresWithConfig.d.ts.map +1 -1
  65. package/dist/hooks/useWalletList.d.ts.map +1 -1
  66. package/dist/index.js +1 -1
  67. package/dist/index.js.map +4 -4
  68. package/dist/pages/ConfirmSwapPage.d.ts +6 -1
  69. package/dist/pages/ConfirmSwapPage.d.ts.map +1 -1
  70. package/dist/pages/Home.d.ts.map +1 -1
  71. package/dist/pages/LiquiditySourcePage.d.ts +2 -1
  72. package/dist/pages/LiquiditySourcePage.d.ts.map +1 -1
  73. package/dist/pages/SelectBlockchainPage.d.ts.map +1 -1
  74. package/dist/pages/SettingsPage.d.ts +6 -1
  75. package/dist/pages/SettingsPage.d.ts.map +1 -1
  76. package/dist/pages/WalletsPage.d.ts +8 -1
  77. package/dist/pages/WalletsPage.d.ts.map +1 -1
  78. package/dist/store/app.d.ts +23 -5
  79. package/dist/store/app.d.ts.map +1 -1
  80. package/dist/store/{quote.d.ts → bestRoute.d.ts} +27 -39
  81. package/dist/store/bestRoute.d.ts.map +1 -0
  82. package/dist/store/meta.d.ts +23 -0
  83. package/dist/store/meta.d.ts.map +1 -0
  84. package/dist/store/settings.d.ts +2 -3
  85. package/dist/store/settings.d.ts.map +1 -1
  86. package/dist/store/slices/config.d.ts +2 -3
  87. package/dist/store/slices/config.d.ts.map +1 -1
  88. package/dist/store/slices/data.d.ts +0 -3
  89. package/dist/store/slices/data.d.ts.map +1 -1
  90. package/dist/store/wallets.d.ts +4 -5
  91. package/dist/store/wallets.d.ts.map +1 -1
  92. package/dist/types/config.d.ts +5 -5
  93. package/dist/types/config.d.ts.map +1 -1
  94. package/dist/types/index.d.ts +1 -1
  95. package/dist/types/index.d.ts.map +1 -1
  96. package/dist/types/routing.d.ts +65 -0
  97. package/dist/types/routing.d.ts.map +1 -0
  98. package/dist/utils/numbers.d.ts +1 -1
  99. package/dist/utils/numbers.d.ts.map +1 -1
  100. package/dist/utils/{quote.d.ts → routing.d.ts} +12 -16
  101. package/dist/utils/routing.d.ts.map +1 -0
  102. package/dist/utils/settings.d.ts +2 -3
  103. package/dist/utils/settings.d.ts.map +1 -1
  104. package/dist/utils/swap.d.ts +22 -25
  105. package/dist/utils/swap.d.ts.map +1 -1
  106. package/dist/utils/wallets.d.ts +2 -2
  107. package/dist/utils/wallets.d.ts.map +1 -1
  108. package/package.json +8 -8
  109. package/src/QueueManager.tsx +14 -14
  110. package/src/Wallets.tsx +15 -35
  111. package/src/Widget.tsx +24 -11
  112. package/src/components/AppRouter.tsx +5 -6
  113. package/src/components/AppRoutes.tsx +39 -6
  114. package/src/components/BlockchainList/BlockchainList.tsx +4 -4
  115. package/src/components/BlockchainsSection/BlockchainsSection.tsx +7 -7
  116. package/src/components/ConfirmWalletsModal/ConfirmWallets.helpers.ts +64 -2
  117. package/src/components/ConfirmWalletsModal/ConfirmWalletsModal.tsx +40 -28
  118. package/src/components/ConfirmWalletsModal/ConfirmWalletsModal.types.ts +3 -1
  119. package/src/components/ConfirmWalletsModal/WalletList.tsx +5 -6
  120. package/src/components/ConfirmWalletsModal/WalletList.type.ts +4 -1
  121. package/src/components/LoadingSwapDetails/LoadingSwapDetails.styles.ts +3 -3
  122. package/src/components/LoadingSwapDetails/LoadingSwapDetails.tsx +6 -6
  123. package/src/components/{NoResult/NoResult.helpers.ts → NoRoutes/NoRoutes.helper.ts} +9 -16
  124. package/src/components/{NoResult/NoResult.styles.ts → NoRoutes/NoRoutes.styles.ts} +0 -2
  125. package/src/components/{NoResult/NoResult.tsx → NoRoutes/NoRoutes.tsx} +12 -10
  126. package/src/components/{NoResult/NoResult.types.ts → NoRoutes/NoRoutes.types.ts} +2 -3
  127. package/src/components/NoRoutes/index.ts +1 -0
  128. package/src/components/NotificationContent/NotificationContent.tsx +2 -3
  129. package/src/components/RouteErrors/RouteErrors.tsx +79 -0
  130. package/src/components/RouteErrors/RouteErrors.types.ts +29 -0
  131. package/src/components/RouteErrors/RouteErrorsModal.tsx +129 -0
  132. package/src/components/{QuoteWarningsAndErrors/QuoteErrorsModalItem.tsx → RouteErrors/RouteErrorsModalItem.tsx} +3 -3
  133. package/src/components/RouteErrors/index.ts +1 -0
  134. package/src/components/SwapDetails/SwapDetails.Placeholder.tsx +1 -1
  135. package/src/components/SwapDetails/SwapDetails.tsx +22 -18
  136. package/src/components/SwitchFromAndTo.tsx +2 -2
  137. package/src/components/TokenList/TokenList.tsx +6 -5
  138. package/src/components/UpdateUrl.tsx +26 -36
  139. package/src/components/WidgetEvents.tsx +2 -4
  140. package/src/constants/errors.ts +10 -8
  141. package/src/constants/messages.ts +1 -1
  142. package/src/constants/searchParams.ts +0 -1
  143. package/src/constants/warnings.ts +7 -7
  144. package/src/hooks/useConfirmSwap.ts +303 -0
  145. package/src/hooks/{useFetchQuote.ts → useFetchBestRoute.ts} +3 -3
  146. package/src/hooks/useNavigateBack.ts +3 -3
  147. package/src/hooks/useSwapInput.ts +23 -58
  148. package/src/hooks/useSyncStoresWithConfig.ts +19 -22
  149. package/src/hooks/useWalletList.ts +5 -3
  150. package/src/pages/ConfirmSwapPage.tsx +235 -58
  151. package/src/pages/HistoryPage.tsx +1 -1
  152. package/src/pages/Home.tsx +247 -58
  153. package/src/pages/LanguagePage.tsx +1 -1
  154. package/src/pages/LiquiditySourcePage.tsx +32 -23
  155. package/src/pages/SelectBlockchainPage.tsx +7 -6
  156. package/src/pages/SelectSwapItemsPage.tsx +5 -5
  157. package/src/pages/SettingsPage.tsx +13 -9
  158. package/src/pages/SwapDetailsPage.tsx +3 -3
  159. package/src/pages/ThemePage.tsx +1 -1
  160. package/src/pages/WalletsPage.tsx +12 -5
  161. package/src/store/app.ts +6 -18
  162. package/src/store/{quote.ts → bestRoute.ts} +60 -68
  163. package/src/store/meta.ts +39 -0
  164. package/src/store/settings.ts +4 -7
  165. package/src/store/slices/config.ts +15 -25
  166. package/src/store/slices/data.ts +29 -64
  167. package/src/store/wallets.ts +8 -11
  168. package/src/types/config.ts +5 -5
  169. package/src/types/index.ts +1 -1
  170. package/src/types/routing.ts +77 -0
  171. package/src/utils/numbers.ts +1 -1
  172. package/src/utils/routing.ts +321 -0
  173. package/src/utils/settings.ts +7 -8
  174. package/src/utils/swap.ts +85 -113
  175. package/src/utils/wallets.ts +17 -29
  176. package/dist/components/NoResult/NoResult.d.ts +0 -4
  177. package/dist/components/NoResult/NoResult.d.ts.map +0 -1
  178. package/dist/components/NoResult/NoResult.helpers.d.ts +0 -4
  179. package/dist/components/NoResult/NoResult.helpers.d.ts.map +0 -1
  180. package/dist/components/NoResult/NoResult.types.d.ts.map +0 -1
  181. package/dist/components/NoResult/index.d.ts +0 -2
  182. package/dist/components/NoResult/index.d.ts.map +0 -1
  183. package/dist/components/Quote/Quote.d.ts +0 -4
  184. package/dist/components/Quote/Quote.d.ts.map +0 -1
  185. package/dist/components/Quote/Quote.styles.d.ts +0 -1429
  186. package/dist/components/Quote/Quote.styles.d.ts.map +0 -1
  187. package/dist/components/Quote/Quote.types.d.ts +0 -22
  188. package/dist/components/Quote/Quote.types.d.ts.map +0 -1
  189. package/dist/components/Quote/QuoteSkeleton.d.ts +0 -4
  190. package/dist/components/Quote/QuoteSkeleton.d.ts.map +0 -1
  191. package/dist/components/Quote/QuoteSkeleton.styles.d.ts +0 -319
  192. package/dist/components/Quote/QuoteSkeleton.styles.d.ts.map +0 -1
  193. package/dist/components/Quote/QuoteSkeleton.types.d.ts +0 -6
  194. package/dist/components/Quote/QuoteSkeleton.types.d.ts.map +0 -1
  195. package/dist/components/Quote/QuoteSummary.d.ts +0 -11
  196. package/dist/components/Quote/QuoteSummary.d.ts.map +0 -1
  197. package/dist/components/Quote/QuoteSummary.styles.d.ts +0 -158
  198. package/dist/components/Quote/QuoteSummary.styles.d.ts.map +0 -1
  199. package/dist/components/Quote/index.d.ts +0 -4
  200. package/dist/components/Quote/index.d.ts.map +0 -1
  201. package/dist/components/QuoteWarningsAndErrors/HighValueLossWarningModal.d.ts +0 -11
  202. package/dist/components/QuoteWarningsAndErrors/HighValueLossWarningModal.d.ts.map +0 -1
  203. package/dist/components/QuoteWarningsAndErrors/QuoteErrorsModalItem.d.ts +0 -4
  204. package/dist/components/QuoteWarningsAndErrors/QuoteErrorsModalItem.d.ts.map +0 -1
  205. package/dist/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.d.ts +0 -4
  206. package/dist/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.d.ts.map +0 -1
  207. package/dist/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.helpers.d.ts +0 -9
  208. package/dist/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.helpers.d.ts.map +0 -1
  209. package/dist/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.styles.d.ts.map +0 -1
  210. package/dist/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.types.d.ts +0 -22
  211. package/dist/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.types.d.ts.map +0 -1
  212. package/dist/components/QuoteWarningsAndErrors/SlippageWariningModal.d.ts +0 -11
  213. package/dist/components/QuoteWarningsAndErrors/SlippageWariningModal.d.ts.map +0 -1
  214. package/dist/components/QuoteWarningsAndErrors/UnknownPriceWarningModal.d.ts +0 -11
  215. package/dist/components/QuoteWarningsAndErrors/UnknownPriceWarningModal.d.ts.map +0 -1
  216. package/dist/components/QuoteWarningsAndErrors/index.d.ts +0 -2
  217. package/dist/components/QuoteWarningsAndErrors/index.d.ts.map +0 -1
  218. package/dist/containers/QuoteInfo/QuoteInfo.d.ts +0 -4
  219. package/dist/containers/QuoteInfo/QuoteInfo.d.ts.map +0 -1
  220. package/dist/containers/QuoteInfo/QuoteInfo.styles.d.ts +0 -472
  221. package/dist/containers/QuoteInfo/QuoteInfo.styles.d.ts.map +0 -1
  222. package/dist/containers/QuoteInfo/QuoteInfo.types.d.ts +0 -17
  223. package/dist/containers/QuoteInfo/QuoteInfo.types.d.ts.map +0 -1
  224. package/dist/containers/QuoteInfo/index.d.ts +0 -2
  225. package/dist/containers/QuoteInfo/index.d.ts.map +0 -1
  226. package/dist/hooks/useConfirmSwap/index.d.ts +0 -2
  227. package/dist/hooks/useConfirmSwap/index.d.ts.map +0 -1
  228. package/dist/hooks/useConfirmSwap/useConfirmSwap.d.ts +0 -3
  229. package/dist/hooks/useConfirmSwap/useConfirmSwap.d.ts.map +0 -1
  230. package/dist/hooks/useConfirmSwap/useConfirmSwap.helpers.d.ts +0 -16
  231. package/dist/hooks/useConfirmSwap/useConfirmSwap.helpers.d.ts.map +0 -1
  232. package/dist/hooks/useConfirmSwap/useConfirmSwap.types.d.ts.map +0 -1
  233. package/dist/hooks/useFetchQuote.d.ts.map +0 -1
  234. package/dist/hooks/useForceAutoConnect.d.ts +0 -2
  235. package/dist/hooks/useForceAutoConnect.d.ts.map +0 -1
  236. package/dist/store/AppStore.d.ts +0 -11
  237. package/dist/store/AppStore.d.ts.map +0 -1
  238. package/dist/store/quote.d.ts.map +0 -1
  239. package/dist/types/quote.d.ts +0 -82
  240. package/dist/types/quote.d.ts.map +0 -1
  241. package/dist/utils/quote.d.ts.map +0 -1
  242. package/src/components/NoResult/index.ts +0 -1
  243. package/src/components/Quote/Quote.styles.ts +0 -212
  244. package/src/components/Quote/Quote.tsx +0 -345
  245. package/src/components/Quote/Quote.types.ts +0 -16
  246. package/src/components/Quote/QuoteSkeleton.styles.ts +0 -110
  247. package/src/components/Quote/QuoteSkeleton.tsx +0 -108
  248. package/src/components/Quote/QuoteSkeleton.types.ts +0 -5
  249. package/src/components/Quote/QuoteSummary.styles.ts +0 -15
  250. package/src/components/Quote/QuoteSummary.tsx +0 -46
  251. package/src/components/Quote/index.ts +0 -3
  252. package/src/components/QuoteWarningsAndErrors/HighValueLossWarningModal.tsx +0 -107
  253. package/src/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.helpers.ts +0 -67
  254. package/src/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.tsx +0 -103
  255. package/src/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.types.ts +0 -24
  256. package/src/components/QuoteWarningsAndErrors/SlippageWariningModal.tsx +0 -77
  257. package/src/components/QuoteWarningsAndErrors/UnknownPriceWarningModal.tsx +0 -38
  258. package/src/components/QuoteWarningsAndErrors/index.ts +0 -1
  259. package/src/containers/QuoteInfo/QuoteInfo.styles.ts +0 -15
  260. package/src/containers/QuoteInfo/QuoteInfo.tsx +0 -97
  261. package/src/containers/QuoteInfo/QuoteInfo.types.ts +0 -17
  262. package/src/containers/QuoteInfo/index.ts +0 -1
  263. package/src/hooks/useConfirmSwap/index.ts +0 -1
  264. package/src/hooks/useConfirmSwap/useConfirmSwap.helpers.ts +0 -168
  265. package/src/hooks/useConfirmSwap/useConfirmSwap.ts +0 -136
  266. package/src/hooks/useConfirmSwap/useConfirmSwap.types.ts +0 -19
  267. package/src/hooks/useForceAutoConnect.ts +0 -27
  268. package/src/store/AppStore.tsx +0 -35
  269. package/src/types/quote.ts +0 -107
  270. package/src/utils/quote.ts +0 -338
  271. /package/src/components/{QuoteWarningsAndErrors/QuoteWarningsAndErrors.styles.ts → RouteErrors/RouteErrors.styles.ts} +0 -0
@@ -0,0 +1,303 @@
1
+ /* eslint-disable @typescript-eslint/no-magic-numbers */
2
+ import type {
3
+ ConfirmSwapError,
4
+ ConfirmSwapWarnings,
5
+ PendingSwapSettings,
6
+ Wallet,
7
+ } from '../types';
8
+ import type { PendingSwap } from '@rango-dev/queue-manager-rango-preset';
9
+ import type { BestRouteResponse } from 'rango-sdk';
10
+
11
+ import { calculatePendingSwap } from '@rango-dev/queue-manager-rango-preset';
12
+ import BigNumber from 'bignumber.js';
13
+ import { useEffect } from 'react';
14
+
15
+ import { errorMessages } from '../constants/errors';
16
+ import { HIGH_SLIPPAGE } from '../constants/swapSettings';
17
+ import { useBestRouteStore } from '../store/bestRoute';
18
+ import { useMetaStore } from '../store/meta';
19
+ import { useSettingsStore } from '../store/settings';
20
+ import { useWalletsStore } from '../store/wallets';
21
+ import {
22
+ ConfirmSwapErrorTypes,
23
+ RouteWarningType,
24
+ SlippageWarningType,
25
+ } from '../types';
26
+ import { numberToString } from '../utils/numbers';
27
+ import {
28
+ isNumberOfSwapsChanged,
29
+ isRouteChanged,
30
+ isRouteInternalCoinsUpdated,
31
+ isRouteSwappersUpdated,
32
+ } from '../utils/routing';
33
+ import {
34
+ createBestRouteRequestBody,
35
+ getBalanceWarnings,
36
+ getMinRequiredSlippage,
37
+ getOutputRatio,
38
+ getPercentageChange,
39
+ getRouteOutputAmount,
40
+ getWalletsForNewSwap,
41
+ hasProperSlippage,
42
+ isOutputAmountChangedALot,
43
+ outputRatioHasWarning,
44
+ } from '../utils/swap';
45
+
46
+ import { useFetchBestRoute } from './useFetchBestRoute';
47
+
48
+ export type ConfirmSwapFetchResult = {
49
+ swap: PendingSwap | null;
50
+ error: ConfirmSwapError | null;
51
+ warnings: ConfirmSwapWarnings | null;
52
+ };
53
+
54
+ export type ConfirmSwap = {
55
+ loading: boolean;
56
+ fetch: (params: Params) => Promise<ConfirmSwapFetchResult>;
57
+ cancelFetch: () => void;
58
+ };
59
+
60
+ type Params = {
61
+ selectedWallets: Wallet[];
62
+ customDestination?: string;
63
+ };
64
+
65
+ /**
66
+ * A request can be successful but in body of the response, it can be some case which is considered as failed.
67
+ */
68
+ function throwErrorIfResponseIsNotValid(
69
+ response: BestRouteResponse,
70
+ params: { inputUsdValue: BigNumber | null; outputUsdValue: number | null }
71
+ ) {
72
+ if (!response.result) {
73
+ throw new Error(errorMessages().noRoutesError.title, {
74
+ cause: {
75
+ type: ConfirmSwapErrorTypes.NO_ROUTE,
76
+ diagnosisMessage: response.diagnosisMessages?.[0],
77
+ },
78
+ });
79
+ }
80
+
81
+ const newRouteOutputUsdValue = new BigNumber(
82
+ response.result?.outputAmount || '0'
83
+ ).multipliedBy(params.outputUsdValue || 0);
84
+
85
+ const outputRatio = getOutputRatio(
86
+ params.inputUsdValue,
87
+ newRouteOutputUsdValue
88
+ );
89
+ const highValueLoss = outputRatioHasWarning(
90
+ params.inputUsdValue,
91
+ outputRatio
92
+ );
93
+
94
+ if (highValueLoss) {
95
+ throw new Error('High value loss for route', {
96
+ cause: {
97
+ type: ConfirmSwapErrorTypes.ROUTE_UPDATED_WITH_HIGH_VALUE_LOSS,
98
+ },
99
+ });
100
+ }
101
+
102
+ return response;
103
+ }
104
+
105
+ function generateWarnings(
106
+ previousRoute: BestRouteResponse,
107
+ currentRoute: BestRouteResponse,
108
+ params: {
109
+ selectedWallets: Wallet[];
110
+ userSlippage: number;
111
+ }
112
+ ): ConfirmSwapWarnings {
113
+ const routeChanged = isRouteChanged(previousRoute, currentRoute);
114
+ const output: ConfirmSwapWarnings = {
115
+ balance: null,
116
+ route: null,
117
+ slippage: null,
118
+ };
119
+
120
+ if (routeChanged) {
121
+ if (isOutputAmountChangedALot(previousRoute, currentRoute)) {
122
+ output.route = {
123
+ type: RouteWarningType.ROUTE_AND_OUTPUT_AMOUNT_UPDATED,
124
+ newOutputAmount: numberToString(getRouteOutputAmount(currentRoute)),
125
+ percentageChange: numberToString(
126
+ getPercentageChange(
127
+ getRouteOutputAmount(previousRoute),
128
+ getRouteOutputAmount(currentRoute)
129
+ ),
130
+ null,
131
+ 2
132
+ ),
133
+ };
134
+ } else if (isNumberOfSwapsChanged(previousRoute, currentRoute)) {
135
+ output.route = {
136
+ type: RouteWarningType.ROUTE_UPDATED,
137
+ };
138
+ } else if (isRouteSwappersUpdated(previousRoute, currentRoute)) {
139
+ output.route = {
140
+ type: RouteWarningType.ROUTE_SWAPPERS_UPDATED,
141
+ };
142
+ } else if (isRouteInternalCoinsUpdated(previousRoute, currentRoute)) {
143
+ output.route = {
144
+ type: RouteWarningType.ROUTE_COINS_UPDATED,
145
+ };
146
+ }
147
+ }
148
+
149
+ const balanceWarnings = getBalanceWarnings(
150
+ currentRoute,
151
+ params.selectedWallets
152
+ );
153
+ const enoughBalance = balanceWarnings.length === 0;
154
+
155
+ if (!enoughBalance) {
156
+ output.balance = {
157
+ messages: balanceWarnings,
158
+ };
159
+ }
160
+
161
+ const minRequiredSlippage = getMinRequiredSlippage(previousRoute);
162
+ const highSlippage = params.userSlippage > HIGH_SLIPPAGE;
163
+
164
+ if (!hasProperSlippage(params.userSlippage.toString(), minRequiredSlippage)) {
165
+ output.slippage = {
166
+ type: SlippageWarningType.INSUFFICIENT_SLIPPAGE,
167
+ slippage: minRequiredSlippage,
168
+ };
169
+ } else if (highSlippage) {
170
+ output.slippage = {
171
+ type: SlippageWarningType.HIGH_SLIPPAGE,
172
+ slippage: params.userSlippage.toString(),
173
+ };
174
+ }
175
+
176
+ return output;
177
+ }
178
+
179
+ export function useConfirmSwap(): ConfirmSwap {
180
+ const {
181
+ fromToken,
182
+ toToken,
183
+ inputAmount,
184
+ inputUsdValue,
185
+ setRoute,
186
+ bestRoute: initialRoute,
187
+ customDestination: customDestinationFromStore,
188
+ } = useBestRouteStore();
189
+
190
+ const {
191
+ slippage,
192
+ customSlippage,
193
+ affiliatePercent,
194
+ affiliateRef,
195
+ affiliateWallets,
196
+ disabledLiquiditySources,
197
+ } = useSettingsStore();
198
+ const { connectedWallets } = useWalletsStore();
199
+
200
+ const { meta } = useMetaStore();
201
+
202
+ const userSlippage = customSlippage || slippage;
203
+
204
+ const { fetch: fetchBestRoute, cancelFetch, loading } = useFetchBestRoute();
205
+
206
+ useEffect(() => cancelFetch, []);
207
+
208
+ const fetch: ConfirmSwap['fetch'] = async (params: Params) => {
209
+ const selectedWallets = params.selectedWallets;
210
+ const customDestination =
211
+ params?.customDestination ?? customDestinationFromStore;
212
+
213
+ if (!fromToken || !toToken || !inputAmount || !initialRoute) {
214
+ return {
215
+ route: null,
216
+ swap: null,
217
+ error: null,
218
+ warnings: null,
219
+ };
220
+ }
221
+
222
+ const requestBody = createBestRouteRequestBody({
223
+ fromToken,
224
+ toToken,
225
+ inputAmount,
226
+ wallets: connectedWallets,
227
+ selectedWallets,
228
+ disabledLiquiditySources,
229
+ slippage: userSlippage,
230
+ affiliateRef,
231
+ affiliatePercent,
232
+ affiliateWallets,
233
+ initialRoute,
234
+ destination: customDestination,
235
+ });
236
+
237
+ let currentRoute: BestRouteResponse;
238
+ try {
239
+ currentRoute = await fetchBestRoute(requestBody).then((response) =>
240
+ throwErrorIfResponseIsNotValid(response, {
241
+ outputUsdValue: toToken.usdPrice,
242
+ inputUsdValue,
243
+ })
244
+ );
245
+ setRoute(currentRoute);
246
+ } catch (error: any) {
247
+ if (error?.code === 'ERR_CANCELED') {
248
+ return {
249
+ swap: null,
250
+ error: {
251
+ type: ConfirmSwapErrorTypes.REQUEST_CANCELED,
252
+ },
253
+ warnings: null,
254
+ };
255
+ }
256
+
257
+ if (error.cause) {
258
+ return {
259
+ swap: null,
260
+ error: error.cause,
261
+ warnings: null,
262
+ };
263
+ }
264
+
265
+ return {
266
+ swap: null,
267
+ error: {
268
+ type: ConfirmSwapErrorTypes.REQUEST_FAILED,
269
+ },
270
+ warnings: null,
271
+ };
272
+ }
273
+
274
+ const swapSettings: PendingSwapSettings = {
275
+ slippage: userSlippage.toString(),
276
+ disabledSwappersGroups: disabledLiquiditySources,
277
+ };
278
+ const swap = calculatePendingSwap(
279
+ inputAmount.toString(),
280
+ currentRoute,
281
+ getWalletsForNewSwap(selectedWallets),
282
+ swapSettings,
283
+ false,
284
+ meta
285
+ );
286
+
287
+ return {
288
+ route: currentRoute,
289
+ swap,
290
+ error: null,
291
+ warnings: generateWarnings(initialRoute, currentRoute, {
292
+ selectedWallets,
293
+ userSlippage,
294
+ }),
295
+ };
296
+ };
297
+
298
+ return {
299
+ loading,
300
+ fetch,
301
+ cancelFetch,
302
+ };
303
+ }
@@ -4,18 +4,18 @@ import { useRef, useState } from 'react';
4
4
 
5
5
  import { httpService } from '../services/httpService';
6
6
 
7
- interface UseFetchQuote {
7
+ interface UseFetchBestRoute {
8
8
  fetch: (requestBody: BestRouteRequest) => Promise<BestRouteResponse>;
9
9
  cancelFetch: () => void;
10
10
  loading: boolean;
11
11
  }
12
12
 
13
- export function useFetchQuote(): UseFetchQuote {
13
+ export function useFetchBestRoute(): UseFetchBestRoute {
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: UseFetchQuote['fetch'] = async (requestBody) => {
18
+ const fetch: UseFetchBestRoute['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 { useQuoteStore } from '../store/quote';
4
+ import { useBestRouteStore } from '../store/bestRoute';
5
5
 
6
6
  export function useNavigateBack() {
7
7
  const isRouterInContext = useInRouterContext();
8
8
  const navigate = useNavigate();
9
- const quoteWalletsConfirmed = useQuoteStore.use.quoteWalletsConfirmed();
9
+ const routeWalletsConfirmed = useBestRouteStore.use.routeWalletsConfirmed();
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
- quoteWalletsConfirmed
29
+ routeWalletsConfirmed
30
30
  ) {
31
31
  return navigate('/' + navigationRoutes.confirmSwap, { replace: true });
32
32
  }
@@ -1,48 +1,37 @@
1
- import type { QuoteError, QuoteWarning } from '../types';
2
-
3
1
  import { useEffect, useRef, useState } from 'react';
4
2
 
5
- import { useAppStore } from '../store/AppStore';
6
- import { useQuoteStore } from '../store/quote';
3
+ import { useBestRouteStore } from '../store/bestRoute';
7
4
  import { useSettingsStore } from '../store/settings';
8
- import { QuoteErrorType } from '../types';
9
5
  import { debounce } from '../utils/common';
10
6
  import { isPositiveNumber } from '../utils/numbers';
11
- import { generateQuoteWarnings } from '../utils/quote';
12
- import { createQuoteRequestBody } from '../utils/swap';
7
+ import { createBestRouteRequestBody } from '../utils/swap';
13
8
  import { tokensAreEqual } from '../utils/wallets';
14
9
 
15
- import {
16
- handleQuoteErrors,
17
- throwErrorIfResponseIsNotValid,
18
- } from './useConfirmSwap/useConfirmSwap.helpers';
19
- import { useFetchQuote } from './useFetchQuote';
10
+ import { useFetchBestRoute } from './useFetchBestRoute';
20
11
 
21
12
  const DEBOUNCE_DELAY = 600;
22
13
 
23
14
  type UseSwapInput = {
24
15
  fetch: () => void;
25
16
  loading: boolean;
26
- error: QuoteError | null;
27
- warning: QuoteWarning | null;
17
+ error: string;
28
18
  };
29
19
 
30
20
  /**
31
- * a hook for fetching quote based on from and to input values
21
+ * a hook for fetching best route based on from and to input values
32
22
  * we use this hook in home page
33
23
  */
34
24
  export function useSwapInput(): UseSwapInput {
35
- const { fetch: fetchQuote, cancelFetch } = useFetchQuote();
36
- const { liquiditySources, enableNewLiquiditySources } = useAppStore().config;
37
- const tokens = useAppStore().tokens();
25
+ const { fetch: fetchBestRoute, cancelFetch } = useFetchBestRoute();
26
+ const [loading, setLoading] = useState(false);
38
27
  const {
39
28
  fromToken,
40
29
  toToken,
41
30
  inputAmount,
42
31
  inputUsdValue,
43
- resetQuote,
44
- setQuote,
45
- } = useQuoteStore();
32
+ resetRoute,
33
+ setRoute,
34
+ } = useBestRouteStore();
46
35
  const {
47
36
  slippage,
48
37
  customSlippage,
@@ -51,9 +40,7 @@ export function useSwapInput(): UseSwapInput {
51
40
  affiliateWallets,
52
41
  disabledLiquiditySources,
53
42
  } = useSettingsStore();
54
- const [loading, setLoading] = useState(false);
55
- const [error, setError] = useState<QuoteError | null>(null);
56
- const [warning, setWarning] = useState<QuoteWarning | null>(null);
43
+ const [error, setError] = useState('');
57
44
  const prevDisabledLiquiditySources = useRef(disabledLiquiditySources);
58
45
  const userSlippage = customSlippage ?? slippage;
59
46
  const hasTokensValue = !fromToken || !toToken;
@@ -62,53 +49,31 @@ export function useSwapInput(): UseSwapInput {
62
49
  tokensAreEqual(fromToken, toToken) ||
63
50
  !isPositiveNumber(inputAmount);
64
51
 
65
- const resetState = (loading: boolean) => {
66
- setLoading(loading);
67
- setError(null);
68
- setWarning(null);
69
- };
70
-
71
52
  const fetch: UseSwapInput['fetch'] = () => {
72
53
  if (!loading) {
73
- resetState(true);
54
+ setLoading(true);
74
55
  }
75
56
  if (!shouldSkipRequest) {
76
- resetQuote();
77
- const requestBody = createQuoteRequestBody({
57
+ resetRoute();
58
+ const requestBody = createBestRouteRequestBody({
78
59
  fromToken,
79
60
  toToken,
80
61
  inputAmount,
81
- liquiditySources,
82
- excludeLiquiditySources: enableNewLiquiditySources,
83
62
  disabledLiquiditySources,
84
63
  slippage: userSlippage,
85
64
  affiliateRef,
86
65
  affiliatePercent,
87
66
  affiliateWallets,
88
67
  });
89
- fetchQuote(requestBody)
68
+ fetchBestRoute(requestBody)
90
69
  .then((res) => {
91
70
  setLoading(false);
92
- setQuote(res);
93
- throwErrorIfResponseIsNotValid(res);
94
- const quoteWarning = generateQuoteWarnings(res, {
95
- fromToken,
96
- toToken,
97
- userSlippage,
98
- tokens,
99
- });
100
- setWarning(quoteWarning);
71
+ setRoute(res);
101
72
  })
102
73
  .catch((error) => {
103
- const { error: quoteError } = handleQuoteErrors(error);
104
- if (
105
- quoteError?.type === QuoteErrorType.NO_RESULT ||
106
- quoteError?.type === QuoteErrorType.REQUEST_FAILED
107
- ) {
108
- resetQuote();
109
- }
110
- if (quoteError?.type !== QuoteErrorType.REQUEST_CANCELED) {
111
- setError(quoteError);
74
+ resetRoute();
75
+ if (error?.code !== 'ERR_CANCELED') {
76
+ setError(error.message);
112
77
  setLoading(false);
113
78
  }
114
79
  });
@@ -123,15 +88,15 @@ export function useSwapInput(): UseSwapInput {
123
88
 
124
89
  useEffect(() => {
125
90
  if (!isPositiveNumber(inputAmount) || inputUsdValue?.eq(0)) {
126
- resetState(false);
91
+ setLoading(false);
127
92
  cancelFetch();
128
93
  return;
129
94
  }
130
95
  if (shouldSkipRequest) {
131
96
  return;
132
97
  }
133
- resetQuote();
134
- resetState(true);
98
+ resetRoute();
99
+ setLoading(true);
135
100
  debouncedFetch();
136
101
  return cancelFetch;
137
102
  }, [inputAmount, shouldSkipRequest]);
@@ -147,5 +112,5 @@ export function useSwapInput(): UseSwapInput {
147
112
  return cancelFetch;
148
113
  }, [disabledLiquiditySources.length]);
149
114
 
150
- return { fetch, loading, error, warning };
115
+ return { fetch, loading, error };
151
116
  }
@@ -1,13 +1,14 @@
1
+ import type { WidgetConfig } from '../types';
1
2
  import type { Asset } from 'rango-sdk';
2
3
 
3
4
  import { useEffect, useMemo, useRef } from 'react';
4
5
 
5
- import { useAppStore } from '../store/AppStore';
6
- import { useQuoteStore } from '../store/quote';
6
+ import { useBestRouteStore } from '../store/bestRoute';
7
+ import { useMetaStore } from '../store/meta';
7
8
  import { useSettingsStore } from '../store/settings';
8
9
  import { tokensAreEqual } from '../utils/wallets';
9
10
 
10
- export function useSyncStoresWithConfig() {
11
+ export function useSyncStoresWithConfig(config: WidgetConfig | undefined) {
11
12
  const {
12
13
  setInputAmount,
13
14
  setToToken,
@@ -18,12 +19,12 @@ export function useSyncStoresWithConfig() {
18
19
  toToken,
19
20
  fromBlockchain,
20
21
  toBlockchain,
21
- } = useQuoteStore();
22
+ } = useBestRouteStore();
22
23
 
23
- const config = useAppStore().config;
24
- const fetchMetaStatus = useAppStore().fetchStatus;
25
- const blockchains = useAppStore().blockchains();
26
- const tokens = useAppStore().tokens();
24
+ const {
25
+ meta: { tokens, blockchains },
26
+ loadingStatus: loadingMetaStatus,
27
+ } = useMetaStore();
27
28
 
28
29
  const { setAffiliateRef, setAffiliatePercent, setAffiliateWallets } =
29
30
  useSettingsStore();
@@ -54,7 +55,7 @@ export function useSyncStoresWithConfig() {
54
55
  }, [config?.amount]);
55
56
 
56
57
  useEffect(() => {
57
- if (fetchMetaStatus === 'success') {
58
+ if (loadingMetaStatus === 'success') {
58
59
  const chain = blockchains.find(
59
60
  (chain) => chain.name === config?.from?.blockchain
60
61
  );
@@ -66,10 +67,8 @@ export function useSyncStoresWithConfig() {
66
67
  setFromBlockchain(chain ?? null);
67
68
  }
68
69
 
69
- if (token) {
70
- setFromToken({ token, meta: { blockchains, tokens } });
71
- } else if (!token && prevConfigFromToken.current) {
72
- setFromToken({ token: null });
70
+ if (token || (!token && prevConfigFromToken.current)) {
71
+ setFromToken(token ?? null);
73
72
  }
74
73
 
75
74
  prevConfigFromBlockchain.current = config?.from?.blockchain;
@@ -80,13 +79,13 @@ export function useSyncStoresWithConfig() {
80
79
  config?.from?.token?.address,
81
80
  config?.from?.token?.blockchain,
82
81
  config?.from?.blockchain,
83
- fetchMetaStatus,
82
+ loadingMetaStatus,
84
83
  ]);
85
84
 
86
85
  useEffect(() => {
87
86
  if (fromToken && fromTokensConfig) {
88
87
  if (!fromTokensConfig.some((token) => tokensAreEqual(token, fromToken))) {
89
- setFromToken({ token: null });
88
+ setFromToken(null);
90
89
  }
91
90
  }
92
91
 
@@ -100,7 +99,7 @@ export function useSyncStoresWithConfig() {
100
99
  useEffect(() => {
101
100
  if (toToken && toTokensConfig) {
102
101
  if (!toTokensConfig.some((token) => tokensAreEqual(token, toToken))) {
103
- setToToken({ token: null });
102
+ setToToken(null);
104
103
  }
105
104
  }
106
105
 
@@ -112,7 +111,7 @@ export function useSyncStoresWithConfig() {
112
111
  }, [toTokensConfig, toBlockchainsConfig]);
113
112
 
114
113
  useEffect(() => {
115
- if (fetchMetaStatus === 'success') {
114
+ if (loadingMetaStatus === 'success') {
116
115
  const chain = blockchains.find(
117
116
  (chain) => chain.name === config?.to?.blockchain
118
117
  );
@@ -124,10 +123,8 @@ export function useSyncStoresWithConfig() {
124
123
  setToBlockchain(chain ?? null);
125
124
  }
126
125
 
127
- if (token) {
128
- setToToken({ token, meta: { blockchains, tokens } });
129
- } else if (!token && prevConfigToToken.current) {
130
- setToToken({ token: null });
126
+ if (token || (!token && prevConfigToToken.current)) {
127
+ setToToken(token ?? null);
131
128
  }
132
129
 
133
130
  prevConfigToBlockchain.current = config?.to?.blockchain;
@@ -138,7 +135,7 @@ export function useSyncStoresWithConfig() {
138
135
  config?.to?.token?.address,
139
136
  config?.to?.token?.blockchain,
140
137
  config?.to?.blockchain,
141
- fetchMetaStatus,
138
+ loadingMetaStatus,
142
139
  ]);
143
140
 
144
141
  useEffect(() => {
@@ -10,7 +10,7 @@ import {
10
10
  } from '@rango-dev/wallets-shared';
11
11
  import { useEffect, useState } from 'react';
12
12
 
13
- import { useAppStore } from '../store/AppStore';
13
+ import { useMetaStore } from '../store/meta';
14
14
  import { useWalletsStore } from '../store/wallets';
15
15
  import { configWalletsToWalletName } from '../utils/providers';
16
16
  import {
@@ -37,7 +37,9 @@ export function useWalletList(params: Params) {
37
37
  const { config, chain, onBeforeConnect, onConnect } = params;
38
38
  const { state, disconnect, getWalletInfo, connect } = useWallets();
39
39
  const { connectedWallets } = useWalletsStore();
40
- const blockchains = useAppStore().blockchains();
40
+ const { blockchains } = useMetaStore().meta;
41
+ const multiWallets =
42
+ typeof config?.multiWallets === 'undefined' ? true : config.multiWallets;
41
43
 
42
44
  /** It can be what has been set by widget config or as a fallback we use all the supported wallets by our library */
43
45
  const listAvailableWalletTypes =
@@ -74,7 +76,7 @@ export function useWalletList(params: Params) {
74
76
  const atLeastOneWalletIsConnected = !!wallets.find(
75
77
  (w) => w.state === WalletState.CONNECTED
76
78
  );
77
- if (!config?.multiWallets && atLeastOneWalletIsConnected) {
79
+ if (!multiWallets && atLeastOneWalletIsConnected) {
78
80
  return;
79
81
  }
80
82
  onBeforeConnect?.(type);