Skip to content

Commit

Permalink
read quote decimals from market acc
Browse files Browse the repository at this point in the history
  • Loading branch information
skrrb committed Jan 29, 2024
1 parent 2177e65 commit caef9ee
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
10 changes: 5 additions & 5 deletions ts/client/src/market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
getProvider,
BN,
} from '@coral-xyz/anchor';
import { QUOTE_DECIMALS, toNative, toUiDecimals } from './utils/utils';
import { toNative, toUiDecimals } from './utils/utils';
import Big from 'big.js';
import { IDL, type OpenbookV2 } from './openbook_v2';
const BATCH_TX_SIZE = 50;
Expand Down Expand Up @@ -124,7 +124,7 @@ export async function findAllMarkets(

function priceLotsToUiConverter(market: MarketAccount): number {
return new Big(10)
.pow(market.baseDecimals - QUOTE_DECIMALS)
.pow(market.baseDecimals - market.quoteDecimals)
.mul(new Big(market.quoteLotSize.toString()))
.div(new Big(market.baseLotSize.toString()))
.toNumber();
Expand All @@ -142,7 +142,7 @@ function quoteLotsToUiConverter(market: MarketAccount): number {
}

export function uiPriceToLots(market: MarketAccount, price: number): BN {
return toNative(price, QUOTE_DECIMALS)
return toNative(price, market.quoteDecimals)
.mul(market.baseLotSize)
.div(market.quoteLotSize.mul(new BN(Math.pow(10, market.baseDecimals))));
}
Expand All @@ -152,7 +152,7 @@ export function uiBaseToLots(market: MarketAccount, quantity: number): BN {
}

export function uiQuoteToLots(market: MarketAccount, uiQuote: number): BN {
return toNative(uiQuote, QUOTE_DECIMALS).div(market.quoteLotSize);
return toNative(uiQuote, market.quoteDecimals).div(market.quoteLotSize);
}

export function priceLotsToNative(market: MarketAccount, price: BN): BN {
Expand All @@ -164,7 +164,7 @@ export function priceLotsToUi(market: MarketAccount, price: BN): number {
}

export function priceNativeToUi(market: MarketAccount, price: number): number {
return toUiDecimals(price, QUOTE_DECIMALS - market.baseDecimals);
return toUiDecimals(price, market.quoteDecimals - market.baseDecimals);
}

export function baseLotsToUi(market: MarketAccount, quantity: BN): number {
Expand Down
4 changes: 0 additions & 4 deletions ts/client/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ export function toUiDecimals(nativeAmount: number, decimals: number): number {

export const QUOTE_DECIMALS = 6;

export function toUiDecimalsForQuote(nativeAmount: number): number {
return toUiDecimals(nativeAmount, QUOTE_DECIMALS);
}

///

///
Expand Down

0 comments on commit caef9ee

Please sign in to comment.