Skip to content

Commit

Permalink
fix drift protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
dtmkeng committed Sep 10, 2024
1 parent 7e3def5 commit 41356f2
Showing 1 changed file with 20 additions and 35 deletions.
55 changes: 20 additions & 35 deletions dexs/drift-protocol/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { CHAIN } from "../../helpers/chains";
import { httpGet } from "../../utils/fetchURL";
import { queryDune } from "../../helpers/dune";
import { BreakdownAdapter, FetchOptions } from "../../adapters/types";

const DAILY_VOL_ENDPOINT =
"https://mainnet-beta.api.drift.trade/stats/24HourVolume";

// const DUNE_QUERY_ID = "3756979"; // https://dune.com/queries/3756979/6318568
const DUNE_QUERY_ID = "4057938"; // Should be faster than the above - https://dune.com/queries/3782153/6359334

Expand All @@ -16,53 +12,42 @@ type DimentionResult = {
dailyRevenue?: number;
};

type IRequest = {
[key: string]: Promise<any>;
}
const requests: IRequest = {}

export async function fetchURLWithRetry(url: string, options: FetchOptions) {
if (!requests[url])
requests[url] = queryDune("4059377", {
start: options.startOfDay,
end: options.startOfDay + 24 * 60 * 60,
})
return requests[url]
}

async function getPerpDimensions(options: FetchOptions): Promise<DimentionResult> {
const dayInSec = 24 * 60 * 60;
const resultRows = await queryDune(DUNE_QUERY_ID, {
start: options.startOfDay,
end: options.startOfDay + dayInSec,
});

const { perpetual_volume, total_revenue, total_taker_fee } = resultRows[0];

return {
dailyVolume: perpetual_volume,
dailyFees: total_taker_fee,
dailyUserFees: total_taker_fee,
dailyRevenue: total_revenue,
};
const volumeResponse = await fetchURLWithRetry("4059377", options)
const dailyVolume = volumeResponse[0].perpetual_volume;
return { dailyVolume };
}

async function getSpotDimensions(): Promise<DimentionResult> {
const volumeResponse = await httpGet(
`${DAILY_VOL_ENDPOINT}?spotMarkets=true`
);

const rawVolumeQuotePrecision = volumeResponse.data.volume;

// Volume will be returned in 10^6 precision
const dailyVolume =
rawVolumeQuotePrecision.length >= 6
? Number(rawVolumeQuotePrecision.slice(0, -6))
: 0;

async function getSpotDimensions(options: FetchOptions): Promise<DimentionResult> {
const volumeResponse = await fetchURLWithRetry("4059377", options)
const dailyVolume = volumeResponse[0].spot_volume;
return { dailyVolume };
}

async function fetch(type: "perp" | "spot", options: FetchOptions) {
const timestamp = Date.now() / 1e3;

if (type === "perp") {
const results = await getPerpDimensions(options);

return {
...results,
timestamp,
};
} else {
const results = await getSpotDimensions();

const results = await getSpotDimensions(options);
return {
...results,
timestamp: Date.now() / 1e3,
Expand Down

0 comments on commit 41356f2

Please sign in to comment.