diff --git a/src/botPage/bot/TradeEngine/Balance.js b/src/botPage/bot/TradeEngine/Balance.js index 58ea161e25..8730573c2d 100644 --- a/src/botPage/bot/TradeEngine/Balance.js +++ b/src/botPage/bot/TradeEngine/Balance.js @@ -1,7 +1,6 @@ import { roundBalance } from '../../common/tools'; import { info } from '../broadcast'; - -let balanceStr = ''; +import { observer as globalObserver } from '../../../common/utils/observer'; export default Engine => class Balance extends Engine { @@ -11,8 +10,10 @@ export default Engine => balance: { balance: b, currency }, } = r; - this.balance = roundBalance({ currency, balance: b }); - balanceStr = `${this.balance} ${currency}`; + const balance = roundBalance({ currency, balance: b }); + const balanceStr = `${balance} ${currency}`; + + globalObserver.setState({ balance, currency }); info({ accountID: this.accountInfo.loginid, balance: balanceStr }); }); @@ -20,17 +21,19 @@ export default Engine => // eslint-disable-next-line class-methods-use-this getBalance(type) { const { scope } = this.store.getState(); - let { balance } = this; + const currency = globalObserver.getState('currency'); + let balance = globalObserver.getState('balance'); // Deduct trade `amount` in this scope for correct value in `balance`-block if (scope === 'BEFORE_PURCHASE') { balance = roundBalance({ - currency: this.tradeOptions.currency, - balance : Number(balance) - this.tradeOptions.amount, + balance: Number(balance) - this.tradeOptions.amount, + currency, }); - balanceStr = `${balance} ${this.tradeOptions.currency}`; } + const balanceStr = `${balance}`; + return type === 'STR' ? balanceStr : Number(balance); } }; diff --git a/src/botPage/bot/TradeEngine/index.js b/src/botPage/bot/TradeEngine/index.js index f93ffbd8ce..a9a2e03ea2 100644 --- a/src/botPage/bot/TradeEngine/index.js +++ b/src/botPage/bot/TradeEngine/index.js @@ -118,8 +118,15 @@ export default class TradeEngine extends Balance(Purchase(Sell(OpenContract(Prop // Only subscribe to balance in browser, not for tests. if (document) { - this.api.subscribeToBalance().then(r => { - this.balance = Number(r.balance.balance); + this.api.subscribeToBalance().then(response => { + const { + balance: { balance, currency }, + } = response; + + globalObserver.setState({ + balance: Number(balance), + currency, + }); resolve(); }); } else { diff --git a/src/botPage/view/View.js b/src/botPage/view/View.js index b96e79b9d1..01fbc666f8 100644 --- a/src/botPage/view/View.js +++ b/src/botPage/view/View.js @@ -76,6 +76,8 @@ api.events.on('balance', response => { const element = elTopMenuBalance; element.textContent = `${balance} ${currency}`; }); + + globalObserver.setState({ balance: b, currency }); }); const addBalanceForToken = token => {