💰Get Token Balances

Learn how to get ERC20, 721, 1155, and POAPs Of Lens Profile(s), including images and metadata, on Ethereum, Polygon, and Gnosis (POAPs).

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 fetch Lens user(s) asset holdings which comprise ERC20s, ERC721s, ERC115s, and POAPs on Ethereum, Polygon, and Gnosis.

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 All ERC20s Owned By Lens Profile(s)

You can fetch all ERC20 tokens on Ethereum and Polygon owned by any Lens Profile(s):

Try Demo

Code

query ERC20OwnedByLensProfiles {
  Ethereum: TokenBalances(
    input: {filter: {owner: {_in: ["bradorbradley.lens", "lens_id:100275"]}, tokenType: {_eq: ERC20}}, blockchain: ethereum, limit: 50}
  ) {
    TokenBalance {
      owner {
        socials(input: {filter: {dappName: {_eq: lens}}}) {
          profileName
          profileTokenId
          profileTokenIdHex
          userAssociatedAddresses
        }
      }
      amount
      tokenAddress
      token {
        name
        symbol
      }
    }
    pageInfo {
      nextCursor
      prevCursor
    }
  }
  Polygon: TokenBalances(
    input: {filter: {owner: {_in: ["bradorbradley.lens", "lens_id:100275"]}, tokenType: {_eq: ERC20}}, blockchain: polygon, limit: 50}
  ) {
    TokenBalance {
      owner {
        socials(input: {filter: {dappName: {_eq: lens}}}) {
          profileName
          profileTokenId
          profileTokenIdHex
          userAssociatedAddresses
        }
      }
      amount
      tokenAddress
      token {
        name
        symbol
      }
    }
    pageInfo {
      nextCursor
      prevCursor
    }
  }
}

Get All NFTs Owned By Lens Profile(s)

You can fetch all NFTs on Ethereum and Polygon owned by any Lens Profile(s):

Try Demo

Code

query NFTsOwnedByLensProfiles {
  Ethereum: TokenBalances(
    input: {filter: {owner: {_in: ["bradorbradley.lens", "lens_id:100275"]}, tokenType: {_in: [ERC1155, ERC721]}}, blockchain: ethereum, limit: 50}
  ) {
    TokenBalance {
      owner {
        socials(input: {filter: {dappName: {_eq: lens}}}) {
          profileName
          profileTokenId
          profileTokenIdHex
          userAssociatedAddresses
        }
      }
      amount
      tokenAddress
      tokenId
      tokenType
      tokenNfts {
        contentValue {
          image {
            extraSmall
            small
            medium
            large
          }
        }
      }
    }
    pageInfo {
      nextCursor
      prevCursor
    }
  }
  Polygon: TokenBalances(
    input: {filter: {owner: {_in: ["bradorbradley.lens", "lens_id:100275"]}, tokenType: {_in: [ERC1155, ERC721]}}, blockchain: polygon, limit: 50}
  ) {
    TokenBalance {
      owner {
        socials(input: {filter: {dappName: {_eq: lens}}}) {
          profileName
          profileTokenId
          profileTokenIdHex
          userAssociatedAddresses
        }
      }
      amount
      tokenAddress
      tokenId
      tokenType
      tokenNfts {
        contentValue {
          image {
            extraSmall
            small
            medium
            large
          }
        }
      }
    }
    pageInfo {
      nextCursor
      prevCursor
    }
  }
}

Get All POAPs Owned By Lens Profile(s)

You can fetch all POAPs tokens owned by any Lens Profile(s):

Try Demo

Code

query POAPsOwnedByLensProfiles {
  Poaps(
    input: {filter: {owner: {_in: ["bradorbradley.lens", "lens_id:100275"]}}, blockchain: ALL}
  ) {
    Poap {
      eventId
      owner {
        socials(input: { filter: { dappName: {_eq: lens}}}) {
          profileName
          profileTokenId
          profileTokenIdHex
          userAssociatedAddresses
        }
      }
      poapEvent {
        eventName
        eventURL
        startDate
        endDate
        country
        city
        contentValue {
          image {
            extraSmall
            large
            medium
            original
            small
          }
        }
      }
    }
  }
}

Developer Support

If you have any questions or need help regarding fetching token balances of Lens profile(s), please join our Airstack's Telegram group.

More Resources

Last updated

#300: add-user-details

Change request updated