1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const node_fetch_1 = require("node-fetch");
|
4 | const ecb_1 = require("./ecb");
|
5 | const lodash_1 = require("lodash");
|
6 | const COINMARKETCAP_API = 'https://api.coinmarketcap.com/v1/ticker/';
|
7 | const ROUNDING_FACTOR = 100000000;
|
8 | class ECBAndCoinMarketCapBackend extends ecb_1.default {
|
9 | async connect() {
|
10 | await super.connect();
|
11 | const ccRates = await this._getCCRates(this.rates.USD);
|
12 | Object.assign(this.rates, ccRates);
|
13 | this.currencies = this.currencies.concat(Object.keys(ccRates));
|
14 | this.currencies.sort();
|
15 | }
|
16 | async _getCCRates(usdRate) {
|
17 | const rateRes = await node_fetch_1.default(COINMARKETCAP_API);
|
18 | if (rateRes.status !== 200) {
|
19 | throw new Error('Unexpected status from coinmarketcap.com: ' + rateRes.status);
|
20 | }
|
21 | const body = await rateRes.json();
|
22 | return lodash_1.fromPairs(body.map((rateInfo) => {
|
23 | return [rateInfo.symbol, Math.floor(ROUNDING_FACTOR / (rateInfo.price_usd * usdRate)) / ROUNDING_FACTOR];
|
24 | }));
|
25 | }
|
26 | }
|
27 | exports.default = ECBAndCoinMarketCapBackend;
|
28 |
|
\ | No newline at end of file |