mirror of
https://github.com/mempool/mempool.git
synced 2025-04-01 16:38:19 +02:00
Allow historical price api to return data of a single currency
This commit is contained in:
parent
f121d16544
commit
b043d698ca
backend/src
frontend/src/app/services
@ -51,13 +51,20 @@ class MiningRoutes {
|
||||
res.status(400).send('Prices are not available on testnets.');
|
||||
return;
|
||||
}
|
||||
if (req.query.timestamp) {
|
||||
res.status(200).send(await PricesRepository.$getNearestHistoricalPrice(
|
||||
parseInt(<string>req.query.timestamp ?? 0, 10)
|
||||
));
|
||||
const timestamp = parseInt(req.query.timestamp as string, 10) || 0;
|
||||
const currency = req.query.currency as string;
|
||||
|
||||
let response;
|
||||
if (timestamp && currency) {
|
||||
response = await PricesRepository.$getNearestHistoricalPrice(timestamp, currency);
|
||||
} else if (timestamp) {
|
||||
response = await PricesRepository.$getNearestHistoricalPrice(timestamp);
|
||||
} else if (currency) {
|
||||
response = await PricesRepository.$getHistoricalPrices(currency);
|
||||
} else {
|
||||
res.status(200).send(await PricesRepository.$getHistoricalPrices());
|
||||
response = await PricesRepository.$getHistoricalPrices();
|
||||
}
|
||||
res.status(200).send(response);
|
||||
} catch (e) {
|
||||
res.status(500).send(e instanceof Error ? e.message : e);
|
||||
}
|
||||
|
@ -315,7 +315,7 @@ class PricesRepository {
|
||||
return rates[0] as ApiPrice;
|
||||
}
|
||||
|
||||
public async $getNearestHistoricalPrice(timestamp: number | undefined): Promise<Conversion | null> {
|
||||
public async $getNearestHistoricalPrice(timestamp: number | undefined, currency?: string): Promise<Conversion | null> {
|
||||
try {
|
||||
const [rates] = await DB.query(`
|
||||
SELECT ${ApiPriceFields}
|
||||
@ -379,7 +379,23 @@ class PricesRepository {
|
||||
USDCHF: computeFx(latestPrice.USD, latestPrice.CHF),
|
||||
USDAUD: computeFx(latestPrice.USD, latestPrice.AUD),
|
||||
USDJPY: computeFx(latestPrice.USD, latestPrice.JPY),
|
||||
};
|
||||
|
||||
if (currency) {
|
||||
if (!latestPrice[currency]) {
|
||||
return null;
|
||||
}
|
||||
const filteredRates = rates.map((rate: any) => {
|
||||
return {
|
||||
time: rate.time,
|
||||
[currency]: rate[currency]
|
||||
};
|
||||
});
|
||||
return {
|
||||
prices: filteredRates as ApiPrice[],
|
||||
exchangeRates: exchangeRates
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
prices: rates as ApiPrice[],
|
||||
@ -391,7 +407,7 @@ class PricesRepository {
|
||||
}
|
||||
}
|
||||
|
||||
public async $getHistoricalPrices(): Promise<Conversion | null> {
|
||||
public async $getHistoricalPrices(currency?: string): Promise<Conversion | null> {
|
||||
try {
|
||||
const [rates] = await DB.query(`
|
||||
SELECT ${ApiPriceFields}
|
||||
@ -411,7 +427,7 @@ class PricesRepository {
|
||||
const computeFx = (usd: number, other: number): number =>
|
||||
Math.round(Math.max(other, 0) / Math.max(usd, 1) * 100) / 100;
|
||||
|
||||
const exchangeRates: ExchangeRates = config.MEMPOOL.CURRENCY_API_KEY ?
|
||||
const exchangeRates: ExchangeRates = config.MEMPOOL.CURRENCY_API_KEY ?
|
||||
{
|
||||
USDEUR: computeFx(latestPrice.USD, latestPrice.EUR),
|
||||
USDGBP: computeFx(latestPrice.USD, latestPrice.GBP),
|
||||
@ -452,7 +468,23 @@ class PricesRepository {
|
||||
USDCHF: computeFx(latestPrice.USD, latestPrice.CHF),
|
||||
USDAUD: computeFx(latestPrice.USD, latestPrice.AUD),
|
||||
USDJPY: computeFx(latestPrice.USD, latestPrice.JPY),
|
||||
};
|
||||
|
||||
if (currency) {
|
||||
if (!latestPrice[currency]) {
|
||||
return null;
|
||||
}
|
||||
const filteredRates = rates.map((rate: any) => {
|
||||
return {
|
||||
time: rate.time,
|
||||
[currency]: rate[currency]
|
||||
};
|
||||
});
|
||||
return {
|
||||
prices: filteredRates as ApiPrice[],
|
||||
exchangeRates: exchangeRates
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
prices: rates as ApiPrice[],
|
||||
|
@ -405,7 +405,7 @@ export class ApiService {
|
||||
);
|
||||
}
|
||||
|
||||
getHistoricalPrice$(timestamp: number | undefined): Observable<Conversion> {
|
||||
getHistoricalPrice$(timestamp: number | undefined, currency?: string): Observable<Conversion> {
|
||||
if (this.stateService.isAnyTestnet()) {
|
||||
return of({
|
||||
prices: [],
|
||||
@ -416,12 +416,47 @@ export class ApiService {
|
||||
USDCHF: 0,
|
||||
USDAUD: 0,
|
||||
USDJPY: 0,
|
||||
USDBGN: 0,
|
||||
USDBRL: 0,
|
||||
USDCNY: 0,
|
||||
USDCZK: 0,
|
||||
USDDKK: 0,
|
||||
USDHKD: 0,
|
||||
USDHRK: 0,
|
||||
USDHUF: 0,
|
||||
USDIDR: 0,
|
||||
USDILS: 0,
|
||||
USDINR: 0,
|
||||
USDISK: 0,
|
||||
USDKRW: 0,
|
||||
USDMXN: 0,
|
||||
USDMYR: 0,
|
||||
USDNOK: 0,
|
||||
USDNZD: 0,
|
||||
USDPHP: 0,
|
||||
USDPLN: 0,
|
||||
USDRON: 0,
|
||||
USDRUB: 0,
|
||||
USDSEK: 0,
|
||||
USDSGD: 0,
|
||||
USDTHB: 0,
|
||||
USDTRY: 0,
|
||||
USDZAR: 0,
|
||||
}
|
||||
});
|
||||
}
|
||||
const queryParams = [];
|
||||
|
||||
if (timestamp) {
|
||||
queryParams.push(`timestamp=${timestamp}`);
|
||||
}
|
||||
|
||||
if (currency) {
|
||||
queryParams.push(`currency=${currency}`);
|
||||
}
|
||||
return this.httpClient.get<Conversion>(
|
||||
this.apiBaseUrl + this.apiBasePath + '/api/v1/historical-price' +
|
||||
(timestamp ? `?timestamp=${timestamp}` : '')
|
||||
`${this.apiBaseUrl}${this.apiBasePath}/api/v1/historical-price` +
|
||||
(queryParams.length > 0 ? `?${queryParams.join('&')}` : '')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user