Skip to content

Commit

Permalink
Implement getAveragePrice
Browse files Browse the repository at this point in the history
  • Loading branch information
maxima-net committed Aug 23, 2022
1 parent e0cdee9 commit 85c0c49
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/exchange/ratesProvider/aggregatedRatesProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import type { Currency } from '../../common';

export interface AggregatedRatesProvider {
getPrice(baseCurrency: Currency['id'], quoteCurrency: Currency['id'], provider?: string): Promise<BigNumber | undefined>;
getAveragePrice(baseCurrency: Currency['id'], quoteCurrency: Currency['id']): Promise<BigNumber | undefined>;
getAvailableProviders(): string[];
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type BigNumber from 'bignumber.js';
import BigNumber from 'bignumber.js';

import type { Currency } from '../../../common';
import type { AggregatedRatesProvider } from '../aggregatedRatesProvider';
Expand All @@ -9,6 +9,19 @@ export class MixedRatesProvider implements AggregatedRatesProvider {
private readonly providersMap: Map<string, RatesProvider>
) { }

async getAveragePrice(baseCurrency: string, quoteCurrency: string): Promise<BigNumber | undefined> {
const providers = this.getAvailableProviders();
const pricePromises = providers.map(provider => this.getPrice(baseCurrency, quoteCurrency, provider));
const pricePromiseResults = await Promise.allSettled(pricePromises);

const prices: BigNumber[] = [];
for (const result of pricePromiseResults)
if (result.status === 'fulfilled' && result.value !== undefined)
prices.push(result.value);

return prices.length ? BigNumber.sum(...prices).div(prices.length) : undefined;
}

async getPrice(baseCurrency: Currency['id'], quoteCurrency: Currency['id'], provider?: string): Promise<BigNumber | undefined> {
let price = await this.getPriceCore(baseCurrency, quoteCurrency, provider);
if (!price) {
Expand Down

0 comments on commit 85c0c49

Please sign in to comment.