-
Notifications
You must be signed in to change notification settings - Fork 950
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #574 from ftm1337/equity
Add Equity - Fees & Volume adapters
- Loading branch information
Showing
2 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |