-
Notifications
You must be signed in to change notification settings - Fork 0
/
apr.ts
150 lines (130 loc) · 4.09 KB
/
apr.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { ethers } from "ethers";
import BigNumber from "bignumber.js";
import {
getSubsLiquidity,
getV3Liquidity,
getXdaiLiquidity,
// getEthPrice,
// getHnyPrice,
getBrightPrice,
getBrightSupply,
} from "./prices"
import express from "express";
export const aprRouter = express.Router();
// brightFarm
const myInfuraURL =
"https://mainnet.infura.io/v3/448d344e9a1c483a8ebdcc6c69833e19";
// providers
let ethProvider: ethers.providers.Provider;
let xdaiProvider: ethers.providers.Provider;
try {
// ETH
ethProvider = new ethers.providers.JsonRpcProvider(myInfuraURL);
// XDAI
xdaiProvider = new ethers.providers.JsonRpcProvider(
"https://rpc.ankr.com/gnosis/51ac381249e27150efb048ea0540d297b5445eef551c8d79775cb12ba7f9f9ca"
// "http://nethermind-xdai.dappnode:8545"
);
getBrightPrice(ethProvider);
} catch (err: any) {
console.log("unable to setup ethers");
console.log(err.message);
}
// CACHE variables
let subsBalance = new BigNumber("0");
let totalV3Liquidity = new BigNumber("0");
let xdaiLiquidity = new BigNumber("0");
let brightPrice = new BigNumber("0");
let brightSupply = new BigNumber("0");
let brightCirculatingSupply = new BigNumber("0");
let brightMaxSupply = new BigNumber("0");
const loop = async () => {
try {
subsBalance = await getSubsLiquidity(ethProvider);
totalV3Liquidity = await getV3Liquidity(ethProvider);
xdaiLiquidity = await getXdaiLiquidity(xdaiProvider);
brightPrice = await getBrightPrice(ethProvider);
({brightSupply, brightCirculatingSupply, brightMaxSupply} = await getBrightSupply(ethProvider, xdaiProvider))
} catch (err: any) {
console.log(`unable to query prices: ${err.message}`);
}
};
const INTERVAL_TIME = 180000; // 3 minutes
setInterval(() => {
loop();
}, INTERVAL_TIME);
loop();
aprRouter.get("/ethLiquidity", async (request, response, next) => {
try {
if (subsBalance.isZero() && totalV3Liquidity.isZero) {
throw new Error("Unable to query eth network");
} else {
response.json({
subsBalance: subsBalance.toString(),
totalV3Liquidity: totalV3Liquidity?.toString(),
});
}
} catch (err) {
response.status(500).json({ error: "Unable to query eth network" });
}
});
aprRouter.get("/xdaiLiquidity", async (request, response, next) => {
try {
if (xdaiLiquidity.isZero()) {
throw new Error("Unable to query xdai network");
} else {
response.json({ totalLiquidity: xdaiLiquidity.toString() });
}
} catch {
response.status(500).json({ error: "Unable to query xdai network" });
}
});
aprRouter.get("/brightPrice", async (request, response, next) => {
try {
if (brightPrice.isZero()) {
throw new Error("Unable to query bright price");
} else {
response.json({ usd: brightPrice.toString() });
}
} catch {
response.status(500).json({ error: "Unable to query bright price" });
}
});
aprRouter.get("/brightSupply", async (request, response, next) => {
try {
if (brightSupply.isZero() || brightCirculatingSupply.isZero()) {
throw new Error("Unable to query bright supply");
} else {
response.json({
totalSupply: brightSupply.toFixed(),
maxTotalSupply: brightMaxSupply.toFixed(),
circulating: brightCirculatingSupply.toFixed(),
});
}
} catch {
response.status(500).json({ error: "Unable to query bright price" });
}
});
// maybe add these later?
// aprRouter.get("/ethPrice", async (request, response, next) => {
// try {
// if (ethPrice.isZero()) {
// response.status(500).json({ error: "Unable to get eth price" });
// } else {
// response.json({ usd: ethPrice.toString() });
// }
// } catch {
// response.status(500).json({ error: "Unable to get eth price" });
// }
// });
// aprRouter.get("/hnyPrice", async (request, response, next) => {
// try {
// if (hnyPrice.isZero()) {
// response.status(500).json({ error: "Unable to get hny price" });
// } else {
// response.json({ usd: hnyPrice.toString() });
// }
// } catch {
// response.status(500).json({ error: "Unable to get hny price" });
// }
// });