Get User details based on Referrer

Fetch User Account details based on its referrer, you can also fetch referrer accounts based on it's referrer address

On drift, users can be referred by other users. The referrer field indicates the user public key that referred the authority user. We can get User accounts referred by a particular referrer by querying the drift_UserStats account.

You can directly copy paste this code on replit and see it in action.

Fetch UserStats details by referrer

const SHYFT_API_KEY = "YOUR-API-KEY";

async function getDataByGraphQl(refAddress) {
  //get userStats by referrer
  const operationsDoc = `
      query MyQuery {
        drift_UserStats(
          where: {referrer: {_eq: ${JSON.stringify(refAddress)}}}
        ) {
          disableUpdatePerpBidAskTwap
          isReferrer
          referrer
          authority
        }
      }
    `; //graphQl query
  const result = await fetch(
    `https://programs.shyft.to/v0/graphql/accounts?api_key=${SHYFT_API_KEY}&network=mainnet-beta`, //SHYFT's GQL endpoint
    {
      method: "POST",
      body: JSON.stringify({
        query: operationsDoc,
        variables: {},
        operationName: "MyQuery",
      }),
    },
  );

  return await result.json();
}

async function getUserStatsByReferrer(referrerPubkey) {
  const { errors, data } = await getDataByGraphQl(reffererPubkey);

  if (errors) {
    console.error(errors);
    console.log("Some Error Occured, please check your API key or try again");
  }

  console.dir(data, { depth: null });
}
getUserStatsByReferrer("GXNAdSaMwPqRr36DHK8YAi8CoKN83PvwUYDGb8YzKQag");
//you can replace with the required referrer address

You can also fetch the referrer for a particular authority account, using the following query

query MyQuery {
  drift_UserStats(
    where: {authority: {_eq: "2jiKcT8ZXEzkhW2uhvKS8Br1r9uDU1o4YUB62bYwtKeJ"}}
  ) {
    authority
    referrer
  }
}

The response will contain the referrer name for the given authority account (pubkey)

Last updated