-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feat/atm-356-swap-preview
# Conflicts: # src/atomex/atomex.ts # src/atomex/atomexContext.ts # src/atomexBuilder/atomexBuilder.ts # src/atomexBuilder/controlledAtomexContext.ts
- Loading branch information
Showing
20 changed files
with
329 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export type { PriceManager, GetPriceParameters, GetAveragePriceParameters } from './priceManager'; | ||
export { MixedPriceManager } from './mixedPriceManager/index'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { MixedPriceManager } from './mixedPriceManager'; |
66 changes: 66 additions & 0 deletions
66
src/exchange/priceManager/mixedPriceManager/mixedPriceManager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import BigNumber from 'bignumber.js'; | ||
|
||
import { Currency, DataSource } from '../../../common'; | ||
import type { PriceProvider } from '../../priceProvider/index'; | ||
import type { GetAveragePriceParameters, GetPriceParameters, PriceManager } from '../priceManager'; | ||
|
||
export class MixedPriceManager implements PriceManager { | ||
constructor( | ||
private readonly providersMap: Map<string, PriceProvider> | ||
) { } | ||
|
||
async getAveragePrice({ baseCurrency, quoteCurrency, dataSource = DataSource.All }: GetAveragePriceParameters): 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, quoteCurrency, provider, dataSource = DataSource.All }: GetPriceParameters): Promise<BigNumber | undefined> { | ||
let price = await this.getPriceCore(baseCurrency, quoteCurrency, provider); | ||
if (!price) { | ||
const reversedPrice = await this.getPriceCore(quoteCurrency, baseCurrency, provider); | ||
if (reversedPrice) | ||
price = reversedPrice.pow(-1); | ||
} | ||
|
||
return price; | ||
} | ||
|
||
getAvailableProviders(): string[] { | ||
return [...this.providersMap.keys()]; | ||
} | ||
|
||
dispose(): Promise<void> { | ||
throw new Error('Method not implemented.'); | ||
} | ||
|
||
private async getPriceCore(baseCurrency: Currency['id'], quoteCurrency: Currency['id'], provider?: string): Promise<BigNumber | undefined> { | ||
const providers = this.getSelectedProviders(provider); | ||
const pricePromises = providers.map(provider => provider.getPrice(baseCurrency, quoteCurrency)); | ||
const pricePromiseResults = await Promise.allSettled(pricePromises); | ||
|
||
for (const result of pricePromiseResults) | ||
if (result.status === 'fulfilled' && result.value !== undefined) | ||
return result.value; | ||
|
||
return undefined; | ||
} | ||
|
||
private getSelectedProviders(provider?: string): PriceProvider[] { | ||
if (!provider) | ||
return [...this.providersMap.values()]; | ||
|
||
const selectedProvider = this.providersMap.get(provider); | ||
if (!selectedProvider) | ||
throw new Error(`Provider not found for key: ${provider}`); | ||
|
||
return [selectedProvider]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type BigNumber from 'bignumber.js'; | ||
|
||
import type { Currency, DataSource, Disposable } from '../../common/index'; | ||
|
||
export interface GetPriceParameters { | ||
baseCurrency: Currency['id']; | ||
quoteCurrency: Currency['id']; | ||
provider?: string; | ||
dataSource?: DataSource; | ||
} | ||
|
||
export interface GetAveragePriceParameters { | ||
baseCurrency: Currency['id']; | ||
quoteCurrency: Currency['id']; | ||
dataSource?: DataSource; | ||
} | ||
|
||
export interface PriceManager extends Disposable { | ||
getPrice(parameters: GetPriceParameters): Promise<BigNumber | undefined>; | ||
getAveragePrice(parameters: GetAveragePriceParameters): Promise<BigNumber | undefined>; | ||
getAvailableProviders(): string[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type BigNumber from 'bignumber.js'; | ||
|
||
import type { Currency } from '../../../common/index'; | ||
import type { ExchangeService } from '../../exchangeService'; | ||
import type { Quote } from '../../models/index'; | ||
import type { PriceProvider } from '../priceProvider'; | ||
|
||
export class AtomexPriceProvider implements PriceProvider { | ||
constructor( | ||
private readonly exchangeService: ExchangeService | ||
) { } | ||
|
||
async getPrice(baseCurrency: Currency['id'], quoteCurrency: Currency['id']): Promise<BigNumber | undefined> { | ||
const symbol = `${baseCurrency}/${quoteCurrency}`; | ||
const quote = (await this.exchangeService.getTopOfBook([{ from: baseCurrency, to: quoteCurrency }]))?.[0]; | ||
|
||
return quote && quote.symbol == symbol ? this.getMiddlePrice(quote) : undefined; | ||
} | ||
|
||
private getMiddlePrice(quote: Quote): BigNumber { | ||
return quote.ask.plus(quote.bid).div(2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { AtomexPriceProvider } from './atomexPriceProvider'; |
52 changes: 52 additions & 0 deletions
52
src/exchange/priceProvider/binance/binancePriceProvider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import BigNumber from 'bignumber.js'; | ||
|
||
import type { Currency } from '../../../common'; | ||
import { HttpClient } from '../../../core'; | ||
import type { PriceProvider } from '../priceProvider'; | ||
import type { BinanceErrorDto, BinanceRatesDto } from './dtos'; | ||
import { isErrorDto } from './utils'; | ||
|
||
export class BinancePriceProvider implements PriceProvider { | ||
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(BinancePriceProvider.baseUrl); | ||
} | ||
|
||
async getPrice(baseCurrency: Currency['id'], quoteCurrency: Currency['id']): Promise<BigNumber | undefined> { | ||
const symbol = `${baseCurrency}${quoteCurrency}`; | ||
const allSymbols = await this.getAllSymbols(); | ||
if (!allSymbols.has(symbol)) | ||
return undefined; | ||
|
||
const urlPath = `${BinancePriceProvider.priceUrlPath}?symbol=${symbol}`; | ||
const responseDto = await this.httpClient.request<BinanceRatesDto | BinanceErrorDto>({ urlPath }, false); | ||
|
||
return this.mapRatesDtoToPrice(responseDto); | ||
} | ||
|
||
private mapRatesDtoToPrice(dto: BinanceRatesDto | BinanceErrorDto): BigNumber | undefined { | ||
if (isErrorDto(dto)) | ||
return undefined; | ||
|
||
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 = BinancePriceProvider.priceUrlPath; | ||
const responseDto = await this.httpClient.request<BinanceRatesDto[]>({ urlPath }, false); | ||
|
||
return responseDto.map(dto => dto.symbol); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export interface BinanceRatesDto { | ||
symbol: string; | ||
price: string; | ||
} | ||
|
||
export interface BinanceErrorDto { | ||
code: number; | ||
msg: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { BinancePriceProvider } from './binancePriceProvider'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { BinanceErrorDto } from './dtos'; | ||
|
||
export const isErrorDto = (dto: unknown): dto is BinanceErrorDto => { | ||
const errorDto = dto as BinanceErrorDto; | ||
return typeof errorDto.code === 'number' && typeof errorDto.msg === 'string'; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type { PriceProvider } from './priceProvider'; | ||
export { AtomexPriceProvider } from './atomex/index'; | ||
export { BinancePriceProvider } from './binance/index'; | ||
export { KrakenPriceProvider } from './kraken/index'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
export interface KrakenRatesDto { | ||
error: string[]; | ||
result: Record<string, KrakenTickerInfo>; | ||
} | ||
|
||
export interface KrakenTickerInfo { | ||
/** | ||
* Ask | ||
*/ | ||
a: [price: string, wholeLotVolume: string, lotVolume: string]; | ||
|
||
/** | ||
* Bid | ||
*/ | ||
b: [price: string, wholeLotVolume: string, lotVolume: string]; | ||
|
||
/** | ||
* Last trade closed | ||
*/ | ||
c: [price: string, lotVolume: string]; | ||
|
||
/** | ||
* Volume | ||
*/ | ||
v: [today: string, last24Hours: string]; | ||
|
||
/** | ||
* Volume weighted average price | ||
*/ | ||
p: [today: string, last24Hours: string]; | ||
|
||
/** | ||
* Number of trades | ||
*/ | ||
t: [today: number, last24Hours: number]; | ||
|
||
/** | ||
* Low | ||
*/ | ||
l: [today: string, last24Hours: string]; | ||
|
||
/** | ||
* High | ||
*/ | ||
h: [today: string, last24Hours: string]; | ||
|
||
/** | ||
* Today's opening price | ||
*/ | ||
o: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { KrakenPriceProvider } from './krakenPriceProvider'; |
Oops, something went wrong.