Last active
March 7, 2024 02:45
-
-
Save losman0s/c3128fe89b8d9110b580ba1cb024ad75 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Amount, toBigNumber } from "@mrgnlabs/mrgn-common"; | |
import { getMarginfiClient } from "./utils"; | |
import { Bank, OraclePrice } from "../src"; | |
import BigNumber from "bignumber.js"; | |
async function main() { | |
const client = await getMarginfiClient(); | |
const uxdBank = client.getBankByTokenSymbol("UXD"); | |
if (!uxdBank) { | |
throw new Error("UXD bank not found"); | |
} | |
const uxdPriceInfo = client.getOraclePriceByBank(uxdBank.address); | |
if (!uxdPriceInfo) { | |
throw new Error("UXD oracle price not found"); | |
} | |
const usdcBank = client.getBankByTokenSymbol("USDC"); | |
if (!usdcBank) { | |
throw new Error("USDC bank not found"); | |
} | |
const usdcPriceInfo = client.getOraclePriceByBank(usdcBank.address); | |
if (!usdcPriceInfo) { | |
throw new Error("USDC oracle price not found"); | |
} | |
const { maxDeposit, maxBorrow, leverage, maintHealth, netAccountValue } = computeLeverParams( | |
100, | |
uxdBank, | |
usdcBank, | |
uxdPriceInfo, | |
usdcPriceInfo, | |
); | |
console.log("Max deposit:", maxDeposit.toFixed()); | |
console.log("Max borrow:", maxBorrow.toFixed()); | |
console.log("Leverage:", leverage); | |
console.log("Final health", maintHealth * 100); | |
console.log("Net value:", netAccountValue.toFixed()); | |
} | |
function computeLeverParams( | |
initialCollateral: Amount, | |
depositBank: Bank, | |
borrowBank: Bank, | |
depositOracleInfo: OraclePrice, | |
borrowOracleInfo: OraclePrice, | |
): { maxBorrow: BigNumber; maxDeposit: BigNumber; leverage: number, maintHealth: number, netAccountValue: BigNumber } { | |
const _initialCollateral = toBigNumber(initialCollateral); | |
const depositInitWeight = depositBank.config.assetWeightInit; | |
const borrowInitWeight = borrowBank.config.liabilityWeightInit; | |
const ltv = depositInitWeight.div(borrowInitWeight); | |
console.log("LTV:", ltv.toFixed()); | |
const depositPriceLow = depositOracleInfo.priceWeighted.lowestPrice; | |
const borrowPriceHigh = borrowOracleInfo.priceWeighted.highestPrice; | |
const initialCollateralValue = _initialCollateral.times(depositPriceLow); | |
const maxDepositValue = initialCollateralValue.div(new BigNumber(1).minus(ltv)); | |
const maxBorrowValue = maxDepositValue.times(ltv); | |
const maxDeposit = maxDepositValue.div(depositPriceLow); | |
const maxBorrow = maxBorrowValue.div(borrowPriceHigh); | |
const leverage = maxDeposit.div(_initialCollateral).toNumber(); | |
const finalMaintWeightedDeposits = maxDeposit.times(depositBank.config.assetWeightMaint).times(depositPriceLow); | |
const finalMaintWeightedBorrows = maxBorrow.times(borrowBank.config.liabilityWeightMaint).times(borrowPriceHigh); | |
const maintHealth = finalMaintWeightedDeposits.minus(finalMaintWeightedBorrows).div(finalMaintWeightedDeposits).toNumber(); | |
const netAccountValue = maxDepositValue.minus(maxBorrowValue); | |
return { maxBorrow, maxDeposit, leverage, maintHealth, netAccountValue }; | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I had inverted:
Fixed now.