Skip to content

Commit

Permalink
Merge pull request #5 from coinsambacom/fix/walltime
Browse files Browse the repository at this point in the history
Fix/walltime
  • Loading branch information
itxtoledo authored Nov 18, 2022
2 parents eb2d346 + 1e80fc8 commit 1142d5a
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 26 deletions.
7 changes: 7 additions & 0 deletions examples/walltime.ts
Original file line number Diff line number Diff line change
@@ -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));
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]:coinsambacom/js-exchanges-connector.git",
"author": "Gustavo <[email protected]>",
"license": "MIT",
Expand Down
100 changes: 75 additions & 25 deletions src/connectors/walltime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> extends Exchange<T> {
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<T> extends Exchange<T> {
constructor(args?: IExchangeImplementationConstructorArgs<T>) {
super({
id: "walltime",
Expand All @@ -17,43 +62,48 @@ export class walltime<T> extends Exchange<T> {
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<ITicker> {
let res = await this.fetch(this.tickerUrl);
const { BRL_XBT: res } = await this.fetch<IWalltimeTicker>(
`${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<IOrderbook> {
const now = Date.now();

const currentRound = await this.fetch<IWalltimeCurrentRound>(
`${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<IWalltimeOrderbook>(
`${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]),
})),
Expand Down

0 comments on commit 1142d5a

Please sign in to comment.