Skip to content

Commit

Permalink
Add better comments to useResolveEditCycleConflicts()
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyd-eth committed Nov 2, 2023
1 parent 48ebd32 commit 6ad8e35
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { t } from '@lingui/macro'
import { AmountInCurrency } from 'components/currency/AmountInCurrency'
import { timeSecondsToDateString } from 'components/v2v3/V2V3Project/ProjectDashboard/utils/timeSecondsToDateString'
import { BigNumber } from 'ethers'
import { V2V3CurrencyOption } from 'models/v2v3/currencyOption'
import { V2V3FundingCycle } from 'models/v2v3/fundingCycle'
import { useMemo } from 'react'
import { fromWad } from 'utils/format/formatNumber'
import { formatCurrencyAmount } from 'utils/formatCurrencyAmount'
import { getBallotStrategyByAddress } from 'utils/v2v3/ballotStrategies'
import { V2V3CurrencyName } from 'utils/v2v3/currency'
import { MAX_DISTRIBUTION_LIMIT } from 'utils/v2v3/math'
import { ConfigurationPanelDatum } from '../../components/ConfigurationPanel'
import { pairToDatum } from '../../utils/pairToDatum'

export const useFormatConfigurationCyclesSection = ({
fundingCycle,
upcomingFundingCycle,
distributionLimitAmountCurrency,
upcomingDistributionLimitAmountCurrency,
}: {
fundingCycle: V2V3FundingCycle | undefined
upcomingFundingCycle?: V2V3FundingCycle | null
distributionLimitAmountCurrency:
| {
distributionLimit: BigNumber | undefined
currency: BigNumber | undefined
}
| undefined
upcomingDistributionLimitAmountCurrency?: {
distributionLimit: BigNumber | undefined
currency: BigNumber | undefined
} | null
}) => {
const durationDatum: ConfigurationPanelDatum = useMemo(() => {
const formatDuration = (duration: BigNumber | undefined) => {
if (duration === undefined) return undefined
if (duration.eq(0)) return t`Not set`
return timeSecondsToDateString(duration.toNumber(), 'short', 'lower')
}
const currentDuration = formatDuration(fundingCycle?.duration)
if (upcomingFundingCycle === null) {
return pairToDatum(t`Duration`, currentDuration, null)
}
const upcomingDuration = formatDuration(upcomingFundingCycle?.duration)

return pairToDatum(t`Duration`, currentDuration, upcomingDuration)
}, [fundingCycle?.duration, upcomingFundingCycle])

const payoutsDatum: ConfigurationPanelDatum = useMemo(() => {
const formatCurrency = (currency: BigNumber | undefined) => {
if (currency === undefined) return undefined
return currency.toNumber() as V2V3CurrencyOption
}
const formatAmountWad = (
amountWad: BigNumber | undefined,
currency: V2V3CurrencyOption | undefined,
) => {
if (amountWad === undefined) return undefined
if (amountWad.eq(MAX_DISTRIBUTION_LIMIT)) return t`Unlimited`
if (amountWad.eq(0)) return t`Zero (no payouts)`
return formatCurrencyAmount({
amount: Number(fromWad(amountWad)),
currency,
})
}
const { distributionLimit, currency } =
distributionLimitAmountCurrency ?? {}
// const currentPayout = formatAmountWad(
// distributionLimit,
// formatCurrency(currency),
// )
const currentPayout = (
<AmountInCurrency
amount={distributionLimit}
currency={V2V3CurrencyName(formatCurrency(currency))}
/>
)

if (upcomingDistributionLimitAmountCurrency === null) {
return pairToDatum(t`Payouts`, currentPayout, null)
}

const upcomingDistributionLimit =
upcomingDistributionLimitAmountCurrency?.distributionLimit !== undefined
? upcomingDistributionLimitAmountCurrency.distributionLimit
: undefined
const upcomingDistributionLimitCurrency =
upcomingDistributionLimitAmountCurrency?.currency !== undefined
? upcomingDistributionLimitAmountCurrency.currency
: undefined
const upcomingPayout = formatAmountWad(
upcomingDistributionLimit,
formatCurrency(upcomingDistributionLimitCurrency),
)

return pairToDatum(t`Payouts`, currentPayout, upcomingPayout)
}, [distributionLimitAmountCurrency, upcomingDistributionLimitAmountCurrency])

const editDeadlineDatum: ConfigurationPanelDatum = useMemo(() => {
const currentBallotStrategy = fundingCycle?.ballot
? getBallotStrategyByAddress(fundingCycle.ballot)
: undefined
const current = currentBallotStrategy?.name
if (upcomingFundingCycle === null) {
return pairToDatum(t`Edit deadline`, current, null)
}

const upcomingBallotStrategy = upcomingFundingCycle?.ballot
? getBallotStrategyByAddress(upcomingFundingCycle.ballot)
: undefined
const upcoming = upcomingBallotStrategy?.name
return pairToDatum(t`Edit deadline`, current, upcoming)
}, [fundingCycle?.ballot, upcomingFundingCycle])

return useMemo(() => {
return {
duration: durationDatum,
payouts: payoutsDatum,
editDeadline: editDeadlineDatum,
}
}, [durationDatum, editDeadlineDatum, payoutsDatum])
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const useResolveEditCycleConflicts = () => {

const { data: queuedCycle } = useProjectQueuedFundingCycle({ projectId })

//
// If no queued cycle, no resolving needs to be done. Return current data
if (!queuedCycle || !fundingCycleMetadata) {
return (data: ReconfigureFundingCycleTxParams) => data
}
Expand All @@ -59,7 +59,8 @@ export const useResolveEditCycleConflicts = () => {
const queuedFcMetadata: V2V3FundingCycleMetadata = queuedCycle[1]

return (data: ReconfigureFundingCycleTxParams) => {
// if the queued cycle's NFT data has changed, inject the queued NFT data.
// If the queued cycle's NFT data has changed, inject the queued *NFT data* only.
// I.e. Calling from "Edit cycle", NFT tx has be called same cycle, pass NFT data into "Edit cycle" tx
if (hasNftConflict(fundingCycleMetadata, queuedFcMetadata)) {
return {
...data,
Expand All @@ -71,7 +72,8 @@ export const useResolveEditCycleConflicts = () => {
},
}
}

// If the queued cycle's NFT data has not changed but there is a queued funding cycle,
// we can assume we're calling from "Launch NFTs" and need to pass the *all the queued FC data*.
return {
...data,
fundingCycleMetadata: {
Expand Down

0 comments on commit 6ad8e35

Please sign in to comment.