Skip to content

Commit

Permalink
Merge pull request #21 from coinsambacom/feat/bitget
Browse files Browse the repository at this point in the history
Feature: added bitget
  • Loading branch information
itxtoledo authored Nov 23, 2023
2 parents 66456ba + df9e20a commit c100854
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ or

# Usage

Before using the library you need to create a class to fetch the data, you can use the code below as an exampleBefore using the library you need to create a class to fetch the data, you can use the code below as an example
Before using the library you need to create a class to fetch the data, you can use the code below as an example

```JavaScript
import {
Expand Down Expand Up @@ -175,6 +175,7 @@ binance.getAllTickers().then(tickers => console.log(tickers));
| bitblue 🇧🇷 | 1 | | | 1 |
| bitcointoyou 🇧🇷 | 1 | | | 1 |
| bitcointrade 🇧🇷 | 1 | | | 1 |
| bitget 🌐 | | 1 | | 1 |
| bitmonedero 🇦🇷 | 1 | | | 1 |
| bitnuvem 🇧🇷 | 1 | | | 1 |
| bitpreco 🇧🇷 | 1 | | 1 | 1 |
Expand Down
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": "2.1.13",
"version": "2.1.14",
"repository": "[email protected]:coinsambacom/js-exchanges-connector.git",
"author": "Gustavo <[email protected]>",
"license": "MIT",
Expand Down
116 changes: 116 additions & 0 deletions src/connectors/bitget.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import {
Exchange,
IExchangeImplementationConstructorArgs,
} from "../interfaces/exchange";
import { IOrderbook, IOrderbookOrder, ITicker } from "../utils/DTOs";

interface BitgetBaseRes<T> {
code: string;
data: T;
msg: string;
requestTime: string;
}

interface BitgetProduct {
baseCoin: string;
buyLimitPriceRatio: string;
makerFeeRate: string;
maxOrderNum: string;
maxTradeAmount: string;
minTradeAmount: string;
minTradeUSDT: string;
priceScale: string;
quantityScale: string;
quoteCoin: string;
quotePrecision: string;
sellLimitPriceRatio: string;
status: string;
symbol: string;
symbolName: string;
takerFeeRate: string;
}

interface BitgetTicker {
askSz: string;
baseVol: string;
bidSz: string;
buyOne: string;
change: string;
changeUtc: string;
close: string;
high24h: string;
low24h: string;
openUtc0: string;
quoteVol: string;
sellOne: string;
symbol: string;
ts: string;
usdtVol: string;
}

type BitgetOrderbookOrder = [string, string];

interface BitgetOrderbook {
asks: BitgetOrderbookOrder[];
bids: BitgetOrderbookOrder[];
ts: string;
}

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

async getAllTickers(): Promise<ITicker[]> {
const { data: markets } = await this.fetch<BitgetBaseRes<BitgetProduct[]>>(
`${this.baseUrl}/spot/v1/public/products`,
);
const { data: tickers } = await this.fetch<BitgetBaseRes<BitgetTicker[]>>(
`${this.baseUrl}/spot/v1/market/tickers`,
);

const res: ITicker[] = [];

for (const market of markets) {
const ticker = tickers.find(
(tickerItem) => tickerItem.symbol === market.symbolName,
);

if (ticker) {
res.push({
exchangeId: this.id,
base: market.baseCoin,
quote: market.quoteCoin,
ask: Number(ticker.sellOne),
bid: Number(ticker.buyOne),
last: Number(ticker.close),
vol: Number(ticker.baseVol),
});
}
}

return res;
}

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

async getBook(base: string, quote: string): Promise<IOrderbook> {
const res = await this.fetch<BitgetBaseRes<BitgetOrderbook>>(
`${this.baseUrl}/v2/spot/market/orderbook?symbol=${base}${quote}&type=step0&limit=100`,
);

return {
asks: res.data.asks.map(this.parseOrder),
bids: res.data.bids.map(this.parseOrder),
};
}
}
1 change: 1 addition & 0 deletions src/exchanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export { bitbay } from "./connectors/bitbay";
export { bitblue } from "./connectors/bitblue";
export { bitcointoyou } from "./connectors/bitcointoyou";
export { bitcointrade } from "./connectors/bitcointrade";
export { bitget } from "./connectors/bitget";
export { bitmonedero } from "./connectors/bitmonedero";
export { bitnuvem } from "./connectors/bitnuvem";
export { bitpreco } from "./connectors/bitpreco";
Expand Down
54 changes: 54 additions & 0 deletions test/bitget.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { expect } from "chai";
import { bitget } from "../src/connectors/bitget";
import { FetcherHandler } from "../src/utils/DTOs";
import { MyFetcher } from "./utils/MyFetcher";
import { expectPropertyTypes } from "./utils/helpers";

const QUOTE = "BRL";

describe("bitget", () => {
let exchange: bitget;

beforeEach(() => {
exchange = new bitget();
const fetcher = new MyFetcher();

FetcherHandler.setFetcher(fetcher);
});

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

expect(Array.isArray(tickers)).to.be.true;

if (tickers.length > 0) {
const ticker = tickers[0];

expectPropertyTypes(ticker, ["exchangeId", "base", "quote"], "string");
expectPropertyTypes(ticker, ["last", "ask", "bid", "vol"], "number");
}
});
});

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

expect(book).to.have.property("asks");
expect(book).to.have.property("bids");
expect(Array.isArray(book.asks)).to.be.true;
expect(Array.isArray(book.bids)).to.be.true;

if (book.asks.length > 0) {
const ask = book.asks[0];
expectPropertyTypes(ask, ["price", "amount"], "number");
}

if (book.bids.length > 0) {
const bid = book.bids[0];
expectPropertyTypes(bid, ["price", "amount"], "number");
}
});
});
});

0 comments on commit c100854

Please sign in to comment.