Skip to content

Commit

Permalink
Merge pull request #910 from Wallchain-Inc/fix-wallchain
Browse files Browse the repository at this point in the history
Fix wallchain
  • Loading branch information
sameepsi authored Jul 18, 2023
2 parents 893e8ba + 0e7869b commit c40ed63
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 36 deletions.
80 changes: 47 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 @@ -838,7 +840,8 @@ const SwapBestTrade: React.FC<{
optimalRate &&
account &&
library &&
chainId
chainId &&
approval === ApprovalState.APPROVED
) {
setBonusRouteFound(false);
setBonusRouteLoading(true);
Expand Down Expand Up @@ -892,7 +895,18 @@ const SwapBestTrade: React.FC<{
outputCurrencySymbol,
outputCurrencyAddress,
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 (
bonusRouteFound &&
(approval === ApprovalState.NOT_APPROVED ||
approval === ApprovalState.UNKNOWN)
) {
setApprovalSubmitted(false);
}
}, [approval, bonusRouteFound]);

useEffect(() => {
fetchOptimalRate();
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 c40ed63

Please sign in to comment.