forked from DefiLlama/yield-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmx.js
81 lines (69 loc) · 2.24 KB
/
gmx.js
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
const axios = require('axios');
const { request, gql } = require('graphql-request');
const api = 'https://api.gmx.io';
const chains = {
arbitrum: {
api: 'https://api.gmx.io',
subgraph: 'https://api.thegraph.com/subgraphs/name/gmx-io/gmx-stats',
},
avalanche: {
api: 'https://gmx-avax-server.uc.r.appspot.com',
subgraph:
'https://api.thegraph.com/subgraphs/name/gmx-io/gmx-avalanche-stats',
},
};
const q = gql`
query MyQuery($tokens: [String]) {
fundingRates(
orderBy: endTimestamp
orderDirection: desc
where: { token_in: $tokens }
) {
id
endFundingRate
endTimestamp
startFundingRate
startTimestamp
token
}
}
`;
exports.getPerpData = async () => {
const data = await Promise.all(
Object.keys(chains).map(async (chain) => {
const prices = (await axios.get(`${chains[chain].api}/prices`)).data;
const tokens = Object.values(
(await axios.get(`${chains[chain].api}/tokens`)).data
);
const markets = Object.keys(prices).map((m) => m.toLowerCase());
const fundingRates = (
await request(chains[chain].subgraph, q, {
tokens: markets,
})
).fundingRates.filter((p) => !p.id.includes('total'));
return Object.entries(prices).map((i) => {
const token = tokens.find((t) => t.id === i[0]).data;
const price = Number(i[1]) / 10 ** 30;
const oiLong = token.guaranteedUsd / 1e30;
const oiShort = (token.globalShortSize / token.liqMaxPrice) * price;
const fr = fundingRates.filter(
(t) => t.token.toLowerCase() === i[0].toLowerCase()
)[1]; // 0 -> most recent, 1 -> previous
const timeDelta = (fr.endTimestamp - fr.startTimestamp) / (60 * 60);
const frPrevious =
(fr.endFundingRate - fr.startFundingRate) / timeDelta;
return {
marketplace: `GMX-${chain}`,
market: `${token.symbol}-USD`,
baseAsset: token.symbol,
fundingRate: Number(token.fundingRate) / 1e6,
fundingRatePrevious: frPrevious / 1e6,
fundingTimePrevious: fr.endTimestamp,
openInterest: oiLong + oiShort,
indexPrice: price,
};
});
})
);
return data.flat();
};