@rango-dev/widget-embedded 0.1.10-next.100 → 0.1.10-next.106
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/App.d.ts.map +1 -1
- package/dist/QueueManager.d.ts.map +1 -1
- package/dist/components/AppRouter.d.ts +6 -1
- package/dist/components/AppRouter.d.ts.map +1 -1
- package/dist/components/AppRoutes.d.ts.map +1 -1
- package/dist/components/Layout.d.ts.map +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/MinRequiredSlippage.d.ts +1 -1
- package/dist/components/warnings/MinRequiredSlippage.d.ts.map +1 -1
- package/dist/constants/navigationRoutes.d.ts +0 -1
- package/dist/constants/navigationRoutes.d.ts.map +1 -1
- package/dist/hooks/useConfirmSwap.d.ts.map +1 -1
- package/dist/lib.d.ts.map +1 -1
- package/dist/pages/ConfirmSwapPage.d.ts.map +1 -1
- package/dist/pages/SelectTokenPage.d.ts.map +1 -1
- package/dist/pages/SwapDetailsPage.d.ts +1 -1
- package/dist/pages/SwapDetailsPage.d.ts.map +1 -1
- package/dist/store/bestRoute.d.ts +3 -1
- package/dist/store/bestRoute.d.ts.map +1 -1
- package/dist/types/routing.d.ts +1 -1
- package/dist/types/swap.d.ts +6 -0
- package/dist/types/swap.d.ts.map +1 -1
- package/dist/utils/common.d.ts +1 -0
- package/dist/utils/common.d.ts.map +1 -1
- package/dist/utils/swap.d.ts +16 -5
- package/dist/utils/swap.d.ts.map +1 -1
- package/dist/utils/time.d.ts +4 -0
- package/dist/utils/time.d.ts.map +1 -0
- package/dist/utils/wallets.d.ts +1 -1
- package/dist/utils/wallets.d.ts.map +1 -1
- package/dist/widget-embedded.cjs.development.js +796 -600
- package/dist/widget-embedded.cjs.development.js.map +1 -1
- package/dist/widget-embedded.cjs.production.min.js +796 -600
- package/dist/widget-embedded.cjs.production.min.js.map +1 -1
- package/dist/widget-embedded.esm.js +805 -609
- package/dist/widget-embedded.esm.js.map +1 -1
- package/package.json +9 -9
- package/src/App.tsx +26 -4
- package/src/QueueManager.tsx +5 -2
- package/src/components/AppRouter.tsx +24 -13
- package/src/components/AppRoutes.tsx +0 -2
- package/src/components/Layout.tsx +13 -0
- package/src/components/UpdateUrl.tsx +1 -3
- package/src/components/warnings/BalanceWarnings.tsx +1 -0
- package/src/components/warnings/MinRequiredSlippage.tsx +9 -4
- package/src/constants/navigationRoutes.ts +0 -1
- package/src/hooks/useConfirmSwap.ts +2 -1
- package/src/hooks/useNavigateBack.ts +3 -3
- package/src/lib.tsx +25 -3
- package/src/pages/ConfirmSwapPage.tsx +121 -20
- package/src/pages/Home.tsx +1 -1
- package/src/pages/SelectTokenPage.tsx +0 -1
- package/src/pages/SwapDetailsPage.tsx +82 -5
- package/src/store/bestRoute.ts +75 -4
- package/src/types/routing.ts +1 -1
- package/src/types/swap.ts +7 -0
- package/src/utils/common.ts +14 -0
- package/src/utils/swap.ts +98 -9
- package/src/utils/time.ts +52 -0
- package/src/utils/wallets.ts +5 -2
- package/dist/pages/ConfirmWalletsPage.d.ts +0 -3
- package/dist/pages/ConfirmWalletsPage.d.ts.map +0 -1
- package/src/pages/ConfirmWalletsPage.tsx +0 -127
|
@@ -6,33 +6,110 @@ import { useNavigateBack } from '../hooks/useNavigateBack';
|
|
|
6
6
|
import { getPendingSwaps } from '../utils/queue';
|
|
7
7
|
import { SwapHistory } from '@rango-dev/ui';
|
|
8
8
|
import { useUiStore } from '../store/ui';
|
|
9
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
cancelSwap,
|
|
11
|
+
PendingSwapNetworkStatus,
|
|
12
|
+
} from '@rango-dev/queue-manager-rango-preset';
|
|
13
|
+
import { useWallets } from '@rango-dev/wallets-core';
|
|
14
|
+
import {
|
|
15
|
+
getCurrentBlockchainOfOrNull,
|
|
16
|
+
getCurrentStep,
|
|
17
|
+
getRelatedWalletOrNull,
|
|
18
|
+
} from '@rango-dev/queue-manager-rango-preset';
|
|
19
|
+
import { getSwapDate } from '../utils/time';
|
|
20
|
+
import { useNavigate } from 'react-router-dom';
|
|
21
|
+
import { useBestRouteStore } from '../store/bestRoute';
|
|
22
|
+
import {
|
|
23
|
+
getLastConvertedTokenInFailedSwap,
|
|
24
|
+
getSwapMessages,
|
|
25
|
+
isNetworkStatusInWarningState,
|
|
26
|
+
shouldRetrySwap,
|
|
27
|
+
} from '../utils/swap';
|
|
10
28
|
|
|
11
29
|
export function SwapDetailsPage() {
|
|
12
30
|
const selectedSwapRequestId = useUiStore.use.selectedSwapRequestId();
|
|
31
|
+
const { canSwitchNetworkTo, connect } = useWallets();
|
|
32
|
+
const retry = useBestRouteStore.use.retry();
|
|
33
|
+
const navigate = useNavigate();
|
|
13
34
|
const { navigateBackFrom } = useNavigateBack();
|
|
14
|
-
const { manager } = useManager();
|
|
15
35
|
const [isCopied, handleCopy] = useCopyToClipboard(2000);
|
|
36
|
+
const { manager } = useManager();
|
|
16
37
|
|
|
17
38
|
const pendingSwaps = getPendingSwaps(manager);
|
|
18
39
|
const selectedSwap = pendingSwaps.find(
|
|
19
40
|
({ swap }) => swap.requestId === selectedSwapRequestId
|
|
20
41
|
);
|
|
21
42
|
|
|
22
|
-
//TODO: implement swap cancellation (move queueManager logic to queueManager/rango-preset)
|
|
23
43
|
const onCancel = () => {
|
|
24
|
-
const swap =
|
|
44
|
+
const swap = manager?.get(selectedSwap?.id!);
|
|
25
45
|
if (swap) cancelSwap(swap);
|
|
26
46
|
};
|
|
47
|
+
|
|
48
|
+
const swap = selectedSwap?.swap;
|
|
49
|
+
if (!swap) return null;
|
|
50
|
+
|
|
51
|
+
const currentStep = getCurrentStep(swap);
|
|
52
|
+
|
|
53
|
+
const currentStepBlockchain = currentStep
|
|
54
|
+
? getCurrentBlockchainOfOrNull(swap, currentStep)
|
|
55
|
+
: null;
|
|
56
|
+
const currentStepWallet = currentStep
|
|
57
|
+
? getRelatedWalletOrNull(swap, currentStep)
|
|
58
|
+
: null;
|
|
59
|
+
const currentStepNetworkStatus = currentStep?.networkStatus;
|
|
60
|
+
|
|
61
|
+
const swapMessages = getSwapMessages(swap, currentStep);
|
|
62
|
+
|
|
63
|
+
const networkWarningState = isNetworkStatusInWarningState(currentStep);
|
|
64
|
+
|
|
65
|
+
const swapDate = getSwapDate(swap);
|
|
66
|
+
|
|
67
|
+
const shouldRetry = shouldRetrySwap(swap);
|
|
68
|
+
|
|
69
|
+
const switchNetwork =
|
|
70
|
+
!!currentStepBlockchain &&
|
|
71
|
+
!!currentStepWallet?.walletType &&
|
|
72
|
+
currentStepNetworkStatus ===
|
|
73
|
+
PendingSwapNetworkStatus.WaitingForNetworkChange &&
|
|
74
|
+
canSwitchNetworkTo(currentStepWallet?.walletType, currentStepBlockchain)
|
|
75
|
+
? connect.bind(null, currentStepWallet.walletType, currentStepBlockchain)
|
|
76
|
+
: undefined;
|
|
77
|
+
|
|
78
|
+
const lastConvertedTokenInFailedSwap =
|
|
79
|
+
getLastConvertedTokenInFailedSwap(swap);
|
|
80
|
+
|
|
27
81
|
return (
|
|
28
82
|
<SwapHistory
|
|
29
83
|
onBack={navigateBackFrom.bind(null, navigationRoutes.swapDetails)}
|
|
30
84
|
//todo: move PendingSwap type to rango-types
|
|
31
85
|
//@ts-ignore
|
|
32
|
-
pendingSwap={
|
|
86
|
+
pendingSwap={swap}
|
|
33
87
|
onCopy={handleCopy}
|
|
34
88
|
isCopied={isCopied}
|
|
35
89
|
onCancel={onCancel}
|
|
90
|
+
shortMessage={swapMessages.shortMessage}
|
|
91
|
+
detailedMessage={swapMessages.detailedMessage}
|
|
92
|
+
currentStepBlockchain={currentStepBlockchain || ''}
|
|
93
|
+
switchNetwork={switchNetwork}
|
|
94
|
+
type={
|
|
95
|
+
networkWarningState ? 'warning' : swap.extraMessageSeverity || undefined
|
|
96
|
+
}
|
|
97
|
+
date={swapDate}
|
|
98
|
+
{...(shouldRetry && {
|
|
99
|
+
onRetry: () => {
|
|
100
|
+
retry(swap);
|
|
101
|
+
setTimeout(() => {
|
|
102
|
+
navigate(navigationRoutes.home);
|
|
103
|
+
}, 0);
|
|
104
|
+
},
|
|
105
|
+
})}
|
|
106
|
+
{...(lastConvertedTokenInFailedSwap && {
|
|
107
|
+
lastConvertedTokenInFailedSwap: {
|
|
108
|
+
blockchain: lastConvertedTokenInFailedSwap.blockchain,
|
|
109
|
+
outputAmount: lastConvertedTokenInFailedSwap.outputAmount,
|
|
110
|
+
symbol: lastConvertedTokenInFailedSwap.symbol,
|
|
111
|
+
},
|
|
112
|
+
})}
|
|
36
113
|
/>
|
|
37
114
|
);
|
|
38
115
|
}
|
package/src/store/bestRoute.ts
CHANGED
|
@@ -13,10 +13,15 @@ import { httpService } from '../services/httpService';
|
|
|
13
13
|
import { useSettingsStore } from './settings';
|
|
14
14
|
import { calcOutputUsdValue, createBestRouteRequestBody } from '../utils/swap';
|
|
15
15
|
import { useMetaStore } from './meta';
|
|
16
|
-
import {
|
|
16
|
+
import {
|
|
17
|
+
getDefaultToken,
|
|
18
|
+
getSortedTokens,
|
|
19
|
+
tokensAreEqual,
|
|
20
|
+
} from '../utils/wallets';
|
|
17
21
|
import { useWalletsStore } from './wallets';
|
|
18
22
|
import { TokenWithBalance } from '../pages/SelectTokenPage';
|
|
19
|
-
|
|
23
|
+
import { PendingSwap } from '@rango-dev/queue-manager-rango-preset/dist/shared';
|
|
24
|
+
import { debounce } from '../utils/common';
|
|
20
25
|
const getUsdValue = (token: Token | null, amount: string) =>
|
|
21
26
|
new BigNumber(amount || ZERO).multipliedBy(token?.usdPrice || 0);
|
|
22
27
|
|
|
@@ -43,6 +48,7 @@ export interface RouteState {
|
|
|
43
48
|
setInputAmount: (amount: string) => void;
|
|
44
49
|
bestRoute: BestRouteResponse | null;
|
|
45
50
|
setBestRoute: (bestRoute: BestRouteResponse | null) => void;
|
|
51
|
+
retry: (pendingSwap: PendingSwap) => void;
|
|
46
52
|
}
|
|
47
53
|
|
|
48
54
|
export const useBestRouteStore = createSelectors(
|
|
@@ -151,6 +157,71 @@ export const useBestRouteStore = createSelectors(
|
|
|
151
157
|
}),
|
|
152
158
|
}));
|
|
153
159
|
},
|
|
160
|
+
retry: (pendingSwap) => {
|
|
161
|
+
const { tokens, blockchains } = useMetaStore.getState().meta;
|
|
162
|
+
const balances = useWalletsStore.getState().balances;
|
|
163
|
+
const failedIndex =
|
|
164
|
+
pendingSwap.status === 'failed'
|
|
165
|
+
? pendingSwap.steps.findIndex((s) => s.status === 'failed')
|
|
166
|
+
: null;
|
|
167
|
+
|
|
168
|
+
if (failedIndex === null || failedIndex < 0) return;
|
|
169
|
+
|
|
170
|
+
const firstStep = pendingSwap.steps[0];
|
|
171
|
+
const lastStep = pendingSwap.steps[pendingSwap.steps.length - 1];
|
|
172
|
+
const fromChain =
|
|
173
|
+
blockchains.find(
|
|
174
|
+
(blockchain) => blockchain.name === firstStep.fromBlockchain
|
|
175
|
+
) || null;
|
|
176
|
+
const toChain =
|
|
177
|
+
blockchains.find(
|
|
178
|
+
(blockchain) => blockchain.name === lastStep.toBlockchain
|
|
179
|
+
) || null;
|
|
180
|
+
|
|
181
|
+
const fromToken = tokens.find((token) =>
|
|
182
|
+
tokensAreEqual(token, {
|
|
183
|
+
blockchain: firstStep.fromBlockchain,
|
|
184
|
+
symbol: firstStep.fromSymbol,
|
|
185
|
+
address: firstStep.fromSymbolAddress,
|
|
186
|
+
})
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
const toToken = tokens.find((token) =>
|
|
190
|
+
tokensAreEqual(token, {
|
|
191
|
+
blockchain: lastStep.toBlockchain,
|
|
192
|
+
symbol: lastStep.toSymbol,
|
|
193
|
+
address: lastStep.toSymbolAddress,
|
|
194
|
+
})
|
|
195
|
+
);
|
|
196
|
+
const sortedSourceTokens = getSortedTokens(
|
|
197
|
+
fromChain,
|
|
198
|
+
tokens,
|
|
199
|
+
balances,
|
|
200
|
+
[]
|
|
201
|
+
);
|
|
202
|
+
const sortedDestinationTokens = getSortedTokens(
|
|
203
|
+
toChain,
|
|
204
|
+
tokens,
|
|
205
|
+
balances,
|
|
206
|
+
[]
|
|
207
|
+
);
|
|
208
|
+
const inputAmount = pendingSwap.inputAmount;
|
|
209
|
+
set({
|
|
210
|
+
fromChain,
|
|
211
|
+
fromToken,
|
|
212
|
+
inputAmount,
|
|
213
|
+
outputAmount: null,
|
|
214
|
+
inputUsdValue: new BigNumber(0),
|
|
215
|
+
outputUsdValue: new BigNumber(0),
|
|
216
|
+
toChain,
|
|
217
|
+
toToken,
|
|
218
|
+
bestRoute: null,
|
|
219
|
+
loading: false,
|
|
220
|
+
error: '',
|
|
221
|
+
sourceTokens: sortedSourceTokens,
|
|
222
|
+
destinationTokens: sortedDestinationTokens,
|
|
223
|
+
});
|
|
224
|
+
},
|
|
154
225
|
}))
|
|
155
226
|
)
|
|
156
227
|
);
|
|
@@ -160,7 +231,7 @@ const bestRoute = (
|
|
|
160
231
|
settingsStore: typeof useSettingsStore
|
|
161
232
|
) => {
|
|
162
233
|
let abortController: AbortController | null = null;
|
|
163
|
-
const fetchBestRoute = () => {
|
|
234
|
+
const fetchBestRoute = debounce(() => {
|
|
164
235
|
const { fromToken, toToken, inputAmount } = bestRouteStore.getState();
|
|
165
236
|
const { slippage, customSlippage, disabledLiquiditySources } =
|
|
166
237
|
settingsStore.getState();
|
|
@@ -201,7 +272,7 @@ const bestRoute = (
|
|
|
201
272
|
loading: false,
|
|
202
273
|
});
|
|
203
274
|
});
|
|
204
|
-
};
|
|
275
|
+
}, 600);
|
|
205
276
|
|
|
206
277
|
useBestRouteStore.subscribe(
|
|
207
278
|
(state) => ({
|
package/src/types/routing.ts
CHANGED
package/src/types/swap.ts
CHANGED
package/src/utils/common.ts
CHANGED
|
@@ -33,3 +33,17 @@ export function shadeColor(color: string, percent: number) {
|
|
|
33
33
|
|
|
34
34
|
return '#' + RR + GG + BB;
|
|
35
35
|
}
|
|
36
|
+
|
|
37
|
+
export function debounce(fn: Function, time: number) {
|
|
38
|
+
let timeoutId: ReturnType<typeof setTimeout> | null;
|
|
39
|
+
return wrapper;
|
|
40
|
+
function wrapper(...args: any) {
|
|
41
|
+
if (timeoutId) {
|
|
42
|
+
clearTimeout(timeoutId);
|
|
43
|
+
}
|
|
44
|
+
timeoutId = setTimeout(() => {
|
|
45
|
+
timeoutId = null;
|
|
46
|
+
fn(...args);
|
|
47
|
+
}, time);
|
|
48
|
+
}
|
|
49
|
+
}
|
package/src/utils/swap.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { PendingSwap } from '@rango-dev/ui/dist/containers/History/types';
|
|
2
1
|
import {
|
|
3
2
|
WalletTypeAndAddress,
|
|
4
3
|
SwapSavedSettings,
|
|
@@ -19,7 +18,12 @@ import { WalletType } from '@rango-dev/wallets-shared';
|
|
|
19
18
|
import { getRequiredBalanceOfWallet } from './routing';
|
|
20
19
|
import { getRequiredChains, SelectedWallet } from './wallets';
|
|
21
20
|
import { LoadingStatus } from '../store/meta';
|
|
22
|
-
import { SwapButtonState } from '../types';
|
|
21
|
+
import { ConvertedToken, SwapButtonState } from '../types';
|
|
22
|
+
import {
|
|
23
|
+
PendingSwapNetworkStatus,
|
|
24
|
+
PendingSwapStep,
|
|
25
|
+
} from '@rango-dev/queue-manager-rango-preset';
|
|
26
|
+
import { PendingSwap } from '@rango-dev/queue-manager-rango-preset';
|
|
23
27
|
|
|
24
28
|
export function getOutputRatio(
|
|
25
29
|
inputUsdValue: BigNumber,
|
|
@@ -187,6 +191,7 @@ export function calculatePendingSwap(
|
|
|
187
191
|
finishTime: null,
|
|
188
192
|
requestId: bestRoute.requestId || '',
|
|
189
193
|
inputAmount: inputAmount,
|
|
194
|
+
//@ts-ignore
|
|
190
195
|
wallets,
|
|
191
196
|
status: 'running',
|
|
192
197
|
isPaused: false,
|
|
@@ -201,6 +206,7 @@ export function calculatePendingSwap(
|
|
|
201
206
|
settings: settings,
|
|
202
207
|
simulationResult: simulationResult,
|
|
203
208
|
validateBalanceOrFee,
|
|
209
|
+
//@ts-ignore
|
|
204
210
|
steps:
|
|
205
211
|
bestRoute.result?.swaps?.map((swap, index) => {
|
|
206
212
|
const swapper = meta.swappers.find(
|
|
@@ -339,25 +345,24 @@ export function hasSlippageError(
|
|
|
339
345
|
|
|
340
346
|
export function getMinRequiredSlippage(
|
|
341
347
|
route: BestRouteResponse
|
|
342
|
-
):
|
|
348
|
+
): string | null {
|
|
343
349
|
const slippages = route.result?.swaps.map(
|
|
344
350
|
(slippage) => slippage.recommendedSlippage
|
|
345
351
|
);
|
|
346
352
|
return (
|
|
347
353
|
slippages
|
|
348
|
-
?.map((s) =>
|
|
349
|
-
?.filter((s) => s > 0)
|
|
350
|
-
?.sort((a, b) => b - a)
|
|
354
|
+
?.map((s) => s?.slippage || '0')
|
|
355
|
+
?.filter((s) => parseFloat(s) > 0)
|
|
356
|
+
?.sort((a, b) => parseFloat(b) - parseFloat(a))
|
|
351
357
|
?.find(() => true) || null
|
|
352
358
|
);
|
|
353
359
|
}
|
|
354
360
|
|
|
355
361
|
export function hasProperSlippage(
|
|
356
362
|
userSlippage: string,
|
|
357
|
-
minRequiredSlippage:
|
|
363
|
+
minRequiredSlippage: string | null
|
|
358
364
|
) {
|
|
359
365
|
if (!minRequiredSlippage) return true;
|
|
360
|
-
//@ts-ignore
|
|
361
366
|
return parseFloat(userSlippage) >= parseFloat(minRequiredSlippage);
|
|
362
367
|
}
|
|
363
368
|
|
|
@@ -386,7 +391,7 @@ export function hasEnoughBalanceAndProperSlippage(
|
|
|
386
391
|
route: BestRouteResponse,
|
|
387
392
|
selectedWallets: SelectedWallet[],
|
|
388
393
|
userSlippage: string,
|
|
389
|
-
minRequiredSlippage:
|
|
394
|
+
minRequiredSlippage: string | null
|
|
390
395
|
): boolean {
|
|
391
396
|
return (
|
|
392
397
|
hasEnoughBalance(route, selectedWallets) &&
|
|
@@ -550,3 +555,87 @@ export function calcOutputUsdValue(
|
|
|
550
555
|
|
|
551
556
|
return amount.multipliedBy(tokenPrice || 0);
|
|
552
557
|
}
|
|
558
|
+
|
|
559
|
+
export function isNetworkStatusInWarningState(
|
|
560
|
+
pendingSwapStep: PendingSwapStep | null
|
|
561
|
+
): boolean {
|
|
562
|
+
return (
|
|
563
|
+
!!pendingSwapStep &&
|
|
564
|
+
pendingSwapStep.networkStatus !== null &&
|
|
565
|
+
pendingSwapStep.networkStatus !== PendingSwapNetworkStatus.NetworkChanged
|
|
566
|
+
);
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
export function getSwapMessages(
|
|
570
|
+
pendingSwap: PendingSwap,
|
|
571
|
+
currentStep: PendingSwapStep | null
|
|
572
|
+
): {
|
|
573
|
+
shortMessage: string;
|
|
574
|
+
detailedMessage: { content: string; long: boolean };
|
|
575
|
+
} {
|
|
576
|
+
const textForRemove = 'bellow button or';
|
|
577
|
+
let message = pendingSwap.extraMessage;
|
|
578
|
+
let detailedMessage = pendingSwap.extraMessageDetail;
|
|
579
|
+
|
|
580
|
+
if (pendingSwap.networkStatusExtraMessageDetail?.includes(textForRemove)) {
|
|
581
|
+
pendingSwap.networkStatusExtraMessageDetail =
|
|
582
|
+
pendingSwap.networkStatusExtraMessageDetail.replace(textForRemove, '');
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
const networkWarningState = isNetworkStatusInWarningState(currentStep);
|
|
586
|
+
|
|
587
|
+
if (networkWarningState) {
|
|
588
|
+
message = pendingSwap.networkStatusExtraMessage || '';
|
|
589
|
+
detailedMessage = pendingSwap.networkStatusExtraMessageDetail || '';
|
|
590
|
+
switch (currentStep?.networkStatus) {
|
|
591
|
+
case PendingSwapNetworkStatus.WaitingForConnectingWallet:
|
|
592
|
+
message = message || 'Waiting for connecting wallet';
|
|
593
|
+
break;
|
|
594
|
+
case PendingSwapNetworkStatus.WaitingForQueue:
|
|
595
|
+
message = message || 'Waiting for other running tasks to be finished';
|
|
596
|
+
break;
|
|
597
|
+
case PendingSwapNetworkStatus.WaitingForNetworkChange:
|
|
598
|
+
message = message || 'Waiting for changing wallet network';
|
|
599
|
+
break;
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
detailedMessage = detailedMessage || '';
|
|
603
|
+
message = message || '';
|
|
604
|
+
const isRpc =
|
|
605
|
+
message?.indexOf('code') !== -1 && message?.indexOf('reason') !== -1;
|
|
606
|
+
|
|
607
|
+
return {
|
|
608
|
+
shortMessage: message,
|
|
609
|
+
detailedMessage: { content: detailedMessage, long: isRpc },
|
|
610
|
+
};
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
export function getLastConvertedTokenInFailedSwap(
|
|
614
|
+
pendingSwap: PendingSwap
|
|
615
|
+
): ConvertedToken {
|
|
616
|
+
let resultToken: ConvertedToken = null;
|
|
617
|
+
if (pendingSwap.status === 'failed') {
|
|
618
|
+
const lastSuccessStep = pendingSwap.steps
|
|
619
|
+
.slice()
|
|
620
|
+
.reverse()
|
|
621
|
+
.filter((step) => step.status === 'success')[0];
|
|
622
|
+
|
|
623
|
+
if (lastSuccessStep) {
|
|
624
|
+
resultToken = {
|
|
625
|
+
blockchain: lastSuccessStep.toBlockchain,
|
|
626
|
+
symbol: lastSuccessStep.toSymbol,
|
|
627
|
+
outputAmount: lastSuccessStep.outputAmount,
|
|
628
|
+
address: lastSuccessStep.toSymbolAddress,
|
|
629
|
+
};
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
return resultToken;
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
export function shouldRetrySwap(pendingSwap: PendingSwap) {
|
|
636
|
+
return (
|
|
637
|
+
pendingSwap.status === 'failed' &&
|
|
638
|
+
!!pendingSwap.finishTime &&
|
|
639
|
+
new Date().getTime() - parseInt(pendingSwap.finishTime) < 4 * 3600 * 1000
|
|
640
|
+
);
|
|
641
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { PendingSwap } from '@rango-dev/queue-manager-rango-preset';
|
|
2
|
+
|
|
3
|
+
export function timeSince(timeMillis: number): string {
|
|
4
|
+
const seconds = Math.floor((new Date().getTime() - timeMillis) / 1000);
|
|
5
|
+
let intervalType: string;
|
|
6
|
+
|
|
7
|
+
let interval = Math.floor(seconds / 31536000);
|
|
8
|
+
if (interval >= 1) {
|
|
9
|
+
intervalType = 'year';
|
|
10
|
+
} else {
|
|
11
|
+
interval = Math.floor(seconds / 2592000);
|
|
12
|
+
if (interval >= 1) {
|
|
13
|
+
intervalType = 'month';
|
|
14
|
+
} else {
|
|
15
|
+
interval = Math.floor(seconds / 86400);
|
|
16
|
+
if (interval >= 1) {
|
|
17
|
+
intervalType = 'day';
|
|
18
|
+
} else {
|
|
19
|
+
interval = Math.floor(seconds / 3600);
|
|
20
|
+
if (interval >= 1) {
|
|
21
|
+
intervalType = 'hour';
|
|
22
|
+
} else {
|
|
23
|
+
interval = Math.floor(seconds / 60);
|
|
24
|
+
if (interval >= 1) {
|
|
25
|
+
intervalType = 'minute';
|
|
26
|
+
} else {
|
|
27
|
+
interval = seconds;
|
|
28
|
+
intervalType = 'second';
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (interval > 1 || interval === 0) {
|
|
36
|
+
intervalType += 's';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return interval + ' ' + intervalType;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function getSwapDate(pendingSwap: PendingSwap) {
|
|
43
|
+
return pendingSwap.finishTime
|
|
44
|
+
? `Finished ${timeSince(parseInt(pendingSwap.finishTime))} ago @ ${new Date(
|
|
45
|
+
parseInt(pendingSwap.finishTime)
|
|
46
|
+
).toLocaleString()}`
|
|
47
|
+
: `Started ${timeSince(
|
|
48
|
+
parseInt(pendingSwap.creationTime)
|
|
49
|
+
)} ago @ ${new Date(
|
|
50
|
+
parseInt(pendingSwap.creationTime)
|
|
51
|
+
).toLocaleString()}`;
|
|
52
|
+
}
|
package/src/utils/wallets.ts
CHANGED
|
@@ -441,10 +441,13 @@ export function getSortedTokens(
|
|
|
441
441
|
return sortTokens(getTokensWithBalance(filteredTokens, balances));
|
|
442
442
|
}
|
|
443
443
|
|
|
444
|
-
export function tokensAreEqual(
|
|
444
|
+
export function tokensAreEqual(
|
|
445
|
+
tokenA: Pick<Token, 'blockchain' | 'symbol' | 'address'> | null,
|
|
446
|
+
tokenB: Pick<Token, 'blockchain' | 'symbol' | 'address'> | null
|
|
447
|
+
) {
|
|
445
448
|
return (
|
|
446
449
|
tokenA?.blockchain === tokenB?.blockchain &&
|
|
447
|
-
tokenA?.
|
|
450
|
+
tokenA?.symbol === tokenB?.symbol &&
|
|
448
451
|
tokenA?.address === tokenB?.address
|
|
449
452
|
);
|
|
450
453
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ConfirmWalletsPage.d.ts","sourceRoot":"","sources":["../src/pages/ConfirmWalletsPage.tsx"],"names":[],"mappings":";AA4BA,wBAAgB,kBAAkB,gBAkGjC"}
|
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
import React, { useEffect } from 'react';
|
|
2
|
-
import { ConfirmWallets } from '@rango-dev/ui';
|
|
3
|
-
import { useNavigate } from 'react-router-dom';
|
|
4
|
-
import { useBestRouteStore } from '../store/bestRoute';
|
|
5
|
-
import { useWalletsStore } from '../store/wallets';
|
|
6
|
-
import { useWallets } from '@rango-dev/wallets-core';
|
|
7
|
-
import { navigationRoutes } from '../constants/navigationRoutes';
|
|
8
|
-
import {
|
|
9
|
-
getKeplrCompatibleConnectedWallets,
|
|
10
|
-
getRequiredChains,
|
|
11
|
-
getSelectableWallets,
|
|
12
|
-
isExperimentalChain,
|
|
13
|
-
} from '../utils/wallets';
|
|
14
|
-
import {
|
|
15
|
-
calcOutputUsdValue,
|
|
16
|
-
getTotalFeeInUsd,
|
|
17
|
-
requiredWallets,
|
|
18
|
-
} from '../utils/swap';
|
|
19
|
-
import { decimalNumber, numberToString } from '../utils/numbers';
|
|
20
|
-
import { useMetaStore } from '../store/meta';
|
|
21
|
-
import { Network, WalletType } from '@rango-dev/wallets-shared';
|
|
22
|
-
import { useNavigateBack } from '../hooks/useNavigateBack';
|
|
23
|
-
import { TokenPreview } from '../components/TokenPreview';
|
|
24
|
-
// @ts-ignore // TODO: fix error in tsc build
|
|
25
|
-
import { t } from 'i18next';
|
|
26
|
-
import { Spacer } from '@rango-dev/ui';
|
|
27
|
-
import RoutesOverview from '../components/RoutesOverview';
|
|
28
|
-
|
|
29
|
-
export function ConfirmWalletsPage() {
|
|
30
|
-
const navigate = useNavigate();
|
|
31
|
-
const { navigateBackFrom } = useNavigateBack();
|
|
32
|
-
const bestRoute = useBestRouteStore.use.bestRoute();
|
|
33
|
-
|
|
34
|
-
const { blockchains, tokens } = useMetaStore.use.meta();
|
|
35
|
-
const accounts = useWalletsStore.use.accounts();
|
|
36
|
-
const selectedWallets = useWalletsStore.use.selectedWallets();
|
|
37
|
-
const initSelectedWallets = useWalletsStore.use.initSelectedWallets();
|
|
38
|
-
const setSelectedWallet = useWalletsStore.use.setSelectedWallet();
|
|
39
|
-
|
|
40
|
-
const { getWalletInfo, connect } = useWallets();
|
|
41
|
-
const confirmDisabled = !requiredWallets(bestRoute).every((chain) =>
|
|
42
|
-
selectedWallets.map((wallet) => wallet.chain).includes(chain)
|
|
43
|
-
);
|
|
44
|
-
|
|
45
|
-
const firstStep = bestRoute?.result?.swaps[0];
|
|
46
|
-
const lastStep =
|
|
47
|
-
bestRoute?.result?.swaps[bestRoute?.result?.swaps.length - 1];
|
|
48
|
-
|
|
49
|
-
const fromAmount = decimalNumber(firstStep?.fromAmount, 3);
|
|
50
|
-
const toAmount = decimalNumber(lastStep?.toAmount, 3);
|
|
51
|
-
useEffect(() => {
|
|
52
|
-
initSelectedWallets();
|
|
53
|
-
}, []);
|
|
54
|
-
|
|
55
|
-
const selectableWallets = getSelectableWallets(
|
|
56
|
-
accounts,
|
|
57
|
-
selectedWallets,
|
|
58
|
-
getWalletInfo
|
|
59
|
-
);
|
|
60
|
-
|
|
61
|
-
const handleConnectChain = (wallet: string) => {
|
|
62
|
-
const network = wallet as Network;
|
|
63
|
-
getKeplrCompatibleConnectedWallets(selectableWallets).forEach(
|
|
64
|
-
(compatibleWallet: WalletType) => connect?.(compatibleWallet, network)
|
|
65
|
-
);
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
const totalFeeInUsd = getTotalFeeInUsd(bestRoute, tokens);
|
|
69
|
-
|
|
70
|
-
return (
|
|
71
|
-
<ConfirmWallets
|
|
72
|
-
requiredWallets={getRequiredChains(bestRoute)}
|
|
73
|
-
selectableWallets={selectableWallets}
|
|
74
|
-
onBack={navigateBackFrom.bind(null, navigationRoutes.confirmWallets)}
|
|
75
|
-
onConfirm={() =>
|
|
76
|
-
navigate(navigationRoutes.confirmSwap, { replace: true })
|
|
77
|
-
}
|
|
78
|
-
onChange={(wallet) => setSelectedWallet(wallet)}
|
|
79
|
-
confirmDisabled={confirmDisabled}
|
|
80
|
-
handleConnectChain={(wallet) => handleConnectChain(wallet)}
|
|
81
|
-
isExperimentalChain={(wallet) =>
|
|
82
|
-
getKeplrCompatibleConnectedWallets(selectableWallets).length > 0
|
|
83
|
-
? isExperimentalChain(blockchains, wallet)
|
|
84
|
-
: false
|
|
85
|
-
}
|
|
86
|
-
previewInputs={
|
|
87
|
-
<>
|
|
88
|
-
<TokenPreview
|
|
89
|
-
chain={{
|
|
90
|
-
displayName: firstStep?.from.blockchain || '',
|
|
91
|
-
logo: firstStep?.from.blockchainLogo || '',
|
|
92
|
-
}}
|
|
93
|
-
token={{
|
|
94
|
-
symbol: firstStep?.from.symbol || '',
|
|
95
|
-
image: firstStep?.from.logo || '',
|
|
96
|
-
}}
|
|
97
|
-
usdValue={calcOutputUsdValue(fromAmount, firstStep?.from.usdPrice)}
|
|
98
|
-
amount={fromAmount}
|
|
99
|
-
label={t('From')}
|
|
100
|
-
loadingStatus={'success'}
|
|
101
|
-
/>
|
|
102
|
-
<Spacer size={12} direction="vertical" />
|
|
103
|
-
<TokenPreview
|
|
104
|
-
chain={{
|
|
105
|
-
displayName: lastStep?.to.blockchain || '',
|
|
106
|
-
logo: lastStep?.to.blockchainLogo || '',
|
|
107
|
-
}}
|
|
108
|
-
token={{
|
|
109
|
-
symbol: lastStep?.to.symbol || '',
|
|
110
|
-
image: lastStep?.to.logo || '',
|
|
111
|
-
}}
|
|
112
|
-
usdValue={calcOutputUsdValue(toAmount, lastStep?.to.usdPrice)}
|
|
113
|
-
amount={toAmount}
|
|
114
|
-
label={t('To')}
|
|
115
|
-
loadingStatus={'success'}
|
|
116
|
-
/>
|
|
117
|
-
</>
|
|
118
|
-
}
|
|
119
|
-
previewRoutes={
|
|
120
|
-
<RoutesOverview
|
|
121
|
-
routes={bestRoute}
|
|
122
|
-
totalFee={numberToString(totalFeeInUsd, 0, 2)}
|
|
123
|
-
/>
|
|
124
|
-
}
|
|
125
|
-
/>
|
|
126
|
-
);
|
|
127
|
-
}
|