UNPKG

@parifi/sdk

Version:

Parifi SDK with common utility functions

193 lines (189 loc) 5.99 kB
// src/subgraph/accounts/index.ts import Decimal2 from "decimal.js"; import { request } from "graphql-request"; // src/subgraph/accounts/subgraphQueries.ts import { gql } from "graphql-request"; var fetchRealizedPnlData = (userAddress) => gql` { account(id: "${userAddress.toLowerCase()}") { id totalRealizedPnlPositions totalRealizedPnlVaults } } `; var fetchAccountByWalletAddress = (walletAddress) => gql` { wallet(id: "${walletAddress}") { id } } `; var fetchLeaderboardUserData = (userAddresses) => gql` { accounts( where: {id_in: [${userAddresses.map((id) => `"${id}"`).join(", ")}]} ) { id totalOrdersCount totalPositionsCount countProfitablePositions countLossPositions countLiquidatedPositions totalVolumeInUsd totalVolumeInUsdLongs totalVolumeInUsdShorts totalRealizedPnlPositions totalAccruedBorrowingFeesInUsd } }`; var fetchIntegratorFees = (userAddresses) => gql` { snxAccounts(where: { owner_in: [${userAddresses.map((id) => `"${id}"`).join(", ")}], type: PERP }) { id accountId owner { id } integratorFeesGenerated } } `; var checkExistingUser = (userAddress) => gql` { snxAccounts( where: { owner: "${userAddress}", type: PERP, } ) { id accountId totalOrdersCount } } `; var depositedCollateralForSnxAccountsQuery = (accountIds) => gql` { snxAccounts (where :{ accountId_in : [${accountIds.map((id) => `"${id}"`).join(", ")}] }) { owner{ id } accountId collateralDeposits { totalAmountDeposited totalAmountWithdrawn totalAmountLiquidated currentDepositedAmount collateralName collateralSymbol collateralDecimals } } } `; // src/common/constants.ts import { Decimal } from "decimal.js"; var PRECISION_MULTIPLIER = new Decimal("10000"); var DEVIATION_PRECISION_MULTIPLIER = new Decimal(10).pow(12); var SECONDS_IN_A_YEAR = new Decimal(365 * 24 * 60 * 60); var MAX_FEE = new Decimal(1e7); var WAD = new Decimal(10).pow(18); var DECIMAL_10 = new Decimal(10); var DECIMAL_ZERO = new Decimal(0); var BIGINT_ZERO = BigInt(0); var ONE_GWEI = 1e9; var DEFAULT_GAS_PRICE = 2 * ONE_GWEI; // src/common/helpers.ts import { formatEther, parseEther } from "viem"; function convertWeiToEther(amountInWei) { if (amountInWei == void 0) { throw new Error("Invalid amount received during conversion: undefined"); } if (typeof amountInWei == "bigint") { return Number(formatEther(amountInWei)); } else if (typeof amountInWei == "string") { return Number(formatEther(BigInt(amountInWei))); } else { throw new Error("Expected string or bigint for conversion"); } } // src/subgraph/accounts/index.ts var getRealizedPnlForUser = async (subgraphEndpoint, userAddress) => { try { const query = fetchRealizedPnlData(userAddress); const subgraphResponse = await request(subgraphEndpoint, query); const realizedPnlResponse = subgraphResponse; if (realizedPnlResponse === null || realizedPnlResponse.account === null) { console.log("Users Realized Pnl data not found"); return { totalRealizedPnlPositions: DECIMAL_ZERO, totalRealizedPnlVaults: DECIMAL_ZERO }; } const totalRealizedPnlPositions = new Decimal2(realizedPnlResponse.account.totalRealizedPnlPositions); const totalRealizedPnlVaults = new Decimal2(realizedPnlResponse.account.totalRealizedPnlVaults); return { totalRealizedPnlPositions, totalRealizedPnlVaults }; } catch (error) { throw error; } }; var getLeaderboardUserData = async (subgraphEndpoint, userAddresses) => { const subgraphResponse = await request(subgraphEndpoint, fetchLeaderboardUserData(userAddresses)); const leaderboardUserData = subgraphResponse.accounts; return leaderboardUserData; }; var getAccountByAddress = async (subgraphEndpoint, userAddresses) => { const subgraphResponse = await request(subgraphEndpoint, fetchAccountByWalletAddress(userAddresses)); if (!subgraphResponse) throw new Error("Error while fetching account data"); return subgraphResponse?.wallet; }; var getFeesByAddress = async (subgraphEndpoint, userAddresses) => { const feesByAddress = /* @__PURE__ */ new Map(); const subgraphResponse = await request(subgraphEndpoint, fetchIntegratorFees(userAddresses)); if (!subgraphResponse) throw new Error("Error while fetching account data"); const snxAccounts = subgraphResponse?.snxAccounts; snxAccounts.forEach((account) => { const userFees = feesByAddress.get(account.owner.id); if (userFees) { const totalFees = userFees + convertWeiToEther(account.integratorFeesGenerated); feesByAddress.set(account.owner.id, totalFees); } else { feesByAddress.set(account.owner.id, convertWeiToEther(account.integratorFeesGenerated)); } }); return feesByAddress; }; var checkIfExistingUser = async (subgraphEndpoint, userAddress) => { const subgraphResponse = await request(subgraphEndpoint, checkExistingUser(userAddress)); if (!subgraphResponse) throw new Error("Error while fetching account data"); const snxAccounts = subgraphResponse?.snxAccounts; let isExisting = false; for (let index = 0; index < snxAccounts.length; index++) { if (Number(snxAccounts[index].totalOrdersCount) > 0) { isExisting = true; break; } } return isExisting; }; var depositedCollateralForSnxAccounts = async (subgraphEndpoint, accountIds) => { const subgraphResponse = await request(subgraphEndpoint, depositedCollateralForSnxAccountsQuery(accountIds)); if (!subgraphResponse) throw new Error("Error while fetching account data"); const snxAccounts = subgraphResponse?.snxAccounts; return snxAccounts; }; export { checkIfExistingUser, depositedCollateralForSnxAccounts, getAccountByAddress, getFeesByAddress, getLeaderboardUserData, getRealizedPnlForUser }; //# sourceMappingURL=index.mjs.map