{"version":3,"sources":["../../../src/subgraph/positions/subgraphQueries.ts"],"sourcesContent":["import { gql } from 'graphql-request';\n\n// Fetches all positions by a user (Both open and closed)\nexport const fetchPositionsByUserQuery = (userAddress: string, count: number = 20, skip: number = 0) =>\n  gql`\n    {\n    snxAccounts(\n      first: ${count}\n      skip: ${skip}\n      where: { owner: \"${userAddress}\", type: PERP }) {\n      id\n      accountId\n      owner {\n        id\n      }\n      collateralDeposits {\n        id\n        collateralName\n        collateralSymbol\n        collateralDecimals\n        collateralAddress\n        currentDepositedAmount\n        totalAmountDeposited\n        totalAmountWithdrawn\n        totalAmountLiquidated\n      }\n      positions(where: {status_in: [OPEN, CLOSED, LIQUIDATED]}) {\n        id\n        market {\n          id\n          marketName\n          marketSymbol\n          feedId\n        }\n        positionSize\n        avgPrice\n        avgPriceDec\n        isLong\n        createdTimestamp\n        status\n        txHash\n        liquidationTxHash\n        closingPrice\n        realizedPositionPnl\n        realizedPnlAfterFees\n        totalFeesPaid\n        createdTimestamp\n        lastRefresh\n        lastRefreshISO\n        canBeLiquidated\n        snapshotCollateralValueUsd\n    }\n    }\n  }`;\n\n// Fetches positions for a user address by status\nexport const fetchPositionsByUserQueryAndStatus = (\n  userAddress: string,\n  status: string,\n  count: number = 20,\n  skip: number = 0,\n) =>\n  gql`\n    {\n    snxAccounts(\n      first: ${count}\n      skip: ${skip}\n      where: { owner: \"${userAddress}\", type: PERP }\n    ) {\n      id\n      accountId\n      owner {\n        id\n      }\n      collateralDeposits {\n        id\n        collateralName\n        collateralSymbol\n        collateralDecimals\n        collateralAddress\n        currentDepositedAmount\n        totalAmountDeposited\n        totalAmountWithdrawn\n        totalAmountLiquidated\n      }\n      positions(where: {status: ${status} }) {\n        id\n        market {\n          id\n          marketName\n          marketSymbol\n          feedId\n        }\n        positionSize\n        avgPrice\n        avgPriceDec\n        isLong\n        createdTimestamp\n        status\n        txHash\n        liquidationTxHash\n        closingPrice\n        realizedPositionPnl\n        realizedPnlAfterFees\n        totalFeesPaid\n        createdTimestamp\n        lastRefresh\n        lastRefreshISO\n        canBeLiquidated\n        snapshotCollateralValueUsd\n    }\n    }\n  }`;\n\n// Fetches open positions for a user address\nexport const fetchOpenPositionsByUser = (userAddress: string, count: number = 20, skip: number = 0) =>\n  gql`\n    {\n    snxAccounts(\n      first: ${count}\n      skip: ${skip}\n      where: {\n        owner: \"${userAddress}\",\n        type: PERP,\n        openPositionCount_gt: 0\n      }\n    ) {\n      id\n      accountId\n      owner {\n        id\n      }\n      collateralDeposits {\n        id\n        collateralName\n        collateralSymbol\n        collateralDecimals\n        collateralAddress\n        currentDepositedAmount\n        totalAmountDeposited\n        totalAmountWithdrawn\n        totalAmountLiquidated\n      }\n      positions(where: {status: OPEN }) {\n        id\n        market {\n          id\n          marketName\n          marketSymbol\n          feedId\n        }\n        positionSize\n        avgPrice\n        avgPriceDec\n        isLong\n        createdTimestamp\n        status\n        txHash\n        liquidationTxHash\n        closingPrice\n        realizedPositionPnl\n        realizedPnlAfterFees\n        totalFeesPaid\n        createdTimestamp\n        lastRefresh\n        lastRefreshISO\n        canBeLiquidated\n        snapshotCollateralValueUsd\n      }\n    }\n  }`;\n\n// Fetches positions for a user address by status\nexport const fetchUserPositionHistory = (userAddress: string, count: number = 20, skip: number = 0) =>\n  gql`\n    {\n    snxAccounts(\n      first: ${count}\n      skip: ${skip}\n      where: { owner: \"${userAddress}\", type: PERP }\n    ) {\n      id\n      accountId\n      owner {\n        id\n      }\n      collateralDeposits {\n        id\n        collateralName\n        collateralSymbol\n        collateralDecimals\n        collateralAddress\n        currentDepositedAmount\n        totalAmountDeposited\n        totalAmountWithdrawn\n        totalAmountLiquidated\n      }\n      positions(where: {status_in: [CLOSED, LIQUIDATED]}) {\n        id\n        market {\n          id\n          marketName\n          marketSymbol\n          feedId\n        }\n        positionSize\n        avgPrice\n        avgPriceDec\n        isLong\n        createdTimestamp\n        status\n        txHash\n        liquidationTxHash\n        closingPrice\n        realizedPositionPnl\n        realizedPnlAfterFees\n        totalFeesPaid\n        createdTimestamp\n        lastRefresh\n        lastRefreshISO\n        canBeLiquidated\n        snapshotCollateralValueUsd\n    }\n    }\n  }`;\n\nexport const fetchPositionByIdQuery = (positionId: string) =>\n  gql`\n    {\n        position(\n            id: \"${positionId}\"\n        ) {\n            id\n            market {\n              id,\n              marketName,\n              marketSymbol,\n              feedId \n            }\n            snxAccount{\n              id\n              accountId\n            }\n            isLong\n            positionSize\n            avgPrice\n            avgPriceDec\n            status\n            txHash\n            liquidationTxHash\n            closingPrice\n            realizedPositionPnl\n            realizedPnlAfterFees\n            totalFeesPaid\n            createdTimestamp\n            lastRefresh\n            lastRefreshISO\n            canBeLiquidated\n            snapshotCollateralValueUsd\n        }\n    }`;\n\nexport const fetchPositionsToLiquidateQuery = (count: number) => gql`\n  {\n    positions(where: { status: OPEN, canBeLiquidated: true }, first: ${count}, orderBy: positionSize, orderDirection: desc) {\n      id\n    }\n  }\n`;\n\n// Fetches positions for a user address by status\nexport const fetchUserPositionHistoryWithTime = (\n  userAddress: string,\n  startTimestamp: number,\n  endTimestamp: number,\n  count: number = 20,\n  skip: number = 0,\n) =>\n  gql`\n    {\n    snxAccounts(\n      first: ${count}\n      skip: ${skip}\n      where: {\n        owner: \"${userAddress}\",\n        type: PERP\n      }\n    ) {\n      id\n      accountId\n      owner {\n        id\n      }\n      collateralDeposits {\n        id\n        collateralName\n        collateralSymbol\n        collateralDecimals\n        collateralAddress\n        currentDepositedAmount\n        totalAmountDeposited\n        totalAmountWithdrawn\n        totalAmountLiquidated\n      }\n      positions( where: {\n          status_in: [CLOSED, LIQUIDATED],\n          createdTimestamp_gte: ${startTimestamp}\n          createdTimestamp_lte: ${endTimestamp}\n      }) {\n        id\n        market {\n          id\n          marketName\n          marketSymbol\n          feedId\n        }\n        positionSize\n        positionCollateral\n        avgPrice\n        avgPriceDec\n        isLong\n        createdTimestamp\n        status\n        txHash\n        liquidationTxHash\n        closingPrice\n        realizedPositionPnl\n        realizedPnlAfterFees\n        totalFeesPaid\n        createdTimestamp\n        lastRefresh\n        lastRefreshISO\n        canBeLiquidated\n        snapshotCollateralValueUsd\n    }\n    }\n  }`;\n\n// Fetches positions for a user address by status\nexport const fetchUserOpenPositionsWithTime = (\n  userAddress: string,\n  startTimestamp: number,\n  endTimestamp: number,\n  count: number = 20,\n  skip: number = 0,\n) =>\n  gql`\n    {\n    snxAccounts(\n      first: ${count}\n      skip: ${skip}\n      where: {\n        owner: \"${userAddress}\",\n        type: PERP,\n        positions_: {\n          status: OPEN,\n          createdTimestamp_gte: ${startTimestamp}\n          createdTimestamp_lte: ${endTimestamp}\n          }\n      }\n    ) {\n      id\n      accountId\n      owner {\n        id\n      }\n      collateralDeposits {\n        id\n        collateralName\n        collateralSymbol\n        collateralDecimals\n        collateralAddress\n        currentDepositedAmount\n        totalAmountDeposited\n        totalAmountWithdrawn\n        totalAmountLiquidated\n      }\n      positions {\n        id\n        market {\n          id\n          marketName\n          marketSymbol\n          feedId\n        }\n        positionSize\n        avgPrice\n        avgPriceDec\n        isLong\n        createdTimestamp\n        status\n        txHash\n        liquidationTxHash\n        closingPrice\n        realizedPositionPnl\n        realizedPnlAfterFees\n        totalFeesPaid\n        createdTimestamp\n        lastRefresh\n        lastRefreshISO\n        canBeLiquidated\n        snapshotCollateralValueUsd\n    }\n    }\n  }`;\n\nexport const fetchLiquidatedPositionsBySnxAccount = (snxAccountId: string, lastRefresh: number) =>\n  gql`\n    {\n    snxAccount(id: \"${snxAccountId}\"\n    ) {\n      id\n      accountId\n      owner {\n        id\n      }\n      collateralDeposits {\n        id\n        collateralName\n        collateralSymbol\n        collateralDecimals\n        collateralAddress\n        currentDepositedAmount\n        totalAmountDeposited\n        totalAmountWithdrawn\n        totalAmountLiquidated\n      }\n      positions(where: { status: LIQUIDATED, lastRefresh_gt: ${lastRefresh} }) {\n        id\n        market {\n          id\n          marketName\n          marketSymbol\n          feedId\n        }\n        positionSize\n        avgPrice\n        avgPriceDec\n        isLong\n        createdTimestamp\n        status\n        txHash\n        liquidationTxHash\n        closingPrice\n        realizedPositionPnl\n        realizedPnlAfterFees\n        totalFeesPaid\n        createdTimestamp\n        lastRefresh\n        lastRefreshISO\n        canBeLiquidated\n        snapshotCollateralValueUsd\n    }\n    }\n  }`;\n\nexport const fetchCrossMarginPositionsBySnxAccount = (snxAccountId: string, lastRefresh: number) => gql`\n  {\n    snxAccount(id: \"${snxAccountId}\") {\n      accountId\n      collateralDeposits {\n        totalAmountLiquidated\n      }\n      positions(where: { status: LIQUIDATED, lastRefresh_gt: ${lastRefresh} }) {\n        lastRefresh\n      }\n    }\n  }\n`;\n\n// Fetches positions for a user address by status\nexport const fetchAllOpenPositionsWithTime = (\n  startTimestamp: number,\n  endTimestamp: number,\n  count: number = 20,\n  skip: number = 0,\n) =>\n  gql`\n    {\n    snxAccounts(\n      first: ${count}\n      skip: ${skip}\n      where: { \n      type: PERP\n      openPositionCount_gt:0\n      }\n    ) {\n      id\n      accountId\n      owner {\n        id\n      }\n      collateralDeposits {\n        id\n        collateralName\n        collateralSymbol\n        collateralDecimals\n        collateralAddress\n        currentDepositedAmount\n        totalAmountDeposited\n        totalAmountWithdrawn\n        totalAmountLiquidated\n      }\n      positions(where: {\n          status: OPEN,\n          createdTimestamp_gte: ${startTimestamp}\n          createdTimestamp_lte: ${endTimestamp}\n          }) \n      {\n        id\n        market {\n          id\n          marketName\n          marketSymbol\n          feedId\n        }\n        positionSize\n        avgPrice\n        avgPriceDec\n        isLong\n        createdTimestamp\n        status\n        txHash\n        liquidationTxHash\n        closingPrice\n        realizedPositionPnl\n        realizedPnlAfterFees\n        totalFeesPaid\n        createdTimestamp\n        lastRefresh\n        lastRefreshISO\n        canBeLiquidated\n        snapshotCollateralValueUsd\n    }\n    }\n  }`;\n\n// Fetches positions for a user address by status\nexport const fetchAllPositionHistoryWithTime = (\n  startTimestamp: number,\n  endTimestamp: number,\n  count: number = 20,\n  skip: number = 0,\n) =>\n  gql`\n    {\n    snxAccounts(\n      first: ${count}\n      skip: ${skip}\n      where: {\n        type: PERP\n        totalPositionsCount_gt :0\n      }\n    ) {\n      id\n      accountId\n      owner {\n        id\n      }\n      collateralDeposits {\n        id\n        collateralName\n        collateralSymbol\n        collateralDecimals\n        collateralAddress\n        currentDepositedAmount\n        totalAmountDeposited\n        totalAmountWithdrawn\n        totalAmountLiquidated\n      }\n      positions(where: {\n          status_in: [CLOSED, LIQUIDATED],\n          createdTimestamp_gte: ${startTimestamp}\n          createdTimestamp_lte: ${endTimestamp}\n      }) {\n        id\n        market {\n          id\n          marketName\n          marketSymbol\n          feedId\n        }\n        positionSize\n        avgPrice\n        avgPriceDec\n        isLong\n        createdTimestamp\n        status\n        txHash\n        liquidationTxHash\n        closingPrice\n        realizedPositionPnl\n        realizedPnlAfterFees\n        totalFeesPaid\n        createdTimestamp\n        lastRefresh\n        lastRefreshISO\n        canBeLiquidated\n        snapshotCollateralValueUsd\n    }\n    }\n  }`;\n"],"mappings":";AAAA,SAAS,WAAW;AAGb,IAAM,4BAA4B,CAAC,aAAqB,QAAgB,IAAI,OAAe,MAChG;AAAA;AAAA;AAAA,eAGa,KAAK;AAAA,cACN,IAAI;AAAA,yBACO,WAAW;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA+C7B,IAAM,qCAAqC,CAChD,aACA,QACA,QAAgB,IAChB,OAAe,MAEf;AAAA;AAAA;AAAA,eAGa,KAAK;AAAA,cACN,IAAI;AAAA,yBACO,WAAW;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kCAkBF,MAAM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