-
Notifications
You must be signed in to change notification settings - Fork 816
/
index.ts
112 lines (97 loc) · 2.83 KB
/
index.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
import { Adapter, FetchOptions } from "../../adapters/types";
import { CHAIN } from "../../helpers/chains";
import request from "graphql-request";
import BigNumber from "bignumber.js";
type TEndpoint = {
[s: CHAIN | string]: string;
}
const endpoints: TEndpoint = {
[CHAIN.OPTIMISM]: `https://api.thegraph.com/subgraphs/name/extrafi/extrasubgraph`,
[CHAIN.BASE]: `https://api.thegraph.com/subgraphs/name/extrafi/extrafionbase`
}
interface IFeePaid {
amount: string;
asset: string;
}
interface ILendingPaid {
eToken: string;
value: string;
}
interface ILendingPool {
eTokenAddress: string;
exchangeRate: string;
underlyingTokenAddress: string;
}
const graphs = (chain: CHAIN) => {
return async ({ fromTimestamp, toTimestamp, createBalances }: FetchOptions) => {
const dailyFees = createBalances()
const farmingQuery = `{
feePaids(
where: { blockTimestamp_lte: ${toTimestamp}, blockTimestamp_gte: ${fromTimestamp} },
first: 1000
) {
amount
asset
}
}`
const graphRes: IFeePaid[] = (await request(endpoints[chain], farmingQuery)).feePaids;
const lendingQuery = `{
mintToTreasuries(
where: { blockTimestamp_lte: ${toTimestamp}, blockTimestamp_gte: ${fromTimestamp} },
first: 1000
) {
eToken
value
}
}`
const lendingGraphRes: ILendingPaid[] = (await request(endpoints[chain], lendingQuery)).mintToTreasuries;
const lendingPoolsQuery = `{
lendingReservePools(first: 1000) {
eTokenAddress
exchangeRate
underlyingTokenAddress
}
}`
const lendingPoolsGraphRes: ILendingPool[] = (await request(endpoints[chain], lendingPoolsQuery)).lendingReservePools;
const lendingFeeList = lendingGraphRes.map((e: ILendingPaid) => {
const targetPoolInfo = lendingPoolsGraphRes.find(poolInfo => {
return e.eToken?.toLowerCase() === poolInfo.eTokenAddress?.toLowerCase()
})
if (targetPoolInfo) {
const asset = targetPoolInfo.underlyingTokenAddress
const amount = new BigNumber(e.value).multipliedBy(new BigNumber(targetPoolInfo.exchangeRate).div(new BigNumber(`1e+18`))).toFixed(0)
return {
asset,
amount
}
}
return {
asset: e.eToken,
amount: e.value
}
})
const allFeesList = [...graphRes, ...lendingFeeList]
allFeesList.map((e: IFeePaid) => {
dailyFees.add(e.asset, e.amount)
})
const dailyRevenue = dailyFees.clone()
dailyRevenue.resizeBy(0.5)
return {
dailyFees, dailyRevenue,
};
};
}
const adapter: Adapter = {
version: 2,
adapter: {
[CHAIN.OPTIMISM]: {
fetch: graphs(CHAIN.OPTIMISM),
start: 1683450630,
},
[CHAIN.BASE]: {
fetch: graphs(CHAIN.BASE),
start: 1693449471,
},
},
};
export default adapter;