Skip to content

refactor(common-ts): add step and tick size for ui market #189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 91 additions & 4 deletions common-ts/src/types/UIMarket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,24 @@ import {
PerpMarkets,
SpotMarkets,
SpotMarketConfig,
SpotMarketAccount,
PerpMarketAccount,
BN,
BigNum,
BASE_PRECISION_EXP,
QUOTE_PRECISION_EXP,
} from '@drift-labs/sdk';
import { MarketId } from './MarketId';
import invariant from 'tiny-invariant';
import { USDC_SPOT_MARKET_INDEX } from '../constants';
import { ENUM_UTILS } from '../utils';
import { Config } from '../Config';
import { MarketAccount } from '../types';

const useAsyncMarketConfigs =
process.env.NEXT_PUBLIC_USE_ASYNC_MARKET_CONFIGS === 'true';

export class UIMarket {
export abstract class UIMarket {
static perpMarkets = PerpMarkets['mainnet-beta'];
static spotMarkets = SpotMarkets['mainnet-beta'];
static perpMarketIds = PerpMarkets['mainnet-beta'].map((m) =>
Expand Down Expand Up @@ -63,16 +70,24 @@ export class UIMarket {
);
}

static create(marketIndex: number, marketType: MarketType) {
return marketType === MarketType.PERP
? new PerpUIMarket(marketIndex)
: new SpotUIMarket(marketIndex);
}

static createSpotMarket(marketIndex: number) {
return new UIMarket(marketIndex, MarketType.SPOT);
return new SpotUIMarket(marketIndex);
}

static createPerpMarket(marketIndex: number) {
return new UIMarket(marketIndex, MarketType.PERP);
return new PerpUIMarket(marketIndex);
}

static fromMarketId(marketId: MarketId) {
return new UIMarket(marketId.marketIndex, marketId.marketType);
return marketId.isPerp
? new PerpUIMarket(marketId.marketIndex)
: new SpotUIMarket(marketId.marketIndex);
}

static checkIsPredictionMarket(marketConfig: PerpMarketConfig) {
Expand Down Expand Up @@ -140,4 +155,76 @@ export class UIMarket {

return baseAssetSymbol;
}

protected geDisplayDpFromSize(size: BN, precisionExp: BN) {
const formattedSize = BigNum.from(size, precisionExp).prettyPrint();
if (formattedSize.includes('.')) {
return formattedSize.split('.')[1].length;
}
return 0;
}

abstract baseDisplayDp(marketAccount: MarketAccount): number;

abstract priceDisplayDp(marketAccount: MarketAccount): number;

abstract getStepSize(marketAccount: MarketAccount): BN;

abstract getTickSize(marketAccount: MarketAccount): BN;
}

export class PerpUIMarket extends UIMarket {
constructor(marketIndex: number) {
super(marketIndex, MarketType.PERP);
}

baseDisplayDp(marketAccount: PerpMarketAccount) {
return this.geDisplayDpFromSize(
(marketAccount as PerpMarketAccount).amm.orderStepSize,
BASE_PRECISION_EXP
);
}

priceDisplayDp(marketAccount: PerpMarketAccount) {
return this.geDisplayDpFromSize(
(marketAccount as PerpMarketAccount).amm.orderTickSize,
QUOTE_PRECISION_EXP
);
}

getStepSize(marketAccount: PerpMarketAccount) {
return (marketAccount as PerpMarketAccount).amm.orderStepSize;
}

getTickSize(marketAccount: PerpMarketAccount) {
return (marketAccount as PerpMarketAccount).amm.orderTickSize;
}
}

export class SpotUIMarket extends UIMarket {
constructor(marketIndex: number) {
super(marketIndex, MarketType.SPOT);
}

baseDisplayDp(marketAccount: SpotMarketAccount) {
return this.geDisplayDpFromSize(
(marketAccount as SpotMarketAccount).orderStepSize,
(this.market as SpotMarketConfig).precisionExp
);
}

priceDisplayDp(marketAccount: SpotMarketAccount) {
return this.geDisplayDpFromSize(
(marketAccount as SpotMarketAccount).orderTickSize,
QUOTE_PRECISION_EXP
);
}

getStepSize(marketAccount: SpotMarketAccount) {
return (marketAccount as SpotMarketAccount).orderStepSize;
}

getTickSize(marketAccount: SpotMarketAccount) {
return (marketAccount as SpotMarketAccount).orderTickSize;
}
}
4 changes: 4 additions & 0 deletions common-ts/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import {
BN,
BigNum,
MarketType,
PerpMarketAccount,
SpotBalanceType,
SpotMarketAccount,
SpotPosition,
TradeSide,
} from '@drift-labs/sdk';
Expand Down Expand Up @@ -273,3 +275,5 @@ export type TradeOffsetPrice =
| 'mark'
| 'entry'
| 'bestOffer';

export type MarketAccount = PerpMarketAccount | SpotMarketAccount;