Skip to content

Commit

Permalink
v5.2.1
Browse files Browse the repository at this point in the history
v5.2.1
  • Loading branch information
platschi authored Dec 1, 2022
2 parents f623452 + 086fa14 commit 400c4f1
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 13 deletions.
5 changes: 2 additions & 3 deletions hooks/useFuturesData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ import { KWENTA_TRACKING_CODE, ORDER_PREVIEW_ERRORS } from 'queries/futures/cons
import { PositionSide, FuturesTradeInputs, FuturesAccountType } from 'queries/futures/types';
import useGetFuturesPotentialTradeDetails from 'queries/futures/useGetFuturesPotentialTradeDetails';
import { setFuturesAccountType, setOrderType as setReduxOrderType } from 'state/futures/reducer';
import { selectMarketAssetRate } from 'state/futures/selectors';
import { selectMarketAssetRate, selectMaxLeverage } from 'state/futures/selectors';
import { selectMarketAsset, selectMarketInfo } from 'state/futures/selectors';
import { useAppSelector, useAppDispatch } from 'state/hooks';
import {
crossMarginMarginDeltaState,
tradeFeesState,
futuresAccountState,
leverageSideState,
maxLeverageState,
orderTypeState,
positionState,
futuresTradeInputsState,
Expand Down Expand Up @@ -99,7 +98,7 @@ const useFuturesData = () => {
const feeCap = useRecoilValue(orderFeeCapState);
const position = useRecoilValue(positionState);
const aboveMaxLeverage = useRecoilValue(aboveMaxLeverageState);
const maxLeverage = useRecoilValue(maxLeverageState);
const maxLeverage = useAppSelector(selectMaxLeverage);
const { crossMarginAvailable, crossMarginAddress } = useRecoilValue(futuresAccountState);
const { tradeFee: crossMarginTradeFee, stopOrderFee, limitOrderFee } = useRecoilValue(
crossMarginSettingsState
Expand Down
5 changes: 2 additions & 3 deletions sections/futures/LeverageInput/LeverageInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import Button from 'components/Button';
import CustomNumericInput from 'components/Input/CustomNumericInput';
import { DEFAULT_FIAT_DECIMALS } from 'constants/defaults';
import { useFuturesContext } from 'contexts/FuturesContext';
import { selectMarketInfo } from 'state/futures/selectors';
import { selectMarketInfo, selectMaxLeverage } from 'state/futures/selectors';
import { useAppSelector } from 'state/hooks';
import {
leverageValueCommittedState,
maxLeverageState,
nextPriceDisclaimerState,
orderTypeState,
positionState,
Expand All @@ -26,13 +25,13 @@ const LeverageInput: FC = () => {
const { t } = useTranslation();
const [mode, setMode] = useState<'slider' | 'input'>('input');
const { leverage } = useRecoilValue(futuresTradeInputsState);
const maxLeverage = useRecoilValue(maxLeverageState);
const orderType = useRecoilValue(orderTypeState);
const isDisclaimerDisplayed = useRecoilValue(nextPriceDisclaimerState);
const setIsLeverageValueCommitted = useSetRecoilState(leverageValueCommittedState);
const position = useRecoilValue(positionState);

const marketInfo = useAppSelector(selectMarketInfo);
const maxLeverage = useAppSelector(selectMaxLeverage);

const { onLeverageChange } = useFuturesContext();

Expand Down
5 changes: 2 additions & 3 deletions sections/futures/MarketInfoBox/MarketInfoBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import styled from 'styled-components';
import InfoBox from 'components/InfoBox';
import PreviewArrow from 'components/PreviewArrow';
import { FuturesPotentialTradeDetails } from 'queries/futures/types';
import { selectMarketInfo } from 'state/futures/selectors';
import { selectMarketInfo, selectMaxLeverage } from 'state/futures/selectors';
import { useAppSelector } from 'state/hooks';
import {
leverageSideState,
maxLeverageState,
orderTypeState,
positionState,
potentialTradeDetailsState,
Expand All @@ -22,14 +21,14 @@ import { formatDollars, formatPercent, zeroBN } from 'utils/formatters/number';
import { PositionSide } from '../types';

const MarketInfoBox: React.FC = () => {
const maxLeverage = useRecoilValue(maxLeverageState);
const position = useRecoilValue(positionState);
const orderType = useRecoilValue(orderTypeState);
const leverageSide = useRecoilValue(leverageSideState);
const { nativeSize } = useRecoilValue(futuresTradeInputsState);
const potentialTrade = useRecoilValue(potentialTradeDetailsState);

const marketInfo = useAppSelector(selectMarketInfo);
const maxLeverage = useAppSelector(selectMaxLeverage);

const totalMargin = position?.remainingMargin ?? zeroBN;
const availableMargin = position?.accessibleMargin ?? zeroBN;
Expand Down
7 changes: 5 additions & 2 deletions sections/futures/OrderSizing/OrderSizeSlider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import styled from 'styled-components';
import ErrorView from 'components/Error';
import StyledSlider from 'components/Slider/StyledSlider';
import { useFuturesContext } from 'contexts/FuturesContext';
import { selectMaxLeverage } from 'state/futures/selectors';
import { useAppSelector } from 'state/hooks';
import {
aboveMaxLeverageState,
crossMarginAccountOverviewState,
futuresTradeInputsState,
leverageSideState,
maxLeverageState,
positionState,
} from 'store/futures';
import { FlexDivRow } from 'styles/common';
Expand All @@ -24,13 +25,15 @@ export default function OrderSizeSlider() {
const { freeMargin: freeCrossMargin } = useRecoilValue(crossMarginAccountOverviewState);
const { susdSize } = useRecoilValue(futuresTradeInputsState);
const aboveMaxLeverage = useRecoilValue(aboveMaxLeverageState);
const maxLeverage = useRecoilValue(maxLeverageState);

const position = useRecoilValue(positionState);
const leverageSide = useRecoilValue(leverageSideState);

const [percent, setPercent] = useState(0);
const [usdValue, setUsdValue] = useState(susdSize);

const maxLeverage = useAppSelector(selectMaxLeverage);

// eslint-disable-next-line
const onChangeMarginPercent = useCallback(
(value, commit = false) => {
Expand Down
4 changes: 2 additions & 2 deletions sections/futures/Trade/ManagePosition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import {
selectIsMarketCapReached,
selectMarketAssetRate,
selectPlaceOrderTranslationKey,
selectMaxLeverage,
} from 'state/futures/selectors';
import { useAppDispatch, useAppSelector } from 'state/hooks';
import {
confirmationModalOpenState,
leverageSideState,
maxLeverageState,
orderTypeState,
positionState,
potentialTradeDetailsState,
Expand Down Expand Up @@ -57,7 +57,6 @@ const ManagePosition: React.FC = () => {
const sizeDelta = useRecoilValue(sizeDeltaState);
const marginDelta = useRecoilValue(crossMarginMarginDeltaState);
const position = useRecoilValue(positionState);
const maxLeverageValue = useRecoilValue(maxLeverageState);
const selectedAccountType = useRecoilValue(futuresAccountTypeState);
const { data: previewTrade, error: previewError, status } = useRecoilValue(
potentialTradeDetailsState
Expand All @@ -77,6 +76,7 @@ const ManagePosition: React.FC = () => {
const isAdvancedOrder = useRecoilValue(isAdvancedOrderState);

const marketInfo = useAppSelector(selectMarketInfo);
const maxLeverageValue = useAppSelector(selectMaxLeverage);

const [isCancelModalOpen, setCancelModalOpen] = React.useState(false);

Expand Down
29 changes: 29 additions & 0 deletions state/futures/selectors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { createSelector } from '@reduxjs/toolkit';
import { wei } from '@synthetixio/wei';

import { DEFAULT_NP_LEVERAGE_ADJUSTMENT } from 'constants/defaults';
import { DEFAULT_MAX_LEVERAGE } from 'constants/futures';
import { FuturesPosition } from 'queries/futures/types';
import { PositionSide } from 'sections/futures/types';
import { selectExchangeRates } from 'state/exchange/selectors';
Expand Down Expand Up @@ -117,6 +119,33 @@ export const selectPosition = createSelector(
}
);

export const selectMaxLeverage = createSelector(
(state: RootState) => state.futures,
selectPosition,
selectMarketInfo,
(futures, position, market) => {
const selectedAccountType = accountType(futures.selectedType);
const leverageSide = futures[selectedAccountType].leverageSide;
const orderType = futures[selectedAccountType].orderType;

const positionLeverage = position?.position?.leverage ?? wei(0);
const positionSide = position?.position?.side;
const marketMaxLeverage = market?.maxLeverage ?? DEFAULT_MAX_LEVERAGE;
const adjustedMaxLeverage =
orderType === 'next price'
? marketMaxLeverage.mul(DEFAULT_NP_LEVERAGE_ADJUSTMENT)
: marketMaxLeverage;

if (!positionLeverage || positionLeverage.eq(wei(0))) return adjustedMaxLeverage;
if (selectedAccountType === 'crossMargin') return adjustedMaxLeverage;
if (positionSide === leverageSide) {
return adjustedMaxLeverage?.sub(positionLeverage);
} else {
return positionLeverage.add(adjustedMaxLeverage);
}
}
);

export const selectPlaceOrderTranslationKey = createSelector(
selectPosition,
(state: RootState) => state.futures[accountType(state.futures.selectedType)].orderType,
Expand Down

1 comment on commit 400c4f1

@vercel
Copy link

@vercel vercel bot commented on 400c4f1 Dec 1, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

kwenta – ./

kwenta-git-main-kwenta.vercel.app
kwenta.io
kwenta-kwenta.vercel.app

Please sign in to comment.