In the Post Job section, users can create a service. We will use and detail the function createService
Good to Know
In this example, we will use reactJS, Ether.js and formik to handle the form on the frontend. You will find a full code example with all imports at the end of tutorial.
// The interface for the inupt field of your forminterfaceIFormValues { title:string; about:string; keywords:string; rateToken:string; rateAmount:number;}// As we using Formik we use the validationSchema to check all the input valueconstvalidationSchema=Yup.object({ title:Yup.string().required('Please provide a title for your service'), about:Yup.string().required('Please provide a description of your service'), keywords:Yup.string().required('Please provide keywords for your service'), rateToken:Yup.string().required('Please select a payment token'), rateAmount:Yup.number().required('Please provide an amount for your service').when('rateToken', {is: (rateToken:string) => rateToken !=='',then: schema =>schema.moreThan( selectedToken?FixedNumber.from(ethers.utils.formatUnits(selectedToken?.minimumTransactionAmount asBigNumberish,selectedToken?.decimals, ), ).toUnsafeFloat():0,`Amount must be greater than ${ selectedToken?FixedNumber.from(ethers.utils.formatUnits(selectedToken?.minimumTransactionAmount asBigNumberish,selectedToken?.decimals, ), ).toUnsafeFloat():0}`, ), }), });
Let's create our form
return ( <FormikinitialValues={initialValues} onSubmit={onSubmit} validationSchema={validationSchema}> {({ isSubmitting, setFieldValue }) => ( <Form> <divclassName='grid grid-cols-1 gap-6 border border-gray-200 rounded-md p-8'> <labelclassName='block'> <spanclassName='text-gray-700'>Title</span> <Fieldtype='text'id='title'name='title'className='mt-1 mb-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50'placeholder='' /> <spanclassName='text-red-500'> <ErrorMessagename='title' /> </span> </label> <labelclassName='block'> <spanclassName='text-gray-700'>About</span> <Fieldas='textarea'id='about'name='about'className='mt-1 mb-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50'placeholder='' /> <spanclassName='text-red-500'> <ErrorMessagename='about' /> </span> </label> .................... .................... /* Formik will catch the submit button and trigger the onSubmit function in the next section */ <SubmitButtonisSubmitting={isSubmitting} label='Post' /> );
② Handle Submit and Post the service
On submit, we create a new transaction and call the createService for that we will need our contract instance and all the parameter including the CID (offchain data stored on IPFS)
constonSubmit=async ( values:IFormValues, { setSubmitting, resetForm, }: { setSubmitting: (isSubmitting:boolean) =>void; resetForm: () =>void }, ) => {/*............................. we proceed to different check and date formatting .............................*//* as we using the defender API to manage signature we need to get it as it's a parameter of the service creation function */// Get platform signatureconstsignature=awaitgetServiceSignature({ profileId:Number(user?.id), cid });// We need to create the CID to post some data off-chainconstcid=awaitpostToIPFS(JSON.stringify({ title:values.title, about:values.about, keywords:values.keywords, role:'buyer', rateToken:values.rateToken, rateAmount: parsedRateAmountString, }), );// We need the contract instance to call the functionconstcontract=newethers.Contract(config.contracts.serviceRegistry,ServiceRegistry.abi, signer, );// we call the createService functionconsttx=awaitcontract.createService(user?.id,process.env.NEXT_PUBLIC_PLATFORM_ID, cid, signature, );/*............................. */
③ Get the Proposal by User With Subgraph API
Here is an example of a Graph Query you can use to get the service by id