Skip to content

Commit

Permalink
fix: calculation of amount to approve
Browse files Browse the repository at this point in the history
  • Loading branch information
fish-sausage committed Jul 13, 2023
1 parent e0b49e7 commit 0e7869b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 36 deletions.
67 changes: 34 additions & 33 deletions src/components/Swap/SwapBestTrade.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,11 @@ const SwapBestTrade: React.FC<{
inputCurrencyV3,
outputCurrencyV3,
]);

const maxAmountInputV2 = maxAmountSpend(
chainIdToUse,
currencyBalances[Field.INPUT],
);
const formattedAmounts = useMemo(() => {
return {
[independentField]: typedValue,
Expand All @@ -388,11 +393,40 @@ const SwapBestTrade: React.FC<{
};
}, [independentField, typedValue, dependentField, showWrap, parsedAmounts]);

const maxAmountInput =
maxAmountInputV2 && inputCurrencyV3
? CurrencyAmount.fromRawAmount(inputCurrencyV3, maxAmountInputV2.raw)
: undefined;

const handleMaxInput = useCallback(() => {
maxAmountInput && onUserInput(Field.INPUT, maxAmountInput.toExact());
setSwapType(SwapSide.SELL);
}, [maxAmountInput, onUserInput]);

const handleHalfInput = useCallback(() => {
if (!maxAmountInput) {
return;
}

const halvedAmount = maxAmountInput.divide('2');

onUserInput(
Field.INPUT,
halvedAmount.toFixed(maxAmountInput.currency.decimals),
);
setSwapType(SwapSide.SELL);
}, [maxAmountInput, onUserInput]);

const atMaxAmountInput = Boolean(
maxAmountInput && parsedAmounts[Field.INPUT]?.equalTo(maxAmountInput),
);

const [approval, approveCallback] = useApproveCallbackFromBestTrade(
pct,
inputCurrencyV3,
optimalRate,
bonusRouteFound,
atMaxAmountInput,
);

const showApproveFlow =
Expand Down Expand Up @@ -620,38 +654,6 @@ const SwapBestTrade: React.FC<{
[onUserInput],
);

const maxAmountInputV2 = maxAmountSpend(
chainIdToUse,
currencyBalances[Field.INPUT],
);
const maxAmountInput =
maxAmountInputV2 && inputCurrencyV3
? CurrencyAmount.fromRawAmount(inputCurrencyV3, maxAmountInputV2.raw)
: undefined;

const handleMaxInput = useCallback(() => {
maxAmountInput && onUserInput(Field.INPUT, maxAmountInput.toExact());
setSwapType(SwapSide.SELL);
}, [maxAmountInput, onUserInput]);

const handleHalfInput = useCallback(() => {
if (!maxAmountInput) {
return;
}

const halvedAmount = maxAmountInput.divide('2');

onUserInput(
Field.INPUT,
halvedAmount.toFixed(maxAmountInput.currency.decimals),
);
setSwapType(SwapSide.SELL);
}, [maxAmountInput, onUserInput]);

const atMaxAmountInput = Boolean(
maxAmountInput && parsedAmounts[Field.INPUT]?.equalTo(maxAmountInput),
);

const onParaswap = () => {
if (showWrap && onWrap) {
onWrap();
Expand Down Expand Up @@ -895,7 +897,6 @@ const SwapBestTrade: React.FC<{
typedValue,
approval, //Added to trigger bonus route search when approval changes
]);

//Reset approvalSubmitted when approval changes, it's needed when user hadn't nor paraswap neither wallchain approvals
useEffect(() => {
if (
Expand Down
10 changes: 7 additions & 3 deletions src/hooks/useApproveCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,19 @@ export function useApproveCallbackFromBestTrade(
currency?: Currency,
optimalRate?: OptimalRate,
bonusRouteFound?: boolean,
atMaxAmountInput?: boolean,
): [ApprovalState, () => Promise<void>] {
const { chainId } = useActiveWeb3React();
const amountToApprove = useMemo(
() =>
optimalRate
? new Fraction(ONE).add(allowedSlippage).multiply(optimalRate.srcAmount)
.quotient
? atMaxAmountInput
? new Fraction(ONE).multiply(optimalRate.srcAmount).quotient
: new Fraction(ONE)
.add(allowedSlippage)
.multiply(optimalRate.srcAmount).quotient
: undefined,
[optimalRate, allowedSlippage],
[optimalRate, allowedSlippage, atMaxAmountInput],
);

return useApproveCallbackV3(
Expand Down

0 comments on commit 0e7869b

Please sign in to comment.