feat: relay trade uuid to krakens api
This commit is contained in:
parent
bec80ec498
commit
8606d0cc94
6 changed files with 25 additions and 17 deletions
|
|
@ -19,23 +19,23 @@ function fetchExchange (settings, cryptoCode) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function buy (settings, cryptoAtoms, fiatCode, cryptoCode) {
|
function buy (settings, cryptoAtoms, fiatCode, cryptoCode, tradeId) {
|
||||||
return fetchExchange(settings, cryptoCode)
|
return fetchExchange(settings, cryptoCode)
|
||||||
.then(r => {
|
.then(r => {
|
||||||
if (r.exchangeName === 'mock-exchange') {
|
if (r.exchangeName === 'mock-exchange') {
|
||||||
return mockExchange.buy(cryptoAtoms, fiatCode, cryptoCode)
|
return mockExchange.buy(cryptoAtoms, fiatCode, cryptoCode)
|
||||||
}
|
}
|
||||||
return ccxt.trade('buy', r.account, cryptoAtoms, fiatCode, cryptoCode, r.exchangeName)
|
return ccxt.trade('buy', r.account, cryptoAtoms, fiatCode, cryptoCode, r.exchangeName, tradeId)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function sell (settings, cryptoAtoms, fiatCode, cryptoCode) {
|
function sell (settings, cryptoAtoms, fiatCode, cryptoCode, tradeId) {
|
||||||
return fetchExchange(settings, cryptoCode)
|
return fetchExchange(settings, cryptoCode)
|
||||||
.then(r => {
|
.then(r => {
|
||||||
if (r.exchangeName === 'mock-exchange') {
|
if (r.exchangeName === 'mock-exchange') {
|
||||||
return mockExchange.sell(cryptoAtoms, fiatCode, cryptoCode)
|
return mockExchange.sell(cryptoAtoms, fiatCode, cryptoCode)
|
||||||
}
|
}
|
||||||
return ccxt.trade('sell', r.account, cryptoAtoms, fiatCode, cryptoCode, r.exchangeName)
|
return ccxt.trade('sell', r.account, cryptoAtoms, fiatCode, cryptoCode, r.exchangeName, tradeId)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -516,14 +516,18 @@ function plugins (settings, deviceId) {
|
||||||
const tradeEntry = expand(_tradeEntry)
|
const tradeEntry = expand(_tradeEntry)
|
||||||
const execute = tradeEntry.type === 'buy' ? exchange.buy : exchange.sell
|
const execute = tradeEntry.type === 'buy' ? exchange.buy : exchange.sell
|
||||||
|
|
||||||
return execute(settings, tradeEntry.cryptoAtoms, tradeEntry.fiatCode, tradeEntry.cryptoCode)
|
return recordTrade(tradeEntry)
|
||||||
.then(() => recordTrade(tradeEntry))
|
.then(newEntry => {
|
||||||
|
return execute(settings, tradeEntry.cryptoAtoms, tradeEntry.fiatCode, tradeEntry.cryptoCode, newEntry.id)
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
return recordTrade(tradeEntry, err)
|
const data = mergeTradeEntryAndError(tradeEntry, err)
|
||||||
|
const sql = pgp.helpers.update(data, ['error'], 'trades') + ` WHERE id = ${newEntry.id}`
|
||||||
|
return db.none(sql)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
throw err
|
throw err
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function recordTradeAndTx (tradeId, { cashInTxs, cashOutTxs }, dbTx) {
|
function recordTradeAndTx (tradeId, { cashInTxs, cashOutTxs }, dbTx) {
|
||||||
|
|
@ -577,7 +581,6 @@ function plugins (settings, deviceId) {
|
||||||
return t.oneOrNone(sql)
|
return t.oneOrNone(sql)
|
||||||
.then(newTrade => recordTradeAndTx(newTrade.id, _tradeEntry, t))
|
.then(newTrade => recordTradeAndTx(newTrade.id, _tradeEntry, t))
|
||||||
})
|
})
|
||||||
}
|
|
||||||
|
|
||||||
function sendMessage (rec) {
|
function sendMessage (rec) {
|
||||||
const notifications = configManager.getGlobalNotifications(settings.config)
|
const notifications = configManager.getGlobalNotifications(settings.config)
|
||||||
|
|
|
||||||
|
|
@ -19,4 +19,6 @@ const loadConfig = (account) => {
|
||||||
return { ...mapped, timeout: 3000 }
|
return { ...mapped, timeout: 3000 }
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { loadConfig, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION }
|
const loadTradeId = (options, id) => _.assign({}, options)
|
||||||
|
|
||||||
|
module.exports = { loadTradeId, loadConfig, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION }
|
||||||
|
|
|
||||||
|
|
@ -8,18 +8,19 @@ const { ORDER_TYPES } = require('./consts')
|
||||||
const DEFAULT_PRICE_PRECISION = 2
|
const DEFAULT_PRICE_PRECISION = 2
|
||||||
const DEFAULT_AMOUNT_PRECISION = 8
|
const DEFAULT_AMOUNT_PRECISION = 8
|
||||||
|
|
||||||
function trade (side, account, cryptoAtoms, fiatCode, cryptoCode, exchangeName) {
|
function trade (side, account, cryptoAtoms, fiatCode, cryptoCode, exchangeName, tradeId) {
|
||||||
try {
|
try {
|
||||||
const exchangeConfig = ALL[exchangeName]
|
const exchangeConfig = ALL[exchangeName]
|
||||||
if (!exchangeConfig) throw Error('Exchange configuration not found')
|
if (!exchangeConfig) throw Error('Exchange configuration not found')
|
||||||
|
|
||||||
const { loadOptions, loadConfig = _.noop, REQUIRED_CONFIG_FIELDS, ORDER_TYPE, AMOUNT_PRECISION } = exchangeConfig
|
const { loadTradeId, loadOptions, loadConfig = _.noop, REQUIRED_CONFIG_FIELDS, ORDER_TYPE, AMOUNT_PRECISION } = exchangeConfig
|
||||||
if (!isConfigValid(account, REQUIRED_CONFIG_FIELDS)) throw Error('Invalid config')
|
if (!isConfigValid(account, REQUIRED_CONFIG_FIELDS)) throw Error('Invalid config')
|
||||||
|
|
||||||
const symbol = buildMarket(fiatCode, cryptoCode, exchangeName)
|
const symbol = buildMarket(fiatCode, cryptoCode, exchangeName)
|
||||||
const precision = _.defaultTo(DEFAULT_AMOUNT_PRECISION, AMOUNT_PRECISION)
|
const precision = _.defaultTo(DEFAULT_AMOUNT_PRECISION, AMOUNT_PRECISION)
|
||||||
const amount = toUnit(cryptoAtoms, cryptoCode).toFixed(precision)
|
const amount = toUnit(cryptoAtoms, cryptoCode).toFixed(precision)
|
||||||
const options = _.isFunction(loadOptions) ? loadOptions(account) : {}
|
const accountOptions = _.isFunction(loadOptions) ? loadOptions(account) : {}
|
||||||
|
const options = loadTradeId(accountOptions, tradeId)
|
||||||
const exchange = new ccxt[exchangeName](loadConfig(account))
|
const exchange = new ccxt[exchangeName](loadConfig(account))
|
||||||
|
|
||||||
if (ORDER_TYPE === ORDER_TYPES.MARKET) {
|
if (ORDER_TYPE === ORDER_TYPES.MARKET) {
|
||||||
|
|
|
||||||
|
|
@ -20,5 +20,6 @@ const loadConfig = (account) => {
|
||||||
return { ...mapped, timeout: 3000 }
|
return { ...mapped, timeout: 3000 }
|
||||||
}
|
}
|
||||||
const loadOptions = ({ walletId }) => ({ walletId })
|
const loadOptions = ({ walletId }) => ({ walletId })
|
||||||
|
const loadTradeId = (options, id) => _.assign({}, options)
|
||||||
|
|
||||||
module.exports = { loadOptions, loadConfig, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION }
|
module.exports = { loadTradeId, loadOptions, loadConfig, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION }
|
||||||
|
|
|
||||||
|
|
@ -19,5 +19,6 @@ const loadConfig = (account) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const loadOptions = () => ({ expiretm: '+60' })
|
const loadOptions = () => ({ expiretm: '+60' })
|
||||||
|
const loadTradeId = (options, id) => _.assign({ userref: id }, options)
|
||||||
|
|
||||||
module.exports = { loadOptions, loadConfig, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION }
|
module.exports = { loadTradeId, loadOptions, loadConfig, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION }
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue