-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.js
More file actions
31 lines (28 loc) · 901 Bytes
/
controller.js
File metadata and controls
31 lines (28 loc) · 901 Bytes
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
class BitcoinController {
#bitcoin
#bitcoinPrices
#commission = process.env.BITCOIN_COMMISSION
constructor() {
setInterval(async () => {
await this.#syncBitcoin()
}, process.env.BITCOIN_FETCH_INTERVAL || 1000)
}
async #syncBitcoin() {
this.#bitcoin = await (await fetch(process.env.BINANCE_API_URL)).json() || this.#bitcoin
this.#bitcoinPrices = this.#formula(this.#bitcoin)
}
async #formula(bitcoin) {
const bidPrice = bitcoin.bidPrice * (1 + this.#commission)
const askPrice = bitcoin.askPrice * (1 + this.#commission)
const midPrice = (bidPrice + askPrice) / 2
return {
bidPrice,
askPrice,
midPrice
}
}
async get() {
return new Promise((resolve, _) => resolve(this.#bitcoinPrices));
}
}
module.exports = BitcoinController;