Skip to content
This repository was archived by the owner on Feb 22, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/botPage/bot/TradeEngine/Balance.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -11,26 +10,30 @@ 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 });
});
}
// 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);
}
};
11 changes: 9 additions & 2 deletions src/botPage/bot/TradeEngine/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions src/botPage/view/View.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ api.events.on('balance', response => {
const element = elTopMenuBalance;
element.textContent = `${balance} ${currency}`;
});

globalObserver.setState({ balance: b, currency });
});

const addBalanceForToken = token => {
Expand Down