Skip to content

Commit

Permalink
fix: atempt #3
Browse files Browse the repository at this point in the history
  • Loading branch information
shontzu-deriv committed Jul 4, 2023
1 parent ee5c8a2 commit 090398d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
27 changes: 11 additions & 16 deletions packages/hooks/src/useTotalAccountBalance.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
import useRealTotalAssetCurrency from './useTotalAssetCurrency';
import useExchangeRate from './useExchangeRate';
import { useEffect, useState } from 'react';
import { useStore } from '@deriv/stores';
import { CFD_PLATFORMS } from '../../shared/src/utils/platform';
/**
* we can use this hook to get the total balance of the given accounts list.
* it loops through the accounts list and adds the balance of each account
* to the total balance, it also converts the balance to the currency of the
* first account in the list
*/
const useTotalAccountBalance = (accounts: { balance?: number; currency?: string }[]) => {
const { traders_hub } = useStore();
const { is_demo } = traders_hub;
const total_assets_real_currency = useRealTotalAssetCurrency();
const { getRate } = useExchangeRate();

const [balance, setBalance] = useState(0);
if (!accounts.length) return { balance: 0, currency: total_assets_real_currency };

useEffect(() => {
if (!accounts.length) {
setBalance(0);
return;
}
const balance = accounts.reduce((total, account) => {
const base_rate = getRate(total_assets_real_currency || '');
const rate = is_demo && CFD_PLATFORMS.MT5 ? getRate('USD') : getRate(total_assets_real_currency || '');
const exchange_rate = base_rate / rate;

const updatedBalance = accounts.reduce((total, account) => {
const base_rate = getRate(total_assets_real_currency || '');
const rate = getRate(account.currency || total_assets_real_currency || '');
const exchange_rate = base_rate / rate;
return total + (account.balance || 0) * exchange_rate;
}, 0);

setBalance(updatedBalance);
}, [accounts, getRate, total_assets_real_currency]);
return total + (account.balance || 0) * exchange_rate;
}, 0);

return {
balance,
Expand Down
8 changes: 3 additions & 5 deletions packages/stores/src/providers/ExchangeRatesProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,22 @@ import merge from 'lodash.merge';
import useStore from '../useStore';

const ExchangeRatesProvider = ({ children }: React.PropsWithChildren<unknown>) => {
const { is_loading, data, subscribe } = useSubscription('exchange_rates');
const { data, subscribe } = useSubscription('exchange_rates');
const {
exchange_rates: { update },
} = useStore();

useEffect(() => {
subscribe({ payload: { base_currency: 'USD' } });
}, [subscribe]);

useEffect(() => {
if (!is_loading && data) {
if (data) {
const { exchange_rates } = data;

if (exchange_rates) update(prev => merge(prev, exchange_rates));
}
}, [is_loading, update, data]);
}, [update, data]);

return <>{children}</>;
};

export default ExchangeRatesProvider;

0 comments on commit 090398d

Please sign in to comment.