Skip to content

Commit

Permalink
Merge pull request #2444 from TomShear/feat/dhedge-entry-exit-fees
Browse files Browse the repository at this point in the history
  • Loading branch information
dtmkeng authored Feb 21, 2025
2 parents c8fd5e4 + 62ee3f8 commit 9789bde
Showing 1 changed file with 49 additions and 4 deletions.
53 changes: 49 additions & 4 deletions fees/dhedge/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
import { FetchOptions, SimpleAdapter } from "../../adapters/types";
import { CHAIN } from "../../helpers/chains";
import { GraphQLClient } from "graphql-request";
const query = `

const queryManagerFeeMinteds = `
query managerFeeMinteds($startTimestamp: BigInt!, $endTimestamp: BigInt!, $first: Int!, $skip: Int!) {
managerFeeMinteds(
where: { blockTimestamp_gte: $startTimestamp, blockTimestamp_lte: $endTimestamp },
first: $first, skip: $skip, orderBy: blockTimestamp, orderDirection: desc
) { managerFee, daoFee, tokenPriceAtFeeMint }
}`
const queryEntryFeeMinteds = `
query entryFeeMinteds($startTimestamp: BigInt!, $endTimestamp: BigInt!, $first: Int!, $skip: Int!) {
entryFeeMinteds(
where: { time_gte: $startTimestamp, time_lte: $endTimestamp },
first: $first, skip: $skip, orderBy: time, orderDirection: desc
) { entryFeeAmount, tokenPrice }
}`

const queryExitFeeMenteds = `
query exitFeeMinteds($startTimestamp: BigInt!, $endTimestamp: BigInt!, $first: Int!, $skip: Int!) {
exitFeeMinteds(
where: { time_gte: $startTimestamp, time_lte: $endTimestamp },
first: $first, skip: $skip, orderBy: time, orderDirection: desc
) { exitFeeAmount, tokenPrice }
}`

// if graph goes down, can be pulled via event logs, example:
// https://optimistic.etherscan.io/tx/0x265e1eeb9a2c68ef8f58fe5e1d7e3f1151dd5e6686d4147445bf1bd8895deb38#eventlog check topic: 0x755a8059d66d8d243bc9f6913f429a811f154599d0538bb0b6a2ac23f23d2ccd
Expand Down Expand Up @@ -77,6 +93,26 @@ const calculateManagerFees = (data: any): number =>
return acc + result;
}, 0);

const calculateEntryFees = (data: any): number =>
data.reduce((acc: number, item: any) => {
const entryFee = Number(item.entryFeeAmount);
const tokenPrice = Number(item.tokenPrice);
const entryFeeFormatted = entryFee / 1e18;
const tokenPriceFormatted = tokenPrice / 1e18;
const result = entryFeeFormatted * tokenPriceFormatted;
return acc + result;
}, 0);

const calculateExitFees = (data: any): number =>
data.reduce((acc: number, item: any) => {
const exitFee = Number(item.exitFeeAmount);
const tokenPrice = Number(item.tokenPrice);
const exitFeeFormatted = exitFee / 1e18;
const tokenPriceFormatted = tokenPrice / 1e18;
const result = exitFeeFormatted * tokenPriceFormatted;
return acc + result;
}, 0);

const calculateDaoFees = (data: any): number =>
data.reduce((acc: number, item: any) => {
const daoFee = Number(item.daoFee);
Expand All @@ -91,11 +127,20 @@ const fetch = async ({ chain, endTimestamp, startTimestamp }: FetchOptions) => {
const config = PROVIDER_CONFIG[chain];
if (!config) throw new Error(`Unsupported chain: ${chain}`);

const dailyFees = await fetchHistoricalFees(chain as CHAIN, query, 'managerFeeMinteds', startTimestamp, endTimestamp)
const dailyManagerFeesEvents = await fetchHistoricalFees(chain as CHAIN, queryManagerFeeMinteds, 'managerFeeMinteds', startTimestamp, endTimestamp);
const dailyEntryFeesEvents = await fetchHistoricalFees(chain as CHAIN, queryEntryFeeMinteds, 'entryFeeMinteds', startTimestamp, endTimestamp);
const dailyExitFeesEvents = await fetchHistoricalFees(chain as CHAIN, queryExitFeeMenteds, 'exitFeeMinteds', startTimestamp, endTimestamp);

const dailyManagerFees = calculateManagerFees(dailyManagerFeesEvents);
const dailyEntryFees = calculateEntryFees(dailyEntryFeesEvents);
const dailyExitFees = calculateExitFees(dailyExitFeesEvents);
const dailyFees = dailyManagerFees + dailyEntryFees + dailyExitFees;

const dailyDaoFees = calculateDaoFees(dailyManagerFeesEvents);

return {
dailyFees: calculateManagerFees(dailyFees),
dailyRevenue: calculateDaoFees(dailyFees),
dailyFees: dailyFees,
dailyRevenue: dailyDaoFees,
timestamp: endTimestamp,
};
}
Expand Down

0 comments on commit 9789bde

Please sign in to comment.