Skip to content

Commit

Permalink
Merge pull request #878 from dalveytech/master
Browse files Browse the repository at this point in the history
blex add volume and derivatives volume
  • Loading branch information
dtmkeng authored Oct 10, 2023
2 parents 48db9a1 + ed18038 commit 0e88a1b
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions dexs/blex/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import request, { gql } from "graphql-request";
import { BreakdownAdapter, Fetch, SimpleAdapter } from "../../adapters/types";
import { CHAIN } from "../../helpers/chains";
import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume";

const endpoints: { [key: string]: string } = {
[CHAIN.ARBITRUM]: "https://api.thegraph.com/subgraphs/name/blex-dex/arbitrum_42161",
}

const allData=gql`
query get_summary($period: String!, $id: String!){
summaries(where: {period: $period, id: $id}){
tradingLPVolume
tradingVolume
}
}
`
const derivativesData=gql`
query get_summary($period: String!, $id: String!){
summaries(where: {period: $period, id: $id}){
tradingVolume
}
}
`

interface IGraphResponse {
summaries: Array<{
tradingVolume: string
tradingLPVolume: string
trades: string
openInterest: string
uniqueUsers: string
fees: string
lpVolume: string
}>
}

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

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

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

const adapter: BreakdownAdapter = {
breakdown: {
"volume": Object.keys(endpoints).reduce((acc, chain) => {
return {
...acc,
[chain]: {
fetch: getFetch(allData)(chain),
start: async () => getStartTimestamp(chain)
}
}
}, {}),
"derivatives": Object.keys(endpoints).reduce((acc, chain) => {
return {
...acc,
[chain]: {
fetch: getFetch(derivativesData)(chain),
start: async () => getStartTimestamp(chain)
}
}
}, {})
}
}
export default adapter;

0 comments on commit 0e88a1b

Please sign in to comment.