-
-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d9a8b89
commit e655d8e
Showing
4 changed files
with
245 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
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 { useCallback, useContext } from 'react' | ||
import { transformEditCycleFormFieldsToTxArgs } from '../utils/editRuleset' | ||
import { EditCycleFormFields } from '../views/V4ProjectSettings/EditCyclePage/EditCycleFormFields' | ||
|
||
export interface EditRulesetTxOpts { | ||
onTransactionPending: (hash: `0x${string}`) => void | ||
onTransactionConfirmed: () => void | ||
onTransactionError: (error: Error) => void | ||
} | ||
|
||
/** | ||
* Takes data in EditCycleFormFields format, converts it to Edit Ruleset tx format and passes it to `writeEditRuleset` | ||
* @returns A function that deploys a project. | ||
*/ | ||
export function useEditRulesetTx() { | ||
const { writeContractAsync: writeEditRuleset } = useWriteJbControllerLaunchRulesetsFor() | ||
const { contracts } = useJBContractContext() | ||
|
||
const { addTransaction } = useContext(TxHistoryContext) | ||
|
||
const { userAddress } = useWallet() | ||
|
||
return useCallback( | ||
async (formValues: EditCycleFormFields, | ||
{ | ||
onTransactionPending: onTransactionPendingCallback, | ||
onTransactionConfirmed: onTransactionConfirmedCallback, | ||
onTransactionError: onTransactionErrorCallback, | ||
}: EditRulesetTxOpts | ||
) => { | ||
if ( | ||
!contracts.controller.data || | ||
!contracts.primaryNativeTerminal.data || | ||
!userAddress | ||
) { | ||
return | ||
} | ||
|
||
const args = transformEditCycleFormFieldsToTxArgs({ | ||
formValues, | ||
primaryNativeTerminal: contracts.primaryNativeTerminal.data, | ||
tokenAddress: NATIVE_TOKEN | ||
}) | ||
|
||
try { | ||
// SIMULATE TX: | ||
// const encodedData = encodeFunctionData({ | ||
// abi: jbControllerAbi, // ABI of the contract | ||
// functionName: 'launchRulesetsFor', | ||
// args, | ||
// }) | ||
|
||
const hash = await writeEditRuleset({ | ||
address: contracts.controller.data, | ||
args, | ||
}) | ||
|
||
onTransactionPendingCallback(hash) | ||
addTransaction?.('Edit Ruleset', { hash }) | ||
// const transactionReceipt: WaitForTransactionReceiptReturnType = await waitForTransactionReceipt( | ||
// wagmiConfig, | ||
// { | ||
// hash, | ||
// }, | ||
// ) | ||
|
||
onTransactionConfirmedCallback() | ||
} catch (e) { | ||
onTransactionErrorCallback( | ||
(e as Error) ?? new Error('Transaction failed'), | ||
) | ||
} | ||
}, | ||
[ | ||
contracts.controller.data, | ||
userAddress, | ||
writeEditRuleset, | ||
contracts.primaryNativeTerminal.data, | ||
addTransaction, | ||
], | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import round from "lodash/round"; | ||
import { otherUnitToSeconds } from "utils/format/formatTime"; | ||
import { EditCycleFormFields } from "../views/V4ProjectSettings/EditCyclePage/EditCycleFormFields"; | ||
|
||
export function transformEditCycleFormFieldsToTxArgs({ | ||
formValues, | ||
primaryNativeTerminal, | ||
tokenAddress, | ||
}: { | ||
formValues: EditCycleFormFields; | ||
primaryNativeTerminal: `0x${string}`; | ||
tokenAddress: `0x${string}`; | ||
}) { | ||
const now = round(new Date().getTime() / 1000); | ||
const mustStartAtOrAfter = now; | ||
|
||
const duration = otherUnitToSeconds({ | ||
duration: formValues.duration, | ||
unit: formValues.durationUnit.value, | ||
}) | ||
const weight = BigInt(formValues.issuanceRate); | ||
const decayPercent = formValues.decayPercent; | ||
const approvalHook = formValues.approvalHook; | ||
|
||
const rulesetConfigurations = [ | ||
{ | ||
mustStartAtOrAfter, | ||
duration, | ||
weight, | ||
decayPercent, | ||
approvalHook, | ||
|
||
metadata: { | ||
reservedPercent: formValues.reservedPercent, | ||
redemptionRate: formValues.redemptionRate, | ||
baseCurrency: 1, // Assuming base currency is a constant value, typically USD | ||
pausePay: formValues.pausePay, | ||
pauseRedeem: false, // Defaulting this value since it's not in formValues | ||
pauseCreditTransfers: !formValues.tokenTransfers, | ||
allowOwnerMinting: formValues.allowOwnerMinting, | ||
allowSetCustomToken: false, // Defaulting to false as it's not in formValues | ||
allowTerminalMigration: formValues.allowTerminalMigration, | ||
allowSetTerminals: formValues.allowSetTerminals, | ||
allowSetController: formValues.allowSetController, | ||
allowAddAccountingContext: false, // Defaulting to false as it's not in formValues | ||
allowAddPriceFeed: false, // Defaulting to false as it's not in formValues | ||
ownerMustSendPayouts: false, // Defaulting to false as it's not in formValues | ||
holdFees: formValues.holdFees, | ||
useTotalSurplusForRedemptions: false, // Defaulting to false as it's not in formValues | ||
useDataHookForPay: false, // Defaulting to false as it's not in formValues | ||
useDataHookForRedeem: false, // Defaulting to false as it's not in formValues | ||
dataHook: "0x0000000000000000000000000000000000000000" as `0x${string}`, // Defaulting to a null address | ||
metadata: 0, // Assuming no additional metadata is provided | ||
}, | ||
|
||
splitGroups: [ | ||
{ | ||
groupId: BigInt(1), // Assuming 1 for payout splits | ||
splits: formValues.payoutSplits.map((split) => ({ | ||
preferAddToBalance: Boolean(split.preferAddToBalance), | ||
percent: Number(split.percent.value), | ||
projectId: BigInt(split.projectId), | ||
beneficiary: split.beneficiary as `0x${string}`, | ||
lockedUntil: split.lockedUntil ?? 0, | ||
hook: split.hook as `0x${string}`, | ||
})), | ||
}, | ||
{ | ||
groupId: BigInt(2), // Assuming 2 for reserved tokens splits | ||
splits: formValues.reservedTokensSplits.map((split) => ({ | ||
preferAddToBalance: Boolean(split.preferAddToBalance), | ||
percent: Number(split.percent.value), | ||
projectId: BigInt(split.projectId), | ||
beneficiary: split.beneficiary as `0x${string}`, | ||
lockedUntil: split.lockedUntil ?? 0, | ||
hook: split.hook as `0x${string}`, | ||
})), | ||
}, | ||
], | ||
|
||
fundAccessLimitGroups: [ | ||
{ | ||
terminal: primaryNativeTerminal, | ||
token: tokenAddress, | ||
payoutLimits: [ | ||
{ | ||
amount: BigInt(formValues.payoutLimit ?? "0"), | ||
currency: 1, // Assuming currency is constant (e.g., USD) | ||
}, | ||
], | ||
surplusAllowances: [ | ||
{ | ||
amount: BigInt(0), // Assuming no surplus allowances for now | ||
currency: 1, // Assuming currency is constant (e.g., USD) | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
]; | ||
|
||
const terminalConfigurations = [ | ||
{ | ||
terminal: primaryNativeTerminal, | ||
accountingContextsToAccept: [] as const, | ||
}, | ||
]; | ||
|
||
return [ | ||
BigInt(now), // Convert the current timestamp to bigint for the first argument | ||
rulesetConfigurations, | ||
terminalConfigurations, | ||
formValues.memo ?? "", | ||
] as const; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters