Skip to content

Commit

Permalink
swaps: fix max native check (#5399)
Browse files Browse the repository at this point in the history
  • Loading branch information
skylarbarrera authored Feb 15, 2024
1 parent 5999196 commit 1f08e05
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/hooks/useSwapInputHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import { useCallback } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { Alert } from '../components/alerts';
import { isNativeAsset } from '@/handlers/assets';
import { greaterThan, multiply, subtract, toFixedDecimals } from '@/helpers/utilities';
import { greaterThan, toFixedDecimals } from '@/helpers/utilities';
import { useGas } from '@/hooks';
import { AppState } from '@/redux/store';
import { updateSwapInputAmount, updateSwapNativeAmount, updateSwapOutputAmount } from '@/redux/swap';
import { ethereumUtils } from '@/utils';

export default function useSwapInputHandlers() {
const dispatch = useDispatch();
const type = useSelector((state: AppState) => state.swap.type);

const { selectedGasFee, l1GasFeeOptimism } = useGas();

Expand All @@ -26,12 +25,11 @@ export default function useSwapInputHandlers() {
const oldAmount = accountAsset?.balance?.amount ?? '0';
let newAmount = oldAmount;
if (isNativeAsset(inputCurrencyAddress, inputCurrencyNetwork) && accountAsset) {
// this subtracts gas from the balance of the asset
newAmount = toFixedDecimals(ethereumUtils.getBalanceAmount(selectedGasFee, accountAsset, l1GasFeeOptimism), 6);
const transactionFee = subtract(oldAmount, newAmount);
const newAmountMinusFee = toFixedDecimals(subtract(newAmount, multiply(transactionFee, 1.5)), 6);

if (greaterThan(newAmountMinusFee, 0)) {
dispatch(updateSwapInputAmount(newAmountMinusFee));
if (greaterThan(newAmount, 0)) {
dispatch(updateSwapInputAmount(newAmount));
} else {
Alert({
message: lang.t('expanded_state.swap.swap_max_insufficient_alert.message', { symbol: accountAsset.symbol }),
Expand Down

0 comments on commit 1f08e05

Please sign in to comment.