Skip to content

Commit

Permalink
merged refactoring fixes ccxt#1325
Browse files Browse the repository at this point in the history
  • Loading branch information
kroitor committed Jan 21, 2018
2 parents 3928a05 + 28d611f commit 716c72d
Show file tree
Hide file tree
Showing 119 changed files with 1,391 additions and 1,068 deletions.
4 changes: 2 additions & 2 deletions ccxt.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ SOFTWARE.
//-----------------------------------------------------------------------------

const Exchange = require ('./js/base/Exchange')
const functions = require ('./js/base/functions')
const errors = require ('./js/base/errors')
, functions = require ('./js/base/functions')
, errors = require ('./js/base/errors')

//-----------------------------------------------------------------------------
// this is updated by vss.js when building
Expand Down
10 changes: 6 additions & 4 deletions doc/manual.rst
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,11 @@ Here's an overview of base exchange properties with values added for example:
},
'version': 'v1', // string ending with digits
'api': { ... }, // dictionary of api endpoints
'hasFetchTickers': true, // true if the exchange implements fetchTickers ()
'hasFetchOHLCV': false, // true if the exchange implements fetchOHLCV ()
'timeframes': { // empty if the exchange !hasFetchOHLCV
'has': {
'fetchTickers': true, // true if the exchange implements fetchTickers ()
'fetchOHLCV': false, // true if the exchange implements fetchOHLCV ()
},
'timeframes': { // empty if the exchange !has.fetchOHLCV
'1m': '1minute',
'1h': '1hour',
'1d': '1day',
Expand Down Expand Up @@ -1159,7 +1161,7 @@ You can call the unified ``fetchOHLCV`` / ``fetch_ohlcv`` method to get the list
// JavaScript
let sleep = (ms) => new Promise (resolve => setTimeout (resolve, ms));
if (exchange.hasFetchOHLCV) {
if (exchange.has.fetchOHLCV) {
(async () => {
for (symbol in exchange.markets) {
await sleep (exchange.rateLimit) // milliseconds
Expand Down
2 changes: 1 addition & 1 deletion examples/js/fetch-ticker-where-available.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let printUsage = function () {
let id = ccxt.exchanges[i]

const exchange = new ccxt[id] ()
if (exchange.hasPublicAPI) {
if (exchange.has.publicAPI) {

try {

Expand Down
2 changes: 1 addition & 1 deletion examples/js/search-all-exchanges.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const checkAgainst = strict ?
// instantiate the exchange
let exchange = new ccxt[id] ()

if (exchange.hasPublicAPI) {
if (exchange.has.publicAPI) {

try {

Expand Down
10 changes: 6 additions & 4 deletions js/_1broker.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ module.exports = class _1broker extends Exchange {
'countries': 'US',
'rateLimit': 1500,
'version': 'v2',
'hasPublicAPI': false,
'hasCORS': true,
'hasFetchTrades': false,
'hasFetchOHLCV': true,
'has': {
'publicAPI': false,
'CORS': true,
'fetchTrades': false,
'fetchOHLCV': true,
},
'timeframes': {
'1m': '60',
'15m': '900',
Expand Down
8 changes: 5 additions & 3 deletions js/_1btcxe.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ module.exports = class _1btcxe extends Exchange {
'name': '1BTCXE',
'countries': 'PA', // Panama
'comment': 'Crypto Capital API',
'hasCORS': true,
'hasFetchOHLCV': true,
'hasWithdraw': true,
'has': {
'CORS': true,
'fetchTickers': true,
'withdraw': true,
},
'timeframes': {
'1d': '1year',
},
Expand Down
10 changes: 6 additions & 4 deletions js/acx.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ module.exports = class acx extends Exchange {
'countries': 'AU',
'rateLimit': 1000,
'version': 'v2',
'hasCORS': true,
'hasFetchTickers': true,
'hasFetchOHLCV': true,
'hasWithdraw': true,
'has': {
'CORS': true,
'fetchTickers': true,
'fetchOHLCV': true,
'withdraw': true,
},
'timeframes': {
'1m': '1',
'5m': '5',
Expand Down
4 changes: 3 additions & 1 deletion js/allcoin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ module.exports = class allcoin extends okcoinusd {
'id': 'allcoin',
'name': 'Allcoin',
'countries': 'CA',
'hasCORS': false,
'has': {
'CORS': false,
},
'extension': '',
'urls': {
'logo': 'https://user-images.githubusercontent.com/1294454/31561809-c316b37c-b061-11e7-8d5a-b547b4d730eb.jpg',
Expand Down
8 changes: 5 additions & 3 deletions js/anxpro.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ module.exports = class anxpro extends Exchange {
'countries': [ 'JP', 'SG', 'HK', 'NZ' ],
'version': '2',
'rateLimit': 1500,
'hasCORS': false,
'hasFetchTrades': false,
'hasWithdraw': true,
'has': {
'CORS': false,
'fetchTrades': false,
'withdraw': true,
},
'urls': {
'logo': 'https://user-images.githubusercontent.com/1294454/27765983-fd8595da-5ec9-11e7-82e3-adb3ab8c2612.jpg',
'api': 'https://anxpro.com/api',
Expand Down
140 changes: 44 additions & 96 deletions js/base/Exchange.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
"use strict";

//-----------------------------------------------------------------------------
/* ------------------------------------------------------------------------ */

const isNode = (typeof window === 'undefined') && !(typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope)
, functions = require ('./functions')
, throttle = require ('./throttle')
, defaultFetch = isNode ? require ('fetch-ponyfill')().fetch : fetch
const functions = require ('./functions')
, Market = require ('./Market')

const { deepExtend
const { isNode
, keys
, values
, deepExtend
, extend
, sleep
, timeout
, flatten
, indexBy
, sortBy
, groupBy
, aggregate
, uuid
, precisionFromString } = functions
, unCamelCase
, precisionFromString
, throttle
, capitalize } = functions

const { now
, sleep
, timeout
, TimedOut } = require ('./functions/time')

const { ExchangeError
, NotSupported
Expand All @@ -27,9 +33,11 @@ const { ExchangeError
, RequestTimeout
, ExchangeNotAvailable } = require ('./errors')

// stub until we get a better solution for Webpack and React
// const journal = isNode && require ('./journal')
const journal = undefined
const defaultFetch = isNode ? require ('fetch-ponyfill')().fetch : fetch

const journal = undefined // isNode && require ('./journal') // stub until we get a better solution for Webpack and React

/* ------------------------------------------------------------------------ */

module.exports = class Exchange {

Expand Down Expand Up @@ -80,9 +88,9 @@ module.exports = class Exchange {

this.iso8601 = timestamp => new Date (timestamp).toISOString ()
this.parse8601 = x => Date.parse (((x.indexOf ('+') >= 0) || (x.slice (-1) == 'Z')) ? x : (x + 'Z'))
this.milliseconds = Date.now
this.microseconds = () => Math.floor (this.milliseconds () * 1000)
this.seconds = () => Math.floor (this.milliseconds () / 1000)
this.milliseconds = now
this.microseconds = () => now () * 1000 // TODO: utilize performance.now for that purpose
this.seconds = () => Math.floor (now () / 1000)
this.id = undefined

// rate limiter settings
Expand All @@ -103,26 +111,6 @@ module.exports = class Exchange {
this.userAgent = undefined
this.twofa = false // two-factor authentication (2FA)
this.timeframes = undefined
this.hasPublicAPI = true
this.hasPrivateAPI = true
this.hasCORS = false
this.hasDeposit = false
this.hasFetchBalance = true
this.hasFetchClosedOrders = false
this.hasFetchCurrencies = false
this.hasFetchMyTrades = false
this.hasFetchOHLCV = false
this.hasFetchOpenOrders = false
this.hasFetchOrder = false
this.hasFetchOrderBook = true
this.hasFetchOrders = false
this.hasFetchTicker = true
this.hasFetchTickers = false
this.hasFetchBidsAsks = false
this.hasFetchTrades = true
this.hasWithdraw = false
this.hasCreateOrder = this.hasPrivateAPI
this.hasCancelOrder = this.hasPrivateAPI

this.apiKey = undefined
this.secret = undefined
Expand Down Expand Up @@ -152,68 +140,23 @@ module.exports = class Exchange {

this.arrayConcat = (a, b) => a.concat (b)

// TODO: generate

this.market_id = this.marketId
this.market_ids = this.marketIds
this.array_concat = this.arrayConcat
this.implode_params = this.implodeParams
this.extract_params = this.extractParams
this.fetch_balance = this.fetchBalance
this.fetch_free_balance = this.fetchFreeBalance
this.fetch_used_balance = this.fetchUsedBalance
this.fetch_total_balance = this.fetchTotalBalance
this.fetch_l2_order_book = this.fetchL2OrderBook
this.fetch_order_book = this.fetchOrderBook
this.fetch_bids_asks = this.fetchBidsAsks
this.fetch_tickers = this.fetchTickers
this.fetch_ticker = this.fetchTicker
this.fetch_trades = this.fetchTrades
this.fetch_order = this.fetchOrder
this.fetch_orders = this.fetchOrders
this.fetch_open_orders = this.fetchOpenOrders
this.fetch_closed_orders = this.fetchClosedOrders
this.fetch_order_status = this.fetchOrderStatus
this.fetch_markets = this.fetchMarkets
this.load_markets = this.loadMarkets
this.set_markets = this.setMarkets
this.parse_balance = this.parseBalance
this.parse_bid_ask = this.parseBidAsk
this.parse_bids_asks = this.parseBidsAsks
this.parse_order_book = this.parseOrderBook
this.parse_trades = this.parseTrades
this.parse_orders = this.parseOrders
this.parse_ohlcv = this.parseOHLCV
this.parse_ohlcvs = this.parseOHLCVs
this.edit_limit_buy_order = this.editLimitBuyOrder
this.edit_limit_sell_order = this.editLimitSellOrder
this.edit_limit_order = this.editLimitOrder
this.edit_order = this.editOrder
this.create_limit_buy_order = this.createLimitBuyOrder
this.create_limit_sell_order = this.createLimitSellOrder
this.create_market_buy_order = this.createMarketBuyOrder
this.create_market_sell_order = this.createMarketSellOrder
this.create_order = this.createOrder
this.calculate_fee = this.calculateFee
this.common_currency_code = this.commonCurrencyCode
this.price_to_precision = this.priceToPrecision
this.amount_to_precision = this.amountToPrecision
this.amount_to_string = this.amountToString
this.fee_to_precision = this.feeToPrecision
this.cost_to_precision = this.costToPrecision
this.precisionFromString = precisionFromString
this.precision_from_string = precisionFromString
this.truncate = functions.truncate
this.truncate_to_string = functions.truncate_to_string
this.uuid = uuid

// API methods metainfo
const names = Object.getOwnPropertyNames (this).concat (
Object.getOwnPropertyNames (this.constructor.prototype))

for (const k of names)
this[unCamelCase (k)] = this[k]

/* exchange's capabilities (overrideable) */

this.has = {
'cancelOrder': this.hasPrivateAPI,
'CORS': false,
'publicAPI': true,
'privateAPI': true,
'cancelOrder': true,
'createDepositAddress': false,
'createOrder': this.hasPrivateAPI,
'createOrder': true,
'deposit': false,
'fetchBalance': this.hasPrivateAPI,
'fetchBalance': true,
'fetchClosedOrders': false,
'fetchCurrencies': false,
'fetchDepositAddress': false,
Expand All @@ -238,6 +181,11 @@ module.exports = class Exchange {
for (const [property, value] of Object.entries (config))
this[property] = deepExtend (this[property], value)

// generate old metainfo interface
for (const k in this.has) {
this['has' + capitalize (k)] = !!this.has[k] // converts 'emulated' to true
}

if (this.api)
this.defineRestApi (this.api, 'request')

Expand Down Expand Up @@ -297,8 +245,8 @@ module.exports = class Exchange {
.then (response => this.handleRestResponse (response, url, method, headers, body))

return timeout (this.timeout, promise).catch (e => {
if (e instanceof RequestTimeout)
throw new RequestTimeout (this.id + ' ' + method + ' ' + url + ' ' + e.message + ' (' + this.timeout + ' ms)')
if (e instanceof TimedOut)
throw new RequestTimeout (this.id + ' ' + method + ' ' + url + ' request timed out (' + this.timeout + ' ms)')
throw e
})
}
Expand Down
Loading

0 comments on commit 716c72d

Please sign in to comment.