From 5987f288a7784e96809da2eb85275f57037d96a7 Mon Sep 17 00:00:00 2001 From: Josh Harvey Date: Sun, 9 Jul 2017 13:42:31 +0300 Subject: [PATCH] moar coinUtils fixes --- lib/admin/funding.js | 12 ++++++++---- lib/blockchain/install.js | 2 +- lib/plugins.js | 17 +++++++++-------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/lib/admin/funding.js b/lib/admin/funding.js index ac4ff964..9fcd75d3 100644 --- a/lib/admin/funding.js +++ b/lib/admin/funding.js @@ -40,13 +40,15 @@ function fetchMachines () { } function computeCrypto (cryptoCode, _balance) { - const unitScale = coinUtils.coins[cryptoCode].unitScale + const cryptoRec = coinUtils.getCryptoCurrency(cryptoCode) + const unitScale = cryptoRec.unitScale return BN(_balance).shift(-unitScale).round(5) } function computeFiat (rate, cryptoCode, _balance) { - const unitScale = coinUtils.coins[cryptoCode].unitScale + const cryptoRec = coinUtils.getCryptoCurrency(cryptoCode) + const unitScale = cryptoRec.unitScale return BN(_balance).shift(-unitScale).mul(rate).round(5) } @@ -59,9 +61,11 @@ function getFunding (_cryptoCode) { const cryptoCode = _cryptoCode || cryptoCodes[0] const fiatCode = config.fiatCurrency const pareCoins = c => _.includes(c.cryptoCode, cryptoCodes) - const cryptoDisplays = _.filter(pareCoins, coinUtils.cryptoDisplays) + const cryptoCurrencies = coinUtils.cryptoCurrencies() + const cryptoDisplays = _.filter(pareCoins, cryptoCurrencies) + const cryptoRec = coinUtils.getCryptoCurrency(cryptoCode) - if (!coinUtils.coins[cryptoCode]) throw new Error(`Unsupported coin: ${cryptoCode}`) + if (!cryptoRec) throw new Error(`Unsupported coin: ${cryptoCode}`) const promises = [ wallet.newFunding(settings, cryptoCode), diff --git a/lib/blockchain/install.js b/lib/blockchain/install.js index 2f6d1c01..9f851260 100644 --- a/lib/blockchain/install.js +++ b/lib/blockchain/install.js @@ -56,7 +56,7 @@ function processCryptos (codes) { function setupCrypto (crypto) { logger.info(`Installing ${crypto.display}...`) - const cryptoDir = coinUtils.cryptoConfigDir(crypto.cryptoCode) + const cryptoDir = coinUtils.cryptoConfigDir(crypto) makeDir.sync(cryptoDir) const cryptoPlugin = plugin(crypto) const oldDir = process.cwd() diff --git a/lib/plugins.js b/lib/plugins.js index 54db00d8..b7f10484 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -27,8 +27,6 @@ const STALE_BALANCE = 3 * T.minutes const PONG_TTL = '1 week' const tradesQueues = {} -const coins = coinUtils.coins - function plugins (settings, deviceId) { function buildRates (tickers) { const config = configManager.machineScoped(deviceId, settings.config) @@ -172,15 +170,16 @@ function plugins (settings, deviceId) { } function mapCoinSettings (coinParams) { - const coin = coinParams[0] + const cryptoCode = coinParams[0] const cryptoNetwork = coinParams[1] - const config = configManager.scoped(coin, deviceId, settings.config) + const config = configManager.scoped(cryptoCode, deviceId, settings.config) const minimumTx = BN(config.minimumTx) const cashInFee = BN(config.cashInFee) + const cryptoRec = coinUtils.getCryptoCurrency(cryptoCode) return { - cryptoCode: coin, - display: coinUtils.display(coin), + cryptoCode, + display: cryptoRec.display, minimumTx: BN.max(minimumTx, cashInFee), cashInFee: cashInFee, cryptoNetwork @@ -284,8 +283,10 @@ function plugins (settings, deviceId) { const lowBalanceMargin = BN(1) - const unitScale = BN(10).pow(coins[cryptoCode].unitScale) - const fiatTransferBalance = balance.mul(rate.div(unitScale)).div(lowBalanceMargin) + const cryptoRec = coinUtils.getCryptoCurrency(cryptoCode) + const unitScale = cryptoRec.unitScale + const shiftedRate = rate.shift(-unitScale) + const fiatTransferBalance = balance.mul(shiftedRate).div(lowBalanceMargin) return {timestamp: balanceRec.timestamp, balance: fiatTransferBalance.truncated().toString()} })