diff --git a/examples/walltime.ts b/examples/walltime.ts new file mode 100644 index 0000000..b7cddfc --- /dev/null +++ b/examples/walltime.ts @@ -0,0 +1,7 @@ +// @ts-ignore +import { walltime } from "../src/index"; + +const ex = new walltime(); + +ex.getBook("BTC", "USD").then((book) => console.log("book", book)); +ex.getTicker("BTC", "USD").then((ticker) => console.log("ticker", ticker)); diff --git a/package.json b/package.json index 4c42e0c..fa853f3 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@coinsamba/js-exchanges-connector", "description": "Collection of JavaScript implementations of cryptocurrency exchange APIs", - "version": "1.4.5", + "version": "1.4.6", "repository": "git@github.com:coinsambacom/js-exchanges-connector.git", "author": "Gustavo ", "license": "MIT", diff --git a/src/connectors/walltime.ts b/src/connectors/walltime.ts index 22a8a68..c0194ce 100644 --- a/src/connectors/walltime.ts +++ b/src/connectors/walltime.ts @@ -2,13 +2,58 @@ import { Exchange, IExchangeImplementationConstructorArgs, } from "../interfaces/exchange"; -import { ITicker } from "../types/common"; +import { IOrderbook, ITicker } from "../types/common"; +import { ConnectorError, ERROR_TYPES } from "../utils/ConnectorError"; -export class walltime extends Exchange { - public id: any; - public baseUrl: any; - public tickerUrl: any; +interface IWalltimeTicker { + version: string; + last_update: Date; + last_update_timestamp: string; + BRL_XBT: { + last_inexact: string; + last: string; + highest_bid_inexact: string; + highest_bid: string; + n_trades_24h: string; + lowest_ask_inexact: string; + lowest_ask: string; + base_volume24h_inexact: string; + base_volume24h: string; + quote_volume24h_inexact: string; + quote_volume24h: string; + base_volume_today_inexact: string; + base_volume_today: string; + quote_volume_today_inexact: string; + quote_volume_today: string; + base_volume_yesterday_inexact: string; + base_volume_yesterday: string; + quote_volume_yesterday_inexact: string; + quote_volume_yesterday: string; + }; +} + +interface IWalltimeCurrentRound { + current_round: number; + code_version: string; + order_book_pages: number; + suspended_actions: any[]; + order_book_prefix: string; + last_trades_prefix: string; + best_offer: { + "brl-xbt": string; + "xbt-brl": string; + }; +} + +type TWalltimeOrderbookOrder = [string, string]; + +interface IWalltimeOrderbook { + timestamp: number; + "xbt-brl": TWalltimeOrderbookOrder[]; + "brl-xbt": TWalltimeOrderbookOrder[]; +} +export class walltime extends Exchange { constructor(args?: IExchangeImplementationConstructorArgs) { super({ id: "walltime", @@ -17,43 +62,48 @@ export class walltime extends Exchange { opts: args?.opts, limiter: args?.limiter, }); - - this.tickerUrl = - "https://s3.amazonaws.com/data-production-walltime-info/production/dynamic/walltime-info.json"; } async getTicker(base: string, quote: string): Promise { - let res = await this.fetch(this.tickerUrl); + const { BRL_XBT: res } = await this.fetch( + `${this.baseUrl}/walltime-info.json?now=${Date.now()}`, + ); - res = res.BRL_XBT; return { exchangeId: this.id, base, quote, - last: res.last_inexact, - ask: res.lowest_ask_inexact, - bid: res.highest_bid_inexact, - vol: res.quote_volume24h_inexact, + last: Number(res.last_inexact), + ask: Number(res.lowest_ask_inexact), + bid: Number(res.highest_bid_inexact), + vol: Number(res.quote_volume24h_inexact), }; } - // eslint-disable-next-line @typescript-eslint/no-unused-vars - async getBook(base: string, quote: string) { - const currentRound = (await this.fetch(this.baseUrl + "/meta.json"))[ - "current-round" - ]; - if (!currentRound) return false; - const res = await this.fetch( - this.baseUrl + "/order-book/v8878cb_r" + currentRound + "_p0.json", + async getBook(_: string, __: string): Promise { + const now = Date.now(); + + const currentRound = await this.fetch( + `${this.baseUrl}/meta.json?now=${now}`, + ); + + if (!currentRound?.current_round) { + throw new ConnectorError( + ERROR_TYPES.API_RESPONSE_ERROR, + "walltime has returned invalid round", + ); + } + + const book = await this.fetch( + `${this.baseUrl}/${currentRound.order_book_prefix}_r${currentRound.current_round}_p0.json?now=${now}`, ); - if (!res) return false; return { - asks: res["xbt-brl"].map((o: [string, string]) => ({ + asks: book["xbt-brl"].map((o) => ({ price: eval(o[1]) / eval(o[0]), amount: eval(o[0]), })), - bids: res["xbt-brl"].map((o: [string, string]) => ({ + bids: book["brl-xbt"].map((o) => ({ price: eval(o[0]) / eval(o[1]), amount: eval(o[1]), })),