Skip to content

Commit

Permalink
Reduce bad requests count
Browse files Browse the repository at this point in the history
  • Loading branch information
maxima-net committed Aug 23, 2022
1 parent f6dbaa9 commit e0cdee9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/exchange/ratesProvider/atomex/atomexRatesProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export class AtomexRatesProvider implements RatesProvider {

async getPrice(baseCurrency: Currency['id'], quoteCurrency: Currency['id']): Promise<BigNumber | undefined> {
const symbol = `${baseCurrency}/${quoteCurrency}`;
const quote = (await this.exchangeService.getTopOfBook([symbol]))?.[0];
const quote = (await this.exchangeService.getTopOfBook([{ from: baseCurrency, to: quoteCurrency }]))?.[0];

return quote ? this.getMiddlePrice(quote) : undefined;
return quote && quote.symbol == symbol ? this.getMiddlePrice(quote) : undefined;
}

private getMiddlePrice(quote: Quote): BigNumber {
Expand Down
23 changes: 22 additions & 1 deletion src/exchange/ratesProvider/binance/binanceRatesProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,22 @@ import { isErrorDto } from './utils';

export class BinanceRatesProvider implements RatesProvider {
private static readonly baseUrl = 'https://www.binance.com';
private static readonly priceUrlPath = '/api/v3/ticker/price';

private readonly httpClient: HttpClient;
private _allSymbols: Set<string> | undefined;

constructor() {
this.httpClient = new HttpClient(BinanceRatesProvider.baseUrl);
}

async getPrice(baseCurrency: Currency['id'], quoteCurrency: Currency['id']): Promise<BigNumber | undefined> {
const urlPath = `/api/v3/ticker/price?symbol=${baseCurrency}${quoteCurrency}`;
const symbol = `${baseCurrency}${quoteCurrency}`;
const allSymbols = await this.getAllSymbols();
if (!allSymbols.has(symbol))
return undefined;

const urlPath = `${BinanceRatesProvider.priceUrlPath}?symbol=${symbol}`;
const responseDto = await this.httpClient.request<BinanceRatesDto | BinanceErrorDto>({ urlPath }, false);

return this.mapRatesDtoToPrice(responseDto);
Expand All @@ -28,4 +35,18 @@ export class BinanceRatesProvider implements RatesProvider {

return new BigNumber(dto.price);
}

private async getAllSymbols(): Promise<Set<string>> {
if (!this._allSymbols)
this._allSymbols = new Set(await this.requestAllSymbols());

return this._allSymbols;
}

private async requestAllSymbols(): Promise<string[]> {
const urlPath = BinanceRatesProvider.priceUrlPath;
const responseDto = await this.httpClient.request<BinanceRatesDto[]>({ urlPath }, false);

return responseDto.map(dto => dto.symbol);
}
}

0 comments on commit e0cdee9

Please sign in to comment.