Merge remote-tracking branch 'origin/main' into dev

This commit is contained in:
Christian Kamm 2022-08-20 11:13:55 +02:00
commit 1132853aab
4 changed files with 49 additions and 12 deletions

View File

@ -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 {

View File

@ -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();

View File

@ -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);
}
/**

View File

@ -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}` +