Fix bug in getMaxBaseForSerum3AskUi where base returned was wrong

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
microwavedcola1 2022-12-19 18:30:26 +01:00
parent 31f56d0e41
commit f3dac89d3e
2 changed files with 14 additions and 4 deletions

View File

@ -649,7 +649,7 @@ export class HealthCache {
const rightValue = fun(right);
// console.log(
// `binaryApproximationSearch left ${left}, leftValue ${leftValue}, right ${right}, rightValue ${rightValue}, targetValue ${targetValue}`,
// ` - binaryApproximationSearch left ${left.toLocaleString()}, leftValue ${leftValue.toLocaleString()}, right ${right.toLocaleString()}, rightValue ${rightValue.toLocaleString()}, targetValue ${targetValue.toLocaleString()}`,
// );
if (
@ -672,7 +672,7 @@ export class HealthCache {
newAmount = left.add(right).mul(I80F48.fromNumber(0.5));
newAmountValue = fun(newAmount);
// console.log(
// ` - left ${left}, right ${right}, newAmount ${newAmount}, newAmountValue ${newAmountValue}, targetValue ${targetValue}`,
// ` - left ${left.toLocaleString()}, right ${right.toLocaleString()}, newAmount ${newAmount.toLocaleString()}, newAmountValue ${newAmountValue.toLocaleString()}, targetValue ${targetValue.toLocaleString()}`,
// );
const error = newAmountValue.sub(targetValue);
if (error.isPos() && error.lt(targetError)) {
@ -925,6 +925,8 @@ export class HealthCache {
return ZERO_I80F48();
}
// console.log(`getMaxSerum3OrderForHealthRatio`);
// Amount which would bring health to 0
// where M = max(A_deposits, B_borrows)
// amount = M + (init_health + M * (B_init_liab - A_init_asset)) / (A_init_liab - B_init_asset);
@ -948,6 +950,8 @@ export class HealthCache {
.sub(quote.assetWeight(HealthType.init)),
),
);
// console.log(` - quoteBorrows ${quoteBorrows.toLocaleString()}`);
// console.log(` - max ${max.toLocaleString()}`);
} else {
const baseBorrows = base.balanceNative.lt(ZERO_I80F48())
? base.balanceNative.abs().mul(base.prices.liab(HealthType.init))
@ -964,6 +968,8 @@ export class HealthCache {
.sub(base.assetWeight(HealthType.init)),
),
);
// console.log(` - baseBorrows ${baseBorrows.toLocaleString()}`);
// console.log(` - max ${max.toLocaleString()}`);
}
const cache = cacheAfterPlacingOrder(zeroAmount);
@ -971,6 +977,10 @@ export class HealthCache {
const zeroAmountHealth = cache.health(HealthType.init);
const zeroAmountRatio = cache.healthRatio(HealthType.init);
// console.log(` - zeroAmount ${zeroAmount.toLocaleString()}`);
// console.log(` - zeroAmountHealth ${zeroAmountHealth.toLocaleString()}`);
// console.log(` - zeroAmountRatio ${zeroAmountRatio.toLocaleString()}`);
function cacheAfterPlacingOrder(amount: I80F48): HealthCache {
const adjustedCache: HealthCache = _.cloneDeep(healthCacheClone);
// adjustedCache.logHealthCache(` before placing order ${amount}`);

View File

@ -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, QUOTE_DECIMALS, TokenIndex } from './bank';
import { Bank, TokenIndex } from './bank';
import { Group } from './group';
import { HealthCache } from './healthCache';
import { PerpMarket, PerpMarketIndex, PerpOrder, PerpOrderSide } from './perp';
@ -698,7 +698,7 @@ export class MangoAccount {
// If its a ask then the reserved fund and potential loan is in base
// also keep some buffer for fees, use taker fees for worst case simulation.
nativeAmount = nativeAmount
// .div(baseBank.price)
.div(baseBank.price)
.div(ONE_I80F48().add(baseBank.loanOriginationFeeRate))
.div(ONE_I80F48().add(I80F48.fromNumber(group.getSerum3FeeRates(false))));
return toUiDecimals(