Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 69 additions & 33 deletions fees/dhedge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ const queryManagerFeeMinteds = `
managerFeeMinteds(
where: { blockTimestamp_gte: $startTimestamp, blockTimestamp_lte: $endTimestamp },
first: $first, skip: $skip, orderBy: blockTimestamp, orderDirection: desc
) { managerFee, daoFee, tokenPriceAtFeeMint }
) { managerFee, daoFee, tokenPriceAtFeeMint, manager }
}`
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 }
) { entryFeeAmount, tokenPrice, managerAddress }
}`

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 }
) { exitFeeAmount, tokenPrice, managerAddress }
}`

// if graph goes down, can be pulled via event logs, example:
Expand Down Expand Up @@ -57,6 +57,16 @@ const PROVIDER_CONFIG = {
},
};

const TOROS_AND_MSTABLE_MANAGERS: string[] = [
"0x813123a13d01d3f07d434673fdc89cbba523f14d", // Toros Optimism
"0x090e7fbd87a673ee3d0b6ccacf0e1d94fb90da59", // Toros Polygon
"0xfbd2b4216f422dc1eee1cff4fb64b726f099def5", // Toros Arbitrum
"0x5619ad05b0253a7e647bd2e4c01c7f40ceab0879", // Toros Base
"0xfbd2b4216f422dc1eee1cff4fb64b726f099def5", // Toros Ethereum
"0x6a21fd094d1c99c8bdbcbc9a7ad4b316a550edd0", // mStable Optimism
"0x3dd46846eed8d147841ae162c8425c08bd8e1b41", // mStable Ethereum
];

const fetchHistoricalFees = async (chainId: CHAIN, query: string, volumeField: string, startTimestamp: number, endTimestamp: number) => {
const { endpoint } = PROVIDER_CONFIG[chainId];

Expand Down Expand Up @@ -87,37 +97,58 @@ const fetchHistoricalFees = async (chainId: CHAIN, query: string, volumeField: s
return allData;
};

const calculateManagerFees = (data: any): number =>
data.reduce((acc: number, item: any) => {
const managerFee = Number(item.managerFee);
const tokenPrice = Number(item.tokenPriceAtFeeMint);
const managerFeeFormatted = managerFee / 1e18;
const tokenPriceFormatted = tokenPrice / 1e18;
const result = managerFeeFormatted * tokenPriceFormatted;
return acc + result;
const calculateManagerFees = (data: any, onlyForTorosAndMstableManagers: boolean): number =>
data
.filter(f => {
if (onlyForTorosAndMstableManagers){
return TOROS_AND_MSTABLE_MANAGERS.includes(f.manager.toLowerCase());
}
return true;
})
.reduce((acc: number, item: any) => {
const managerFee = Number(item.managerFee);
const tokenPrice = Number(item.tokenPriceAtFeeMint);
const managerFeeFormatted = managerFee / 1e18;
const tokenPriceFormatted = tokenPrice / 1e18;
const result = managerFeeFormatted * tokenPriceFormatted;
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;
const calculateEntryFees = (data: any, onlyForTorosAndMstableManagers: boolean): number =>
data
.filter(f => {
if (onlyForTorosAndMstableManagers){
return TOROS_AND_MSTABLE_MANAGERS.includes(f.managerAddress.toLowerCase());
}
return true;
})
.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;
const calculateExitFees = (data: any, onlyForTorosAndMstableManagers: boolean): number =>
data
.filter(f => {
if (onlyForTorosAndMstableManagers){
return TOROS_AND_MSTABLE_MANAGERS.includes(f.managerAddress.toLowerCase());
}
return true;
})
.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 =>
const calculateDhedgeDaoFees = (data: any): number =>
data.reduce((acc: number, item: any) => {
const daoFee = Number(item.daoFee);
const tokenPrice = Number(item.tokenPriceAtFeeMint);
Expand All @@ -135,12 +166,17 @@ const fetch = async ({ chain, endTimestamp, startTimestamp }: FetchOptions) => {
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 dailyManagerFees = calculateManagerFees(dailyManagerFeesEvents, false);
const dailyEntryFees = calculateEntryFees(dailyEntryFeesEvents, false);
const dailyExitFees = calculateExitFees(dailyExitFeesEvents, false);
const dailyFees = dailyManagerFees + dailyEntryFees + dailyExitFees;

const dailyDaoFees = calculateDaoFees(dailyManagerFeesEvents);
const dailyDhedgeDaoFees = calculateDhedgeDaoFees(dailyManagerFeesEvents);
const dailyTorosMstableManagersFees = calculateManagerFees(dailyManagerFeesEvents, true);
const dailyTorosMstableEntryFees = calculateEntryFees(dailyEntryFeesEvents, true);
const dailyTorosMstableExitFees = calculateExitFees(dailyExitFeesEvents, true);

const dailyDaoFees = dailyDhedgeDaoFees + dailyTorosMstableExitFees + dailyTorosMstableManagersFees + dailyTorosMstableEntryFees;

return {
dailyFees,
Expand All @@ -153,7 +189,7 @@ const fetch = async ({ chain, endTimestamp, startTimestamp }: FetchOptions) => {
const info = {
methodology: {
Fees: 'All fees generated from dHEDGE vaults.',
Revenue: 'All revenue collected by the dHEDGE protocol from fees generated.',
Revenue: 'Vaults fees going to treasury.',
}
}

Expand Down
2 changes: 0 additions & 2 deletions fees/toros/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,12 @@ const fetch = async ({ chain, endTimestamp, startTimestamp }: FetchOptions) => {

return {
dailyFees,
dailyRevenue: dailyFees,
timestamp: endTimestamp,
};
}

const methodology = {
Fees: 'All fees generated from Toros vaults.',
Revenue: 'All revenue collected by the Toros protocol.',
}

const adapter: SimpleAdapter = {
Expand Down
Loading