-
Notifications
You must be signed in to change notification settings - Fork 807
/
blitz.ts
127 lines (111 loc) · 3.15 KB
/
blitz.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { CHAIN } from "../helpers/chains";
import { Adapter, FetchResultFees } from "../adapters/types";
import { httpPost } from "../utils/fetchURL";
interface MarketSnapshots {
interval: {
count: number;
granularity: number;
max_time: number;
};
}
interface QueryBody {
market_snapshots: MarketSnapshots;
}
interface IData {
[s: string]: string;
}
interface Snapshot {
[s: string]: IData;
}
interface Response {
snapshots: Snapshot[];
}
const query = async (max_time: number): Promise<Response> => {
const body: QueryBody = {
market_snapshots: {
interval: {
count: 2,
granularity: 86400,
max_time: max_time,
},
},
};
const archiveBaseUrl = "https://archive.blast-prod.vertexprotocol.com/v1";
const response = await httpPost(archiveBaseUrl, body);
return response;
};
const sumAllProductStats = (stat_map: IData): number => {
let stat_sum = 0;
for (const v of Object.values(stat_map)) {
stat_sum += parseInt(v);
}
return stat_sum / 1e18;
};
const get24hrStat = async (
field: string,
max_time: number
): Promise<number> => {
const response = await query(max_time);
const cur_res: Snapshot = response.snapshots[0];
const past_res: Snapshot = response.snapshots[1];
return (
sumAllProductStats(cur_res[field]) - sumAllProductStats(past_res[field])
);
};
const getCumulativeStat = async (
field: string,
max_time: number
): Promise<number> => {
const response = await query(max_time);
const cur_res = response.snapshots[0];
return sumAllProductStats(cur_res[field]);
};
const getCumulativeFees = async (max_time: number): Promise<number> => {
const fees = await getCumulativeStat("cumulative_taker_fees", max_time);
const sequencer_fees = await getCumulativeStat(
"cumulative_sequencer_fees",
max_time
);
return fees - sequencer_fees;
};
const getCumulativeRevenue = async (max_time: number): Promise<number> => {
const fees = await getCumulativeFees(max_time);
const rebates = await getCumulativeStat("cumulative_maker_fees", max_time);
return fees + rebates;
};
const get24hrFees = async (max_time: number): Promise<number> => {
const fees = await get24hrStat("cumulative_taker_fees", max_time);
const sequencer_fees = await get24hrStat(
"cumulative_sequencer_fees",
max_time
);
return fees - sequencer_fees;
};
const get24hrRevenue = async (max_time: number): Promise<number> => {
const fees = await get24hrFees(max_time);
const rebates = await get24hrStat("cumulative_maker_fees", max_time);
return fees + rebates;
};
const fetch = async (timestamp: number): Promise<FetchResultFees> => {
const dailyFees = await get24hrFees(timestamp);
const dailyRevenue = await get24hrRevenue(timestamp);
const totalFees = await getCumulativeFees(timestamp);
const totalRev = await getCumulativeRevenue(timestamp);
return {
dailyFees: `${dailyFees}`,
dailyRevenue: `${dailyRevenue}`,
totalRevenue: `${totalRev}`,
totalFees: `${totalFees}`,
timestamp,
};
};
const adapter: Adapter = {
adapter: {
[CHAIN.BLAST]: {
fetch: fetch,
runAtCurrTime: true,
start: 1710259200,
},
},
};
export default adapter;