@parifi/sdk
Version:
Parifi SDK with common utility functions
77 lines (72 loc) • 2.28 kB
JavaScript
// src/subgraph/scripts/index.ts
import request from "graphql-request";
// src/subgraph/scripts/subgraphQueries.ts
import { gql } from "graphql-request";
var fetchIntegratorFeesWithTimestampQuery = (startTimeStamp, endTimeStamp) => gql`{
snxAccounts(
first: 1000
where: { type: PERP, totalOrdersCount_gt: 0 }) {
id
integratorFeesGenerated
accountId
owner {
id
}
orders(
where: {
status: SETTLED,
createdTimestamp_gt: ${startTimeStamp},
createdTimestamp_lt: ${endTimeStamp}
}
) {
id
referralFees
collectedFees
}
}
}
`;
// src/subgraph/scripts/index.ts
import { Decimal as Decimal2 } from "decimal.js";
// 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";
// src/subgraph/scripts/index.ts
var getEstimatedRewardsForUser = async (subgraphEndpoint, userAddress, totalRewardsForPeriod, startTimestamp, endTimestamp) => {
const response = await request(
subgraphEndpoint,
fetchIntegratorFeesWithTimestampQuery(startTimestamp, endTimestamp)
);
const feeDetails = response?.snxAccounts ?? [];
let totalFeesForPeriod = 0;
let userFeesForPeriod = 0;
feeDetails.forEach((account) => {
account.orders.forEach((order) => {
const formattedFees = new Decimal2(order.referralFees).div(WAD);
if (account.owner.id == userAddress) {
userFeesForPeriod += Number(formattedFees.toString());
}
totalFeesForPeriod += Number(formattedFees.toString());
});
});
if (totalFeesForPeriod === 0) {
return 0;
}
const estimatedUserRewards = userFeesForPeriod * totalRewardsForPeriod / totalFeesForPeriod;
return estimatedUserRewards;
};
export {
getEstimatedRewardsForUser
};
//# sourceMappingURL=index.mjs.map