Get User accounts based on authority

Fetch User Account details based on its authority

Each user account is associated with an authority, which is the owner of the account. To query the user details associated with an authority, we query the User account’s authority field using the _eq filter. Please note, the User account fields can be handpicked as per your applications requirements.

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

Fetch User Details for an Authority

const SHYFT_API_KEY = "YOUR-API-KEY";

async function getDataByGraphQl(authorityAddress) {
  //get user by authority
  const operationsDoc = `
      query MyQuery {
        drift_User(
          limit: 10
          where: {authority: {_eq: ${JSON.stringify(authorityAddress)}}}
        ) {
          delegate
          subAccountId
          pubkey
          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 getUserDetailsByAuthority(authorityPubkey) {
  const { errors, data } = await getDataByGraphQl(authorityPubkey);

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

  console.dir(data, { depth: null });
}
getUserDetailsByAuthority("Ctae6pxZZYKQ2tZRavw1tEVhVN5YNRJRhTBFGDbipmtw")
//authority address of the user to be fetched

Last updated