Skip to content

Commit

Permalink
v7.2.6
Browse files Browse the repository at this point in the history
v7.2.6
  • Loading branch information
platschi authored Jun 15, 2023
2 parents 81f2bad + 7348635 commit 9870a97
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 69 deletions.
37 changes: 30 additions & 7 deletions sdk/services/futures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,11 +526,11 @@ export default class FuturesService {

public async getIsolatedTradePreview(
marketAddress: string,
marketKey: FuturesMarketKey,
orderType: ContractOrderType,
inputs: {
sizeDelta: Wei;
price: Wei;
skewAdjustedPrice: Wei;
leverageSide: PositionSide;
}
) {
Expand All @@ -542,12 +542,13 @@ export default class FuturesService {
this.sdk.context.walletAddress
);

return formatPotentialTrade(
details,
inputs.skewAdjustedPrice,
inputs.sizeDelta,
inputs.leverageSide
const skewAdjustedPrice = await this.getSkewAdjustedPrice(
inputs.price,
marketAddress,
marketKey
);

return formatPotentialTrade(details, skewAdjustedPrice, inputs.sizeDelta, inputs.leverageSide);
}

public async getCrossMarginTradePreview(
Expand All @@ -570,9 +571,15 @@ export default class FuturesService {
tradeParams.orderPrice.toBN()
);

const skewAdjustedPrice = await this.getSkewAdjustedPrice(
tradeParams.orderPrice,
marketAddress,
marketKey
);

return formatPotentialTrade(
preview,
tradeParams.orderPrice,
skewAdjustedPrice,
tradeParams.sizeDelta,
tradeParams.leverageSide
);
Expand Down Expand Up @@ -1179,6 +1186,22 @@ export default class FuturesService {
);
}

public getSkewAdjustedPrice = async (price: Wei, marketAddress: string, marketKey: string) => {
const marketContract = new EthCallContract(marketAddress, PerpsMarketABI);
const { PerpsV2MarketSettings } = this.sdk.context.multicallContracts;
if (!PerpsV2MarketSettings) throw new Error(UNSUPPORTED_NETWORK);

const [marketSkew, skewScale] = await this.sdk.context.multicallProvider.all([
marketContract.marketSkew(),
PerpsV2MarketSettings.skewScale(formatBytes32String(marketKey)),
]);

const skewWei = wei(marketSkew);
const scaleWei = wei(skewScale);

return price.mul(skewWei.div(scaleWei).add(1));
};

// Private methods

private getInternalFuturesMarket(marketAddress: string, marketKey: FuturesMarketKey) {
Expand Down
1 change: 0 additions & 1 deletion sdk/types/futures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,6 @@ export type FuturesPotentialTradeDetails<T = Wei> = {
showStatus: boolean;
statusMessage: string;
priceImpact: T;
slippageAmount: T;
exceedsPriceProtection: boolean;
};

Expand Down
19 changes: 5 additions & 14 deletions sdk/utils/futures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ export const mapFuturesPositions = (
);
};

// TODO: Move to app
export const serializePotentialTrade = (
preview: FuturesPotentialTradeDetails
): FuturesPotentialTradeDetails<string> => ({
Expand All @@ -284,9 +285,9 @@ export const serializePotentialTrade = (
leverage: preview.leverage.toString(),
notionalValue: preview.notionalValue.toString(),
priceImpact: preview.priceImpact.toString(),
slippageAmount: preview.slippageAmount.toString(),
});

// TODO: Move to app
export const unserializePotentialTrade = (
preview: FuturesPotentialTradeDetails<string>
): FuturesPotentialTradeDetails => ({
Expand All @@ -300,7 +301,6 @@ export const unserializePotentialTrade = (
leverage: wei(preview.leverage),
notionalValue: wei(preview.notionalValue),
priceImpact: wei(preview.priceImpact),
slippageAmount: wei(preview.slippageAmount),
});

export const formatDelayedOrder = (
Expand Down Expand Up @@ -337,24 +337,16 @@ export const formatDelayedOrder = (

export const formatPotentialTrade = (
preview: PostTradeDetailsResponse,
basePrice: Wei,
skewAdjustedPrice: Wei,
nativeSizeDelta: Wei,
leverageSide: PositionSide
) => {
const { fee, liqPrice, margin, price, size, status } = preview;

const tradeValueWithoutSlippage = wei(nativeSizeDelta).abs().mul(wei(basePrice));
const notionalValue = wei(size).mul(wei(basePrice));
const notionalValue = wei(size).mul(wei(price));
const leverage = margin.gt(0) ? notionalValue.div(wei(margin)) : ZERO_WEI;
const priceImpact = wei(price).sub(skewAdjustedPrice).div(skewAdjustedPrice).abs();

const priceImpact = wei(price).sub(basePrice).div(basePrice).abs();
const slippageDirection = nativeSizeDelta.gt(0)
? priceImpact.gt(0)
? -1
: nativeSizeDelta.lt(0)
? priceImpact.lt(0)
: -1
: 1;
return {
fee: wei(fee),
liqPrice: wei(liqPrice),
Expand All @@ -370,7 +362,6 @@ export const formatPotentialTrade = (
statusMessage: getTradeStatusMessage(status),
priceImpact: priceImpact,
exceedsPriceProtection: priceImpact.mul(100).gt(getDefaultPriceImpact('market')),
slippageAmount: priceImpact.mul(slippageDirection).mul(tradeValueWithoutSlippage),
};
};

Expand Down
4 changes: 3 additions & 1 deletion sections/futures/EditPositionModal/EditPositionSizeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ export default function EditPositionSizeModal() {

const resultingLeverage = useMemo(() => {
if (!preview || !position) return;
return preview.size.mul(marketPrice).div(position.remainingMargin).abs();
return position.remainingMargin.gt(0)
? preview.size.mul(marketPrice).div(position.remainingMargin).abs()
: wei(0);
}, [preview, position, marketPrice]);

const maxNativeIncreaseValue = useMemo(() => {
Expand Down
4 changes: 2 additions & 2 deletions sections/futures/LeverageInput/LeverageInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { editTradeSizeInput } from 'state/futures/actions';
import { setLeverageInput } from 'state/futures/reducer';
import {
selectLeverageInput,
selectMarketPrice,
selectMarketIndexPrice,
selectMaxLeverage,
selectPosition,
selectFuturesType,
Expand Down Expand Up @@ -44,7 +44,7 @@ const LeverageInput: FC = memo(() => {
const [mode, setMode] = useState<'slider' | 'input'>('input');
const position = useAppSelector(selectPosition);
const maxLeverage = useAppSelector(selectMaxLeverage);
const marketPrice = useAppSelector(selectMarketPrice);
const marketPrice = useAppSelector(selectMarketIndexPrice);
const leverageInput = useAppSelector(selectLeverageInput);
const futuresType = useAppSelector(selectFuturesType);
const crossMarginMarginDelta = useAppSelector(selectCrossMarginMarginDelta);
Expand Down
9 changes: 7 additions & 2 deletions sections/futures/MobileTrade/UserTabs/PositionsTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const PositionsTab = () => {
const markPrice = markPrices[market?.marketKey!] ?? ZERO_WEI;
return {
market: market!,
remainingMargin: position.remainingMargin,
position: position.position!,
avgEntryPrice: thisPositionHistory?.avgEntryPrice,
stopLoss: position.stopLoss?.targetPrice,
Expand Down Expand Up @@ -195,7 +196,7 @@ const PositionsTab = () => {
<PositionCell>
<Body color="secondary">Market Margin</Body>
<FlexDivRow justifyContent="start">
<NumericValue value={row.position.initialMargin} />
<NumericValue value={row.remainingMargin} />
{accountType === 'cross_margin' && (
<>
<Spacer width={5} />
Expand All @@ -215,7 +216,11 @@ const PositionsTab = () => {
</PositionCell>
<PositionCell>
<Body color="secondary">Liquidation</Body>
<Currency.Price price={row.position.liquidationPrice} colorType="preview" />
<Currency.Price
price={row.position.liquidationPrice}
formatOptions={{ suggestDecimals: true }}
colorType="preview"
/>
</PositionCell>
<PositionCell>
<Body color="secondary">Unrealized PnL</Body>
Expand Down
4 changes: 2 additions & 2 deletions sections/futures/OrderSizing/OrderSizing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { ZERO_WEI } from 'sdk/constants/number';
import { floorNumber, formatCryptoCurrency, formatDollars, isZero } from 'sdk/utils/number';
import { editTradeSizeInput } from 'state/futures/actions';
import {
selectMarketPrice,
selectMarketIndexPrice,
selectPosition,
selectTradeSizeInputs,
selectCrossMarginOrderPrice,
Expand All @@ -34,7 +34,7 @@ const OrderSizing: React.FC<OrderSizingProps> = memo(({ isMobile }) => {
const { susdSizeString, nativeSizeString } = useAppSelector(selectTradeSizeInputs);

const position = useAppSelector(selectPosition);
const marketAssetRate = useAppSelector(selectMarketPrice);
const marketAssetRate = useAppSelector(selectMarketIndexPrice);
const orderPrice = useAppSelector(selectCrossMarginOrderPrice);
const assetInputType = useAppSelector(selectSelectedInputDenomination);
const maxUsdInputAmount = useAppSelector(selectMaxUsdSizeInput);
Expand Down
4 changes: 2 additions & 2 deletions sections/futures/ProfitCalculator/ProfitCalculator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import styled from 'styled-components';
import BaseModal from 'components/BaseModal';
import { PositionSide } from 'sdk/types/futures';
import PositionButtons from 'sections/futures/PositionButtons';
import { selectMarketAsset, selectMarketPrice } from 'state/futures/selectors';
import { selectMarketAsset, selectMarketIndexPrice } from 'state/futures/selectors';
import { useAppSelector } from 'state/hooks';
import { getMarketName } from 'utils/futures';

Expand All @@ -24,7 +24,7 @@ const ProfitCalculator: FC<ProfitCalculatorProps> = memo(({ setOpenProfitCalcMod
const marketAsset__RemovedSChar = marketAsset[0] === 's' ? marketAsset.slice(1) : marketAsset;
const { t } = useTranslation();

const marketPrice = useAppSelector(selectMarketPrice);
const marketPrice = useAppSelector(selectMarketIndexPrice);

// Wei
const [entryPrice, setEntryPrice] = useState('');
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 @@ -14,7 +14,7 @@ import { setTradePanelDrawerOpen } from 'state/futures/reducer';
import {
selectMarketInfo,
selectIsMarketCapReached,
selectMarketPrice,
selectMarketIndexPrice,
selectPlaceOrderTranslationKey,
selectMaxLeverage,
selectTradePreviewError,
Expand Down Expand Up @@ -53,7 +53,7 @@ const ManagePosition: React.FC = () => {
const isMarketCapReached = useAppSelector(selectIsMarketCapReached);
const placeOrderTranslationKey = useAppSelector(selectPlaceOrderTranslationKey);
const orderPrice = useAppSelector(selectCrossMarginOrderPrice);
const marketAssetRate = useAppSelector(selectMarketPrice);
const marketAssetRate = useAppSelector(selectMarketIndexPrice);
const marketInfo = useAppSelector(selectMarketInfo);
const indexPrice = useAppSelector(selectMarketPriceInfo);
const previewStatus = useAppSelector(selectTradePreviewStatus);
Expand Down
4 changes: 2 additions & 2 deletions sections/futures/Trade/SLTPInputs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { setCrossMarginTradeStopLoss, setCrossMarginTradeTakeProfit } from 'stat
import {
selectLeverageInput,
selectLeverageSide,
selectMarketPrice,
selectMarketIndexPrice,
selectSlTpTradeInputs,
} from 'state/futures/selectors';
import { useAppDispatch, useAppSelector } from 'state/hooks';
Expand All @@ -30,7 +30,7 @@ const SL_OPTIONS = ['2%', '5%', '10%', '20%', '50%'];
export default function SLTPInputs() {
const dispatch = useAppDispatch();
const { takeProfitPrice, stopLossPrice } = useAppSelector(selectSlTpTradeInputs);
const currentPrice = useAppSelector(selectMarketPrice);
const currentPrice = useAppSelector(selectMarketIndexPrice);
const leverageSide = useAppSelector(selectLeverageSide);
const leverage = useAppSelector(selectLeverageInput);
const hideWarning = useAppSelector(selectAckedOrdersWarning);
Expand Down
4 changes: 2 additions & 2 deletions sections/futures/Trade/TradePanelPriceInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { editTradeOrderPrice } from 'state/futures/actions';
import {
selectCrossMarginOrderPrice,
selectLeverageSide,
selectMarketPrice,
selectMarketIndexPrice,
selectOrderType,
} from 'state/futures/selectors';
import { useAppDispatch, useAppSelector } from 'state/hooks';
Expand All @@ -14,7 +14,7 @@ import OrderPriceInput from '../OrderPriceInput';
export default function TradePanelPriceInput() {
const dispatch = useAppDispatch();

const marketPrice = useAppSelector(selectMarketPrice);
const marketPrice = useAppSelector(selectMarketIndexPrice);
const leverageSide = useAppSelector(selectLeverageSide);
const orderPrice = useAppSelector(selectCrossMarginOrderPrice);
const orderType = useAppSelector(selectOrderType);
Expand Down
Loading

1 comment on commit 9870a97

@vercel
Copy link

@vercel vercel bot commented on 9870a97 Jun 15, 2023

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.