Skip to content

Feature: BTC Markets #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Feature: BTC Markets
  • Loading branch information
itxtoledo committed Mar 14, 2025
commit cc8ea5037f85fb8b66439f1bad6bf630aa0035d9
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ FetcherHandler.setFetcher(new MyFetcher());
| bitso 🇲🇽 | ✓ | | | ✓ |
| bitstamp | ✓ | | | ✓ |
| brasilbitcoin 🇧🇷 | ✓ | ✓ | | ✓ |
| btcmarkets 🇦🇺 | ✓ | ✓ | | ✓ |
| buda 🇨🇴🇵🇪🇦🇷🇨🇱 | ✓ | | | ✓ |
| bybit 🌐 | ✓ | | ✓ | ✓ |
| cexio 🌐 | ✓ | | ✓ | ✓ |
Expand Down
139 changes: 139 additions & 0 deletions src/connectors/btcmarkets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import {
Exchange,
IExchangeImplementationConstructorArgs,
} from "../interfaces/exchange";
import {
FetcherRequisitionMethods,
IOrderbook,
IOrderbookOrder,
ITicker,
} from "../utils/DTOs";

type BtcMarketsMarketRes = {
marketId: string;
baseAssetName: string;
quoteAssetName: string;
minOrderAmount: string;
maxOrderAmount: string;
amountDecimals: string;
priceDecimals: string;
status: string;
};

type BtcMarketsTicker = {
marketId: string;
bestBid: string;
bestAsk: string;
lastPrice: string;
volume24h: string;
volumeQte24h: string;
price24h: string;
pricePct24h: string;
low24h: string;
high24h: string;
timestamp: string;
};

type BtcMarketsOrderBookRes = {
marketId: string;
snapshotId: number;
asks: BtcMarketsOrderBookEntry[];
bids: BtcMarketsOrderBookEntry[];
};

type BtcMarketsOrderBookEntry = [string, string];

export class btcmarkets<T = any> extends Exchange<T> {
constructor(args?: IExchangeImplementationConstructorArgs<T>) {
super({
id: "btcmarkets",
baseUrl: "https://api.btcmarkets.net/v3",
opts: args?.opts,
});
}

private parseTicker(ticker: BtcMarketsTicker): ITicker {
const [base, quote] = ticker.marketId.split("-");

return {
exchangeId: this.id,
base: base!,
quote: quote!,
ask: Number(ticker.bestAsk),
bid: Number(ticker.bestBid),
last: Number(ticker.lastPrice),
vol: Number(ticker.volume24h),
};
}

async getAllTickers(): Promise<ITicker[]> {
const markets = await this.fetch<BtcMarketsMarketRes[]>({
url: `${this.baseUrl}/markets`,
method: FetcherRequisitionMethods.GET,
});

const fetchTickersForMarkets = async (
marketsBatch: BtcMarketsMarketRes[],
) => {
const url = new URL(`${this.baseUrl}/markets/tickers`);
marketsBatch.forEach((market) =>
url.searchParams.append("marketId", market.marketId),
);

return this.fetch<BtcMarketsTicker[]>({
url: url.toString(),
method: FetcherRequisitionMethods.GET,
});
};

const batchSize = 10;
const batchedMarkets = Array.from(
{ length: Math.ceil(markets.length / batchSize) },
(_, i) => markets.slice(i * batchSize, i * batchSize + batchSize),
);

const results = await Promise.allSettled(
batchedMarkets.map((batch) => fetchTickersForMarkets(batch)),
);

const tickers: BtcMarketsTicker[] = results
.filter(
(result): result is PromiseFulfilledResult<BtcMarketsTicker[]> =>
result.status === "fulfilled",
)
.flatMap((result) => result.value);

return tickers.map((ticker) => this.parseTicker(ticker));
}

async getTicker(base: string, quote: string): Promise<ITicker> {
const res = await this.fetch<BtcMarketsTicker>({
url: `${this.baseUrl}/markets/${base}-${quote}/ticker`,
method: FetcherRequisitionMethods.GET,
});

return this.parseTicker(res);
}

private parseOrderbookOrder([
price,
amount,
]: BtcMarketsOrderBookEntry): IOrderbookOrder {
return {
price: Number(price),
amount: Number(amount),
};
}

async getBook(base: string, quote: string): Promise<IOrderbook> {
const res = await this.fetch<BtcMarketsOrderBookRes>({
url: `${this.baseUrl}/markets/${base}-${quote}/orderbook`,
method: FetcherRequisitionMethods.GET,
});

return {
asks: res.asks.map(this.parseOrderbookOrder),
bids: res.bids.map(this.parseOrderbookOrder),
};
}
}
1 change: 1 addition & 0 deletions src/exchanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export { bitso } from "./connectors/bitso";
export { bitstamp } from "./connectors/bitstamp";
export { brasilbitcoin_otc } from "./connectors/brasilbitcoin_otc";
export { brasilbitcoin } from "./connectors/brasilbitcoin";
export { btcmarkets } from "./connectors/btcmarkets";
export { buda } from "./connectors/buda";
export { bybit } from "./connectors/bybit";
export { cexio } from "./connectors/cexio";
Expand Down
2 changes: 1 addition & 1 deletion test/brasilbitcoin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const CONNECTOR = "brasilbitcoin",
BASE = "BTC",
QUOTE = "BRL";

describe.only(CONNECTOR, () => {
describe(CONNECTOR, () => {
let exchange: IExchange;

beforeEach(async () => {
Expand Down
43 changes: 43 additions & 0 deletions test/btcmarkets.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { IExchange } from "../src/utils/DTOs";

import { testAllTickers, testBook, testTicker } from "./utils/helpers";

const CONNECTOR = "btcmarkets",
BASE = "BTC",
QUOTE = "AUD";

describe.only(CONNECTOR, () => {
let exchange: IExchange;

beforeEach(async () => {
const imported = await import(`../src/connectors/${CONNECTOR}`);

const ExchangeClass = Object.values(imported)[0] as { new (): IExchange };

exchange = new ExchangeClass();
});

describe("getAllTickers", () => {
it("should return an array of ITicker objects", async () => {
const tickers = await exchange.getAllTickers!();

testAllTickers(tickers);
});
});

describe("getTicker", () => {
it("should return an ITicker object", async () => {
const ticker = await exchange.getTicker!(BASE, QUOTE);

testTicker(ticker);
});
});

describe("getBook", () => {
it("should return an IOrderbook object", async () => {
const book = await exchange.getBook!(BASE, QUOTE);

testBook(book);
});
});
});