@rango-dev/widget-embedded 0.1.10-next.100

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 (204) hide show
  1. package/dist/App.d.ts +9 -0
  2. package/dist/App.d.ts.map +1 -0
  3. package/dist/QueueManager.d.ts +4 -0
  4. package/dist/QueueManager.d.ts.map +1 -0
  5. package/dist/components/AppRouter.d.ts +9 -0
  6. package/dist/components/AppRouter.d.ts.map +1 -0
  7. package/dist/components/AppRoutes.d.ts +8 -0
  8. package/dist/components/AppRoutes.d.ts.map +1 -0
  9. package/dist/components/BottomLogo.d.ts +3 -0
  10. package/dist/components/BottomLogo.d.ts.map +1 -0
  11. package/dist/components/ChangeSlippageButton.d.ts +3 -0
  12. package/dist/components/ChangeSlippageButton.d.ts.map +1 -0
  13. package/dist/components/ConfirmSwapErrors.d.ts +4 -0
  14. package/dist/components/ConfirmSwapErrors.d.ts.map +1 -0
  15. package/dist/components/ConfirmSwapWarnings.d.ts +4 -0
  16. package/dist/components/ConfirmSwapWarnings.d.ts.map +1 -0
  17. package/dist/components/Footer.d.ts +2 -0
  18. package/dist/components/Footer.d.ts.map +1 -0
  19. package/dist/components/Header.d.ts +10 -0
  20. package/dist/components/Header.d.ts.map +1 -0
  21. package/dist/components/HeaderButtons.d.ts +4 -0
  22. package/dist/components/HeaderButtons.d.ts.map +1 -0
  23. package/dist/components/Layout.d.ts +7 -0
  24. package/dist/components/Layout.d.ts.map +1 -0
  25. package/dist/components/RoutesOverview.d.ts +10 -0
  26. package/dist/components/RoutesOverview.d.ts.map +1 -0
  27. package/dist/components/SwitchFromAndTo.d.ts +4 -0
  28. package/dist/components/SwitchFromAndTo.d.ts.map +1 -0
  29. package/dist/components/TokenInfo.d.ts +18 -0
  30. package/dist/components/TokenInfo.d.ts.map +1 -0
  31. package/dist/components/TokenPreview.d.ts +20 -0
  32. package/dist/components/TokenPreview.d.ts.map +1 -0
  33. package/dist/components/UpdateUrl.d.ts +2 -0
  34. package/dist/components/UpdateUrl.d.ts.map +1 -0
  35. package/dist/components/warnings/BalanceWarnings.d.ts +7 -0
  36. package/dist/components/warnings/BalanceWarnings.d.ts.map +1 -0
  37. package/dist/components/warnings/ConfirmSwapExtraMessages.d.ts +7 -0
  38. package/dist/components/warnings/ConfirmSwapExtraMessages.d.ts.map +1 -0
  39. package/dist/components/warnings/HighSlippageWarning.d.ts +7 -0
  40. package/dist/components/warnings/HighSlippageWarning.d.ts.map +1 -0
  41. package/dist/components/warnings/MinRequiredSlippage.d.ts +7 -0
  42. package/dist/components/warnings/MinRequiredSlippage.d.ts.map +1 -0
  43. package/dist/constants/errors.d.ts +4 -0
  44. package/dist/constants/errors.d.ts.map +1 -0
  45. package/dist/constants/navigationRoutes.d.ts +15 -0
  46. package/dist/constants/navigationRoutes.d.ts.map +1 -0
  47. package/dist/constants/numbers.d.ts +3 -0
  48. package/dist/constants/numbers.d.ts.map +1 -0
  49. package/dist/constants/searchParams.d.ts +8 -0
  50. package/dist/constants/searchParams.d.ts.map +1 -0
  51. package/dist/constants/swapSettings.d.ts +6 -0
  52. package/dist/constants/swapSettings.d.ts.map +1 -0
  53. package/dist/globalStyles.d.ts +3 -0
  54. package/dist/globalStyles.d.ts.map +1 -0
  55. package/dist/hooks/useConfirmSwap.d.ts +11 -0
  56. package/dist/hooks/useConfirmSwap.d.ts.map +1 -0
  57. package/dist/hooks/useCopyToClipboard.d.ts +2 -0
  58. package/dist/hooks/useCopyToClipboard.d.ts.map +1 -0
  59. package/dist/hooks/useNavigateBack.d.ts +4 -0
  60. package/dist/hooks/useNavigateBack.d.ts.map +1 -0
  61. package/dist/hooks/usePrevious.d.ts +2 -0
  62. package/dist/hooks/usePrevious.d.ts.map +1 -0
  63. package/dist/hooks/useSelectLanguage.d.ts +5 -0
  64. package/dist/hooks/useSelectLanguage.d.ts.map +1 -0
  65. package/dist/hooks/useTheme.d.ts +8 -0
  66. package/dist/hooks/useTheme.d.ts.map +1 -0
  67. package/dist/i18n.d.ts +3 -0
  68. package/dist/i18n.d.ts.map +1 -0
  69. package/dist/index.d.ts +2 -0
  70. package/dist/index.d.ts.map +1 -0
  71. package/dist/index.js +8 -0
  72. package/dist/lib.d.ts +8 -0
  73. package/dist/lib.d.ts.map +1 -0
  74. package/dist/mockData/pendingSwap.d.ts +2 -0
  75. package/dist/mockData/pendingSwap.d.ts.map +1 -0
  76. package/dist/pages/ConfirmSwapPage.d.ts +3 -0
  77. package/dist/pages/ConfirmSwapPage.d.ts.map +1 -0
  78. package/dist/pages/ConfirmWalletsPage.d.ts +3 -0
  79. package/dist/pages/ConfirmWalletsPage.d.ts.map +1 -0
  80. package/dist/pages/HistoryPage.d.ts +3 -0
  81. package/dist/pages/HistoryPage.d.ts.map +1 -0
  82. package/dist/pages/Home.d.ts +9 -0
  83. package/dist/pages/Home.d.ts.map +1 -0
  84. package/dist/pages/LiquiditySourcesPage.d.ts +8 -0
  85. package/dist/pages/LiquiditySourcesPage.d.ts.map +1 -0
  86. package/dist/pages/SelectChainPage.d.ts +9 -0
  87. package/dist/pages/SelectChainPage.d.ts.map +1 -0
  88. package/dist/pages/SelectTokenPage.d.ts +15 -0
  89. package/dist/pages/SelectTokenPage.d.ts.map +1 -0
  90. package/dist/pages/SettingsPage.d.ts +8 -0
  91. package/dist/pages/SettingsPage.d.ts.map +1 -0
  92. package/dist/pages/SwapDetailsPage.d.ts +3 -0
  93. package/dist/pages/SwapDetailsPage.d.ts.map +1 -0
  94. package/dist/pages/WalletsPage.d.ts +9 -0
  95. package/dist/pages/WalletsPage.d.ts.map +1 -0
  96. package/dist/services/httpService.d.ts +3 -0
  97. package/dist/services/httpService.d.ts.map +1 -0
  98. package/dist/store/bestRoute.d.ts +28 -0
  99. package/dist/store/bestRoute.d.ts.map +1 -0
  100. package/dist/store/meta.d.ts +9 -0
  101. package/dist/store/meta.d.ts.map +1 -0
  102. package/dist/store/selectors.d.ts +11 -0
  103. package/dist/store/selectors.d.ts.map +1 -0
  104. package/dist/store/settings.d.ts +17 -0
  105. package/dist/store/settings.d.ts.map +1 -0
  106. package/dist/store/ui.d.ts +2 -0
  107. package/dist/store/ui.d.ts.map +1 -0
  108. package/dist/store/wallets.d.ts +28 -0
  109. package/dist/store/wallets.d.ts.map +1 -0
  110. package/dist/types/config.d.ts +40 -0
  111. package/dist/types/config.d.ts.map +1 -0
  112. package/dist/types/index.d.ts +4 -0
  113. package/dist/types/index.d.ts.map +1 -0
  114. package/dist/types/routing.d.ts +45 -0
  115. package/dist/types/routing.d.ts.map +1 -0
  116. package/dist/types/swap.d.ts +11 -0
  117. package/dist/types/swap.d.ts.map +1 -0
  118. package/dist/utils/common.d.ts +4 -0
  119. package/dist/utils/common.d.ts.map +1 -0
  120. package/dist/utils/numbers.d.ts +14 -0
  121. package/dist/utils/numbers.d.ts.map +1 -0
  122. package/dist/utils/queue.d.ts +4 -0
  123. package/dist/utils/queue.d.ts.map +1 -0
  124. package/dist/utils/routing.d.ts +18 -0
  125. package/dist/utils/routing.d.ts.map +1 -0
  126. package/dist/utils/swap.d.ts +45 -0
  127. package/dist/utils/swap.d.ts.map +1 -0
  128. package/dist/utils/wallets.d.ts +29 -0
  129. package/dist/utils/wallets.d.ts.map +1 -0
  130. package/dist/widget-embedded.cjs.development.js +3101 -0
  131. package/dist/widget-embedded.cjs.development.js.map +1 -0
  132. package/dist/widget-embedded.cjs.production.min.js +3101 -0
  133. package/dist/widget-embedded.cjs.production.min.js.map +1 -0
  134. package/dist/widget-embedded.esm.js +3092 -0
  135. package/dist/widget-embedded.esm.js.map +1 -0
  136. package/package.json +55 -0
  137. package/readme.md +2 -0
  138. package/src/App.tsx +91 -0
  139. package/src/QueueManager.tsx +94 -0
  140. package/src/app.css +27 -0
  141. package/src/components/AppRouter.tsx +60 -0
  142. package/src/components/AppRoutes.tsx +105 -0
  143. package/src/components/BottomLogo.tsx +47 -0
  144. package/src/components/ChangeSlippageButton.tsx +19 -0
  145. package/src/components/ConfirmSwapErrors.tsx +38 -0
  146. package/src/components/ConfirmSwapWarnings.tsx +35 -0
  147. package/src/components/Footer.tsx +8 -0
  148. package/src/components/Header.tsx +37 -0
  149. package/src/components/HeaderButtons.tsx +47 -0
  150. package/src/components/Layout.tsx +96 -0
  151. package/src/components/RoutesOverview.tsx +60 -0
  152. package/src/components/SwitchFromAndTo.tsx +30 -0
  153. package/src/components/TokenInfo.tsx +311 -0
  154. package/src/components/TokenPreview.tsx +175 -0
  155. package/src/components/UpdateUrl.tsx +162 -0
  156. package/src/components/warnings/BalanceWarnings.tsx +39 -0
  157. package/src/components/warnings/ConfirmSwapExtraMessages.tsx +20 -0
  158. package/src/components/warnings/HighSlippageWarning.tsx +18 -0
  159. package/src/components/warnings/MinRequiredSlippage.tsx +18 -0
  160. package/src/constants/errors.ts +3 -0
  161. package/src/constants/navigationRoutes.ts +14 -0
  162. package/src/constants/numbers.ts +3 -0
  163. package/src/constants/searchParams.ts +7 -0
  164. package/src/constants/swapSettings.ts +9 -0
  165. package/src/globalStyles.ts +26 -0
  166. package/src/hooks/useConfirmSwap.ts +260 -0
  167. package/src/hooks/useCopyToClipboard.ts +23 -0
  168. package/src/hooks/useNavigateBack.ts +40 -0
  169. package/src/hooks/usePrevious.ts +13 -0
  170. package/src/hooks/useSelectLanguage.ts +15 -0
  171. package/src/hooks/useTheme.ts +127 -0
  172. package/src/i18n.js +30 -0
  173. package/src/index.tsx +14 -0
  174. package/src/languages/en.json +14 -0
  175. package/src/languages/tr.json +11 -0
  176. package/src/lib.tsx +119 -0
  177. package/src/mockData/pendingSwap.ts +607 -0
  178. package/src/pages/ConfirmSwapPage.tsx +72 -0
  179. package/src/pages/ConfirmWalletsPage.tsx +127 -0
  180. package/src/pages/HistoryPage.tsx +29 -0
  181. package/src/pages/Home.tsx +228 -0
  182. package/src/pages/LiquiditySourcesPage.tsx +64 -0
  183. package/src/pages/SelectChainPage.tsx +42 -0
  184. package/src/pages/SelectTokenPage.tsx +64 -0
  185. package/src/pages/SettingsPage.tsx +98 -0
  186. package/src/pages/SwapDetailsPage.tsx +38 -0
  187. package/src/pages/WalletsPage.tsx +119 -0
  188. package/src/services/httpService.ts +5 -0
  189. package/src/store/bestRoute.ts +245 -0
  190. package/src/store/meta.ts +35 -0
  191. package/src/store/selectors.ts +19 -0
  192. package/src/store/settings.ts +84 -0
  193. package/src/store/ui.ts +21 -0
  194. package/src/store/wallets.ts +190 -0
  195. package/src/types/config.ts +44 -0
  196. package/src/types/index.ts +3 -0
  197. package/src/types/routing.ts +60 -0
  198. package/src/types/swap.ts +13 -0
  199. package/src/utils/common.ts +35 -0
  200. package/src/utils/numbers.ts +133 -0
  201. package/src/utils/queue.ts +24 -0
  202. package/src/utils/routing.ts +150 -0
  203. package/src/utils/swap.ts +552 -0
  204. package/src/utils/wallets.ts +481 -0
@@ -0,0 +1,162 @@
1
+ import { useEffect, useRef } from 'react';
2
+ import {
3
+ createSearchParams,
4
+ useLocation,
5
+ useSearchParams,
6
+ } from 'react-router-dom';
7
+ import { useBestRouteStore } from '../store/bestRoute';
8
+ import { useMetaStore } from '../store/meta';
9
+ import { SearchParams } from '../constants/searchParams';
10
+ import { navigationRoutes } from '../constants/navigationRoutes';
11
+ import { useUiStore } from '../store/ui';
12
+ import { searchParamsToToken } from '../utils/routing';
13
+
14
+ export function UpdateUrl() {
15
+ const firstRender = useRef(true);
16
+ const [searchParams, setSearchParams] = useSearchParams();
17
+ const location = useLocation();
18
+ const firstRenderSearchParams = useRef(location.search);
19
+ const searchParamsRef = useRef<Record<string, string>>({});
20
+ const fromChain = useBestRouteStore.use.fromChain();
21
+ const toChain = useBestRouteStore.use.toChain();
22
+ const fromToken = useBestRouteStore.use.fromToken();
23
+ const toToken = useBestRouteStore.use.toToken();
24
+ const setFromChain = useBestRouteStore.use.setFromChain();
25
+ const setFromToken = useBestRouteStore.use.setFromToken();
26
+ const setToChain = useBestRouteStore.use.setToChain();
27
+ const setToToken = useBestRouteStore.use.setToToken();
28
+ const inputAmount = useBestRouteStore.use.inputAmount();
29
+ const setInputAmount = useBestRouteStore.use.setInputAmount();
30
+ const loadingStatus = useMetaStore.use.loadingStatus();
31
+ const { blockchains, tokens } = useMetaStore.use.meta();
32
+ const setSelectedSwap = useUiStore.use.setSelectedSwap();
33
+
34
+ useEffect(() => {
35
+ const params: Record<string, string> = {};
36
+ createSearchParams(firstRenderSearchParams.current).forEach(
37
+ (value, key) => {
38
+ params[key] = value;
39
+ }
40
+ );
41
+ searchParamsRef.current = params;
42
+ const requestId =
43
+ location.pathname.split(navigationRoutes.swaps + '/')[1] || null;
44
+ if (requestId) setSelectedSwap(requestId);
45
+ }, []);
46
+
47
+ useEffect(() => {
48
+ let fromChainString = '',
49
+ fromTokenString = '',
50
+ toChainString = '',
51
+ toTokenString = '',
52
+ fromAmount = '';
53
+ if (
54
+ loadingStatus !== 'success' &&
55
+ ![navigationRoutes.confirmWallets, navigationRoutes.confirmSwap].includes(
56
+ location.pathname
57
+ )
58
+ ) {
59
+ fromChainString = searchParamsRef.current[SearchParams.FROM_CHAIN];
60
+ fromTokenString = searchParamsRef.current[SearchParams.FROM_TOKEN];
61
+ toChainString = searchParamsRef.current[SearchParams.TO_CHAIN];
62
+ toTokenString = searchParamsRef.current[SearchParams.TO_TOKEN];
63
+ fromAmount = searchParamsRef.current[SearchParams.FROM_AMOUNT];
64
+ setSearchParams(
65
+ {
66
+ ...(fromChainString && {
67
+ [SearchParams.FROM_CHAIN]: fromChainString,
68
+ }),
69
+ ...(fromTokenString && {
70
+ [SearchParams.FROM_TOKEN]: fromTokenString,
71
+ }),
72
+ ...(toChainString && { [SearchParams.TO_CHAIN]: toChainString }),
73
+ ...(toTokenString && { [SearchParams.TO_TOKEN]: toTokenString }),
74
+ ...(fromAmount && {
75
+ [SearchParams.FROM_AMOUNT]: fromAmount.toString(),
76
+ }),
77
+ },
78
+ { replace: true }
79
+ );
80
+ }
81
+ if (!firstRender.current) {
82
+ let fromChainString = '',
83
+ fromTokenString = '',
84
+ toChainString = '',
85
+ toTokenString = '',
86
+ fromAmount = '';
87
+ if (loadingStatus !== 'success') {
88
+ fromChainString = searchParamsRef.current[SearchParams.FROM_CHAIN];
89
+ fromTokenString = searchParamsRef.current[SearchParams.FROM_TOKEN];
90
+ toChainString = searchParamsRef.current[SearchParams.TO_CHAIN];
91
+ toTokenString = searchParamsRef.current[SearchParams.TO_TOKEN];
92
+ fromAmount = searchParamsRef.current[SearchParams.FROM_AMOUNT];
93
+ } else {
94
+ if (location.state === 'redirect') return;
95
+ fromChainString = fromChain?.name || '';
96
+ fromTokenString =
97
+ (fromToken?.symbol || '') +
98
+ (fromToken?.address ? `--${fromToken?.address}` : '');
99
+ toChainString = toChain?.name || '';
100
+ toTokenString =
101
+ (toToken?.symbol || '') +
102
+ (toToken?.address ? `--${toToken?.address}` : '');
103
+ fromAmount = inputAmount;
104
+
105
+ setSearchParams(
106
+ {
107
+ ...(fromChainString && {
108
+ [SearchParams.FROM_CHAIN]: fromChainString,
109
+ }),
110
+ ...(fromTokenString && {
111
+ [SearchParams.FROM_TOKEN]: fromTokenString,
112
+ }),
113
+ ...(toChainString && { [SearchParams.TO_CHAIN]: toChainString }),
114
+ ...(toTokenString && { [SearchParams.TO_TOKEN]: toTokenString }),
115
+ ...(fromAmount && {
116
+ [SearchParams.FROM_AMOUNT]: fromAmount.toString(),
117
+ }),
118
+ },
119
+ { replace: true }
120
+ );
121
+ }
122
+ }
123
+ firstRender.current = false;
124
+ }, [location.pathname, inputAmount]);
125
+
126
+ useEffect(() => {
127
+ if (loadingStatus === 'success') {
128
+ const fromChainString = searchParams.get(SearchParams.FROM_CHAIN);
129
+ const fromTokenString = searchParams.get(SearchParams.FROM_TOKEN);
130
+ const toChainString = searchParams.get(SearchParams.TO_CHAIN);
131
+ const toTokenString = searchParams.get(SearchParams.TO_TOKEN);
132
+ const fromAmount = searchParams.get(SearchParams.FROM_AMOUNT);
133
+ const fromChain = blockchains.find(
134
+ (blockchain) => blockchain.name === fromChainString
135
+ );
136
+ const fromToken = searchParamsToToken(
137
+ tokens,
138
+ fromTokenString,
139
+ fromChain || null
140
+ );
141
+ const toChain = blockchains.find(
142
+ (blockchain) => blockchain.name === toChainString
143
+ );
144
+ const toToken = searchParamsToToken(
145
+ tokens,
146
+ toTokenString,
147
+ toChain || null
148
+ );
149
+ if (!!fromChain) {
150
+ setFromChain(fromChain);
151
+ if (!!fromToken) setFromToken(fromToken);
152
+ }
153
+ if (!!toChain) {
154
+ setToChain(toChain);
155
+ if (!!toToken) setToToken(toToken);
156
+ }
157
+ if (fromAmount) setInputAmount(fromAmount);
158
+ }
159
+ }, [loadingStatus]);
160
+
161
+ return null;
162
+ }
@@ -0,0 +1,39 @@
1
+ import { styled, Typography } from '@rango-dev/ui';
2
+ import React from 'react';
3
+
4
+ interface PropTypes {
5
+ messages: string[];
6
+ }
7
+
8
+ const List = styled('ul', {
9
+ variants: {
10
+ showListStyle: {
11
+ true: { paddingLeft: '$24' },
12
+ },
13
+ },
14
+ });
15
+
16
+ const ListItem = styled('li', {
17
+ variants: {
18
+ showListStyle: {
19
+ true: { listStyleType: 'disc', listStylePosition: 'outside' },
20
+ },
21
+ },
22
+ });
23
+
24
+ const Message = styled(Typography, {
25
+ display: 'block',
26
+ });
27
+
28
+ export function BalanceWarnings({ messages }: PropTypes) {
29
+ const showListStyle = messages.length > 1;
30
+ return (
31
+ <List showListStyle={showListStyle}>
32
+ {messages.map((warning) => (
33
+ <ListItem showListStyle={showListStyle}>
34
+ <Message variant="body2">{warning}</Message>
35
+ </ListItem>
36
+ ))}
37
+ </List>
38
+ );
39
+ }
@@ -0,0 +1,20 @@
1
+ import React from 'react';
2
+ import { HIGH_SLIPPAGE } from '../../constants/swapSettings';
3
+ import { HighSlippageWarning } from './HighSlippageWarning';
4
+
5
+ interface PropTypes {
6
+ selectedSlippage: number;
7
+ }
8
+
9
+ export function ConfirmSwapExtraMessages(props: PropTypes) {
10
+ const { selectedSlippage } = props;
11
+ const highSlippage = selectedSlippage >= HIGH_SLIPPAGE;
12
+
13
+ return (
14
+ <>
15
+ {highSlippage && (
16
+ <HighSlippageWarning selectedSlippage={selectedSlippage} />
17
+ )}
18
+ </>
19
+ );
20
+ }
@@ -0,0 +1,18 @@
1
+ import { Alert } from '@rango-dev/ui';
2
+ import React from 'react';
3
+ import { ChangeSlippageButton } from '../ChangeSlippageButton';
4
+
5
+ interface PropTypes {
6
+ selectedSlippage: number;
7
+ }
8
+
9
+ export function HighSlippageWarning(props: PropTypes) {
10
+ const { selectedSlippage } = props;
11
+
12
+ return (
13
+ <Alert type="warning" footer={<ChangeSlippageButton />}>
14
+ Caution, your slippage is high (=
15
+ {selectedSlippage}). Your trade may be front run.
16
+ </Alert>
17
+ );
18
+ }
@@ -0,0 +1,18 @@
1
+ import { Typography } from '@rango-dev/ui';
2
+ import React from 'react';
3
+ import { ChangeSlippageButton } from '../ChangeSlippageButton';
4
+
5
+ interface PropTypes {
6
+ minRequiredSlippage: number | null;
7
+ }
8
+
9
+ export function MinRequiredSlippage({ minRequiredSlippage }: PropTypes) {
10
+ return (
11
+ <Typography variant="body2">
12
+ We recommend you to increase slippage to at least &nbsp;
13
+ {minRequiredSlippage}
14
+ &nbsp; for this route.
15
+ <ChangeSlippageButton />
16
+ </Typography>
17
+ );
18
+ }
@@ -0,0 +1,3 @@
1
+ export const errorMessages = {
2
+ genericServerError: 'Error connecting server, please reload the app and try again',
3
+ };
@@ -0,0 +1,14 @@
1
+ export const navigationRoutes = {
2
+ home: '/',
3
+ fromChain: '/from-chain',
4
+ fromToken: '/from-token',
5
+ toChain: '/to-chain',
6
+ toToken: '/to-token',
7
+ settings: '/settings',
8
+ liquiditySources: '/settings/liquidity-sources',
9
+ swaps: '/swaps',
10
+ wallets: '/wallets',
11
+ confirmSwap: '/confirm-swap',
12
+ confirmWallets: '/confirm-wallets',
13
+ swapDetails: '/swaps/:requestId',
14
+ };
@@ -0,0 +1,3 @@
1
+ import BigNumber from 'bignumber.js';
2
+
3
+ export const ZERO = new BigNumber(0);
@@ -0,0 +1,7 @@
1
+ export enum SearchParams {
2
+ 'FROM_CHAIN' = 'fromChain',
3
+ 'FROM_TOKEN' = 'fromToken',
4
+ 'TO_CHAIN' = 'toChain',
5
+ 'TO_TOKEN' = 'toToken',
6
+ 'FROM_AMOUNT' = 'fromAmount',
7
+ }
@@ -0,0 +1,9 @@
1
+ export const SLIPPAGES = [0.5, 1, 3, 5, 8, 13, 20];
2
+
3
+ export const DEFAULT_SLIPPAGE = 1;
4
+
5
+ export const HIGH_SLIPPAGE = 5;
6
+
7
+ export const MAX_SLIPPAGE = 100;
8
+
9
+ export const MIN_SLIPPGAE = 0;
@@ -0,0 +1,26 @@
1
+ import { globalCss } from '@rango-dev/ui';
2
+
3
+ export const globalStyles = globalCss({
4
+ '*': {
5
+ '&::-webkit-scrollbar': { width: '$8' },
6
+ '&::-webkit-scrollbar-thumb': {
7
+ backgroundColor: '$neutrals400',
8
+ },
9
+ '&::-webkit-scrollbar-thumb:hover': {
10
+ backgroundColor: '$neutrals500',
11
+ },
12
+ '&::-webkit-scrollbar-track': {
13
+ backgroundColor: '$neutrals300',
14
+ },
15
+ },
16
+ });
17
+ export const globalFont = (fontFamily: string) =>
18
+ globalCss({
19
+ '@import': [
20
+ "url('https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap')",
21
+ "url('https://fonts.cdnfonts.com/css/times-new-roman')",
22
+ ],
23
+ body: {
24
+ fontFamily,
25
+ },
26
+ })();
@@ -0,0 +1,260 @@
1
+ import { useEffect, useRef, useState } from 'react';
2
+ import { useBestRouteStore } from '../store/bestRoute';
3
+ import { useMetaStore } from '../store/meta';
4
+ import { useSettingsStore } from '../store/settings';
5
+ import { useWalletsStore } from '../store/wallets';
6
+ import {
7
+ ConfirmSwapError,
8
+ ConfirmSwapErrorTypes,
9
+ ConfirmSwapWarningTypes,
10
+ ConfirmSwapWarnings,
11
+ PendingSwapSettings,
12
+ } from '../types';
13
+ import { PendingSwap } from '@rango-dev/ui/dist/containers/History/types';
14
+ import {
15
+ calculatePendingSwap,
16
+ createBestRouteRequestBody,
17
+ getBalanceWarnings,
18
+ getMinRequiredSlippage,
19
+ getOutputRatio,
20
+ getPercentageChange,
21
+ getRouteOutputAmount,
22
+ getWalletsForNewSwap,
23
+ hasProperSlippage,
24
+ isOutputAmountChangedALot,
25
+ outputRatioHasWarning,
26
+ } from '../utils/swap';
27
+ import { httpService } from '../services/httpService';
28
+ import BigNumber from 'bignumber.js';
29
+ import {
30
+ isNumberOfSwapsChanged,
31
+ isRouteChanged,
32
+ isRouteInternalCoinsUpdated,
33
+ isRouteSwappersUpdated,
34
+ } from '../utils/routing';
35
+ import { numberToString } from '../utils/numbers';
36
+ import { BestRouteResponse } from 'rango-sdk';
37
+
38
+ type ConfirmSwap = {
39
+ loading: boolean;
40
+ errors: ConfirmSwapError[];
41
+ warnings: ConfirmSwapWarnings[];
42
+ confirmSwap: (() => Promise<PendingSwap | undefined>) | null;
43
+ };
44
+
45
+ export function useConfirmSwap(): ConfirmSwap {
46
+ const fromToken = useBestRouteStore.use.fromToken();
47
+ const toToken = useBestRouteStore.use.toToken();
48
+ const inputAmount = useBestRouteStore.use.inputAmount();
49
+ const initialRoute = useBestRouteStore.use.bestRoute();
50
+ const setBestRoute = useBestRouteStore.use.setBestRoute();
51
+ const inputUsdValue = useBestRouteStore.use.inputUsdValue();
52
+ const accounts = useWalletsStore.use.accounts();
53
+ const selectedWallets = useWalletsStore.use.selectedWallets();
54
+ const meta = useMetaStore.use.meta();
55
+ const customSlippage = useSettingsStore.use.customSlippage();
56
+ const slippage = useSettingsStore.use.slippage();
57
+ const disabledLiquiditySources =
58
+ useSettingsStore.use.disabledLiquiditySources();
59
+ const userSlippage = customSlippage || slippage;
60
+ const proceedAnywayRef = useRef(false);
61
+ const confiremedRouteRef = useRef<BestRouteResponse | null>(null);
62
+ let abortControllerRef = useRef<AbortController | null>(null);
63
+
64
+ const [loading, setLoading] = useState(false);
65
+ const [errors, setErrors] = useState<ConfirmSwapError[]>([]);
66
+ const [warnings, setWarnings] = useState<ConfirmSwapWarnings[]>([]);
67
+
68
+ useEffect(() => {
69
+ return () => abortControllerRef.current?.abort();
70
+ }, []);
71
+
72
+ if (!fromToken || !toToken || !inputAmount || !initialRoute)
73
+ return { loading: false, warnings: [], errors: [], confirmSwap: null };
74
+
75
+ const confirmSwap = async (): Promise<PendingSwap | undefined> => {
76
+ if (proceedAnywayRef.current) {
77
+ const swapSettings: PendingSwapSettings = {
78
+ slippage: userSlippage.toString(),
79
+ disabledSwappersGroups: disabledLiquiditySources,
80
+ };
81
+
82
+ if (errors.length > 0) setErrors([]);
83
+ if (warnings.length > 0) setWarnings([]);
84
+ proceedAnywayRef.current = false;
85
+
86
+ if (confiremedRouteRef.current) {
87
+ const newSwap = calculatePendingSwap(
88
+ inputAmount.toString(),
89
+ confiremedRouteRef.current,
90
+ getWalletsForNewSwap(selectedWallets),
91
+ swapSettings,
92
+ false,
93
+ meta
94
+ );
95
+
96
+ return newSwap;
97
+ }
98
+ return;
99
+ }
100
+
101
+ abortControllerRef.current = new AbortController();
102
+
103
+ setLoading(true);
104
+
105
+ const requestBody = createBestRouteRequestBody(
106
+ fromToken,
107
+ toToken,
108
+ inputAmount,
109
+ accounts,
110
+ selectedWallets,
111
+ disabledLiquiditySources,
112
+ userSlippage,
113
+ true
114
+ );
115
+
116
+ try {
117
+ const confiremedRoute = await httpService.getBestRoute(requestBody, {
118
+ signal: abortControllerRef.current?.signal,
119
+ });
120
+
121
+ abortControllerRef.current = null;
122
+
123
+ setLoading(false);
124
+
125
+ if (
126
+ !confiremedRoute.result ||
127
+ !new BigNumber(confiremedRoute.requestAmount).isEqualTo(
128
+ new BigNumber(inputAmount || '-1')
129
+ )
130
+ ) {
131
+ setErrors((prevState) =>
132
+ prevState.concat({
133
+ type: ConfirmSwapErrorTypes.NO_ROUTE,
134
+ })
135
+ );
136
+ return;
137
+ }
138
+
139
+ const confirmSwapState: Omit<ConfirmSwap, 'confirmSwap' | 'loading'> = {
140
+ errors: [],
141
+ warnings: [],
142
+ };
143
+
144
+ const routeChanged = isRouteChanged(initialRoute, confiremedRoute!);
145
+
146
+ if (routeChanged) {
147
+ setBestRoute(confiremedRoute);
148
+ const newRouteOutputUsdValue = new BigNumber(
149
+ confiremedRoute.result?.outputAmount || '0'
150
+ ).multipliedBy(toToken.usdPrice || 0);
151
+
152
+ const outputRatio = getOutputRatio(
153
+ inputUsdValue,
154
+ newRouteOutputUsdValue
155
+ );
156
+ const highValueLoss = outputRatioHasWarning(inputUsdValue, outputRatio);
157
+
158
+ if (highValueLoss)
159
+ confirmSwapState.errors.push({
160
+ type: ConfirmSwapErrorTypes.ROUTE_UPDATED_WITH_HIGH_VALUE_LOSS,
161
+ });
162
+ else if (isOutputAmountChangedALot(initialRoute, confiremedRoute))
163
+ confirmSwapState.warnings.push({
164
+ type: ConfirmSwapWarningTypes.ROUTE_AND_OUTPUT_AMOUNT_UPDATED,
165
+ newOutputAmount: numberToString(
166
+ getRouteOutputAmount(confiremedRoute)
167
+ ),
168
+ percentageChange: numberToString(
169
+ getPercentageChange(
170
+ getRouteOutputAmount(initialRoute),
171
+ getRouteOutputAmount(confiremedRoute)
172
+ ),
173
+ null,
174
+ 2
175
+ ),
176
+ });
177
+ else if (isRouteInternalCoinsUpdated(initialRoute, confiremedRoute))
178
+ confirmSwapState.warnings.push({
179
+ type: ConfirmSwapWarningTypes.ROUTE_COINS_UPDATED,
180
+ });
181
+ else if (isNumberOfSwapsChanged(initialRoute, confiremedRoute))
182
+ confirmSwapState.warnings.push({
183
+ type: ConfirmSwapWarningTypes.ROUTE_UPDATED,
184
+ });
185
+ else if (isRouteSwappersUpdated(initialRoute, confiremedRoute))
186
+ confirmSwapState.warnings.push({
187
+ type: ConfirmSwapWarningTypes.ROUTE_SWAPPERS_UPDATED,
188
+ });
189
+ }
190
+
191
+ const balanceWarnings = getBalanceWarnings(
192
+ confiremedRoute,
193
+ selectedWallets
194
+ );
195
+ const enoughBalance = balanceWarnings.length === 0;
196
+
197
+ if (!enoughBalance)
198
+ confirmSwapState.warnings.push({
199
+ type: ConfirmSwapWarningTypes.INSUFFICIENT_BALANCE,
200
+ messages: balanceWarnings,
201
+ });
202
+
203
+ const minRequiredSlippage = getMinRequiredSlippage(initialRoute);
204
+
205
+ if (!hasProperSlippage(userSlippage.toString(), minRequiredSlippage))
206
+ confirmSwapState.errors.push({
207
+ type: ConfirmSwapErrorTypes.INSUFFICIENT_SLIPPAGE,
208
+ minRequiredSlippage,
209
+ });
210
+
211
+ const noErrors = confirmSwapState.errors.length === 0;
212
+ const noWarnings = confirmSwapState.warnings.length === 0;
213
+
214
+ if (
215
+ (noErrors || (!noErrors && proceedAnywayRef.current)) &&
216
+ (noWarnings || (!noWarnings && proceedAnywayRef.current))
217
+ ) {
218
+ const swapSettings: PendingSwapSettings = {
219
+ slippage: userSlippage.toString(),
220
+ disabledSwappersGroups: disabledLiquiditySources,
221
+ };
222
+
223
+ const newSwap = calculatePendingSwap(
224
+ inputAmount.toString(),
225
+ confiremedRoute,
226
+ getWalletsForNewSwap(selectedWallets),
227
+ swapSettings,
228
+ false,
229
+ meta
230
+ );
231
+
232
+ return newSwap;
233
+ } else if (!proceedAnywayRef.current) {
234
+ proceedAnywayRef.current = true;
235
+ setErrors(confirmSwapState.errors);
236
+ setWarnings(confirmSwapState.warnings);
237
+ confiremedRouteRef.current = confiremedRoute;
238
+ }
239
+ return;
240
+ } catch (error) {
241
+ if ((error as any)?.code === 'ERR_CANCELED') {
242
+ return;
243
+ }
244
+
245
+ const status = (error as any)?.response?.status;
246
+
247
+ setLoading(false);
248
+ setErrors([
249
+ {
250
+ type: ConfirmSwapErrorTypes.REQUEST_FAILED,
251
+ status,
252
+ },
253
+ ]);
254
+
255
+ return;
256
+ }
257
+ };
258
+
259
+ return { loading, warnings, errors, confirmSwap };
260
+ }
@@ -0,0 +1,23 @@
1
+ import React from 'react';
2
+ import copy from 'copy-to-clipboard';
3
+
4
+ export default function useCopyToClipboard(resetInterval?: number) {
5
+ const [isCopied, setCopied] = React.useState(false);
6
+
7
+ const handleCopy = React.useCallback((text: string | number) => {
8
+ copy(text.toString());
9
+ setCopied(true);
10
+ }, []);
11
+
12
+ React.useEffect(() => {
13
+ let timeout: NodeJS.Timeout;
14
+ if (isCopied && resetInterval) {
15
+ timeout = setTimeout(() => setCopied(false), resetInterval);
16
+ }
17
+ return () => {
18
+ clearTimeout(timeout);
19
+ };
20
+ }, [isCopied, resetInterval]);
21
+
22
+ return [isCopied, handleCopy] as const;
23
+ }
@@ -0,0 +1,40 @@
1
+ import { useInRouterContext, useNavigate } from 'react-router-dom';
2
+ import { navigationRoutes } from '../constants/navigationRoutes';
3
+
4
+ export function useNavigateBack() {
5
+ const isRouterInContext = useInRouterContext();
6
+ const navigate = useNavigate();
7
+
8
+ navigationRoutes;
9
+
10
+ const navigateBackFrom = (currentRoute: string) => {
11
+ if (
12
+ !isRouterInContext ||
13
+ (window.history.state && window.history.state.idx > 0)
14
+ )
15
+ navigate(-1);
16
+ else {
17
+ if (
18
+ [
19
+ navigationRoutes.fromChain,
20
+ navigationRoutes.fromToken,
21
+ navigationRoutes.toChain,
22
+ navigationRoutes.toToken,
23
+ navigationRoutes.settings,
24
+ navigationRoutes.wallets,
25
+ navigationRoutes.swaps,
26
+ navigationRoutes.confirmWallets,
27
+ ].includes(currentRoute)
28
+ )
29
+ navigate(navigationRoutes.home, { replace: true });
30
+ else if (currentRoute === navigationRoutes.liquiditySources)
31
+ navigate(navigationRoutes.settings, { replace: true });
32
+ else if (currentRoute === navigationRoutes.swapDetails)
33
+ navigate(navigationRoutes.swaps, { replace: true });
34
+ else if (currentRoute === navigationRoutes.confirmSwap)
35
+ navigate(navigationRoutes.confirmWallets, { replace: true });
36
+ }
37
+ };
38
+
39
+ return { navigateBackFrom };
40
+ }
@@ -0,0 +1,13 @@
1
+ import { useEffect, useRef } from 'react';
2
+
3
+ export default function usePrevious(value: string) {
4
+ // The ref object is a generic container whose current property is mutable ...
5
+ // ... and can hold any value, similar to an instance property on a class
6
+ const ref = useRef<string>();
7
+ // Store current value in ref
8
+ useEffect(() => {
9
+ ref.current = value;
10
+ }, [value]); // Only re-run if value changes
11
+ // Return previous value (happens before update in useEffect above)
12
+ return ref.current;
13
+ }
@@ -0,0 +1,15 @@
1
+ import { useTranslation } from 'react-i18next';
2
+
3
+ export default function useSelectLanguage() {
4
+ const { i18n } = useTranslation();
5
+ // @ts-ignore
6
+ const currentLanguage = i18n?.language || 'en';
7
+ const changeLanguage = (value: string) => {
8
+ if (currentLanguage !== value) {
9
+ // @ts-ignore
10
+ i18n.changeLanguage(value);
11
+ }
12
+ };
13
+
14
+ return { changeLanguage, currentLanguage };
15
+ }