πŸ’°Combinations of ERC20s, NFTs, and POAPs

Learn how to fetch the list of common token holders across various ERC20s, NFTs, and POAPs.

Airstack provides easy-to-use APIs for enriching dapps and integrating on-chain and off-chain data from various blockchains.

Table Of Contents

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

Pre-requisites

  • An Airstack account

  • 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>;
  }
};

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.

Pre-requisites

Best Practices

With nested queries, Airstack finds the intersection of common holders of various ERC20s, NFTs, and POAPs in the following order:

  1. Filtering holders on the 1st outermost query

  2. Comparing holders of 1st outermost query against the holders of 2nd outermost query

  3. Comparing holders of the 1st and 2nd outermost query against the holders on the 3rd outermost query

  4. Comparing holders of 1st, 2nd, ..., nth outermost query against the holders on (n + 1)-th outermost query

Since the number of objects returned in the responses will be dependent on the number of holders on the 1st outermost query, it's most efficient that you have the ERC20, NFT, or POAP with the least amount of holders on the 1st outermost query.

Suppose there is a token (ERC20 or NFT) and a POAP:

  • Token A: 100,000 holders

  • POAP B: 1,000 holders.

If Token A is the input on the 1st outermost query, then the end result will be 100,000 objects in the response array.

On the other hand, if POAP B is the input on the 1st outermost query, then the end result will be instead ONLY 1,000 objects in the response array.

The latter approach will be more efficient and easier for further formatting.

Common Holders of A Token (ERC20 or NFT) and A POAP

Fetching

You can fetch the common holder of a token and a POAP by providing the token contract address and the POAP event ID:

Try Demo

Code

query GetCommonHoldersOfNounsAndEthCC {
  TokenBalances(
    input: {filter: {tokenAddress: {_eq: "0x9c8ff314c9bc7f6e59a9d9225fb22946427edc03"}}, blockchain: ethereum, limit: 200}
  ) {
    TokenBalance {
      owner {
        poaps(input: {filter: {eventId: {_eq: "141910"}}}) {
          owner {
            addresses
          }
        }
      }
    }
  }
}

All the common holders' addresses will be returned inside the innermost owner.addresses 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?.addresses)
  )
    .filter(Boolean)
    .flat(2)
    .filter((address, index, array) => array.indexOf(address) === index) ?? [];

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

[
  "0xc77d249809ae5a118eef66227d1a01a3d62c82d4",
  "0x3291e96b3bff7ed56e3ca8364273c5b4654b2b37",
  "0xe348c7959e47646031cea7ed30266a6702d011cc",
  // ...other token holders
  "0xa69babef1ca67a37ffaf7a485dfff3382056e78c",
  "0x46340b20830761efd32832a74d7169b29feb9758",
  "0x2008b6c3d07b061a84f790c035c2f6dc11a0be70"
]

Common Holders of Any Tokens (ERC20s or NFTs) and POAPs

Fetching

In order to add more tokens or POAPs, then more nested queries will be necessary.

Within the nested queries, you can either add tokenBalances with a new token contract address under the owner for adding new tokens:

Try Demo

Code

query GetCommonHoldersOfTokenAndAnyPOAPs {
  TokenBalances(
    input: {filter: {tokenAddress: {_eq: "0x9c8ff314c9bc7f6e59a9d9225fb22946427edc03"}}, blockchain: ethereum, limit: 200}
  ) {
    TokenBalance {
      owner {
        poaps(input: {filter: {eventId: {_eq: "141910"}}}) {
          owner {
            poaps(input: {filter: {eventId: {_eq: "3687"}}}) {
              owner {
                addresses
              }
            }
          }
        }
      }
    }
  }
}

or add poaps to add new POAP with new POAP event ID:

query GetCommonHoldersOfAnyTokensAndPOAP {
  TokenBalances(
    input: {filter: {tokenAddress: {_eq: "0x9c8ff314c9bc7f6e59a9d9225fb22946427edc03"}}, blockchain: ethereum, limit: 200}
  ) {
    TokenBalance {
      owner {
        poaps(input: {filter: {eventId: {_eq: "141910"}}}) {
          owner {
            tokenBalances(input: {filter: {tokenAddress: {_eq: "0xbd139ec36b0288f510d1b53423b5a03a50be2afa"}}}) {
              owner {
                addresses
              }
            }
          }
        }
      }
    }
  }
}

All the common holders' addresses will be returned inside the innermost owner.addresses 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?.tokenBalances?.map(({ owner }) => owner?.addresses)
    )
  )
    .filter(Boolean)
    .flat(3)
    .filter((address, index, array) => array.indexOf(address) === index) ?? [];

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

[
  "0xc77d249809ae5a118eef66227d1a01a3d62c82d4",
  "0x3291e96b3bff7ed56e3ca8364273c5b4654b2b37",
  "0xe348c7959e47646031cea7ed30266a6702d011cc",
  // ...other token holders
  "0xa69babef1ca67a37ffaf7a485dfff3382056e78c",
  "0x46340b20830761efd32832a74d7169b29feb9758",
  "0x2008b6c3d07b061a84f790c035c2f6dc11a0be70"
]

Developer Support

If you have any questions or need help regarding fetching token holders of various ERC20s, NFTs, and POAPs, please join our Airstack's Telegram group.

More Resources

Last updated

Was this helpful?