chore: server code formatting
This commit is contained in:
parent
aedabcbdee
commit
68517170e2
234 changed files with 9824 additions and 6195 deletions
|
|
@ -1,51 +1,53 @@
|
|||
const { COINS } = require('@lamassu/coins')
|
||||
const _ = require('lodash/fp')
|
||||
const { utils: coinUtils } = require('@lamassu/coins')
|
||||
|
||||
const kraken = require('../exchange/kraken')
|
||||
const bitstamp = require('../exchange/bitstamp')
|
||||
const itbit = require('../exchange/itbit')
|
||||
const binanceus = require('../exchange/binanceus')
|
||||
const cex = require('../exchange/cex')
|
||||
const bitpay = require('../ticker/bitpay')
|
||||
const binance = require('../exchange/binance')
|
||||
const bitfinex = require('../exchange/bitfinex')
|
||||
const logger = require('../../logger')
|
||||
|
||||
const { BTC, BCH, DASH, ETH, LTC, ZEC, USDT, TRX, USDT_TRON, LN, USDC } = COINS
|
||||
|
||||
const ALL = {
|
||||
cex: cex,
|
||||
binanceus: binanceus,
|
||||
kraken: kraken,
|
||||
bitstamp: bitstamp,
|
||||
itbit: itbit,
|
||||
bitpay: bitpay,
|
||||
binance: binance,
|
||||
bitfinex: bitfinex
|
||||
}
|
||||
|
||||
function buildMarket (fiatCode, cryptoCode, serviceName) {
|
||||
if (!_.includes(cryptoCode, ALL[serviceName].CRYPTO)) {
|
||||
throw new Error('Unsupported crypto: ' + cryptoCode)
|
||||
}
|
||||
|
||||
if (_.isNil(fiatCode)) throw new Error('Market pair building failed: Missing fiat code')
|
||||
return cryptoCode + '/' + fiatCode
|
||||
}
|
||||
|
||||
function verifyFiatSupport (fiatCode, serviceName) {
|
||||
const fiat = ALL[serviceName].FIAT
|
||||
return fiat === 'ALL_CURRENCIES' ? true : _.includes(fiatCode, fiat)
|
||||
}
|
||||
|
||||
function isConfigValid (config, fields) {
|
||||
const values = _.map(it => _.get(it)(config))(fields)
|
||||
return _.every(it => it || it === 0)(values)
|
||||
}
|
||||
|
||||
function defaultFiatMarket (serviceName) {
|
||||
return ALL[serviceName].DEFAULT_FIAT_MARKET
|
||||
}
|
||||
|
||||
module.exports = { buildMarket, ALL, verifyFiatSupport, isConfigValid, defaultFiatMarket }
|
||||
const _ = require('lodash/fp')
|
||||
|
||||
const kraken = require('../exchange/kraken')
|
||||
const bitstamp = require('../exchange/bitstamp')
|
||||
const itbit = require('../exchange/itbit')
|
||||
const binanceus = require('../exchange/binanceus')
|
||||
const cex = require('../exchange/cex')
|
||||
const bitpay = require('../ticker/bitpay')
|
||||
const binance = require('../exchange/binance')
|
||||
const bitfinex = require('../exchange/bitfinex')
|
||||
|
||||
const ALL = {
|
||||
cex: cex,
|
||||
binanceus: binanceus,
|
||||
kraken: kraken,
|
||||
bitstamp: bitstamp,
|
||||
itbit: itbit,
|
||||
bitpay: bitpay,
|
||||
binance: binance,
|
||||
bitfinex: bitfinex,
|
||||
}
|
||||
|
||||
function buildMarket(fiatCode, cryptoCode, serviceName) {
|
||||
if (!_.includes(cryptoCode, ALL[serviceName].CRYPTO)) {
|
||||
throw new Error('Unsupported crypto: ' + cryptoCode)
|
||||
}
|
||||
|
||||
if (_.isNil(fiatCode))
|
||||
throw new Error('Market pair building failed: Missing fiat code')
|
||||
return cryptoCode + '/' + fiatCode
|
||||
}
|
||||
|
||||
function verifyFiatSupport(fiatCode, serviceName) {
|
||||
const fiat = ALL[serviceName].FIAT
|
||||
return fiat === 'ALL_CURRENCIES' ? true : _.includes(fiatCode, fiat)
|
||||
}
|
||||
|
||||
function isConfigValid(config, fields) {
|
||||
const values = _.map(it => _.get(it)(config))(fields)
|
||||
return _.every(it => it || it === 0)(values)
|
||||
}
|
||||
|
||||
function defaultFiatMarket(serviceName) {
|
||||
return ALL[serviceName].DEFAULT_FIAT_MARKET
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
buildMarket,
|
||||
ALL,
|
||||
verifyFiatSupport,
|
||||
isConfigValid,
|
||||
defaultFiatMarket,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,17 +7,19 @@ const request = require('request-promise')
|
|||
const { utils: coinUtils } = require('@lamassu/coins')
|
||||
|
||||
const logger = require('../../logger')
|
||||
const { isRemoteNode, isRemoteWallet } = require('../../environment-helper')
|
||||
const { isRemoteWallet } = require('../../environment-helper')
|
||||
const { isEnvironmentValid } = require('../../blockchain/install')
|
||||
|
||||
const BLOCKCHAIN_DIR = process.env.BLOCKCHAIN_DIR
|
||||
|
||||
|
||||
module.exports = {
|
||||
fetch, fetchDigest, parseConf, rpcConfig
|
||||
fetch,
|
||||
fetchDigest,
|
||||
parseConf,
|
||||
rpcConfig,
|
||||
}
|
||||
|
||||
function fetch (account = {}, method, params) {
|
||||
function fetch(account = {}, method, params) {
|
||||
params = _.defaultTo([], params)
|
||||
|
||||
return Promise.resolve(true)
|
||||
|
|
@ -25,18 +27,22 @@ function fetch (account = {}, method, params) {
|
|||
const data = {
|
||||
method,
|
||||
params,
|
||||
id: uuid.v4()
|
||||
id: uuid.v4(),
|
||||
}
|
||||
|
||||
if (_.isNil(account.port)) throw new Error('port attribute required for jsonRpc')
|
||||
if (_.isNil(account.port))
|
||||
throw new Error('port attribute required for jsonRpc')
|
||||
|
||||
const url = _.defaultTo(`http://${account.host}:${account.port}`, account.url)
|
||||
const url = _.defaultTo(
|
||||
`http://${account.host}:${account.port}`,
|
||||
account.url,
|
||||
)
|
||||
|
||||
return axios({
|
||||
method: 'post',
|
||||
auth: {username: account.username, password: account.password},
|
||||
auth: { username: account.username, password: account.password },
|
||||
url,
|
||||
data
|
||||
data,
|
||||
})
|
||||
})
|
||||
.then(r => {
|
||||
|
|
@ -44,17 +50,19 @@ function fetch (account = {}, method, params) {
|
|||
return r.data.result
|
||||
})
|
||||
.catch(err => {
|
||||
throw new Error(JSON.stringify({
|
||||
responseMessage: _.get('message', err),
|
||||
message: _.get('response.data.error.message', err),
|
||||
code: _.get('response.data.error.code', err)
|
||||
}))
|
||||
throw new Error(
|
||||
JSON.stringify({
|
||||
responseMessage: _.get('message', err),
|
||||
message: _.get('response.data.error.message', err),
|
||||
code: _.get('response.data.error.code', err),
|
||||
}),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
function generateDigestOptions (account = {}, method, params) {
|
||||
function generateDigestOptions(account = {}, method, params) {
|
||||
const headers = {
|
||||
'Content-Type': 'application/json'
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
|
||||
const dataString = `{"jsonrpc":"2.0","id":"${uuid.v4()}","method":"${method}","params":${JSON.stringify(params)}}`
|
||||
|
|
@ -68,31 +76,30 @@ function generateDigestOptions (account = {}, method, params) {
|
|||
auth: {
|
||||
user: account.username,
|
||||
pass: account.password,
|
||||
sendImmediately: false
|
||||
}
|
||||
sendImmediately: false,
|
||||
},
|
||||
}
|
||||
|
||||
return options
|
||||
}
|
||||
|
||||
function fetchDigest(account = {}, method, params = []) {
|
||||
return Promise.resolve(true)
|
||||
.then(() => {
|
||||
if (_.isNil(account.port))
|
||||
throw new Error('port attribute required for jsonRpc')
|
||||
return Promise.resolve(true).then(() => {
|
||||
if (_.isNil(account.port))
|
||||
throw new Error('port attribute required for jsonRpc')
|
||||
|
||||
const options = generateDigestOptions(account, method, params)
|
||||
return request(options)
|
||||
})
|
||||
const options = generateDigestOptions(account, method, params)
|
||||
return request(options)
|
||||
})
|
||||
}
|
||||
|
||||
function split (str) {
|
||||
function split(str) {
|
||||
const i = str.indexOf('=')
|
||||
if (i === -1) return []
|
||||
return [str.slice(0, i), str.slice(i + 1)]
|
||||
}
|
||||
|
||||
function parseConf (confPath) {
|
||||
function parseConf(confPath) {
|
||||
const conf = fs.readFileSync(confPath)
|
||||
const lines = conf.toString().split('\n')
|
||||
|
||||
|
|
@ -109,34 +116,36 @@ function parseConf (confPath) {
|
|||
return res
|
||||
}
|
||||
|
||||
function rpcConfig (cryptoRec) {
|
||||
function rpcConfig(cryptoRec) {
|
||||
try {
|
||||
if (isRemoteWallet(cryptoRec) && isEnvironmentValid(cryptoRec)) {
|
||||
return {
|
||||
username: process.env[`${cryptoRec.cryptoCode}_NODE_USER`],
|
||||
password: process.env[`${cryptoRec.cryptoCode}_NODE_PASSWORD`],
|
||||
host: process.env[`${cryptoRec.cryptoCode}_NODE_RPC_HOST`],
|
||||
port: process.env[`${cryptoRec.cryptoCode}_NODE_RPC_PORT`]
|
||||
port: process.env[`${cryptoRec.cryptoCode}_NODE_RPC_PORT`],
|
||||
}
|
||||
}
|
||||
|
||||
const configPath = coinUtils.configPath(cryptoRec, BLOCKCHAIN_DIR)
|
||||
const config = parseConf(configPath)
|
||||
|
||||
|
||||
return {
|
||||
username: config.rpcuser,
|
||||
password: config.rpcpassword,
|
||||
host: 'localhost',
|
||||
port: config.rpcport || cryptoRec.defaultPort
|
||||
port: config.rpcport || cryptoRec.defaultPort,
|
||||
}
|
||||
} catch (err) {
|
||||
if (!isEnvironmentValid(cryptoRec)) {
|
||||
logger.error('Environment is not correctly setup for remote wallet usage!')
|
||||
logger.error(
|
||||
`Environment is not correctly setup for remote wallet usage!, ${err}`,
|
||||
)
|
||||
} else {
|
||||
logger.error('Wallet is currently not installed!')
|
||||
}
|
||||
return {
|
||||
port: cryptoRec.defaultPort
|
||||
port: cryptoRec.defaultPort,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue