📞Recommendation Engines

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

Airstack provides easy-to-use APIs for enriching Farcaster applications and for integrating onchain and offchain data with Farcaster.

In this tutorial, you will learn how to recommend followers for Farcaster users 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 Farcaster User(s) Based on Token Transfers

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

Try Demo

Show recommendations by token transfers for Farcaster user name varunsrin.eth and id 5650

Code

query GetRecommendationsByTokenTransfers {
  # first query on Ethereum
  ethereum: TokenTransfers(
    input: {filter: {_or: {from: {_in: ["fc_fname:dwr", "fc_fid:5650"]}, to: {_in: ["fc_fname:dwr", "fc_fid:5650"]}}}, blockchain: ethereum, limit: 50}
  ) {
    TokenTransfer {
      from {
        addresses
        socials(input: { filter: { dappName: {_in: farcaster}}}) {
          userId
          profileName
        }
      }
      to {
        addresses
        socials(input: { filter: { dappName: {_in: farcaster}}}) {
          userId
          profileName
        }
      }
    }
  }
  # second query on Polygon
  polygon: TokenTransfers(
    input: {filter: {_or: {from: {_in: ["fc_fname:dwr", "fc_fid:5650"]}, to: {_in: ["fc_fname:dwr", "fc_fid:5650"]}}}, blockchain: polygon, limit: 50}
  ) {
    TokenTransfer {
      from {
        addresses
        socials(input: { filter: { dappName: {_in: farcaster}}}) {
          userId
          profileName
        }
        domains {
          dappName
        }
      }
      to {
        addresses
        socials(input: { filter: { dappName: {_in: farcaster}}}) {
          userId
          profileName
        }
      }
    }
  }
}

Get Recommendation Follows For Farcaster User(s) Based on POAPs

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

Try Demo

Show POAPs owned by Farcaster user name dwr.eth and user id 1

Code

query POAPsOwnedByFarcasterUser {
  Poaps(
    input: {filter: {owner: {_in: ["fc_fname:dwr.eth", "fc_fid:1"]}}, 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

Show follow recommendations based on POAP event IDs 6584, 14498, and 6481

Code

query GetAllAddressesSocialsAndENSOfPOAP {
  Poaps(input: {filter: {eventId: {_in: ["6584", "14498", "6481"]}}, blockchain: ALL, limit: 10}) {
    Poap {
      owner {
        identity
        socials(input: {filter: {dappName: {_eq: farcaster}}}) {
          profileName
          userId
        }
      }
    }
  }
}

The follow recommendation will provide a list of Farcaster users with the Farcaster user name from the profileName field and Farcaster user ID from the userId field.

Get Recommendation Follows For Farcaster User(s) Based on NFTs

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

Try Code

Show NFTs on Ethereum and Polygon owned by Farcaster user name varunsrin.eth and id 5650

Code

query GetNFTs {
  ethereum: TokenBalances(
    input: {filter: {owner: {_in: ["fc_fname:varunsrin.eth", "fc_fid:5650"]}, tokenType: {_in: [ERC1155, ERC721]}}, blockchain: ethereum, limit: 200}
  ) {
    TokenBalance {
      tokenAddress
    }
  }
  polygon: TokenBalances(
    input: {filter: {owner: {_in: ["fc_fname:varunsrin.eth", "fc_fid:5650"]}, 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

Show follow recommendations based on NFTs

Code

query GetNFTHoldersAndImages {
  ethereum: TokenNfts(
    input: {filter: {address: {_in: ["0x00000000000061ad8ee190710508a818ae5325c3", "0x37fb80ef28008704288087831464058a4a3940ae", "0x5a3c077906abf9a46a9de87bb3c6d075a8a67851"]}}, blockchain: ethereum}
  ) {
    TokenNft {
      tokenBalances {
        owner {
          identity
          socials(input: {filter: {dappName: {_eq: farcaster}}}) {
            profileName
            userId
          }
        }
      }
    }
  }
  polygon: TokenNfts(
    input: {filter: {address: {_in: ["0xd18359edd97ff13609c1978452b05b43213222d7", "0x27562885f784616be44a0dc801ff18ed4551ba3d", "0x579720c63ed37fd4bd60a44fc27a25d6f169e95e"]}}, blockchain: polygon}
  ) {
    TokenNft {
      tokenBalances {
        owner {
          identity
          socials(input: {filter: {dappName: {_eq: farcaster}}}) {
            profileName
            userId
          }
        }
      }
    }
  }
}

The follow recommendation will provide a list of Farcaster users with the Farcaster user name from the profileName field and Farcaster user ID from the userId field.

Get Recommendation Follows For Farcaster User(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

Show common holders of Nouns NFT and EthCC POAP that also have Farcaster

Code

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

All the common holders' Farcaster 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": "worthalter",
    "userId": "9456",
    "userAssociatedAddresses": [
      "0xc19628f57a46389b0ac0fc113de273f91b07faca",
      "0xf6b6f07862a02c85628b3a9688beae07fea9c863"
    ]
  },
  // ...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