Skip to content

Commit

Permalink
Fix wrong weekly interest value, change formatting for sol values
Browse files Browse the repository at this point in the history
  • Loading branch information
iamsphere committed Dec 8, 2024
1 parent 1ebe1b8 commit 507406e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React, { FC, useMemo } from 'react'

import { useWallet } from '@solana/wallet-adapter-react'
import { calculateCurrentInterestSolPure } from 'fbonds-core/lib/fbond-protocol/functions/perpetual'
import { map, maxBy, sumBy } from 'lodash'
import moment from 'moment'

import { Button } from '@banx/components/Buttons'
import { CounterSlider } from '@banx/components/Slider'
Expand All @@ -14,7 +16,6 @@ import { core } from '@banx/api/nft'
import { SECONDS_IN_DAY } from '@banx/constants'
import { useModal } from '@banx/store/common'
import {
calcWeeklyInterestFee,
calcWeightedAverage,
calculateLendValue,
calculateLenderApr,
Expand Down Expand Up @@ -133,7 +134,22 @@ const calculateSummaryInfo = (loans: core.Loan[]) => {
const totalDebt = sumBy(loans, (loan) => calculateLendValue(loan))

const totalLoanValue = map(loans, (loan) => calculateLendValue(loan))
const totalWeeklyInterest = sumBy(loans, (loan) => calcWeeklyInterestFee(loan))
const totalWeeklyInterest = sumBy(loans, (loan) => {
const isTerminatingStatus = isLoanTerminating(loan)
const aprRate = isTerminatingStatus
? calculateLenderApr(loan)
: loan.bondTradeTransaction.amountOfBonds

const repayValue = calculateLendValue(loan)

const currentTimeInUnix = moment().unix()
return calculateCurrentInterestSolPure({
loanValue: repayValue,
startTime: currentTimeInUnix,
currentTime: currentTimeInUnix + SECONDS_IN_DAY * 7,
rateBasePoints: aprRate,
})
})

const totalAprArray = map(loans, (loan) => {
const isTerminatingStatus = isLoanTerminating(loan)
Expand Down
12 changes: 8 additions & 4 deletions src/utils/tokens/constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ export const DECIMAL_PLACES_LIMITS = {
],
[LendingTokenType.NativeSol]: [
{ limit: 1000, decimalPlaces: 0 }, //? Values up to 1000 have 0 decimal places
{ limit: 0.01, decimalPlaces: 2 }, //? Values up to 0.01 have 2 decimal places
{ limit: 0, decimalPlaces: 3 }, //? Values greater than 0 but less than 0.01 have 3 decimal places
{ limit: 0.1, decimalPlaces: 2 }, //? Values up to 0.1 have 2 decimal places
{ limit: 0.01, decimalPlaces: 3 }, //? Values up to 0.01 have 3 decimal places
{ limit: 0, decimalPlaces: 4 }, //? Values greater than 0 but less than 0.01 have 4 decimal places
],
[LendingTokenType.BanxSol]: [
{ limit: 1000, decimalPlaces: 0 }, //? Values up to 1000 have 0 decimal places
{ limit: 0.01, decimalPlaces: 2 }, //? Values up to 0.01 have 2 decimal places
{ limit: 0, decimalPlaces: 3 }, //? Values greater than 0 but less than 0.01 have 3 decimal places
{ limit: 0.1, decimalPlaces: 2 }, //? Values up to 0.1 have 2 decimal places
{ limit: 0.01, decimalPlaces: 3 }, //? Values up to 0.01 have 3 decimal places
{ limit: 0, decimalPlaces: 4 }, //? Values greater than 0 but less than 0.01 have 4 decimal places
],
}

Expand All @@ -70,3 +72,5 @@ export const COLLATERAL_DECIMAL_PLACES_LIMITS = [
{ limit: 0.01, decimalPlaces: 2 }, //? Values up to 0.01 have 2 decimal places
{ limit: 0, decimalPlaces: 3 }, //? Values greater than 0 but less than 0.01 have 3 decimal places
]

export const COMMA_SEPARATION_THRESHOLD = 1000
7 changes: 5 additions & 2 deletions src/utils/tokens/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { find, findIndex, repeat } from 'lodash'
import { convertToDecimalString, formatNumbersWithCommas } from '../common'
import {
COLLATERAL_DECIMAL_PLACES_LIMITS,
COMMA_SEPARATION_THRESHOLD,
DECIMAL_PLACES_LIMITS,
DEFAULT_DECIMAL_PLACES,
MIN_COLLATERAL_VALUE_TO_DISPLAY,
Expand Down Expand Up @@ -43,8 +44,10 @@ export const formatDecimalWithoutTrailingZeros = (
}

export const formatTokenValue = (value: number, tokenType: LendingTokenType): string => {
const formattedValueWithoutTrailingZeros = formatDecimalWithoutTrailingZeros(value, tokenType)
return formatNumbersWithCommas(formattedValueWithoutTrailingZeros)
const formattedValue = formatDecimalWithoutTrailingZeros(value, tokenType)
return value > COMMA_SEPARATION_THRESHOLD
? formatNumbersWithCommas(formattedValue)
: formattedValue
}

export const getDecimalPlaces = (value: number, tokenType: LendingTokenType): number => {
Expand Down

0 comments on commit 507406e

Please sign in to comment.