Skip to content

Commit

Permalink
fix(Swap): update trade object on each block (#1297)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsimao authored Jun 15, 2023
1 parent bf01bb3 commit 1b48685
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/pages/AMM/Swap/components/SwapForm/SwapForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Big from 'big.js';
import { ChangeEventHandler, Key, useEffect, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';
import { useDebounce } from 'react-use';
import { useDebounce, useInterval } from 'react-use';

import { StoreType } from '@/common/types/util.types';
import { convertMonetaryAmountToValueInUSD, formatUSD, newSafeMonetaryAmount } from '@/common/utils/utils';
Expand All @@ -20,6 +20,7 @@ import {
} from '@/lib/form';
import { SlippageManager } from '@/pages/AMM/shared/components';
import { SwapPair } from '@/types/swap';
import { REFETCH_INTERVAL } from '@/utils/constants/api';
import { SWAP_PRICE_IMPACT_LIMIT } from '@/utils/constants/swap';
import { getTokenPrice } from '@/utils/helpers/prices';
import { useGetBalances } from '@/utils/hooks/api/tokens/use-get-balances';
Expand Down Expand Up @@ -120,20 +121,21 @@ const SwapForm = ({
onSuccess: onSwap
});

useDebounce(
() => {
if (!pair.input || !pair.output || !inputAmount) {
return setTrade(undefined);
}
const handleChangeTrade = () => {
if (!pair.input || !pair.output || !inputAmount) {
return setTrade(undefined);
}

const inputMonetaryAmount = newMonetaryAmount(inputAmount, pair.input, true);
const trade = window.bridge.amm.getOptimalTrade(inputMonetaryAmount, pair.output, liquidityPools);
const inputMonetaryAmount = newMonetaryAmount(inputAmount, pair.input, true);
const trade = window.bridge.amm.getOptimalTrade(inputMonetaryAmount, pair.output, liquidityPools);

setTrade(trade);
},
500,
[inputAmount, pair]
);
setTrade(trade);
};

// attemp to update trade object on each new block
useInterval(handleChangeTrade, REFETCH_INTERVAL.BLOCK);

useDebounce(handleChangeTrade, 500, [inputAmount, pair]);

const inputBalance = pair.input && getAvailableBalance(pair.input.ticker);
const outputBalance = pair.output && getAvailableBalance(pair.output.ticker);
Expand Down

2 comments on commit 1b48685

@vercel
Copy link

@vercel vercel bot commented on 1b48685 Jun 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 1b48685 Jun 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.