Fetch and query a delegates for user accounts on Drift v2 Program .
A Delegate is an address that can control an account on the user's behalf. The delegated user (public key) although has limited power, can perform most operations on the account except for withdrawals. To find delegated users on Drift Protocol, we need to query the delegate field in the drift_User account.
You can directly copy paste this code on replit and see it in action.
Fetch user account for a particular delegate
const SHYFT_API_KEY = "YOUR-API-KEY";
async function getDataByGraphQl(delegateAddress) {
//get user by delegated account
const operationsDoc = `
query MyQuery {
drift_User(
limit: 10
where: {delegate: {_eq: ${JSON.stringify(delegateAddress)}}}
) {
delegate
authority
pubkey
}
}
`; //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 getReserveCurrencyDetails(delegatePubkey) {
const { errors, data } = await getDataByGraphQl(delegatePubkey);
if (errors) {
console.error(errors);
console.log("Some Error Occured, please check your API key or try again");
}
console.dir(data, { depth: null });
}
getReserveCurrencyDetails("EBWSHvJmWkg1r2oUPDcKxfBxA1QG2fdZdAqH2EVNTzxs")
The response contains authority and delegate which indicates the account owner and the delegated account, more fields can also be cherrypicked as per your requirement.
You can also query the delegate account for a particular user account or authority, using the following query
query MyQuery {
drift_User(
where: {authority: {_eq: "AUYs3A9gMLn5tqySAQ4Sqea8PB5sWAXmgZGz8GBerYE3"}}
) { //replace with the user/authority wallet which you want to query
status
pubkey
authority
delegate
}
}