From 9712573b00a8e784f82ba189b6f2ec3aaf566468 Mon Sep 17 00:00:00 2001 From: Bob van der Helm <34470358+bobthebuidlr@users.noreply.github.com> Date: Tue, 15 Aug 2023 10:34:01 -0300 Subject: [PATCH] [bugfix] fix leverage and implement buffer for health (#374) * [bugfix] fix leverage and implement buffer for health * [bugfix] properly remove council page from nav * [bugfix] ensure integer values for HC * [bugfix] infinite rerender withdraw + negative values --- src/components/Account/AccountDetails.tsx | 2 +- src/components/Council/Overview.tsx | 34 ----------------------- src/components/Header/DesktopHeader.tsx | 1 - src/hooks/useHealthComputer.tsx | 8 +++++- src/hooks/useUpdatedAccount/index.ts | 10 +++++-- src/utils/accounts.ts | 3 +- src/utils/constants.ts | 2 ++ 7 files changed, 20 insertions(+), 40 deletions(-) delete mode 100644 src/components/Council/Overview.tsx diff --git a/src/components/Account/AccountDetails.tsx b/src/components/Account/AccountDetails.tsx index c2af854b..1deea823 100644 --- a/src/components/Account/AccountDetails.tsx +++ b/src/components/Account/AccountDetails.tsx @@ -61,7 +61,7 @@ function AccountDetails(props: Props) { options={{ maxDecimals: 0, minDecimals: 0, suffix: '%' }} animate /> - {updatedHealth && health !== updatedHealth && ( + {updatedHealth > 0 && health !== updatedHealth && ( <> {`Council page for ${address}`} - ) : ( - Council view only - ) -} - -function Fallback() { - return -} - -export default function Overview() { - return ( - - }> - - - - ) -} diff --git a/src/components/Header/DesktopHeader.tsx b/src/components/Header/DesktopHeader.tsx index 07496390..b1ec918a 100644 --- a/src/components/Header/DesktopHeader.tsx +++ b/src/components/Header/DesktopHeader.tsx @@ -13,7 +13,6 @@ export const menuTree: { page: Page; label: string }[] = [ { page: 'lend', label: 'Earn' }, { page: 'borrow', label: 'Borrow' }, { page: 'portfolio', label: 'Portfolio' }, - { page: 'council', label: 'Council' }, ] export default function DesktopHeader() { diff --git a/src/hooks/useHealthComputer.tsx b/src/hooks/useHealthComputer.tsx index f9ad677e..efcdf6f1 100644 --- a/src/hooks/useHealthComputer.tsx +++ b/src/hooks/useHealthComputer.tsx @@ -24,6 +24,7 @@ import { import useStore from 'store' import { BN_ZERO } from 'constants/math' import { BN } from 'utils/helpers' +import { HEALTH_BUFFER } from 'utils/constants' export default function useHealthComputer(account?: Account) { const { data: prices } = usePrices() @@ -41,7 +42,6 @@ export default function useHealthComputer(account?: Account) { [prices, baseCurrency.denom], ) - const vaultPositionValues = useMemo(() => { if (!account?.vaults) return null return account.vaults.reduce( @@ -141,6 +141,8 @@ export default function useHealthComputer(account?: Account) { (denom: string, target: BorrowTarget) => { if (!healthComputer) return BN_ZERO return BN(max_borrow_estimate_js(healthComputer, denom, target)) + .times(1 - HEALTH_BUFFER) + .integerValue() }, [healthComputer], ) @@ -149,6 +151,8 @@ export default function useHealthComputer(account?: Account) { (denom: string) => { if (!healthComputer) return BN_ZERO return BN(max_withdraw_estimate_js(healthComputer, denom)) + .times(1 - HEALTH_BUFFER) + .integerValue() }, [healthComputer], ) @@ -158,6 +162,8 @@ export default function useHealthComputer(account?: Account) { if (!healthComputer) return BN_ZERO try { return BN(max_swap_estimate_js(healthComputer, from, to, kind)) + .times(1 - HEALTH_BUFFER) + .integerValue() } catch { return BN_ZERO } diff --git a/src/hooks/useUpdatedAccount/index.ts b/src/hooks/useUpdatedAccount/index.ts index e67c1b2e..eec93acc 100644 --- a/src/hooks/useUpdatedAccount/index.ts +++ b/src/hooks/useUpdatedAccount/index.ts @@ -24,11 +24,17 @@ export function useUpdatedAccount(account?: Account) { (denom: string) => { if (!account) return const deposit = account.deposits.find((deposit) => deposit.denom === denom) + if (deposit) { - removeDeposits([...removedDeposits, deposit]) + removeDeposits((prevRemovedDeposits) => { + return [ + ...prevRemovedDeposits.filter((removedDeposit) => removedDeposit.denom !== denom), + deposit, + ] + }) } }, - [account, removedDeposits], + [account, removeDeposits], ) useEffect(() => { diff --git a/src/utils/accounts.ts b/src/utils/accounts.ts index 0a8b9c3d..3a1579de 100644 --- a/src/utils/accounts.ts +++ b/src/utils/accounts.ts @@ -79,7 +79,8 @@ export const calculateAccountBorrowRate = ( export function calculateAccountLeverage(account: Account, prices: BNCoin[]) { const [deposits, lends, debts, vaults] = getAccountPositionValues(account, prices) - return debts.dividedBy(deposits.plus(lends).plus(vaults)).plus(1) + const netValue = deposits.plus(lends).plus(vaults).minus(debts) + return debts.dividedBy(netValue).plus(1) } export function getAmount(denom: string, coins: Coin[]): BigNumber { diff --git a/src/utils/constants.ts b/src/utils/constants.ts index fb59eac3..e88482d5 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -9,3 +9,5 @@ export const defaultFee: StdFee = { } export const SECONDS_IN_A_YEAR = 31540000 + +export const HEALTH_BUFFER = 0.01