UNPKG

bitget-api

Version:

Complete Node.js & JavaScript SDK for Bitget V1-V3 REST APIs & WebSockets, with TypeScript & end-to-end tests.

606 lines 16.7 kB
import BaseRestClient from './util/BaseRestClient.js'; import { REST_CLIENT_TYPE_ENUM } from './util/requestUtils.js'; /** * REST API client for all V3 endpoints */ export class RestClientV3 extends BaseRestClient { getClientType() { return REST_CLIENT_TYPE_ENUM.v3; } /** * * Custom SDK functions * */ /** * This method is used to get the latency and time sync between the client and the server. * This is not official API endpoint and is only used for internal testing purposes. * Use this method to check the latency and time sync between the client and the server. * Final values might vary slightly, but it should be within few ms difference. * If you have any suggestions or improvements to this measurement, please create an issue or pull request on GitHub. */ async fetchLatencySummary() { const clientTimeReqStart = Date.now(); const serverTime = await this.getServerTime(); const clientTimeReqEnd = Date.now(); console.log('serverTime', serverTime); const serverTimeMs = Number(serverTime.data.serverTime); const roundTripTime = clientTimeReqEnd - clientTimeReqStart; const estimatedOneWayLatency = Math.floor(roundTripTime / 2); // Adjust server time by adding estimated one-way latency const adjustedServerTime = serverTimeMs + estimatedOneWayLatency; // Calculate time difference between adjusted server time and local time const timeDifference = adjustedServerTime - clientTimeReqEnd; const result = { localTime: clientTimeReqEnd, serverTime: serverTimeMs, roundTripTime, estimatedOneWayLatency, adjustedServerTime, timeDifference, }; console.log('Time synchronization results:'); console.log(result); console.log(`Your approximate latency to exchange server: One way: ${estimatedOneWayLatency}ms. Round trip: ${roundTripTime}ms. `); if (timeDifference > 500) { console.warn(`WARNING! Time difference between server and client clock is greater than 500ms. It is currently ${timeDifference}ms. Consider adjusting your system clock to avoid unwanted clock sync errors! Visit https://github.com/tiagosiebler/awesome-crypto-examples/wiki/Timestamp-for-this-request-is-outside-of-the-recvWindow for more information`); } else { console.log(`Time difference between server and client clock is within acceptable range of 500ms. It is currently ${timeDifference}ms.`); } return result; } async fetchServerTime() { const res = await this.getServerTime(); return Number(res.data.serverTime); } /** * * Public endpoints * */ getServerTime() { return this.get('/api/v3/public/time'); } /** * * =====Market======= endpoints * */ /** * Get Instruments */ getInstruments(params) { return this.get('/api/v3/market/instruments', params); } /** * Get Tickers */ getTickers(params) { return this.get('/api/v3/market/tickers', params); } /** * Get OrderBook */ getOrderBook(params) { return this.get('/api/v3/market/orderbook', params); } /** * Get Recent Public Fills */ getFills(params) { return this.get('/api/v3/market/fills', params); } /** * Get Proof Of Reserves */ getProofOfReserves() { return this.get('/api/v3/market/proof-of-reserves'); } /** * Get Open Interest */ getOpenInterest(params) { return this.get('/api/v3/market/open-interest', params); } /** * Get Kline/Candlestick */ getCandles(params) { return this.get('/api/v3/market/candles', params); } /** * Get Kline/Candlestick History */ getHistoryCandles(params) { return this.get('/api/v3/market/history-candles', params); } /** * Get Current Funding Rate */ getCurrentFundingRate(params) { return this.get('/api/v3/market/current-fund-rate', params); } /** * Get Funding Rate History */ getHistoryFundingRate(params) { return this.get('/api/v3/market/history-fund-rate', params); } /** * Get Risk Reserve */ getRiskReserve(params) { return this.get('/api/v3/market/risk-reserve', params); } /** * Get Discount Rate */ getDiscountRate() { return this.get('/api/v3/market/discount-rate'); } /** * Get Margin Loan */ getMarginLoans(params) { return this.get('/api/v3/market/margin-loans', params); } /** * Get Position Tier */ getPositionTier(params) { return this.get('/api/v3/market/position-tier', params); } /** * Get Open Interest Limit */ getContractsOi(params) { return this.get('/api/v3/market/oi-limit', params); } /** * * =====Account======= endpoints * */ /** * Get Account Assets */ getBalances() { return this.getPrivate('/api/v3/account/assets'); } /** * Get Fund Account Assets */ getFundingAssets(params) { return this.getPrivate('/api/v3/account/funding-assets', params); } /** * Get Account Info */ getAccountSettings() { return this.getPrivate('/api/v3/account/settings'); } /** * Set Leverage */ setLeverage(params) { return this.postPrivate('/api/v3/account/set-leverage', params); } /** * Set Holding Mode */ setHoldMode(params) { return this.postPrivate('/api/v3/account/set-hold-mode', params); } /** * Get Financial Records */ getFinancialRecords(params) { return this.getPrivate('/api/v3/account/financial-records', params); } /** * Get Repayable Coins */ getRepayableCoins() { return this.getPrivate('/api/v3/account/repayable-coins'); } /** * Get Payment Coins */ getPaymentCoins() { return this.getPrivate('/api/v3/account/payment-coins'); } /** * Repay */ submitRepay(params) { return this.postPrivate('/api/v3/account/repay', params); } /** * Get Convert Records */ getConvertRecords(params) { return this.getPrivate('/api/v3/account/convert-records', params); } /** * Set up deposit account - Configure default recharge account for a certain symbol * This configuration item remains valid for a long time. That is, once a user sets a default * recharge account for a certain symbol, it will be retained permanently, and there is no need to reconfigure it. * Permission: UTA mgt. (read & write) */ setDepositAccount(params) { return this.postPrivate('/api/v3/account/deposit-account', params); } /** * Switch Deduct - Set BGB deduction */ switchDeduct(params) { return this.postPrivate('/api/v3/account/switch-deduct', params); } /** * Get Deduct Info - Get BGB deduction status */ getDeductInfo() { return this.getPrivate('/api/v3/account/deduct-info'); } /** * Get Trading Fee Rate */ getFeeRate(params) { return this.getPrivate('/api/v3/account/fee-rate', params); } /** * Switch Account - Switch to classic account mode * Only supports parent accounts. * This endpoint is only used for switching to classic account mode. * Please note that since the account switching process takes approximately 1 minute, * the successful response you receive only indicates that the request has been received, * and does not mean that the account has been successfully switched to the classic account. * Please use the query switching status interface to confirm whether the account switching is successful. */ downgradeAccountToClassic() { return this.postPrivate('/api/v3/account/switch'); } /** * Get Switch Status - Get account switching status * Only supports parent accounts. */ getUnifiedAccountSwitchStatus() { return this.getPrivate('/api/v3/account/switch-status'); } /** * * =====SubAccount======= endpoints * */ /** * Create Sub-account */ createSubAccount(params) { return this.postPrivate('/api/v3/user/create-sub', params); } /** * Freeze/Unfreeze Sub-account */ freezeSubAccount(params) { return this.postPrivate('/api/v3/user/freeze-sub', params); } /** * Get Sub-account Unified Account Assets */ getSubUnifiedAssets(params) { return this.getPrivate('/api/v3/account/sub-unified-assets', params); } /** * Get Sub-account List */ getSubAccountList(params) { return this.getPrivate('/api/v3/user/sub-list', params); } /** * Create Sub-account API Key */ createSubAccountApiKey(params) { return this.postPrivate('/api/v3/user/create-sub-api', params); } /** * Modify Sub-account API Key */ updateSubAccountApiKey(params) { return this.postPrivate('/api/v3/user/update-sub-api', params); } /** * Delete Sub-account API Key */ deleteSubAccountApiKey(params) { return this.postPrivate('/api/v3/user/delete-sub-api', params); } /** * Get Sub-account API Keys */ getSubAccountApiKeys(params) { return this.getPrivate('/api/v3/user/sub-api-list', params); } /** * * =====Transfer======= endpoints * */ /** * Get Transferable Coins */ getTransferableCoins(params) { return this.getPrivate('/api/v3/account/transferable-coins', params); } /** * Transfer */ submitTransfer(params) { return this.postPrivate('/api/v3/account/transfer', params); } /** * Main-Sub Account Transfer */ subAccountTransfer(params) { return this.postPrivate('/api/v3/account/sub-transfer', params); } /** * Get Main-Sub Transfer Records */ getSubTransferRecords(params) { return this.getPrivate('/api/v3/account/sub-transfer-record', params); } /** * * =====Deposit======= endpoints * */ /** * Get Deposit Address */ getDepositAddress(params) { return this.getPrivate('/api/v3/account/deposit-address', params); } /** * Get Sub Deposit Address */ getSubDepositAddress(params) { return this.getPrivate('/api/v3/account/sub-deposit-address', params); } /** * Get Deposit Records */ getDepositRecords(params) { return this.getPrivate('/api/v3/account/deposit-records', params); } /** * Get Sub Deposit Records */ getSubDepositRecords(params) { return this.postPrivate('/api/v3/account/sub-deposit-records', params); } /** * * =====Withdraw======= endpoints * */ /** * Withdraw - Includes on-chain withdrawals and internal transfers */ submitWithdraw(params) { return this.postPrivate('/api/v3/account/withdraw', params); } /** * Get Withdraw Records */ getWithdrawRecords(params) { return this.getPrivate('/api/v3/account/withdrawal-records', params); } /** * * =====Trade======= endpoints * */ /** * Place Order */ submitNewOrder(params) { return this.postPrivate('/api/v3/trade/place-order', params); } /** * Modify Order */ modifyOrder(params) { return this.postPrivate('/api/v3/trade/modify-order', params); } /** * Cancel Order */ cancelOrder(params) { return this.postPrivate('/api/v3/trade/cancel-order', params); } /** * Batch Order */ placeBatchOrders(params) { return this.postPrivate('/api/v3/trade/place-batch', params); } /** * Batch Modify Orders */ batchModifyOrders(params) { return this.postPrivate('/api/v3/trade/batch-modify-order', params); } /** * Batch Cancel */ cancelBatchOrders(params) { return this.postPrivate('/api/v3/trade/cancel-batch', params); } /** * Cancel All Orders */ cancelAllOrders(params) { return this.postPrivate('/api/v3/trade/cancel-symbol-order', params); } /** * Close All Positions */ closeAllPositions(params) { return this.postPrivate('/api/v3/trade/close-positions', params); } /** * Get Order Details */ getOrderInfo(params) { return this.getPrivate('/api/v3/trade/order-info', params); } /** * Get Open Orders */ getUnfilledOrders(params) { return this.getPrivate('/api/v3/trade/unfilled-orders', params); } /** * Get Order History */ getHistoryOrders(params) { return this.getPrivate('/api/v3/trade/history-orders', params); } /** * Get Fill History */ getTradeFills(params) { return this.getPrivate('/api/v3/trade/fills', params); } /** * Get Position Info */ getCurrentPosition(params) { return this.getPrivate('/api/v3/position/current-position', params); } /** * Get Positions History */ getPositionHistory(params) { return this.getPrivate('/api/v3/position/history-position', params); } /** * Get Max Open Available */ getMaxOpenAvailable(params) { return this.postPrivate('/api/v3/account/max-open-available', params); } /** * Get Position ADL Rank - Get position auto-deleveraging ranking */ getPositionAdlRank() { return this.getPrivate('/api/v3/position/adlRank'); } /** * CountDown Cancel All */ countdownCancelAll(params) { return this.postPrivate('/api/v3/trade/countdown-cancel-all', params); } /** * * =====Inst Loan======= endpoints * */ /** * Get Transferred Quantity */ getLoanTransfered(params) { return this.getPrivate('/api/v3/ins-loan/transfered', params); } /** * Get Trade Symbols */ getLoanSymbols(params) { return this.getPrivate('/api/v3/ins-loan/symbols', params); } /** * Get Risk Unit */ getLoanRiskUnit() { return this.getPrivate('/api/v3/ins-loan/risk-unit'); } /** * Get Repayment Orders */ getLoanRepaidHistory(params) { return this.getPrivate('/api/v3/ins-loan/repaid-history', params); } /** * Get Product Info */ getLoanProductInfo(params) { return this.getPrivate('/api/v3/ins-loan/product-infos', params); } /** * Get Loan Orders */ getLoanOrder(params) { return this.getPrivate('/api/v3/ins-loan/loan-order', params); } /** * Get LTV */ getLoanLTVConvert(params) { return this.getPrivate('/api/v3/ins-loan/ltv-convert', params); } /** * Get Margin Coin Info */ getLoanMarginCoinInfo(params) { return this.getPrivate('/api/v3/ins-loan/ensure-coins-convert', params); } /** * Bind/Unbind UID to Risk Unit */ bindLoanUid(params) { return this.postPrivate('/api/v3/ins-loan/bind-uid', params); } /** * * =====Strategy======= endpoints * */ /** * Place Strategy Order */ submitStrategyOrder(params) { return this.postPrivate('/api/v3/trade/place-strategy-order', params); } /** * Modify Strategy Order */ modifyStrategyOrder(params) { return this.postPrivate('/api/v3/trade/modify-strategy-order', params); } /** * Cancel Strategy Order */ cancelStrategyOrder(params) { return this.postPrivate('/api/v3/trade/cancel-strategy-order', params); } /** * Get Unfilled Strategy Orders */ getUnfilledStrategyOrders(params) { return this.getPrivate('/api/v3/trade/unfilled-strategy-orders', params); } /** * Get Strategy Order History */ getHistoryStrategyOrders(params) { return this.getPrivate('/api/v3/trade/history-strategy-orders', params); } } //# sourceMappingURL=rest-client-v3.js.map