From 49f77c50b84438c5a86e0bc16933479ada3212fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Ma=C5=82ecki?= Date: Mon, 21 Apr 2014 00:36:47 +0200 Subject: [PATCH] Bring back trading to `fiatBalance` --- lib/trader.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/trader.js b/lib/trader.js index 610ce8f4..8c6d7301 100644 --- a/lib/trader.js +++ b/lib/trader.js @@ -116,7 +116,21 @@ Trader.prototype.fiatBalance = function (transferSatoshis, tradeFiat) { // [ $ ] = [ $ ] var fiatTransferBalance = ((adjustedTransferBalance / SATOSHI_FACTOR) * rate) / lowBalanceMargin; - return fiatTransferBalance; + // If this server is also configured to trade received fiat for Bitcoins, + // we also need to calculate if we have enough funds on our trade exchange. + if (balance.tradeBalance === null) return fiatTransferBalance; + var tradeBalance = balance.tradeBalance; + + // We need to secure `tradeFiat` (amount of fiat in this transaction) and + // enough fiat to cover our trading queue (trades aren't executed immediately). + var adjustedFiat = tradeFiat + this.tradeQueueFiatBalance(rate); + + // So we subtract `adjustedFiat` from `tradeBalance` and again, apply + // `lowBalanceMargin`. + var fiatTradeBalance = (tradeBalance - adjustedFiat) / lowBalanceMargin; + + // And we return the smallest number. + return Math.min(fiatTransferBalance, fiatTradeBalance); }; Trader.prototype.sendBitcoins = function (deviceFingerprint, tx, cb) {