Skip to content

Commit

Permalink
Implement Kraken rates provider
Browse files Browse the repository at this point in the history
  • Loading branch information
maxima-net committed Aug 23, 2022
1 parent 872dadb commit d9bd07c
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/exchange/ratesProvider/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type { RatesProvider } from './ratesProvider';
export { BinanceRatesProvider } from './binance/index';
export { KrakenRatesProvider } from './kraken/index';
51 changes: 51 additions & 0 deletions src/exchange/ratesProvider/kraken/dtos.ts
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;
}
1 change: 1 addition & 0 deletions src/exchange/ratesProvider/kraken/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { KrakenRatesProvider } from './krakenRatesProvider';
34 changes: 34 additions & 0 deletions src/exchange/ratesProvider/kraken/krakenRatesProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import BigNumber from 'bignumber.js';

import { HttpClient } from '../../../core';
import type { RatesProvider } from '../ratesProvider';
import type { KrakenRatesDto } from './dtos';

export class KrakenRatesProvider implements RatesProvider {
private static readonly baseUrl = 'https://api.kraken.com';

private readonly httpClient: HttpClient;

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

async getPrice(quoteCurrency: string, baseCurrency: string): Promise<BigNumber | undefined> {
const symbol = `${quoteCurrency}${baseCurrency}`;
const urlPath = `/0/public/Ticker?pair=${symbol}`;
const responseDto = await this.httpClient.request<KrakenRatesDto>({ urlPath }, false);

return this.mapRatesDtoToPrice(responseDto, symbol);
}

private mapRatesDtoToPrice(dto: KrakenRatesDto, symbol: string): BigNumber | undefined {
if (dto.error)
return undefined;

const tickerInfo = dto.result[symbol];
if (!tickerInfo)
return undefined;

return new BigNumber(tickerInfo.c[0]);
}
}
7 changes: 7 additions & 0 deletions src/exchange/ratesProvider/ratesProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type BigNumber from 'bignumber.js';

import type { Currency } from '../../common';

export interface RatesProvider {
getPrice(quoteCurrency: Currency['id'], baseCurrency: Currency['id']): Promise<BigNumber | undefined>;
}

0 comments on commit d9bd07c

Please sign in to comment.