/
pika-protocol-v4.ts
129 lines (119 loc) · 3.23 KB
/
pika-protocol-v4.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
128
129
import { Chain } from "@defillama/sdk/build/general";
import BigNumber from "bignumber.js";
import request, { gql } from "graphql-request";
import { Adapter, FetchResultFees, FetchResultVolume } from "../adapters/types";
import { CHAIN } from "../helpers/chains";
import { getUniqStartOfTodayTimestamp } from "../helpers/getUniSubgraphVolume";
import { getTimestampAtStartOfDayUTC } from "../utils/date";
import { getBlock } from "../helpers/getBlock";
interface IVault {
txCount: number;
}
interface ITransaction {
id: number;
timestamp: number;
tradeFee: number;
count: number;
}
interface ILiquidation {
timestamp: number;
remainingReward: number;
}
type IURL = {
[l: string | Chain]: string;
}
interface IFees {
vaults: IVault[];
}
interface ITransactions {
transactions: ITransaction[]
}
interface ILiquidations {
liquidations: ILiquidation[]
}
const endpoints: IURL = {
[CHAIN.OPTIMISM]: "https://api.thegraph.com/subgraphs/name/ethandev0/pikaperpv4_optimism"
}
const fetch = (chain: Chain) => {
return async (): Promise<FetchResultFees> => {
let todayLiquidationFee = 0;
let todayTradeFee = 0;
const timestamp = Math.floor(Date.now() / 1000 - 86400);
const graphQuery1 = gql
`
{
vaults(first: 1)
{
txCount
}
}
`;
const res1: IFees = (await request(endpoints[chain], graphQuery1));
const totalCount = res1.vaults[0].txCount
let skip = totalCount;
while(skip > totalCount - 3000) {
const graphQuery2 = gql
`
{
transactions(first:1000, where: {count_lt: ${skip}}, orderBy: count, orderDirection: desc)
{
id
singleAmount
timestamp
tradeFee
count
}
}
`;
const res2: ITransactions = (await request(endpoints[chain], graphQuery2));
if (res2 !== undefined) {
let transactions = res2.transactions;
transactions.map((item: ITransaction) => {
if (item.timestamp > timestamp) {
todayTradeFee += item.tradeFee / 100000000
}
});
skip = transactions[transactions.length - 1].count * 1
}
}
const graphQuery3 = gql
`
{
liquidations(where: {
timestamp_gt: ${timestamp}
})
{
id
remainingReward
timestamp
}
}
`;
const res3: ILiquidations = (await request(endpoints[chain], graphQuery3));
let liquidations = res3.liquidations;
liquidations.map((item: ILiquidation) => {
if (item.timestamp > timestamp) {
todayLiquidationFee += item.remainingReward / 100000000;
}
});
const dailyFees = todayTradeFee + todayLiquidationFee;
const dailySupplySideRevenue = dailyFees * 0.5;
const dailyProtocolRevenue = dailyFees * 0.3;
return {
timestamp,
dailyFees: dailyFees.toString(),
dailyRevenue: dailyProtocolRevenue.toString(),
dailySupplySideRevenue: dailySupplySideRevenue.toString(),
dailyProtocolRevenue: dailyProtocolRevenue.toString(),
};
}
}
const adapter: Adapter = {
adapter: {
[CHAIN.OPTIMISM]: {
fetch: fetch(CHAIN.OPTIMISM),
start: 1687910400,
},
},
};
export default adapter;