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

354
src/util/BaseRestClient.ts Normal file
View File

@@ -0,0 +1,354 @@
import axios, { AxiosRequestConfig, AxiosResponse, Method } from 'axios';
import { API_ERROR_CODE } from '../constants/enum';
import { RestClientType } from '../types';
import { signMessage } from './node-support';
import {
RestClientOptions,
serializeParams,
getRestBaseUrl,
} from './requestUtils';
// axios.interceptors.request.use((request) => {
// console.log(
// new Date(),
// 'Starting Request',
// JSON.stringify(
// {
// headers: request.headers,
// url: request.url,
// method: request.method,
// params: request.params,
// data: request.data,
// },
// null,
// 2
// )
// );
// return request;
// });
// axios.interceptors.response.use((response) => {
// console.log(new Date(), 'Response:', JSON.stringify(response, null, 2));
// return response;
// });
interface SignedRequest<T extends object | undefined = {}> {
originalParams: T;
paramsWithSign?: T & { sign: string };
serializedParams: string;
sign: string;
queryParamsWithSign: string;
timestamp: number;
recvWindow: number;
}
interface UnsignedRequest<T extends object | undefined = {}> {
originalParams: T;
paramsWithSign: T;
}
type SignMethod = 'keyInBody' | 'usdc' | 'bitget';
export default abstract class BaseRestClient {
private options: RestClientOptions;
private baseUrl: string;
private globalRequestOptions: AxiosRequestConfig;
private apiKey: string | undefined;
private apiSecret: string | undefined;
private clientType: RestClientType;
private apiPass: string | undefined;
/** Defines the client type (affecting how requests & signatures behave) */
abstract getClientType(): RestClientType;
/**
* Create an instance of the REST client. Pass API credentials in the object in the first parameter.
* @param {RestClientOptions} [restClientOptions={}] options to configure REST API connectivity
* @param {AxiosRequestConfig} [networkOptions={}] HTTP networking options for axios
*/
constructor(
restOptions: RestClientOptions = {},
networkOptions: AxiosRequestConfig = {}
) {
this.clientType = this.getClientType();
this.options = {
recvWindow: 5000,
/** Throw errors if any request params are empty */
strictParamValidation: false,
...restOptions,
};
this.globalRequestOptions = {
// in ms == 5 minutes by default
timeout: 1000 * 60 * 5,
// custom request options based on axios specs - see: https://github.com/axios/axios#request-config
...networkOptions,
headers: {
'X-CHANNEL-CODE': '3tem',
'Content-Type': 'application/json',
locale: 'en-US',
},
};
this.baseUrl = getRestBaseUrl(false, restOptions);
this.apiKey = this.options.apiKey;
this.apiSecret = this.options.apiSecret;
this.apiPass = this.options.apiPass;
// Throw if one of the 3 values is missing, but at least one of them is set
const credentials = [this.apiKey, this.apiSecret, this.apiPass];
if (
credentials.includes(undefined) &&
credentials.some((v) => typeof v === 'string')
) {
throw new Error(
'API Key, Secret & Passphrase are ALL required to use the authenticated REST client'
);
}
}
get(endpoint: string, params?: any) {
return this._call('GET', endpoint, params, true);
}
getPrivate(endpoint: string, params?: any) {
return this._call('GET', endpoint, params, false);
}
post(endpoint: string, params?: any) {
return this._call('POST', endpoint, params, true);
}
postPrivate(endpoint: string, params?: any) {
return this._call('POST', endpoint, params, false);
}
deletePrivate(endpoint: string, params?: any) {
return this._call('DELETE', endpoint, params, false);
}
/**
* @private Make a HTTP request to a specific endpoint. Private endpoints are automatically signed.
*/
private async _call(
method: Method,
endpoint: string,
params?: any,
isPublicApi?: boolean
): Promise<any> {
// Sanity check to make sure it's only ever prefixed by one forward slash
const requestUrl = [this.baseUrl, endpoint].join(
endpoint.startsWith('/') ? '' : '/'
);
// Build a request and handle signature process
const options = await this.buildRequest(
method,
endpoint,
requestUrl,
params,
isPublicApi
);
// console.log('full request: ', options);
// Dispatch request
return axios(options)
.then((response) => {
// console.log('response: ', response.data);
// console.error('res: ', response);
// if (response.data && response.data?.code !== API_ERROR_CODE.SUCCESS) {
// throw response.data;
// }
if (response.status == 200) {
if (
typeof response.data?.code === 'string' &&
response.data?.code !== '00000'
) {
throw { response };
}
return response.data;
}
throw { response };
})
.catch((e) => this.parseException(e));
}
/**
* @private generic handler to parse request exceptions
*/
parseException(e: any): unknown {
if (this.options.parseExceptions === false) {
throw e;
}
// Something happened in setting up the request that triggered an Error
if (!e.response) {
if (!e.request) {
throw e.message;
}
// request made but no response received
throw e;
}
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
const response: AxiosResponse = e.response;
// console.error('err: ', response?.data);
throw {
code: response.status,
message: response.statusText,
body: response.data,
headers: response.headers,
requestOptions: {
...this.options,
apiPass: 'omittedFromError',
apiSecret: 'omittedFromError',
},
};
}
/**
* @private sign request and set recv window
*/
private async signRequest<T extends object | undefined = {}>(
data: T,
endpoint: string,
method: Method,
signMethod: SignMethod
): Promise<SignedRequest<T>> {
const timestamp = Date.now();
const res: SignedRequest<T> = {
originalParams: {
...data,
},
sign: '',
timestamp,
recvWindow: 0,
serializedParams: '',
queryParamsWithSign: '',
};
if (!this.apiKey || !this.apiSecret) {
return res;
}
// It's possible to override the recv window on a per rquest level
const strictParamValidation = this.options.strictParamValidation;
if (signMethod === 'bitget') {
const signRequestParams =
method === 'GET'
? serializeParams(data, strictParamValidation, '?')
: JSON.stringify(data) || '';
const paramsStr =
timestamp + method.toUpperCase() + endpoint + signRequestParams;
// console.log('sign params: ', paramsStr);
res.sign = await signMessage(paramsStr, this.apiSecret, 'base64');
res.queryParamsWithSign = signRequestParams;
return res;
}
return res;
}
private async prepareSignParams<TParams extends object | undefined>(
method: Method,
endpoint: string,
signMethod: SignMethod,
params?: TParams,
isPublicApi?: true
): Promise<UnsignedRequest<TParams>>;
private async prepareSignParams<TParams extends object | undefined>(
method: Method,
endpoint: string,
signMethod: SignMethod,
params?: TParams,
isPublicApi?: false | undefined
): Promise<SignedRequest<TParams>>;
private async prepareSignParams<TParams extends object | undefined>(
method: Method,
endpoint: string,
signMethod: SignMethod,
params?: TParams,
isPublicApi?: boolean
) {
if (isPublicApi) {
return {
originalParams: params,
paramsWithSign: params,
};
}
if (!this.apiKey || !this.apiSecret) {
throw new Error('Private endpoints require api and private keys set');
}
return this.signRequest(params, endpoint, method, signMethod);
}
/** Returns an axios request object. Handles signing process automatically if this is a private API call */
private async buildRequest(
method: Method,
endpoint: string,
url: string,
params?: any,
isPublicApi?: boolean
): Promise<AxiosRequestConfig> {
const options: AxiosRequestConfig = {
...this.globalRequestOptions,
url: url,
method: method,
};
for (const key in params) {
if (typeof params[key] === 'undefined') {
delete params[key];
}
}
if (isPublicApi || !this.apiKey || !this.apiPass) {
return {
...options,
params: params,
};
}
const signResult = await this.prepareSignParams(
method,
endpoint,
'bitget',
params,
isPublicApi
);
if (!options.headers) {
options.headers = {};
}
options.headers['ACCESS-KEY'] = this.apiKey;
options.headers['ACCESS-PASSPHRASE'] = this.apiPass;
options.headers['ACCESS-TIMESTAMP'] = signResult.timestamp;
options.headers['ACCESS-SIGN'] = signResult.sign;
options.headers['Content-Type'] = 'application/json';
if (method === 'GET') {
return {
...options,
url: options.url + signResult.queryParamsWithSign,
};
}
return {
...options,
data: params,
};
}
}

205
src/util/WsStore.ts Normal file
View File

@@ -0,0 +1,205 @@
import WebSocket from 'isomorphic-ws';
import { WsPrivateTopic, WsTopic } from '../types';
import { DefaultLogger } from './logger';
export enum WsConnectionStateEnum {
INITIAL = 0,
CONNECTING = 1,
CONNECTED = 2,
CLOSING = 3,
RECONNECTING = 4,
// ERROR = 5,
}
/** A "topic" is always a string */
export type BitgetInstType = 'SP' | 'SPBL' | 'MC' | 'UMCBL' | 'DMCBL';
// TODO: generalise so this can be made a reusable module for other clients
export interface WsTopicSubscribeEventArgs {
instType: BitgetInstType;
channel: WsTopic;
/** The symbol, e.g. "BTCUSDT" */
instId: string;
}
type WsTopicList = Set<WsTopicSubscribeEventArgs>;
interface WsStoredState {
/** The currently active websocket connection */
ws?: WebSocket;
/** The current lifecycle state of the connection (enum) */
connectionState?: WsConnectionStateEnum;
/** A timer that will send an upstream heartbeat (ping) when it expires */
activePingTimer?: ReturnType<typeof setTimeout> | undefined;
/** A timer tracking that an upstream heartbeat was sent, expecting a reply before it expires */
activePongTimer?: ReturnType<typeof setTimeout> | undefined;
/** If a reconnection is in progress, this will have the timer for the delayed reconnect */
activeReconnectTimer?: ReturnType<typeof setTimeout> | undefined;
/**
* All the topics we are expected to be subscribed to (and we automatically resubscribe to if the connection drops)
*
* A "Set" and a deep object match are used to ensure we only subscribe to a topic once (tracking a list of unique topics we're expected to be connected to)
*/
subscribedTopics: WsTopicList;
isAuthenticated?: boolean;
}
function isDeepObjectMatch(object1: any, object2: any) {
for (const key in object1) {
if (object1[key] !== object2[key]) {
return false;
}
}
return true;
}
export default class WsStore<WsKey extends string> {
private wsState: Record<string, WsStoredState> = {};
private logger: typeof DefaultLogger;
constructor(logger: typeof DefaultLogger) {
this.logger = logger || DefaultLogger;
}
/** Get WS stored state for key, optionally create if missing */
get(key: WsKey, createIfMissing?: true): WsStoredState;
get(key: WsKey, createIfMissing?: false): WsStoredState | undefined;
get(key: WsKey, createIfMissing?: boolean): WsStoredState | undefined {
if (this.wsState[key]) {
return this.wsState[key];
}
if (createIfMissing) {
return this.create(key);
}
}
getKeys(): WsKey[] {
return Object.keys(this.wsState) as WsKey[];
}
create(key: WsKey): WsStoredState | undefined {
if (this.hasExistingActiveConnection(key)) {
this.logger.warning(
'WsStore setConnection() overwriting existing open connection: ',
this.getWs(key)
);
}
this.wsState[key] = {
subscribedTopics: new Set(),
connectionState: WsConnectionStateEnum.INITIAL,
};
return this.get(key);
}
delete(key: WsKey): void {
// TODO: should we allow this at all? Perhaps block this from happening...
if (this.hasExistingActiveConnection(key)) {
const ws = this.getWs(key);
this.logger.warning(
'WsStore deleting state for connection still open: ',
ws
);
ws?.close();
}
delete this.wsState[key];
}
/* connection websocket */
hasExistingActiveConnection(key: WsKey): boolean {
return this.get(key) && this.isWsOpen(key);
}
getWs(key: WsKey): WebSocket | undefined {
return this.get(key)?.ws;
}
setWs(key: WsKey, wsConnection: WebSocket): WebSocket {
if (this.isWsOpen(key)) {
this.logger.warning(
'WsStore setConnection() overwriting existing open connection: ',
this.getWs(key)
);
}
this.get(key, true).ws = wsConnection;
return wsConnection;
}
/* connection state */
isWsOpen(key: WsKey): boolean {
const existingConnection = this.getWs(key);
return (
!!existingConnection &&
existingConnection.readyState === existingConnection.OPEN
);
}
getConnectionState(key: WsKey): WsConnectionStateEnum {
return this.get(key, true).connectionState!;
}
setConnectionState(key: WsKey, state: WsConnectionStateEnum) {
this.get(key, true).connectionState = state;
}
isConnectionState(key: WsKey, state: WsConnectionStateEnum): boolean {
return this.getConnectionState(key) === state;
}
/* subscribed topics */
getTopics(key: WsKey): WsTopicList {
return this.get(key, true).subscribedTopics;
}
getTopicsByKey(): Record<string, WsTopicList> {
const result = {};
for (const refKey in this.wsState) {
result[refKey] = this.getTopics(refKey as WsKey);
}
return result;
}
// Since topics are objects we can't rely on the set to detect duplicates
getMatchingTopic(key: WsKey, topic: WsTopicSubscribeEventArgs) {
// if (typeof topic === 'string') {
// return this.getMatchingTopic(key, { channel: topic });
// }
const allTopics = this.getTopics(key).values();
for (const storedTopic of allTopics) {
if (isDeepObjectMatch(topic, storedTopic)) {
return storedTopic;
}
}
}
addTopic(key: WsKey, topic: WsTopicSubscribeEventArgs) {
// if (typeof topic === 'string') {
// return this.addTopic(key, {
// instType: 'sp',
// channel: topic,
// instId: 'default',
// };
// }
// Check for duplicate topic. If already tracked, don't store this one
const existingTopic = this.getMatchingTopic(key, topic);
if (existingTopic) {
return this.getTopics(key);
}
return this.getTopics(key).add(topic);
}
deleteTopic(key: WsKey, topic: WsTopicSubscribeEventArgs) {
// Check if we're subscribed to a topic like this
const storedTopic = this.getMatchingTopic(key, topic);
if (storedTopic) {
this.getTopics(key).delete(storedTopic);
}
return this.getTopics(key);
}
}

View File

@@ -0,0 +1,47 @@
function _arrayBufferToBase64(buffer: ArrayBuffer) {
let binary = '';
const bytes = new Uint8Array(buffer);
const len = bytes.byteLength;
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return window.btoa(binary);
}
export async function signMessage(
message: string,
secret: string,
method: 'hex' | 'base64'
): Promise<string> {
const encoder = new TextEncoder();
const key = await window.crypto.subtle.importKey(
'raw',
encoder.encode(secret),
{ name: 'HMAC', hash: { name: 'SHA-256' } },
false,
['sign']
);
const signature = await window.crypto.subtle.sign(
'HMAC',
key,
encoder.encode(message)
);
switch (method) {
case 'hex': {
return Array.prototype.map
.call(new Uint8Array(signature), (x: any) =>
('00' + x.toString(16)).slice(-2)
)
.join('');
}
case 'base64': {
return _arrayBufferToBase64(signature);
}
default: {
((x: never) => {})(method);
throw new Error(`Unhandled sign method: ${method}`);
}
}
}

5
src/util/index.ts Normal file
View File

@@ -0,0 +1,5 @@
export * from './BaseRestClient';
export * from './requestUtils';
export * from './WsStore';
export * from './logger';
export * from './websocket-util';

22
src/util/logger.ts Normal file
View File

@@ -0,0 +1,22 @@
export type LogParams = null | any;
export const DefaultLogger = {
silly: (...params: LogParams): void => {
// console.log(params);
},
debug: (...params: LogParams): void => {
console.log(params);
},
notice: (...params: LogParams): void => {
console.log(params);
},
info: (...params: LogParams): void => {
console.info(params);
},
warning: (...params: LogParams): void => {
console.error(params);
},
error: (...params: LogParams): void => {
console.error(params);
},
};

23
src/util/node-support.ts Normal file
View File

@@ -0,0 +1,23 @@
import { createHmac } from 'crypto';
/** This is async because the browser version uses a promise (browser-support) */
export async function signMessage(
message: string,
secret: string,
method: 'hex' | 'base64'
): Promise<string> {
const hmac = createHmac('sha256', secret).update(message);
switch (method) {
case 'hex': {
return hmac.digest('hex');
}
case 'base64': {
return hmac.digest().toString('base64');
}
default: {
((x: never) => {})(method);
throw new Error(`Unhandled sign method: ${method}`);
}
}
}

92
src/util/requestUtils.ts Normal file
View File

@@ -0,0 +1,92 @@
export interface RestClientOptions {
/** Your API key */
apiKey?: string;
/** Your API secret */
apiSecret?: string;
/** The passphrase you set when creating the API Key (NOT your account password) */
apiPass?: string;
/** Set to `true` to connect to testnet. Uses the live environment by default. */
// testnet?: boolean;
/** Override the max size of the request window (in ms) */
recvWindow?: number;
/** Default: false. If true, we'll throw errors if any params are undefined */
strictParamValidation?: boolean;
/**
* Optionally override API protocol + domain
* e.g baseUrl: 'https://api.bitget.com'
**/
baseUrl?: string;
/** Default: true. whether to try and post-process request exceptions (and throw them). */
parseExceptions?: boolean;
}
export function serializeParams<T extends object | undefined = {}>(
params: T,
strict_validation = false,
prefixWith: string = ''
): string {
if (!params) {
return '';
}
const queryString = Object.keys(params)
.sort()
.map((key) => {
const value = params[key];
if (strict_validation === true && typeof value === 'undefined') {
throw new Error(
'Failed to sign API request due to undefined parameter'
);
}
return `${key}=${value}`;
})
.join('&');
// Only prefix if there's a value
return queryString ? prefixWith + queryString : queryString;
}
export function getRestBaseUrl(
useTestnet: boolean,
restInverseOptions: RestClientOptions
): string {
const exchangeBaseUrls = {
livenet: 'https://api.bitget.com',
livenet2: 'https://capi.bitget.com',
testnet: 'https://noTestnet',
};
if (restInverseOptions.baseUrl) {
return restInverseOptions.baseUrl;
}
if (useTestnet) {
return exchangeBaseUrls.testnet;
}
return exchangeBaseUrls.livenet;
}
export function isWsPong(msg: any): boolean {
// bitget
if (msg?.data === 'pong') {
return true;
}
return false;
}
/**
* Used to switch how authentication/requests work under the hood (primarily for SPOT since it's different there)
*/
export const REST_CLIENT_TYPE_ENUM = {
spot: 'spot',
futures: 'futures',
broker: 'broker',
} as const;

135
src/util/websocket-util.ts Normal file
View File

@@ -0,0 +1,135 @@
import { WsKey } from '../types';
import { signMessage } from './node-support';
import { BitgetInstType, WsTopicSubscribeEventArgs } from './WsStore';
/**
* Some exchanges have two livenet environments, some have test environments, some dont. This allows easy flexibility for different exchanges.
* Examples:
* - One livenet and one testnet: NetworkMap<'livenet' | 'testnet'>
* - One livenet, sometimes two, one testnet: NetworkMap<'livenet' | 'testnet', 'livenet2'>
* - Only one livenet, no other networks: NetworkMap<'livenet'>
*/
type NetworkMap<
TRequiredKeys extends string,
TOptionalKeys extends string | undefined = undefined
> = Record<TRequiredKeys, string> &
(TOptionalKeys extends string
? Record<TOptionalKeys, string | undefined>
: Record<TRequiredKeys, string>);
export const WS_BASE_URL_MAP: Record<
WsKey,
Record<'all', NetworkMap<'livenet'>>
> = {
mixv1: {
all: {
livenet: 'wss://ws.bitget.com/mix/v1/stream',
},
},
spotv1: {
all: {
livenet: 'wss://ws.bitget.com/spot/v1/stream',
},
},
};
/** Should be one WS key per unique URL */
export const WS_KEY_MAP = {
spotv1: 'spotv1',
mixv1: 'mixv1',
} as const;
/** Any WS keys in this list will trigger auth on connect, if credentials are available */
export const WS_AUTH_ON_CONNECT_KEYS: WsKey[] = [
WS_KEY_MAP.spotv1,
WS_KEY_MAP.mixv1,
];
/** Any WS keys in this list will ALWAYS skip the authentication process, even if credentials are available */
export const PUBLIC_WS_KEYS = [] as WsKey[];
/**
* Used to automatically determine if a sub request should be to a public or private ws (when there's two separate connections).
* Unnecessary if there's only one connection to handle both public & private topics.
*/
export const PRIVATE_TOPICS = ['account', 'orders', 'positions', 'ordersAlgo'];
export function isPrivateChannel<TChannel extends string>(
channel: TChannel
): boolean {
return PRIVATE_TOPICS.includes(channel);
}
export function getWsKeyForTopic(
subscribeEvent: WsTopicSubscribeEventArgs,
isPrivate?: boolean
): WsKey {
const instType = subscribeEvent.instType.toUpperCase() as BitgetInstType;
switch (instType) {
case 'SP':
case 'SPBL': {
return WS_KEY_MAP.spotv1;
}
case 'MC':
case 'UMCBL':
case 'DMCBL': {
return WS_KEY_MAP.mixv1;
}
default: {
throw neverGuard(
instType,
`getWsKeyForTopic(): Unhandled market ${'instrumentId'}`
);
}
}
}
/** Force subscription requests to be sent in smaller batches, if a number is returned */
export function getMaxTopicsPerSubscribeEvent(wsKey: WsKey): number | null {
switch (wsKey) {
case 'mixv1':
case 'spotv1': {
// Technically there doesn't seem to be a documented cap, but there is a size limit per request. Doesn't hurt to batch requests.
return 15;
}
default: {
throw neverGuard(wsKey, `getWsKeyForTopic(): Unhandled wsKey`);
}
}
}
export const WS_ERROR_ENUM = {
INVALID_ACCESS_KEY: 30011,
};
export function neverGuard(x: never, msg: string): Error {
return new Error(`Unhandled value exception "${x}", ${msg}`);
}
export async function getWsAuthSignature(
apiKey: string | undefined,
apiSecret: string | undefined,
apiPass: string | undefined,
recvWindow: number = 0
): Promise<{
expiresAt: number;
signature: string;
}> {
if (!apiKey || !apiSecret || !apiPass) {
throw new Error(
`Cannot auth - missing api key, secret or passcode in config`
);
}
const signatureExpiresAt = ((Date.now() + recvWindow) / 1000).toFixed(0);
const signature = await signMessage(
signatureExpiresAt + 'GET' + '/user/verify',
apiSecret,
'base64'
);
return {
expiresAt: Number(signatureExpiresAt),
signature,
};
}