# Get PrepPositions for an User Account

A <mark style="color:yellow;">perpetual position</mark> in a perpetual market refers to a trader's open position in a perpetual futures contract. This position can be either ***long*** (betting that the price of the underlying asset will increase) or ***short*** (betting that the price will decrease). We can query PerpPositions data for a particular marketIndex via SuperIndexer using the following steps:&#x20;

* We query the *user account* details for an *`authority`* (user wallet)&#x20;
* The user account details will contain *perpPositions* for the user. We need to filter positions where either the *`baseAssetAmount`* or *`quoteAssetAmount`* or the *`lpShares`* is not equal to zero(these are all perpPosition fields). Also, the *`openOrders`* field should not equal to zero.&#x20;
* Once done, finally we filter the final list of positions while matching *`marketIndex`* field.&#x20;

You can directly copy paste this code on <mark style="color:yellow;">replit</mark> and see it in action.

#### Fetch perpPosition for a User Account by authority and market index

{% tabs %}
{% tab title="Code Snippet" %}
{% code overflow="wrap" %}

```javascript
const BN = require("bn.js");
const SHYFT_API_KEY = "YOUR-API-KEY";
const ZERO = new BN(0);

async function getperpPositionDataByGraphQl(authorityAddress) {
  const operationsDoc = `
        query MyQuery {
            drift_User(
                where: {authority: {_eq: ${JSON.stringify(authorityAddress)}}}
            ) {
                perpPositions
                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 main(authorityPubkey, marketIndex) {
  const { errors, data } = await getperpPositionDataByGraphQl(authorityPubkey);
  if (errors) {
    console.error(errors);
    console.log("Some Error Occured, please check your API key or try again");
  }
  let allMatchedMarkets = [];
  for (let index = 0; index < data.drift_User.length; index++) {
    const currentPerpPosition = data.drift_User[index];

    const matchedMarkets = currentPerpPosition.perpPositions.filter(
      (perpPosition) =>
        !new BN(perpPosition.baseAssetAmount).eq(ZERO) ||
        !new BN(perpPosition.quoteAssetAmount).eq(ZERO) ||
        !(perpPosition.openOrders == 0) ||
        !new BN(perpPosition.lpShares).eq(ZERO)
    );
    if (matchedMarkets) {
      matchedMarkets.forEach((mMar) => {
        allMatchedMarkets.push(mMar);
      });
    }
  }
  if(!allMatchedMarkets.length){
    console.log("No perpPostions found");
    return
  }
  console.log("PerpPositions found: ");
  console.dir(allMatchedMarkets.find(m => m.marketIndex == marketIndex),{depth: null});
  
}
main("BgGAVukE1j8JDsvXwcnneuoN8LTpDuiRUtYfQWociQjL", 2);

```

{% endcode %}
{% endtab %}

{% tab title="Response" %}

```json

PerpPositions found:
{
  lpShares: '0',
  openAsks: '0',
  openBids: '0',
  perLpBase: 0,
  openOrders: 1,
  settledPnl: '-12993115',
  marketIndex: 2,
  baseAssetAmount: '-1500000000',
  quoteAssetAmount: '3585448868',
  quoteEntryAmount: '3573160286',
  quoteBreakEvenAmount: '3572455753',
  lastBaseAssetAmountPerLp: '0',
  remainderBaseAssetAmount: 0,
  lastCumulativeFundingRate: '956085947061',
  lastQuoteAssetAmountPerLp: '0'
}
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.shyft.to/solana-indexers/case-studies/drift/get-preppositions-for-an-user-account.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
