Skip to content

Commit

Permalink
shontzu/WALL-865/Incorrect-total-asset-amount (binary-com#9217)
Browse files Browse the repository at this point in the history
* fix: attemp #3

* chore: remove irrelevant check

* chore: unit test

* chore: unit test fail

* chore: fixes based on review

* chore: empty commit

* chore: code revie fixes

* chore: revert test case changes

* chore: rename type to TUseTotalAccountBalance

* chore: format doc and added test case for multiple accounts in different currencies

---------

Co-authored-by: Ali(Ako) Hosseini <ali.hosseini@deriv.com>
  • Loading branch information
shontzu-deriv and ali-hosseini-deriv committed Jul 21, 2023
1 parent 8623b8a commit 7b4ba8f
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
79 changes: 79 additions & 0 deletions packages/hooks/src/__tests__/useTotalAccountBalance.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,85 @@ describe('useTotalAccountBalance', () => {
expect(result.current.balance).toBe(0);
});

test('should return total balance correctly when user has one account', () => {
const mock = mockStore({
client: {
active_accounts: [
{
balance: 10000,
currency: 'USD',
account_type: 'real',
},
],
},
});

const wrapper = ({ children }: { children: JSX.Element }) => (
<StoreProvider store={mock}>{children}</StoreProvider>
);
const { result } = renderHook(() => useTotalAccountBalance(mock.client.active_accounts), { wrapper });
expect(result.current.balance).toBe(10000);
});

test('should return total balance correctly when user has multiple accounts in same currency', () => {
const mock = mockStore({
client: {
active_accounts: [
{
balance: 10000,
currency: 'USD',
account_type: 'demo',
},
{
balance: 10000,
currency: 'USD',
account_type: 'demo',
},
],
},
});

const wrapper = ({ children }: { children: JSX.Element }) => (
<StoreProvider store={mock}>{children}</StoreProvider>
);
const { result } = renderHook(() => useTotalAccountBalance(mock.client.active_accounts), { wrapper });
expect(result.current.balance).toBe(20000);
});

test('should return total balance correctly when user has multiple accounts in different currencies', () => {
const mock = mockStore({
exchange_rates: {
data: {
rates: {
EUR: 2,
USD: 1,
},
},
},
client: {
active_accounts: [
{
balance: 10000,
currency: 'USD',
account_type: 'demo',
},
{
balance: 10000,
currency: 'EUR',
account_type: 'demo',
},
],
},
});

const wrapper = ({ children }: { children: JSX.Element }) => (
<StoreProvider store={mock}>{children}</StoreProvider>
);
const { result } = renderHook(() => useTotalAccountBalance(mock.client.active_accounts), { wrapper });
expect(result.current.balance).not.toBe(20000);
expect(result.current.balance).toBe(15000);
});

test('should return total balance correctly when user has multiple accounts', async () => {
const mock = mockStore({
exchange_rates: {
Expand Down
10 changes: 7 additions & 3 deletions packages/hooks/src/useTotalAccountBalance.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import useRealTotalAssetCurrency from './useTotalAssetCurrency';
import useExchangeRate from './useExchangeRate';

/**
* 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
*/
type TUseTotalAccountBalance = {
balance?: number;
currency?: string;
account_type?: string;
};

const useTotalAccountBalance = (accounts: { balance?: number; currency?: string }[]) => {
const useTotalAccountBalance = (accounts: TUseTotalAccountBalance[]) => {
const total_assets_real_currency = useRealTotalAssetCurrency();
const { getRate } = useExchangeRate();

if (!accounts.length) return { balance: 0, currency: total_assets_real_currency };

const balance = accounts.reduce((total, account) => {
const base_rate = getRate(total_assets_real_currency || '');
const base_rate = account?.account_type === 'demo' ? 1 : getRate(total_assets_real_currency || '');
const rate = getRate(account.currency || total_assets_real_currency || '');
const exchange_rate = base_rate / rate;

Expand Down

0 comments on commit 7b4ba8f

Please sign in to comment.