Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
microwavedcola1 2022-07-14 14:29:44 +02:00
parent 62bc3649d7
commit 227eda9844
2 changed files with 106 additions and 80 deletions

View File

@ -92,15 +92,25 @@ export class MangoAccount {
return ta ? ta.native(bank) : ZERO_I80F48;
}
getEquivalentNativeUsdcPosition(bank: Bank): I80F48 {
const ta = this.findToken(bank.tokenIndex);
return ta
? ta
.native(bank)
.mul(I80F48.fromNumber(Math.pow(10, QUOTE_DECIMALS)))
.div(I80F48.fromNumber(Math.pow(10, bank.mintDecimals)))
.mul(bank.price)
: ZERO_I80F48;
static getEquivalentNativeUsdcPosition(
sourceBank: Bank,
nativeTokenPosition: TokenPosition,
): I80F48 {
return nativeTokenPosition
.native(sourceBank)
.mul(I80F48.fromNumber(Math.pow(10, QUOTE_DECIMALS)))
.div(I80F48.fromNumber(Math.pow(10, sourceBank.mintDecimals)))
.mul(sourceBank.price);
}
static getEquivalentNativeTokenPosition(
targetBank: Bank,
nativeUsdcPosition: I80F48,
): I80F48 {
return nativeUsdcPosition
.div(targetBank.price)
.div(I80F48.fromNumber(Math.pow(10, QUOTE_DECIMALS)))
.mul(I80F48.fromNumber(Math.pow(10, targetBank.mintDecimals)));
}
getNativeDeposits(bank: Bank): I80F48 {
@ -191,14 +201,21 @@ export class MangoAccount {
/**
* The amount of given native token you can borrow, considering all existing assets as collateral except the deposits for this token.
* The existing native deposits need to be added to get the full amount that could be withdrawn.
* Note 1: The existing native deposits need to be added to get the full amount that could be withdrawn.
* Note 2: The group might have less native deposits than what this returns.
*/
getMaxWithdrawWithBorrowForToken(group: Group, tokenName: string): I80F48 {
const bank = group.banksMap.get(tokenName);
const initHealth = (this.accountData as MangoAccountData).initHealth;
const inUsdcUnits = this.getEquivalentNativeUsdcPosition(bank);
const inUsdcUnits = MangoAccount.getEquivalentNativeUsdcPosition(
bank,
this.findToken(bank.tokenIndex),
);
const newInitHealth = initHealth.sub(inUsdcUnits.mul(bank.initAssetWeight));
return newInitHealth.div(bank.price.mul(bank.initLiabWeight));
return MangoAccount.getEquivalentNativeTokenPosition(
bank,
newInitHealth.div(bank.initLiabWeight),
);
}
/**

View File

@ -61,74 +61,83 @@ async function main() {
await mangoAccount.reload(client, group);
}
if (true) {
await mangoAccount.reload(client, group);
console.log(
'mangoAccount.getEquity() ' +
toUiDecimals(mangoAccount.getEquity().toNumber()),
);
console.log(
'mangoAccount.getHealth(HealthType.init) ' +
toUiDecimals(mangoAccount.getHealth(HealthType.init).toNumber()),
);
console.log(
'mangoAccount.getHealthRatio(HealthType.init) ' +
mangoAccount.getHealthRatio(HealthType.init).toNumber(),
);
console.log(
'mangoAccount.getCollateralValue() ' +
toUiDecimals(mangoAccount.getCollateralValue().toNumber()),
);
console.log(
'mangoAccount.getAssetsVal() ' +
toUiDecimals(mangoAccount.getAssetsVal().toNumber()),
);
console.log(
'mangoAccount.getLiabsVal() ' +
toUiDecimals(mangoAccount.getLiabsVal().toNumber()),
);
console.log(
"mangoAccount.getMaxWithdrawWithBorrowForToken(group, 'SOL') " +
toUiDecimals(
(
await mangoAccount.getMaxWithdrawWithBorrowForToken(group, 'SOL')
).toNumber(),
),
);
console.log(
"mangoAccount.getMaxSourceForTokenSwap(group, 'USDC', 'BTC') " +
toUiDecimals(
(
await mangoAccount.getMaxSourceForTokenSwap(
group,
'USDC',
'BTC',
0.94,
)
).toNumber(),
),
);
console.log(
'mangoAccount.simHealthWithTokenPositionChanges ' +
toUiDecimals(
(
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(),
),
);
}
await mangoAccount.reload(client, group);
console.log(
'mangoAccount.getEquity() ' +
toUiDecimals(mangoAccount.getEquity().toNumber()),
);
console.log(
'mangoAccount.getHealth(HealthType.init) ' +
toUiDecimals(mangoAccount.getHealth(HealthType.init).toNumber()),
);
console.log(
'mangoAccount.getHealthRatio(HealthType.init) ' +
mangoAccount.getHealthRatio(HealthType.init).toNumber(),
);
console.log(
'mangoAccount.getCollateralValue() ' +
toUiDecimals(mangoAccount.getCollateralValue().toNumber()),
);
console.log(
'mangoAccount.getAssetsVal() ' +
toUiDecimals(mangoAccount.getAssetsVal().toNumber()),
);
console.log(
'mangoAccount.getLiabsVal() ' +
toUiDecimals(mangoAccount.getLiabsVal().toNumber()),
);
console.log(
"mangoAccount.getMaxWithdrawWithBorrowForToken(group, 'SOL') " +
toUiDecimals(
(
await mangoAccount.getMaxWithdrawWithBorrowForToken(group, 'SOL')
).toNumber(),
),
);
// let withdraw = mangoAccount
// .getNative(group.banksMap.get('SOL')!)
// .add(mangoAccount.getMaxWithdrawWithBorrowForToken(group, 'SOL'))
// .toNumber();
// console.log(` Withdrawing ${toUiDecimals(withdraw)} SOL`);
// await client.tokenWithdraw2(group, mangoAccount, 'SOL', withdraw, true);
// console.log(` Depositing ${toUiDecimals(withdraw)} SOL`);
// await client.tokenDeposit(group, mangoAccount, 'SOL', withdraw);
console.log(
"mangoAccount.getMaxSourceForTokenSwap(group, 'USDC', 'BTC') " +
toUiDecimals(
(
await mangoAccount.getMaxSourceForTokenSwap(
group,
'USDC',
'BTC',
0.94,
)
).toNumber(),
),
);
console.log(
'mangoAccount.simHealthWithTokenPositionChanges ' +
toUiDecimals(
(
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(),
),
);
process.exit();
}