Create set follow module typed data

📘

full code repo https://github.com/lens-protocol/lens-api-examples

This API call allows you to get the typed data to then call the withSig method to set your follow module for your profile on lens.

🚧

This request is protected by authentication

hint: this means it requires an x-access-token header put in the request with your authentication token.

Typed data is a way to try to show the users what they are signing in a more readable format. You can read more about it here.

Constructing that type of data is normally difficult. On the type data, you also need to get the nonce, deadline, contract version, contract address, chain id, and the name of the contract for the signature to be able to be signed and verified.

When using this API the server checks every detail before it generates the typed data. For example: if you try to create typed data on an always failing transaction the server will throw an error in a human-readable form. This is great for debugging but also saves issues with users sending always failing transactions or a mismatch of a bad request.

We will show you the typed data approach using ethers and the API side by side. Keep in mind that with the typed data approach you use the withSig methods which can be called by you with your signature or with that signature any relay could call it for you on your behalf allowing gasless transactions.

API Design

mutation CreateSetFollowModuleTypedData {
  createSetFollowModuleTypedData(request:{
    profileId: "0x03",
    followModule: {
        feeFollowModule: {
            amount: {
               currency: "0xD40282e050723Ae26Aeb0F77022dB14470f4e011",
               value: "0.01"
            },
            recipient: "0xEEA0C1f5ab0159dba749Dc0BAee462E5e293daaF"
        }
     }
  }) {
    id
    expiresAt
    typedData {
      types {
        SetFollowModuleWithSig {
          name
          type
        }
      }
      domain {
        name
        chainId
        version
        verifyingContract
      }
      value {
        nonce
        deadline
        profileId
        followModule
        followModuleInitData
      }
    }
  }
}
{
  "data": {
    "createSetFollowModuleTypedData": {
      "id": "27313933-5a6d-4b9a-b5a7-64a77506c988",
      "expiresAt": "2022-02-18T15:08:11.000Z",
      "typedData": {
        "types": {
          "SetFollowModuleWithSig": [
            {
              "name": "profileId",
              "type": "uint256"
            },
            {
              "name": "followModule",
              "type": "address"
            },
            {
              "name": "followModuleInitData",
              "type": "bytes"
            },
            {
              "name": "nonce",
              "type": "uint256"
            },
            {
              "name": "deadline",
              "type": "uint256"
            }
          ]
        },
        "domain": {
          "name": "Lens Protocol Profile",
          "chainId": 80001,
          "version": "1",
          "verifyingContract": "0x23C1ce2b0865406955Da08F1D31c13fcc3f72A3a"
        },
        "value": {
          "nonce": 0,
          "deadline": 1645196891,
          "profileId": "0x03",
          "followModule": "0x2268063b81e7BDf4Bc681ef6d054Bbd1513aaE4A",
          "followModuleInitData": "0x000000000000000000000000000000000000000000000000002386f26fc10000000000000000000000000000d40282e050723ae26aeb0f77022db14470f4e011000000000000000000000000eea0c1f5ab0159dba749dc0baee462e5e293daaf"
        }
      }
    }
  }
}
type Mutation {
  createSetFollowModuleTypedData(
    request: CreateSetFollowModuleRequest!
  ): CreateSetFollowModuleBroadcastItemResult!
}

Request

Let's touch on this request so it's super clear.

ProfileId - required

This is mandatory

FollowModule

Defining the follow module is very easy with the schema we have created:

Free follow module

You can unset your follow module back to nothing by using the freeFollowModule

mutation CreateSetFollowModuleTypedData {
  createSetFollowModuleTypedData(request:{
    profileId: "0x03",
    followModule: {
        freeFollowModule: true
     }
  }) {
    id
    expiresAt
    typedData {
      types {
        SetFollowModuleWithSig {
          name
          type
        }
      }
      domain {
        name
        chainId
        version
        verifyingContract
      }
      value {
        nonce
        deadline
        profileId
        followModule
        followModuleInitData
      }
    }
  }
}

Fee follow module

You can charge to have someone follow you; to set this up you can do:

mutation CreateSetFollowModuleTypedData {
  createSetFollowModuleTypedData(request:{
    profileId: "0x03",
    followModule: {
        feeFollowModule: {
            amount: {
               currency: "0xD40282e050723Ae26Aeb0F77022dB14470f4e011",
               value: "0.01"
            },
            recipient: "0xEEA0C1f5ab0159dba749Dc0BAee462E5e293daaF"
        }
     }
  }) {
    id
    expiresAt
    typedData {
      types {
        SetFollowModuleWithSig {
          name
          type
        }
      }
      domain {
        name
        chainId
        version
        verifyingContract
      }
      value {
        nonce
        deadline
        profileId
        followModule
        followModuleInitData
      }
    }
  }
}

As you see above we have mapped the currency we want to be paid in alongside the value which should be passed in as the normal amount not shifted to the decimal places as our server does this for you. So if you want 1 WETH you would enter 1 as a value. The final property defined is the recipient you want the funds to go to.

Revert follow module

You do not want anyone to follow you; to set this up you can do:

mutation CreateSetFollowModuleTypedData {
  createSetFollowModuleTypedData(request:{
    profileId: "0x03",
    followModule: {
        revertFollowModule: true
     }
  }) {
    id
    expiresAt
    typedData {
      types {
        SetFollowModuleWithSig {
          name
          type
        }
      }
      domain {
        name
        chainId
        version
        verifyingContract
      }
      value {
        nonce
        deadline
        profileId
        followModule
        followModuleInitData
      }
    }
  }
}

Profile follow module

You only want people with profiles to be able to follow you and only once on that profile (avoiding spam); to set this up you can do:

mutation CreateSetFollowModuleTypedData {
  createSetFollowModuleTypedData(request:{
    profileId: "0x03",
    followModule: {
        profileFollowModule: true
     }
  }) {
    id
    expiresAt
    typedData {
      types {
        SetFollowModuleWithSig {
          name
          type
        }
      }
      domain {
        name
        chainId
        version
        verifyingContract
      }
      value {
        nonce
        deadline
        profileId
        followModule
        followModuleInitData
      }
    }
  }
}

📘

The API will support all modules which get whitelisted as they get approved.

as they do this doc will be updated alongside it.

Full code example

import { apolloClient } from './apollo-client';
import { gql } from '@apollo/client'

const CREATE_SET_FOLLOW_MODULE_TYPED_DATA = `
  mutation($request: CreateSetFollowModuleRequest!) { 
    createSetFollowModuleTypedData(request: $request) {
      id
      expiresAt
      typedData {
        types {
          SetFollowModuleWithSig {
            name
            type
          }
        }
      domain {
        name
        chainId
        version
        verifyingContract
      }
      value {
        nonce
        deadline
        profileId
        followModule
        followModuleInitData
      }
     }
   }
 }
`

export const createSetFollowModuleTypedData = (setFollowModuleRequest) => {
   return apolloClient.mutate({
    mutation: gql(CREATE_SET_FOLLOW_MODULE_TYPED_DATA),
    variables: {
      request: setFollowModuleRequest
    },
  })
}
import { ApolloClient, InMemoryCache, HttpLink, ApolloLink } from '@apollo/client'

const httpLink = new HttpLink({ uri: 'https://api-mumbai.lens.dev/' });

// example how you can pass in the x-access-token into requests using `ApolloLink`
const authLink = new ApolloLink((operation, forward) => {
  // Retrieve the authorization token from local storage.
  const token = localStorage.getItem('auth_token');

  // Use the setContext method to set the HTTP headers.
  operation.setContext({
    headers: {
      'x-access-token': token ? `Bearer ${token}` : ''
    }
  });

  // Call the next link in the middleware chain.
  return forward(operation);
});

export const apolloClient = new ApolloClient({
  link: authLink.concat(httpLink),
  cache: new InMemoryCache(),
})

Hooking typed data in with ethers

import { signedTypeData, getAddressFromSigner, splitSignature } from './ethers.service';
import { createFollowTypedData } from './create-follow-typed-data';
import { lensHub } from './lens-hub';

export const setFollowModule = async () => {
  // hard coded to make the code example clear
  const setFollowModuleRequest = {
    profileId: "0x01",
    followModule: {
      feeFollowModule: {
        amount: {
          currency: "0x3C68CE8504087f89c640D02d133646d98e64ddd9",
          value: "0.01"
        },
        recipient: "0xEEA0C1f5ab0159dba749Dc0BAee462E5e293daaF"
      }
    }
  };
        
  const result = await createSetFollowModuleTypedData(setFollowModuleRequest);
  const typedData = result.data.createSetFollowModuleTypedData.typedData;
  
  const signature = await signedTypeData(typedData.domain, typedData.types, typedData.value);
  const { v, r, s } = splitSignature(signature);
  
  const tx = await lensHub.setFollowModuleWithSig({
    profileId: typedData.value.profileId,
    followModule: typedData.value.followModule,
    followModuleInitData: typedData.value.followModuleInitData,
    sig: {
      v,
      r,
      s,
      deadline: typedData.value.deadline,
    },
  });
  console.log(tx.hash);
  // 0x64464dc0de5aac614a82dfd946fc0e17105ff6ed177b7d677ddb88ec772c52d3
  // you can look at how to know when its been indexed here: 
  //   - https://docs.lens.dev/docs/has-transaction-been-indexed
}
// this is showing you how you use it with react for example
// if your using node or something else you can import using
// @apollo/client/core!
import { apolloClient } from './apollo-client';
import { gql } from '@apollo/client'

const CREATE_SET_FOLLOW_MODULE_TYPED_DATA = `
  mutation($request: CreateSetFollowModuleRequest!) { 
    createSetFollowModuleTypedData(request: $request) {
      id
      expiresAt
      typedData {
        types {
          SetFollowModuleWithSig {
            name
            type
          }
        }
      domain {
        name
        chainId
        version
        verifyingContract
      }
      value {
        nonce
        deadline
        profileId
        followModule
        followModuleInitData
      }
     }
   }
 }
`

export const createSetFollowModuleTypedData = (setFollowModuleRequest) => {
   return apolloClient.mutate({
    mutation: gql(CREATE_SET_FOLLOW_MODULE_TYPED_DATA),
    variables: {
      request: setFollowModuleRequest
    },
  })
}
// this is showing you how you use it with react for example
// if your using node or something else you can import using
// @apollo/client/core!
import { ApolloClient, InMemoryCache, HttpLink, ApolloLink } from '@apollo/client'

const httpLink = new HttpLink({ uri: 'https://api-mumbai.lens.dev/' });

// example how you can pass in the x-access-token into requests using `ApolloLink`
const authLink = new ApolloLink((operation, forward) => {
  // Retrieve the authorization token from local storage.
  // if your using node etc you have to handle your auth different
  const token = localStorage.getItem('auth_token');

  // Use the setContext method to set the HTTP headers.
  operation.setContext({
    headers: {
      'x-access-token': token ? `Bearer ${token}` : ''
    }
  });

  // Call the next link in the middleware chain.
  return forward(operation);
});

export const apolloClient = new ApolloClient({
  link: authLink.concat(httpLink),
  cache: new InMemoryCache(),
})
import { ethers, utils } from 'ethers';
import omitDeep from 'omit-deep';

// This code will assume you are using MetaMask.
// It will also assume that you have already done all the connecting to metamask
// this is purely here to show you how the public API hooks together
export const ethersProvider = new ethers.providers.Web3Provider(window.ethereum);

export const getSigner = () => {
    return ethersProvider.getSigner();
}

export const getAddressFromSigner = () => {
  return getSigner().address;
}

export const init = async() => {
    const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });
  return accounts[0];
}

export const signedTypeData = (domain, types, value) => {
  const signer = getSigner();
  return signer._signTypedData(
    omitDeep(domain, '__typename'),
    omitDeep(types, '__typename'),
    omitDeep(value, '__typename')
  );
}

export const splitSignature = (signature) => {
    return utils.splitSignature(signature)
}

export const sendTx = (transaction) => {
  const signer = ethersProvider.getSigner();
  return signer.sendTransaction(transaction);
}
import { getSigner } from './ethers.service';

// lens contract info can all be found on the deployed
// contract address on polygon.
// not defining here as it will bloat the code example
export const lensHub = new ethers.Contract(
  LENS_HUB_CONTRACT_ADDRESS,
  LENS_HUB_ABI,
  getSigner()
)

Hooking in without using the type data

You may not want to go down the typed data with the signature route and just send the transaction directly from the client to the blockchain without any API call to map the data for you. You will need to do the encoding of the follow module data if any exists yourself. This is out of scope for the API documentation as would have been explained and showed how to do it in the contract docs. The contract method is setFollowModule.


Did this page help you?