-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 #878 from dalveytech/master
blex add volume and derivatives volume
- Loading branch information
Showing
1 changed file
with
91 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,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; |