Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui improvements #163

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/huma-shared/src/utils/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { BigNumber, BigNumberish, ethers } from 'ethers'
import { isEmpty } from './common'
import { scientificToDecimal } from './scientificToDecimal'

const numberFormatter = new Intl.NumberFormat('en-US')
const numberFormatter = new Intl.NumberFormat('en-US', {
maximumFractionDigits: 2,
})

export const formatMoney = (
num: number | string | undefined,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
import { formatNumber, UnderlyingTokenInfo } from '@huma-finance/shared'
import {
formatNumber,
PoolInfoV2,
timestampToLL,
UnderlyingTokenInfo,
useLenderDepositRecordV2,
useLPConfigV2,
useNextEpochStartTimeV2,
} from '@huma-finance/shared'
import { ethers } from 'ethers'
import React, { useState } from 'react'
import React, { useEffect, useState } from 'react'

import { useWeb3React } from '@web3-react/core'
import { TrancheInfo } from '.'
import { useAppDispatch } from '../../../hooks/useRedux'
import {
setError,
setRedeemAmount,
setRedeemShares,
setStep,
} from '../../../store/widgets.reducers'
import { WIDGET_STEP } from '../../../store/widgets.store'
import { InputAmountModal } from '../../InputAmountModal'
import { LoadingModal } from '../../LoadingModal'

type Props = {
poolInfo: PoolInfoV2
poolUnderlyingToken: UnderlyingTokenInfo
trancheInfo: TrancheInfo
}

export function ChooseAmount({
poolInfo,
poolUnderlyingToken,
trancheInfo,
}: Props): React.ReactElement | null {
const dispatch = useAppDispatch()
const { symbol } = poolUnderlyingToken
const { account, provider } = useWeb3React()
const [currentAmount, setCurrentAmount] = useState<number>(0)
const [currentShares, setCurrentShares] = useState<number>(0)
const maxSharesFormatted = ethers.utils.formatUnits(
Expand All @@ -31,6 +45,40 @@ export function ChooseAmount({
)
const sharePrice =
trancheInfo.assets.toNumber() / trancheInfo.shares.toNumber()
const [depositRecord] = useLenderDepositRecordV2(
poolInfo.poolName,
trancheInfo.tranche,
account,
provider,
)
const lpConfig = useLPConfigV2(poolInfo.poolName, provider)
const nextEpochStartTime = useNextEpochStartTimeV2(
poolInfo.poolName,
provider,
)

useEffect(() => {
if (depositRecord && lpConfig && nextEpochStartTime) {
const SECONDS_IN_A_DAY = 24 * 60 * 60
if (
nextEpochStartTime <
depositRecord.lastDepositTime.toNumber() +
lpConfig.withdrawalLockoutPeriodInDays * SECONDS_IN_A_DAY
) {
dispatch(setStep(WIDGET_STEP.Error))
dispatch(
setError({
errorReason: 'Redemption too soon',
errorMessage: `Your last deposit was on ${timestampToLL(
depositRecord!.lastDepositTime.toNumber(),
)}. Depositors need to wait ${
lpConfig!.withdrawalLockoutPeriodInDays
} days before redemption`,
}),
)
}
}
}, [depositRecord, dispatch, lpConfig, nextEpochStartTime])

const handleChangeShares = (newShares: number) => {
const amount = Number(newShares) * sharePrice
Expand All @@ -44,6 +92,10 @@ export function ChooseAmount({
dispatch(setStep(WIDGET_STEP.Transfer))
}

if (!depositRecord || !lpConfig || !nextEpochStartTime) {
return <LoadingModal title='Redemption' />
}

return (
<InputAmountModal
title='Redemption'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export function AddRedemptionV2({
)}
{step === WIDGET_STEP.ChooseAmount && trancheInfo && (
<ChooseAmount
poolInfo={poolInfo}
poolUnderlyingToken={poolUnderlyingToken}
trancheInfo={trancheInfo}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
TrancheType,
UnderlyingTokenInfo,
formatNumber,
useLPConfigV2,
usePoolSafeAllowanceV2,
usePoolUnderlyingTokenBalanceV2,
} from '@huma-finance/shared'
Expand All @@ -14,6 +15,7 @@ import { useAppDispatch } from '../../../hooks/useRedux'
import { setStep, setSupplyAmount } from '../../../store/widgets.reducers'
import { WIDGET_STEP } from '../../../store/widgets.store'
import { InputAmountModal } from '../../InputAmountModal'
import { LoadingModal } from '../../LoadingModal'

type Props = {
poolInfo: PoolInfoV2
Expand Down Expand Up @@ -44,6 +46,8 @@ export function ChooseAmount({
balance,
poolUnderlyingToken.decimals,
)
const lpConfig = useLPConfigV2(poolInfo.poolName, provider)
const isUniTranche = lpConfig?.maxSeniorJuniorRatio === 0

const handleChangeAmount = (newAmount: number) => {
setCurrentAmount(newAmount)
Expand All @@ -61,10 +65,16 @@ export function ChooseAmount({
dispatch(setStep(step))
}

if (!lpConfig) {
return <LoadingModal title='Supply' />
}

return (
<InputAmountModal
title='Enter Amount'
subTitle={`Supplying ${symbol} with ${selectedTranche}`}
subTitle={`Supplying ${symbol} with ${
isUniTranche ? 'uni tranche' : selectedTranche
}`}
tokenSymbol={symbol}
currentAmount={currentAmount}
handleChangeAmount={handleChangeAmount}
Expand Down
Loading