refactor: replace custom field function with const and move update trade logic inside a function
This commit is contained in:
parent
f45c783876
commit
8567b7887c
6 changed files with 25 additions and 19 deletions
|
|
@ -19,7 +19,8 @@ function fetchExchange (settings, cryptoCode) {
|
|||
})
|
||||
}
|
||||
|
||||
function buy (settings, cryptoAtoms, fiatCode, cryptoCode, tradeId) {
|
||||
function buy (settings, tradeEntry, tradeId) {
|
||||
const { cryptoAtoms, fiatCode, cryptoCode } = tradeEntry
|
||||
return fetchExchange(settings, cryptoCode)
|
||||
.then(r => {
|
||||
if (r.exchangeName === 'mock-exchange') {
|
||||
|
|
@ -29,7 +30,8 @@ function buy (settings, cryptoAtoms, fiatCode, cryptoCode, tradeId) {
|
|||
})
|
||||
}
|
||||
|
||||
function sell (settings, cryptoAtoms, fiatCode, cryptoCode, tradeId) {
|
||||
function sell (settings, tradeEntry, tradeId) {
|
||||
const { cryptoAtoms, fiatCode, cryptoCode } = tradeEntry
|
||||
return fetchExchange(settings, cryptoCode)
|
||||
.then(r => {
|
||||
if (r.exchangeName === 'mock-exchange') {
|
||||
|
|
|
|||
|
|
@ -517,16 +517,22 @@ function plugins (settings, deviceId) {
|
|||
const execute = tradeEntry.type === 'buy' ? exchange.buy : exchange.sell
|
||||
|
||||
return recordTrade(tradeEntry)
|
||||
.then(newEntry => {
|
||||
return execute(settings, tradeEntry.cryptoAtoms, tradeEntry.fiatCode, tradeEntry.cryptoCode, newEntry.id)
|
||||
.then(newEntry =>
|
||||
execute(settings, tradeEntry, newEntry.id)
|
||||
.catch(err => {
|
||||
const data = mergeTradeEntryAndError(tradeEntry, err)
|
||||
const sql = pgp.helpers.update(data, ['error'], 'trades') + ` WHERE id = ${newEntry.id}`
|
||||
return db.none(sql)
|
||||
.then(() => {
|
||||
throw err
|
||||
})
|
||||
updateTradeEntry(tradeEntry, newEntry, err)
|
||||
.catch(console.log)
|
||||
throw err
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
function updateTradeEntry (tradeEntry, newEntry, err) {
|
||||
const data = mergeTradeEntryAndError(tradeEntry, err)
|
||||
const sql = pgp.helpers.update(data, ['error'], 'trades') + ` WHERE id = ${newEntry.id}`
|
||||
return db.none(sql)
|
||||
.then(() => {
|
||||
throw err
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -19,6 +19,4 @@ const loadConfig = (account) => {
|
|||
return { ...mapped, timeout: 3000 }
|
||||
}
|
||||
|
||||
const loadTradeId = (options, id) => _.assign({}, options)
|
||||
|
||||
module.exports = { loadTradeId, loadConfig, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION }
|
||||
module.exports = { loadConfig, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION }
|
||||
|
|
|
|||
|
|
@ -13,14 +13,15 @@ function trade (side, account, cryptoAtoms, fiatCode, cryptoCode, exchangeName,
|
|||
const exchangeConfig = ALL[exchangeName]
|
||||
if (!exchangeConfig) throw Error('Exchange configuration not found')
|
||||
|
||||
const { loadTradeId, loadOptions, loadConfig = _.noop, REQUIRED_CONFIG_FIELDS, ORDER_TYPE, AMOUNT_PRECISION } = exchangeConfig
|
||||
const { CUSTOM_KEY, loadOptions, loadConfig = _.noop, REQUIRED_CONFIG_FIELDS, ORDER_TYPE, AMOUNT_PRECISION } = exchangeConfig
|
||||
if (!isConfigValid(account, REQUIRED_CONFIG_FIELDS)) throw Error('Invalid config')
|
||||
|
||||
const symbol = buildMarket(fiatCode, cryptoCode, exchangeName)
|
||||
const precision = _.defaultTo(DEFAULT_AMOUNT_PRECISION, AMOUNT_PRECISION)
|
||||
const amount = toUnit(cryptoAtoms, cryptoCode).toFixed(precision)
|
||||
const accountOptions = _.isFunction(loadOptions) ? loadOptions(account) : {}
|
||||
const options = loadTradeId(accountOptions, tradeId)
|
||||
const withCustomKey = CUSTOM_KEY ? { [CUSTOM_KEY]: tradeId } : {}
|
||||
const options = _.assign(accountOptions, withCustomKey)
|
||||
const exchange = new ccxt[exchangeName](loadConfig(account))
|
||||
|
||||
if (ORDER_TYPE === ORDER_TYPES.MARKET) {
|
||||
|
|
|
|||
|
|
@ -20,6 +20,5 @@ const loadConfig = (account) => {
|
|||
return { ...mapped, timeout: 3000 }
|
||||
}
|
||||
const loadOptions = ({ walletId }) => ({ walletId })
|
||||
const loadTradeId = (options, id) => _.assign({}, options)
|
||||
|
||||
module.exports = { loadTradeId, loadOptions, loadConfig, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION }
|
||||
module.exports = { loadOptions, loadConfig, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION }
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ const CRYPTO = [BTC, ETH, LTC, DASH, ZEC, BCH, USDT]
|
|||
const FIAT = ['USD', 'EUR']
|
||||
const AMOUNT_PRECISION = 6
|
||||
const REQUIRED_CONFIG_FIELDS = ['apiKey', 'privateKey']
|
||||
const CUSTOM_KEY = 'userref'
|
||||
|
||||
const loadConfig = (account) => {
|
||||
const mapper = {
|
||||
|
|
@ -19,6 +20,5 @@ const loadConfig = (account) => {
|
|||
}
|
||||
|
||||
const loadOptions = () => ({ expiretm: '+60' })
|
||||
const loadTradeId = (options, id) => _.assign({ userref: id }, options)
|
||||
|
||||
module.exports = { loadTradeId, loadOptions, loadConfig, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION }
|
||||
module.exports = { CUSTOM_KEY, loadOptions, loadConfig, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue