Skip to content

Commit

Permalink
Added 50% option in while swapping
Browse files Browse the repository at this point in the history
  • Loading branch information
sameepsi committed Jan 7, 2022
1 parent 4b1234e commit 4856b63
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/components/Swap/Swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import useToggledVersion, { Version } from 'hooks/useToggledVersion';
import {
addMaticToMetamask,
confirmPriceImpactWithoutFee,
halfAmountSpend,
maxAmountSpend,
} from 'utils';
import { computeTradePriceBreakdown, warningSeverity } from 'utils/prices';
Expand Down Expand Up @@ -384,10 +385,18 @@ const Swap: React.FC<{
currencyBalances[Field.INPUT],
);

const halfAmountInput: CurrencyAmount | undefined = halfAmountSpend(
currencyBalances[Field.INPUT],
);

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

const handleHalfInput = useCallback(() => {
halfAmountInput && onUserInput(Field.INPUT, halfAmountInput.toExact());
}, [halfAmountInput, onUserInput]);

const atMaxAmountInput = Boolean(
maxAmountInput && parsedAmounts[Field.INPUT]?.equalTo(maxAmountInput),
);
Expand Down Expand Up @@ -557,7 +566,9 @@ const Swap: React.FC<{
<CurrencyInput
title='From:'
currency={currencies[Field.INPUT]}
onHalf={handleHalfInput}
onMax={handleMaxInput}
showHalfButton={true}
showMaxButton={!atMaxAmountInput}
otherCurrency={currencies[Field.OUTPUT]}
handleCurrencySelect={handleCurrencySelect}
Expand Down
8 changes: 8 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1447,6 +1447,14 @@ export function maxAmountSpend(
return currencyAmount;
}

export function halfAmountSpend(
currencyAmount?: CurrencyAmount,
): CurrencyAmount | undefined {
const maxAmount = maxAmountSpend(currencyAmount);
if (!maxAmount) return undefined;

return CurrencyAmount.ether(JSBI.divide(maxAmount.raw, JSBI.BigInt(2)));
}
export function getRouterContract(
_: number,
library: Web3Provider,
Expand Down

0 comments on commit 4856b63

Please sign in to comment.