From ccde9dbe1e04173503b7ac489a0f216367e68027 Mon Sep 17 00:00:00 2001 From: Bob van der Helm <34470358+bobthebuidlr@users.noreply.github.com> Date: Tue, 12 Sep 2023 15:57:56 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9Bvault=20deposit:=20add=20buffer=20t?= =?UTF-8?q?o=20prevent=20over=20providing=20(#459)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/constants.ts | 1 + src/utils/vaults.ts | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) 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