From 8021e39c1aa9ce7337e6ada791bf0273264c41ae Mon Sep 17 00:00:00 2001 From: Gustavo Date: Thu, 17 Nov 2022 19:55:04 -0300 Subject: [PATCH 1/2] added types for walltime --- src/connectors/walltime.ts | 67 +++++++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 16 deletions(-) diff --git a/src/connectors/walltime.ts b/src/connectors/walltime.ts index 22a8a68..b6f74d4 100644 --- a/src/connectors/walltime.ts +++ b/src/connectors/walltime.ts @@ -4,11 +4,47 @@ import { } from "../interfaces/exchange"; import { ITicker } from "../types/common"; -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; + }; +} + +export class walltime extends Exchange { constructor(args?: IExchangeImplementationConstructorArgs) { super({ id: "walltime", @@ -17,31 +53,30 @@ 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`, + ); - 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" - ]; + const currentRound = await this.fetch( + this.baseUrl + "/meta.json", + ); + if (!currentRound) return false; const res = await this.fetch( this.baseUrl + "/order-book/v8878cb_r" + currentRound + "_p0.json", From 1e80fc8fd25ee85b99f443360986c2ee49c32ded Mon Sep 17 00:00:00 2001 From: Gustavo Date: Fri, 18 Nov 2022 12:47:21 -0300 Subject: [PATCH 2/2] fixed walltime --- examples/walltime.ts | 7 +++++++ package.json | 2 +- src/connectors/walltime.ts | 37 ++++++++++++++++++++++++++----------- 3 files changed, 34 insertions(+), 12 deletions(-) create mode 100644 examples/walltime.ts 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 b6f74d4..c0194ce 100644 --- a/src/connectors/walltime.ts +++ b/src/connectors/walltime.ts @@ -2,7 +2,8 @@ import { Exchange, IExchangeImplementationConstructorArgs, } from "../interfaces/exchange"; -import { ITicker } from "../types/common"; +import { IOrderbook, ITicker } from "../types/common"; +import { ConnectorError, ERROR_TYPES } from "../utils/ConnectorError"; interface IWalltimeTicker { version: string; @@ -44,6 +45,14 @@ interface IWalltimeCurrentRound { }; } +type TWalltimeOrderbookOrder = [string, string]; + +interface IWalltimeOrderbook { + timestamp: number; + "xbt-brl": TWalltimeOrderbookOrder[]; + "brl-xbt": TWalltimeOrderbookOrder[]; +} + export class walltime extends Exchange { constructor(args?: IExchangeImplementationConstructorArgs) { super({ @@ -57,7 +66,7 @@ export class walltime extends Exchange { async getTicker(base: string, quote: string): Promise { const { BRL_XBT: res } = await this.fetch( - `${this.baseUrl}/walltime-info.json`, + `${this.baseUrl}/walltime-info.json?now=${Date.now()}`, ); return { @@ -71,24 +80,30 @@ export class walltime extends Exchange { }; } - // eslint-disable-next-line @typescript-eslint/no-unused-vars - async getBook(base: string, quote: string) { + async getBook(_: string, __: string): Promise { + const now = Date.now(); + const currentRound = await this.fetch( - this.baseUrl + "/meta.json", + `${this.baseUrl}/meta.json?now=${now}`, ); - if (!currentRound) return false; - const res = await this.fetch( - this.baseUrl + "/order-book/v8878cb_r" + currentRound + "_p0.json", + 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]), })),