@rango-dev/widget-embedded 0.1.10-next.92 → 0.1.10-next.94
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.
- package/dist/QueueManager.d.ts.map +1 -1
- package/dist/components/AppRoutes.d.ts +5 -1
- package/dist/components/AppRoutes.d.ts.map +1 -1
- package/dist/components/ConfirmSwapErrors.d.ts +3 -2
- package/dist/components/ConfirmSwapErrors.d.ts.map +1 -1
- package/dist/components/ConfirmSwapWarnings.d.ts +3 -2
- package/dist/components/ConfirmSwapWarnings.d.ts.map +1 -1
- package/dist/components/Layout.d.ts +1 -1
- package/dist/components/Layout.d.ts.map +1 -1
- package/dist/components/SwitchFromAndTo.d.ts +1 -1
- package/dist/components/SwitchFromAndTo.d.ts.map +1 -1
- package/dist/components/UpdateUrl.d.ts +1 -1
- package/dist/components/UpdateUrl.d.ts.map +1 -1
- package/dist/components/warnings/BalanceWarnings.d.ts.map +1 -1
- package/dist/components/warnings/ConfirmSwapExtraMessages.d.ts.map +1 -1
- package/dist/hooks/useConfirmSwap.d.ts +11 -0
- package/dist/hooks/useConfirmSwap.d.ts.map +1 -0
- package/dist/hooks/usePrevious.d.ts +1 -1
- package/dist/hooks/usePrevious.d.ts.map +1 -1
- package/dist/pages/ConfirmSwapPage.d.ts.map +1 -1
- package/dist/pages/Home.d.ts.map +1 -1
- package/dist/pages/SelectChainPage.d.ts.map +1 -1
- package/dist/pages/SelectTokenPage.d.ts.map +1 -1
- package/dist/pages/SettingsPage.d.ts.map +1 -1
- package/dist/pages/WalletsPage.d.ts.map +1 -1
- package/dist/store/bestRoute.d.ts +25 -0
- package/dist/store/bestRoute.d.ts.map +1 -1
- package/dist/store/settings.d.ts +15 -0
- package/dist/store/settings.d.ts.map +1 -1
- package/dist/store/wallets.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/{store/confirmSwap.d.ts → types/routing.d.ts} +14 -5
- package/dist/types/routing.d.ts.map +1 -0
- package/dist/utils/numbers.d.ts +1 -1
- package/dist/utils/numbers.d.ts.map +1 -1
- package/dist/utils/routing.d.ts +11 -5
- package/dist/utils/routing.d.ts.map +1 -1
- package/dist/utils/swap.d.ts +4 -3
- package/dist/utils/swap.d.ts.map +1 -1
- package/dist/utils/wallets.d.ts +7 -2
- package/dist/utils/wallets.d.ts.map +1 -1
- package/dist/widget-embedded.cjs.development.js +581 -492
- package/dist/widget-embedded.cjs.development.js.map +1 -1
- package/dist/widget-embedded.cjs.production.min.js +581 -492
- package/dist/widget-embedded.cjs.production.min.js.map +1 -1
- package/dist/widget-embedded.esm.js +582 -493
- package/dist/widget-embedded.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/QueueManager.tsx +1 -0
- package/src/components/AppRoutes.tsx +54 -21
- package/src/components/ChangeSlippageButton.tsx +1 -1
- package/src/components/ConfirmSwapErrors.tsx +1 -1
- package/src/components/ConfirmSwapWarnings.tsx +1 -4
- package/src/components/Header.tsx +1 -1
- package/src/components/Layout.tsx +10 -11
- package/src/components/UpdateUrl.tsx +13 -22
- package/src/components/warnings/BalanceWarnings.tsx +27 -4
- package/src/components/warnings/ConfirmSwapExtraMessages.tsx +7 -1
- package/src/components/warnings/HighSlippageWarning.tsx +1 -1
- package/src/components/warnings/MinRequiredSlippage.tsx +2 -2
- package/src/hooks/useConfirmSwap.ts +260 -0
- package/src/pages/ConfirmSwapPage.tsx +17 -8
- package/src/pages/ConfirmWalletsPage.tsx +1 -1
- package/src/pages/Home.tsx +0 -1
- package/src/pages/SelectChainPage.tsx +2 -7
- package/src/pages/SelectTokenPage.tsx +22 -57
- package/src/pages/SettingsPage.tsx +8 -9
- package/src/pages/WalletsPage.tsx +16 -6
- package/src/store/bestRoute.ts +82 -37
- package/src/store/settings.ts +2 -2
- package/src/store/wallets.ts +26 -0
- package/src/types/index.ts +1 -0
- package/src/types/routing.ts +60 -0
- package/src/utils/numbers.ts +38 -17
- package/src/utils/routing.ts +115 -24
- package/src/utils/swap.ts +19 -11
- package/src/utils/wallets.ts +117 -73
- package/dist/store/confirmSwap.d.ts.map +0 -1
- package/src/store/confirmSwap.ts +0 -342
package/package.json
CHANGED
package/src/QueueManager.tsx
CHANGED
|
@@ -13,48 +13,73 @@ import { ConfirmWalletsPage } from '../pages/ConfirmWalletsPage';
|
|
|
13
13
|
import { SwapDetailsPage } from '../pages/SwapDetailsPage';
|
|
14
14
|
import { Configs } from '../types';
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
multiWallets = true,
|
|
25
|
-
titleSize,
|
|
26
|
-
titleWeight,
|
|
27
|
-
}: Configs) =>
|
|
28
|
-
useRoutes([
|
|
16
|
+
interface PropTypes {
|
|
17
|
+
configs?: Configs;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function AppRoutes(props: PropTypes) {
|
|
21
|
+
const { configs } = props;
|
|
22
|
+
|
|
23
|
+
return useRoutes([
|
|
29
24
|
{
|
|
30
25
|
path: navigationRoutes.home,
|
|
31
26
|
element: (
|
|
32
|
-
<Home
|
|
27
|
+
<Home
|
|
28
|
+
title={configs?.title}
|
|
29
|
+
titleSize={configs?.titleSize}
|
|
30
|
+
titleWeight={configs?.titleWeight}
|
|
31
|
+
/>
|
|
33
32
|
),
|
|
34
33
|
},
|
|
35
34
|
{
|
|
36
35
|
path: navigationRoutes.fromChain,
|
|
37
|
-
element:
|
|
36
|
+
element: (
|
|
37
|
+
<SelectChainPage
|
|
38
|
+
type="from"
|
|
39
|
+
supportedChains={configs?.fromChains || 'all'}
|
|
40
|
+
/>
|
|
41
|
+
),
|
|
38
42
|
},
|
|
39
43
|
{
|
|
40
44
|
path: navigationRoutes.toChain,
|
|
41
|
-
element:
|
|
45
|
+
element: (
|
|
46
|
+
<SelectChainPage
|
|
47
|
+
type="to"
|
|
48
|
+
supportedChains={configs?.toChains || 'all'}
|
|
49
|
+
/>
|
|
50
|
+
),
|
|
42
51
|
},
|
|
43
52
|
{
|
|
44
53
|
path: navigationRoutes.fromToken + '/*',
|
|
45
|
-
element:
|
|
54
|
+
element: (
|
|
55
|
+
<SelectTokenPage
|
|
56
|
+
type="from"
|
|
57
|
+
supportedTokens={configs?.fromTokens || 'all'}
|
|
58
|
+
/>
|
|
59
|
+
),
|
|
46
60
|
},
|
|
47
61
|
{
|
|
48
62
|
path: navigationRoutes.toToken,
|
|
49
|
-
element:
|
|
63
|
+
element: (
|
|
64
|
+
<SelectTokenPage
|
|
65
|
+
type="to"
|
|
66
|
+
supportedTokens={configs?.toTokens || 'all'}
|
|
67
|
+
/>
|
|
68
|
+
),
|
|
50
69
|
},
|
|
51
70
|
{
|
|
52
71
|
path: navigationRoutes.settings,
|
|
53
|
-
element:
|
|
72
|
+
element: (
|
|
73
|
+
<SettingsPage supportedSwappers={configs?.liquiditySources || 'all'} />
|
|
74
|
+
),
|
|
54
75
|
},
|
|
55
76
|
{
|
|
56
77
|
path: navigationRoutes.liquiditySources,
|
|
57
|
-
element:
|
|
78
|
+
element: (
|
|
79
|
+
<LiquiditySourcePage
|
|
80
|
+
supportedSwappers={configs?.liquiditySources || 'all'}
|
|
81
|
+
/>
|
|
82
|
+
),
|
|
58
83
|
},
|
|
59
84
|
{ path: navigationRoutes.swaps, element: <HistoryPage /> },
|
|
60
85
|
{
|
|
@@ -64,9 +89,17 @@ export const AppRoutes = ({
|
|
|
64
89
|
{
|
|
65
90
|
path: navigationRoutes.wallets,
|
|
66
91
|
element: (
|
|
67
|
-
<WalletsPage
|
|
92
|
+
<WalletsPage
|
|
93
|
+
supportedWallets={configs?.wallets || 'all'}
|
|
94
|
+
multiWallets={
|
|
95
|
+
typeof configs?.multiWallets === 'undefined'
|
|
96
|
+
? true
|
|
97
|
+
: configs.multiWallets
|
|
98
|
+
}
|
|
99
|
+
/>
|
|
68
100
|
),
|
|
69
101
|
},
|
|
70
102
|
{ path: navigationRoutes.confirmSwap, element: <ConfirmSwapPage /> },
|
|
71
103
|
{ path: navigationRoutes.confirmWallets, element: <ConfirmWalletsPage /> },
|
|
72
104
|
]);
|
|
105
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { Typography } from '@rango-dev/ui';
|
|
3
|
-
import { ConfirmSwapError, ConfirmSwapErrorTypes } from '../
|
|
3
|
+
import { ConfirmSwapError, ConfirmSwapErrorTypes } from '../types';
|
|
4
4
|
import { MinRequiredSlippage } from './warnings/MinRequiredSlippage';
|
|
5
5
|
|
|
6
6
|
export function ConfirmSwapErrors(errors: ConfirmSwapError[]) {
|
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { Typography } from '@rango-dev/ui';
|
|
3
|
-
import {
|
|
4
|
-
ConfirmSwapWarnings,
|
|
5
|
-
ConfirmSwapWarningTypes,
|
|
6
|
-
} from '../store/confirmSwap';
|
|
3
|
+
import { ConfirmSwapWarningTypes, ConfirmSwapWarnings } from '../types';
|
|
7
4
|
import { BalanceWarnings } from './warnings/BalanceWarnings';
|
|
8
5
|
|
|
9
6
|
export function ConfirmSwapWarnings(warnings: ConfirmSwapWarnings[]) {
|
|
@@ -29,7 +29,7 @@ export function Header(props: PropTypes) {
|
|
|
29
29
|
return (
|
|
30
30
|
<HeaderContainer>
|
|
31
31
|
<Typography variant="h4" className={titleStyle()}>
|
|
32
|
-
{t(title.toLocaleLowerCase())
|
|
32
|
+
{title ? t(title.toLocaleLowerCase()) : ''}
|
|
33
33
|
</Typography>
|
|
34
34
|
<HeaderButtons onClickRefresh={onClickRefresh} />
|
|
35
35
|
</HeaderContainer>
|
|
@@ -30,7 +30,7 @@ const WalletImage = styled('img', {
|
|
|
30
30
|
});
|
|
31
31
|
|
|
32
32
|
export type LayoutProps = {
|
|
33
|
-
configs
|
|
33
|
+
configs?: Configs;
|
|
34
34
|
};
|
|
35
35
|
|
|
36
36
|
export function Layout({ configs }: LayoutProps) {
|
|
@@ -43,7 +43,6 @@ export function Layout({ configs }: LayoutProps) {
|
|
|
43
43
|
)
|
|
44
44
|
);
|
|
45
45
|
|
|
46
|
-
const { fromChain, toChain, toToken, fromToken, fromAmount } = configs;
|
|
47
46
|
const setFromChain = useBestRouteStore.use.setFromChain();
|
|
48
47
|
const setFromToken = useBestRouteStore.use.setFromToken();
|
|
49
48
|
const setToChain = useBestRouteStore.use.setToChain();
|
|
@@ -55,17 +54,17 @@ export function Layout({ configs }: LayoutProps) {
|
|
|
55
54
|
useUiStore.use.connectWalletsButtonDisabled();
|
|
56
55
|
const { t } = useTranslation();
|
|
57
56
|
useEffect(() => {
|
|
58
|
-
setToChain(toChain);
|
|
59
|
-
setToToken(toToken);
|
|
60
|
-
}, [toChain, toToken]);
|
|
57
|
+
setToChain(configs?.toChain || null);
|
|
58
|
+
setToToken(configs?.toToken || null);
|
|
59
|
+
}, [configs?.toChain, configs?.toToken]);
|
|
61
60
|
useEffect(() => {
|
|
62
|
-
setInputAmount(fromAmount
|
|
63
|
-
}, [fromAmount]);
|
|
61
|
+
setInputAmount(configs?.fromAmount?.toString() || '');
|
|
62
|
+
}, [configs?.fromAmount]);
|
|
64
63
|
|
|
65
64
|
useEffect(() => {
|
|
66
|
-
setFromToken(fromToken);
|
|
67
|
-
setFromChain(fromChain);
|
|
68
|
-
}, [fromToken, fromChain]);
|
|
65
|
+
setFromToken(configs?.fromToken || null);
|
|
66
|
+
setFromChain(configs?.fromChain || null);
|
|
67
|
+
}, [configs?.fromToken, configs?.fromChain]);
|
|
69
68
|
return (
|
|
70
69
|
<>
|
|
71
70
|
<Header>
|
|
@@ -91,7 +90,7 @@ export function Layout({ configs }: LayoutProps) {
|
|
|
91
90
|
</Typography>
|
|
92
91
|
</Button>
|
|
93
92
|
</Header>
|
|
94
|
-
<AppRoutes {
|
|
93
|
+
<AppRoutes configs={configs} />
|
|
95
94
|
</>
|
|
96
95
|
);
|
|
97
96
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Token } from 'rango-sdk';
|
|
2
1
|
import { useEffect, useRef } from 'react';
|
|
3
2
|
import {
|
|
4
3
|
createSearchParams,
|
|
@@ -10,23 +9,7 @@ import { useMetaStore } from '../store/meta';
|
|
|
10
9
|
import { SearchParams } from '../constants/searchParams';
|
|
11
10
|
import { navigationRoutes } from '../constants/navigationRoutes';
|
|
12
11
|
import { useUiStore } from '../store/ui';
|
|
13
|
-
|
|
14
|
-
function searchParamsToToken(
|
|
15
|
-
tokens: Token[],
|
|
16
|
-
searchParams: string | null
|
|
17
|
-
): Token | null {
|
|
18
|
-
return (
|
|
19
|
-
tokens.find((token) => {
|
|
20
|
-
const symbolAndAddress = searchParams?.split('--');
|
|
21
|
-
if (symbolAndAddress?.length === 1)
|
|
22
|
-
return token.symbol === symbolAndAddress[0] && token.address === null;
|
|
23
|
-
return (
|
|
24
|
-
token.symbol === symbolAndAddress?.[0] &&
|
|
25
|
-
token.address === symbolAndAddress?.[1]
|
|
26
|
-
);
|
|
27
|
-
}) || null
|
|
28
|
-
);
|
|
29
|
-
}
|
|
12
|
+
import { searchParamsToToken } from '../utils/routing';
|
|
30
13
|
|
|
31
14
|
export function UpdateUrl() {
|
|
32
15
|
const firstRender = useRef(true);
|
|
@@ -142,19 +125,27 @@ export function UpdateUrl() {
|
|
|
142
125
|
|
|
143
126
|
useEffect(() => {
|
|
144
127
|
if (loadingStatus === 'success') {
|
|
145
|
-
const
|
|
128
|
+
const fromChainString = searchParams.get(SearchParams.FROM_CHAIN);
|
|
146
129
|
const fromTokenString = searchParams.get(SearchParams.FROM_TOKEN);
|
|
147
130
|
const toChainString = searchParams.get(SearchParams.TO_CHAIN);
|
|
148
131
|
const toTokenString = searchParams.get(SearchParams.TO_TOKEN);
|
|
149
132
|
const fromAmount = searchParams.get(SearchParams.FROM_AMOUNT);
|
|
150
133
|
const fromChain = blockchains.find(
|
|
151
|
-
(blockchain) => blockchain.name ===
|
|
134
|
+
(blockchain) => blockchain.name === fromChainString
|
|
135
|
+
);
|
|
136
|
+
const fromToken = searchParamsToToken(
|
|
137
|
+
tokens,
|
|
138
|
+
fromTokenString,
|
|
139
|
+
fromChain || null
|
|
152
140
|
);
|
|
153
|
-
const fromToken = searchParamsToToken(tokens, fromTokenString);
|
|
154
141
|
const toChain = blockchains.find(
|
|
155
142
|
(blockchain) => blockchain.name === toChainString
|
|
156
143
|
);
|
|
157
|
-
const toToken = searchParamsToToken(
|
|
144
|
+
const toToken = searchParamsToToken(
|
|
145
|
+
tokens,
|
|
146
|
+
toTokenString,
|
|
147
|
+
toChain || null
|
|
148
|
+
);
|
|
158
149
|
if (!!fromChain) {
|
|
159
150
|
setFromChain(fromChain);
|
|
160
151
|
if (!!fromToken) setFromToken(fromToken);
|
|
@@ -1,16 +1,39 @@
|
|
|
1
|
-
import { Typography } from '@rango-dev/ui';
|
|
1
|
+
import { styled, Typography } from '@rango-dev/ui';
|
|
2
2
|
import React from 'react';
|
|
3
3
|
|
|
4
4
|
interface PropTypes {
|
|
5
5
|
messages: string[];
|
|
6
6
|
}
|
|
7
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
|
+
|
|
8
28
|
export function BalanceWarnings({ messages }: PropTypes) {
|
|
29
|
+
const showListStyle = messages.length > 1;
|
|
9
30
|
return (
|
|
10
|
-
<
|
|
31
|
+
<List showListStyle={showListStyle}>
|
|
11
32
|
{messages.map((warning) => (
|
|
12
|
-
<
|
|
33
|
+
<ListItem showListStyle={showListStyle}>
|
|
34
|
+
<Message variant="body2">{warning}</Message>
|
|
35
|
+
</ListItem>
|
|
13
36
|
))}
|
|
14
|
-
</
|
|
37
|
+
</List>
|
|
15
38
|
);
|
|
16
39
|
}
|
|
@@ -10,5 +10,11 @@ export function ConfirmSwapExtraMessages(props: PropTypes) {
|
|
|
10
10
|
const { selectedSlippage } = props;
|
|
11
11
|
const highSlippage = selectedSlippage >= HIGH_SLIPPAGE;
|
|
12
12
|
|
|
13
|
-
return
|
|
13
|
+
return (
|
|
14
|
+
<>
|
|
15
|
+
{highSlippage && (
|
|
16
|
+
<HighSlippageWarning selectedSlippage={selectedSlippage} />
|
|
17
|
+
)}
|
|
18
|
+
</>
|
|
19
|
+
);
|
|
14
20
|
}
|
|
@@ -12,7 +12,7 @@ export function HighSlippageWarning(props: PropTypes) {
|
|
|
12
12
|
return (
|
|
13
13
|
<Alert type="warning">
|
|
14
14
|
<Typography variant="body2">
|
|
15
|
-
|
|
15
|
+
Caution, your slippage is high (=
|
|
16
16
|
{selectedSlippage}). Your trade may be front run.
|
|
17
17
|
<ChangeSlippageButton />
|
|
18
18
|
</Typography>
|
|
@@ -9,9 +9,9 @@ interface PropTypes {
|
|
|
9
9
|
export function MinRequiredSlippage({ minRequiredSlippage }: PropTypes) {
|
|
10
10
|
return (
|
|
11
11
|
<Typography variant="body2">
|
|
12
|
-
|
|
12
|
+
We recommend you to increase slippage to at least
|
|
13
13
|
{minRequiredSlippage}
|
|
14
|
-
for this route.
|
|
14
|
+
for this route.
|
|
15
15
|
<ChangeSlippageButton />
|
|
16
16
|
</Typography>
|
|
17
17
|
);
|
|
@@ -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
|
+
}
|