Skip to content

Commit

Permalink
[PREVIEW] 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 bfffab4
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 15 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 @@ -128,6 +128,7 @@ export const useReconfigureFundingCycle = ({
],
memo,
mustStartAtOrAfter: editingMustStartAtOrAfter,
launchedNewNfts,
})

const txOpts = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,18 @@ 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
}

const queuedFcData: V2V3FundingCycleData = queuedCycle[0]
const queuedFcMetadata: V2V3FundingCycleMetadata = queuedCycle[1]

return (data: ReconfigureFundingCycleTxParams) => {
// if the queued cycle's NFT data has changed, inject the queued NFT data.
return (
data: ReconfigureFundingCycleTxParams & { launchedNewNfts?: boolean },
) => {
// Calling from "Edit cycle" and an NFT tx has be called same cycle: pass that previously queued NFT data into this "Edit cycle" tx
if (hasNftConflict(fundingCycleMetadata, queuedFcMetadata)) {
return {
...data,
Expand All @@ -71,17 +73,20 @@ export const useResolveEditCycleConflicts = () => {
},
}
}

return {
...data,
fundingCycleMetadata: {
...data.fundingCycleMetadata,
...queuedFcMetadata,
},
fundingCycleData: {
...data.fundingCycleData,
...queuedFcData,
},
}
// Calling from "Launch NFTs" and an "Edit cycle" tx has be called same cycle: pass that previously queued data into this "Launch NFTs" tx
if (data.launchedNewNfts) {
return {
...data,
fundingCycleMetadata: {
...data.fundingCycleMetadata,
...queuedFcMetadata,
},
fundingCycleData: {
...data.fundingCycleData,
...queuedFcData,
},
}
// Calling "Edit cycle" when a previously "Edit cycle" tx exists: ignore the old tx data, return the new one
} else return data
}
}

0 comments on commit bfffab4

Please sign in to comment.