@rango-dev/widget-embedded 0.1.10 → 0.1.11-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { WalletState, InfoCircleIcon, AngleDownIcon, Typography, Spacer, Button, TextField, styled, Tooltip, RetryIcon, HistoryIcon, SettingsIcon, Header as Header$1, VerticalSwapIcon, BestRoute, Alert, ChevronRightIcon, GasIcon, ConfirmSwap, History, LiquiditySourcesSelector, BlockchainSelector, TokenSelector, Settings, SecondaryPage, Spinner, LoadingFailedAlert, Wallet, SwapHistory, AddWalletIcon, globalCss, createTheme, SwapContainer } from '@rango-dev/ui';
1
+ import { WalletState, Typography, InfoCircleIcon, AngleDownIcon, Spacer, Button, TextField, styled, Tooltip, RetryIcon, HistoryIcon, SettingsIcon, Header as Header$1, VerticalSwapIcon, BestRoute, Alert, ChevronRightIcon, GasIcon, ConfirmSwap, History, LiquiditySourcesSelector, BlockchainSelector, TokenSelector, Settings, SecondaryPage, Spinner, LoadingFailedAlert, Wallet, SwapHistory, AddWalletIcon, globalCss, createTheme, SwapContainer } from '@rango-dev/ui';
2
2
  import React, { useRef, useEffect, useState, useLayoutEffect } from 'react';
3
3
  import { useInRouterContext as useInRouterContext$1, MemoryRouter, useLocation as useLocation$1, useNavigate as useNavigate$1 } from 'react-router';
4
4
  import { PendingSwapNetworkStatus, useQueueManager, getCurrentStep, getCurrentBlockchainOfOrNull, getRelatedWalletOrNull, cancelSwap, swapQueueDef, checkWaitingForNetworkChange } from '@rango-dev/queue-manager-rango-preset';
@@ -436,7 +436,7 @@ function getSwapButtonState(loadingMetaStatus, accounts, loading, bestRoute, has
436
436
  };else if (inputIsZero) return {
437
437
  title: 'Enter an amount',
438
438
  disabled: true
439
- };else if (!bestRoute) return {
439
+ };else if (!bestRoute || !bestRoute.result) return {
440
440
  title: 'Swap',
441
441
  disabled: true
442
442
  };else if (hasLimitError) return {
@@ -744,8 +744,21 @@ function getRequiredBalanceOfWallet(selectedWallet, fee) {
744
744
  if (!relatedFeeStatus) return null;
745
745
  return relatedFeeStatus.requiredAssets;
746
746
  }
747
- function isRouteParametersChanged(prevParams, currentParams) {
748
- return !!currentParams.fromToken && !!currentParams.toToken && (prevParams.fromChain?.name !== currentParams.fromChain?.name || prevParams.toChain?.name !== currentParams.toChain?.name || prevParams.fromToken?.symbol !== currentParams.fromToken?.symbol || prevParams.toToken?.symbol !== currentParams.toToken?.symbol || prevParams.fromToken?.blockchain !== currentParams.fromToken?.blockchain || prevParams.toToken?.blockchain !== currentParams.toToken?.blockchain || prevParams.fromToken?.address !== currentParams.fromToken?.address || prevParams.toToken?.address !== currentParams.toToken?.address || prevParams.inputAmount !== currentParams.inputAmount || prevParams.slippage !== currentParams.slippage || prevParams.customSlippage !== currentParams.customSlippage || prevParams.disabledLiquiditySources?.length !== currentParams.disabledLiquiditySources?.length);
747
+ function isRouteParametersChanged(params) {
748
+ if (params.store === 'bestRoute') {
749
+ const {
750
+ prevState,
751
+ currentState
752
+ } = params;
753
+ return !!currentState.fromToken && !!currentState.toToken && (prevState.fromChain?.name !== currentState.fromChain?.name || prevState.toChain?.name !== currentState.toChain?.name || prevState.fromToken?.symbol !== currentState.fromToken?.symbol || prevState.toToken?.symbol !== currentState.toToken?.symbol || prevState.fromToken?.blockchain !== prevState.fromToken?.blockchain || prevState.toToken?.blockchain !== currentState.toToken?.blockchain || prevState.fromToken?.address !== currentState.fromToken?.address || prevState.toToken?.address !== currentState.toToken?.address || prevState.inputAmount !== currentState.inputAmount);
754
+ } else if (params.store === 'settings') {
755
+ const {
756
+ prevState,
757
+ currentState
758
+ } = params;
759
+ return prevState.slippage !== currentState.slippage || prevState.customSlippage !== currentState.customSlippage || prevState.disabledLiquiditySources?.length !== currentState.disabledLiquiditySources?.length || prevState.infiniteApprove || currentState.infiniteApprove;
760
+ }
761
+ return false;
749
762
  }
750
763
  //todo: refactor bestRoute store and add loadingStatus
751
764
  const getBestRouteStatus = (loading, error) => {
@@ -1011,7 +1024,6 @@ const useBestRouteStore = /*#__PURE__*/createSelectors( /*#__PURE__*/create()( /
1011
1024
  toToken: token
1012
1025
  })),
1013
1026
  setInputAmount: amount => {
1014
- console.log(amount, typeof amount);
1015
1027
  set(state => ({
1016
1028
  inputAmount: amount,
1017
1029
  ...(!amount && {
@@ -1064,7 +1076,17 @@ const useBestRouteStore = /*#__PURE__*/createSelectors( /*#__PURE__*/create()( /
1064
1076
  sourceTokens: sortedSourceTokens,
1065
1077
  destinationTokens: sortedDestinationTokens
1066
1078
  });
1067
- }
1079
+ },
1080
+ switchFromAndTo: () => set(state => ({
1081
+ fromChain: state.toChain,
1082
+ fromToken: state.toToken,
1083
+ toChain: state.fromChain,
1084
+ toToken: state.fromToken,
1085
+ sourceTokens: state.destinationTokens,
1086
+ destinationTokens: state.sourceTokens,
1087
+ inputAmount: state.outputAmount?.toString() || '',
1088
+ inputUsdValue: getUsdValue(state.toToken, state.outputAmount?.toString() || '')
1089
+ }))
1068
1090
  }))));
1069
1091
  const bestRoute = (bestRouteStore, settingsStore) => {
1070
1092
  let abortController = null;
@@ -1117,7 +1139,7 @@ const bestRoute = (bestRouteStore, settingsStore) => {
1117
1139
  inputAmount,
1118
1140
  inputUsdValue
1119
1141
  } = useBestRouteStore.getState();
1120
- if (!inputAmount || inputAmount === '0') return;
1142
+ if (!inputAmount || inputAmount === '0' || inputUsdValue.eq(0)) return;
1121
1143
  if (tokensAreEqual(fromToken, toToken)) return bestRouteStore.setState({
1122
1144
  loading: false,
1123
1145
  bestRoute: null,
@@ -1139,17 +1161,26 @@ const bestRoute = (bestRouteStore, settingsStore) => {
1139
1161
  toToken: state.toToken,
1140
1162
  inputAmount: state.inputAmount
1141
1163
  }), bestRouteParamsListener, {
1142
- equalityFn: (prevState, state) => {
1143
- if (isRouteParametersChanged(prevState, state)) return false;else return true;
1164
+ equalityFn: (prevState, currentState) => {
1165
+ if (isRouteParametersChanged({
1166
+ store: 'bestRoute',
1167
+ prevState,
1168
+ currentState
1169
+ })) return false;else return true;
1144
1170
  }
1145
1171
  });
1146
1172
  useSettingsStore.subscribe(state => ({
1147
1173
  slippage: state.slippage,
1148
1174
  customSlippage: state.customSlippage,
1149
- disabledLiquiditySources: state.disabledLiquiditySources
1175
+ disabledLiquiditySources: state.disabledLiquiditySources,
1176
+ infiniteApprove: state.infiniteApprove
1150
1177
  }), bestRouteParamsListener, {
1151
- equalityFn: (prevState, state) => {
1152
- if (isRouteParametersChanged(prevState, state)) return false;else return true;
1178
+ equalityFn: (prevState, currentState) => {
1179
+ if (isRouteParametersChanged({
1180
+ store: 'settings',
1181
+ prevState,
1182
+ currentState
1183
+ })) return false;else return true;
1153
1184
  }
1154
1185
  });
1155
1186
  return {
@@ -1314,6 +1345,20 @@ function UpdateUrl() {
1314
1345
  return null;
1315
1346
  }
1316
1347
 
1348
+ function PercentageChange(props) {
1349
+ const {
1350
+ inputUsdValue,
1351
+ outputUsdValue
1352
+ } = props;
1353
+ if (!inputUsdValue || !outputUsdValue || !outputUsdValue.gt(0)) return null;
1354
+ const percentageChange = getPercentageChange(inputUsdValue.toNumber(), outputUsdValue.toNumber());
1355
+ const showPercentageChange = percentageChange?.lt(0);
1356
+ return React.createElement(React.Fragment, null, showPercentageChange && React.createElement(Typography, {
1357
+ variant: "caption",
1358
+ color: "$error500"
1359
+ }, `(${numberToString(percentageChange, 0, 2)}%)`));
1360
+ }
1361
+
1317
1362
  const Box = /*#__PURE__*/styled('div', {
1318
1363
  display: 'flex',
1319
1364
  flexDirection: 'column',
@@ -1357,6 +1402,13 @@ const Container = /*#__PURE__*/styled('div', {
1357
1402
  '.amount': {
1358
1403
  width: '30%'
1359
1404
  }
1405
+ },
1406
+ '.output-usd': {
1407
+ display: 'flex',
1408
+ div: {
1409
+ display: 'flex',
1410
+ paddingLeft: '$8'
1411
+ }
1360
1412
  }
1361
1413
  });
1362
1414
  const StyledImage = /*#__PURE__*/styled('img', {
@@ -1441,10 +1493,15 @@ function TokenInfo(props) {
1441
1493
  type: "primary",
1442
1494
  variant: "ghost",
1443
1495
  size: "compact"
1444
- }, t('Max')))) : React.createElement(Typography, {
1496
+ }, t('Max')))) : React.createElement("div", {
1497
+ className: "output-usd"
1498
+ }, React.createElement(PercentageChange, {
1499
+ inputUsdValue: inputUsdValue,
1500
+ outputUsdValue: props.outputUsdValue
1501
+ }), React.createElement("div", null, React.createElement(Typography, {
1445
1502
  variant: "caption",
1446
1503
  color: "neutrals600"
1447
- }, `$${numberToString(props.outputUsdValue)}`)), React.createElement("div", {
1504
+ }, `$${numberToString(props.outputUsdValue)}`)))), React.createElement("div", {
1448
1505
  className: "form"
1449
1506
  }, React.createElement(Button, {
1450
1507
  className: "selectors",
@@ -1667,10 +1724,6 @@ function Home(props) {
1667
1724
  const fromToken = useBestRouteStore.use.fromToken();
1668
1725
  const toChain = useBestRouteStore.use.toChain();
1669
1726
  const toToken = useBestRouteStore.use.toToken();
1670
- const setFromChain = useBestRouteStore.use.setFromChain();
1671
- const setFromToken = useBestRouteStore.use.setFromToken();
1672
- const setToChain = useBestRouteStore.use.setToChain();
1673
- const setToToken = useBestRouteStore.use.setToToken();
1674
1727
  const setInputAmount = useBestRouteStore.use.setInputAmount();
1675
1728
  const inputUsdValue = useBestRouteStore.use.inputUsdValue();
1676
1729
  const inputAmount = useBestRouteStore.use.inputAmount();
@@ -1683,14 +1736,7 @@ function Home(props) {
1683
1736
  const loadingMetaStatus = useMetaStore.use.loadingStatus();
1684
1737
  const accounts = useWalletsStore.use.accounts();
1685
1738
  const setCurrentPage = useUiStore.use.setCurrentPage();
1686
- const swithFromAndTo = () => {
1687
- setFromChain(toChain);
1688
- setFromToken(toToken);
1689
- setToChain(fromChain);
1690
- setToToken(fromToken);
1691
- setInputAmount(outputAmount?.toString() || '');
1692
- setCount(prev => prev + 1);
1693
- };
1739
+ const switchFromAndTo = useBestRouteStore.use.switchFromAndTo();
1694
1740
  const errorMessage = loadingMetaStatus === 'failed' ? errorMessages.genericServerError : bestRouteError;
1695
1741
  const needsToWarnEthOnPath = false;
1696
1742
  const showBestRoute = inputAmount && (!!bestRoute || fetchingBestRoute || bestRouteError);
@@ -1723,7 +1769,10 @@ function Home(props) {
1723
1769
  inputAmount: inputAmount
1724
1770
  }), React.createElement(SwitchButtonContainer, null, React.createElement(Button, {
1725
1771
  variant: "ghost",
1726
- onClick: swithFromAndTo
1772
+ onClick: () => {
1773
+ switchFromAndTo();
1774
+ setCount(prev => prev + 1);
1775
+ }
1727
1776
  }, React.createElement(VerticalSwapIcon, {
1728
1777
  size: 32
1729
1778
  }), isRouterInContext && React.createElement(SwithFromAndTo, {
@@ -1853,7 +1902,10 @@ const Container$3 = /*#__PURE__*/styled('div', {
1853
1902
  display: 'flex',
1854
1903
  justifyContent: 'space-between',
1855
1904
  alignItems: 'center',
1856
- minHeight: '32px'
1905
+ minHeight: '32px',
1906
+ '.usd-value': {
1907
+ paddingLeft: '$8'
1908
+ }
1857
1909
  },
1858
1910
  '.form': {
1859
1911
  display: 'flex',
@@ -1898,7 +1950,8 @@ function TokenPreview(props) {
1898
1950
  const {
1899
1951
  chain,
1900
1952
  token,
1901
- loadingStatus
1953
+ loadingStatus,
1954
+ percentageChange
1902
1955
  } = props;
1903
1956
  const {
1904
1957
  t
@@ -1920,10 +1973,11 @@ function TokenPreview(props) {
1920
1973
  }, React.createElement(Typography, {
1921
1974
  variant: "body2",
1922
1975
  color: "neutrals800"
1923
- }, props.label), props.usdValue && React.createElement(Typography, {
1976
+ }, props.label), React.createElement("div", null, percentageChange, props.usdValue && React.createElement(Typography, {
1924
1977
  variant: "caption",
1925
- color: "neutrals600"
1926
- }, `$${numberToString(props.usdValue)}`)), React.createElement("div", {
1978
+ color: "neutrals600",
1979
+ className: "usd-value"
1980
+ }, `$${numberToString(props.usdValue)}`))), React.createElement("div", {
1927
1981
  className: "form"
1928
1982
  }, React.createElement(Button, {
1929
1983
  className: "selectors",
@@ -2302,6 +2356,8 @@ function ConfirmSwapPage() {
2302
2356
  const setSelectedWallet = useWalletsStore.use.setSelectedWallet();
2303
2357
  const slippage = useSettingsStore.use.slippage();
2304
2358
  const customSlippage = useSettingsStore.use.customSlippage();
2359
+ const inputUsdValue = useBestRouteStore.use.inputUsdValue();
2360
+ const outputUsdValue = useBestRouteStore.use.outputUsdValue();
2305
2361
  const bestRouteloadingStatus = getBestRouteStatus(fetchingBestRoute, !!fetchingBestRouteError);
2306
2362
  const {
2307
2363
  manager
@@ -2364,7 +2420,7 @@ function ConfirmSwapPage() {
2364
2420
  symbol: firstStep?.from.symbol || '',
2365
2421
  image: firstStep?.from.logo || ''
2366
2422
  },
2367
- usdValue: calcOutputUsdValue(fromAmount, firstStep?.from.usdPrice),
2423
+ usdValue: inputUsdValue,
2368
2424
  amount: fromAmount,
2369
2425
  label: t('From'),
2370
2426
  loadingStatus: bestRouteloadingStatus
@@ -2380,10 +2436,14 @@ function ConfirmSwapPage() {
2380
2436
  symbol: lastStep?.to.symbol || '',
2381
2437
  image: lastStep?.to.logo || ''
2382
2438
  },
2383
- usdValue: calcOutputUsdValue(toAmount, lastStep?.to.usdPrice),
2439
+ usdValue: outputUsdValue,
2384
2440
  amount: toAmount,
2385
2441
  label: t('To'),
2386
- loadingStatus: bestRouteloadingStatus
2442
+ loadingStatus: bestRouteloadingStatus,
2443
+ percentageChange: React.createElement(PercentageChange, {
2444
+ inputUsdValue: inputUsdValue,
2445
+ outputUsdValue: outputUsdValue
2446
+ })
2387
2447
  })),
2388
2448
  previewRoutes: React.createElement(RoutesOverview, {
2389
2449
  routes: bestRoute,