diff --git a/ts/client/src/test/market.ts b/ts/client/src/test/market.ts index 03d835922..35bc97554 100644 --- a/ts/client/src/test/market.ts +++ b/ts/client/src/test/market.ts @@ -62,18 +62,37 @@ async function testWatchMarket(): Promise { async function testMarketLots(): Promise { const client = initReadOnlyOpenbookClient(); - const marketPk = new PublicKey( + const marketPk1 = new PublicKey( 'Hojg6SoyQAjXRBU4HtR48RB5YVfNzu2vwcLMK6xXPSJS', ); - const market = await Market.load(client, marketPk); + const market1 = await Market.load(client, marketPk1); + const tick1 = market1.tickSize.toNumber(); + if ('1' !== market1.priceUiToLots(tick1).toString()) { + throw new Error('price lot calculation rounds wrongly'); + } + if ('0' !== market1.priceUiToLots(.9 * tick1).toString()) { + throw new Error('price lot calculation rounds wrongly'); + } + if ('1' !== market1.priceUiToLots(1.9 * tick1).toString()) { + throw new Error('price lot calculation rounds wrongly'); + } + if ('10000000000' !== market1.priceUiToLots(1).toString()) { + throw new Error('price lot calculation rounds wrongly'); + } - if ('1' !== market.priceUiToLots(1e-10).toString()) { + const marketPk2 = new PublicKey('DBSZ24hqXS5o8djunrTzBsJUb1P8ZvBs1nng5rmZKsJt'); + const market2 = await Market.load(client, marketPk2); + const tick2 = market2.tickSize.toNumber(); + if ('1' !== market2.priceUiToLots(tick2).toString()) { + throw new Error('price lot calculation rounds wrongly'); + } + if ('0' !== market2.priceUiToLots(.9 * tick2).toString()) { throw new Error('price lot calculation rounds wrongly'); } - if ('0' !== market.priceUiToLots(9e-11).toString()) { + if ('1' !== market2.priceUiToLots(1.9 * tick2).toString()) { throw new Error('price lot calculation rounds wrongly'); } - if ('1' !== market.priceUiToLots(19e-11).toString()) { + if ('10000000000000' !== market2.priceUiToLots(1).toString()) { throw new Error('price lot calculation rounds wrongly'); } } diff --git a/ts/client/src/utils/utils.ts b/ts/client/src/utils/utils.ts index cac121773..c9e4fc77e 100644 --- a/ts/client/src/utils/utils.ts +++ b/ts/client/src/utils/utils.ts @@ -49,7 +49,7 @@ export function percentageToDecimal(percentage: number): number { } export function toNative(uiAmount: number, decimals: number): BN { - return new BN(Math.round(uiAmount * Math.pow(10, decimals))); + return new BN((uiAmount * Math.pow(10, decimals)).toFixed(0)); } export function toUiDecimals(nativeAmount: number, decimals: number): number {