Skip to content
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
32 changes: 6 additions & 26 deletions modules/exchanges/crypto/bitmex/common/authentication.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { createHmac } from 'crypto';

import { HttpMethod } from '@dripjs/types';
import { stringify } from 'qs';

import { apiBasePath } from '../rest/types';
import { isNodeJs } from './helpers';

// tslint:disable-next-line:no-var-requires
const createHmac = require('create-hmac');

// tslint:disable-next-line:no-var-requires
const urljoin = require('url-join');
import { apiBasePath } from '../rest/constants';

/**
* Sign a message. hex( HMAC_SHA256(secret, verb + url + nonce + data) )
Expand Down Expand Up @@ -42,39 +37,24 @@ export function getWSAuthQuery(apiKey: string, apiSecret: string): string {
return stringify(query);
}

export function getRestAuthHeaders(
method: HttpMethod,
baseUrl: string,
endpoint: string,
apiKey: string,
apiSecret: string,
data?: any,
): AuthHeaders {
export function getRestAuthHeaders(method: HttpMethod, endpoint: string, apiKey: string, apiSecret: string, data?: any): AuthHeaders {
const query = method === HttpMethod.GET && Object.keys(data).length !== 0 ? `?${stringify(data)}` : '';
const url = urljoin(apiBasePath, endpoint, query);
const url = `${apiBasePath}${endpoint}${query}`;
// 3min timeout
const expires = Math.round(Date.now() / 1000) + 60 * 3;
const body = method === HttpMethod.GET ? '' : data;
const signature = signMessage(apiSecret, method, url, expires, body);

const authHeaders: AuthHeaders = {
return {
'Content-Type': 'application/json',
accept: 'application/json',
'api-expires': String(expires),
'api-key': apiKey,
'api-signature': signature,
};
// 当前环境为node时
if (isNodeJs()) {
// 设置Origin
authHeaders.Origin = baseUrl;
}

return authHeaders;
}

export interface AuthHeaders {
Origin?: string;
'Content-Type': string;
accept: string;
'api-expires': string;
Expand Down
35 changes: 35 additions & 0 deletions modules/exchanges/crypto/bitmex/common/get-channel-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* get channel name
*
* @param params
*
* eg1:
* input: {
* pair: 'XBTUSD',
* endPoint: RestPublicEndPoints.Trade,
* }
* output: 'trade:XBTUSD'
*
* eg2:
* input: {
* pair: ['XBTUSD', 'ETHUSD'],
* endPoint: RestPublicEndPoints.Trade,
* }
* output: ['trade:XBTUSD', 'trade:ETHUSD']
*/
export function getChannelName(params: { pair?: string | string[]; endPoint: string }): string | string[] {
let subStr = `${params.endPoint}`;
if (params.pair) {
if (params.pair instanceof Array) {
const channels: string[] = [];
for (const p of params.pair) {
channels.push(`${params.endPoint}:${p}`);
}

return channels;
}
subStr = `${params.endPoint}:${params.pair}`;
}

return subStr;
}
71 changes: 0 additions & 71 deletions modules/exchanges/crypto/bitmex/common/helpers.ts

This file was deleted.

3 changes: 2 additions & 1 deletion modules/exchanges/crypto/bitmex/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './authentication';
export * from './helpers';
export * from './get-channel-name';
export * from './transform-orderbook';
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { testnetConfig } from '@dripjs/testing';
import { Observable, Subject } from 'rxjs';
import { mergeMap, take } from 'rxjs/operators';

import { Order } from '../../rest/internal/private/order';
import { Orderbook } from '../../rest/internal/public/orderbook';
import { Order, Orderbook } from '../../rest/internal';
import { OrderResponse, OrderSide, OrderType } from '../../types';

/**
Expand All @@ -10,25 +11,36 @@ import { OrderResponse, OrderSide, OrderType } from '../../types';
* @param handler 执行的特定方法
* @return void
*/
export const receiveOrderData = async <T>(symbol: string, handler: (order: OrderResponse) => Promise<any> = async () => {}): Promise<T> => {
const order = new Order(testnetConfig);
const orderbook = new Orderbook(testnetConfig);
const obRes = await orderbook.fetch({
symbol,
depth: 10,
});
const price = +obRes.orderbook.bids[4][0];
const odRes = await order.create({
symbol,
side: OrderSide.Buy,
price,
orderQty: 25,
ordType: OrderType.Limit,
});
const result = await handler(odRes.order);
await order.cancel({
orderID: odRes.order.orderID,
});
export const receiveOrderData = <T>(symbol: string, handler: (order: OrderResponse) => Promise<T>): Observable<T> => {
const remaining$ = new Subject<number>();
const order = new Order(testnetConfig, remaining$);
const orderbook = new Orderbook(testnetConfig, remaining$);

return result;
return orderbook
.fetch({
symbol,
depth: 10,
})
.pipe(
take(1),
mergeMap((res) =>
order.create({
symbol,
side: OrderSide.Buy,
price: +res.data.bids[4][0],
orderQty: 25,
ordType: OrderType.Limit,
}),
),
mergeMap(async (oRes) => {
const res = await handler(oRes.data);
await order
.cancel({
orderID: oRes.data.orderID,
})
.toPromise();

return res;
}),
);
};
19 changes: 19 additions & 0 deletions modules/exchanges/crypto/bitmex/common/transform-orderbook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { OrderbookL2Response, OrderbookResponse } from '../types';

export function transformOrderbook(data: OrderbookResponse[]): OrderbookL2Response {
const orderbook: OrderbookL2Response = {
bids: [],
asks: [],
};
for (const item of data) {
const orderbookItem: [string, string] = [`${item.price}`, `${item.size}`];
if (item.side === 'Sell') {
orderbook.asks.push(orderbookItem);
} else {
orderbook.bids.push(orderbookItem);
}
}
orderbook.asks.reverse();

return orderbook;
}
2 changes: 2 additions & 0 deletions modules/exchanges/crypto/bitmex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@
},
"homepage": "https://github.com/drip-trader/dripjs/tree/master/modules/exchanges/crypto/bitmex#readme",
"devDependencies": {
"@dripjs/testing": "^0.1.20",
"@types/qs": "^6.5.3",
"@types/ws": "6.0.1",
"axios-mock-adapter": "^1.19.0",
"rollup": "^1.12.3",
"rollup-plugin-commonjs": "^10.0.0",
"rollup-plugin-json": "^4.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
/**
* rest请求最小剩余可用数
*/
export const MINIMUM_REMAINING_NUM = 15;

/**
* rest请求最大剩余可用数
*/
export const MAX_REMAINING_NUM = 60;

export const endpoints = {
production: 'https://www.bitmex.com/',
testnet: 'https://testnet.bitmex.com/',
production: 'https://www.bitmex.com',
testnet: 'https://testnet.bitmex.com',
};

export const apiBasePath = '/api/v1/';
Expand All @@ -21,9 +31,16 @@ export enum PublicEndPoints {

// 要求进行身份验证的连接端点集
export enum PrivateEndPoints {
/** 邀请人状态,已邀请用户及分红比率 */
/** 你委托的更新 */
Order = 'order',
/** 复数订单 */
OrderBulk = 'order/bulk',
/** 全部订单 */
OrderAll = 'order/all',
/** 你仓位的更新 */
Position = 'position',
/** 仓位杠杆的更新 */
PositionLeverage = 'position/leverage',
/** 报价 */
Quote = 'quote',
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './order';
export * from './multiple-order';
export * from './position';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './multiple-order';
Loading