Skip to content

Commit

Permalink
v6.0.6-alpha
Browse files Browse the repository at this point in the history
v6.0.6-alpha
  • Loading branch information
platschi authored Jan 30, 2023
2 parents 5d29485 + 9161c9c commit 67b00c9
Show file tree
Hide file tree
Showing 32 changed files with 232 additions and 161 deletions.
4 changes: 2 additions & 2 deletions constants/futures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { wei } from '@synthetixio/wei';

import { FuturesOrderType } from 'queries/futures/types';

export const ISOLATED_MARGIN_ORDER_TYPES: FuturesOrderType[] = ['delayed offchain', 'market'];
export const CROSS_MARGIN_ORDER_TYPES: FuturesOrderType[] = ['market', 'limit', 'stop market'];
export const ISOLATED_MARGIN_ORDER_TYPES: FuturesOrderType[] = ['delayedOffchain', 'market'];
export const CROSS_MARGIN_ORDER_TYPES: FuturesOrderType[] = ['market', 'limit', 'stopMarket'];
export const ORDER_KEEPER_ETH_DEPOSIT = wei(0.01);
export const DEFAULT_MAX_LEVERAGE = wei(10);
export const DEFAULT_DELAYED_LEVERAGE_CAP = wei(100);
Expand Down
3 changes: 2 additions & 1 deletion constants/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export const EXTERNAL_LINKS = {
NextPriceBlogPost: 'https://docs.kwenta.io/products/futures/next-price',
},
Governance: {
Kips: 'https://kips.kwenta.io/all-kip/',
Kips:
'https://app.radicle.xyz/seeds/maple.radicle.garden/rad:git:hnrkq1oex148yz4zi9tm7spfnjaryyuc93yay/tree/master',
Vote: 'https://snapshot.org/#/kwenta.eth',
},
Competition: {
Expand Down
2 changes: 1 addition & 1 deletion hooks/useFuturesData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ const useFuturesData = () => {
switch (orderType) {
case 'limit':
return limitOrderFee;
case 'stop market':
case 'stopMarket':
return stopOrderFee;
default:
return zeroBN;
Expand Down
8 changes: 1 addition & 7 deletions queries/futures/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,7 @@ export type FuturesTradeInputs = {
orderPrice?: Wei | undefined;
};

export type FuturesOrderType =
| 'market'
| 'next price'
| 'stop market'
| 'limit'
| 'delayed'
| 'delayed offchain';
export type FuturesOrderType = 'market' | 'stopMarket' | 'limit' | 'delayed' | 'delayedOffchain';

export type SpotsFee = {
timestamp: string;
Expand Down
4 changes: 4 additions & 0 deletions sdk/contracts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import ExchangeRatesABI from './abis/ExchangeRates.json';
import FuturesMarketDataABI from './abis/FuturesMarketData.json';
import FuturesMarketSettingsABI from './abis/FuturesMarketSettings.json';
import KwentaStakingRewardsABI from './abis/KwentaStakingRewards.json';
import PerpsV2MarketABI from './abis/PerpsV2Market.json';
import PerpsV2MarketDataABI from './abis/PerpsV2MarketData.json';
import PerpsV2MarketSettingsABI from './abis/PerpsV2MarketSettings.json';
import StakingRewardsABI from './abis/StakingRewards.json';
Expand Down Expand Up @@ -51,6 +52,9 @@ export type AllContractsMap = Record<
{ addresses: Partial<Record<NetworkId, string>>; Factory: ContractFactory }
>;

export const getPerpsV2MarketMulticall = (marketAddress: string) =>
new EthCallContract(marketAddress, PerpsV2MarketABI);

export const getContractsByNetwork = (
networkId: NetworkId,
provider: ethers.providers.Provider | ethers.Signer
Expand Down
33 changes: 22 additions & 11 deletions sdk/services/futures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import { mapFuturesOrders } from 'queries/futures/utils';
import { UNSUPPORTED_NETWORK } from 'sdk/common/errors';
import { BPS_CONVERSION, DEFAULT_DESIRED_TIMEDELTA } from 'sdk/constants/futures';
import { Period, PERIOD_IN_SECONDS } from 'sdk/constants/period';
import { getContractsByNetwork } from 'sdk/contracts';
import { getContractsByNetwork, getPerpsV2MarketMulticall } from 'sdk/contracts';
import FuturesMarketABI from 'sdk/contracts/abis/FuturesMarket.json';
import FuturesMarketInternal from 'sdk/contracts/FuturesMarketInternal';
import {
CrossMarginBase__factory,
PerpsV2MarketData,
PerpsV2Market__factory,
} from 'sdk/contracts/types';
import { IPerpsV2MarketConsolidated } from 'sdk/contracts/types/PerpsV2Market';
import { IPerpsV2MarketSettings } from 'sdk/contracts/types/PerpsV2MarketData';
import { NetworkOverrideOptions } from 'sdk/types/common';
import {
Expand Down Expand Up @@ -247,14 +248,12 @@ export default class FuturesService {
)) as boolean[];

// map the positions using the results
const positions = positionDetails
.map((position, ind) => {
const canLiquidate = canLiquidateState[ind];
const marketKey = futuresMarkets[ind].marketKey;
const asset = futuresMarkets[ind].asset;
return mapFuturesPosition(position, canLiquidate, asset, marketKey);
})
.filter(({ remainingMargin }) => remainingMargin.gt(0));
const positions = positionDetails.map((position, ind) => {
const canLiquidate = canLiquidateState[ind];
const marketKey = futuresMarkets[ind].marketKey;
const asset = futuresMarkets[ind].asset;
return mapFuturesPosition(position, canLiquidate, asset, marketKey);
});

return positions;
}
Expand Down Expand Up @@ -511,11 +510,23 @@ export default class FuturesService {

// Perps V2 read functions
public async getDelayedOrder(account: string, marketAddress: string) {
const market = PerpsV2Market__factory.connect(marketAddress, this.sdk.context.signer);
const market = PerpsV2Market__factory.connect(marketAddress, this.sdk.context.provider);
const order = await market.delayedOrders(account);
return formatDelayedOrder(account, marketAddress, order);
}

public async getDelayedOrders(account: string, marketAddresses: string[]) {
const marketContracts = marketAddresses.map(getPerpsV2MarketMulticall);

const orders = (await this.sdk.context.multicallProvider.all(
marketContracts.map((market) => market.delayedOrders(account))
)) as IPerpsV2MarketConsolidated.DelayedOrderStructOutput[];

return orders.map((order, ind) => {
return formatDelayedOrder(account, marketAddresses[ind], order);
});
}

public async getIsolatedTradePreview(
marketAddress: string,
orderType: OrderType,
Expand All @@ -526,7 +537,7 @@ export default class FuturesService {
leverageSide: PositionSide;
}
) {
const market = PerpsV2Market__factory.connect(marketAddress, this.sdk.context.signer);
const market = PerpsV2Market__factory.connect(marketAddress, this.sdk.context.provider);
const details = await market.postTradeDetails(
inputs.sizeDelta.toBN(),
inputs.price.toBN(),
Expand Down
6 changes: 3 additions & 3 deletions sdk/types/futures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export type FuturesMarket<T = Wei> = {
makerFeeOffchainDelayedOrder: T;
takerFeeOffchainDelayedOrder: T;
};
openInterest?: {
openInterest: {
shortPct: number;
longPct: number;
shortUSD: T;
Expand Down Expand Up @@ -169,13 +169,13 @@ export enum OrderType {
export const OrderNameByType: Record<OrderType, string> = {
[OrderType.MARKET]: 'market',
[OrderType.DELAYED]: 'delayed',
[OrderType.DELAYED_OFFCHAIN]: 'delayed offchain',
[OrderType.DELAYED_OFFCHAIN]: 'delayedOffchain',
};

export const OrderTypeByName: Record<string, OrderType> = {
market: OrderType.MARKET,
delayed: OrderType.DELAYED,
'delayed offchain': OrderType.DELAYED_OFFCHAIN,
delayedOffchain: OrderType.DELAYED_OFFCHAIN,
};

export type FuturesFilledPosition<T = Wei> = {
Expand Down
2 changes: 1 addition & 1 deletion sdk/utils/futures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ export const calculateCrossMarginFee = (
susdSize: Wei,
feeRates: CrossMarginSettings
) => {
if (orderType !== 'limit' && orderType !== 'stop market') return zeroBN;
if (orderType !== 'limit' && orderType !== 'stopMarket') return zeroBN;
const advancedOrderFeeRate =
orderType === 'limit' ? feeRates.limitOrderFee : feeRates.stopOrderFee;
return susdSize.mul(advancedOrderFeeRate);
Expand Down
24 changes: 14 additions & 10 deletions sections/dashboard/FuturesMarketsTable/FuturesMarketsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ import { DEFAULT_CRYPTO_DECIMALS } from 'constants/defaults';
import ROUTES from 'constants/routes';
import Connector from 'containers/Connector';
import { getDisplayAsset } from 'sdk/utils/futures';
import { selectFuturesType, selectMarkets, selectMarketVolumes } from 'state/futures/selectors';
import {
selectFuturesType,
selectMarkets,
selectMarketVolumes,
selectMarkPrices,
} from 'state/futures/selectors';
import { useAppSelector } from 'state/hooks';
import { selectPrices } from 'state/prices/selectors';
import { pastRatesState } from 'store/futures';
import { getSynthDescription, MarketKeyByAsset, FuturesMarketAsset } from 'utils/futures';

Expand All @@ -30,14 +34,14 @@ const FuturesMarketsTable: FC = () => {
const pastRates = useRecoilValue(pastRatesState);
const futuresVolumes = useAppSelector(selectMarketVolumes);
const accountType = useAppSelector(selectFuturesType);
const prices = useAppSelector(selectPrices);
const markPrices = useAppSelector(selectMarkPrices);

let data = useMemo(() => {
return futuresMarkets.map((market) => {
const description = getSynthDescription(market.asset, synthsMap, t);
const volume = futuresVolumes[market.marketKey]?.volume;
const pastPrice = pastRates.find((price) => price.synth === getDisplayAsset(market.asset));
const marketPrice = prices[market.asset]?.offChain ?? prices[market.asset]?.onChain ?? wei(0);
const marketPrice = markPrices[market.marketKey] ?? wei(0);

return {
asset: market.asset,
Expand All @@ -48,17 +52,17 @@ const FuturesMarketsTable: FC = () => {
volume: volume?.toNumber() ?? 0,
pastPrice: pastPrice?.price,
priceChange: pastPrice?.price && marketPrice.sub(pastPrice?.price).div(marketPrice),
fundingRate: market.currentFundingRate.div(marketPrice) ?? null,
fundingRate: market.currentFundingRate ?? null,
openInterest: market.marketSize.mul(marketPrice),
openInterestNative: market.marketSize,
longInterest: market.marketSize.add(market.marketSkew).div('2').abs().mul(marketPrice),
shortInterest: market.marketSize.sub(market.marketSkew).div('2').abs().mul(marketPrice),
longInterest: market.openInterest.longUSD,
shortInterest: market.openInterest.shortUSD,
marketSkew: market.marketSkew,
isSuspended: market.isSuspended,
marketClosureReason: market.marketClosureReason,
};
});
}, [synthsMap, futuresMarkets, pastRates, futuresVolumes, prices, t]);
}, [synthsMap, futuresMarkets, pastRates, futuresVolumes, markPrices, t]);

return (
<>
Expand Down Expand Up @@ -103,10 +107,10 @@ const FuturesMarketsTable: FC = () => {
{
Header: (
<TableHeader>
{t('dashboard.overview.futures-markets-table.oracle-price')}
{t('dashboard.overview.futures-markets-table.mark-price')}
</TableHeader>
),
accessor: 'oraclePrice',
accessor: 'price',
Cell: (cellProps: CellProps<typeof data[number]>) => {
const formatOptions = {
minDecimals: DEFAULT_CRYPTO_DECIMALS,
Expand Down
Loading

0 comments on commit 67b00c9

Please sign in to comment.