In this section, we will cover in detail how delegation works for the service creation.
The process is essentially the same for all the actions we have covered (except for minting delegation, which will be discussed in a separate section). Now, let's focus on delegation for service creation.
1 – The front end
First, we need to modify our ServiceForm.tsx component. If you recall, the classic workflow was as follows:
Fill out the service creation form.
Click on submit.
It triggers the onSubmit function, which calls the createService function in the TalentLayerService contract.
Now, we will add a new step that checks if delegation is activated. If it is, we will call the relevant API.
As you can see, we check if delegation is activated using the isActiveDelegate variable. If it is, we call the delegateCreateService function. Let's take a closer look at it.
2 - The Back end
The delegateCreateService is a part of the Next API management workflow. You can explore further details in the Next Next API documentation
In the code above, we are calling the /api/delegate/create-service API
You can find all the delegate API in pages > api > delegate
// pages/api/createService.tsimport { NextApiRequest, NextApiResponse } from'next';import { Contract } from'ethers';import { config } from'../../../config';import TalentLayerService from'../../../contracts/ABI/TalentLayerService.json';import { getServiceSignature } from'../../../utils/signature';import { getDelegationSigner, isPlatformAllowedToDelegate } from'../utils/delegate';exportdefaultasyncfunctionhandler(req:NextApiRequest, res:NextApiResponse) {const { userId,userAddress,cid } =req.body;// @dev : you can add here all the check you need to confirm the delagation for a userawaitisPlatformAllowedToDelegate(userAddress, res);try {constsigner=awaitgetDelegationSigner(res);if (!signer) {return; }constsignature=awaitgetServiceSignature({ profileId:Number(userId), cid });constserviceRegistryContract=newContract(config.contracts.serviceRegistry,TalentLayerService.abi, signer, );consttransaction=awaitserviceRegistryContract.createService( userId,process.env.NEXT_PUBLIC_PLATFORM_ID, cid, signature, );res.status(200).json({ transaction: transaction }); } catch (error) {console.log('errorDebug', error);res.status(500).json('tx failed'); }}
An important part of the code below is that we set a new signer (the platform) for the createService transaction. The new signer is based on and composed of the variables you have set up in the .env file:
With isPlatformAllowedToDelegate, we check if the user has activated delegation by requesting the user graph and verifying if a delegation address has been set by the platform.
With getDelegationSigner, we ensure that the platform has correctly configured all the necessary variables to instantiate the signer for delegation purposes.