Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add fees and dexs zeno #1470

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
120 changes: 120 additions & 0 deletions dexs/zeno/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import { Adapter } from "../../adapters/types";
import { CHAIN } from "../../helpers/chains";
import { gql, GraphQLClient } from "graphql-request";
import type { ChainEndpoints, FetchOptions } from "../../adapters/types";
import { Chain } from "@defillama/sdk/build/general";
import { HOUR, getTimestampAtStartOfHour } from "../../utils/date";
import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume";

const endpoints = {
[CHAIN.METIS]:
"https://metisapi.0xgraph.xyz/subgraphs/name/metis-andromeda-prod-stats",
};

type MarketStat = {
id: string;
totalTradingVolume: string;
};

const graphs = (graphUrls: ChainEndpoints) => {
return (chain: Chain) => {
return async (options: FetchOptions) => {
const startTime = Date.now();
const hourStartTime = startTime / 1e3;

if (chain === CHAIN.METIS) {
// Get total trading volume
const totalTradingVolumeQuery = gql`
{
marketStats {
id
totalTradingVolume
}
}
`;

const graphQLClient = new GraphQLClient(graphUrls[chain]);
graphQLClient.setHeader("origin", "https://zeno.exchange");
const totalMarketStats = (
await graphQLClient.request(totalTradingVolumeQuery)
).marketStats as Array<MarketStat>;
const totalVolume =
totalMarketStats.reduce(
(accum: number, t: MarketStat) =>
accum + parseInt(t.totalTradingVolume),
0 as number
) / 1e30;

const chunkSize = 40;
const splitMarket: MarketStat[][] = [];
for (let i = 0; i < totalMarketStats.length; i += chunkSize) {
const chunk = totalMarketStats.slice(i, i + chunkSize);
splitMarket.push(chunk);
}

let last24hrVolume = 0;
for (const markets of splitMarket) {
// Get daily trading volume
const ids: Array<string> = [];

let latestHourIndex = Math.floor(
getTimestampAtStartOfHour(hourStartTime) / HOUR
);

for (let i = 0; i < 24; i++) {
for (const marketStat of markets) {
ids.push(`"${latestHourIndex - i}_${marketStat.id}"`);
}
}

const filter = ids.join(",");

const last24hrVolumeQuery = gql`
{
marketHourlyStats(
where: {
id_in: [${filter}]
}
) {
tradingVolume
}
}
`;

const last24hrMarketStats = (
await graphQLClient.request(last24hrVolumeQuery)
).marketHourlyStats as Array<{ tradingVolume: string }>;
last24hrVolume +=
last24hrMarketStats.reduce(
(accum, t) => accum + parseInt(t.tradingVolume),
0 as number
) / 1e30;
}

return {
timestamp: startTime,
totalVolume: totalVolume.toString(),
dailyVolume: last24hrVolume.toString(),
};
}

return {
timestamp: startTime,
totalVolume: "0",
dailyVolume: "0",
};
};
};
};

const adapter: Adapter = {
version: 2,
adapter: {
[CHAIN.METIS]: {
fetch: graphs(endpoints)(CHAIN.METIS),
start: 1710294153,
},
},
};

export default adapter;
119 changes: 119 additions & 0 deletions fees/zeno.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { Adapter } from "../adapters/types";
import { CHAIN } from "../helpers/chains";
import { gql, GraphQLClient } from "graphql-request";
import type { ChainEndpoints, FetchV2 } from "../adapters/types";
import { getTimestampAtStartOfDayUTC } from "../utils/date";

const endpoints = {
[CHAIN.METIS]:
"https://metisapi.0xgraph.xyz/subgraphs/name/metis-andromeda-prod-stats",
};

const graphs = (graphUrls: ChainEndpoints) => {
const fetch: FetchV2 = async ({ chain, startTimestamp }) => {
if (chain === CHAIN.METIS) {
const floorDayTimestamp = getTimestampAtStartOfDayUTC(startTimestamp);
const totalFeeQuery = gql`
{
globalFeesStat(id: "global") {
totalFeePaid
settlementFeePaid
liquidationFeePaid
borrowingFeePaid
tradingFeePaid
addLiquidityFeePaid
removeLiquidityFeePaid
fundingFeePaid
}
}
`;
const dailyFeeQuery = gql`
{
dailyFeesStat(id: "${floorDayTimestamp}") {
totalFeePaid
settlementFeePaid
liquidationFeePaid
borrowingFeePaid
tradingFeePaid
addLiquidityFeePaid
removeLiquidityFeePaid
fundingFeePaid
}
}
`;
const graphQLClient = new GraphQLClient(graphUrls[chain]);
graphQLClient.setHeader("origin", "https://zeno.exchange");
const totalFeeResp = await graphQLClient.request(totalFeeQuery);
const dailyFeeResp = await graphQLClient.request(dailyFeeQuery);

const finalizedDailyFee =
Number(dailyFeeResp.dailyFeesStat.totalFeePaid) / 1e30;

const finalizedTotalFee =
Number(totalFeeResp.globalFeesStat.totalFeePaid) / 1e30;
const finalizedDailyFeeWithoutFundingFee =
(Number(dailyFeeResp.dailyFeesStat.tradingFeePaid) +
Number(dailyFeeResp.dailyFeesStat.borrowingFeePaid) +
Number(dailyFeeResp.dailyFeesStat.liquidationFeePaid) +
Number(dailyFeeResp.dailyFeesStat.settlementFeePaid) +
Number(dailyFeeResp.dailyFeesStat.addLiquidityFeePaid) +
Number(dailyFeeResp.dailyFeesStat.removeLiquidityFeePaid)) /
1e30;
const finalizedDailyUserFee =
(Number(dailyFeeResp.dailyFeesStat.tradingFeePaid) +
Number(dailyFeeResp.dailyFeesStat.borrowingFeePaid) +
Number(dailyFeeResp.dailyFeesStat.liquidationFeePaid) +
Number(dailyFeeResp.dailyFeesStat.fundingFeePaid) +
Number(dailyFeeResp.dailyFeesStat.settlementFeePaid)) /
1e30;
const finalizedTotalUserFee =
(Number(totalFeeResp.globalFeesStat.tradingFeePaid) +
Number(totalFeeResp.globalFeesStat.borrowingFeePaid) +
Number(totalFeeResp.globalFeesStat.liquidationFeePaid) +
Number(totalFeeResp.globalFeesStat.fundingFeePaid) +
Number(totalFeeResp.globalFeesStat.settlementFeePaid)) /
1e30;

const dailyHoldersRevenue =
(finalizedDailyFeeWithoutFundingFee * 35) / 90;
const dailyProtocolRevenue =
(finalizedDailyFeeWithoutFundingFee * 5) / 90;
const dailySupplySideRevenue =
(finalizedDailyFeeWithoutFundingFee * 50) / 90;
return {
dailyFees: finalizedDailyFee.toString(),
dailyUserFees: finalizedDailyUserFee.toString(),
dailyRevenue: (dailyHoldersRevenue + dailyProtocolRevenue).toString(),
dailyProtocolRevenue: dailyProtocolRevenue.toString(),
dailyHoldersRevenue: dailyHoldersRevenue.toString(),
dailySupplySideRevenue: dailySupplySideRevenue.toString(),
totalFees: finalizedTotalFee.toString(),
totalUserFees: finalizedTotalUserFee.toString(),
};
}

return {
dailyFees: "0",
dailyUserFees: "0",
dailyRevenue: "0",
dailyProtocolRevenue: "0",
dailyHoldersRevenue: "0",
dailySupplySideRevenue: "0",
totalFees: "0",
totalUserFees: "0",
};
};
return fetch;
};

const adapter: Adapter = {
version: 2,
adapter: {
[CHAIN.METIS]: {
fetch: graphs(endpoints),
start: 1710294153,
},
},
};

export default adapter;
Loading