Skip to content

Commit

Permalink
fix: v4 edit ruleset tx (#4467)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyd-eth authored Sep 27, 2024
1 parent 7a23aa8 commit 4209bbc
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,11 @@ export const usePayoutsTable = () => {
})
: undefined // undefined means DL is infinite

const newSplitPercentPPB = round(
(_amount / (newDistributionLimit ?? 0)) * ONE_BILLION,
const newSplitPercentPPB = round(newDistributionLimit ?
(_amount / (newDistributionLimit)) * ONE_BILLION
: 0
)

let adjustedSplits: Split[] = newSplits ?? payoutSplits
// recalculate all split percents based on newly added split amount
if (newDistributionLimit && !distributionLimitIsInfinite) {
Expand Down
13 changes: 8 additions & 5 deletions src/packages/v4/hooks/useEditRulesetTx.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TxHistoryContext } from 'contexts/Transaction/TxHistoryContext'
import { useWallet } from 'hooks/Wallet'
import { NATIVE_TOKEN } from 'juice-sdk-core'
import { useJBContractContext, useWriteJbControllerLaunchRulesetsFor } from 'juice-sdk-react'
import { useJBContractContext, useWriteJbControllerQueueRulesetsOf } from 'juice-sdk-react'
import { useCallback, useContext } from 'react'
import { transformEditCycleFormFieldsToTxArgs } from '../utils/editRuleset'
import { EditCycleFormFields } from '../views/V4ProjectSettings/EditCyclePage/EditCycleFormFields'
Expand All @@ -17,8 +17,8 @@ export interface EditMetadataTxOpts {
* @returns A function that deploys a project.
*/
export function useEditRulesetTx() {
const { writeContractAsync: writeEditRuleset } = useWriteJbControllerLaunchRulesetsFor()
const { contracts } = useJBContractContext()
const { writeContractAsync: writeEditRuleset } = useWriteJbControllerQueueRulesetsOf()
const { contracts, projectId } = useJBContractContext()

const { addTransaction } = useContext(TxHistoryContext)

Expand All @@ -43,16 +43,19 @@ export function useEditRulesetTx() {
const args = transformEditCycleFormFieldsToTxArgs({
formValues,
primaryNativeTerminal: contracts.primaryNativeTerminal.data,
tokenAddress: NATIVE_TOKEN
tokenAddress: NATIVE_TOKEN,
projectId
})

try {
// SIMULATE TX:
// const encodedData = encodeFunctionData({
// abi: jbControllerAbi, // ABI of the contract
// functionName: 'launchRulesetsFor',
// functionName: 'queueRulesetsOf',
// args,
// })
// console.log('contracts address: ', contracts.controller.data)
// console.log('encodedData: ', encodedData)

const hash = await writeEditRuleset({
address: contracts.controller.data,
Expand Down
2 changes: 1 addition & 1 deletion src/packages/v4/utils/distributions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export function ensureSplitsSumTo100Percent({
}

// Calculate the ratio to adjust each split by
const ratio = max / currentTotal
const ratio = currentTotal ? max / currentTotal : 0

// Adjust each split
const adjustedSplits = splits.map(split => {
Expand Down
20 changes: 8 additions & 12 deletions src/packages/v4/utils/editRuleset.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { NATIVE_TOKEN } from "juice-sdk-core";
import round from "lodash/round";
import { parseWad } from "utils/format/formatNumber";
import { otherUnitToSeconds } from "utils/format/formatTime";
import { EditCycleFormFields } from "../views/V4ProjectSettings/EditCyclePage/EditCycleFormFields";

export function transformEditCycleFormFieldsToTxArgs({
formValues,
primaryNativeTerminal,
tokenAddress,
projectId,
}: {
formValues: EditCycleFormFields;
primaryNativeTerminal: `0x${string}`;
tokenAddress: `0x${string}`;
projectId: bigint;
}) {
const now = round(new Date().getTime() / 1000);
const mustStartAtOrAfter = now;
Expand Down Expand Up @@ -56,7 +60,7 @@ export function transformEditCycleFormFieldsToTxArgs({

splitGroups: [
{
groupId: BigInt(1), // Assuming 1 for payout splits
groupId: BigInt(NATIVE_TOKEN),
splits: formValues.payoutSplits.map((split) => ({
preferAddToBalance: Boolean(split.preferAddToBalance),
percent: Number(split.percent.value),
Expand All @@ -67,7 +71,7 @@ export function transformEditCycleFormFieldsToTxArgs({
})),
},
{
groupId: BigInt(2), // Assuming 2 for reserved tokens splits
groupId: BigInt(1),
splits: formValues.reservedTokensSplits.map((split) => ({
preferAddToBalance: Boolean(split.preferAddToBalance),
percent: Number(split.percent.value),
Expand All @@ -85,7 +89,7 @@ export function transformEditCycleFormFieldsToTxArgs({
token: tokenAddress,
payoutLimits: [
{
amount: BigInt(formValues.payoutLimit ?? "0"),
amount: parseWad(formValues.payoutLimit).toBigInt(),
currency: 1, // Assuming currency is constant (e.g., USD)
},
],
Expand All @@ -100,17 +104,9 @@ export function transformEditCycleFormFieldsToTxArgs({
},
];

const terminalConfigurations = [
{
terminal: primaryNativeTerminal,
accountingContextsToAccept: [] as const,
},
];

return [
BigInt(now), // Convert the current timestamp to bigint for the first argument
projectId,
rulesetConfigurations,
terminalConfigurations,
formValues.memo ?? "",
] as const;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { WeiPerEther } from '@ethersproject/constants'
import { CurrencyName } from 'constants/currency'
import { JBSplit } from 'juice-sdk-core'
import { distributionLimitsEqual } from 'packages/v4/utils/distributions'
import { MAX_PAYOUT_LIMIT } from 'packages/v4/utils/math'
import { splitsListsHaveDiff } from 'packages/v4/utils/v4Splits'
import { parseWad } from 'utils/format/formatNumber'
import { useEditCycleFormContext } from '../../EditCycleFormContext'

export const usePayoutsSectionValues = () => {
Expand All @@ -27,10 +27,10 @@ export const usePayoutsSectionValues = () => {

const newDistributionLimitNum: number = editCycleForm?.getFieldValue('payoutLimit')
const newDistributionLimit =
newDistributionLimitNum ? BigInt(newDistributionLimitNum) * WeiPerEther.toBigInt() : MAX_PAYOUT_LIMIT
newDistributionLimitNum ? parseWad(newDistributionLimitNum).toBigInt() : MAX_PAYOUT_LIMIT

const currentDistributionLimitNum = initialFormData?.payoutLimit
const currentDistributionLimit = currentDistributionLimitNum ? BigInt(currentDistributionLimitNum) * WeiPerEther.toBigInt() : MAX_PAYOUT_LIMIT
const currentDistributionLimit = currentDistributionLimitNum ? parseWad(currentDistributionLimitNum).toBigInt() : MAX_PAYOUT_LIMIT

const distributionLimitHasDiff =
!distributionLimitsEqual(currentDistributionLimit, newDistributionLimit) ||
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useJBTokenContext } from 'juice-sdk-react'
import { useJBRuleset, useJBTokenContext } from 'juice-sdk-react'
import round from 'lodash/round'
import { useJBUpcomingRuleset } from 'packages/v4/hooks/useJBUpcomingRuleset'
import { splitsListsHaveDiff } from 'packages/v4/utils/v4Splits'
Expand All @@ -11,6 +11,7 @@ export const useTokensSectionValues = () => {

const formValues: EditCycleFormFields = editCycleForm?.getFieldsValue(true)

const { data: ruleset } = useJBRuleset()
const {
ruleset: upcomingRuleset,
} = useJBUpcomingRuleset()
Expand Down Expand Up @@ -43,11 +44,13 @@ export const useTokensSectionValues = () => {
)

const onlyDiscountRateApplied =
upcomingRuleset &&
newMintRate &&
round(upcomingRuleset?.weight.toFloat(), 4) === round(newMintRate, 4)
(
upcomingRuleset &&
newMintRate &&
round(upcomingRuleset?.weight.toFloat(), 4) === round(newMintRate, 4)
) || ruleset?.duration === 0

const mintRateHasDiff = !onlyDiscountRateApplied
const mintRateHasDiff = !onlyDiscountRateApplied

const reservedRateHasDiff = Boolean(
currentReservedRate &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
secondsToOtherUnit,
} from 'utils/format/formatTime'

import { Ether } from 'juice-sdk-core'
import { useJBRuleset, useJBRulesetMetadata } from 'juice-sdk-react'
import { useJBUpcomingRuleset } from 'packages/v4/hooks/useJBUpcomingRuleset'
import { usePayoutLimit } from 'packages/v4/hooks/usePayoutLimit'
Expand All @@ -29,6 +30,8 @@ export const useLoadEditCycleData = () => {
const { data: payoutLimit } = usePayoutLimit()

const [editCycleForm] = Form.useForm<EditCycleFormFields>()

const payoutLimitAmount = new Ether(payoutLimit.amount).toFloat()

useEffect(() => {
if (ruleset && rulesetMetadata) {
Expand Down Expand Up @@ -62,7 +65,7 @@ export const useLoadEditCycleData = () => {
allowTerminalMigration: rulesetMetadata.allowTerminalMigration,
pausePay: rulesetMetadata.pausePay,
payoutSplits: payoutSplits ?? [],
payoutLimit: payoutLimit ? Number(payoutLimit.amount) : undefined, // TODO: format
payoutLimit: payoutLimitAmount,
payoutLimitCurrency: V4CurrencyName(payoutLimit?.currency) ?? 'ETH',
holdFees: rulesetMetadata?.holdFees,
issuanceRate,
Expand Down

0 comments on commit 4209bbc

Please sign in to comment.