Skip to content

Commit

Permalink
Hirad/ Account switcher demo total asset change (#5622)
Browse files Browse the repository at this point in the history
* Account switcher total asset is shown correctly

* Changed currency that is shown

* fix--api call is put in useEffect now

* Made the condition shorter

* Removed the redundant parameter in the exchange_rates call

* Pulled from master

* Pulled from master

* Added currency exchange to real account tab of account switcher
  • Loading branch information
hirad-deriv committed Aug 15, 2022
1 parent 3e5a645 commit 23c6946
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand All @@ -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({
Expand Down Expand Up @@ -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 }
Expand All @@ -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;
Expand Down Expand Up @@ -903,9 +919,9 @@ const AccountSwitcher = props => {
</Text>
<Text size='xs' color='prominent' className='acc-switcher__balance'>
<Money
currency={isRealAccountTab ? props.obj_total_balance.currency : 'USD'}
currency={props.obj_total_balance.currency}
amount={formatMoney(
isRealAccountTab ? props.obj_total_balance.currency : 'USD',
props.obj_total_balance.currency,
isRealAccountTab ? getTotalRealAssets() : getTotalDemoAssets(),
true
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import BinarySocket from '_common/base/socket_base';

export const getExchangeRate = async (from_currency, to_currency) => {
const { exchange_rates } = await BinarySocket.exchange_rates(from_currency);

return exchange_rates.rates[to_currency];
};
1 change: 1 addition & 0 deletions packages/core/src/Utils/ExchangeCurrencyRate/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './exchange_currency_rate';
3 changes: 3 additions & 0 deletions packages/core/src/_common/base/socket_base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });

Expand Down Expand Up @@ -424,6 +426,7 @@ const BinarySocketBase = (() => {
buyAndSubscribe,
sell,
cashier,
exchange_rates,
cashierPayments,
subscribeCashierPayments,
cancelCryptoTransaction,
Expand Down

1 comment on commit 23c6946

@vercel
Copy link

@vercel vercel bot commented on 23c6946 Aug 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

deriv-app – ./

binary.sx
deriv-app.vercel.app
deriv-app.binary.sx
deriv-app-git-master.binary.sx

Please sign in to comment.