Skip to content

Commit

Permalink
feat!: the class PublicClient1 extends FetchClient
Browse files Browse the repository at this point in the history
- all methods return native promises.
- `DefaultTimeout` has been removed
- export `ApiUri`
- properties `symbol` and `currency` are `readonly`
  • Loading branch information
vansergen committed Dec 24, 2020
1 parent 1818b16 commit 83d38b0
Showing 1 changed file with 101 additions and 50 deletions.
151 changes: 101 additions & 50 deletions src/public1.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
import { RPC } from "rpc-request";
import { FetchClient } from "rpc-request";

export const DefaultTimeout = 30000;
export const ApiUri = "https://api.bitfinex.com/";
export const DefaultSymbol = "BTCUSD";
export const DefaultCurrency = "USD";

export type Symb = { symbol?: string };
export interface Symb {
symbol?: string;
}

export type Currency = { currency?: string };
export interface Currency {
currency?: string;
}

export type GetFundingBook = Currency & {
export interface GetFundingBook extends Currency {
limit_bids?: number;
limit_asks?: number;
};
}

export type GetOrderBook = Symb & {
export interface GetOrderBook extends Symb {
limit_bids?: number;
limit_asks?: number;
group?: 0 | 1;
};
}

export type GetTrades = Symb & { timestamp?: number; limit_trades?: number };
export interface GetTrades extends Symb {
timestamp?: number;
limit_trades?: number;
}

export type GetLends = Currency & { timestamp?: number; limit_lends?: number };
export interface GetLends extends Currency {
timestamp?: number;
limit_lends?: number;
}

export type Ticker = {
export interface Ticker {
mid: string;
bid: string;
ask: string;
Expand All @@ -32,45 +42,51 @@ export type Ticker = {
high: string;
volume: string;
timestamp: string;
};
}

export type Stats = { period: number; volume: string }[];

export type FundingBookItem = {
export interface FundingBookItem {
rate: string;
amount: string;
period: number;
timestamp: string;
frr: "No" | "Yes";
};
}

export type FundingBook = { bids: FundingBookItem[]; asks: FundingBookItem[] };
export interface FundingBook {
bids: FundingBookItem[];
asks: FundingBookItem[];
}

export type OrderBookItem = {
export interface OrderBookItem {
price: string;
amount: string;
timestamp: string;
};
}

export type OrderBook = { bids: OrderBookItem[]; asks: OrderBookItem[] };
export interface OrderBook {
bids: OrderBookItem[];
asks: OrderBookItem[];
}

export type Trade = {
export interface Trade {
timestamp: number;
tid: number;
price: string;
amount: string;
exchange: "bitfinex";
type: "sell" | "buy" | "";
};
}

export type Lend = {
export interface Lend {
rate: string;
amount_lent: string;
amount_used: string;
timestamp: number;
};
}

export type SymbolDetail = {
export interface SymbolDetail {
pair: string;
price_precision: number;
initial_margin: string;
Expand All @@ -79,89 +95,124 @@ export type SymbolDetail = {
minimum_order_size: string;
expiration: string;
margin: boolean;
};
}

export type PublicClient1Params = {
export interface PublicClient1Params {
symbol?: string;
timeout?: number;
currency?: string;
};
}

export class PublicClient1 extends RPC {
readonly symbol: string;
readonly currency: string;
export class PublicClient1 extends FetchClient<unknown> {
public readonly symbol: string;
public readonly currency: string;

constructor({
public constructor({
symbol = DefaultSymbol,
timeout = DefaultTimeout,
currency = DefaultCurrency,
}: PublicClient1Params = {}) {
super({ timeout, baseUrl: "https://api.bitfinex.com", json: true });
super({}, { baseUrl: ApiUri, transform: "json" });
this.symbol = symbol;
this.currency = currency;
}

/**
* Get the ticker
*/
getTicker({ symbol = this.symbol }: Symb = {}): Promise<Ticker> {
return this.get({ uri: "/v1/pubticker/" + symbol });
public async getTicker({ symbol = this.symbol }: Symb = {}): Promise<Ticker> {
const ticker = (await this.get(`/v1/pubticker/${symbol}`)) as Ticker;
return ticker;
}

/**
* Various statistics about the requested pair.
*/
getStats({ symbol = this.symbol }: Symb = {}): Promise<Stats> {
return this.get({ uri: "/v1/stats/" + symbol });
public async getStats({ symbol = this.symbol }: Symb = {}): Promise<Stats> {
const stats = (await this.get(`/v1/stats/${symbol}`)) as Stats;
return stats;
}

/**
* Get the full margin funding book
*/
getFundingBook({
public async getFundingBook({
currency = this.currency,
...qs
}: GetFundingBook = {}): Promise<FundingBook> {
return this.get({ uri: "/v1/lendbook/" + currency, qs });
const url = new URL(`/v1/lendbook/${currency}`, ApiUri);
PublicClient1.addOptions(url, { ...qs });
const path = `${url.pathname}${url.search}`;
const book = (await this.get(path)) as FundingBook;
return book;
}

/**
* Get the full order book.
*/
getOrderBook({
public async getOrderBook({
symbol = this.symbol,
...qs
}: GetOrderBook = {}): Promise<OrderBook> {
return this.get({ uri: "/v1/book/" + symbol, qs });
const url = new URL(`/v1/book/${symbol}`, ApiUri);
PublicClient1.addOptions(url, { ...qs });
const path = `${url.pathname}${url.search}`;
const book = (await this.get(path)) as OrderBook;
return book;
}

/**
* Get a list of the most recent trades for the given symbol.
*/
getTrades({ symbol = this.symbol, ...qs }: GetTrades = {}): Promise<Trade[]> {
return this.get({ uri: "/v1/trades/" + symbol, qs });
public async getTrades({
symbol = this.symbol,
...qs
}: GetTrades = {}): Promise<Trade[]> {
const url = new URL(`/v1/trades/${symbol}`, ApiUri);
PublicClient1.addOptions(url, { ...qs });
const path = `${url.pathname}${url.search}`;
const trades = (await this.get(path)) as Trade[];
return trades;
}

/**
* Get a list of the most recent funding data for the given currency: total amount provided and Flash Return Rate (in % by 365 days) over time.
*/
getLends({ currency = this.currency, ...qs }: GetLends = {}): Promise<
Lend[]
> {
return this.get({ uri: "/v1/lends/" + currency, qs });
public async getLends({
currency = this.currency,
...qs
}: GetLends = {}): Promise<Lend[]> {
const url = new URL(`/v1/lends/${currency}`, ApiUri);
PublicClient1.addOptions(url, { ...qs });
const path = `${url.pathname}${url.search}`;
const lends = (await this.get(path)) as Lend[];
return lends;
}

/**
* Get the list of symbol names.
*/
getSymbols(): Promise<string[]> {
return this.get({ uri: "/v1/symbols" });
public async getSymbols(): Promise<string[]> {
const symbols = (await this.get("/v1/symbols")) as string[];
return symbols;
}

/**
* Get a list of valid symbol IDs and the pair details.
*/
getSymbolDetails(): Promise<SymbolDetail[]> {
return this.get({ uri: "/v1/symbols_details" });
public async getSymbolDetails(): Promise<SymbolDetail[]> {
const details = (await this.get("/v1/symbols_details")) as SymbolDetail[];
return details;
}

protected static addOptions(
target: URL,
data: Record<string, string | number | boolean | undefined>
): void {
for (const key in data) {
const value = data[key];
if (typeof value !== "undefined") {
target.searchParams.append(key, value.toString());
}
}
}
}

0 comments on commit 83d38b0

Please sign in to comment.