📞Recommendation Engines

Learn how to get recommended followers for Lens profiles based on on-chain insights from token transfers, POAPs, NFTS, and token holder combinations.

Airstack provides easy-to-use APIs for enriching Lens applications and integrating on-chain and off-chain data with Lens.

In this tutorial, you will learn how to recommend followers for Lens profiles based on on-chain insights from token transfers, POAPs, NFTS, and token holder combinations.

In this guide you will learn how to use Airstack to:

Pre-requisites

  • An Airstack account (free)

  • Basic knowledge of GraphQL

Get Started

JavaScript/TypeScript/Python

If you are using JavaScript/TypeScript or Python, Install the Airstack SDK:

React

npm install @airstack/airstack-react

Node

npm install @airstack/node

Then, add the following snippets to your code:

import { init, useQuery } from "@airstack/airstack-react";

init("YOUR_AIRSTACK_API_KEY");

const query = "YOUR_QUERY"; // Replace with GraphQL Query

const Component = () => {
  const { data, loading, error } = useQuery(query);

  if (loading) {
    return <p>Loading...</p>;
  }

  if (error) {
    return <p>Error: {error.message}</p>;
  }

  // Render your component using the data returned by the query
};

Other Programming Languages

To access the Airstack APIs in other languages, you can use https://api.airstack.xyz/gql as your GraphQL endpoint.

🤖 AI Natural Language​

Airstack provides an AI solution for you to build GraphQL queries to fulfill your use case easily. You can find the AI prompt of each query in the demo's caption or title for yourself to try.

Get Recommendation Follows For Lens Profile(s) Based on Token Transfers

To get recommendations by token transfers, simply fetch all token transfer that is received and sent from the Lens profile(s):

Try Demo

Code

query GetRecommendationsByTokenTransfers {
  ethereum: TokenTransfers(
    input: {filter: {_or: {from: {_in: ["bradorbradley.lens"]}, to: {_in: ["bradorbradley.lens"]}}}, blockchain: ethereum, limit: 50}
  ) {
    TokenTransfer {
      from {
        addresses
        socials(input: {filter: {dappName: {_in: lens}}}) {
          userId
          profileName
        }
      }
      to {
        addresses
        socials(input: {filter: {dappName: {_in: lens}}}) {
          userId
          profileName
        }
      }
    }
  }
  polygon: TokenTransfers(
    input: {filter: {_or: {from: {_in: ["bradorbradley.lens"]}, to: {_in: ["bradorbradley.lens"]}}}, blockchain: polygon, limit: 50}
  ) {
    TokenTransfer {
      from {
        addresses
        socials(input: {filter: {dappName: {_in: lens}}}) {
          userId
          profileName
        }
      }
      to {
        addresses
        socials(input: {filter: {dappName: {_in: lens}}}) {
          userId
          profileName
        }
      }
    }
  }
}

Get Recommendation Follows For Lens Profile(s) Based on POAPs

To get recommendations by POAPs, first fetch all POAPs that is owned by the Lens profile(s):

Try Demo

Code

query POAPsOwnedByLensProfiles {
  Poaps(
    input: {filter: {owner: {_in: ["bradorbradley.lens"]}}, blockchain: ALL}
  ) {
    Poap {
      eventId
      poapEvent {
        eventName
        eventURL
        startDate
        endDate
        country
        city
        contentValue {
          image {
            extraSmall
            large
            medium
            original
            small
          }
        }
      }
    }
    pageInfo {
      nextCursor
      prevCursor
    }
  }
}

With the response, you can compile the eventIds as an array that can be forwarded as an input for the next query.

The next query for recommending follows based on POAPs will be fetching all the holders of the POAP with the eventIds in the array:

Try Demo

Code

query GetAllAddressesSocialsAndENSOfPOAP {
  Poaps(
    input: {filter: {eventId: {_in: ["47553", "80122", "37964"]}}, blockchain: ALL, limit: 200}
  ) {
    Poap {
      owner {
        identity
        socials(input: {filter: {dappName: {_eq: lens}}}) {
          profileName
          userId
        }
      }
    }
  }
}

The follow recommendation will provide a list of Lens profiles with the Lens profile name from the profileName field.

Get Recommendation Follows For Lens Profile(s) Based on NFTs

To get recommendations by NFTs, first fetch all NFTs that is owned by the Lens profile(s) on both Ethereum and Polygon:

Try Code

Code

query GetNFTs {
  ethereum: TokenBalances(
    input: {filter: {owner: {_in: ["bradorbradley.lens"]}, tokenType: {_in: [ERC1155, ERC721]}}, blockchain: ethereum, limit: 200}
  ) {
    TokenBalance {
      tokenAddress
    }
  }
  polygon: TokenBalances(
    input: {filter: {owner: {_in: ["bradorbradley.lens"]}, tokenType: {_in: [ERC1155, ERC721]}}, blockchain: polygon, limit: 200}
  ) {
    TokenBalance {
      tokenAddress
    }
  }
}

With the response, you can compile the many tokenAddress as an array that can be forwarded as an input for the next query.

The next query for recommending follows based on NFTs will be fetching all the holders of the NFT with the eventIds in the array:

Try Demo

Code

query GetNFTHoldersAndImages {
  ethereum: TokenNfts(
    input: {filter: {address: {_in: ["0xf5806ba5635911aa1d2b7b794172d55c731ca860", "0xa4ed1befd956d4f444e7010955c3c06ffbd46ac7", "0x9d90669665607f08005cae4a7098143f554c59ef"]}}, blockchain: ethereum}
  ) {
    TokenNft {
      tokenBalances {
        owner {
          identity
          socials(input: {filter: {dappName: {_eq: lens}}}) {
            profileName
            userId
          }
        }
      }
    }
  }
  polygon: TokenNfts(
    input: {filter: {address: {_in: ["0xa71d227ffc872a4627e832f87b49ef46b29dd050", "0x5ef718b8360ef2b82fb971b50350913e2bad4783", "0xdeb053d41521672af06ba75d61be3364977461af"]}}, blockchain: polygon}
  ) {
    TokenNft {
      tokenBalances {
        owner {
          identity
          socials(input: {filter: {dappName: {_eq: lens}}}) {
            profileName
            userId
          }
        }
      }
    }
  }
}

The follow recommendation will provide a list of Lens profiles name from the profileName field.

Get Recommendation Follows For Lens Profile(s) Based on NFTs and POAPs Commonly Held

Fetching

You can fetch follow recommendations based on the common holder of a specific NFT and a specific POAP by providing the token contract address and the POAP event ID:

Try Demo

Code

query GetCommonHoldersOfNounsAndEthCC {
  TokenBalances(
    input: {filter: {tokenAddress: {_eq: "0x977e43ab3eb8c0aece1230ba187740342865ee78"}}, blockchain: ethereum, limit: 200}
  ) {
    TokenBalance {
      owner {
        poaps(input: {filter: {eventId: {_eq: "43882"}}}) {
          owner {
            socials(input: {filter: {dappName: {_eq: lens}}}) {
              profileName
              userId
              userAssociatedAddresses
            }
          }
        }
      }
    }
  }
}

All the common holders' Lens profile details will be returned inside the innermost owner.socials field.

Formatting

To get the list of all holders in a flat array, use the following format function:

const formatFunction = (data) =>
  data?.TokenBalances?.TokenBalance?.map(({ owner }) =>
    owner?.poaps?.map(({ owner }) => owner?.socials)
  )
    .filter(Boolean)
    .flat(2)
    .filter((social, index, array) => array.indexOf(social) === index) ?? [];

The final result will the the list of all common holders in an array:

[
  {
    "profileName": "westlakevillage.lens",
    "userId": "0x8ec94086a724cbec4d37097b8792ce99cadcd520",
    "userAssociatedAddresses": [
      "0x8ec94086a724cbec4d37097b8792ce99cadcd520"
    ]
  },
  {
    "profileName": "brad.lens",
    "userId": "0x8ec94086a724cbec4d37097b8792ce99cadcd520",
    "userAssociatedAddresses": [
      "0x8ec94086a724cbec4d37097b8792ce99cadcd520"
    ]
  },
  {
    "profileName": "bradorbradley.lens",
    "userId": "0x8ec94086a724cbec4d37097b8792ce99cadcd520",
    "userAssociatedAddresses": [
      "0x8ec94086a724cbec4d37097b8792ce99cadcd520"
    ]
  },
  {
    "profileName": "hanimourra.lens",
    "userId": "0x8ec94086a724cbec4d37097b8792ce99cadcd520",
    "userAssociatedAddresses": [
      "0x8ec94086a724cbec4d37097b8792ce99cadcd520"
    ]
  },
  // ...other token holders
]

Developer Support

If you have any questions or need help regarding building a recommendation engine, please join our Airstack's Telegram group.

More Resources

Last updated

#300: add-user-details

Change request updated