lamassu-server/lib/exchange.js
2020-05-27 22:43:00 +01:00

40 lines
1.1 KiB
JavaScript

const configManager = require('./new-config-manager')
const ph = require('./plugin-helper')
function lookupExchange (settings, cryptoCode) {
const exchange = configManager.getWalletSettings(cryptoCode, settings.config).exchange
if (exchange === 'no-exchange') return null
return exchange
}
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]
return {exchange, account}
})
}
function buy (settings, cryptoAtoms, fiatCode, cryptoCode) {
return fetchExchange(settings, cryptoCode)
.then(r => r.exchange.buy(r.account, cryptoAtoms, fiatCode, cryptoCode))
}
function sell (settings, cryptoAtoms, fiatCode, cryptoCode) {
return fetchExchange(settings, cryptoCode)
.then(r => r.exchange.sell(r.account, cryptoAtoms, fiatCode, cryptoCode))
}
function active (settings, cryptoCode) {
return !!lookupExchange(settings, cryptoCode)
}
module.exports = {
buy,
sell,
active
}