Skip to content

Commit

Permalink
update metavault derivatives v2 chains
Browse files Browse the repository at this point in the history
  • Loading branch information
0xrmvdao committed Mar 13, 2024
1 parent 3ce87d0 commit 32896e4
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 26 deletions.
28 changes: 22 additions & 6 deletions dexs/metavault-derivatives-v2/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import { SimpleAdapter, FetchResultVolume } from "../../adapters/types";
import {
SimpleAdapter,
FetchResultVolume,
ChainEndpoints,
} from "../../adapters/types";
import { CHAIN } from "../../helpers/chains";
import { getTimestampAtStartOfDayUTC } from "../../utils/date";
import { Chain } from "@defillama/sdk/build/general";
import request, { gql } from "graphql-request";

const endpoints: ChainEndpoints = {
[CHAIN.LINEA]:
"https://api.studio.thegraph.com/query/55804/linea-trade/version/latest",
[CHAIN.POLYGON]:
"https://api.thegraph.com/subgraphs/name/sdcrypt0/polygon-trade",
};

interface IReferralRecord {
volume: string; // Assuming volume is a string that represents a number
timestamp: number;
Expand All @@ -15,7 +26,7 @@ interface IVolumeStat {
id: string;
}

const fetch = () => {
const fetch = (endpoint) => {
return async (timestamp: number): Promise<FetchResultVolume> => {
const todaysTimestamp = getTimestampAtStartOfDayUTC(timestamp);

Expand All @@ -29,8 +40,6 @@ const fetch = () => {
}
`;

const endpoint =
"https://linea-graph-node.metavault.trade/subgraphs/name/metavault/perpv1";
const response = await request(endpoint, graphQuery);
const volumeStats: IVolumeStat[] = response.volumeStats;

Expand All @@ -57,8 +66,15 @@ const methodology = {
const adapter: SimpleAdapter = {
adapter: {
[CHAIN.LINEA]: {
fetch: fetch(),
start: 1701950449,
fetch: fetch(endpoints[CHAIN.LINEA]),
start: 1709251200,
meta: {
methodology,
},
},
[CHAIN.POLYGON]: {
fetch: fetch(endpoints[CHAIN.POLYGON]),
start: 1709251200,
meta: {
methodology,
},
Expand Down
58 changes: 38 additions & 20 deletions fees/metavault-derivatives-v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,24 @@ import { CHAIN } from "../../helpers/chains";

import { getTimestampAtStartOfDayUTC } from "../../utils/date";

const endpoints = {
const endpoints: ChainEndpoints = {
[CHAIN.LINEA]:
"https://linea-graph-node.metavault.trade/subgraphs/name/metavault/perpv1",
"https://api.studio.thegraph.com/query/55804/linea-trade/version/latest",
[CHAIN.POLYGON]:
"https://api.thegraph.com/subgraphs/name/sdcrypt0/polygon-trade",
};
interface IFeeStat {
cumulativeFeeUsd: string;
feeUsd: string;
id: string;
}

const graphs = (graphUrls: ChainEndpoints) => {
return (chain: Chain) => {
return async (timestamp: number) => {
const todaysTimestamp = getTimestampAtStartOfDayUTC(timestamp);
const period = "daily";
const fetch = (endpoint) => {
return async (timestamp: number) => {
const todaysTimestamp = getTimestampAtStartOfDayUTC(timestamp);
const period = "daily";

const graphQuery = gql`{
const graphQuery = gql`{
feeStats(where: {timestamp: ${todaysTimestamp}, period: "${period}"}) {
id
timestamp
Expand All @@ -28,28 +34,40 @@ const graphs = (graphUrls: ChainEndpoints) => {
}
}`;

const graphRes = await request(graphUrls[chain], graphQuery);
const response = await request(endpoint, graphQuery);
const feeStats: IFeeStat[] = response.feeStats;

const dailyFee = parseInt(graphRes.feeStats[0].feeUsd);
let dailyFeeUSD = BigInt(0);
let totalFeeUSD = BigInt(0);

const finalDailyFee = dailyFee / 1e18;
const totalFees = parseInt(graphRes.feeStats[0].cumulativeFeeUsd) / 1e18;
feeStats.forEach((fee) => {
dailyFeeUSD += BigInt(fee.feeUsd);
totalFeeUSD += BigInt(fee.cumulativeFeeUsd);
});

return {
timestamp,
dailyFees: finalDailyFee.toString(),
totalFees: totalFees.toString(),
//dailyRevenue: (finalDailyFee * 0.3).toString(),
};
const finalDailyFee = parseInt(dailyFeeUSD.toString()) / 1e18;
const finalTotalFee = parseInt(totalFeeUSD.toString()) / 1e18;

return {
timestamp: todaysTimestamp,
dailyFees: finalDailyFee.toString(),
totalFees: finalTotalFee.toString(),
};
};
};

const adapter: Adapter = {
adapter: {
[CHAIN.LINEA]: {
fetch: graphs(endpoints)(CHAIN.LINEA),
start: 1701950449,
fetch: fetch(endpoints[CHAIN.LINEA]),
start: 1709251200,
meta: {
methodology: "All treasuryFee, poolFee and keeperFee are collected",
},
},
[CHAIN.POLYGON]: {
fetch: fetch(endpoints[CHAIN.POLYGON]),
start: 1709251200,
meta: {
methodology: "All treasuryFee, poolFee and keeperFee are collected",
},
Expand Down

0 comments on commit 32896e4

Please sign in to comment.