import { roundBalance } from '../../common/tools';
import { info } from '../broadcast';
import { observer as globalObserver } from '../../../common/utils/observer';

export default Engine =>
    class Balance extends Engine {
        observeBalance() {
            this.listen('balance', r => {
                const {
                    balance: { balance: b, currency },
                } = r;

                const balance = roundBalance({ currency, balance: b });
                const balanceStr = `${balance} ${currency}`;

                globalObserver.setState({ balance, currency });

                info({ accountID: this.accountInfo.loginid, balance: balanceStr });
            });
        }
        // eslint-disable-next-line class-methods-use-this
        getBalance(type) {
            const { scope } = this.store.getState();
            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({
                    balance: Number(balance) - this.tradeOptions.amount,
                    currency,
                });
            }

            const balanceStr = `${balance}`;

            return type === 'STR' ? balanceStr : Number(balance);
        }
    };

@@ -808,485 +808,53 @@
 nst 
-%7B scope %7D = this.store.getState();%0A            const currency = globalObserver.getState('currency');%0A            let balance = globalObserver.getState('balance');%0A%0A            // Deduct trade %60amount%60 in this scope for correct value in %60balance%60-block%0A            if (scope === 'BEFORE_PURCHASE') %7B%0A                balance = roundBalance(%7B%0A                    balance: Number(balance) - this.tradeOptions.amount,%0A                    currency,%0A                %7D);%0A            %7D%0A
+balance = globalObserver.getState('balance');
 %0A   
@@ -891,25 +891,24 @@
 %7Bbalance%7D%60;%0A
-%0A
             

Remove deducting trade amount prematurely