UNPKG

bfx-api-node-rest

Version:
1,358 lines (1,209 loc) 69.1 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>rest2.js - Documentation</title> <script src="scripts/prettify/prettify.js"></script> <script src="scripts/prettify/lang-css.js"></script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc.css"> <script src="scripts/nav.js" defer></script> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <input type="checkbox" id="nav-trigger" class="nav-trigger" /> <label for="nav-trigger" class="navicon-button x"> <div class="navicon"></div> </label> <label for="nav-trigger" class="overlay"></label> <nav > <input type="text" id="nav-search" placeholder="Search" /> <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="RESTv1.html">RESTv1</a><ul class='methods'><li data-type='method'><a href="RESTv1.html#account_infos">account_infos</a></li><li data-type='method'><a href="RESTv1.html#active_credits">active_credits</a></li><li data-type='method'><a href="RESTv1.html#active_offers">active_offers</a></li><li data-type='method'><a href="RESTv1.html#active_orders">active_orders</a></li><li data-type='method'><a href="RESTv1.html#active_positions">active_positions</a></li><li data-type='method'><a href="RESTv1.html#balance_history">balance_history</a></li><li data-type='method'><a href="RESTv1.html#cancel_all_orders">cancel_all_orders</a></li><li data-type='method'><a href="RESTv1.html#cancel_multiple_orders">cancel_multiple_orders</a></li><li data-type='method'><a href="RESTv1.html#cancel_offer">cancel_offer</a></li><li data-type='method'><a href="RESTv1.html#cancel_order">cancel_order</a></li><li data-type='method'><a href="RESTv1.html#claim_position">claim_position</a></li><li data-type='method'><a href="RESTv1.html#close_swap">close_swap</a></li><li data-type='method'><a href="RESTv1.html#fundingbook">fundingbook</a></li><li data-type='method'><a href="RESTv1.html#get_symbols">get_symbols</a></li><li data-type='method'><a href="RESTv1.html#lends">lends</a></li><li data-type='method'><a href="RESTv1.html#margin_infos">margin_infos</a></li><li data-type='method'><a href="RESTv1.html#movements">movements</a></li><li data-type='method'><a href="RESTv1.html#multiple_new_orders">multiple_new_orders</a></li><li data-type='method'><a href="RESTv1.html#new_deposit">new_deposit</a></li><li data-type='method'><a href="RESTv1.html#new_offer">new_offer</a></li><li data-type='method'><a href="RESTv1.html#new_order">new_order</a></li><li data-type='method'><a href="RESTv1.html#offer_status">offer_status</a></li><li data-type='method'><a href="RESTv1.html#order_status">order_status</a></li><li data-type='method'><a href="RESTv1.html#orderbook">orderbook</a></li><li data-type='method'><a href="RESTv1.html#orders_history">orders_history</a></li><li data-type='method'><a href="RESTv1.html#past_trades">past_trades</a></li><li data-type='method'><a href="RESTv1.html#replace_order">replace_order</a></li><li data-type='method'><a href="RESTv1.html#stats">stats</a></li><li data-type='method'><a href="RESTv1.html#symbols_details">symbols_details</a></li><li data-type='method'><a href="RESTv1.html#taken_swaps">taken_swaps</a></li><li data-type='method'><a href="RESTv1.html#ticker">ticker</a></li><li data-type='method'><a href="RESTv1.html#today">today</a></li><li data-type='method'><a href="RESTv1.html#total_taken_swaps">total_taken_swaps</a></li><li data-type='method'><a href="RESTv1.html#trades">trades</a></li><li data-type='method'><a href="RESTv1.html#transfer">transfer</a></li><li data-type='method'><a href="RESTv1.html#wallet_balances">wallet_balances</a></li><li data-type='method'><a href="RESTv1.html#withdraw">withdraw</a></li></ul></li><li><a href="RESTv2.html">RESTv2</a><ul class='methods'><li data-type='method'><a href="RESTv2.html#accountFees">accountFees</a></li><li data-type='method'><a href="RESTv2.html#accountSummary">accountSummary</a></li><li data-type='method'><a href="RESTv2.html#accountTrades">accountTrades</a></li><li data-type='method'><a href="RESTv2.html#activeOrders">activeOrders</a></li><li data-type='method'><a href="RESTv2.html#activeOrdersWithIds">activeOrdersWithIds</a></li><li data-type='method'><a href="RESTv2.html#addPulse">addPulse</a></li><li data-type='method'><a href="RESTv2.html#addPulseComment">addPulseComment</a></li><li data-type='method'><a href="RESTv2.html#alertDelete">alertDelete</a></li><li data-type='method'><a href="RESTv2.html#alertList">alertList</a></li><li data-type='method'><a href="RESTv2.html#alertSet">alertSet</a></li><li data-type='method'><a href="RESTv2.html#calcAvailableBalance">calcAvailableBalance</a></li><li data-type='method'><a href="RESTv2.html#cancelAllFundingOffers">cancelAllFundingOffers</a></li><li data-type='method'><a href="RESTv2.html#cancelFundingOffer">cancelFundingOffer</a></li><li data-type='method'><a href="RESTv2.html#cancelOrder">cancelOrder</a></li><li data-type='method'><a href="RESTv2.html#cancelOrderMulti">cancelOrderMulti</a></li><li data-type='method'><a href="RESTv2.html#cancelOrders">cancelOrders</a></li><li data-type='method'><a href="RESTv2.html#cancelOrderWithCid">cancelOrderWithCid</a></li><li data-type='method'><a href="RESTv2.html#candles">candles</a></li><li data-type='method'><a href="RESTv2.html#changeLogs">changeLogs</a></li><li data-type='method'><a href="RESTv2.html#claimPosition">claimPosition</a></li><li data-type='method'><a href="RESTv2.html#closeFunding">closeFunding</a></li><li data-type='method'><a href="RESTv2.html#closePosition">closePosition</a></li><li data-type='method'><a href="RESTv2.html#conf">conf</a></li><li data-type='method'><a href="RESTv2.html#currencies">currencies</a></li><li data-type='method'><a href="RESTv2.html#deletePulse">deletePulse</a></li><li data-type='method'><a href="RESTv2.html#deleteSettings">deleteSettings</a></li><li data-type='method'><a href="RESTv2.html#derivsPositionCollateralSet">derivsPositionCollateralSet</a></li><li data-type='method'><a href="RESTv2.html#exchangeRate">exchangeRate</a></li><li data-type='method'><a href="RESTv2.html#fetchPulseComments">fetchPulseComments</a></li><li data-type='method'><a href="RESTv2.html#fundingCreditHistory">fundingCreditHistory</a></li><li data-type='method'><a href="RESTv2.html#fundingCredits">fundingCredits</a></li><li data-type='method'><a href="RESTv2.html#fundingInfo">fundingInfo</a></li><li data-type='method'><a href="RESTv2.html#fundingLoanHistory">fundingLoanHistory</a></li><li data-type='method'><a href="RESTv2.html#fundingLoans">fundingLoans</a></li><li data-type='method'><a href="RESTv2.html#fundingOfferHistory">fundingOfferHistory</a></li><li data-type='method'><a href="RESTv2.html#fundingOffers">fundingOffers</a></li><li data-type='method'><a href="RESTv2.html#fundingTrades">fundingTrades</a></li><li data-type='method'><a href="RESTv2.html#futures">futures</a></li><li data-type='method'><a href="RESTv2.html#generateInvoice">generateInvoice</a></li><li data-type='method'><a href="RESTv2.html#generateToken">generateToken</a></li><li data-type='method'><a href="RESTv2.html#getCoreSettings">getCoreSettings</a></li><li data-type='method'><a href="RESTv2.html#getDepositAddress">getDepositAddress</a></li><li data-type='method'><a href="RESTv2.html#getSettings">getSettings</a></li><li data-type='method'><a href="RESTv2.html#getURL">getURL</a></li><li data-type='method'><a href="RESTv2.html#inactiveSymbols">inactiveSymbols</a></li><li data-type='method'><a href="RESTv2.html#invalidateAuthToken">invalidateAuthToken</a></li><li data-type='method'><a href="RESTv2.html#keepFunding">keepFunding</a></li><li data-type='method'><a href="RESTv2.html#keyPermissions">keyPermissions</a></li><li data-type='method'><a href="RESTv2.html#ledgers">ledgers</a></li><li data-type='method'><a href="RESTv2.html#liquidations">liquidations</a></li><li data-type='method'><a href="RESTv2.html#logins">logins</a></li><li data-type='method'><a href="RESTv2.html#marginInfo">marginInfo</a></li><li data-type='method'><a href="RESTv2.html#marketAveragePrice">marketAveragePrice</a></li><li data-type='method'><a href="RESTv2.html#movements">movements</a></li><li data-type='method'><a href="RESTv2.html#orderBook">orderBook</a></li><li data-type='method'><a href="RESTv2.html#orderHistory">orderHistory</a></li><li data-type='method'><a href="RESTv2.html#orderHistoryWithIds">orderHistoryWithIds</a></li><li data-type='method'><a href="RESTv2.html#orderMultiOp">orderMultiOp</a></li><li data-type='method'><a href="RESTv2.html#orderTrades">orderTrades</a></li><li data-type='method'><a href="RESTv2.html#payDepositsUnlinked">payDepositsUnlinked</a></li><li data-type='method'><a href="RESTv2.html#payInvoiceComplete">payInvoiceComplete</a></li><li data-type='method'><a href="RESTv2.html#payInvoiceCreate">payInvoiceCreate</a></li><li data-type='method'><a href="RESTv2.html#payInvoiceList">payInvoiceList</a></li><li data-type='method'><a href="RESTv2.html#performance">performance</a></li><li data-type='method'><a href="RESTv2.html#positions">positions</a></li><li data-type='method'><a href="RESTv2.html#positionsAudit">positionsAudit</a></li><li data-type='method'><a href="RESTv2.html#positionsHistory">positionsHistory</a></li><li data-type='method'><a href="RESTv2.html#positionsSnapshot">positionsSnapshot</a></li><li data-type='method'><a href="RESTv2.html#publicPulseHistory">publicPulseHistory</a></li><li data-type='method'><a href="RESTv2.html#publicPulseProfile">publicPulseProfile</a></li><li data-type='method'><a href="RESTv2.html#pulseHistory">pulseHistory</a></li><li data-type='method'><a href="RESTv2.html#stats">stats</a></li><li data-type='method'><a href="RESTv2.html#status">status</a></li><li data-type='method'><a href="RESTv2.html#statusMessages">statusMessages</a></li><li data-type='method'><a href="RESTv2.html#submitAutoFunding">submitAutoFunding</a></li><li data-type='method'><a href="RESTv2.html#submitFundingOffer">submitFundingOffer</a></li><li data-type='method'><a href="RESTv2.html#submitOrder">submitOrder</a></li><li data-type='method'><a href="RESTv2.html#submitOrderMulti">submitOrderMulti</a></li><li data-type='method'><a href="RESTv2.html#symbolDetails">symbolDetails</a></li><li data-type='method'><a href="RESTv2.html#symbols">symbols</a></li><li data-type='method'><a href="RESTv2.html#ticker">ticker</a></li><li data-type='method'><a href="RESTv2.html#tickers">tickers</a></li><li data-type='method'><a href="RESTv2.html#tickersHistory">tickersHistory</a></li><li data-type='method'><a href="RESTv2.html#trades">trades</a></li><li data-type='method'><a href="RESTv2.html#transfer">transfer</a></li><li data-type='method'><a href="RESTv2.html#updateOrder">updateOrder</a></li><li data-type='method'><a href="RESTv2.html#updateOrderMulti">updateOrderMulti</a></li><li data-type='method'><a href="RESTv2.html#updateSettings">updateSettings</a></li><li data-type='method'><a href="RESTv2.html#userInfo">userInfo</a></li><li data-type='method'><a href="RESTv2.html#usesAgent">usesAgent</a></li><li data-type='method'><a href="RESTv2.html#wallets">wallets</a></li><li data-type='method'><a href="RESTv2.html#walletsHistory">walletsHistory</a></li><li data-type='method'><a href="RESTv2.html#withdraw">withdraw</a></li></ul></li></ul><h3><a href="global.html">Global</a></h3> </nav> <div id="main"> <h1 class="page-title">rest2.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>'use strict' const debug = require('debug')('bfx:rest2') const rp = require('request-promise') const Promise = require('bluebird') const _isEmpty = require('lodash/isEmpty') const _isString = require('lodash/isString') const _isFunction = require('lodash/isFunction') const _pick = require('lodash/pick') const _omitBy = require('lodash/omitBy') const _isNil = require('lodash/isNil') const _isInteger = require('lodash/isInteger') const _flatten = require('lodash/flatten') const { URLSearchParams } = require('url') const { genAuthSig, nonce, isClass } = require('bfx-api-node-util') const { FundingCredit, FundingLoan, FundingOffer, FundingTrade, MarginInfo, Order, Position, Trade, PublicTrade, TradingTicker, TradingTickerHist, FundingTicker, FundingTickerHist, Wallet, WalletHist, Alert, Candle, Movement, LedgerEntry, Liquidations, UserInfo, Currency, StatusMessagesDeriv, Notification, Login, ChangeLog, PublicPulseProfile, PulseMessage, Invoice, SymbolDetails, TransactionFee, AccountSummary, AuthPermission, CoreSettings } = require('bfx-api-node-models') const BASE_TIMEOUT = 15000 const API_URL = 'https://api.bitfinex.com' /** * Parses response into notification object * * @param {object} data - notification * @returns {Notification} n * @private */ function _takeResNotify (data) { const notification = new Notification(data) return notification } /** * Communicates with v2 of the Bitfinex HTTP API */ class RESTv2 { /** * Instantiate a new REST v2 transport. * * @param {object} opts - options * @param {string} [opts.affCode] - affiliate code to be applied to all orders * @param {string} [opts.apiKey] - API key * @param {string} [opts.apiSecret] - API secret * @param {string} [opts.authToken] - optional auth option * @param {string} [opts.url] - endpoint URL * @param {boolean} [opts.transform] - default false * @param {object} [opts.agent] - optional node agent for connection (proxy) * @param {number} [opts.timeout] - default 15000 */ constructor (opts = { affCode: null, apiKey: '', apiSecret: '', authToken: '', company: '', url: API_URL, transform: false, agent: null, timeout: BASE_TIMEOUT }) { this._checkOpts(opts) this._url = opts.url || API_URL this._apiKey = opts.apiKey || '' this._apiSecret = opts.apiSecret || '' this._authToken = opts.authToken || '' this._company = opts.company || '' this._transform = !!opts.transform this._agent = opts.agent this._affCode = opts.affCode this._timeout = opts.timeout || BASE_TIMEOUT } /** * Check constructor options * * @param {object} opts - constructor options * @throws {Error} - throws an Error if check is not passed * @returns {undefined} * @private */ _checkOpts (opts) { if ( !_isNil(opts.timeout) &amp;&amp; !_isInteger(opts.timeout) ) { throw new Error('ERR_TIMEOUT_DATA_TYPE_ERROR') } } /** * @returns {boolean} url */ getURL () { return this._url } /** * @returns {boolean} usesAgent */ usesAgent () { return !!this._agent } /** * @param {string} path - path * @param {object} payload - payload * @param {Function} [cb] - callback * @param {object|Function} transformer - model class or function * @returns {Promise} p * @private */ _makeAuthRequest (path, payload = {}, cb, transformer) { if ((!this._apiKey || !this._apiSecret) &amp;&amp; !this._authToken) { const e = new Error('missing api key or secret') return this._cb(e, null, cb) } const url = `${this._url}/v2${path}` const n = nonce() const sanitizedPayload = _omitBy(payload, _isNil) const keys = () => { const sigPayload = `/api/v2${path}${n}${JSON.stringify(sanitizedPayload)}` const { sig } = genAuthSig(this._apiSecret, sigPayload) return { 'bfx-apikey': this._apiKey, 'bfx-signature': sig } } const auth = (this._authToken) ? { 'bfx-token': this._authToken } : keys() debug('POST %s', url) return rp({ url, method: 'POST', timeout: this._timeout, headers: { 'bfx-nonce': n, ...auth }, agent: this._agent, body: sanitizedPayload, json: true }).then((data) => { return this._response(data, transformer, cb) }) } /** * @param {string} path - path * @param {Function} [cb] - callback * @param {object|Function} transformer - model class or function * @returns {Promise} p * @private */ _makePublicRequest (path, cb, transformer) { const url = `${this._url}/v2${path}` debug('GET %s', url) return rp({ url, method: 'GET', timeout: this._timeout, agent: this._agent, json: true }).then((data) => { return this._response(data, transformer, cb) }) } /** * NOTE: New API method, only returns a promise. Callback support will be * deprecated! * * @param {string} path - path * @param {object} body - payload * @param {object|Function} transformer - model class or function * @returns {Promise} p * @private */ _makePublicPostRequest (path, body, transformer) { const url = `${this._url}/v2${path}` debug('POST %s', url) return rp({ url, method: 'POST', timeout: this._timeout, agent: this._agent, json: true, body }).then((data) => { return this._response(data, transformer) }) } /** * @param {object} data * @param {object|Function} transformer - model class or function * @returns {object|object[]} finalData * @private */ _doTransform (data, transformer) { if (isClass(transformer)) { return this._classTransform(data, transformer) } else if (_isFunction(transformer)) { return transformer(data) } else { return data } } /** * @param {object} data - data * @param {object} ModelClass - class * @returns {object|object[]} finalData * @private */ _classTransform (data, ModelClass) { if (!data || data.length === 0) return [] if (!ModelClass || !this._transform) return data if (Array.isArray(data[0])) { return data.map(row => new ModelClass(row, this)) } return new ModelClass(data, this) } /** * @param {object} data - data * @param {object|Function} transformer - model class or function * @param {Function} [cb] - callback * @returns {object|object[]} finalData * @private */ _response (data, transformer, cb) { try { const res = (this._transform) ? this._doTransform(data, transformer) : data return this._cb(null, res, cb) } catch (e) { return this._cb(e, null, cb) } } /** * @param {Error} err - error * @param {object} res -resposne * @param {Function} [cb] - callback * @returns {Promise} p * @private */ _cb (err, res, cb) { return new Promise((resolve, reject) => { const _isCbFunc = _isFunction(cb) if (err) { if (err.error &amp;&amp; err.error[1] === 10114) { err.message += ' see https://github.com/bitfinexcom/bitfinex-api-node/blob/master/README.md#nonce-too-small for help' } if (_isCbFunc) cb(err) return reject(err) } else { if (_isCbFunc) cb(null, res) return resolve(res) } }) } /** * @param {Array[]} data order matters * @returns {Array[]} merged arr of currencies and names sorted with no pairs repeated adding pool and explorer to each * @private */ _genCurrencyList (data) { if (!Array.isArray(data) || data.length !== 6) { return data } const transformArrToObj = (arr) => { const obj = {} arr.forEach((c) => { if (!Array.isArray(c)) { obj[c] = c } else if (c.length > 1) { obj[c[0]] = c[1] } }) return obj } const listedCurr = transformArrToObj(data[0]) const mapedCurrSym = transformArrToObj(data[1]) const mapedCurrLabel = transformArrToObj(data[2]) const pool = transformArrToObj(data[3]) const explorer = transformArrToObj(data[4]) const walletFx = transformArrToObj(data[5]) const allCurrObj = { ...listedCurr, ...mapedCurrSym, ...mapedCurrLabel } // Assigne explores of pool to currencies Object.keys(pool).forEach((key) => { if (!explorer[key]) { if (explorer[pool[key]]) { explorer[key] = explorer[pool[key]] } } }) const allCurArr = [] Object.keys(allCurrObj).forEach((key) => { const cPool = pool[key] || null const cExpl = explorer[key] || [] const cName = allCurrObj[key] const cSymbol = mapedCurrSym[key] || key const cWfx = walletFx[key] || [] allCurArr.push([key, cName, cPool, cExpl, cSymbol, cWfx]) }) return allCurArr } /** * @param {string} symbol - i.e. tBTCUSD * @param {string} prec - i.e. P0 * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-public-books */ orderBook (symbol = 'tBTCUSD', prec = 'P0', cb) { return this._makePublicRequest(`/book/${symbol}/${prec}`, cb) } /** * @param {string} nickname - i.e. Bitfinex * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/reference#rest-public-pulse-profile */ publicPulseProfile (nickname, cb) { return this._makePublicRequest(`/pulse/profile/${nickname}`, cb, PublicPulseProfile) } /** * @param {string} limit - Number of records (Max: 100) * @param {string} end - Millisecond start time * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/reference#rest-public-pulse-hist */ publicPulseHistory (limit, end, cb) { return this._makePublicRequest(`/pulse/hist?limit=${limit}&amp;end=${end}`, cb, PulseMessage) } /** * @param {object} params - parameters * @param {string} params.symbol - Symbol you want information about i.e tBTCUSD, fUSD * @param {string} params.amount - Amount. Positive for buy, negative for sell (ex. "1.123") * @param {string} params.period - (optional) Maximum period for Margin Funding * @param {string} params.rate_limit - Limit rate/price (ex. "1000.5") * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/reference#rest-public-calc-market-average-price */ marketAveragePrice (params, cb) { const usp = new URLSearchParams(params) return this._makePublicPostRequest(`/calc/trade/avg?${usp.toString()}`, cb) } /** * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-public-platform-status */ status (cb = () => { }) { return this._makePublicRequest('/platform/status', cb) } /** * @param {string} [type] - type * @param {string[]} [keys] - keys * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#status */ statusMessages (type = 'deriv', keys = ['ALL'], cb) { const url = `/status/${type}?keys=${keys.join(',')}` const transformer = (type === 'deriv') ? StatusMessagesDeriv : null return this._makePublicRequest(url, cb, transformer) } /** * @param {string} symbol - symbol * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-public-ticker */ ticker (symbol = 'tBTCUSD', cb) { const transformer = (data) => { const ticker = [symbol, ...data] return (symbol[0] === 't') ? new TradingTicker(ticker) : new FundingTicker(ticker) } return this._makePublicRequest(`/ticker/${symbol}`, cb, transformer) } /** * @param {string[]} symbols - symbols * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-public-tickers */ tickers (symbols = [], cb) { const transformer = (data) => { return data.map(ticker => ( (ticker[0] || '')[0] === 't' ? new TradingTicker(ticker) : new FundingTicker(ticker) )) } const url = `/tickers?symbols=${symbols.join(',')}` return this._makePublicRequest(url, cb, transformer) } /** * @param {string[]} symbols - symbols * @param {number} [start] - query start timestamp * @param {number} [end] - query end timestamp * @param {number} [limit] - query limit * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-public-tickers-history */ tickersHistory (symbols = [], start, end, limit = 250, cb) { const transformer = (data) => { return data.map(ticker => ( (ticker[0] || '')[0] === 't' ? new TradingTickerHist(ticker) : new FundingTickerHist(ticker) )) } const s = (start) ? `&amp;start=${start}` : '' const e = (end) ? `&amp;end=${end}` : '' const params = `?symbols=${symbols.join(',')}${s}${e}&amp;limit=${limit}` const url = `/tickers/hist${params}` return this._makePublicRequest(url, cb, transformer) } /** * @param {string} key - key * @param {string} context - context * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-public-stats */ stats (key = 'pos.size:1m:tBTCUSD:long', context = 'hist', cb) { return this._makePublicRequest(`/stats1/${key}/${context}`, cb) } /** * * @param {object} opts - options * @param {string} opts.timeframe - 1m, 5m, 15m, 30m, 1h, 3h, 6h, 12h, 1D, 7D, 14D, 1M * @param {string} opts.symbol - symbol * @param {string} opts.section - hist, last * @param {Function} [cb] - callback * @returns {Promise} p * @see http://docs.bitfinex.com/v2/reference#rest-public-candles */ candles ({ timeframe = '1m', symbol = 'tBTCUSD', section = 'hist', query = {} }, cb) { let url = `/candles/trade:${timeframe}:${symbol}/${section}` if (Object.keys(query).length > 0) { url += `?${new URLSearchParams(query).toString()}` } return this._makePublicRequest(url, cb, Candle) } /** * Query configuration information * * @param {string[]} keys - keys * @param {Function} [cb] - callback * @returns {Promise} p */ conf (keys = [], cb = () => { }) { if (_isEmpty(keys)) { return Promise.resolve([]) } const url = `/conf/${keys.join(',')}` return this._makePublicRequest(url, cb) } /** * Get a list of valid currencies ids, full names, pool and explorer * * @param {Function?} [cb] - legacy callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-public-currencies */ currencies (cb = () => { }) { const suffix = (this._company) ? ':' + this._company : '' const url = `/conf/${[ `pub:list:currency${suffix}`, `pub:map:currency:sym${suffix}`, `pub:map:currency:label${suffix}`, `pub:map:currency:pool${suffix}`, `pub:map:currency:explorer${suffix}`, `pub:map:currency:wfx${suffix}` ].join(',')}` return this._makePublicRequest(url, cb, (data) => { const res = this._genCurrencyList(data) return this._doTransform(res, Currency) }) } /** * @param {string} type - type * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-auth-alert-list */ alertList (type = 'price', cb) { return this._makeAuthRequest('/auth/r/alerts', { type }, cb, Alert) } /** * @param {string} type - type * @param {string} symbol - symbol * @param {number} price - price * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-auth-alert-set */ alertSet (type = 'price', symbol = 'tBTCUSD', price = 0, cb) { return this._makeAuthRequest('/auth/w/alert/set', { type, symbol, price }, cb, Alert) } /** * @param {string} symbol - symbol * @param {number} price - price * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-auth-alert-delete */ alertDelete (symbol = 'tBTCUSD', price = 0, cb) { return this._makeAuthRequest('/auth/w/alert/del', { symbol, price }, cb) } /** * @param {string} symbol - symbol * @param {number} [start] - query start * @param {number} [end] - query end * @param {number} [limit] - query limit * @param {number} [sort] - if 1, sorts results oldest first * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-public-trades */ trades (symbol = 'tBTCUSD', start = null, end = null, limit = null, sort = null, cb) { const query = {} if (start !== null) query.start = start if (end !== null) query.end = end if (limit !== null) query.limit = limit if (sort !== null) query.sort = sort let url = `/trades/${symbol}/hist` if (Object.keys(query).length > 0) { url += `?${new URLSearchParams(query).toString()}` } return this._makePublicRequest(url, cb, PublicTrade) } /** * @param {number} [start] - query start * @param {number} [end] - query end * @param {number} [limit] - query limit * @param {number} [sort] - if 1, sorts results oldest first * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-public-liquidations */ liquidations (start = null, end = null, limit = null, sort = null, cb) { const query = {} if (start !== null) query.start = start if (end !== null) query.end = end if (limit !== null) query.limit = limit if (sort !== null) query.sort = sort let url = '/liquidations/hist' if (Object.keys(query).length > 0) { url += `?${new URLSearchParams(query).toString()}` } return this._makePublicRequest(url, cb, Liquidations) } /** * @param {string} [symbol] - optional, omit/leave empty for all * @param {number} [start] - query start * @param {number} [end] - query end * @param {number} [limit] - query limit * @param {number} [sort] - if 1, sorts results oldest first * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-auth-trades-hist */ accountTrades (symbol, start = null, end = null, limit = null, sort = null, cb) { const url = !_isEmpty(symbol) ? `/auth/r/trades/${symbol}/hist` : '/auth/r/trades/hist' return this._makeAuthRequest(url, { start, end, limit, sort }, cb, Trade) } /** * @param {number} [start] - query start * @param {number} [end] - query end * @param {number} [limit] - query limit * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-auth-logins-hist */ logins (start = 0, end = Date.now(), limit = 25, cb) { const url = '/auth/r/logins/hist' return this._makeAuthRequest(url, { start, end, limit }, cb, Login) } /** * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-auth-wallets */ wallets (cb) { return this._makeAuthRequest('/auth/r/wallets', {}, cb, Wallet) } /** * @param {number} [end] - query end * @param {string} [currency] - currency * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-auth-wallets-hist */ walletsHistory (end = Date.now(), currency = null, cb) { return this._makeAuthRequest('/auth/r/wallets/hist', { end, currency }, cb, WalletHist) } /** * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/reference#rest-auth-info-user */ userInfo (cb) { return this._makeAuthRequest('/auth/r/info/user', {}, cb, UserInfo) } /** * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-auth-orders */ activeOrders (cb) { return this._makeAuthRequest('/auth/r/orders', {}, cb, Order) } /** * @param {Array} [ids] - order ids * @param {Function} cb - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-auth-orders */ activeOrdersWithIds (ids = [], cb) { const url = '/auth/r/orders' return this._makeAuthRequest(url, { id: ids }, cb, Order) } /** * @param {string} [ccy] - i.e. ETH * @param {number} [start] - query start * @param {number} [end] - query end * @param {number} [limit] - query limit, default 25 * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#movements */ movements (ccy, start = null, end = Date.now(), limit = 25, cb) { const url = ccy ? `/auth/r/movements/${ccy}/hist` : '/auth/r/movements/hist' return this._makeAuthRequest(url, { start, end, limit }, cb, Movement) } /** * @param {object|string} filters - filters * @param {number} [start] - query start * @param {number} [end] - query end * @param {number} [limit] - default 25 * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#ledgers */ ledgers (filters, start = null, end = Date.now(), limit = 25, cb) { const parseFilters = (sent) => { if (_isString(sent)) return { ccy: sent } return sent || {} } const { ccy, category } = parseFilters(filters) const url = ccy ? `/auth/r/ledgers/${ccy}/hist` : '/auth/r/ledgers/hist' return this._makeAuthRequest(url, { start, end, limit, category }, cb, LedgerEntry) } /** * @param {string?} symbol - optional, omit/leave empty for all * @param {number} [start] - query start * @param {number} [end] - query end * @param {number} [limit] - query limit * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/reference#rest-auth-orders-history */ orderHistory (symbol, start = null, end = null, limit = null, cb) { const url = !_isEmpty(symbol) ? `/auth/r/orders/${symbol}/hist` : '/auth/r/orders/hist' return this._makeAuthRequest(url, { start, end, limit }, cb, Order) } /** * @param {Array} [ids] - order ids * @param {Function} cb - callback * @returns {Promise} p * @see https://docs.bitfinex.com/reference#rest-auth-orders-history */ orderHistoryWithIds (ids = [], cb) { const url = '/auth/r/orders/hist' return this._makeAuthRequest(url, { id: ids }, cb, Order) } /** * @param {string} [symbol] - symbol * @param {number} [start] - query start * @param {number} [end] - query end * @param {number} [limit] - query limit * @param {number} [orderID] - order ID * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-auth-order-trades */ orderTrades (symbol = 'tBTCUSD', start = null, end = null, limit = null, orderID, cb) { return this._makeAuthRequest(`/auth/r/order/${symbol}:${orderID}/trades`, { start, end, limit }, cb, Trade) } /** * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-auth-positions */ positions (cb) { return this._makeAuthRequest('/auth/r/positions', {}, cb, Position) } /** * @param {number} [start] - query start * @param {number} [end] - query end * @param {number} [limit] - query limit * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-auth-positions-history */ positionsHistory (start = 0, end = Date.now(), limit = 50, cb) { return this._makeAuthRequest('/auth/r/positions/hist', { start, end, limit }, cb, Position) } /** * @param {Array[]} [id] - ids of positions to audit * @param {number} [start] - query start * @param {number} [end] - query end * @param {number} [limit] - query limit * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-auth-positions-audit */ positionsAudit (id = [], start = 0, end = Date.now(), limit = 250, cb) { return this._makeAuthRequest('/auth/r/positions/audit', { id, start, end, limit }, cb, Position) } /** * @param {number} [start] - query start * @param {number} [end] - query end * @param {number} [limit] - query limit * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-auth-positions-snap */ positionsSnapshot (start = 0, end = Date.now(), limit = 50, cb) { return this._makeAuthRequest('/auth/r/positions/snap', { start, end, limit }, cb, Position) } /** * @param {string} symbol - symbol * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-auth-funding-offers */ fundingOffers (symbol = 'fUSD', cb) { return this._makeAuthRequest(`/auth/r/funding/offers/${symbol}`, {}, cb, FundingOffer) } /** * @param {string} [symbol] - omit/leave empty for all * @param {number} [start] - query start * @param {number} [end] - query end * @param {number} [limit] - query limit * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-auth-funding-offers-hist */ fundingOfferHistory (symbol, start = null, end = null, limit = null, cb) { const url = !_isEmpty(symbol) ? `/auth/r/funding/offers/${symbol}/hist` : '/auth/r/funding/offers/hist' return this._makeAuthRequest(url, { start, end, limit }, cb, FundingOffer) } /** * @param {string} [symbol] - symbol * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-auth-funding-loans */ fundingLoans (symbol = 'fUSD', cb) { return this._makeAuthRequest(`/auth/r/funding/loans/${symbol}`, {}, cb, FundingLoan) } /** * @param {string} [symbol] - omit/leave empty for all * @param {number} [start] - query start * @param {number} [end] - query end * @param {number} [limit] - query limit * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-auth-funding-loans-hist */ fundingLoanHistory (symbol, start = null, end = null, limit = null, cb) { const url = !_isEmpty(symbol) ? `/auth/r/funding/loans/${symbol}/hist` : '/auth/r/funding/loans/hist' return this._makeAuthRequest(url, { start, end, limit }, cb, FundingLoan) } /** * @param {string} [symbol] - symbol * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-auth-funding-credits */ fundingCredits (symbol = 'fUSD', cb) { return this._makeAuthRequest(`/auth/r/funding/credits/${symbol}`, {}, cb, FundingCredit) } /** * @param {string} [symbol] - omit/leave empty for all * @param {number} [start] - query start * @param {number} [end] - query end * @param {number} [limit] - query limit * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-auth-funding-credits-hist */ fundingCreditHistory (symbol, start = null, end = null, limit = null, cb) { const url = !_isEmpty(symbol) ? `/auth/r/funding/credits/${symbol}/hist` : '/auth/r/funding/credits/hist' return this._makeAuthRequest(url, { start, end, limit }, cb, FundingCredit) } /** * @param {string} [symbol] - optional, omit/leave empty for all * @param {number} [start] - query start * @param {number} [end] - query end * @param {number} [limit] - query limit * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-auth-funding-trades-hist */ fundingTrades (symbol = 'fBTC', start = 0, end = Date.now(), limit = null, cb) { const url = !_isEmpty(symbol) ? `/auth/r/funding/trades/${symbol}/hist` : '/auth/r/funding/trades/hist' return this._makeAuthRequest(url, { start, end, limit }, cb, FundingTrade) } /** * @param {string} key - key * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-auth-info-margin */ marginInfo (key = 'base', cb) { return this._makeAuthRequest(`/auth/r/info/margin/${key}`, {}, cb, MarginInfo) } /** * @param {number} [start] - query start * @param {number} [end] - query end * @param {number} [limit] - query limit * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-auth-audit-hist */ changeLogs (start = 0, end = Date.now(), limit = null, cb) { return this._makeAuthRequest('/auth/r/audit/hist', { start, end, limit }, cb, ChangeLog) } /** * @param {string} key - key * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-auth-info-funding */ fundingInfo (key = 'fUSD', cb) { return this._makeAuthRequest(`/auth/r/info/funding/${key}`, {}, cb) } /** * @param {object} params - parameters * @param {string} params.type - Specify the funding type ('credit' or 'loan') * @param {string} params.id - The loan or credit id * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/reference#rest-auth-keep-funding */ keepFunding (params, cb) { return this._makeAuthRequest('/auth/w/funding/keep', params, cb) .then(_takeResNotify) } /** * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-auth-performance */ performance (cb) { return this._makeAuthRequest('/auth/r/stats/perf:1D/hist', {}, cb) } /** * @param {string} symbol - symbol * @param {string} dir - dir * @param {number} rate - rate * @param {string} type - type * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-auth-calc-bal-avail */ calcAvailableBalance (symbol = 'tBTCUSD', dir, rate, type, cb) { return this._makeAuthRequest('/auth/calc/order/avail', { symbol, dir, rate, type }, cb) } /** * Get a list of valid symbol names * * @param {Function?} [cb] - legacy callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-public-symbols */ symbols (cb = () => { }) { const url = '/conf/pub:list:pair:exchange' return this._makePublicRequest(url, cb, (data) => { return data &amp;&amp; data[0] }) } /** * Get a list of inactive symbol names * * @param {Function?} [cb] - legacy callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-public-symbols */ inactiveSymbols (cb = () => { }) { const url = '/conf/pub:list:pair:exchange:inactive' return this._makePublicRequest(url, cb, (data) => { return data &amp;&amp; data[0] }) } /** * Get a list of valid symbol names * * @param {Function?} [cb] - legacy callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-public-futures */ futures (cb = () => { }) { const url = '/conf/pub:list:pair:futures' return this._makePublicRequest(url, cb, (data) => { return data &amp;&amp; data[0] }) } /** * Changes the collateral value of an active derivatives position for a certain pair. * * @param {string} symbol - symbol * @param {number} collateral - collateral * @param {Function} [cb] - legacy callback * @returns {Promise} p * @see https://docs.bitfinex.com/v2/reference#rest-auth-deriv-pos-collateral-set */ derivsPositionCollateralSet (symbol, collateral, cb) { const url = '/auth/w/deriv/collateral/set' const isRequestValid = (res) => !!(res &amp;&amp; res[0] &amp;&amp; res[0][0]) return this._makeAuthRequest(url, { symbol, collateral }, cb, isRequestValid) } /** * Get a list of valid symbol names and details * * @param {object} params - options * @param {boolean} params.includeFuturePairs - optional, default value is true * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/reference#rest-public-conf */ symbolDetails ({ includeFuturePairs = true } = {}, cb) { const url = `/conf/pub:info:pair${includeFuturePairs ? ',pub:info:pair:futures' : ''}` const transformer = (data) => { return data &amp;&amp; this._classTransform(_flatten(data), SymbolDetails) } return this._makePublicRequest(url, cb, transformer) } /** * Request account withdrawl fees * * @param {Function} [cb] - callback * @returns {Promise} p */ accountFees (cb) { const url = '/conf/pub:map:currency:tx:fee' const transformer = (data) => { return data &amp;&amp; this._classTransform(data[0], TransactionFee) } return this._makePublicRequest(url, cb, transformer) } /** * Returns a 30-day summary of your trading volume and return on margin * funding. * * @param {Function} [cb] - callback * @returns {Promise} p * @see https://docs.bitfinex.com/reference#rest-auth-summary */ accountSummary (cb) { const url = '/auth/r/summary' return this._makeAuthRequest(url, {}, cb, AccountSummary) } /** * Fetch the permissions of the key or token being used to generate this request * * @param {Function} [cb] - callback * @returns {Promise} p */ keyPermissions (cb) { return this._makeAuthRequest('/auth/r/permissions', {}, cb, AuthPermission) } /** * @param {object} params - parameters * @param {number} params.position_id - position ID * @param {Function} [cb] - callback * @returns {Promise} p * * @see https://docs.bitfinex.com/reference#rest-auth-submit-order */ closePosition (params, cb) { return this.positions() .then(res => { if (!this._transform) { res = res.map(row => new Position(row, this)) } const position = res.find(p => p.id === params.position_id &amp;&amp; p.status === 'ACTIVE') if (!position) throw new Error('position not found') return position }) .then(position => { const order = new Order({ type: Order.type.MARKET, symbol: position.symbol, amount: position.amount * -1, flags: Order.flags.POS_CLOSE }) return this.submitOrder(order) }) .then(res => this._cb(null, res, cb)) .catch(err => this._cb(err, null, cb)) } /** * @param {object} settings - key:value map * @param {Function} [cb] - callback * @returns {Promise} p */ updateSettings (settings, cb) { return this._makeAuthRequest('/auth/w/settings/set', { settings }, cb) } /** * @param {string[]} keys - keys * @param {Function} [cb] - callback * @returns {Promise} p */ deleteSettings (keys, cb) { return this._makeAuthRequest('/auth/w/settings/del', { keys }, cb) } /** * @param {string[]} keys - keys * @param {Function} [cb] - callback * @returns {Promise} p */ getSettings (keys, cb) { return this._makeAuthRequest('/auth/r/settings', { keys }, cb) } /** * @param {string[]} keys - keys * @param {Function} [cb] - callback * @returns {Promise} p */ getCoreSettings (keys, cb) { return this._makeAuthRequest('/auth/r/settings/core', { keys }, cb, CoreSettings) } /** * @param {string} ccy1 - i.e. BTC * @param {string} ccy2 - i.e. USD * @returns {Promise} p - resolves to currenct exchange rate */ exchangeRate (ccy1, ccy2) { return this._makePublicPostRequest('/calc/fx', { ccy1, ccy2 }).then(res => res[0]) } /** * @param {object} opts - options * @param {number} [opts.ttl] - time-to-live in seconds * @param {string} opts.scope - scope of the token * @param {string[]} [opts.caps] - token caps/permissions * @param {boolean} [opts.writePermission] - token write permission * @param {string} [opts._cust_ip] - user ip address * @param {Function} [cb] - callback * @returns {Promise} p */ generateToken (opts, cb) { let params = _pick(opts || {}, ['ttl', 'scope', 'caps', 'writePermission', '_cust_ip']) params = _omitBy(params, _isNil) if (!params.scope) return this._cb(new Error('scope param is required'), null, cb) return this._makeAuthRequest('/auth/w/token', params, cb) } /** * @param {string} authToken - target that will be invalidated * @param {Function} [cb] - callback * @returns {Promise} p */ invalidateAuthToken (authToken, cb) { const params = { token: authToken } return this._makeAuthRequest('/auth/w/token/del', params, cb) } /** * Submit new order * * @param {Order} order - order model instance * @param {Function} [cb] - callback * @returns {Promise} p */ submitOrder (order, cb) { const packet = order.toNewOrderPacket() if (this._affCode) { if (!packet.meta) { packet.meta = {} } packet.meta.aff_code = packet.meta.aff_code || this._affCode // eslint-disable-line } return this._makeAuthRequest('/auth/w/order/submit', packet, cb) .then((res) => { //