Skip to content

Commit

Permalink
Merge pull request #574 from ftm1337/equity
Browse files Browse the repository at this point in the history
Add Equity - Fees & Volume adapters
  • Loading branch information
dtmkeng committed Jul 3, 2023
2 parents 1e539e2 + 9700438 commit 55ab4d3
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
73 changes: 73 additions & 0 deletions dexs/equity-spot/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import request, { gql } from "graphql-request";
import { Fetch, SimpleAdapter } from "../../adapters/types";
import { CHAIN } from "../../helpers/chains";
import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume";

const endpoints: { [key: string]: string } = {
[CHAIN.FANTOM]: "https://api.thegraph.com/subgraphs/name/chimpydev/equity-core",
}

const historicalData = gql`
query get_volume($period: String!, $id: String!) {
volumeStats(where: {period: $period, id: $id}) {
swap
}
}
`

interface IGraphResponse {
volumeStats: Array<{
burn: string,
liquidation: string,
margin: string,
mint: string,
swap: string,
}>
}

const getFetch = (chain: string): Fetch => async (timestamp: number) => {
const dayTimestamp = getUniqStartOfTodayTimestamp(new Date((timestamp * 1000)))
const dailyData: IGraphResponse = await request(endpoints[chain], historicalData, {
id: String(dayTimestamp) + ':daily',
period: 'daily',
})
const totalData: IGraphResponse = await request(endpoints[chain], historicalData, {
id: 'total',
period: 'total',
})

return {
timestamp: dayTimestamp,
dailyVolume:
dailyData.volumeStats.length == 1
? String(Number(Object.values(dailyData.volumeStats[0]).reduce((sum, element) => String(Number(sum) + Number(element)))) * 10 ** -30)
: undefined,
totalVolume:
totalData.volumeStats.length == 1
? String(Number(Object.values(totalData.volumeStats[0]).reduce((sum, element) => String(Number(sum) + Number(element)))) * 10 ** -30)
: undefined,

}
}

const getStartTimestamp = async (chain: string) => {
const startTimestamps: { [chain: string]: number } = {
[CHAIN.FANTOM]: 1685464000,
}
return startTimestamps[chain]
}

const adapter: SimpleAdapter = {
adapter: Object.keys(endpoints).reduce((acc, chain) => {
return {
...acc,
[chain]: {
fetch: getFetch(chain),
start: async () => getStartTimestamp(chain),
runAtCurrTime: true
}
}
}, {})
}

export default adapter;
57 changes: 57 additions & 0 deletions fees/equity/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Chain } from "@defillama/sdk/build/general";
import { gql, request } from "graphql-request";
import type { ChainEndpoints } from "../../adapters/types";
import { Adapter } from "../../adapters/types";
import { FANTOM } from "../../helpers/chains";
import { getTimestampAtStartOfDayUTC } from "../../utils/date";

const endpoints = {
[FANTOM]: "https://api.thegraph.com/subgraphs/name/chimpydev/equity-core",
};

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

const graphQuery = gql`{
feeStat(id: "${searchTimestamp}") {
mint
burn
marginAndLiquidation
swap
}
}`;

const graphRes = await request(graphUrls[chain], graphQuery);

const dailyFee =
parseInt(graphRes.feeStat.mint) +
parseInt(graphRes.feeStat.burn) +
parseInt(graphRes.feeStat.marginAndLiquidation) +
parseInt(graphRes.feeStat.swap);
const finalDailyFee = dailyFee / 1e30;

return { // 100.00% of All & Any Fees generated goes to veEQUAL voters
timestamp,
dailyFees: finalDailyFee.toString(),
dailyRevenue: finalDailyFee.toString(),
};
};
};
};

const adapter: Adapter = {
adapter: {
[FANTOM]: {
fetch: graphs(endpoints)(FANTOM),
start: async () => 1685464000,
meta: {
methodology: '100.00% of All & Any Fees generated from All activity on Any Equity Platform Product goes solely to veEQUAL voters.'
}
},
},
};

export default adapter;

0 comments on commit 55ab4d3

Please sign in to comment.