ts: serum display function for max leverage (#202)
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com> Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
parent
cec0fcab99
commit
0c6cc16023
|
@ -278,13 +278,13 @@ export class HealthCache {
|
|||
healthType: HealthType = HealthType.init,
|
||||
): I80F48 {
|
||||
const adjustedCache: HealthCache = _.cloneDeep(this);
|
||||
const quoteBanks = group.banksMapByTokenIndex.get(
|
||||
const quoteBank = group.getFirstBankByTokenIndex(
|
||||
serum3Market.quoteTokenIndex,
|
||||
);
|
||||
if (!quoteBanks) {
|
||||
if (!quoteBank) {
|
||||
throw new Error(`No bank for index ${serum3Market.quoteTokenIndex}`);
|
||||
}
|
||||
const quoteIndex = adjustedCache.getOrCreateTokenInfoIndex(quoteBanks[0]);
|
||||
const quoteIndex = adjustedCache.getOrCreateTokenInfoIndex(quoteBank);
|
||||
const quote = adjustedCache.tokenInfos[quoteIndex];
|
||||
|
||||
// Move token balance to reserved funds in open orders,
|
||||
|
@ -315,13 +315,13 @@ export class HealthCache {
|
|||
healthType: HealthType = HealthType.init,
|
||||
): I80F48 {
|
||||
const adjustedCache: HealthCache = _.cloneDeep(this);
|
||||
const baseBanks = group.banksMapByTokenIndex.get(
|
||||
const baseBank = group.getFirstBankByTokenIndex(
|
||||
serum3Market.baseTokenIndex,
|
||||
);
|
||||
if (!baseBanks) {
|
||||
if (!baseBank) {
|
||||
throw new Error(`No bank for index ${serum3Market.quoteTokenIndex}`);
|
||||
}
|
||||
const baseIndex = adjustedCache.getOrCreateTokenInfoIndex(baseBanks[0]);
|
||||
const baseIndex = adjustedCache.getOrCreateTokenInfoIndex(baseBank);
|
||||
const base = adjustedCache.tokenInfos[baseIndex];
|
||||
|
||||
// Move token balance to reserved funds in open orders,
|
||||
|
@ -537,24 +537,24 @@ export class HealthCache {
|
|||
side: Serum3Side,
|
||||
minRatio: I80F48,
|
||||
) {
|
||||
const baseBanks = group.banksMapByTokenIndex.get(
|
||||
const baseBank = group.getFirstBankByTokenIndex(
|
||||
serum3Market.baseTokenIndex,
|
||||
);
|
||||
if (!baseBanks) {
|
||||
if (!baseBank) {
|
||||
throw new Error(`No bank for index ${serum3Market.baseTokenIndex}`);
|
||||
}
|
||||
const quoteBanks = group.banksMapByTokenIndex.get(
|
||||
const quoteBank = group.getFirstBankByTokenIndex(
|
||||
serum3Market.quoteTokenIndex,
|
||||
);
|
||||
if (!quoteBanks) {
|
||||
if (!quoteBank) {
|
||||
throw new Error(`No bank for index ${serum3Market.quoteTokenIndex}`);
|
||||
}
|
||||
|
||||
const healthCacheClone: HealthCache = _.cloneDeep(this);
|
||||
|
||||
const baseIndex = healthCacheClone.getOrCreateTokenInfoIndex(baseBanks[0]);
|
||||
const baseIndex = healthCacheClone.getOrCreateTokenInfoIndex(baseBank);
|
||||
const quoteIndex = healthCacheClone.getOrCreateTokenInfoIndex(
|
||||
quoteBanks[0],
|
||||
quoteBank,
|
||||
);
|
||||
const base = healthCacheClone.tokenInfos[baseIndex];
|
||||
const quote = healthCacheClone.tokenInfos[quoteIndex];
|
||||
|
@ -647,11 +647,11 @@ export class HealthCache {
|
|||
return side === Serum3Side.bid
|
||||
? amount
|
||||
.div(quote.oraclePrice)
|
||||
.div(ONE_I80F48.add(baseBanks[0].loanOriginationFeeRate))
|
||||
.div(ONE_I80F48.add(baseBank.loanOriginationFeeRate))
|
||||
.div(ONE_I80F48.add(I80F48.fromNumber(group.getFeeRate(false))))
|
||||
: amount
|
||||
.div(base.oraclePrice)
|
||||
.div(ONE_I80F48.add(quoteBanks[0].loanOriginationFeeRate))
|
||||
.div(ONE_I80F48.add(quoteBank.loanOriginationFeeRate))
|
||||
.div(ONE_I80F48.add(I80F48.fromNumber(group.getFeeRate(false))));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import BN from 'bn.js';
|
|||
import { MangoClient } from '../client';
|
||||
import { SERUM3_PROGRAM_ID } from '../constants';
|
||||
import { Group } from './group';
|
||||
import { MAX_I80F48, ONE_I80F48, ZERO_I80F48 } from './I80F48';
|
||||
|
||||
export class Serum3Market {
|
||||
public name: string;
|
||||
|
@ -49,6 +50,70 @@ export class Serum3Market {
|
|||
this.name = utf8.decode(new Uint8Array(name)).split('\x00')[0];
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param group
|
||||
* @returns maximum leverage one can bid on this market, this is only for display purposes,
|
||||
* also see getMaxQuoteForSerum3BidUi and getMaxBaseForSerum3AskUi
|
||||
*/
|
||||
maxBidLeverage(group: Group): number {
|
||||
const baseBank = group.getFirstBankByTokenIndex(this.baseTokenIndex);
|
||||
if (!baseBank) {
|
||||
throw new Error(
|
||||
`bank for base token with index ${this.baseTokenIndex} not found`,
|
||||
);
|
||||
}
|
||||
|
||||
const quoteBank = group.getFirstBankByTokenIndex(this.quoteTokenIndex);
|
||||
if (!quoteBank) {
|
||||
throw new Error(
|
||||
`bank for quote token with index ${this.quoteTokenIndex} not found`,
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
quoteBank.initLiabWeight.sub(baseBank.initAssetWeight).lte(ZERO_I80F48)
|
||||
) {
|
||||
return MAX_I80F48.toNumber();
|
||||
}
|
||||
|
||||
return ONE_I80F48.div(
|
||||
quoteBank.initLiabWeight.sub(baseBank.initAssetWeight),
|
||||
).toNumber();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param group
|
||||
* @returns maximum leverage one can ask on this market, this is only for display purposes,
|
||||
* also see getMaxQuoteForSerum3BidUi and getMaxBaseForSerum3AskUi
|
||||
*/
|
||||
maxAskLeverage(group: Group): number {
|
||||
const baseBank = group.getFirstBankByTokenIndex(this.baseTokenIndex);
|
||||
if (!baseBank) {
|
||||
throw new Error(
|
||||
`bank for base token with index ${this.baseTokenIndex} not found`,
|
||||
);
|
||||
}
|
||||
|
||||
const quoteBank = group.getFirstBankByTokenIndex(this.quoteTokenIndex);
|
||||
if (!quoteBank) {
|
||||
throw new Error(
|
||||
`bank for quote token with index ${this.quoteTokenIndex} not found`,
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
baseBank.initLiabWeight.sub(quoteBank.initAssetWeight).lte(ZERO_I80F48)
|
||||
) {
|
||||
return MAX_I80F48.toNumber();
|
||||
}
|
||||
|
||||
return ONE_I80F48.div(
|
||||
baseBank.initLiabWeight.sub(quoteBank.initAssetWeight),
|
||||
).toNumber();
|
||||
}
|
||||
|
||||
public async loadBids(client: MangoClient, group: Group): Promise<Orderbook> {
|
||||
const serum3MarketExternal = group.serum3MarketExternalsMap.get(
|
||||
this.serumMarketExternal.toBase58(),
|
||||
|
|
|
@ -35,6 +35,7 @@ async function main() {
|
|||
|
||||
const group = await client.getGroupForCreator(admin.publicKey, GROUP_NUM);
|
||||
console.log(`Group ${group.publicKey.toBase58()}`);
|
||||
console.log(`${group.toString()}`);
|
||||
|
||||
const banks = Array.from(group.banksMapByMint.values()).flat();
|
||||
const banksMapUsingTokenIndex = new Map(
|
||||
|
|
Loading…
Reference in New Issue