simHealthWithTokenPositionChanges -> simHealthRatioWithTokenPositionChanges
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
parent
92a37f23ed
commit
4f25742b26
|
@ -162,6 +162,22 @@ export class HealthCache {
|
|||
);
|
||||
}
|
||||
|
||||
simHealthRatioWithTokenPositionChanges(
|
||||
group: Group,
|
||||
tokenChanges: { tokenName: string; tokenAmount: number }[],
|
||||
healthType: HealthType = HealthType.init,
|
||||
): I80F48 {
|
||||
const adjustedCache: HealthCache = _.cloneDeep(this);
|
||||
for (const change of tokenChanges) {
|
||||
const bank = group.banksMap.get(change.tokenName);
|
||||
adjustedCache.tokenInfos[bank.tokenIndex].balance =
|
||||
adjustedCache.tokenInfos[bank.tokenIndex].balance.add(
|
||||
I80F48.fromNumber(change.tokenAmount).mul(bank.price),
|
||||
);
|
||||
}
|
||||
return adjustedCache.healthRatio(healthType);
|
||||
}
|
||||
|
||||
getMaxSourceForTokenSwap(
|
||||
group: Group,
|
||||
sourceTokenName: string,
|
||||
|
|
|
@ -237,34 +237,19 @@ export class MangoAccount {
|
|||
}
|
||||
|
||||
/**
|
||||
* Simulates new health after applying tokenChanges to the token positions. Useful to simulate health after a potential swap.
|
||||
* Simulates new health ratio after applying tokenChanges to the token positions.
|
||||
* e.g. useful to simulate health after a potential swap.
|
||||
*/
|
||||
simHealthWithTokenPositionChanges(
|
||||
simHealthRatioWithTokenPositionChanges(
|
||||
group: Group,
|
||||
tokenChanges: { tokenName: string; tokenAmount: number }[],
|
||||
healthType: HealthType = HealthType.init,
|
||||
): I80F48 {
|
||||
// This is a approximation of the easy case, where
|
||||
// mango account has no token positions for tokens in changes list, or
|
||||
// the change is in direction e.g. deposits for deposits, borrows for borrows, of existing token position.
|
||||
// TODO: recompute entire health using components.
|
||||
const initHealth = (this.accountData as MangoAccountData).initHealth;
|
||||
for (const change of tokenChanges) {
|
||||
const bank = group.banksMap.get(change.tokenName);
|
||||
if (change.tokenAmount >= 0) {
|
||||
initHealth.add(
|
||||
bank.initAssetWeight
|
||||
.mul(I80F48.fromNumber(change.tokenAmount))
|
||||
.mul(bank.price),
|
||||
);
|
||||
} else {
|
||||
initHealth.sub(
|
||||
bank.initLiabWeight
|
||||
.mul(I80F48.fromNumber(change.tokenAmount))
|
||||
.mul(bank.price),
|
||||
);
|
||||
}
|
||||
}
|
||||
return initHealth;
|
||||
return this.accountData.healthCache.simHealthRatioWithTokenPositionChanges(
|
||||
group,
|
||||
tokenChanges,
|
||||
healthType,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -60,24 +60,21 @@ async function debugUser(client, group, mangoAccount) {
|
|||
);
|
||||
|
||||
console.log(
|
||||
'mangoAccount.simHealthWithTokenPositionChanges ' +
|
||||
toUiDecimalsForQuote(
|
||||
(
|
||||
await mangoAccount.simHealthWithTokenPositionChanges(group, [
|
||||
{
|
||||
tokenName: 'USDC',
|
||||
tokenAmount:
|
||||
-20_000 *
|
||||
Math.pow(10, group.banksMap.get('BTC')!.mintDecimals!),
|
||||
},
|
||||
{
|
||||
tokenName: 'BTC',
|
||||
tokenAmount:
|
||||
1 * Math.pow(10, group.banksMap.get('BTC')!.mintDecimals!),
|
||||
},
|
||||
])
|
||||
).toNumber(),
|
||||
),
|
||||
'mangoAccount.simHealthRatioWithTokenPositionChanges ' +
|
||||
(
|
||||
await mangoAccount.simHealthRatioWithTokenPositionChanges(group, [
|
||||
{
|
||||
tokenName: 'USDC',
|
||||
tokenAmount:
|
||||
-95_000 * Math.pow(10, group.banksMap.get('USDC')!.mintDecimals!),
|
||||
},
|
||||
{
|
||||
tokenName: 'BTC',
|
||||
tokenAmount:
|
||||
4 * Math.pow(10, group.banksMap.get('BTC')!.mintDecimals!),
|
||||
},
|
||||
])
|
||||
).toNumber(),
|
||||
);
|
||||
|
||||
function getMaxSourceForTokenSwapWrapper(src, tgt) {
|
||||
|
|
Loading…
Reference in New Issue