Code readability and added the forgotten mocks
This commit is contained in:
parent
134eaaa518
commit
7accdaa84f
12 changed files with 126 additions and 96 deletions
|
|
@ -1,37 +1,34 @@
|
|||
var ccxt = require('ccxt')
|
||||
const coinUtils = require('../../coin-utils')
|
||||
const common = require('../common/ccxt')
|
||||
const kraken = require('./kraken')
|
||||
const bitstamp = require('./bitstamp')
|
||||
const itbit = require('./itbit')
|
||||
const consts = require('./consts')
|
||||
const _ = require('lodash/fp')
|
||||
const ccxt = require('ccxt')
|
||||
|
||||
const ALL_EXCHANGES = {
|
||||
kraken,
|
||||
bitstamp,
|
||||
itbit
|
||||
}
|
||||
const { toUnit } = require('../../coin-utils')
|
||||
const { buildMarket, ALL } = require('../common/ccxt')
|
||||
const { ORDER_TYPES } = require('./consts')
|
||||
|
||||
const DEFAULT_PRICE_PRECISION = 2
|
||||
const DEFAULT_AMOUNT_PRECISION = 8
|
||||
|
||||
function trade (side, account, cryptoAtoms, fiatCode, cryptoCode, exchangeName) {
|
||||
try {
|
||||
const exchangeConfig = ALL_EXCHANGES[exchangeName]
|
||||
const exchangeConfig = ALL[exchangeName]
|
||||
if (!exchangeConfig) throw Error('Exchange configuration not found')
|
||||
|
||||
if (!exchangeConfig) throw Error('no exchange')
|
||||
if (exchangeConfig.isConfigValid && !exchangeConfig.isConfigValid(account)) throw Error('Invalid config')
|
||||
const { loadOptions, loadConfig = _.noop, isConfigValid, ORDER_TYPE, AMOUNT_PRECISION } = exchangeConfig
|
||||
if (_.isFunction(isConfigValid) && !isConfigValid(account)) throw Error('Invalid config')
|
||||
|
||||
const exchange = new ccxt[exchangeName](exchangeConfig.loadConfig(account))
|
||||
const symbol = buildMarket(fiatCode, cryptoCode, exchangeName)
|
||||
const precision = _.defaultTo(DEFAULT_AMOUNT_PRECISION, AMOUNT_PRECISION)
|
||||
const amount = toUnit(cryptoAtoms, cryptoCode).toFixed(precision)
|
||||
const options = _.isFunction(loadOptions) ? loadOptions(account) : {}
|
||||
const exchange = new ccxt[exchangeName](loadConfig(account))
|
||||
|
||||
const symbol = common.verifyCurrencies(exchangeName, fiatCode, cryptoCode)
|
||||
const precision = exchangeConfig.amountPrecision ? exchangeConfig.amountPrecision() : consts.DECIMAL_PRECISION.DEFAULT_AMOUNT
|
||||
const amount = coinUtils.toUnit(cryptoAtoms, cryptoCode).toFixed(precision)
|
||||
const options = exchangeConfig.loadOptions ? exchangeConfig.loadOptions(account) : {}
|
||||
if (exchangeConfig.ORDER_TYPE === consts.ORDER_TYPES.MARKET) {
|
||||
return exchange.createOrder(symbol, consts.ORDER_TYPES.MARKET, side, amount, null, options)
|
||||
if (ORDER_TYPE === ORDER_TYPES.MARKET) {
|
||||
return exchange.createOrder(symbol, ORDER_TYPES.MARKET, side, amount, null, options)
|
||||
}
|
||||
return exchange.fetchOrderBook(symbol)
|
||||
.then(orderBook => {
|
||||
const price = calculatePrice(side, amount, orderBook).toFixed(consts.DECIMAL_PRECISION.PRICE)
|
||||
return exchange.createOrder(symbol, consts.ORDER_TYPES.LIMIT, side, amount, price, options)
|
||||
const price = calculatePrice(side, amount, orderBook).toFixed(DEFAULT_PRICE_PRECISION)
|
||||
return exchange.createOrder(symbol, ORDER_TYPES.LIMIT, side, amount, price, options)
|
||||
})
|
||||
} catch (e) {
|
||||
return Promise.reject(e)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue