fix
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
parent
50e9f39b76
commit
2f8436d8f7
|
@ -174,19 +174,27 @@ export class HealthCache {
|
||||||
|
|
||||||
simHealthRatioWithTokenPositionChanges(
|
simHealthRatioWithTokenPositionChanges(
|
||||||
group: Group,
|
group: Group,
|
||||||
tokenChanges: {
|
nativeTokenChanges: {
|
||||||
tokenAmount: number;
|
nativeTokenAmount: I80F48;
|
||||||
mintPk: PublicKey;
|
mintPk: PublicKey;
|
||||||
}[],
|
}[],
|
||||||
healthType: HealthType = HealthType.init,
|
healthType: HealthType = HealthType.init,
|
||||||
): I80F48 {
|
): I80F48 {
|
||||||
const adjustedCache: HealthCache = _.cloneDeep(this);
|
const adjustedCache: HealthCache = _.cloneDeep(this);
|
||||||
for (const change of tokenChanges) {
|
for (const change of nativeTokenChanges) {
|
||||||
const bank: Bank = group.getFirstBankByMint(change.mintPk);
|
const bank: Bank = group.getFirstBankByMint(change.mintPk);
|
||||||
const changeIndex = adjustedCache.getOrCreateTokenInfoIndex(bank);
|
const changeIndex = adjustedCache.getOrCreateTokenInfoIndex(bank);
|
||||||
adjustedCache.tokenInfos[changeIndex].balance = adjustedCache.tokenInfos[
|
adjustedCache.tokenInfos[changeIndex].balance = adjustedCache.tokenInfos[
|
||||||
changeIndex
|
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);
|
return adjustedCache.healthRatio(healthType);
|
||||||
}
|
}
|
||||||
|
|
|
@ -301,21 +301,55 @@ export class MangoAccount {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simulates new health ratio after applying tokenChanges to the token positions.
|
* 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.
|
* e.g. useful to simulate health after a potential swap.
|
||||||
* Note: health ratio is technically ∞ if liabs are 0
|
* Note: health ratio is technically ∞ if liabs are 0
|
||||||
* @returns health ratio, in percentage form
|
* @returns health ratio, in percentage form
|
||||||
*/
|
*/
|
||||||
simHealthRatioWithTokenPositionChanges(
|
simHealthRatioWithTokenPositionChanges(
|
||||||
group: Group,
|
group: Group,
|
||||||
tokenChanges: {
|
nativeTokenChanges: {
|
||||||
tokenAmount: number;
|
nativeTokenAmount: I80F48;
|
||||||
mintPk: PublicKey;
|
mintPk: PublicKey;
|
||||||
}[],
|
}[],
|
||||||
healthType: HealthType = HealthType.init,
|
healthType: HealthType = HealthType.init,
|
||||||
): I80F48 {
|
): I80F48 {
|
||||||
return this.accountData.healthCache.simHealthRatioWithTokenPositionChanges(
|
return this.accountData.healthCache.simHealthRatioWithTokenPositionChanges(
|
||||||
group,
|
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,
|
healthType,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -84,45 +84,56 @@ async function debugUser(
|
||||||
group.banksMapByName.get(token)[0].mint,
|
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('SOL');
|
||||||
await getMaxWithdrawWithBorrowForTokenUiWrapper('MSOL');
|
await getMaxWithdrawWithBorrowForTokenUiWrapper('MSOL');
|
||||||
await getMaxWithdrawWithBorrowForTokenUiWrapper('USDC');
|
await getMaxWithdrawWithBorrowForTokenUiWrapper('USDC');
|
||||||
await getMaxWithdrawWithBorrowForTokenUiWrapper('BTC');
|
await getMaxWithdrawWithBorrowForTokenUiWrapper('BTC');
|
||||||
|
|
||||||
|
function simHealthRatioWithTokenPositionChangesWrapper(debug, change) {
|
||||||
console.log(
|
console.log(
|
||||||
'mangoAccount.simHealthRatioWithTokenPositionChanges ' +
|
`mangoAccount.simHealthRatioWithTokenPositionChanges ${debug}` +
|
||||||
(
|
mangoAccount
|
||||||
await mangoAccount.simHealthRatioWithTokenPositionChanges(group, [
|
.simHealthRatioWithTokenPositionUiChanges(group, [change])
|
||||||
{
|
.toNumber(),
|
||||||
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(),
|
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
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) {
|
function getMaxSourceForTokenSwapWrapper(src, tgt) {
|
||||||
console.log(
|
console.log(
|
||||||
|
|
Loading…
Reference in New Issue