Skip to main content

Get Users's Balance Of Certain Fan Token

In this tutorial, you will learn how to get user's balance of a certain fan token by calling the Moxie Protocol Subgraph API.

Include User's Vesting Addresses

Moxie protocol users have Moxie token currently vesting in their vesting contract. A huge portion of them use these to bid on auctions and buy/sell fan tokens on the Moxie protocol.

Therefore, it is important that you include the user's vesting addresses in the query to get user's balance of a certain fan token. To get user's vesting contract addresses, click here.

Pre-requisites

Install dependencies graphql and graphql-request to your project :
npm install graphql graphql-request 

Step 1: Add The API Query To Your Code

To get user's balance of a certain fan token, you can use the following query:

query MyQuery($userAddresses: [ID!], $fanTokenAddress: String) {
users(where: { id_in: $userAddresses }) {
portfolio(where: { subjectToken: $fanTokenAddress }) {
balance
buyVolume
sellVolume
subjectToken {
name
symbol
}
}
}
}
With this GraphQL query, you can add it to your source code and call the API with the graphql-request library:
index.ts
import { gql, GraphQLClient } from "graphql-request";


const graphQLClient = new GraphQLClient(
"https://api.studio.thegraph.com/query/23537/moxie_protocol_stats_mainnet/version/latest"
);

const query = gql`
query MyQuery($userAddresses: [ID!], $fanTokenAddress: String) {
users(where: { id_in: $userAddresses }) {
portfolio(where: { subjectToken: $fanTokenAddress }) {
balance
buyVolume
sellVolume
subjectToken {
name
symbol
}
}
}
}
`;

const variable = {
"userAddress": "0x00d620def6ccb76c92dbfc87bc2bebab7637ec53",
// provide the fan token address here to get user's balance
"fanTokenAddress": "0x00e7d5fad22a375568d03b0688abbdfa4ec3b729"
}


try {
const data = await graphQLClient.request(query, variable);
console.log(data);
} catch (e) {
throw new Error(e);
}

Step 2: Execute Your Code

Once you have your code ready, you can execute it by running the following command:

ts-node index.ts

If it runs successfully, you should see the data returned in the terminal:

{
"data": {
"users": [
{
"portfolio": [
{
"balance": "10000000000000000000",
"buyVolume": "10000000000000000000",
"sellVolume": "0",
"subjectToken": {
"name": "/fitness",
"symbol": "cid:fitness"
}
}
]
}
]
}
}

Congrats! 🥳🎉 You've just fetched user's balance of a certain fan token!

Developer Support

If you have any questions or need help with other use cases, feel free to join the /airstack Warpcast channel and ask your questions there.

Our team is always ready to help you with any questions you may have.