Skip to content

Commit f1e64c1

Browse files
authored
Wraeth/v4 project nft (#4521)
1 parent 85e9588 commit f1e64c1

36 files changed

+1342
-226
lines changed
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { useWallet } from 'hooks/Wallet'
2+
import { useReadJb721TiersHookPayCreditsOf } from 'juice-sdk-react'
3+
import { useV4NftRewards } from 'packages/v4/contexts/V4NftRewards/V4NftRewardsProvider'
24
import { V4CurrencyOption } from 'packages/v4/models/v4CurrencyOption'
35
import React from 'react'
46
import { useProjectDispatch } from './redux/hooks'
5-
// import { projectCartActions } from './redux/projectCartSlice'
7+
import { projectCartActions } from './redux/projectCartSlice'
68

79
export type ProjectCartCurrencyAmount = {
810
amount: number
@@ -20,26 +22,26 @@ export const ReduxProjectCartProvider = ({
2022
}: {
2123
children: React.ReactNode
2224
}) => {
23-
// const { rewardTiers } = useContext(NftRewardsContext).nftRewards
25+
const {
26+
nftRewards: { rewardTiers },
27+
} = useV4NftRewards()
2428

2529
const { userAddress } = useWallet()
26-
// const userNftCredits = useNftCredits(userAddress)
30+
const { data: nftCredits } = useReadJb721TiersHookPayCreditsOf({
31+
address: userAddress,
32+
})
2733

2834
const dispatch = useProjectDispatch()
2935

3036
// Set the nfts on load
31-
// useEffect(() => {
32-
// dispatch(projectCartActions.setAllNftRewards(rewardTiers ?? []))
33-
// }, [dispatch, rewardTiers])
37+
React.useEffect(() => {
38+
dispatch(projectCartActions.setAllNftRewards(rewardTiers ?? []))
39+
}, [dispatch, rewardTiers])
3440

3541
// Set the user's NFT credits on load
36-
// useEffect(() => {
37-
// dispatch(
38-
// projectCartActions.setUserNftCredits(
39-
// userNftCredits.data?.toBigInt() ?? 0n,
40-
// ),
41-
// )
42-
// }, [dispatch, userNftCredits.data])
42+
React.useEffect(() => {
43+
dispatch(projectCartActions.setUserNftCredits(nftCredits ?? 0n))
44+
}, [dispatch, nftCredits])
4345

4446
return <>{children}</>
4547
}

src/packages/v4/components/ProjectDashboard/V4PayRedeemCard/NftReward.tsx

Lines changed: 0 additions & 93 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { Trans } from '@lingui/macro'
2+
import { CartItemBadge } from 'components/CartItemBadge'
3+
import { SmallNftSquare } from 'components/NftRewards/SmallNftSquare'
4+
import { useNftCartItem } from 'packages/v4/hooks/useNftCartItem'
5+
import { twMerge } from 'tailwind-merge'
6+
import { ProjectCartNftReward } from '../../../ReduxProjectCartProvider'
7+
8+
export const ReceiveNftItem = ({
9+
className,
10+
nftReward,
11+
}: {
12+
className?: string
13+
nftReward: ProjectCartNftReward
14+
}) => {
15+
const { fileUrl, name, quantity } = useNftCartItem(nftReward)
16+
17+
return (
18+
<div className={twMerge('flex flex-col gap-4', className)}>
19+
<div className="flex items-center justify-between gap-3">
20+
<div className="flex items-center">
21+
<SmallNftSquare
22+
className="h-12 w-12"
23+
nftReward={{
24+
fileUrl: fileUrl ?? '',
25+
name: name ?? '',
26+
}}
27+
/>
28+
<span className="ml-3">{name}</span>
29+
<CartItemBadge className="ml-2">
30+
<Trans>NFT</Trans>
31+
</CartItemBadge>
32+
</div>
33+
<div>{quantity}</div>
34+
</div>
35+
</div>
36+
)
37+
}

src/packages/v4/components/ProjectDashboard/V4PayRedeemCard/PayProjectModal/components/ReceiveSection.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from '../hooks/usePayProjectModal/usePayProjectModal'
77
import { useProjectPaymentTokens } from '../hooks/useProjectPaymentTokens'
88
import { EditRewardBeneficiary } from './EditRewardBeneficiary'
9+
import { ReceiveNftItem } from './ReceiveNftItem'
910
import { ReceiveTokensItem } from './ReceiveTokensItem'
1011

1112
export const ReceiveSection = ({ className }: { className?: string }) => {
@@ -36,6 +37,13 @@ export const ReceiveSection = ({ className }: { className?: string }) => {
3637
</span>
3738
</div>
3839
<ReceiveTokensItem className="mt-5" />
40+
{nftRewards.map(nftReward => (
41+
<ReceiveNftItem
42+
key={nftReward.id}
43+
nftReward={nftReward}
44+
className="mt-5"
45+
/>
46+
))}
3947
</div>
4048
)
4149
}

src/packages/v4/components/ProjectDashboard/V4PayRedeemCard/PayProjectModal/hooks/usePayProjectModal/usePayProjectTx.ts

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1+
import { waitForTransactionReceipt } from '@wagmi/core'
2+
import { TxHistoryContext } from 'contexts/Transaction/TxHistoryContext'
13
import { FormikHelpers } from 'formik'
24
import { useWallet } from 'hooks/Wallet'
35
import { useCurrencyConverter } from 'hooks/useCurrencyConverter'
4-
import { useProjectSelector } from 'packages/v4/components/ProjectDashboard/redux/hooks'
5-
import { ProjectPayReceipt } from 'packages/v4/views/V4ProjectDashboard/hooks/useProjectPageQueries'
6-
// import { NftRewardsContext } from 'packages/v4/contexts/NftRewards/NftRewardsContext'
7-
// import { useProjectHasErc20 } from 'packages/v4/hooks/useProjectHasErc20'
8-
import { waitForTransactionReceipt } from '@wagmi/core'
9-
import { TxHistoryContext } from 'contexts/Transaction/TxHistoryContext'
10-
import { NATIVE_TOKEN } from 'juice-sdk-core'
6+
import { DEFAULT_METADATA, NATIVE_TOKEN } from 'juice-sdk-core'
117
import {
128
useJBContractContext,
9+
useJBRulesetContext,
10+
usePreparePayMetadata,
1311
useWriteJbMultiTerminalPay,
1412
} from 'juice-sdk-react'
15-
// import { useProjectHasErc20 } from 'packages/v2v3/hooks/useProjectHasErc20'
13+
import { useProjectSelector } from 'packages/v4/components/ProjectDashboard/redux/hooks'
14+
import { useV4NftRewards } from 'packages/v4/contexts/V4NftRewards/V4NftRewardsProvider'
1615
import { V4_CURRENCY_ETH } from 'packages/v4/utils/currency'
16+
import { ProjectPayReceipt } from 'packages/v4/views/V4ProjectDashboard/hooks/useProjectPageQueries'
1717
import { wagmiConfig } from 'packages/v4/wagmiConfig'
1818
import { useCallback, useContext, useMemo } from 'react'
1919
import { buildPaymentMemo } from 'utils/buildPaymentMemo'
@@ -42,11 +42,12 @@ export const usePayProjectTx = ({
4242
const { payAmount, chosenNftRewards } = useProjectSelector(
4343
state => state.projectCart,
4444
)
45-
// const {
46-
// nftRewards: { rewardTiers },
47-
// } = useContext(NftRewardsContext)
45+
const {
46+
nftRewards: { rewardTiers },
47+
} = useV4NftRewards()
4848
const converter = useCurrencyConverter()
4949
const { receivedTickets } = useProjectPaymentTokens()
50+
// TODO: implement
5051
// const projectHasErc20 = useProjectHasErc20()
5152

5253
const buildPayReceipt = useCallback(
@@ -76,10 +77,21 @@ export const usePayProjectTx = ({
7677
}
7778
}, [payAmount, converter])
7879

79-
// const prepareDelegateMetadata = usePrepareDelegatePayMetadata(weiAmount, {
80-
// nftRewards: chosenNftRewards,
81-
// receivedTickets,
82-
// })
80+
const {
81+
rulesetMetadata: { data: rulesetMetadata },
82+
} = useJBRulesetContext()
83+
const metadata = usePreparePayMetadata(
84+
rulesetMetadata?.dataHook
85+
? {
86+
jb721Hook: {
87+
dataHookAddress: rulesetMetadata.dataHook,
88+
tierIdsToMint: chosenNftRewards
89+
.map(({ id, quantity }) => Array(quantity).fill(BigInt(id)))
90+
.flat(),
91+
},
92+
}
93+
: undefined,
94+
)
8395

8496
const { writeContractAsync: writePay } = useWriteJbMultiTerminalPay()
8597
const { contracts, projectId } = useJBContractContext()
@@ -103,13 +115,13 @@ export const usePayProjectTx = ({
103115
const memo = buildPaymentMemo({
104116
text: messageString,
105117
imageUrl: attachedUrl,
106-
// nftUrls: chosenNftRewards
107-
// .map(
108-
// ({ id }) =>
109-
// (rewardTiers ?? []).find(({ id: tierId }) => tierId === id)
110-
// ?.fileUrl,
111-
// )
112-
// .filter((url): url is string => !!url),
118+
nftUrls: chosenNftRewards
119+
.map(
120+
({ id }) =>
121+
(rewardTiers ?? []).find(({ id: tierId }) => tierId === id)
122+
?.fileUrl,
123+
)
124+
.filter((url): url is string => !!url),
113125
})
114126
const beneficiary = (values.beneficiaryAddress ?? userAddress) as Address
115127
const args = [
@@ -119,9 +131,15 @@ export const usePayProjectTx = ({
119131
beneficiary,
120132
0n,
121133
memo,
122-
'0x0',
134+
metadata ?? DEFAULT_METADATA,
123135
] as const
124136

137+
// SIMULATE TRANSACTION:
138+
// const encodedData = encodeFunctionData({
139+
// abi: jbMultiTerminalAbi, // ABI of the contract
140+
// functionName: 'pay',
141+
// args,
142+
// })
125143
try {
126144
const hash = await writePay({
127145
address: contracts.primaryNativeTerminal.data,
@@ -147,21 +165,19 @@ export const usePayProjectTx = ({
147165
}
148166
},
149167
[
150-
// projectHasErc20,
151-
buildPayReceipt,
152-
// chosenNftRewards,
153-
onTransactionConfirmedCallback,
154-
onTransactionErrorCallback,
155-
onTransactionPendingCallback,
156-
// payProjectTx,
157-
// rewardTiers,
158168
weiAmount,
169+
contracts.primaryNativeTerminal.data,
159170
userAddress,
160-
// prepareDelegateMetadata,
171+
chosenNftRewards,
161172
projectId,
173+
metadata,
174+
rewardTiers,
162175
writePay,
163-
contracts.primaryNativeTerminal.data,
176+
onTransactionPendingCallback,
164177
addTransaction,
178+
onTransactionConfirmedCallback,
179+
buildPayReceipt,
180+
onTransactionErrorCallback,
165181
],
166182
)
167183
}

0 commit comments

Comments
 (0)