diff --git a/packages/core/src/App/Containers/AccountSwitcher/account-switcher.jsx b/packages/core/src/App/Containers/AccountSwitcher/account-switcher.jsx index 02c4e41533c0..7e4b96cbbe0b 100644 --- a/packages/core/src/App/Containers/AccountSwitcher/account-switcher.jsx +++ b/packages/core/src/App/Containers/AccountSwitcher/account-switcher.jsx @@ -25,6 +25,7 @@ import { import { localize, Localize } from '@deriv/translations'; import { getAccountTitle } from 'App/Containers/RealAccountSignup/helpers/constants'; import { connect } from 'Stores/connect'; +import { getExchangeRate } from 'Utils/ExchangeCurrencyRate/exchange_currency_rate'; import { AccountsItemLoader } from 'App/Components/Layout/Header/Components/Preloader'; import AccountList from './account-switcher-account-list.jsx'; import AccountWrapper from './account-switcher-account-wrapper.jsx'; @@ -40,6 +41,7 @@ const AccountSwitcher = props => { const [is_dmt5_real_visible, setDmt5RealVisible] = React.useState(true); const [is_dxtrade_demo_visible, setDxtradeDemoVisible] = React.useState(true); const [is_dxtrade_real_visible, setDxtradeRealVisible] = React.useState(true); + const [exchanged_rate, setExchangedRate] = React.useState(''); const wrapper_ref = React.useRef(); const scroll_ref = React.useRef(null); @@ -53,6 +55,14 @@ const AccountSwitcher = props => { } }, [getMaxAccountsDisplayed]); + React.useEffect(() => { + const vrtc_loginid = props.account_list.find(account => account.is_virtual).loginid; + + getExchangeRate(props.accounts[vrtc_loginid].currency, props.obj_total_balance.currency).then(res => + setExchangedRate(res) + ); + }, []); + React.useEffect(() => { if (scroll_ref.current && (is_dmt5_real_visible || is_dxtrade_real_visible)) { scroll_ref.current.scrollIntoView({ @@ -326,7 +336,12 @@ const AccountSwitcher = props => { .filter(account => (is_demo ? isDemo(account) : !isDemo(account))) .reduce( (total, account) => { - total.balance += account.balance; + const real_account_loginid = props.account_list.find(acc => !acc.is_virtual).loginid; + if (!is_demo && props.accounts[real_account_loginid].currency !== account.currency) { + total.balance += account.balance * exchanged_rate; + } else { + total.balance += account.balance; + } return total; }, { balance: 0 } @@ -336,10 +351,11 @@ const AccountSwitcher = props => { const getTotalDemoAssets = () => { const vrtc_loginid = props.account_list.find(account => account.is_virtual).loginid; const vrtc_balance = props.accounts[vrtc_loginid] ? props.accounts[vrtc_loginid].balance : 0; + const vrtc_currency = props.accounts[vrtc_loginid] ? props.accounts[vrtc_loginid].currency : 'USD'; const mt5_demo_total = getTotalBalance(props.mt5_login_list); const dxtrade_demo_total = getTotalBalance(props.dxtrade_accounts_list); - let total = vrtc_balance; + let total = vrtc_currency !== props.obj_total_balance.currency ? vrtc_balance * exchanged_rate : vrtc_balance; if (Array.isArray(props.mt5_login_list)) { total += mt5_demo_total.balance; @@ -903,9 +919,9 @@ const AccountSwitcher = props => { { + const { exchange_rates } = await BinarySocket.exchange_rates(from_currency); + + return exchange_rates.rates[to_currency]; +}; diff --git a/packages/core/src/Utils/ExchangeCurrencyRate/index.js b/packages/core/src/Utils/ExchangeCurrencyRate/index.js new file mode 100644 index 000000000000..3a92b58cd082 --- /dev/null +++ b/packages/core/src/Utils/ExchangeCurrencyRate/index.js @@ -0,0 +1 @@ +export * from './exchange_currency_rate'; diff --git a/packages/core/src/_common/base/socket_base.js b/packages/core/src/_common/base/socket_base.js index dbbe51b22302..740f41415e5b 100644 --- a/packages/core/src/_common/base/socket_base.js +++ b/packages/core/src/_common/base/socket_base.js @@ -180,6 +180,8 @@ const BinarySocketBase = (() => { const cashier = (action, parameters = {}) => deriv_api.send({ cashier: action, ...parameters }); + const exchange_rates = from_currency => deriv_api.send({ exchange_rates: 1, base_currency: from_currency }); + const cashierPayments = ({ provider, transaction_type }) => deriv_api.send({ cashier_payments: 1, provider, transaction_type }); @@ -424,6 +426,7 @@ const BinarySocketBase = (() => { buyAndSubscribe, sell, cashier, + exchange_rates, cashierPayments, subscribeCashierPayments, cancelCryptoTransaction,