Skip to content

Commit 9d4b4d6

Browse files
authored
Merge pull request #1534 from lukecaan/master
Fixed issues with Drift adapter
2 parents 27dda68 + 57d435d commit 9d4b4d6

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

dexs/drift-protocol/index.ts

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { BreakdownAdapter } from "../../adapters/types";
66
const DAILY_VOL_ENDPOINT =
77
"https://mainnet-beta.api.drift.trade/stats/24HourVolume";
88

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

1112
type DimentionResult = {
1213
dailyVolume?: number;
@@ -15,19 +16,26 @@ type DimentionResult = {
1516
dailyRevenue?: number;
1617
};
1718

18-
async function getPerpDimensions(): Promise<DimentionResult> {
19+
const sum = (values: number[]) => values.reduce((a, b) => a + b, 0);
20+
21+
// This is the previous method for query_id 3756979 which was too slow .. saving for posterity if we manage to speed it up.
22+
async function _getPerpDimensions(): Promise<DimentionResult> {
1923
const resultRows = await queryDune(DUNE_QUERY_ID);
2024

21-
const summaryRow = resultRows.find((row) => row.market_index === null);
25+
const marketRows = resultRows.filter(
26+
(row) => row.market_index !== null && row.market_index >= 0
27+
);
2228

2329
// Perp Volume
24-
const dailyVolume = summaryRow.total_volume as number;
30+
const dailyVolume = sum(marketRows.map((row) => row.total_volume as number));
2531

2632
// All taker fees paid
27-
const dailyFees = summaryRow.total_taker_fee as number;
33+
const dailyFees = sum(marketRows.map((row) => row.total_taker_fee as number));
2834

2935
// All taker fees paid, minus maker rebates paid - not sure if this should be used as the "dailyFees" number instead.
30-
const dailyRevenue = summaryRow.total_revenue as number;
36+
const dailyRevenue = sum(
37+
marketRows.map((row) => row.total_revenue as number)
38+
);
3139

3240
return {
3341
dailyVolume,
@@ -37,6 +45,19 @@ async function getPerpDimensions(): Promise<DimentionResult> {
3745
};
3846
}
3947

48+
async function getPerpDimensions(): Promise<DimentionResult> {
49+
const resultRows = await queryDune(DUNE_QUERY_ID);
50+
51+
const { perpetual_volume, total_revenue, total_taker_fee } = resultRows[0];
52+
53+
return {
54+
dailyVolume: perpetual_volume,
55+
dailyFees: total_taker_fee,
56+
dailyUserFees: total_taker_fee,
57+
dailyRevenue: total_revenue,
58+
};
59+
}
60+
4061
async function getSpotDimensions(): Promise<DimentionResult> {
4162
const volumeResponse = await httpGet(
4263
`${DAILY_VOL_ENDPOINT}?spotMarkets=true`
@@ -72,21 +93,24 @@ async function fetch(type: "perp" | "spot") {
7293
};
7394
}
7495
}
75-
const emtry = async (timestamp: number) => {return{timestamp}}
96+
97+
// Used to replace "fetch" to disable a query if it starts failing
98+
const emtry = async (timestamp: number) => {
99+
return { timestamp };
100+
};
101+
76102
const adapter: BreakdownAdapter = {
77103
breakdown: {
78104
swap: {
79105
[CHAIN.SOLANA]: {
80-
// fetch: () => fetch("spot"),
81-
fetch: emtry,
106+
fetch: () => fetch("spot"),
82107
runAtCurrTime: true,
83108
start: 1690239600,
84109
},
85110
},
86111
derivatives: {
87112
[CHAIN.SOLANA]: {
88-
// // fetch: () => fetch("perp"),
89-
fetch: emtry,
113+
fetch: () => fetch("perp"),
90114
runAtCurrTime: true,
91115
start: 1690239600,
92116
},

0 commit comments

Comments
 (0)