@@ -2,25 +2,8 @@ import { useState } from 'react';
22import { useRouter } from 'next/navigation' ;
33import { toast } from 'sonner' ;
44import { useWalletContext } from '@/components/providers/wallet-provider' ;
5- import { signTransaction } from '@/lib/config/wallet-kit' ;
6- import {
7- useInitializeEscrow ,
8- useFundEscrow ,
9- useSendTransaction ,
10- } from '@trustless-work/escrow' ;
11- import {
12- EscrowType ,
13- EscrowRequestResponse ,
14- Status ,
15- InitializeMultiReleaseEscrowResponse ,
16- FundEscrowPayload ,
17- } from '@trustless-work/escrow' ;
18- import {
19- createHackathonEscrow ,
20- getTotalPrizePoolForFunding ,
21- } from '@/lib/utils/hackathon-escrow' ;
22- import { transformToApiFormat } from '@/lib/utils/hackathon-form-transforms' ;
23- import type { Hackathon , PublishHackathonRequest } from '@/lib/api/hackathons' ;
5+ import { getTotalPrizePoolForFunding } from '@/lib/utils/hackathon-escrow' ;
6+ import type { Hackathon } from '@/lib/api/hackathons' ;
247import type { RewardsFormData } from '@/components/organization/hackathons/new/tabs/schemas/rewardsSchema' ;
258import type { InfoFormData } from '@/components/organization/hackathons/new/tabs/schemas/infoSchema' ;
269import type { TimelineFormData } from '@/components/organization/hackathons/new/tabs/schemas/timelineSchema' ;
@@ -55,9 +38,6 @@ export const useHackathonPublish = ({
5538} : UseHackathonPublishProps ) => {
5639 const router = useRouter ( ) ;
5740 const { walletAddress } = useWalletContext ( ) ;
58- const { deployEscrow } = useInitializeEscrow ( ) ;
59- const { fundEscrow } = useFundEscrow ( ) ;
60- const { sendTransaction } = useSendTransaction ( ) ;
6141 const [ isPublishing , setIsPublishing ] = useState ( false ) ;
6242
6343 const publish = async ( ) => {
@@ -97,128 +77,18 @@ export const useHackathonPublish = ({
9777 return ;
9878 }
9979
80+ if ( ! draftId ) {
81+ toast . error ( 'Draft ID is required' ) ;
82+ return ;
83+ }
84+
10085 setIsPublishing ( true ) ;
101- let contractId : string | undefined ;
10286
10387 try {
104- toast . info ( 'Creating escrow contract...' ) ;
105- const escrowPayload = createHackathonEscrow ( {
106- signer : walletAddress ,
107- organizationAddress : walletAddress ,
108- hackathonTitle : stepData . information . name || 'Hackathon' ,
109- hackathonDescription : stepData . information . description || '' ,
110- rewards : stepData . rewards ,
111- } ) ;
112-
113- const escrowResponse : EscrowRequestResponse = await deployEscrow (
114- escrowPayload ,
115- 'multi-release' as EscrowType
116- ) ;
117-
118- if (
119- escrowResponse . status !== ( 'SUCCESS' as Status ) ||
120- ! escrowResponse . unsignedTransaction
121- ) {
122- const errorMessage =
123- 'message' in escrowResponse &&
124- typeof escrowResponse . message === 'string'
125- ? escrowResponse . message
126- : 'Failed to create escrow' ;
127- throw new Error ( errorMessage ) ;
128- }
129-
130- toast . info ( 'Please sign the escrow deployment transaction...' ) ;
131- const signedEscrowXdr = await signTransaction ( {
132- unsignedTransaction : escrowResponse . unsignedTransaction ,
133- address : walletAddress ,
134- } ) ;
135-
136- if ( ! signedEscrowXdr || signedEscrowXdr . trim ( ) === '' ) {
137- throw new Error ( 'Transaction signing was cancelled' ) ;
138- }
139-
140- const escrowSendResponse = await sendTransaction ( signedEscrowXdr ) ;
141-
142- if (
143- 'status' in escrowSendResponse &&
144- escrowSendResponse . status !== ( 'SUCCESS' as Status )
145- ) {
146- const errorMessage =
147- 'message' in escrowSendResponse &&
148- typeof escrowSendResponse . message === 'string'
149- ? escrowSendResponse . message
150- : 'Failed to deploy escrow' ;
151- throw new Error ( errorMessage ) ;
152- }
153-
154- const escrowData =
155- escrowSendResponse as InitializeMultiReleaseEscrowResponse ;
156- contractId = escrowData . contractId ;
157- const escrowAddress = escrowData . contractId ;
158- const transactionHash = escrowData . contractId ;
159-
160- toast . info ( 'Funding escrow with prize pool...' ) ;
161- const fundPayload : FundEscrowPayload = {
162- contractId : contractId ,
163- signer : walletAddress ,
164- amount : totalPrizeAmount ,
165- } ;
166-
167- const fundResponse : EscrowRequestResponse = await fundEscrow (
168- fundPayload ,
169- 'multi-release' as EscrowType
170- ) ;
171-
172- if (
173- fundResponse . status !== ( 'SUCCESS' as Status ) ||
174- ! fundResponse . unsignedTransaction
175- ) {
176- const errorMessage =
177- 'message' in fundResponse && typeof fundResponse . message === 'string'
178- ? fundResponse . message
179- : 'Failed to fund escrow' ;
180- throw new Error ( errorMessage ) ;
181- }
182-
183- toast . info ( 'Please sign the funding transaction...' ) ;
184- const signedFundXdr = await signTransaction ( {
185- unsignedTransaction : fundResponse . unsignedTransaction ,
186- address : walletAddress ,
187- } ) ;
188-
189- if ( ! signedFundXdr || signedFundXdr . trim ( ) === '' ) {
190- throw new Error ( 'Transaction signing was cancelled' ) ;
191- }
192-
193- const fundSendResponse = await sendTransaction ( signedFundXdr ) ;
194-
195- if (
196- 'status' in fundSendResponse &&
197- fundSendResponse . status !== ( 'SUCCESS' as Status )
198- ) {
199- const errorMessage =
200- 'message' in fundSendResponse &&
201- typeof fundSendResponse . message === 'string'
202- ? fundSendResponse . message
203- : 'Failed to fund escrow' ;
204- throw new Error ( errorMessage ) ;
205- }
206-
20788 toast . info ( 'Publishing hackathon...' ) ;
208- const apiData = transformToApiFormat ( stepData ) as PublishHackathonRequest ;
209- if ( draftId ) {
210- apiData . draftId = draftId ;
211- }
212- apiData . contractId = contractId ;
213- apiData . escrowAddress = escrowAddress ;
214- apiData . transactionHash = transactionHash ;
215- apiData . escrowDetails = {
216- totalPrizeAmount : totalPrizeAmount ,
217- placeholderMilestone : true ,
218- winnerMilestonesToBeAdded : true ,
219- } ;
89+ // The backend now handles the custodial wallet and escrow logic
90+ const hackathon = await publishDraftAction ( draftId , organizationId ) ;
22091
221- const hackathon = await publishDraftAction ( draftId ! , organizationId ! ) ;
22292 toast . success ( 'Hackathon published successfully!' ) ;
22393
22494 if ( organizationId && hackathon . id ) {
@@ -232,23 +102,10 @@ export const useHackathonPublish = ({
232102 let errorMessage = 'Failed to publish hackathon' ;
233103
234104 if ( error instanceof Error ) {
235- const errorMsg = error . message . toLowerCase ( ) ;
236- if (
237- errorMsg . includes ( 'user rejected' ) ||
238- errorMsg . includes ( 'cancelled' ) ||
239- errorMsg . includes ( 'rejected' ) ||
240- errorMsg . includes ( 'transaction signing was cancelled' )
241- ) {
242- errorMessage = 'Transaction signing was cancelled. Please try again.' ;
243- } else if ( errorMsg . includes ( 'sign' ) ) {
244- errorMessage = 'Failed to sign transaction. Please try again.' ;
245- } else {
246- errorMessage = error . message ;
247- }
105+ errorMessage = error . message ;
248106 }
249107
250108 toast . error ( errorMessage ) ;
251-
252109 throw error ;
253110 } finally {
254111 setIsPublishing ( false ) ;
0 commit comments