UNPKG

node-bithumb

Version:
327 lines (326 loc) 10.8 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const axios_1 = __importDefault(require("axios")); const crypto_1 = __importDefault(require("crypto")); const query_string_1 = __importDefault(require("query-string")); class ApiBithumb { constructor(apiKey, secretKey, paymentCurrency) { this.apiKey = apiKey; this.secretKey = secretKey; this.paymentCurrency = paymentCurrency; /** * Bithumb hosts */ this.hosts = { publicHost: 'https://api.bithumb.com/public', infoHost: 'https://api.bithumb.com/info', tradeHost: 'https://api.bithumb.com/trade', }; } /** * Provides the current price of the asset in the bithumb * https://apidocs.bithumb.com/docs/ticker */ async getTicker(coinCode) { const param = `${coinCode}_${this.paymentCurrency}`; const res = await this.requestPublic('ticker', param); return res; } /** * Provides current sales information for assets in the bithumb * https://apidocs.bithumb.com/docs/order_book */ async getOrderBook(coinCode) { const param = `${coinCode}_${this.paymentCurrency}`; const res = await this.requestPublic('orderbook', param); return res; } /** * Provides details of the completion of the transaction in the bithumb * https://apidocs.bithumb.com/docs/transaction_history */ async getTransactionHistory(coinCode) { const param = `${coinCode}_${this.paymentCurrency}`; const res = await this.requestPublic('transaction_history', param); return res; } /** * Provides information about the status of an asset's deposits and withdrawals in the bithumb * https://apidocs.bithumb.com/docs/assets_status */ async getAssetsStatus(orderCurrency) { const res = await this.requestPublic('assetsstatus', orderCurrency); return res; } /** * Provides Bithumb Index (BTMI, BTAI) information. * https://apidocs.bithumb.com/docs/btci */ async getBtci() { const res = await this.requestPublic('btci'); return res; } /** * provides a Candlestick data. * https://apidocs.bithumb.com/docs/candlestick */ async GetCandlestick(orderCurrency, paymentCurrency, chartIntervals) { const endpoint = `/candlestick/${orderCurrency}_${paymentCurrency}`; const res = await this.requestPublic(endpoint, chartIntervals); const candleData = res.data.map((candle) => ({ timestamp: candle[0], opening_price: candle[1], closing_price: candle[2], max_price: candle[3], min_price: candle[4], volume: candle[5], })); return candleData; } /** * Provide information on membership and coin transaction fees. * https://apidocs.bithumb.com/docs/account */ async postAccount(coinCode) { const params = { order_currency: coinCode, }; const res = await this.requestInfo('account', params); return res; } /** * Provide information on assets held by members. * This API Response could be change the name on the key. * https://apidocs.bithumb.com/docs/balance */ async postBalance(coinCode = 'BTC') { const params = { currency: coinCode, }; const res = await this.requestInfo('balance', params); const data = this.refineBalance(res, coinCode); return data; } /** * Provide the address of the member's coin deposit wallet. * https://apidocs.bithumb.com/docs/wallet_address */ async postWalletAddress(coinCode) { const params = { currency: coinCode || 'BTC', }; const res = await this.requestInfo('wallet_address', params); return res; } /** * provides members' virtual asset transaction information. * https://apidocs.bithumb.com/docs/ticker_user */ async postTickerUser(orderCurrency) { const params = { order_currency: orderCurrency, }; const res = await this.requestInfo('ticker', params); return res; } /** * Provide details of the member's purchase/sale registration waiting or transaction. * https://apidocs.bithumb.com/docs/orders */ async postOrders(params) { const param = { ...params, }; const res = await this.requestInfo('orders', param); return res; } /** * Provide details of the member's purchase/sale details. * https://apidocs.bithumb.com/docs/orders_detail */ async postOrderDetail(orderId, orderCurrency) { const param = { orderId, order_currency: orderCurrency, }; const res = await this.requestInfo('order_detail', param); return res; } /** * Provide information on the member's transaction completion history. * https://apidocs.bithumb.com/docs/transactions */ async postUserTransctions(searchGb, orderCurrency, offset, count) { const param = { searchGb, order_currency: orderCurrency, offset, count, }; const res = await this.requestInfo('user_transactions', param); return res; } /** * provides a designated price purchase/sale registration function. * https://apidocs.bithumb.com/docs/place */ async postPlace(orderCurrency, units, price, type) { const param = { order_currency: orderCurrency, units, price, type, }; const res = await this.requestTrade('place', param); return res; } /** * provides a registered purchase/sale order cancellation function. * https://apidocs.bithumb.com/docs/cancel */ async postCancel(type, orderId, orderCurrency) { const param = { type, order_id: orderId, order_currency: orderCurrency, }; const res = await this.requestTrade('cancel', param); return res; } /** * provides market price buying. * https://apidocs.bithumb.com/docs/market_buy */ async postMarketBuy(units, orderCurrency) { const param = { units, order_currency: orderCurrency, }; const res = await this.requestTrade('market_buy', param); return res; } /** * provides market price selling function. * https://apidocs.bithumb.com/docs/market_sell */ async postMarketSell(units, orderCurrency) { const param = { units, order_currency: orderCurrency, }; const res = await this.requestTrade('market_sell', param); return res; } /** * provides a virtual asset withdrawal application function. * https://apidocs.bithumb.com/docs/withdrawal_coin */ async postWithdrawalCoin(units, address, currency = 'BTC', desination) { const param = { units, address, currency, desination, }; const res = await this.requestTrade('btc_withdrawal', param); return res; } /** * provides a KRW withdrawal application function. * https://apidocs.bithumb.com/docs/withdrawal_krw */ async postWithdrawalKrw(bank, account, price) { const param = { bank, account, price, }; const res = await this.requestTrade('krw_withdrawal', param); return res; } /** * request Public API */ async requestPublic(endpoint, param) { const res = await axios_1.default({ method: 'GET', url: `${this.hosts.publicHost}/${endpoint}/${param || ''}`, }); this.checkStatus(res); return res.data; } /** * request Info API */ async requestInfo(endpoint, params = {}) { const param = Object.assign(params, { payment_currency: this.paymentCurrency, }); const headers = this.getBithumbHeaders(`/info/${endpoint}`, param); const res = await axios_1.default({ method: 'POST', url: `${this.hosts.infoHost}/${endpoint}`, data: query_string_1.default.stringify(param), headers, }); this.checkStatus(res); return res.data; } /** * request Trade API */ async requestTrade(endpoint, params = {}) { const param = Object.assign(params, { payment_currency: this.paymentCurrency, }); const headers = this.getBithumbHeaders(`/trade/${endpoint}`, param); const res = await axios_1.default({ method: 'POST', url: `${this.hosts.tradeHost}/${endpoint}`, data: query_string_1.default.stringify(param), headers, }); this.checkStatus(res); return res.data; } /** * Check the status and throw */ checkStatus(res) { if (res.data?.status !== '0000') { const errRes = res.data; throw new Error(`${JSON.stringify(errRes)} [Check the documents: https://apidocs.bithumb.com/docs/err_code]`); } } getBithumbHeaders(endpoint, parameters = {}) { const nonce = new Date().getTime(); const requestSignature = `${endpoint}${String.fromCharCode(0)}${query_string_1.default.stringify(parameters)}${String.fromCharCode(0)}${nonce}`; const hmacSignature = Buffer.from(crypto_1.default.createHmac('sha512', this.secretKey) .update(requestSignature) .digest('hex')).toString('base64'); return { 'Api-Key': this.apiKey, 'Api-Sign': hmacSignature, 'Api-Nonce': nonce, }; } refineBalance(res, coinCode) { const data = { ...res, data: { total_coin: res.data[`total_${coinCode}`], total_krw: res.data.total_krw, in_use_coin: res.data[`in_use_${coinCode}`], in_use_krw: res.data.in_use_krw, available_coin: res.data[`available_${coinCode}`], available_krw: res.data.available_krw, xcoin_last_coin: res.data[`xcoin_last_${coinCode}`], }, }; return data; } } exports.default = ApiBithumb;