chore(): updated all examples to use v2 clients

This commit is contained in:
JJ-Cro
2025-02-27 12:20:36 +01:00
parent 577ac8f4a5
commit 503baf3107
8 changed files with 81 additions and 91 deletions

View File

@@ -86,7 +86,7 @@ Each REST API group has a dedicated REST client. To avoid confusion, here are th
| Class | Description | | Class | Description |
|:------------------------------------: |:---------------------------------------------------------------------------------------------: | |:------------------------------------: |:---------------------------------------------------------------------------------------------: |
| [RestClientV2](src/rest-client-v2.ts) | [V2 REST APIs](https://www.bitget.com/api-doc/common/intro) | | [RestClientV2](src/rest-client-v2.ts) | [V2 REST APIs](https://www.bitget.com/api-doc/common/intro) |
| [WebsocketClient](src/websocket-client-v2.ts) | Universal client for all Bitget's V2 Websockets | | [WebsocketClientV2](src/websocket-client-v2.ts) | Universal client for all Bitget's V2 Websockets |
| [~~SpotClient~~ (deprecated, use RestClientV2)](src/spot-client.ts) | [~~Spot APIs~~](https://bitgetlimited.github.io/apidoc/en/spot/#introduction) | | [~~SpotClient~~ (deprecated, use RestClientV2)](src/spot-client.ts) | [~~Spot APIs~~](https://bitgetlimited.github.io/apidoc/en/spot/#introduction) |
| [~~FuturesClient~~ (deprecated, use RestClientV2)](src/futures-client.ts) | [~~Futures APIs~~](https://bitgetlimited.github.io/apidoc/en/mix/#introduction) | | [~~FuturesClient~~ (deprecated, use RestClientV2)](src/futures-client.ts) | [~~Futures APIs~~](https://bitgetlimited.github.io/apidoc/en/mix/#introduction) |
| [~~BrokerClient~~ (deprecated, use RestClientV2)](src/broker-client.ts) | [~~Broker APIs~~](https://bitgetlimited.github.io/apidoc/en/broker/#introduction) | | [~~BrokerClient~~ (deprecated, use RestClientV2)](src/broker-client.ts) | [~~Broker APIs~~](https://bitgetlimited.github.io/apidoc/en/broker/#introduction) |
@@ -164,7 +164,7 @@ For more examples, including how to use websockets with Bitget, check the [examp
Pass a custom logger which supports the log methods `silly`, `debug`, `notice`, `info`, `warning` and `error`, or override methods from the default logger as desired. Pass a custom logger which supports the log methods `silly`, `debug`, `notice`, `info`, `warning` and `error`, or override methods from the default logger as desired.
```javascript ```javascript
const { WebsocketClient, DefaultLogger } = require('bitget-api'); const { WebsocketClientV2, DefaultLogger } = require('bitget-api');
// Disable all logging on the silly level (less console logs) // Disable all logging on the silly level (less console logs)
const customLogger = { const customLogger = {

View File

@@ -1,4 +1,4 @@
import { RestClientV2, WebsocketClient } from '../src/index'; import { RestClientV2 } from '../src/index';
// or // or
// import { RestClientV2 } from 'bitget-api'; // import { RestClientV2 } from 'bitget-api';

View File

@@ -1,4 +1,4 @@
import { RestClientV2, WebsocketClient } from '../src/index'; import { RestClientV2 } from '../src/index';
// or // or
// import { RestClientV2 } from 'bitget-api'; // import { RestClientV2 } from 'bitget-api';

View File

@@ -1,7 +1,7 @@
import { RestClientV2, SpotClient } from '../src/index'; import { RestClientV2 } from '../src/index';
// or // or
// import { SpotClient } from 'bitget-api'; // import { RestClientV2 } from 'bitget-api';
const restClient = new RestClientV2(); const restClient = new RestClientV2();

View File

@@ -1,26 +1,18 @@
import { import {
FuturesClient, FuturesPlaceOrderRequestV2,
isWsFuturesAccountSnapshotEvent, RestClientV2,
isWsFuturesPositionsSnapshotEvent, WebsocketClientV2,
NewFuturesOrder,
WebsocketClient,
} from '../src'; } from '../src';
// or // or
// import { // import { FuturesPlaceOrderRequestV2, RestClientV2, WebsocketClientV2 } from '../src';
// FuturesClient,
// isWsFuturesAccountSnapshotEvent,
// isWsFuturesPositionsSnapshotEvent,
// NewFuturesOrder,
// WebsocketClient,
// } from 'bitget-api';
// read from environmental variables // read from environmental variables
const API_KEY = process.env.API_KEY_COM; const API_KEY = process.env.API_KEY_COM;
const API_SECRET = process.env.API_SECRET_COM; const API_SECRET = process.env.API_SECRET_COM;
const API_PASS = process.env.API_PASS_COM; const API_PASS = process.env.API_PASS_COM;
const client = new FuturesClient({ const client = new RestClientV2({
apiKey: API_KEY, apiKey: API_KEY,
apiSecret: API_SECRET, apiSecret: API_SECRET,
apiPass: API_PASS, apiPass: API_PASS,
@@ -29,7 +21,7 @@ const client = new FuturesClient({
// apiPass: 'apiPassHere', // apiPass: 'apiPassHere',
}); });
const wsClient = new WebsocketClient({ const wsClient = new WebsocketClientV2({
apiKey: API_KEY, apiKey: API_KEY,
apiSecret: API_SECRET, apiSecret: API_SECRET,
apiPass: API_PASS, apiPass: API_PASS,
@@ -44,28 +36,6 @@ function promiseSleep(milliseconds) {
return new Promise((resolve) => setTimeout(resolve, milliseconds)); return new Promise((resolve) => setTimeout(resolve, milliseconds));
} }
// WARNING: for sensitive math you should be using a library such as decimal.js!
function roundDown(value, decimals) {
return Number(
Math.floor(parseFloat(value + 'e' + decimals)) + 'e-' + decimals,
);
}
/** WS event handler that uses type guards to narrow down event type */
async function handleWsUpdate(event) {
if (isWsFuturesAccountSnapshotEvent(event)) {
console.log(new Date(), 'ws update (account balance):', event);
return;
}
if (isWsFuturesPositionsSnapshotEvent(event)) {
console.log(new Date(), 'ws update (positions):', event);
return;
}
logWSEvent('update (unhandled)', event);
}
/** /**
* This is a simple script wrapped in a immediately invoked function expression (to execute the below workflow immediately). * This is a simple script wrapped in a immediately invoked function expression (to execute the below workflow immediately).
* *
@@ -77,12 +47,11 @@ async function handleWsUpdate(event) {
* - immediately send closing orders for any active futures positions * - immediately send closing orders for any active futures positions
* - check positions again * - check positions again
* *
* The corresponding UI for this is at https://www.bitget.com/en/mix/usdt/BTCUSDT_UMCBL
*/ */
(async () => { (async () => {
try { try {
// Add event listeners to log websocket events on account // Add event listeners to log websocket events on account
wsClient.on('update', (data) => handleWsUpdate(data)); wsClient.on('update', (data) => logWSEvent('update', data));
wsClient.on('open', (data) => logWSEvent('open', data)); wsClient.on('open', (data) => logWSEvent('open', data));
wsClient.on('response', (data) => logWSEvent('response', data)); wsClient.on('response', (data) => logWSEvent('response', data));
@@ -91,23 +60,34 @@ async function handleWsUpdate(event) {
wsClient.on('authenticated', (data) => logWSEvent('authenticated', data)); wsClient.on('authenticated', (data) => logWSEvent('authenticated', data));
wsClient.on('exception', (data) => logWSEvent('exception', data)); wsClient.on('exception', (data) => logWSEvent('exception', data));
// Subscribe to private account topics // futures private
wsClient.subscribeTopic('UMCBL', 'account'); // : account updates
wsClient.subscribeTopic('USDT-FUTURES', 'account');
// : position updates // : position updates
wsClient.subscribeTopic('UMCBL', 'positions'); wsClient.subscribeTopic('USDT-FUTURES', 'positions');
// : order updates // : order updates
wsClient.subscribeTopic('UMCBL', 'orders'); wsClient.subscribeTopic('USDT-FUTURES', 'orders');
// wait briefly for ws to be ready (could also use the response or authenticated events, to make sure topics are subscribed to before starting) // wait briefly for ws to be ready (could also use the response or authenticated events, to make sure topics are subscribed to before starting)
await promiseSleep(2.5 * 1000); await promiseSleep(2.5 * 1000);
const symbol = 'BTCUSDT_UMCBL'; const symbol = 'BTCUSDT';
const marginCoin = 'USDT'; const marginCoin = 'USDT';
const balanceResult = await client.getAccount(symbol, marginCoin); const balanceResult = await client.getFuturesAccountAssets({
productType: 'USDT-FUTURES',
});
const accountBalance = balanceResult.data; const accountBalance = balanceResult.data;
// const balances = allBalances.filter((bal) => Number(bal.available) != 0); // const balances = allBalances.filter((bal) => Number(bal.available) != 0);
const usdtAmount = accountBalance.available; const assetList = accountBalance.find(
(bal) => bal.marginCoin === marginCoin,
)?.assetList;
const usdtAmount = assetList?.find(
(asset) => asset.coin === 'USDT',
)?.balance;
console.log('USDT balance: ', usdtAmount); console.log('USDT balance: ', usdtAmount);
if (!usdtAmount) { if (!usdtAmount) {
@@ -115,7 +95,10 @@ async function handleWsUpdate(event) {
return; return;
} }
const symbolRulesResult = await client.getSymbols('umcbl'); const symbolRulesResult = await client.getFuturesContractConfig({
symbol,
productType: 'USDT-FUTURES',
});
const bitcoinUSDFuturesRule = symbolRulesResult.data.find( const bitcoinUSDFuturesRule = symbolRulesResult.data.find(
(row) => row.symbol === symbol, (row) => row.symbol === symbol,
); );
@@ -126,21 +109,25 @@ async function handleWsUpdate(event) {
return; return;
} }
const order: NewFuturesOrder = { const order: FuturesPlaceOrderRequestV2 = {
marginCoin, marginCoin: marginCoin,
marginMode: 'crossed',
productType: 'USDT-FUTURES',
orderType: 'market', orderType: 'market',
side: 'open_long', side: 'buy',
size: bitcoinUSDFuturesRule.minTradeNum, size: bitcoinUSDFuturesRule.minTradeNum,
symbol, symbol: symbol,
} as const; } as const;
console.log('placing order: ', order); console.log('placing order: ', order);
const result = await client.submitOrder(order); const result = await client.futuresSubmitOrder(order);
console.log('order result: ', result); console.log('order result: ', result);
const positionsResult = await client.getPositions('umcbl'); const positionsResult = await client.getFuturesPositions({
productType: 'USDT-FUTURES',
});
const positionsToClose = positionsResult.data.filter( const positionsToClose = positionsResult.data.filter(
(pos) => pos.total !== '0', (pos) => pos.total !== '0',
); );
@@ -149,10 +136,11 @@ async function handleWsUpdate(event) {
// Loop through any active positions and send a closing market order on each position // Loop through any active positions and send a closing market order on each position
for (const position of positionsToClose) { for (const position of positionsToClose) {
const closingSide = const closingSide = position.holdSide === 'long' ? 'sell' : 'buy';
position.holdSide === 'long' ? 'close_long' : 'close_short'; const closingOrder: FuturesPlaceOrderRequestV2 = {
const closingOrder: NewFuturesOrder = {
marginCoin: position.marginCoin, marginCoin: position.marginCoin,
marginMode: 'crossed',
productType: 'USDT-FUTURES',
orderType: 'market', orderType: 'market',
side: closingSide, side: closingSide,
size: position.available, size: position.available,
@@ -161,13 +149,15 @@ async function handleWsUpdate(event) {
console.log('closing position with market order: ', closingOrder); console.log('closing position with market order: ', closingOrder);
const result = await client.submitOrder(closingOrder); const result = await client.futuresSubmitOrder(closingOrder);
console.log('position closing order result: ', result); console.log('position closing order result: ', result);
} }
console.log( console.log(
'positions after closing all: ', 'positions after closing all: ',
await client.getPositions('umcbl'), await client.getFuturesPositions({
productType: 'USDT-FUTURES',
}),
); );
} catch (e) { } catch (e) {
console.error('request failed: ', e); console.error('request failed: ', e);

View File

@@ -1,14 +1,17 @@
import { SpotClient, WebsocketClient } from '../src/index'; import {
RestClientV2,
SpotOrderRequestV2,
WebsocketClientV2,
} from '../src/index';
// or // import { RestClientV2, WebsocketClient } from '../src/index';
// import { SpotClient } from 'bitget-api';
// read from environmental variables // read from environmental variables
const API_KEY = process.env.API_KEY_COM; const API_KEY = process.env.API_KEY_COM;
const API_SECRET = process.env.API_SECRET_COM; const API_SECRET = process.env.API_SECRET_COM;
const API_PASS = process.env.API_PASS_COM; const API_PASS = process.env.API_PASS_COM;
const client = new SpotClient({ const client = new RestClientV2({
apiKey: API_KEY, apiKey: API_KEY,
apiSecret: API_SECRET, apiSecret: API_SECRET,
apiPass: API_PASS, apiPass: API_PASS,
@@ -17,7 +20,7 @@ const client = new SpotClient({
// apiPass: 'apiPassHere', // apiPass: 'apiPassHere',
}); });
const wsClient = new WebsocketClient({ const wsClient = new WebsocketClientV2({
apiKey: API_KEY, apiKey: API_KEY,
apiSecret: API_SECRET, apiSecret: API_SECRET,
apiPass: API_PASS, apiPass: API_PASS,
@@ -32,13 +35,6 @@ function promiseSleep(milliseconds) {
return new Promise((resolve) => setTimeout(resolve, milliseconds)); return new Promise((resolve) => setTimeout(resolve, milliseconds));
} }
// WARNING: for sensitive math you should be using a library such as decimal.js!
function roundDown(value, decimals) {
return Number(
Math.floor(parseFloat(value + 'e' + decimals)) + 'e-' + decimals,
);
}
/** This is a simple script wrapped in a immediately invoked function expression, designed to check for any available BTC balance and immediately sell the full amount for USDT */ /** This is a simple script wrapped in a immediately invoked function expression, designed to check for any available BTC balance and immediately sell the full amount for USDT */
(async () => { (async () => {
try { try {
@@ -52,16 +48,22 @@ function roundDown(value, decimals) {
wsClient.on('exception', (data) => logWSEvent('exception', data)); wsClient.on('exception', (data) => logWSEvent('exception', data));
// Subscribe to private account topics // Subscribe to private account topics
wsClient.subscribeTopic('SPBL', 'account'); // spot private
wsClient.subscribeTopic('SPBL', 'orders'); // : account updates
wsClient.subscribeTopic('SPOT', 'account');
// : order updates (note: symbol is required)
wsClient.subscribeTopic('SPOT', 'orders', 'BTCUSDT');
// wait briefly for ws to be ready (could also use the response or authenticated events, to make sure topics are subscribed to before starting) // wait briefly for ws to be ready (could also use the response or authenticated events, to make sure topics are subscribed to before starting)
await promiseSleep(2.5 * 1000); await promiseSleep(2.5 * 1000);
const balanceResult = await client.getBalance(); const balanceResult = await client.getSpotAccountAssets();
const allBalances = balanceResult.data; const allBalances = balanceResult.data;
// const balances = allBalances.filter((bal) => Number(bal.available) != 0);
const balanceBTC = allBalances.find((bal) => bal.coinName === 'BTC'); const balanceBTC = allBalances.find(
(bal) => bal.coin === 'BTC' || bal.coin === 'btc',
);
const btcAmount = balanceBTC ? Number(balanceBTC.available) : 0; const btcAmount = balanceBTC ? Number(balanceBTC.available) : 0;
// console.log('balance: ', JSON.stringify(balances, null, 2)); // console.log('balance: ', JSON.stringify(balances, null, 2));
console.log('BTC balance result: ', balanceBTC); console.log('BTC balance result: ', balanceBTC);
@@ -72,30 +74,28 @@ function roundDown(value, decimals) {
} }
console.log(`BTC available: ${btcAmount}`); console.log(`BTC available: ${btcAmount}`);
const symbol = 'BTCUSDT_SPBL'; const symbol = 'BTCUSDT';
const symbolsResult = await client.getSymbols(); const symbolsResult = await client.getSpotSymbolInfo();
const btcRules = symbolsResult.data.find((rule) => rule.symbol === symbol); const btcRules = symbolsResult.data.find((rule) => rule.symbol === symbol);
console.log('btc trading rules: ', btcRules); console.log('btc trading rules: ', btcRules);
if (!btcRules) { if (!btcRules) {
return console.log('no rules found for trading ' + symbol); return console.log('no rules found for trading ' + symbol);
} }
const quantityScale = Number(btcRules.quantityScale); const quantity = btcRules.minTradeAmount;
// const quantityRoundedDown = btcAmount - btcAmount % 0.01
const quantity = roundDown(btcAmount, quantityScale);
const order = { const order: SpotOrderRequestV2 = {
symbol: symbol, symbol: symbol,
side: 'sell', side: 'sell',
force: 'normal',
orderType: 'market', orderType: 'market',
quantity: String(quantity), force: 'gtc',
size: quantity,
} as const; } as const;
console.log('submitting order: ', order); console.log('submitting order: ', order);
const sellResult = await client.submitOrder(order); const sellResult = await client.spotSubmitOrder(order);
console.log('sell result: ', sellResult); console.log('sell result: ', sellResult);
} catch (e) { } catch (e) {

View File

@@ -1,4 +1,4 @@
import { WebsocketClientV2, DefaultLogger } from '../src'; import { DefaultLogger, WebsocketClientV2 } from '../src';
// or // or
// import { DefaultLogger, WS_KEY_MAP, WebsocketClientV2 } from 'bitget-api'; // import { DefaultLogger, WS_KEY_MAP, WebsocketClientV2 } from 'bitget-api';

View File

@@ -1,4 +1,4 @@
import { DefaultLogger, WS_KEY_MAP, WebsocketClientV2 } from '../src'; import { DefaultLogger, WebsocketClientV2, WS_KEY_MAP } from '../src';
// or // or
// import { DefaultLogger, WS_KEY_MAP, WebsocketClientV2 } from 'bitget-api'; // import { DefaultLogger, WS_KEY_MAP, WebsocketClientV2 } from 'bitget-api';