mirror of
https://github.com/mempool/mempool.git
synced 2025-03-15 12:20:28 +01:00
Merge pull request #4561 from mempool/mononaut/fix-prices
Fix bad coinbase price url, switch to median prices
This commit is contained in:
commit
f694d5333a
2 changed files with 12 additions and 6 deletions
|
@ -5,14 +5,14 @@ class CoinbaseApi implements PriceFeed {
|
||||||
public name: string = 'Coinbase';
|
public name: string = 'Coinbase';
|
||||||
public currencies: string[] = ['USD', 'EUR', 'GBP'];
|
public currencies: string[] = ['USD', 'EUR', 'GBP'];
|
||||||
|
|
||||||
public url: string = 'https://api.coinbase.com/v2/prices/spot?currency=';
|
public url: string = 'https://api.coinbase.com/v2/prices/BTC-{CURRENCY}/spot';
|
||||||
public urlHist: string = 'https://api.exchange.coinbase.com/products/BTC-{CURRENCY}/candles?granularity={GRANULARITY}';
|
public urlHist: string = 'https://api.exchange.coinbase.com/products/BTC-{CURRENCY}/candles?granularity={GRANULARITY}';
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async $fetchPrice(currency): Promise<number> {
|
public async $fetchPrice(currency): Promise<number> {
|
||||||
const response = await query(this.url + currency);
|
const response = await query(this.url.replace('{CURRENCY}', currency));
|
||||||
if (response && response['data'] && response['data']['amount']) {
|
if (response && response['data'] && response['data']['amount']) {
|
||||||
return parseInt(response['data']['amount'], 10);
|
return parseInt(response['data']['amount'], 10);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -23,6 +23,14 @@ export interface PriceHistory {
|
||||||
[timestamp: number]: ApiPrice;
|
[timestamp: number]: ApiPrice;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getMedian(arr: number[]): number {
|
||||||
|
const sortedArr = arr.slice().sort((a, b) => a - b);
|
||||||
|
const mid = Math.floor(sortedArr.length / 2);
|
||||||
|
return sortedArr.length % 2 !== 0
|
||||||
|
? sortedArr[mid]
|
||||||
|
: (sortedArr[mid - 1] + sortedArr[mid]) / 2;
|
||||||
|
}
|
||||||
|
|
||||||
class PriceUpdater {
|
class PriceUpdater {
|
||||||
public historyInserted = false;
|
public historyInserted = false;
|
||||||
private timeBetweenUpdatesMs = 360_0000 / config.MEMPOOL.PRICE_UPDATES_PER_HOUR;
|
private timeBetweenUpdatesMs = 360_0000 / config.MEMPOOL.PRICE_UPDATES_PER_HOUR;
|
||||||
|
@ -173,7 +181,7 @@ class PriceUpdater {
|
||||||
if (prices.length === 0) {
|
if (prices.length === 0) {
|
||||||
this.latestPrices[currency] = -1;
|
this.latestPrices[currency] = -1;
|
||||||
} else {
|
} else {
|
||||||
this.latestPrices[currency] = Math.round((prices.reduce((partialSum, a) => partialSum + a, 0)) / prices.length);
|
this.latestPrices[currency] = Math.round(getMedian(prices));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -300,9 +308,7 @@ class PriceUpdater {
|
||||||
if (grouped[time][currency].length === 0) {
|
if (grouped[time][currency].length === 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
prices[currency] = Math.round((grouped[time][currency].reduce(
|
prices[currency] = Math.round(getMedian(grouped[time][currency]));
|
||||||
(partialSum, a) => partialSum + a, 0)
|
|
||||||
) / grouped[time][currency].length);
|
|
||||||
}
|
}
|
||||||
await PricesRepository.$savePrices(parseInt(time, 10), prices);
|
await PricesRepository.$savePrices(parseInt(time, 10), prices);
|
||||||
++totalInserted;
|
++totalInserted;
|
||||||
|
|
Loading…
Add table
Reference in a new issue