-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from tiagosiebler/fixes
- Loading branch information
Showing
11 changed files
with
828 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { SpotClient } from '../src/index'; | ||
|
||
// or | ||
// import { SpotClient } from 'bybit-api'; | ||
|
||
const client = new SpotClient(); | ||
|
||
const symbol = 'BTCUSDT'; | ||
|
||
(async () => { | ||
try { | ||
// console.log('getSymbols: ', await client.getSymbols()); | ||
// console.log('getOrderBook: ', await client.getOrderBook(symbol)); | ||
console.log('getOrderBook: ', await client.getOrderBook(symbol)); | ||
} catch (e) { | ||
console.error('request failed: ', e); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { DefaultLogger } from '../src'; | ||
import { WebsocketClient, wsKeySpotPublic } from '../src/websocket-client'; | ||
|
||
// or | ||
// import { DefaultLogger, WebsocketClient } from 'bybit-api'; | ||
|
||
(async () => { | ||
const logger = { | ||
...DefaultLogger, | ||
// silly: () => {}, | ||
}; | ||
|
||
const wsClient = new WebsocketClient({ | ||
// key: key, | ||
// secret: secret, | ||
// market: 'inverse', | ||
// market: 'linear', | ||
market: 'spot', | ||
}, logger); | ||
|
||
wsClient.on('update', (data) => { | ||
console.log('raw message received ', JSON.stringify(data, null, 2)); | ||
}); | ||
|
||
wsClient.on('open', (data) => { | ||
console.log('connection opened open:', data.wsKey); | ||
|
||
if (data.wsKey === wsKeySpotPublic) { | ||
// Spot public. | ||
// wsClient.subscribePublicSpotTrades('BTCUSDT'); | ||
// wsClient.subscribePublicSpotTradingPair('BTCUSDT'); | ||
// wsClient.subscribePublicSpotV1Kline('BTCUSDT', '1m'); | ||
// wsClient.subscribePublicSpotOrderbook('BTCUSDT', 'full'); | ||
} | ||
}); | ||
wsClient.on('response', (data) => { | ||
console.log('log response: ', JSON.stringify(data, null, 2)); | ||
}); | ||
wsClient.on('reconnect', ({ wsKey }) => { | ||
console.log('ws automatically reconnecting.... ', wsKey); | ||
}); | ||
wsClient.on('reconnected', (data) => { | ||
console.log('ws has reconnected ', data?.wsKey ); | ||
}); | ||
|
||
// Inverse | ||
// wsClient.subscribe('trade'); | ||
|
||
// Linear | ||
// wsClient.subscribe('trade.BTCUSDT'); | ||
|
||
// For spot, request public connection first then send required topics on 'open' | ||
// wsClient.connectPublic(); | ||
|
||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from './inverse-client'; | ||
export * from './inverse-futures-client'; | ||
export * from './linear-client'; | ||
export * from './spot-client'; | ||
export * from './websocket-client'; | ||
export * from './logger'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import { AxiosRequestConfig } from 'axios'; | ||
import { KlineInterval } from './types/shared'; | ||
import { NewSpotOrder, OrderSide, OrderTypeSpot, SpotOrderQueryById } from './types/spot'; | ||
import BaseRestClient from './util/BaseRestClient'; | ||
import { GenericAPIResponse, getRestBaseUrl, RestClientOptions } from './util/requestUtils'; | ||
import RequestWrapper from './util/requestWrapper'; | ||
|
||
export class SpotClient extends BaseRestClient { | ||
protected requestWrapper: RequestWrapper; | ||
|
||
/** | ||
* @public Creates an instance of the Spot REST API client. | ||
* | ||
* @param {string} key - your API key | ||
* @param {string} secret - your API secret | ||
* @param {boolean} [useLivenet=false] | ||
* @param {RestClientOptions} [restClientOptions={}] options to configure REST API connectivity | ||
* @param {AxiosRequestConfig} [requestOptions={}] HTTP networking options for axios | ||
*/ | ||
constructor( | ||
key?: string | undefined, | ||
secret?: string | undefined, | ||
useLivenet: boolean = false, | ||
restClientOptions: RestClientOptions = {}, | ||
requestOptions: AxiosRequestConfig = {} | ||
) { | ||
super(key, secret, getRestBaseUrl(useLivenet, restClientOptions), restClientOptions, requestOptions); | ||
|
||
// this.requestWrapper = new RequestWrapper( | ||
// key, | ||
// secret, | ||
// getRestBaseUrl(useLivenet, restClientOptions), | ||
// restClientOptions, | ||
// requestOptions | ||
// ); | ||
return this; | ||
} | ||
|
||
async getServerTime(urlKeyOverride?: string): Promise<number> { | ||
const result = await this.get('/spot/v1/time'); | ||
return result.serverTime; | ||
} | ||
|
||
/** | ||
* | ||
* Market Data Endpoints | ||
* | ||
**/ | ||
|
||
getSymbols() { | ||
return this.get('/spot/v1/symbols'); | ||
} | ||
|
||
getOrderBook(symbol: string, limit?: number) { | ||
return this.get('/spot/quote/v1/depth', { | ||
symbol, limit | ||
}); | ||
} | ||
|
||
getMergedOrderBook(symbol: string, scale?: number, limit?: number) { | ||
return this.get('/spot/quote/v1/depth/merged', { | ||
symbol, | ||
scale, | ||
limit, | ||
}); | ||
} | ||
|
||
getTrades(symbol: string, limit?: number) { | ||
return this.get('/spot/v1/trades', { | ||
symbol, | ||
limit, | ||
}); | ||
} | ||
|
||
getCandles(symbol: string, interval: KlineInterval, limit?: number, startTime?: number, endTime?: number) { | ||
return this.get('/spot/v1/trades', { | ||
symbol, | ||
interval, | ||
limit, | ||
startTime, | ||
endTime, | ||
}); | ||
} | ||
|
||
get24hrTicker(symbol?: string) { | ||
return this.get('/spot/quote/v1/ticker/24hr', { symbol }); | ||
} | ||
|
||
getLastTradedPrice(symbol?: string) { | ||
return this.get('/spot/quote/v1/ticker/price', { symbol }); | ||
} | ||
|
||
getBestBidAskPrice(symbol?: string) { | ||
return this.get('/spot/quote/v1/ticker/book_ticker', { symbol }); | ||
} | ||
|
||
/** | ||
* Account Data Endpoints | ||
*/ | ||
|
||
submitOrder(params: NewSpotOrder) { | ||
return this.postPrivate('/spot/v1/order', params); | ||
} | ||
|
||
getOrder(params: SpotOrderQueryById) { | ||
return this.getPrivate('/spot/v1/order', params); | ||
} | ||
|
||
cancelOrder(params: SpotOrderQueryById) { | ||
return this.deletePrivate('/spot/v1/order', params); | ||
} | ||
|
||
cancelOrderBatch(params: { | ||
symbol: string; | ||
side?: OrderSide; | ||
orderTypes: OrderTypeSpot[] | ||
}) { | ||
const orderTypes = params.orderTypes ? params.orderTypes.join(',') : undefined; | ||
return this.deletePrivate('/spot/order/batch-cancel', { | ||
...params, | ||
orderTypes, | ||
}); | ||
} | ||
|
||
getOpenOrders(symbol?: string, orderId?: string, limit?: number) { | ||
return this.getPrivate('/spot/v1/open-orders', { | ||
symbol, | ||
orderId, | ||
limit, | ||
}); | ||
} | ||
|
||
getPastOrders(symbol?: string, orderId?: string, limit?: number) { | ||
return this.getPrivate('/spot/v1/history-orders', { | ||
symbol, | ||
orderId, | ||
limit, | ||
}); | ||
} | ||
|
||
getMyTrades(symbol?: string, limit?: number, fromId?: number, toId?: number) { | ||
return this.getPrivate('/spot/v1/myTrades', { | ||
symbol, | ||
limit, | ||
fromId, | ||
toId, | ||
}); | ||
} | ||
|
||
/** | ||
* Wallet Data Endpoints | ||
*/ | ||
|
||
getBalances() { | ||
return this.getPrivate('/spot/v1/account'); | ||
} | ||
} |
Oops, something went wrong.