From 9b3a41526095499915c9e784f6560bb829008f41 Mon Sep 17 00:00:00 2001 From: Christian Kamm Date: Fri, 19 Aug 2022 16:46:59 +0200 Subject: [PATCH] ts: separate price and uiPrice --- ts/client/src/accounts/bank.ts | 2 + ts/client/src/accounts/group.ts | 44 ++++++++++++++++++- ts/client/src/accounts/mangoAccount.ts | 11 +---- ts/client/src/debug-scripts/mb-debug-banks.ts | 4 +- 4 files changed, 49 insertions(+), 12 deletions(-) diff --git a/ts/client/src/accounts/bank.ts b/ts/client/src/accounts/bank.ts index a002024b1..cc05a9e62 100644 --- a/ts/client/src/accounts/bank.ts +++ b/ts/client/src/accounts/bank.ts @@ -26,6 +26,7 @@ export class Bank { public util0: I80F48; public util1: I80F48; public price: I80F48; + public uiPrice: number; public collectedFeesNative: I80F48; public loanFeeRate: I80F48; public loanOriginationFeeRate: I80F48; @@ -178,6 +179,7 @@ export class Bank { this.liquidationFee = I80F48.from(liquidationFee); this.dust = I80F48.from(dust); this.price = undefined; + this.uiPrice = undefined; } toString(): string { diff --git a/ts/client/src/accounts/group.ts b/ts/client/src/accounts/group.ts index 5485f317d..86d2b8a0b 100644 --- a/ts/client/src/accounts/group.ts +++ b/ts/client/src/accounts/group.ts @@ -9,6 +9,8 @@ import { Bank, MintInfo } from './bank'; import { I80F48, ONE_I80F48 } from './I80F48'; import { PerpMarket } from './perp'; import { Serum3Market } from './serum3'; +import { toNativeDecimals } from '../utils'; +import BN from 'bn.js'; export class Group { static from( @@ -225,6 +227,7 @@ export class Group { for (const bank of banks[index]) { if (bank.name === 'USDC') { bank.price = ONE_I80F48; + bank.uiPrice = 1; } else { // TODO: Implement switchboard oracle type if ( @@ -234,9 +237,17 @@ export class Group { ) { const stubOracle = coder.decode('stubOracle', price.data); bank.price = new I80F48(stubOracle.price.val); + bank.uiPrice = this?.toUiPrice( + bank.price, + bank.mint, + this?.insuranceMint, + ); } else { - bank.price = I80F48.fromNumber( - parsePriceData(price.data).previousPrice, + bank.uiPrice = parsePriceData(price.data).previousPrice; + bank.price = this?.toNativePrice( + bank.uiPrice, + bank.mint, + this?.insuranceMint, ); } } @@ -264,6 +275,35 @@ export class Group { } } + public toUiPrice( + price: I80F48, + tokenMintPk: PublicKey, + quoteMintPk: PublicKey, + ): number { + const tokenDecimals = this.getMintDecimals(tokenMintPk); + const quoteDecimals = this.getMintDecimals(quoteMintPk); + return price + .mul(I80F48.fromNumber(Math.pow(10, tokenDecimals - quoteDecimals))) + .toNumber(); + } + + public toNativePrice( + uiPrice: number, + tokenMintPk: PublicKey, + quoteMintPk: PublicKey, + ): I80F48 { + const tokenDecimals = this.getMintDecimals(tokenMintPk); + const quoteDecimals = this.getMintDecimals(quoteMintPk); + return I80F48.fromNumber(uiPrice).mul( + I80F48.fromNumber(Math.pow(10, quoteDecimals - tokenDecimals)), + ); + } + + public toNativeDecimals(uiAmount: number, mintPk: PublicKey): BN { + const decimals = this.getMintDecimals(mintPk); + return toNativeDecimals(uiAmount, decimals); + } + toString(): string { let res = 'Group\n'; res = res + ' pk: ' + this.publicKey.toString(); diff --git a/ts/client/src/accounts/mangoAccount.ts b/ts/client/src/accounts/mangoAccount.ts index c5fbac82a..870bbd27b 100644 --- a/ts/client/src/accounts/mangoAccount.ts +++ b/ts/client/src/accounts/mangoAccount.ts @@ -110,11 +110,7 @@ export class MangoAccount { nativeTokenPosition: TokenPosition, ): I80F48 { return nativeTokenPosition - ? nativeTokenPosition - .native(sourceBank) - .mul(I80F48.fromNumber(Math.pow(10, QUOTE_DECIMALS))) - .div(I80F48.fromNumber(Math.pow(10, sourceBank.mintDecimals))) - .mul(sourceBank.price) + ? nativeTokenPosition.native(sourceBank).mul(sourceBank.price) : ZERO_I80F48; } @@ -122,10 +118,7 @@ export class MangoAccount { targetBank: Bank, nativeUsdcPosition: I80F48, ): I80F48 { - return nativeUsdcPosition - .div(targetBank.price) - .div(I80F48.fromNumber(Math.pow(10, QUOTE_DECIMALS))) - .mul(I80F48.fromNumber(Math.pow(10, targetBank.mintDecimals))); + return nativeUsdcPosition.div(targetBank.price); } /** diff --git a/ts/client/src/debug-scripts/mb-debug-banks.ts b/ts/client/src/debug-scripts/mb-debug-banks.ts index 9e7bed3d4..5e1bba482 100644 --- a/ts/client/src/debug-scripts/mb-debug-banks.ts +++ b/ts/client/src/debug-scripts/mb-debug-banks.ts @@ -27,7 +27,7 @@ async function main() { const group = await client.getGroupForCreator(admin.publicKey, 2); console.log(`Group ${group.publicKey.toBase58()}`); - const banks = await client.getBanksForGroup(group); + const banks = Array.from(group.banksMapByMint.values()).flat(); const banksMapUsingTokenIndex = new Map( banks.map((bank) => { (bank as any).indexedDepositsByMangoAccounts = ZERO_I80F48; @@ -94,6 +94,8 @@ async function main() { `\n ${'bank'.padEnd(40)} ${bank.publicKey}` + `\n ${'vault'.padEnd(40)} ${bank.vault}` + `\n ${'mint'.padEnd(40)} ${bank.mint}` + + `\n ${'price'.padEnd(40)} ${bank.price.toNumber()}` + + `\n ${'uiPrice'.padEnd(40)} ${bank.uiPrice}` + `\n ${'error'.padEnd(40)} ${error}` + `\n ${'collectedFeesNative'.padEnd(40)} ${bank.collectedFeesNative}` + `\n ${'dust'.padEnd(40)} ${bank.dust}` +