feat: relay trade uuid to krakens api

This commit is contained in:
José Oliveira 2021-04-19 21:00:56 +01:00 committed by Josh Harvey
parent bec80ec498
commit 8606d0cc94
6 changed files with 25 additions and 17 deletions

View file

@ -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)
}) })
} }

View file

@ -516,12 +516,16 @@ 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 => {
.catch(err => { return execute(settings, tradeEntry.cryptoAtoms, tradeEntry.fiatCode, tradeEntry.cryptoCode, newEntry.id)
return recordTrade(tradeEntry, err) .catch(err => {
.then(() => { const data = mergeTradeEntryAndError(tradeEntry, err)
throw err const sql = pgp.helpers.update(data, ['error'], 'trades') + ` WHERE id = ${newEntry.id}`
return db.none(sql)
.then(() => {
throw err
})
}) })
}) })
} }
@ -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)

View file

@ -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 }

View file

@ -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) {

View file

@ -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 }

View file

@ -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 }