Skip to content

Commit

Permalink
Merge pull request #676 from chameleon-jeff/master
Browse files Browse the repository at this point in the history
Hyperliquid: Use global stats endpoint to include all tokens in volume
  • Loading branch information
dtmkeng committed Jul 28, 2023
2 parents 4217a7c + 99ea280 commit 447c59d
Showing 1 changed file with 7 additions and 41 deletions.
48 changes: 7 additions & 41 deletions dexs/hyperliquid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,19 @@ import { CHAIN } from "../../helpers/chains";
import axios from "axios";

const URL = "https://api.hyperliquid.xyz/info";
const coins = {
'ARB': 'coingecko:arbitrum',
'DOGE': 'coingecko:dogecoin',
'OP': 'coingecko:optimism',
'DYDX': 'coingecko:dydx',
'SOL': 'coingecko:solana',
'LTC': 'coingecko:litecoin',
'AVAX': 'coingecko:avalanche-2',
'ETH': 'coingecko:ethereum',
'ATOM': 'coingecko:cosmos',
'BTC': 'coingecko:bitcoin',
'MATIC': 'coingecko:matic-network',
'APE': 'coingecko:apecoin',
'BNB': 'coingecko:binancecoin',
}


interface IAPIResponse {
volume: string;
timestamp: number;
id: string;
volumeUSD: number;
closePrice: string;
};

const getBody = (coin: string) => {
return {"type":"candleSnapshot", "req": {"coin": coin, "interval": "1d", "startTime": 1677283200000}}
interface Response {
totalVolume?: number;
dailyVolume?: number;
}

const fetch = async (timestamp: number) => {
const dayTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000))
const historical = (await Promise.all(Object.keys(coins).map((coins: string) =>axios.post(URL, getBody(coins)))))
.map((a: any, index: number) => a.data.map((e: any) => { return { timestamp: e.t / 1000, volume: e.v, closePrice: e.c, id: Object.values(coins)[index]}})).flat()
const {totalVolume, dailyVolume}: Response = (await axios.post(URL, {"type": "globalStats"})).data;
const dayTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000));

const historicalUSD = historical.map((e: IAPIResponse) => {
return {
...e,
volumeUSD: Number(e.volume) * Number(e.closePrice)
}
});
const dailyVolume = historicalUSD.filter((e: IAPIResponse) => e.timestamp === dayTimestamp)
.reduce((a: number, {volumeUSD}) => a+volumeUSD, 0);
const totalVolume = historicalUSD.filter((e: IAPIResponse) => e.timestamp <= dayTimestamp)
.reduce((a: number, {volumeUSD}) => a+volumeUSD, 0);
return {
totalVolume: `${totalVolume}`,
dailyVolume: dailyVolume ? `${dailyVolume}` : undefined,
totalVolume: totalVolume?.toString(),
dailyVolume: dailyVolume?.toString(),
timestamp: dayTimestamp,
};
};
Expand Down

0 comments on commit 447c59d

Please sign in to comment.