From 7231ceea53dbb68b5dc9bccfa43a43977e46a22b Mon Sep 17 00:00:00 2001 From: Christian Kamm Date: Mon, 19 Dec 2022 13:20:23 +0100 Subject: [PATCH] ts client: Fix avg entry price and break even price --- ts/client/src/accounts/mangoAccount.ts | 16 ++++++---------- ts/client/src/accounts/perp.ts | 6 +++++- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/ts/client/src/accounts/mangoAccount.ts b/ts/client/src/accounts/mangoAccount.ts index e4dd61e84..4abe68d0f 100644 --- a/ts/client/src/accounts/mangoAccount.ts +++ b/ts/client/src/accounts/mangoAccount.ts @@ -6,7 +6,7 @@ import { MangoClient } from '../client'; import { OPENBOOK_PROGRAM_ID } from '../constants'; import { I80F48, I80F48Dto, ONE_I80F48, ZERO_I80F48 } from '../numbers/I80F48'; import { toNativeI80F48, toUiDecimals, toUiDecimalsForQuote } from '../utils'; -import { Bank, TokenIndex } from './bank'; +import { Bank, QUOTE_DECIMALS, TokenIndex } from './bank'; import { Group } from './group'; import { HealthCache } from './healthCache'; import { PerpMarket, PerpMarketIndex, PerpOrder, PerpOrderSide } from './perp'; @@ -1278,8 +1278,8 @@ export class PerpPosition { } public getAverageEntryPriceUi(perpMarket: PerpMarket): number { - return perpMarket.priceLotsToUi( - new BN(this.avgEntryPricePerBaseLot / perpMarket.baseLotSize.toNumber()), + return perpMarket.priceNativeToUi( + this.avgEntryPricePerBaseLot / perpMarket.baseLotSize.toNumber(), ); } @@ -1287,13 +1287,9 @@ export class PerpPosition { if (this.basePositionLots.eq(new BN(0))) { return 0; } - return perpMarket.priceLotsToUi( - new BN( - this.quoteRunningNative - .neg() - .div(this.basePositionLots.mul(perpMarket.baseLotSize)) - .toNumber(), - ), + return perpMarket.priceNativeToUi( + -this.quoteRunningNative.toNumber() / + this.basePositionLots.mul(perpMarket.baseLotSize).toNumber(), ); } diff --git a/ts/client/src/accounts/perp.ts b/ts/client/src/accounts/perp.ts index ddc1ccd57..ef4d0fa35 100644 --- a/ts/client/src/accounts/perp.ts +++ b/ts/client/src/accounts/perp.ts @@ -5,7 +5,7 @@ import Big from 'big.js'; import { MangoClient } from '../client'; import { I80F48, I80F48Dto, ZERO_I80F48 } from '../numbers/I80F48'; import { Modify } from '../types'; -import { As, toNative, U64_MAX_BN } from '../utils'; +import { As, toNative, toUiDecimals, U64_MAX_BN } from '../utils'; import { OracleConfig, OracleConfigDto, @@ -370,6 +370,10 @@ export class PerpMarket { return parseFloat(price.toString()) * this.priceLotsToUiConverter; } + public priceNativeToUi(price: number): number { + return toUiDecimals(price, QUOTE_DECIMALS - this.baseDecimals); + } + public baseLotsToUi(quantity: BN): number { return parseFloat(quantity.toString()) * this.baseLotsToUiConverter; }