Service creation example

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:

  1. Fill out the service creation form.

  2. Click on submit.

  3. 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.

// ..............

const { isActiveDelegate } = useContext(TalentLayerContext);

// ..............
 if (isActiveDelegate) {
    const response = await delegateCreateService(user.id, user.address, cid);
    tx = response.data.transaction;
  } else {
    const contract = new ethers.Contract(
      config.contracts.serviceRegistry,
      ServiceRegistry.abi,
      signer,
    );

    tx = await contract.createService(
      user?.id,
      process.env.NEXT_PUBLIC_PLATFORM_ID,
      cid,
      signature,
    );
  }  

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

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:

  • NEXT_PRIVATE_DELEGATE_SEED_PHRASE

  • NEXT_PUBLIC_DELEGATE_ADDRESS

Here is the core of the delegation

  • 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.

See the Full Code Implemented on Our Demo DAPP

Last updated

Was this helpful?