Skip to content

Commit

Permalink
Show the right "balance needed" value if we fallback from STX to regu…
Browse files Browse the repository at this point in the history
…lar Swaps (#19230)

* Show the right balance needed value if we fallback from STX to regular Swaps
* Fix usage of StxErrorTypes
  • Loading branch information
dan437 authored May 25, 2023
1 parent af018ef commit e1b1227
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
8 changes: 4 additions & 4 deletions ui/ducks/swaps/swaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ const slice = createSlice({
setCurrentSmartTransactionsError: (state, action) => {
const errorType = Object.values(StxErrorTypes).includes(action.payload)
? action.payload
: StxErrorTypes.UNAVAILABLE;
: StxErrorTypes.unavailable;
state.currentSmartTransactionsError = errorType;
},
setSwapsSTXSubmitLoading: (state, action) => {
Expand Down Expand Up @@ -554,7 +554,7 @@ const disableStxIfRegularTxInProgress = (dispatch, transactions) => {
for (const transaction of transactions) {
if (IN_PROGRESS_TRANSACTION_STATUSES.includes(transaction.status)) {
dispatch(
setCurrentSmartTransactionsError(StxErrorTypes.REGULAR_TX_IN_PROGRESS),
setCurrentSmartTransactionsError(StxErrorTypes.regularTxPending),
);
break;
}
Expand Down Expand Up @@ -939,7 +939,7 @@ export const signAndSendSwapsSmartTransaction = ({
if (!fees) {
log.error('"fetchSwapsSmartTransactionFees" failed');
dispatch(setSwapsSTXSubmitLoading(false));
dispatch(setCurrentSmartTransactionsError(StxErrorTypes.UNAVAILABLE));
dispatch(setCurrentSmartTransactionsError(StxErrorTypes.unavailable));
return;
}
if (approveTxParams) {
Expand Down Expand Up @@ -1329,7 +1329,7 @@ export function fetchSwapsSmartTransactionFees({
const errorObj = parseSmartTransactionsError(e.message);
if (
fallbackOnNotEnoughFunds ||
errorObj?.error !== StxErrorTypes.NOT_ENOUGH_FUNDS
errorObj?.error !== StxErrorTypes.notEnoughFunds
) {
dispatch(setCurrentSmartTransactionsError(errorObj?.error));
}
Expand Down
18 changes: 8 additions & 10 deletions ui/pages/swaps/view-quote/view-quote.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ export default function ViewQuote() {
);
const swapsNetworkConfig = useSelector(getSwapsNetworkConfig, shallowEqual);
const unsignedTransaction = usedQuote.trade;
const isSmartTransaction =
currentSmartTransactionsEnabled && smartTransactionsOptInStatus;

let gasFeeInputs;
if (networkAndAccountSupports1559) {
Expand Down Expand Up @@ -458,7 +460,7 @@ export default function ViewQuote() {
: null;

let ethBalanceNeededStx;
if (smartTransactionsError?.balanceNeededWei) {
if (isSmartTransaction && smartTransactionsError?.balanceNeededWei) {
ethBalanceNeededStx = decWEIToDecETH(
smartTransactionsError.balanceNeededWei -
smartTransactionsError.currentBalanceWei,
Expand All @@ -467,7 +469,7 @@ export default function ViewQuote() {

const destinationToken = useSelector(getDestinationTokenInfo, isEqual);
useEffect(() => {
if (currentSmartTransactionsEnabled && smartTransactionsOptInStatus) {
if (isSmartTransaction) {
if (insufficientTokens) {
dispatch(setBalanceError(true));
} else if (balanceError && !insufficientTokens) {
Expand All @@ -483,8 +485,7 @@ export default function ViewQuote() {
insufficientEth,
balanceError,
dispatch,
currentSmartTransactionsEnabled,
smartTransactionsOptInStatus,
isSmartTransaction,
]);

useEffect(() => {
Expand Down Expand Up @@ -520,10 +521,7 @@ export default function ViewQuote() {
ethBalanceNeeded;

// If it's a Smart Transaction and ETH balance is needed, we want to show a warning.
const isStxAndEthBalanceIsNeeded =
currentSmartTransactionsEnabled &&
smartTransactionsOptInStatus &&
ethBalanceNeededStx;
const isStxAndEthBalanceIsNeeded = isSmartTransaction && ethBalanceNeededStx;

// Indicates if we should show to a user a warning about insufficient funds for swapping.
const showInsufficientWarning =
Expand Down Expand Up @@ -924,14 +922,14 @@ export default function ViewQuote() {
]);

useEffect(() => {
if (currentSmartTransactionsEnabled && smartTransactionsOptInStatus) {
if (isSmartTransaction) {
// Removes a smart transactions error when the component loads.
dispatch({
type: SET_SMART_TRANSACTIONS_ERROR,
payload: null,
});
}
}, [currentSmartTransactionsEnabled, smartTransactionsOptInStatus, dispatch]);
}, [isSmartTransaction, dispatch]);

return (
<div className="view-quote">
Expand Down

0 comments on commit e1b1227

Please sign in to comment.