Skip to content

Commit

Permalink
fix: redemption modal fidelity, MAX button, ETHFromRedemption when 0 …
Browse files Browse the repository at this point in the history
…redemp rate
  • Loading branch information
johnnyd-eth committed Dec 30, 2024
1 parent d4f2f8c commit 0ec5223
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import { t, Trans } from '@lingui/macro'
import { Checkbox, Descriptions, Form } from 'antd'
import { Trans, t } from '@lingui/macro'
import { fromWad, parseWad } from 'utils/format/formatNumber'
import { useContext, useState } from 'react'
import { useForm, useWatch } from 'antd/lib/form/Form'
import InputAccessoryButton from 'components/buttons/InputAccessoryButton'

import { BigNumber } from 'ethers'
import { Callout } from 'components/Callout/Callout'
import ETHAmount from 'components/currency/ETHAmount'
import FormattedNumberInput from 'components/inputs/FormattedNumberInput'
import TransactionModal from 'components/modals/TransactionModal'
import { TokenAmount } from 'components/TokenAmount'
import InputAccessoryButton from 'components/buttons/InputAccessoryButton'
import { ProjectMetadataContext } from 'contexts/ProjectMetadataContext'
import { BigNumber } from 'ethers'
import { useWallet } from 'hooks/Wallet'
import { TokenAmount } from 'components/TokenAmount'
import TransactionModal from 'components/modals/TransactionModal'
import { V2V3ProjectContext } from 'packages/v2v3/contexts/Project/V2V3ProjectContext'
import { useETHReceivedFromTokens } from 'packages/v2v3/hooks/contractReader/useETHReceivedFromTokens'
import useTotalBalanceOf from 'packages/v2v3/hooks/contractReader/useTotalBalanceOf'
import { useBurnTokensTx } from 'packages/v2v3/hooks/transactor/useBurnTokensTx'
import { useRedeemTokensTx } from 'packages/v2v3/hooks/transactor/useRedeemTokensTx'
import { V2V3_CURRENCY_USD } from 'packages/v2v3/utils/currency'
import { formatRedemptionRate } from 'packages/v2v3/utils/math'
import { useContext, useState } from 'react'
import { fromWad, parseWad } from 'utils/format/formatNumber'
import { emitErrorNotification } from 'utils/notifications'
import { formatRedemptionRate } from 'packages/v2v3/utils/math'
import { tokenSymbolText } from 'utils/tokenSymbolText'
import { useBurnTokensTx } from 'packages/v2v3/hooks/transactor/useBurnTokensTx'
import { useETHReceivedFromTokens } from 'packages/v2v3/hooks/contractReader/useETHReceivedFromTokens'
import { useRedeemTokensTx } from 'packages/v2v3/hooks/transactor/useRedeemTokensTx'
import useTotalBalanceOf from 'packages/v2v3/hooks/contractReader/useTotalBalanceOf'
import { useWallet } from 'hooks/Wallet'

// This doubles as the 'Redeem' and 'Burn' modal depending on if project has overflow
export function V2V3BurnOrRedeemModal({
Expand Down Expand Up @@ -187,7 +188,7 @@ export function V2V3BurnOrRedeemModal({
const personalBalanceExceeded =
redeemAmount && parseFloat(redeemAmount) > parseFloat(fromWad(totalBalance))
const inUSD = distributionLimitCurrency?.eq(V2V3_CURRENCY_USD)

return (
<TransactionModal
transactionPending={transactionPending}
Expand Down Expand Up @@ -266,6 +267,7 @@ export function V2V3BurnOrRedeemModal({
<TokenAmount
amountWad={totalBalance ?? BigNumber.from(0)}
tokenSymbol={tokenSymbol}
decimals={8}
/>
</Descriptions.Item>
<Descriptions.Item label={<Trans>Total redemption value</Trans>}>
Expand Down Expand Up @@ -294,7 +296,11 @@ export function V2V3BurnOrRedeemModal({
accessory={
<InputAccessoryButton
content={t`MAX`}
onClick={() => setRedeemAmount(fromWad(totalBalance))}
onClick={() => {
const maxRedeemAmount = fromWad(totalBalance)
setRedeemAmount(maxRedeemAmount)
form.setFieldsValue({ redeemAmount: maxRedeemAmount })
}}
/>
}
disabled={totalBalance?.eq(0)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ProjectMetadataContext } from 'contexts/ProjectMetadataContext'
import { BigNumber } from 'ethers'
import { MAX_REDEMPTION_RATE } from 'packages/v2v3/utils/math'
import { ProjectMetadataContext } from 'contexts/ProjectMetadataContext'
import { V2BallotState } from 'models/ballot'
import { V2V3ProjectContext } from 'packages/v2v3/contexts/Project/V2V3ProjectContext'
import { MAX_REDEMPTION_RATE } from 'packages/v2v3/utils/math'
import { useContext } from 'react'
import { parseWad } from 'utils/format/formatNumber'
import { useContext } from 'react'
import { useProjectReservedTokens } from './ProjectReservedTokens'

/**
Expand Down Expand Up @@ -52,11 +52,15 @@ export function useETHReceivedFromTokens({
if (!fundingCycleMetadata || !realTotalTokenSupply?.gt(0) || !tokenAmount)
return BigNumber.from(0)

const redemptionRate =
const redemptionRate =
ballotState === V2BallotState.Active
? fundingCycleMetadata.ballotRedemptionRate
: fundingCycleMetadata.redemptionRate

if (redemptionRate.eq(0)) {
return BigNumber.from(0)
}

const tokenAmountWad = parseWad(tokenAmount)

// base = ox/s
Expand All @@ -66,7 +70,7 @@ export function useETHReceivedFromTokens({
.mul(tokenAmountWad)
.div(realTotalTokenSupply)
: BigNumber.from(0)

// numerator = r + (x(1 - r)/s)
const numerator = redemptionRate.add(
tokenAmountWad
Expand Down

0 comments on commit 0ec5223

Please sign in to comment.