Skip to content

Commit

Permalink
fix: refactor v4 create flow to not rely on project provider
Browse files Browse the repository at this point in the history
  • Loading branch information
aeolianeth committed Sep 5, 2024
1 parent 6a6e9dc commit d9a8b89
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 65 deletions.
121 changes: 73 additions & 48 deletions src/packages/v4/hooks/useLaunchProjectTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@ import { DEFAULT_MEMO } from 'constants/transactionDefaults'
import { TxHistoryContext } from 'contexts/Transaction/TxHistoryContext'
import { useWallet } from 'hooks/Wallet'
import { NATIVE_TOKEN } from 'juice-sdk-core'
import { useJBContractContext, useWriteJbControllerLaunchProjectFor } from 'juice-sdk-react'
import { useWriteJbControllerLaunchProjectFor } from 'juice-sdk-react'
import { LaunchV2V3ProjectData } from 'packages/v2v3/hooks/transactor/useLaunchProjectTx'
import { useCallback, useContext } from 'react'
import { DEFAULT_MUST_START_AT_OR_AFTER } from 'redux/slices/editingV2Project'
import { WaitForTransactionReceiptReturnType } from 'viem'
import { LaunchV2V3ProjectArgs, transformV2V3CreateArgsToV4 } from '../utils/launchProject'
import { Address, WaitForTransactionReceiptReturnType } from 'viem'
import {
LaunchV2V3ProjectArgs,
transformV2V3CreateArgsToV4,
} from '../utils/launchProject'
import { wagmiConfig } from '../wagmiConfig'
import { useCurrentRouteChainId } from './useCurrentRouteChainId'

const CREATE_EVENT_IDX = 2
const PROJECT_ID_TOPIC_IDX = 1
const HEX_BASE = 16

export interface LaunchTxOpts {
onTransactionPending: (hash: `0x${string}`) => void
onTransactionConfirmed: (hash: `0x${string}`, projectId: number) => void
onTransactionConfirmed: (hash: `0x${string}`, projectId: number) => void
onTransactionError: (error: Error) => void
}

Expand All @@ -33,92 +37,112 @@ const getProjectIdFromLaunchReceipt = (
txReceipt?.logs[CREATE_EVENT_IDX]?.topics?.[PROJECT_ID_TOPIC_IDX]
if (!projectIdHex) return 0

const projectId = parseInt(projectIdHex, HEX_BASE);
const projectId = parseInt(projectIdHex, HEX_BASE)
return projectId
}

// todo no ideal to hardcode these addresses
const SUPPORTED_JB_MULTITERMINAL_ADDRESS = {
'84532': '0x88e8ED1Dd942B2aB4Adc1e3b50Bd0EdB9822231E' as Address,
'421614': '0x88e8ED1Dd942B2aB4Adc1e3b50Bd0EdB9822231E' as Address,
'11155111': '0x88e8ED1Dd942B2aB4Adc1e3b50Bd0EdB9822231E' as Address,
'11155420': '0x88e8ED1Dd942B2aB4Adc1e3b50Bd0EdB9822231E' as Address,
}

const SUPPORTED_JB_CONTROLLER_ADDRESS = {
'84532': '0x1e4c8DFfE8D72aeB63e8dDbE9eF89bc368cbbE99' as Address,
'421614': '0x1e4c8DFfE8D72aeB63e8dDbE9eF89bc368cbbE99' as Address,
'11155111': '0x1e4c8DFfE8D72aeB63e8dDbE9eF89bc368cbbE99' as Address,
'11155420': '0x1e4c8DFfE8D72aeB63e8dDbE9eF89bc368cbbE99' as Address,
}

/**
* Takes data in V2V3 format, converts it to v4 format and passes it to `writeLaunchProject`
* @returns A function that deploys a project.
*/
export function useLaunchProjectTx() {
const { writeContractAsync: writeLaunchProject } = useWriteJbControllerLaunchProjectFor()
const { contracts } = useJBContractContext()
const { writeContractAsync: writeLaunchProject } =
useWriteJbControllerLaunchProjectFor()

const chainId = useCurrentRouteChainId()
const terminalAddress = chainId
? SUPPORTED_JB_MULTITERMINAL_ADDRESS[chainId]
: undefined

const controllerAddress = chainId
? SUPPORTED_JB_CONTROLLER_ADDRESS[chainId]
: undefined

const { addTransaction } = useContext(TxHistoryContext)

const { userAddress } = useWallet()

return useCallback(
async ({
owner,
projectMetadataCID,
fundingCycleData,
fundingCycleMetadata,
fundAccessConstraints,
groupedSplits = [],
mustStartAtOrAfter = DEFAULT_MUST_START_AT_OR_AFTER,
}: LaunchV2V3ProjectData,
{
onTransactionPending: onTransactionPendingCallback,
onTransactionConfirmed: onTransactionConfirmedCallback,
onTransactionError: onTransactionErrorCallback,
}: LaunchTxOpts
) => {
if (
!contracts.controller.data ||
!contracts.primaryNativeTerminal.data ||
!userAddress
) {
async (
{
owner,
projectMetadataCID,
fundingCycleData,
fundingCycleMetadata,
fundAccessConstraints,
groupedSplits = [],
mustStartAtOrAfter = DEFAULT_MUST_START_AT_OR_AFTER,
}: LaunchV2V3ProjectData,
{
onTransactionPending: onTransactionPendingCallback,
onTransactionConfirmed: onTransactionConfirmedCallback,
onTransactionError: onTransactionErrorCallback,
}: LaunchTxOpts,
) => {
if (!controllerAddress || !terminalAddress || !userAddress || !chainId) {
return
}

const _owner = owner && owner.length ? owner : userAddress

const v2v3Args = [
_owner,
[projectMetadataCID, JUICEBOX_MONEY_PROJECT_METADATA_DOMAIN],
[projectMetadataCID, JUICEBOX_MONEY_PROJECT_METADATA_DOMAIN],
fundingCycleData,
fundingCycleMetadata,
mustStartAtOrAfter,
fundingCycleMetadata,
mustStartAtOrAfter,
groupedSplits,
fundAccessConstraints,
[contracts.primaryNativeTerminal.data], // _terminals, just supporting single for now
// Eventually should be something like:
// getTerminalsFromFundAccessConstraints(
// fundAccessConstraints,
// contracts.primaryNativeTerminal.data,
// ),
[terminalAddress], // _terminals, just supporting single for now
// Eventually should be something like:
// getTerminalsFromFundAccessConstraints(
// fundAccessConstraints,
// contracts.primaryNativeTerminal.data,
// ),
DEFAULT_MEMO,
] as LaunchV2V3ProjectArgs

const args = transformV2V3CreateArgsToV4({
v2v3Args,
primaryNativeTerminal: contracts.primaryNativeTerminal.data,
tokenAddress: NATIVE_TOKEN
primaryNativeTerminal: terminalAddress,
tokenAddress: NATIVE_TOKEN,
})

try {
// SIMULATE TX:
// const encodedData = encodeFunctionData({
// abi: jbControllerAbi, // ABI of the contract
// functionName: 'launchProjectFor',
// args,
// functionName: 'launchProjectFor',
// args,
// })

const hash = await writeLaunchProject({
address: contracts.controller.data,
chainId,
address: controllerAddress,
args,
})

onTransactionPendingCallback(hash)
addTransaction?.('Launch Project', { hash })
const transactionReceipt: WaitForTransactionReceiptReturnType = await waitForTransactionReceipt(
wagmiConfig,
{
const transactionReceipt: WaitForTransactionReceiptReturnType =
await waitForTransactionReceipt(wagmiConfig, {
hash,
},
)
})

const newProjectId = getProjectIdFromLaunchReceipt(transactionReceipt)

Expand All @@ -130,10 +154,11 @@ export function useLaunchProjectTx() {
}
},
[
contracts.controller.data,
chainId,
controllerAddress,
userAddress,
writeLaunchProject,
contracts.primaryNativeTerminal.data,
terminalAddress,
addTransaction,
],
)
Expand Down
19 changes: 2 additions & 17 deletions src/pages/create/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ import { AppWrapper } from 'components/common/CoreAppWrapper/CoreAppWrapper'
import { Head } from 'components/common/Head/Head'
import { CV_V3 } from 'constants/cv'
import { FEATURE_FLAGS } from 'constants/featureFlags'
import { OPEN_IPFS_GATEWAY_HOSTNAME } from 'constants/ipfs'
import { NETWORKS_BY_NAME } from 'constants/networks'
import { SiteBaseUrl } from 'constants/url'
import { TransactionProvider } from 'contexts/Transaction/TransactionProvider'
import { JBProjectProvider } from 'juice-sdk-react'
import { NetworkName } from 'models/networkName'
import { Create as V2V3Create } from 'packages/v2v3/components/Create/Create'
import { V2V3ContractsProvider } from 'packages/v2v3/contexts/Contracts/V2V3ContractsProvider'
import { V2V3CurrencyProvider } from 'packages/v2v3/contexts/V2V3CurrencyProvider'
Expand All @@ -33,16 +29,7 @@ export default function CreatePage() {
if (featureFlagEnabled(FEATURE_FLAGS.V4)) {
contentByVersion = (
<WagmiProvider config={wagmiConfig}>
{/* Hardcode V4 create to Sepoila for now */}
<JBProjectProvider
chainId={NETWORKS_BY_NAME[NetworkName.sepolia].chainId as 11155111}
projectId={1n}
ctxProps={{
metadata: { ipfsGatewayHostname: OPEN_IPFS_GATEWAY_HOSTNAME },
}}
>
<V4Create />
</JBProjectProvider>
<V4Create />
</WagmiProvider>
)
}
Expand All @@ -55,9 +42,7 @@ export default function CreatePage() {
/>

<AppWrapper>
<Provider store={store}>
{contentByVersion}
</Provider>
<Provider store={store}>{contentByVersion}</Provider>
</AppWrapper>
</>
)
Expand Down

0 comments on commit d9a8b89

Please sign in to comment.