@rango-dev/widget-embedded 0.1.10-next.76 → 0.1.10-next.80
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/hooks/useConfirmSwap.d.ts +1 -11
- package/dist/hooks/useConfirmSwap.d.ts.map +1 -1
- package/dist/pages/ConfirmWalletsPage.d.ts.map +1 -1
- package/dist/pages/WalletsPage.d.ts.map +1 -1
- package/dist/utils/wallets.d.ts +2 -0
- package/dist/utils/wallets.d.ts.map +1 -1
- package/dist/widget-embedded.cjs.development.js +101 -130
- package/dist/widget-embedded.cjs.development.js.map +1 -1
- package/dist/widget-embedded.cjs.production.min.js +101 -130
- package/dist/widget-embedded.cjs.production.min.js.map +1 -1
- package/dist/widget-embedded.esm.js +102 -131
- package/dist/widget-embedded.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/hooks/useConfirmSwap.ts +94 -60
- package/src/pages/ConfirmWalletsPage.tsx +23 -9
- package/src/pages/WalletsPage.tsx +6 -1
- package/src/utils/wallets.ts +147 -55
package/package.json
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import { PendingSwap } from '@rango-dev/ui/dist/containers/History/types';
|
|
2
1
|
import {
|
|
3
2
|
BestRouteType,
|
|
4
3
|
SimulationValidationStatus,
|
|
5
|
-
SwapSavedSettings,
|
|
6
4
|
} from '@rango-dev/ui/dist/types/swaps';
|
|
7
|
-
import { WalletType } from '@rango-dev/wallets-shared';
|
|
5
|
+
// import { WalletType } from '@rango-dev/wallets-shared';
|
|
8
6
|
import BigNumber from 'bignumber.js';
|
|
9
7
|
import { BestRouteRequest, BestRouteResponse } from 'rango-sdk';
|
|
10
8
|
import { useState } from 'react';
|
|
@@ -13,12 +11,15 @@ import { useBestRouteStore } from '../store/bestRoute';
|
|
|
13
11
|
import { useSettingsStore } from '../store/settings';
|
|
14
12
|
import { useWalletsStore } from '../store/wallets';
|
|
15
13
|
import { compareRoutes, getRequiredBalanceOfWallet } from '../utils/routing';
|
|
16
|
-
import { calculatePendingSwap } from '../utils/swap';
|
|
17
14
|
import { SelectedWallet } from '../utils/wallets';
|
|
18
15
|
|
|
19
16
|
type CheckFeeAndBalanceResult =
|
|
20
17
|
| {
|
|
21
|
-
hasEnoughBalanceOrSlippage: {
|
|
18
|
+
hasEnoughBalanceOrSlippage: {
|
|
19
|
+
balance: boolean;
|
|
20
|
+
slippage: boolean;
|
|
21
|
+
routeChanged: boolean;
|
|
22
|
+
};
|
|
22
23
|
bestRoute: BestRouteType;
|
|
23
24
|
}
|
|
24
25
|
| { bestRoute: null }
|
|
@@ -34,12 +35,15 @@ export function useConfirmSwap() {
|
|
|
34
35
|
const selectedWallets = useWalletsStore.use.selectedWallets();
|
|
35
36
|
|
|
36
37
|
const slippage = useSettingsStore.use.slippage();
|
|
37
|
-
const disabledLiquiditySources =
|
|
38
|
+
const disabledLiquiditySources =
|
|
39
|
+
useSettingsStore.use.disabledLiquiditySources();
|
|
38
40
|
const [loading, setLoading] = useState(false);
|
|
39
41
|
const [error, setError] = useState('');
|
|
40
42
|
const [warning, setWarning] = useState('');
|
|
41
43
|
const [data, setData] = useState<BestRouteResponse | null>(null);
|
|
42
|
-
const [feeStatus, setFeeStatus] = useState<
|
|
44
|
+
const [feeStatus, setFeeStatus] = useState<
|
|
45
|
+
SimulationValidationStatus[] | null
|
|
46
|
+
>(null);
|
|
43
47
|
const [bestRouteChanged, setBestRouteChanged] = useState(false);
|
|
44
48
|
const [enoughBalance, setEnoughBalance] = useState<boolean | null>(null);
|
|
45
49
|
|
|
@@ -47,22 +51,27 @@ export function useConfirmSwap() {
|
|
|
47
51
|
route: BestRouteType,
|
|
48
52
|
selectedWallets: SelectedWallet[],
|
|
49
53
|
userSlippage: string,
|
|
50
|
-
routeChanged: boolean
|
|
54
|
+
routeChanged: boolean
|
|
51
55
|
): { balance: boolean; slippage: boolean; routeChanged: boolean } => {
|
|
52
56
|
const fee = route.validationStatus;
|
|
53
57
|
|
|
54
|
-
if (fee === null || fee.length === 0)
|
|
58
|
+
if (fee === null || fee.length === 0)
|
|
59
|
+
return { balance: true, slippage: true, routeChanged };
|
|
55
60
|
|
|
56
61
|
for (const wallet of selectedWallets) {
|
|
57
62
|
const requiredAssets = getRequiredBalanceOfWallet(wallet, fee);
|
|
58
63
|
if (!requiredAssets) continue;
|
|
59
64
|
|
|
60
|
-
const hasEnoughBalance = requiredAssets
|
|
61
|
-
|
|
65
|
+
const hasEnoughBalance = requiredAssets
|
|
66
|
+
?.map((it) => it.ok)
|
|
67
|
+
.reduce((a, b) => a && b);
|
|
68
|
+
if (!hasEnoughBalance)
|
|
69
|
+
return { balance: false, slippage: true, routeChanged };
|
|
62
70
|
}
|
|
63
71
|
|
|
64
72
|
const slippages = route.result?.swaps?.map((s) => s.recommendedSlippage);
|
|
65
|
-
const slippageError =
|
|
73
|
+
const slippageError =
|
|
74
|
+
(slippages?.filter((s) => !!s?.error)?.length || 0) > 0;
|
|
66
75
|
const minSlippage =
|
|
67
76
|
slippages
|
|
68
77
|
?.map((s) => parseFloat(s?.slippage.toString() || '0') || 0)
|
|
@@ -81,24 +90,34 @@ export function useConfirmSwap() {
|
|
|
81
90
|
};
|
|
82
91
|
|
|
83
92
|
const checkFeeAndBalance = async (
|
|
84
|
-
selectedWallets: SelectedWallet[]
|
|
93
|
+
selectedWallets: SelectedWallet[]
|
|
85
94
|
): Promise<CheckFeeAndBalanceResult> => {
|
|
86
95
|
setLoading(true);
|
|
87
96
|
setFeeStatus(null);
|
|
88
97
|
|
|
89
98
|
const selectedWalletsMap = selectedWallets.reduce(
|
|
90
|
-
(
|
|
91
|
-
|
|
99
|
+
(
|
|
100
|
+
selectedWalletsMap: BestRouteRequest['selectedWallets'],
|
|
101
|
+
selectedWallet
|
|
102
|
+
) => (
|
|
103
|
+
(selectedWalletsMap[selectedWallet.chain] = selectedWallet.address),
|
|
104
|
+
selectedWalletsMap
|
|
92
105
|
),
|
|
93
|
-
{}
|
|
106
|
+
{}
|
|
94
107
|
);
|
|
95
108
|
|
|
96
109
|
const connectedWallets: BestRouteRequest['connectedWallets'] = [];
|
|
97
110
|
|
|
98
111
|
accounts.forEach((account) => {
|
|
99
|
-
const chainAndAccounts = connectedWallets.find(
|
|
112
|
+
const chainAndAccounts = connectedWallets.find(
|
|
113
|
+
(wallet) => wallet.blockchain
|
|
114
|
+
);
|
|
100
115
|
if (!!chainAndAccounts) chainAndAccounts.addresses.push(account.address);
|
|
101
|
-
else
|
|
116
|
+
else
|
|
117
|
+
connectedWallets.push({
|
|
118
|
+
blockchain: account.chain,
|
|
119
|
+
addresses: [account.address],
|
|
120
|
+
});
|
|
102
121
|
});
|
|
103
122
|
|
|
104
123
|
const r = await httpService
|
|
@@ -127,18 +146,23 @@ export function useConfirmSwap() {
|
|
|
127
146
|
});
|
|
128
147
|
setLoading(false);
|
|
129
148
|
if (!r || !r.result || !bestRoute) return { bestRoute: null };
|
|
130
|
-
if (
|
|
149
|
+
if (
|
|
150
|
+
!new BigNumber(r.requestAmount).isEqualTo(
|
|
151
|
+
new BigNumber(inputAmount || '-1')
|
|
152
|
+
)
|
|
153
|
+
)
|
|
154
|
+
return null;
|
|
131
155
|
setFeeStatus(r.validationStatus);
|
|
132
156
|
const routeChangeStatus = compareRoutes(
|
|
133
157
|
bestRoute,
|
|
134
158
|
r,
|
|
135
159
|
inputAmount!.toString(),
|
|
136
160
|
fromToken!.usdPrice,
|
|
137
|
-
toToken!.usdPrice
|
|
161
|
+
toToken!.usdPrice
|
|
138
162
|
);
|
|
139
163
|
setBestRoute(r);
|
|
140
164
|
const isChanged = routeChangeStatus.isChanged;
|
|
141
|
-
const changeWarningMessage = routeChangeStatus.warningMessage;
|
|
165
|
+
// const changeWarningMessage = routeChangeStatus.warningMessage;
|
|
142
166
|
setBestRouteChanged(isChanged);
|
|
143
167
|
setWarning('Best route changed');
|
|
144
168
|
setData(r);
|
|
@@ -147,7 +171,7 @@ export function useConfirmSwap() {
|
|
|
147
171
|
r,
|
|
148
172
|
selectedWallets,
|
|
149
173
|
slippage.toString(),
|
|
150
|
-
isChanged
|
|
174
|
+
isChanged
|
|
151
175
|
),
|
|
152
176
|
bestRoute: r,
|
|
153
177
|
};
|
|
@@ -156,36 +180,36 @@ export function useConfirmSwap() {
|
|
|
156
180
|
const swap = () => {
|
|
157
181
|
if (!bestRoute) return;
|
|
158
182
|
if (inputAmount!.toString() === '') return;
|
|
159
|
-
const wallets = selectedWallets.reduce(
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
);
|
|
183
|
+
// const wallets = selectedWallets.reduce(
|
|
184
|
+
// (
|
|
185
|
+
// selectedWalletsMap: { [p: string]: { address: string; walletType: WalletType } },
|
|
186
|
+
// selectedWallet,
|
|
187
|
+
// ) => (
|
|
188
|
+
// (selectedWalletsMap[selectedWallet.chain] = {
|
|
189
|
+
// address: selectedWallet.address,
|
|
190
|
+
// walletType: selectedWallet.walletType,
|
|
191
|
+
// }),
|
|
192
|
+
// selectedWalletsMap
|
|
193
|
+
// ),
|
|
194
|
+
// {},
|
|
195
|
+
// );
|
|
172
196
|
|
|
173
197
|
const proceedAnyway = enoughBalance !== null;
|
|
174
198
|
|
|
175
|
-
const settings: SwapSavedSettings = {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
};
|
|
180
|
-
if (proceedAnyway) {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
199
|
+
// const settings: SwapSavedSettings = {
|
|
200
|
+
// slippage: slippage.toString(),
|
|
201
|
+
// disabledSwappersGroups: ['Osmosis'],
|
|
202
|
+
// disabledSwappersIds: [],
|
|
203
|
+
// };
|
|
204
|
+
// if (proceedAnyway) {
|
|
205
|
+
// const newSwap: PendingSwap = calculatePendingSwap(
|
|
206
|
+
// inputAmount!.toString(),
|
|
207
|
+
// bestRoute,
|
|
208
|
+
// wallets,
|
|
209
|
+
// settings,
|
|
210
|
+
// false,
|
|
211
|
+
// );
|
|
212
|
+
// }
|
|
189
213
|
|
|
190
214
|
!proceedAnyway &&
|
|
191
215
|
checkFeeAndBalance(selectedWallets)
|
|
@@ -199,13 +223,15 @@ export function useConfirmSwap() {
|
|
|
199
223
|
setError('confirm swap error');
|
|
200
224
|
return;
|
|
201
225
|
} else {
|
|
202
|
-
|
|
226
|
+
// @ts-ignore
|
|
227
|
+
const { hasEnoughBalanceOrSlippage } = data;
|
|
203
228
|
if (!hasEnoughBalanceOrSlippage) {
|
|
204
229
|
return;
|
|
205
230
|
}
|
|
206
231
|
|
|
207
232
|
setEnoughBalance(
|
|
208
|
-
hasEnoughBalanceOrSlippage.balance &&
|
|
233
|
+
hasEnoughBalanceOrSlippage.balance &&
|
|
234
|
+
hasEnoughBalanceOrSlippage.slippage
|
|
209
235
|
);
|
|
210
236
|
|
|
211
237
|
if (
|
|
@@ -213,13 +239,13 @@ export function useConfirmSwap() {
|
|
|
213
239
|
hasEnoughBalanceOrSlippage.slippage &&
|
|
214
240
|
!hasEnoughBalanceOrSlippage.routeChanged
|
|
215
241
|
) {
|
|
216
|
-
const newSwap: PendingSwap = calculatePendingSwap(
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
);
|
|
242
|
+
// const newSwap: PendingSwap = calculatePendingSwap(
|
|
243
|
+
// inputAmount!.toString(),
|
|
244
|
+
// newBestRoute,
|
|
245
|
+
// wallets,
|
|
246
|
+
// settings,
|
|
247
|
+
// true,
|
|
248
|
+
// );
|
|
223
249
|
} else if (!hasEnoughBalanceOrSlippage.balance) {
|
|
224
250
|
setError('not enough balance');
|
|
225
251
|
}
|
|
@@ -229,6 +255,14 @@ export function useConfirmSwap() {
|
|
|
229
255
|
console.log('unexpected error', error);
|
|
230
256
|
});
|
|
231
257
|
};
|
|
232
|
-
|
|
233
|
-
|
|
258
|
+
return {
|
|
259
|
+
loading,
|
|
260
|
+
error,
|
|
261
|
+
data,
|
|
262
|
+
warning,
|
|
263
|
+
feeStatus,
|
|
264
|
+
bestRouteChanged,
|
|
265
|
+
enoughBalance,
|
|
266
|
+
swap,
|
|
267
|
+
} as any;
|
|
234
268
|
}
|
|
@@ -5,8 +5,10 @@ import { useBestRouteStore } from '../store/bestRoute';
|
|
|
5
5
|
import { useWalletsStore } from '../store/wallets';
|
|
6
6
|
import { useWallets } from '@rango-dev/wallets-core';
|
|
7
7
|
import { navigationRoutes } from '../constants/navigationRoutes';
|
|
8
|
-
import { getRequiredChains, getSelectableWallets, SelectedWallet } from '../utils/wallets';
|
|
8
|
+
import { getKeplrCompatibleConnectedWallets, getRequiredChains, getSelectableWallets, isExperimentalChain, SelectedWallet } from '../utils/wallets';
|
|
9
9
|
import { requiredWallets } from '../utils/swap';
|
|
10
|
+
import { useMetaStore } from '../store/meta';
|
|
11
|
+
import { Network, WalletType } from '@rango-dev/wallets-shared';
|
|
10
12
|
|
|
11
13
|
export interface SelectableWallet extends SelectedWallet {
|
|
12
14
|
image: string;
|
|
@@ -15,15 +17,15 @@ export interface SelectableWallet extends SelectedWallet {
|
|
|
15
17
|
|
|
16
18
|
export function ConfirmWalletsPage() {
|
|
17
19
|
const navigate = useNavigate();
|
|
18
|
-
|
|
19
20
|
const bestRoute = useBestRouteStore.use.bestRoute();
|
|
20
21
|
|
|
22
|
+
const { blockchains } = useMetaStore.use.meta();
|
|
21
23
|
const accounts = useWalletsStore.use.accounts();
|
|
22
24
|
const selectedWallets = useWalletsStore.use.selectedWallets();
|
|
23
25
|
const initSelectedWallets = useWalletsStore.use.initSelectedWallets();
|
|
24
26
|
const setSelectedWallet = useWalletsStore.use.setSelectedWallet();
|
|
25
27
|
|
|
26
|
-
const { getWalletInfo } = useWallets();
|
|
28
|
+
const { getWalletInfo, connect } = useWallets();
|
|
27
29
|
const confirmDisabled = !requiredWallets(bestRoute).every((chain) =>
|
|
28
30
|
selectedWallets.map((wallet) => wallet.chain).includes(chain),
|
|
29
31
|
);
|
|
@@ -32,20 +34,32 @@ export function ConfirmWalletsPage() {
|
|
|
32
34
|
initSelectedWallets();
|
|
33
35
|
}, []);
|
|
34
36
|
|
|
37
|
+
const selectableWallets = getSelectableWallets(
|
|
38
|
+
accounts,
|
|
39
|
+
selectedWallets,
|
|
40
|
+
getWalletInfo,
|
|
41
|
+
getRequiredChains(bestRoute),
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
const handleConnectChain = (wallet: string) => {
|
|
45
|
+
const network = wallet as Network;
|
|
46
|
+
getKeplrCompatibleConnectedWallets(selectableWallets).forEach(
|
|
47
|
+
(compatibleWallet: WalletType) =>
|
|
48
|
+
connect?.(compatibleWallet, network)
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
35
52
|
return (
|
|
36
53
|
<ConfirmWallets
|
|
37
54
|
requiredWallets={getRequiredChains(bestRoute)}
|
|
38
|
-
selectableWallets={
|
|
39
|
-
accounts,
|
|
40
|
-
selectedWallets,
|
|
41
|
-
getWalletInfo,
|
|
42
|
-
getRequiredChains(bestRoute),
|
|
43
|
-
)}
|
|
55
|
+
selectableWallets={selectableWallets}
|
|
44
56
|
onBack={() => navigate(-1)}
|
|
45
57
|
swap={bestRoute!}
|
|
46
58
|
onConfirm={() => navigate(navigationRoutes.confirmSwap)}
|
|
47
59
|
onChange={(wallet) => setSelectedWallet(wallet)}
|
|
48
60
|
confirmDisabled={confirmDisabled}
|
|
61
|
+
handleConnectChain={(wallet) => handleConnectChain(wallet)}
|
|
62
|
+
isExperimentalChain={(wallet) => getKeplrCompatibleConnectedWallets(selectableWallets).length > 0 ? isExperimentalChain(blockchains , wallet) : false}
|
|
49
63
|
/>
|
|
50
64
|
);
|
|
51
65
|
}
|
|
@@ -11,6 +11,7 @@ const ListContainer = styled('div', {
|
|
|
11
11
|
gridTemplateColumns: ' repeat(2, minmax(0, 1fr))',
|
|
12
12
|
height: '450px',
|
|
13
13
|
alignContent: 'baseline',
|
|
14
|
+
padding: '0 0.5rem',
|
|
14
15
|
});
|
|
15
16
|
|
|
16
17
|
const AlertContainer = styled('div', {
|
|
@@ -19,7 +20,11 @@ const AlertContainer = styled('div', {
|
|
|
19
20
|
export function WalletsPage() {
|
|
20
21
|
const navigate = useNavigate();
|
|
21
22
|
const { state, disconnect, getWalletInfo, connect } = useWallets();
|
|
22
|
-
const wallets = getlistWallet(
|
|
23
|
+
const wallets = getlistWallet(
|
|
24
|
+
state,
|
|
25
|
+
getWalletInfo,
|
|
26
|
+
Object.values(WalletType)
|
|
27
|
+
);
|
|
23
28
|
const [walletErrorMessage, setWalletErrorMessage] = useState('');
|
|
24
29
|
|
|
25
30
|
const onSelectWallet = async (type: WalletType) => {
|