diff --git a/src/utils/constants.ts b/src/utils/constants.ts index 45708df4..18101443 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -13,3 +13,4 @@ export const SECONDS_IN_A_YEAR = 31540000 export const LTV_BUFFER = 0.01 export const DEPOSIT_CAP_BUFFER = 0.999 +export const VAULT_DEPOSIT_BUFFER = 0.9999 \ No newline at end of file diff --git a/src/utils/vaults.ts b/src/utils/vaults.ts index b250108a..1c5ad221 100644 --- a/src/utils/vaults.ts +++ b/src/utils/vaults.ts @@ -5,6 +5,7 @@ import { TESTNET_VAULTS_META_DATA, VAULTS_META_DATA } from 'constants/vaults' import { BNCoin } from 'types/classes/BNCoin' import { Action } from 'types/generated/mars-credit-manager/MarsCreditManager.types' import { getAssetByDenom } from 'utils/assets' +import { VAULT_DEPOSIT_BUFFER } from 'utils/constants' import { getCoinAmount, getCoinValue } from 'utils/formatters' import { getValueFromBNCoins, mergeBNCoinArrays } from 'utils/helpers' import { getTokenPrice } from 'utils/tokens' @@ -27,7 +28,12 @@ export function getVaultDepositCoinsAndValue( ) { const depositsAndReclaims = mergeBNCoinArrays(deposits, reclaims) const borrowingsAndDepositsAndReclaims = mergeBNCoinArrays(borrowings, depositsAndReclaims) - const totalValue = getValueFromBNCoins(borrowingsAndDepositsAndReclaims, prices) + + // The BUFFER is to account for rounding errors. Otherwise, it might happen we try to deposit more value + // into the vaults than there are funds available. + const totalValue = getValueFromBNCoins(borrowingsAndDepositsAndReclaims, prices).times( + VAULT_DEPOSIT_BUFFER, + ) const halfValue = totalValue.dividedBy(2) const primaryAsset = getAssetByDenom(vault.denoms.primary) ?? ASSETS[0] @@ -180,4 +186,4 @@ function getSwapAction(denomIn: string, denomOut: string, amount: BigNumber, sli slippage: slippage.toString(), }, } -} +} \ No newline at end of file