fix: apys and aprs (#514)

* fix: fixed aprs

* fix: apy and apr

* fix: fixed build
This commit is contained in:
Linkie Link 2023-09-30 08:25:56 +02:00 committed by GitHub
parent f20ae553d9
commit fac07787c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 17 additions and 6 deletions

View File

@ -36,6 +36,7 @@ export default async function getVaults(): Promise<Vault[]> {
), ),
}, },
apy: apr ? convertAprToApy(apr.apr, 365) : null, apy: apr ? convertAprToApy(apr.apr, 365) : null,
apr: apr ? apr.apr / 100 : null,
ltv: { ltv: {
max: Number(vaultConfig.max_loan_to_value), max: Number(vaultConfig.max_loan_to_value),
liq: Number(vaultConfig.liquidation_threshold), liq: Number(vaultConfig.liquidation_threshold),

View File

@ -52,7 +52,7 @@ export default function useAccountBalanceData(props: Props) {
}) })
const vaults = accountVaults.map((vault) => { const vaults = accountVaults.map((vault) => {
const apy = (vault.apy ?? 0) * 100 const apy = vault.apy ?? 0
const prevVault = updatedAccount const prevVault = updatedAccount
? account?.vaults.find((position) => position.name === vault.name) ? account?.vaults.find((position) => position.name === vault.name)
: vault : vault

View File

@ -76,6 +76,7 @@ function Fallback() {
const mockVaults: Vault[] = vaults.map((vault) => ({ const mockVaults: Vault[] = vaults.map((vault) => ({
...vault, ...vault,
apy: null, apy: null,
apr: null,
ltv: { ltv: {
max: 0, max: 0,
liq: 0, liq: 0,

View File

@ -56,11 +56,13 @@ export function addValueToVaults(
if (!vaultMetaData) return if (!vaultMetaData) return
const apy = availableVaults.find((vault) => vault.address === vaultValue.address)?.apy ?? null const apy = availableVaults.find((vault) => vault.address === vaultValue.address)?.apy ?? null
const apr = availableVaults.find((vault) => vault.address === vaultValue.address)?.apr ?? null
vaults.push({ vaults.push({
...vaultMetaData, ...vaultMetaData,
...MOCK_DEPOSITED_VAULT_POSITION, ...MOCK_DEPOSITED_VAULT_POSITION,
apy, apy,
apr,
values: { values: {
primary: halfValue, primary: halfValue,
secondary: halfValue, secondary: halfValue,

View File

@ -34,6 +34,7 @@ interface VaultConfig extends VaultMetaData, VaultInfo {}
interface Vault extends VaultConfig { interface Vault extends VaultConfig {
apy: number | null apy: number | null
apr: number | null
} }
interface VaultValuesAndAmounts { interface VaultValuesAndAmounts {

View File

@ -9,7 +9,8 @@ import {
import { byDenom } from 'utils/array' import { byDenom } from 'utils/array'
import { getAssetByDenom } from 'utils/assets' import { getAssetByDenom } from 'utils/assets'
import { BN } from 'utils/helpers' import { BN } from 'utils/helpers'
import { convertApyToApr } from 'utils/parsers'
import { convertApyToApr } from './parsers'
export const calculateAccountBalanceValue = ( export const calculateAccountBalanceValue = (
account: Account | AccountChange, account: Account | AccountChange,
@ -66,7 +67,11 @@ export const calculateAccountApr = (
lendingAssetsData: LendingMarketTableData[], lendingAssetsData: LendingMarketTableData[],
prices: BNCoin[], prices: BNCoin[],
): BigNumber => { ): BigNumber => {
const totalValue = calculateAccountBalanceValue(account, prices) const depositValue = calculateAccountValue('deposits', account, prices)
const lendsValue = calculateAccountValue('lends', account, prices)
const vaultsValue = calculateAccountValue('vaults', account, prices)
const totalValue = depositValue.plus(lendsValue).plus(vaultsValue)
if (totalValue.isZero()) return BN_ZERO if (totalValue.isZero()) return BN_ZERO
const { vaults, lends, debts } = account const { vaults, lends, debts } = account
@ -88,7 +93,7 @@ export const calculateAccountApr = (
vaults?.forEach((vault) => { vaults?.forEach((vault) => {
const lockedValue = vault.values.primary.plus(vault.values.secondary) const lockedValue = vault.values.primary.plus(vault.values.secondary)
const positionInterest = lockedValue.multipliedBy(convertApyToApr(vault?.apy ?? 0, 365)) const positionInterest = lockedValue.multipliedBy(vault?.apr ?? 0)
totalVaultsInterestValue = totalVaultsInterestValue.plus(positionInterest) totalVaultsInterestValue = totalVaultsInterestValue.plus(positionInterest)
}) })
@ -97,16 +102,17 @@ export const calculateAccountApr = (
if (!asset) return BN_ZERO if (!asset) return BN_ZERO
const price = prices.find(byDenom(debt.denom))?.amount ?? 0 const price = prices.find(byDenom(debt.denom))?.amount ?? 0
const amount = BN(debt.amount).shiftedBy(-asset.decimals) const amount = BN(debt.amount).shiftedBy(-asset.decimals)
const apr = const apy =
borrowAssetsData.find((borrowAsset) => borrowAsset.asset.denom === debt.denom)?.borrowRate ?? borrowAssetsData.find((borrowAsset) => borrowAsset.asset.denom === debt.denom)?.borrowRate ??
0 0
const positionInterest = amount.multipliedBy(price).multipliedBy(apr) const positionInterest = amount.multipliedBy(price).multipliedBy(convertApyToApr(apy, 365))
totalDebtInterestValue = totalDebtInterestValue.plus(positionInterest) totalDebtInterestValue = totalDebtInterestValue.plus(positionInterest)
}) })
const totalInterstValue = totalLendsInterestValue const totalInterstValue = totalLendsInterestValue
.plus(totalVaultsInterestValue) .plus(totalVaultsInterestValue)
.minus(totalDebtInterestValue) .minus(totalDebtInterestValue)
return totalInterstValue.dividedBy(totalValue).times(100) return totalInterstValue.dividedBy(totalValue).times(100)
} }