Refactored some exchange code

This commit is contained in:
José Oliveira 2021-01-21 11:08:20 +00:00 committed by Josh Harvey
parent 54a231ab60
commit 134eaaa518
20 changed files with 141 additions and 643 deletions

View file

@ -1,5 +1,5 @@
const configManager = require('./new-config-manager')
const ph = require('./plugin-helper')
const ccxt = require('./plugins/exchange/ccxt')
function lookupExchange (settings, cryptoCode) {
const exchange = configManager.getWalletSettings(cryptoCode, settings.config).exchange
@ -10,23 +10,22 @@ function lookupExchange (settings, cryptoCode) {
function fetchExchange (settings, cryptoCode) {
return Promise.resolve()
.then(() => {
const plugin = lookupExchange(settings, cryptoCode)
if (!plugin) throw new Error('No exchange set')
const exchange = ph.load(ph.EXCHANGE, plugin)
const account = settings.accounts[plugin]
const exchangeName = lookupExchange(settings, cryptoCode)
if (!exchangeName) throw new Error('No exchange set')
const account = settings.accounts[exchangeName]
return {exchange, account}
return { exchangeName, account }
})
}
function buy (settings, cryptoAtoms, fiatCode, cryptoCode) {
return fetchExchange(settings, cryptoCode)
.then(r => r.exchange.buy(r.account, cryptoAtoms, fiatCode, cryptoCode))
.then(r => ccxt.trade('buy', r.account, cryptoAtoms, fiatCode, cryptoCode, r.exchangeName))
}
function sell (settings, cryptoAtoms, fiatCode, cryptoCode) {
return fetchExchange(settings, cryptoCode)
.then(r => r.exchange.sell(r.account, cryptoAtoms, fiatCode, cryptoCode))
.then(r => ccxt.trade('sell', r.account, cryptoAtoms, fiatCode, cryptoCode, r.exchangeName))
}
function active (settings, cryptoCode) {