Skip to content

Commit

Permalink
improve handling large prices
Browse files Browse the repository at this point in the history
  • Loading branch information
mschneider committed Apr 16, 2024
1 parent e050696 commit 6125e7e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
29 changes: 24 additions & 5 deletions ts/client/src/test/market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,37 @@ async function testWatchMarket(): Promise<void> {

async function testMarketLots(): Promise<void> {
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');
}
}
Expand Down
2 changes: 1 addition & 1 deletion ts/client/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 6125e7e

Please sign in to comment.