Skip to content

Commit

Permalink
SOV-1867: Fix margin long slippage form (#2508)
Browse files Browse the repository at this point in the history
* Fix margin long slippage form

* Fix slippage price for Close dialog

* Rename calculateMinReturn method
  • Loading branch information
tiltom authored Mar 22, 2023
1 parent f5a768c commit 096291d
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 24 deletions.
2 changes: 2 additions & 0 deletions src/app/hooks/trading/useTrading_resolvePairTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface PairTokensResponse {
collateralToken: Asset;
useLoanTokens: boolean;
useCollateralTokens: boolean;
quoteToken: Asset;
}

export function useTrading_resolvePairTokens(
Expand All @@ -33,5 +34,6 @@ export function useTrading_resolvePairTokens(
collateralToken,
useLoanTokens: loanToken === collateral,
useCollateralTokens: loanToken !== collateral,
quoteToken: pair.longAsset,
};
}
2 changes: 1 addition & 1 deletion src/app/hooks/useCacheCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function useCacheCall<T = any>(
// @dev: generating id from contractName, methodName and args
// @dev: ignoring account because contract response is the same for all accounts unless account is part of the args
const hashedArgs = useMemo(
() => idHash([getContract(contractName).address, methodName, args]),
() => idHash([getContract(contractName)?.address, methodName, args]),
// eslint-disable-next-line react-hooks/exhaustive-deps
[contractName, methodName, JSON.stringify(args)],
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { CollateralAssets } from '../CollateralAssets';
import { AssetRenderer } from 'app/components/AssetRenderer';
import { TxFeeCalculator } from '../TxFeeCalculator';
import { ErrorBadge } from 'app/components/Form/ErrorBadge';
import { calculateMinimumReturn } from '../../utils/marginUtils';
import { calculateSlippagePrice } from '../../utils/marginUtils';
import { SlippageForm } from '../SlippageForm';
import settingIcon from 'assets/images/settings-blue.svg';
import { ActionButton } from 'app/components/Form/ActionButton';
Expand Down Expand Up @@ -151,7 +151,10 @@ export const ClosePositionDialog: React.FC<IClosePositionDialogProps> = ({
...args,
);

const { minReturn } = calculateMinimumReturn(value.withdrawAmount, slippage);
const { minimumPrice } = calculateSlippagePrice(
value.withdrawAmount,
slippage,
);

const token = useMemo(
() => assetByTokenAddress(value.withdrawToken) || collateral,
Expand Down Expand Up @@ -268,8 +271,8 @@ export const ClosePositionDialog: React.FC<IClosePositionDialogProps> = ({
<div className="tw-font-semibold">
<LoadableValue
loading={loadingValue}
value={weiToAssetNumberFormat(minReturn, token)}
tooltip={weiToNumberFormat(minReturn, 18)}
value={weiToAssetNumberFormat(minimumPrice, token)}
tooltip={weiToNumberFormat(minimumPrice, 18)}
/>{' '}
<AssetRenderer asset={token} />
</div>
Expand Down
40 changes: 32 additions & 8 deletions src/app/pages/MarginTradePage/components/SlippageForm/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import classNames from 'classnames';
import { translations } from 'locales/i18n';
Expand All @@ -7,10 +7,15 @@ import { FormGroup } from 'app/components/Form/FormGroup';
import { AssetRenderer } from 'app/components/AssetRenderer';
import { Slider } from 'app/components/Form/Slider';
import { DummyInput } from 'app/components/Form/Input';
import { calculateMinimumReturn } from '../../utils/marginUtils';
import { calculateSlippagePrice } from '../../utils/marginUtils';
import styles from './dialog.module.scss';
import { weiToAssetNumberFormat } from 'utils/display-text/format';
import { sliderDefaultLabelValues } from 'app/components/Form/Slider/sliderDefaultLabelValues';
import { useSelector } from 'react-redux';
import { selectMarginTradePage } from '../../selectors';
import { TradingPosition } from 'types/trading-position';

const DEFAULT_ASSET = Asset.SOV;

interface ISlippageFormProps {
onClose: () => void;
Expand All @@ -30,7 +35,17 @@ export const SlippageForm: React.FC<ISlippageFormProps> = ({
isTrade,
}) => {
const { t } = useTranslation();
const { minReturn } = calculateMinimumReturn(amount, value);
const { position } = useSelector(selectMarginTradePage);
const { minimumPrice, maximumPrice } = calculateSlippagePrice(amount, value);

const isLongPosition = useMemo(() => position === TradingPosition.LONG, [
position,
]);

const slippagePrice = useMemo(
() => (isLongPosition && isTrade ? maximumPrice : minimumPrice),
[isLongPosition, isTrade, maximumPrice, minimumPrice],
);

return (
<div className="tw-rounded-3xl tw-absolute tw-inset-0 tw-bg-black tw-p-4">
Expand Down Expand Up @@ -61,11 +76,15 @@ export const SlippageForm: React.FC<ISlippageFormProps> = ({

{isTrade ? (
<LabelValuePair
label={t(translations.marginTradePage.tradeDialog.minEntry)}
label={t(
translations.marginTradePage.tradeDialog[
isLongPosition ? 'maxEntry' : 'minEntry'
],
)}
value={
<>
{weiToAssetNumberFormat(minReturn, asset || Asset.SOV)}{' '}
<AssetRenderer asset={asset || Asset.SOV} />
{weiToAssetNumberFormat(slippagePrice, asset || DEFAULT_ASSET)}{' '}
<AssetRenderer asset={asset || DEFAULT_ASSET} />
</>
}
className="tw-mt-5"
Expand All @@ -78,9 +97,14 @@ export const SlippageForm: React.FC<ISlippageFormProps> = ({
>
<DummyInput
value={
<>{weiToAssetNumberFormat(minReturn, asset || Asset.SOV)}</>
<>
{weiToAssetNumberFormat(
slippagePrice,
asset || DEFAULT_ASSET,
)}
</>
}
appendElem={<AssetRenderer asset={asset || Asset.SOV} />}
appendElem={<AssetRenderer asset={asset || DEFAULT_ASSET} />}
className="tw-h-10 tw-truncate"
data-action-id="margin-reviewTransaction-minimumEntryPrice"
/>
Expand Down
3 changes: 2 additions & 1 deletion src/app/pages/MarginTradePage/components/TradeForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export const TradeForm: React.FC<ITradeFormProps> = ({ pairType }) => {
loanToken,
collateralToken,
useLoanTokens,
quoteToken,
} = useTrading_resolvePairTokens(pair, position, collateral);
const {
value: estimations,
Expand Down Expand Up @@ -485,7 +486,7 @@ export const TradeForm: React.FC<ITradeFormProps> = ({ pairType }) => {
onClose={() => setOpenSlippage(false)}
amount={toWei(price)}
value={slippage}
asset={collateralToken}
asset={quoteToken}
onChange={setSlippage}
isTrade={true}
/>
Expand Down
21 changes: 11 additions & 10 deletions src/app/pages/MarginTradePage/utils/marginUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,16 @@ export const getExitTransactionHash = (
}
};

export const calculateMinimumReturn = (
export const calculateSlippagePrice = (
amount: string,
slippage: number = 0.1,
) => {
return {
amount,
slippage,
minReturn: bignumber(amount)
.sub(bignumber(amount).mul(slippage / 100))
.toFixed(0),
};
};
) => ({
amount,
slippage,
minimumPrice: bignumber(amount)
.sub(bignumber(amount).mul(slippage / 100))
.toFixed(0),
maximumPrice: bignumber(amount)
.add(bignumber(amount).mul(slippage / 100))
.toFixed(0),
});
1 change: 1 addition & 0 deletions src/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -1740,6 +1740,7 @@
"liquidationPrice": "Est. Liquidation price:",
"entryPrice": "Position Entry Price:",
"minEntry": "Minimum Entry Price: ",
"maxEntry": "Maximum Entry Price: ",
"minLiq": "Minimum Liquidation Price: ",
"interestAPR": "Interest APY",
"renewalDate": "Renewal Date:",
Expand Down

0 comments on commit 096291d

Please sign in to comment.