initial commit, add bitget rest api and websockets connector

This commit is contained in:
Tiago Siebler
2022-10-09 23:01:08 +01:00
commit 0f75ded05c
59 changed files with 15246 additions and 0 deletions

View File

@@ -0,0 +1,161 @@
import { API_ERROR_CODE, SpotClient } from '../../src';
import { sucessEmptyResponseObject } from '../response.util';
describe('Private Spot REST API GET Endpoints', () => {
const API_KEY = process.env.API_KEY_COM;
const API_SECRET = process.env.API_SECRET_COM;
const API_PASS = process.env.API_PASS_COM;
it('should have api credentials to test with', () => {
expect(API_KEY).toStrictEqual(expect.any(String));
expect(API_SECRET).toStrictEqual(expect.any(String));
expect(API_PASS).toStrictEqual(expect.any(String));
});
const api = new SpotClient({
apiKey: API_KEY,
apiSecret: API_SECRET,
apiPass: API_PASS,
});
const symbol = 'BTCUSDT_SPBL';
const coin = 'BTC';
const timestampOneHourAgo = new Date().getTime() - 1000 * 60 * 60;
const from = timestampOneHourAgo.toFixed(0);
const to = String(Number(from) + 1000 * 60 * 30); // 30 minutes
// Seems to throw a permission error, probably because withdrawal permissions aren't set on this key (requires IP whitelist)
it.skip('getDepositAddress()', async () => {
try {
expect(await api.getDepositAddress(coin)).toStrictEqual('');
} catch (e) {
console.error('exception: ', e);
expect(e).toBeNull();
}
});
it('getWithdrawals()', async () => {
try {
expect(await api.getWithdrawals(coin, from, to)).toMatchObject({
...sucessEmptyResponseObject(),
data: expect.any(Array),
});
} catch (e) {
console.error('getWithdrawals: ', e);
expect(e).toBeNull();
}
});
it('getDeposits()', async () => {
try {
expect(await api.getDeposits(coin, from, to)).toMatchObject({
...sucessEmptyResponseObject(),
data: expect.any(Array),
});
} catch (e) {
console.error('getDeposits: ', e);
expect(e).toBeNull();
}
});
it('getApiKeyInfo()', async () => {
// No auth error == test pass
try {
expect(await api.getApiKeyInfo()).toMatchObject({
...sucessEmptyResponseObject(),
data: {
user_id: expect.any(String),
authorities: expect.any(Array),
},
});
} catch (e) {
console.error('getApiKeyInfo: ', e);
expect(e).toBeNull();
}
});
it('getBalance()', async () => {
try {
// expect(await api.getWithdrawals(coin, from, to)).toStrictEqual('');
expect(await api.getBalance()).toMatchObject({
...sucessEmptyResponseObject(),
data: expect.any(Array),
});
} catch (e) {
console.error('getBalance: ', e);
expect(e).toBeNull();
}
});
it('getTransactionHistory()', async () => {
try {
expect(await api.getTransactionHistory()).toMatchObject({
...sucessEmptyResponseObject(),
data: expect.any(Array),
});
} catch (e) {
console.error('getTransactionHistory: ', e);
expect(e).toBeNull();
}
});
it('getTransferHistory()', async () => {
try {
expect(await api.getTransferHistory()).toMatchObject({
...sucessEmptyResponseObject(),
data: expect.any(Array),
});
} catch (e) {
console.error('getTransferHistory: ', e);
expect(e).toBeNull();
}
});
it('getOrder()', async () => {
try {
expect(await api.getOrder(symbol, '12345')).toMatchObject({
...sucessEmptyResponseObject(),
data: expect.any(Array),
});
} catch (e) {
console.error('getOrder: ', e);
expect(e).toBeNull();
}
});
it('getOpenOrders()', async () => {
try {
expect(await api.getOpenOrders()).toMatchObject({
...sucessEmptyResponseObject(),
data: expect.any(Array),
});
} catch (e) {
console.error('getOpenOrders: ', e);
expect(e).toBeNull();
}
});
it('getOrderHistory()', async () => {
try {
expect(await api.getOrderHistory(symbol)).toMatchObject({
...sucessEmptyResponseObject(),
data: expect.any(Array),
});
} catch (e) {
console.error('getOrderHistory: ', e);
expect(e).toBeNull();
}
});
it('getOrderFills()', async () => {
try {
expect(await api.getOrderFills(symbol, '12345')).toMatchObject({
...sucessEmptyResponseObject(),
data: expect.any(Array),
});
} catch (e) {
console.error('getOrderFills: ', e);
expect(e).toBeNull();
}
});
});

View File

@@ -0,0 +1,152 @@
import { API_ERROR_CODE, SpotClient } from '../../src';
import { sucessEmptyResponseObject } from '../response.util';
describe('Private Spot REST API POST Endpoints', () => {
const API_KEY = process.env.API_KEY_COM;
const API_SECRET = process.env.API_SECRET_COM;
const API_PASS = process.env.API_PASS_COM;
it('should have api credentials to test with', () => {
expect(API_KEY).toStrictEqual(expect.any(String));
expect(API_SECRET).toStrictEqual(expect.any(String));
expect(API_PASS).toStrictEqual(expect.any(String));
});
const api = new SpotClient({
apiKey: API_KEY,
apiSecret: API_SECRET,
apiPass: API_PASS,
});
const symbol = 'BTCUSDT_SPBL';
const coin = 'USDT';
const timestampOneHourAgo = new Date().getTime() - 1000 * 60 * 60;
const from = timestampOneHourAgo.toFixed(0);
const to = String(Number(from) + 1000 * 60 * 30); // 30 minutes
it('transfer()', async () => {
try {
expect(
await api.transfer({
amount: '100',
coin,
fromType: 'spot',
toType: 'mix_usdt',
})
).toStrictEqual('');
// .toMatchObject({
// // not sure what this error means, probably no balance
// code: '42013',
// });
} catch (e) {
// console.error('transfer: ', e);
expect(e.body).toMatchObject({
// not sure what this error means, probably no balance
code: '42013',
});
}
});
it('withdraw()', async () => {
try {
expect(
await api.withdraw({
amount: '100',
coin,
chain: 'TRC20',
address: `123456`,
})
).toMatchObject({
...sucessEmptyResponseObject(),
data: expect.any(Array),
});
} catch (e) {
expect(e.body).toMatchObject({
code: API_ERROR_CODE.INCORRECT_PERMISSIONS,
});
}
});
it('innerWithdraw()', async () => {
try {
expect(await api.innerWithdraw(coin, '12345', '1')).toMatchObject({
...sucessEmptyResponseObject(),
data: expect.any(Array),
});
} catch (e) {
expect(e.body).toMatchObject({
code: API_ERROR_CODE.INCORRECT_PERMISSIONS,
});
}
});
it('submitOrder()', async () => {
try {
expect(
await api.submitOrder({
symbol,
side: 'buy',
orderType: 'market',
quantity: '1',
force: 'normal',
})
).toMatchObject({
...sucessEmptyResponseObject(),
data: expect.any(Array),
});
} catch (e) {
expect(e.body).toMatchObject({
code: API_ERROR_CODE.QTY_LESS_THAN_MINIMUM,
});
}
});
it('batchSubmitOrder()', async () => {
try {
expect(
await api.batchSubmitOrder(symbol, [
{
side: 'buy',
orderType: 'market',
quantity: '1',
force: 'normal',
},
])
).toMatchObject({
...sucessEmptyResponseObject(),
data: expect.any(Array),
});
} catch (e) {
expect(e.body).toMatchObject({
code: API_ERROR_CODE.QTY_LESS_THAN_MINIMUM,
});
}
});
it('cancelOrder()', async () => {
try {
expect(await api.cancelOrder(symbol, '123456')).toMatchObject({
...sucessEmptyResponseObject(),
data: expect.any(Array),
});
} catch (e) {
expect(e.body).toMatchObject({
code: API_ERROR_CODE.ORDER_NOT_FOUND,
});
}
});
it('batchCancelOrder()', async () => {
try {
expect(await api.batchCancelOrder(symbol, ['123456'])).toMatchObject({
...sucessEmptyResponseObject(),
data: expect.any(Array),
});
} catch (e) {
expect(e.body).toMatchObject({
code: API_ERROR_CODE.ORDER_NOT_FOUND,
});
}
});
});

108
test/spot/public.test.ts Normal file
View File

@@ -0,0 +1,108 @@
import { API_ERROR_CODE, SpotClient } from '../../src';
import {
notAuthenticatedError,
successResponseString,
sucessEmptyResponseObject,
} from '../response.util';
describe('Public Spot REST API Endpoints', () => {
const api = new SpotClient();
const symbol = 'BTCUSDT_SPBL';
const timestampOneHourAgo = new Date().getTime() / 1000 - 1000 * 60 * 60;
const from = Number(timestampOneHourAgo.toFixed(0));
// it('should throw for unauthenticated private calls', async () => {
// expect(() => api.getOpenOrders()).rejects.toMatchObject(
// notAuthenticatedError()
// );
// expect(() => api.getBalances()).rejects.toMatchObject(
// notAuthenticatedError()
// );
// });
/**
*
* Public
*
*/
it('getServerTime()', async () => {
// expect(await api.getServerTime()).toStrictEqual('');
expect(await api.getServerTime()).toMatchObject(successResponseString());
});
it('fetchServertime() returns number', async () => {
expect(await api.fetchServerTime()).toStrictEqual(expect.any(Number));
});
it('getCoins()', async () => {
expect(await api.getCoins()).toMatchObject({
...sucessEmptyResponseObject(),
data: expect.any(Array),
});
});
it('getSymbols()', async () => {
expect(await api.getSymbols()).toMatchObject({
...sucessEmptyResponseObject(),
data: expect.any(Array),
});
});
it('getSymbol()', async () => {
expect(await api.getSymbol(symbol)).toMatchObject({
...sucessEmptyResponseObject(),
data: {
baseCoin: expect.any(String),
},
});
});
/**
*
* Market
*
*/
it('getTicker()', async () => {
expect(await api.getTicker(symbol)).toMatchObject({
...sucessEmptyResponseObject(),
data: {
askSz: expect.any(String),
baseVol: expect.any(String),
},
});
});
it('getAllTickers()', async () => {
expect(await api.getAllTickers()).toMatchObject({
...sucessEmptyResponseObject(),
data: expect.any(Array),
});
});
it('getMarketTrades()', async () => {
expect(await api.getMarketTrades(symbol)).toMatchObject({
...sucessEmptyResponseObject(),
data: expect.any(Array),
});
});
it('getCandles()', async () => {
expect(await api.getCandles(symbol, '1min')).toMatchObject({
...sucessEmptyResponseObject(),
data: expect.any(Array),
});
});
it('getDepth()', async () => {
expect(await api.getDepth(symbol, 'step0')).toMatchObject({
...sucessEmptyResponseObject(),
data: {
bids: expect.any(Array),
asks: expect.any(Array),
},
});
});
});