Skip to content

Commit

Permalink
compiles
Browse files Browse the repository at this point in the history
  • Loading branch information
aeolianeth committed Nov 19, 2024
1 parent 4b0d9af commit 9d019e4
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,83 @@
import { TransactionCallbacks } from 'models/transaction'
import { DEFAULT_JB_721_DELEGATE_VERSION } from 'packages/v2v3/hooks/defaultContracts/useDefaultJB721Delegate'
import { ONE_BILLION } from 'constants/numbers'
import { DEFAULT_JB_721_TIER_CATEGORY } from 'constants/transactionDefaults'
import { JBTiered721Flags, NftRewardTier } from 'models/nftRewards'
import { LaunchTxOpts } from 'packages/v4/hooks/useLaunchProjectTx'
import { useLaunchProjectWithNftsTx } from 'packages/v4/hooks/useLaunchProjectWithNftsTx'
import { useCallback, useMemo } from 'react'
import { JB721TierConfig, JB721TiersHookFlags } from 'packages/v4/models/nfts'
import { useCallback } from 'react'
import {
useAppSelector,
useEditingV2V3FundAccessConstraintsSelector,
useEditingV2V3FundingCycleDataSelector,
useEditingV2V3FundingCycleMetadataSelector,
} from 'redux/hooks/useAppSelector'
import { DEFAULT_NFT_FLAGS } from 'redux/slices/editingV2Project'
import { encodeIpfsUri } from 'utils/ipfs'
import { NFT_FUNDING_CYCLE_METADATA_OVERRIDES } from 'utils/nftFundingCycleMetadataOverrides'
import { buildJB721TierParams } from 'utils/nftRewards'
import { sortNftsByContributionFloor } from 'utils/nftRewards'
import { Address, parseEther, zeroAddress } from 'viem'
export const DEFAULT_NFT_MAX_SUPPLY = ONE_BILLION - 1

function nftRewardTierToJB721TierConfig(
rewardTier: NftRewardTier,
cid: string,
): JB721TierConfig {
const price = parseEther(rewardTier.contributionFloor.toString())
const initialSupply = rewardTier.maxSupply ?? DEFAULT_NFT_MAX_SUPPLY
const encodedIPFSUri = encodeIpfsUri(cid)

const reserveFrequency = rewardTier.reservedRate
? rewardTier.reservedRate - 1
: 0
const reserveBeneficiary =
(rewardTier.beneficiary as Address | undefined) ?? zeroAddress
const votingUnits = parseInt(rewardTier.votingWeight ?? '0')
// should default to 0, with useVotingUnits `true`, to save gas

return {
price,
initialSupply,
votingUnits,
reserveFrequency,
reserveBeneficiary,
encodedIPFSUri,
allowOwnerMint: false,
useReserveBeneficiaryAsDefault: false,
transfersPausable: false,
useVotingUnits: true,
cannotBeRemoved: false,
cannotIncreaseDiscountPercent: false,
discountPercent: 0,
remainingSupply: initialSupply,
category: DEFAULT_JB_721_TIER_CATEGORY,
resolvedUri: '',
}
}

function buildJB721TierParams({
cids, // MUST BE SORTED BY CONTRIBUTION FLOOR (TODO: not ideal)
rewardTiers,
}: {
cids: string[]
rewardTiers: NftRewardTier[]
}): JB721TierConfig[] {
const sortedRewardTiers = sortNftsByContributionFloor(rewardTiers)

return cids.map((cid, index) => {
const rewardTier = sortedRewardTiers[index]

return nftRewardTierToJB721TierConfig(rewardTier, cid)
})
}

function toV4Flags(v2v3Flags: JBTiered721Flags): JB721TiersHookFlags {
return {
noNewTiersWithOwnerMinting: v2v3Flags.lockManualMintingChanges,
noNewTiersWithReserves: v2v3Flags.lockReservedTokenChanges,
noNewTiersWithVotes: v2v3Flags.lockVotingUnitChanges,
preventOverspending: v2v3Flags.preventOverspending,
}
}

/**
* Hook that returns a function that deploys a project with NFT rewards.
Expand All @@ -33,22 +100,12 @@ export const useDeployNftProject = () => {
const fundingCycleData = useEditingV2V3FundingCycleDataSelector()
const fundAccessConstraints = useEditingV2V3FundAccessConstraintsSelector()

const collectionName = useMemo(
() =>
nftRewards.collectionMetadata.name
? nftRewards.collectionMetadata.name
: projectMetadata.name,
[nftRewards.collectionMetadata.name, projectMetadata.name],
)
const collectionSymbol = useMemo(
() => nftRewards.collectionMetadata.symbol ?? '',
[nftRewards.collectionMetadata.symbol],
)
const nftFlags = useMemo(
() => nftRewards.flags ?? DEFAULT_NFT_FLAGS,
[nftRewards.flags],
)
const governanceType = nftRewards.governanceType
const collectionName = nftRewards.collectionMetadata.name
? nftRewards.collectionMetadata.name
: projectMetadata.name
const collectionSymbol = nftRewards.collectionMetadata.symbol ?? ''
const nftFlags = nftRewards.flags ?? DEFAULT_NFT_FLAGS
// const governanceType = nftRewards.governanceType
const currency = nftRewards.pricing.currency

/**
Expand All @@ -63,15 +120,14 @@ export const useDeployNftProject = () => {
rewardTierCids,
nftCollectionMetadataUri,

onDone,
onConfirmed,
onCancelled,
onError,
onTransactionPending,
onTransactionConfirmed,
onTransactionError,
}: {
metadataCid: string
rewardTierCids: string[]
nftCollectionMetadataUri: string
} & TransactionCallbacks) => {
} & LaunchTxOpts) => {
if (!collectionName) throw new Error('No collection name or project name')
if (!(rewardTierCids.length && nftRewards.rewardTiers))
throw new Error('No NFTs')
Expand All @@ -80,8 +136,8 @@ export const useDeployNftProject = () => {
const tiers = buildJB721TierParams({
cids: rewardTierCids,
rewardTiers: nftRewards.rewardTiers,
version: DEFAULT_JB_721_DELEGATE_VERSION,
})
const flags = toV4Flags(nftFlags)

return await launchProjectWithNftsTx(
{
Expand All @@ -90,9 +146,8 @@ export const useDeployNftProject = () => {
collectionName,
collectionSymbol,
currency,
governanceType,
tiers,
flags: nftFlags,
flags,
},
projectData: {
owner: inputProjectOwner?.length ? inputProjectOwner : undefined,
Expand All @@ -108,10 +163,9 @@ export const useDeployNftProject = () => {
},
},
{
onDone,
onConfirmed,
onCancelled,
onError,
onTransactionPending,
onTransactionConfirmed,
onTransactionError,
},
)
},
Expand All @@ -123,7 +177,6 @@ export const useDeployNftProject = () => {
reservedTokensGroupedSplits,
launchProjectWithNftsTx,
collectionSymbol,
governanceType,
inputProjectOwner,
fundingCycleData,
mustStartAtOrAfter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export const useDeployProject = () => {
const isNftProject = useIsNftProject()
const uploadNftRewards = useUploadNftRewards()
const deployNftProject = useDeployNftProject()

const deployStandardProject = useDeployStandardProject()

const {
Expand Down
35 changes: 23 additions & 12 deletions src/packages/v4/hooks/useLaunchProjectWithNftsTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from 'packages/v4/models/nfts'
import { useContext } from 'react'
import { DEFAULT_MUST_START_AT_OR_AFTER } from 'redux/slices/editingV2Project'
import { ipfsUri } from 'utils/ipfs'
import {
Address,
toBytes,
Expand All @@ -34,6 +35,7 @@ import {
SUPPORTED_JB_CONTROLLER_ADDRESS,
SUPPORTED_JB_MULTITERMINAL_ADDRESS,
} from './useLaunchProjectTx'

function createSalt() {
const base: string = '0x' + Math.random().toString(16).slice(2) // idk lol
const salt = toHex(toBytes(base, { size: 32 }))
Expand Down Expand Up @@ -65,7 +67,6 @@ export function useLaunchProjectWithNftsTx() {
const defaultJBController = chainId
? SUPPORTED_JB_CONTROLLER_ADDRESS[chainId]
: undefined

const defaultJBETHPaymentTerminal = chainId
? SUPPORTED_JB_MULTITERMINAL_ADDRESS[chainId]
: undefined
Expand All @@ -74,7 +75,7 @@ export function useLaunchProjectWithNftsTx() {
: undefined

const { writeContractAsync: writeLaunchProject } =
useWriteJb721TiersHookProjectDeployerLaunchProjectFor()
useWriteJb721TiersHookProjectDeployerLaunchProjectFor()

return async (
{
Expand Down Expand Up @@ -130,16 +131,21 @@ export function useLaunchProjectWithNftsTx() {

return Promise.resolve(false)
}
const _owner = owner?.length ? owner : userAddress
const _owner = (owner?.length ? owner : userAddress) as Address

const deployTiered721HookData: JBDeploy721TiersHookConfig = {
name: collectionName,
symbol: collectionSymbol,
baseUri: '', // ?
tokenUriResolver: zeroAddress, // ?
contractUri: collectionUri, // ?
tiersConfig: tiers,
reserveBeneficiary: zeroAddress, //?;
baseUri: ipfsUri(''), // ?
tokenUriResolver: zeroAddress,
contractUri: ipfsUri(collectionUri),
tiersConfig: {
currency,
decimals: 18,
prices: zeroAddress,
tiers,
},
reserveBeneficiary: zeroAddress,
flags,
}

Expand All @@ -166,12 +172,17 @@ export function useLaunchProjectWithNftsTx() {
})

const args = [
owner,
_owner,
deployTiered721HookData, //_deployTiered721HookData
launchProjectData, // _launchProjectData,
{
projectUri: launchProjectData[1],
rulesetConfigurations: launchProjectData[2],
terminalConfigurations: launchProjectData[3],
memo: launchProjectData[4],
}, // _launchProjectData,
defaultJBController,
createSalt(),
]
// createSalt(),
] as const

try {
// SIMULATE TX: TODO update for nfts
Expand Down
20 changes: 13 additions & 7 deletions src/packages/v4/models/nfts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { JBRulesetData, JBRulesetMetadata } from 'juice-sdk-core'

import { jb721TiersHookStoreAbi } from 'juice-sdk-react'
import { LaunchV2V3ProjectData } from 'packages/v2v3/hooks/transactor/useLaunchProjectTx'
import { Address, ContractFunctionReturnType } from 'viem'
Expand All @@ -8,23 +7,30 @@ import { FundAccessLimitGroup } from './fundAccessLimits'
import { LaunchProjectJBTerminal } from './terminals'
import { V4CurrencyOption } from './v4CurrencyOption'

type JB721TierParams = Omit<
/**
* @see https://github.com/Bananapus/nana-721-hook/blob/main/src/structs/JB721TierConfig.sol
*/
export type JB721TierConfig = Omit<
ContractFunctionReturnType<
typeof jb721TiersHookStoreAbi,
'view',
'tiersOf'
>[0],
'id'
>
'id' | 'votingUnits'
> & {
useReserveBeneficiaryAsDefault: boolean
useVotingUnits: boolean
votingUnits: number
}

type JB721InitTiersConfig = {
tiers: JB721TierParams[]
tiers: JB721TierConfig[]
currency: number
decimals: number
prices: Address // JBPrices address
}

type JB721TiersHookFlags = {
export type JB721TiersHookFlags = {
noNewTiersWithReserves: boolean
noNewTiersWithVotes: boolean
noNewTiersWithOwnerMinting: boolean
Expand Down Expand Up @@ -70,7 +76,7 @@ interface DeployTiered721DelegateData {
collectionName: string
collectionSymbol: string
currency: V4CurrencyOption
tiers: JB721TierParams[]
tiers: JB721TierConfig[]
flags: JB721TiersHookFlags
}

Expand Down
7 changes: 4 additions & 3 deletions src/packages/v4/utils/launchProjectTransformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,11 @@ export function transformFCMetadataToRulesetMetadata({
}

type LaunchProjectJBSplit = Omit<JBSplit, 'percent'> & { percent: number }

export type LaunchV4ProjectGroupedSplit = Omit<
V4GroupedSplits<SplitGroup>,
'splits'
> & { splits: LaunchProjectJBSplit[] }
'splits' | 'groupId'
> & { splits: LaunchProjectJBSplit[], groupId: bigint }

export function transformV2V3SplitsToV4({
v2v3Splits,
Expand All @@ -135,7 +136,7 @@ export function transformV2V3SplitsToV4({
}): LaunchV4ProjectGroupedSplit[] {
return v2v3Splits.map(group => ({
groupId:
group.group === SplitGroup.ETHPayout ? Number(BigInt(NATIVE_TOKEN)) : 1, // TODO dont hardcode reserved token group as 1n
group.group === SplitGroup.ETHPayout ? BigInt(NATIVE_TOKEN) : 1n, // TODO dont hardcode reserved token group as 1n
splits: group.splits.map(split => ({
preferAddToBalance: Boolean(split.preferClaimed),
percent: split.percent,
Expand Down
9 changes: 9 additions & 0 deletions src/redux/slices/editingV2Project/defaultState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
serializeV2V3FundingCycleData,
serializeV2V3FundingCycleMetadata,
} from 'packages/v2v3/utils/serializers'
import { JB721TiersHookFlags } from 'packages/v4/models/nfts'
import { projectDescriptionTemplate } from 'templates/create/projectDescriptionTemplate'
import { CreateState, ProjectState } from './types'

Expand Down Expand Up @@ -92,6 +93,14 @@ export const DEFAULT_NFT_FLAGS: JBTiered721Flags = {
preventOverspending: false,
}


export const DEFAULT_NFT_FLAGS_V4: JB721TiersHookFlags = {
noNewTiersWithReserves: false,
noNewTiersWithVotes: false,
noNewTiersWithOwnerMinting: false,
preventOverspending: false,
}

const DEFAULT_PROJECT_METADATA_STATE: ProjectMetadata = {
name: '',
infoUri: '',
Expand Down
4 changes: 2 additions & 2 deletions src/utils/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ export function pinataToGatewayUrl(url: string) {
*
* Hex-encoded CIDs are used to store some CIDs on-chain because they are more gas-efficient.
*/
export function encodeIpfsUri(cid: string) {
return '0x' + Buffer.from(base58.decode(cid).slice(2)).toString('hex')
export function encodeIpfsUri(cid: string): `0x${string}` {
return `0x${Buffer.from(base58.decode(cid).slice(2)).toString('hex')}`
}

/**
Expand Down

0 comments on commit 9d019e4

Please sign in to comment.