Skip to content

Commit

Permalink
updated cryptomarket api
Browse files Browse the repository at this point in the history
  • Loading branch information
itxtoledo committed Jul 17, 2022
1 parent 9527e7b commit 435099d
Showing 1 changed file with 68 additions and 23 deletions.
91 changes: 68 additions & 23 deletions src/connectors/cryptomarket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,97 @@ import {
Exchange,
IExchangeImplementationConstructorArgs,
} from "../interfaces/exchange";
import { IOrderbook, ITicker } from "../types/common";
import { IOrderbook, IOrderbookOrder, ITicker } from "../types/common";

interface ICryptoMarketTicker {
ask: string;
bid: string;
last: string;
low: string;
high: string;
open: string;
volume: string;
volume_quote: string;
timestamp: string;
}

interface ICryptoMarketTickersRes {
[pair: string]: ICryptoMarketTicker;
}

type ICryptoMarketOrder = [string, string];

interface ICryptoMarketOrderbookRes {
ask: ICryptoMarketOrder[];
bid: ICryptoMarketOrder[];
}

export class cryptomarket<T> extends Exchange<T> {
constructor(args?: IExchangeImplementationConstructorArgs<T>) {
super({
id: "cryptomarket",
baseUrl: "https://api.cryptomkt.com/v1",
baseUrl: "https://api.exchange.cryptomkt.com/api/3",
opts: args?.opts,
limiter: args?.limiter,
});
}

async getAllTickersByQuote(quote: string): Promise<ITicker[]> {
const { data: res } = await this.fetch<ICryptoMarketTickersRes>(
this.baseUrl + "/public/ticker",
);

const tickers: ITicker[] = [];

for (const pair in res) {
if (pair.endsWith(quote)) {
const ticker = res[pair] as ICryptoMarketTicker;
tickers.push({
exchangeId: this.id,
base: pair.replace(quote, ""),
quote,
last: Number(ticker.last),
ask: Number(ticker.ask),
bid: Number(ticker.bid),
vol: Number(ticker.volume),
});
}
}

return tickers;
}

async getTicker(base: string, quote: string): Promise<ITicker> {
let res = await this.fetch(this.baseUrl + "/ticker?market=" + base + quote);
const ticker = await this.fetch<ICryptoMarketTicker>(
this.baseUrl + "/public/ticker/" + base + quote,
);

res = res.data[0];
return {
exchangeId: this.id,
base,
quote,
last: res.last_price,
ask: res.ask,
bid: res.bid,
vol: res.volume,
last: Number(ticker.last),
ask: Number(ticker.ask),
bid: Number(ticker.bid),
vol: Number(ticker.volume),
};
}

private parseOrder([price, amount]: ICryptoMarketOrder): IOrderbookOrder {
return {
price: Number(price),
amount: Number(amount),
};
}

async getBook(base: string, quote: string): Promise<IOrderbook> {
let res1 = await this.fetch(
this.baseUrl + "/book?market=" + base + quote + "&type=sell&page=0",
);
let res2 = await this.fetch(
this.baseUrl + "/book?market=" + base + quote + "&type=buy&page=0",
const book = await this.fetch<ICryptoMarketOrderbookRes>(
this.baseUrl + "/public/orderbook/" + base + quote,
);

res1 = res1.data;
res2 = res2.data;
return {
asks: res1.map((o: { price: string; amount: string }) => ({
price: Number(o.price),
amount: Number(o.amount),
})),
bids: res2.map((o: { price: string; amount: string }) => ({
price: Number(o.price),
amount: Number(o.amount),
})),
asks: book.ask.map(this.parseOrder),
bids: book.bid.map(this.parseOrder),
};
}
}

0 comments on commit 435099d

Please sign in to comment.