From 89680f637ce7887a3b44f9b4a958363abde51bce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakob=20Pov=C5=A1i=C4=8D?= Date: Wed, 13 Dec 2023 12:18:15 +0100 Subject: [PATCH] Update volatility factor and compute max withdraw for bank --- packages/marginfi-client-v2/src/models/account/pure.ts | 5 ++++- packages/marginfi-v2-ui-state/src/lib/mrgnlend.ts | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/marginfi-client-v2/src/models/account/pure.ts b/packages/marginfi-client-v2/src/models/account/pure.ts index fe3d345b36..27c1e1c3ff 100644 --- a/packages/marginfi-client-v2/src/models/account/pure.ts +++ b/packages/marginfi-client-v2/src/models/account/pure.ts @@ -305,7 +305,10 @@ class MarginfiAccount { } // apply volatility factor to avoid failure due to price volatility / slippage - const initUntiedCollateralForBank = freeCollateral.times(_volatilityFactor); + // initCollateralForBank > freeCollateral, so volatilityBuffer > 0 + const volatilityBuffer = (initCollateralForBank.minus(freeCollateral)).times(1 - _volatilityFactor); + const initUntiedCollateralForBank = freeCollateral.minus(volatilityBuffer); + const priceLowestBias = bank.getPrice(priceInfo, PriceBias.Lowest); const initWeightedPrice = priceLowestBias.times(initAssetWeight); const maxWithdraw = initUntiedCollateralForBank.div(initWeightedPrice); diff --git a/packages/marginfi-v2-ui-state/src/lib/mrgnlend.ts b/packages/marginfi-v2-ui-state/src/lib/mrgnlend.ts index ae0606c0c2..9f7955c8bb 100644 --- a/packages/marginfi-v2-ui-state/src/lib/mrgnlend.ts +++ b/packages/marginfi-v2-ui-state/src/lib/mrgnlend.ts @@ -23,7 +23,7 @@ import { Connection, PublicKey, SystemProgram } from "@solana/web3.js"; import BN from "bn.js"; const FEE_MARGIN = 0.01; -const VOLATILITY_FACTOR = 0.98; +const VOLATILITY_FACTOR = 0.975; const DEFAULT_ACCOUNT_SUMMARY = { healthFactor: 0, @@ -347,7 +347,7 @@ function makeExtendedBankInfo( const maxWithdraw = floor( Math.min( - marginfiAccount.computeMaxWithdrawForBank(bank.address).toNumber(), + marginfiAccount.computeMaxWithdrawForBank(bank.address, { volatilityFactor: VOLATILITY_FACTOR }).toNumber(), bankInfo.availableLiquidity ), bankInfo.mintDecimals