refactor: add trade id to trade entry object

This commit is contained in:
José Oliveira 2021-06-29 13:59:37 +01:00 committed by Josh Harvey
parent 700244e45b
commit 9b4bae4097
3 changed files with 10 additions and 8 deletions

View file

@ -19,25 +19,25 @@ function fetchExchange (settings, cryptoCode) {
})
}
function buy (settings, tradeEntry, tradeId) {
function buy (settings, tradeEntry) {
const { cryptoAtoms, fiatCode, cryptoCode } = tradeEntry
return fetchExchange(settings, cryptoCode)
.then(r => {
if (r.exchangeName === 'mock-exchange') {
return mockExchange.buy(cryptoAtoms, fiatCode, cryptoCode)
}
return ccxt.trade('buy', r.account, cryptoAtoms, fiatCode, cryptoCode, r.exchangeName, tradeId)
return ccxt.trade('buy', r.account, tradeEntry, r.exchangeName)
})
}
function sell (settings, tradeEntry, tradeId) {
function sell (settings, tradeEntry) {
const { cryptoAtoms, fiatCode, cryptoCode } = tradeEntry
return fetchExchange(settings, cryptoCode)
.then(r => {
if (r.exchangeName === 'mock-exchange') {
return mockExchange.sell(cryptoAtoms, fiatCode, cryptoCode)
}
return ccxt.trade('sell', r.account, cryptoAtoms, fiatCode, cryptoCode, r.exchangeName, tradeId)
return ccxt.trade('sell', r.account, tradeEntry, r.exchangeName)
})
}

View file

@ -517,8 +517,9 @@ function plugins (settings, deviceId) {
const execute = tradeEntry.type === 'buy' ? exchange.buy : exchange.sell
return recordTrade(tradeEntry)
.then(newEntry =>
execute(settings, tradeEntry, newEntry.id)
.then(newEntry => {
tradeEntry.tradeId = newEntry.id
return execute(settings, tradeEntry)
.catch(err => {
updateTradeEntry(tradeEntry, newEntry, err)
.then(() => {
@ -526,7 +527,7 @@ function plugins (settings, deviceId) {
throw err
})
})
)
})
}
function updateTradeEntry (tradeEntry, newEntry, err) {

View file

@ -8,7 +8,8 @@ const { ORDER_TYPES } = require('./consts')
const DEFAULT_PRICE_PRECISION = 2
const DEFAULT_AMOUNT_PRECISION = 8
function trade (side, account, cryptoAtoms, fiatCode, cryptoCode, exchangeName, tradeId) {
function trade (side, account, tradeEntry, exchangeName) {
const { cryptoAtoms, fiatCode, cryptoCode, tradeId } = tradeEntry
try {
const exchangeConfig = ALL[exchangeName]
if (!exchangeConfig) throw Error('Exchange configuration not found')