@parifi/sdk
Version:
Parifi SDK with common utility functions
233 lines (228 loc) • 8.43 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/subgraph/accounts/index.ts
var accounts_exports = {};
__export(accounts_exports, {
checkIfExistingUser: () => checkIfExistingUser,
depositedCollateralForSnxAccounts: () => depositedCollateralForSnxAccounts,
getAccountByAddress: () => getAccountByAddress,
getFeesByAddress: () => getFeesByAddress,
getLeaderboardUserData: () => getLeaderboardUserData,
getRealizedPnlForUser: () => getRealizedPnlForUser
});
module.exports = __toCommonJS(accounts_exports);
var import_decimal2 = __toESM(require("decimal.js"));
var import_graphql_request2 = require("graphql-request");
// src/subgraph/accounts/subgraphQueries.ts
var import_graphql_request = require("graphql-request");
var fetchRealizedPnlData = (userAddress) => import_graphql_request.gql`
{
account(id: "${userAddress.toLowerCase()}") {
id
totalRealizedPnlPositions
totalRealizedPnlVaults
}
}
`;
var fetchAccountByWalletAddress = (walletAddress) => import_graphql_request.gql`
{
wallet(id: "${walletAddress}") {
id
}
}
`;
var fetchLeaderboardUserData = (userAddresses) => import_graphql_request.gql`
{
accounts(
where: {id_in: [${userAddresses.map((id) => `"${id}"`).join(", ")}]}
) {
id
totalOrdersCount
totalPositionsCount
countProfitablePositions
countLossPositions
countLiquidatedPositions
totalVolumeInUsd
totalVolumeInUsdLongs
totalVolumeInUsdShorts
totalRealizedPnlPositions
totalAccruedBorrowingFeesInUsd
}
}`;
var fetchIntegratorFees = (userAddresses) => import_graphql_request.gql`
{
snxAccounts(where:
{
owner_in: [${userAddresses.map((id) => `"${id}"`).join(", ")}],
type: PERP
}) {
id
accountId
owner {
id
}
integratorFeesGenerated
}
}
`;
var checkExistingUser = (userAddress) => import_graphql_request.gql`
{
snxAccounts(
where: {
owner: "${userAddress}",
type: PERP,
}
) {
id
accountId
totalOrdersCount
}
}
`;
var depositedCollateralForSnxAccountsQuery = (accountIds) => import_graphql_request.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
var import_decimal = require("decimal.js");
var PRECISION_MULTIPLIER = new import_decimal.Decimal("10000");
var DEVIATION_PRECISION_MULTIPLIER = new import_decimal.Decimal(10).pow(12);
var SECONDS_IN_A_YEAR = new import_decimal.Decimal(365 * 24 * 60 * 60);
var MAX_FEE = new import_decimal.Decimal(1e7);
var WAD = new import_decimal.Decimal(10).pow(18);
var DECIMAL_10 = new import_decimal.Decimal(10);
var DECIMAL_ZERO = new import_decimal.Decimal(0);
var BIGINT_ZERO = BigInt(0);
var ONE_GWEI = 1e9;
var DEFAULT_GAS_PRICE = 2 * ONE_GWEI;
// src/common/helpers.ts
var import_viem = require("viem");
function convertWeiToEther(amountInWei) {
if (amountInWei == void 0) {
throw new Error("Invalid amount received during conversion: undefined");
}
if (typeof amountInWei == "bigint") {
return Number((0, import_viem.formatEther)(amountInWei));
} else if (typeof amountInWei == "string") {
return Number((0, import_viem.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 (0, import_graphql_request2.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 import_decimal2.default(realizedPnlResponse.account.totalRealizedPnlPositions);
const totalRealizedPnlVaults = new import_decimal2.default(realizedPnlResponse.account.totalRealizedPnlVaults);
return { totalRealizedPnlPositions, totalRealizedPnlVaults };
} catch (error) {
throw error;
}
};
var getLeaderboardUserData = async (subgraphEndpoint, userAddresses) => {
const subgraphResponse = await (0, import_graphql_request2.request)(subgraphEndpoint, fetchLeaderboardUserData(userAddresses));
const leaderboardUserData = subgraphResponse.accounts;
return leaderboardUserData;
};
var getAccountByAddress = async (subgraphEndpoint, userAddresses) => {
const subgraphResponse = await (0, import_graphql_request2.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 (0, import_graphql_request2.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 (0, import_graphql_request2.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 (0, import_graphql_request2.request)(subgraphEndpoint, depositedCollateralForSnxAccountsQuery(accountIds));
if (!subgraphResponse) throw new Error("Error while fetching account data");
const snxAccounts = subgraphResponse?.snxAccounts;
return snxAccounts;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
checkIfExistingUser,
depositedCollateralForSnxAccounts,
getAccountByAddress,
getFeesByAddress,
getLeaderboardUserData,
getRealizedPnlForUser
});
//# sourceMappingURL=index.js.map