Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
microwavedcola1 2022-08-23 08:34:44 +02:00
parent 50e9f39b76
commit 2f8436d8f7
3 changed files with 93 additions and 40 deletions

View File

@ -174,19 +174,27 @@ export class HealthCache {
simHealthRatioWithTokenPositionChanges(
group: Group,
tokenChanges: {
tokenAmount: number;
nativeTokenChanges: {
nativeTokenAmount: I80F48;
mintPk: PublicKey;
}[],
healthType: HealthType = HealthType.init,
): I80F48 {
const adjustedCache: HealthCache = _.cloneDeep(this);
for (const change of tokenChanges) {
for (const change of nativeTokenChanges) {
const bank: Bank = group.getFirstBankByMint(change.mintPk);
const changeIndex = adjustedCache.getOrCreateTokenInfoIndex(bank);
adjustedCache.tokenInfos[changeIndex].balance = adjustedCache.tokenInfos[
changeIndex
].balance.add(I80F48.fromNumber(change.tokenAmount).mul(bank.price));
].balance.add(change.nativeTokenAmount.mul(bank.price));
console.log(' ');
console.log(`change.mintPk ${change.mintPk.toBase58()}`);
console.log(`changeIndex ${changeIndex}`);
console.log(
`adjustedCache.tokenInfos[changeIndex].balance ${adjustedCache.tokenInfos[
changeIndex
].balance.toNumber()}`,
);
}
return adjustedCache.healthRatio(healthType);
}

View File

@ -301,21 +301,55 @@ export class MangoAccount {
/**
* Simulates new health ratio after applying tokenChanges to the token positions.
* Note: token changes are expected in native amounts
*
* e.g. useful to simulate health after a potential swap.
* Note: health ratio is technically if liabs are 0
* @returns health ratio, in percentage form
*/
simHealthRatioWithTokenPositionChanges(
group: Group,
tokenChanges: {
tokenAmount: number;
nativeTokenChanges: {
nativeTokenAmount: I80F48;
mintPk: PublicKey;
}[],
healthType: HealthType = HealthType.init,
): I80F48 {
return this.accountData.healthCache.simHealthRatioWithTokenPositionChanges(
group,
tokenChanges,
nativeTokenChanges,
healthType,
);
}
/**
* Simulates new health ratio after applying tokenChanges to the token positions.
* Note: token changes are expected in ui amounts
*
* e.g. useful to simulate health after a potential swap.
* Note: health ratio is technically if liabs are 0
* @returns health ratio, in percentage form
*/
simHealthRatioWithTokenPositionUiChanges(
group: Group,
uiTokenChanges: {
uiTokenAmount: number;
mintPk: PublicKey;
}[],
healthType: HealthType = HealthType.init,
): I80F48 {
const nativeTokenChanges = uiTokenChanges.map((tokenChange) => {
return {
nativeTokenAmount: I80F48.fromNumber(
tokenChange.uiTokenAmount *
Math.pow(10, group.getMintDecimals(tokenChange.mintPk)),
),
mintPk: tokenChange.mintPk,
};
});
return this.accountData.healthCache.simHealthRatioWithTokenPositionChanges(
group,
nativeTokenChanges,
healthType,
);
}

View File

@ -84,45 +84,56 @@ async function debugUser(
group.banksMapByName.get(token)[0].mint,
),
);
try {
await client.tokenWithdraw(
group,
mangoAccount,
group.banksMapByName.get(token)[0].mint,
mangoAccount.getMaxWithdrawWithBorrowForTokenUi(
group,
group.banksMapByName.get(token)[0].mint,
),
true,
);
} catch (error) {
console.log(error);
}
}
await getMaxWithdrawWithBorrowForTokenUiWrapper('SOL');
await getMaxWithdrawWithBorrowForTokenUiWrapper('MSOL');
await getMaxWithdrawWithBorrowForTokenUiWrapper('USDC');
await getMaxWithdrawWithBorrowForTokenUiWrapper('BTC');
console.log(
'mangoAccount.simHealthRatioWithTokenPositionChanges ' +
(
await mangoAccount.simHealthRatioWithTokenPositionChanges(group, [
{
mintPk: group.banksMapByName.get('USDC')[0].mint,
tokenAmount:
-95_000 *
Math.pow(10, group.banksMapByName.get('USDC')[0]!.mintDecimals!),
},
{
mintPk: group.banksMapByName.get('BTC')[0].mint,
tokenAmount:
4 *
Math.pow(10, group.banksMapByName.get('BTC')[0]!.mintDecimals!),
},
])
).toNumber(),
);
function simHealthRatioWithTokenPositionChangesWrapper(debug, change) {
console.log(
`mangoAccount.simHealthRatioWithTokenPositionChanges ${debug}` +
mangoAccount
.simHealthRatioWithTokenPositionUiChanges(group, [change])
.toNumber(),
);
}
simHealthRatioWithTokenPositionChangesWrapper('sol 1 ', {
mintPk: group.banksMapByName.get('SOL')[0].mint,
uiTokenAmount: 1,
});
simHealthRatioWithTokenPositionChangesWrapper('sol -1 ', {
mintPk: group.banksMapByName.get('SOL')[0].mint,
uiTokenAmount: -1,
});
simHealthRatioWithTokenPositionChangesWrapper('msol 1 ', {
mintPk: group.banksMapByName.get('MSOL')[0].mint,
uiTokenAmount: 1,
});
simHealthRatioWithTokenPositionChangesWrapper('msol -1 ', {
mintPk: group.banksMapByName.get('MSOL')[0].mint,
uiTokenAmount: -1,
});
simHealthRatioWithTokenPositionChangesWrapper('usdc 10 ', {
mintPk: group.banksMapByName.get('USDC')[0].mint,
uiTokenAmount: 10,
});
simHealthRatioWithTokenPositionChangesWrapper('usdc -10 ', {
mintPk: group.banksMapByName.get('USDC')[0].mint,
uiTokenAmount: -10,
});
simHealthRatioWithTokenPositionChangesWrapper('btc 0.001 ', {
mintPk: group.banksMapByName.get('BTC')[0].mint,
uiTokenAmount: 0.001,
});
simHealthRatioWithTokenPositionChangesWrapper('btc -0.001 ', {
mintPk: group.banksMapByName.get('BTC')[0].mint,
uiTokenAmount: -0.001,
});
simHealthRatioWithTokenPositionChangesWrapper('soETH -0.001 ', {
mintPk: group.banksMapByName.get('soETH')[0].mint,
uiTokenAmount: -0.001,
});
function getMaxSourceForTokenSwapWrapper(src, tgt) {
console.log(