Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shontzu/WALL-865/Incorrect-total-asset-amount #9217

Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
362602e
fix: attemp #3
shontzu-deriv Jul 4, 2023
c84aaba
Merge branch 'master' into shontzu/WALL-865/Incorrect-total-asset-amount
shontzu-deriv Jul 4, 2023
18115a3
Merge branch 'master' of github.com:binary-com/deriv-app into shontzu…
shontzu-deriv Jul 5, 2023
fcc6413
chore: remove irrelevant check
shontzu-deriv Jul 5, 2023
90ad533
chore: unit test
shontzu-deriv Jul 5, 2023
ed620a9
Merge branch 'master' of github.com:binary-com/deriv-app into shontzu…
shontzu-deriv Jul 5, 2023
f31277b
chore: unit test fail
shontzu-deriv Jul 5, 2023
4678543
Merge branch 'master' of github.com:binary-com/deriv-app into shontzu…
shontzu-deriv Jul 6, 2023
6c99640
chore: fixes based on review
shontzu-deriv Jul 6, 2023
3ab5a76
chore: empty commit
shontzu-deriv Jul 6, 2023
78d9185
Merge branch 'master' of github.com:binary-com/deriv-app into shontzu…
shontzu-deriv Jul 7, 2023
d4625c1
chore: code revie fixes
shontzu-deriv Jul 7, 2023
83b11d1
chore: revert test case changes
shontzu-deriv Jul 7, 2023
faea6de
Merge branch 'master' into shontzu/WALL-865/Incorrect-total-asset-amount
shontzu-deriv Jul 7, 2023
f407aff
Merge branch 'master' into shontzu/WALL-865/Incorrect-total-asset-amount
shontzu-deriv Jul 7, 2023
82c8ca2
chore: rename type to TUseTotalAccountBalance
shontzu-deriv Jul 7, 2023
202d5e8
Merge branch 'master' into shontzu/WALL-865/Incorrect-total-asset-amount
shontzu-deriv Jul 7, 2023
8624ae2
Merge branch 'master' into shontzu/WALL-865/Incorrect-total-asset-amount
shontzu-deriv Jul 10, 2023
a5e7e5b
chore: format doc and added test case for multiple accounts in differ…
shontzu-deriv Jul 10, 2023
5f3ec8c
Merge branch 'master' into shontzu/WALL-865/Incorrect-total-asset-amount
shontzu-deriv Jul 11, 2023
1fa3dc1
Merge branch 'master' into shontzu/WALL-865/Incorrect-total-asset-amount
shontzu-deriv Jul 21, 2023
4a51fd6
Merge branch 'master' into shontzu/WALL-865/Incorrect-total-asset-amount
ali-hosseini-deriv Jul 21, 2023
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
16 changes: 4 additions & 12 deletions packages/hooks/src/__tests__/useTotalAccountBalance.spec.tsx
Copy link
Contributor

Choose a reason for hiding this comment

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

@shontzu-deriv I don't think we need to change the existing tests, I think you should add a new test case for the new scenario 🤔

Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,15 @@ describe('useTotalAccountBalance', () => {

test('should return total balance correctly when user has multiple accounts', async () => {
const mock = mockStore({
exchange_rates: {
data: {
rates: {
EUR: 2,
AUD: 3,
},
},
},
client: {
active_accounts: [
{
currency: 'AUD',
balance: 300,
currency: 'USD',
balance: 10000,
},
{
currency: 'EUR',
balance: 200,
balance: 10000,
},
],
},
Expand All @@ -44,6 +36,6 @@ describe('useTotalAccountBalance', () => {
);
const { result } = renderHook(() => useTotalAccountBalance(mock.client.active_accounts), { wrapper });

expect(result.current.balance).toBe(200);
expect(result.current.balance).toBe(20000);
});
});
9 changes: 5 additions & 4 deletions packages/hooks/src/useTotalAccountBalance.ts
shontzu-deriv marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import useRealTotalAssetCurrency from './useTotalAssetCurrency';
import useExchangeRate from './useExchangeRate';

import { useStore } from '@deriv/stores';
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
import { useStore } from '@deriv/stores';

/**
* 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 }[]) => {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const useTotalAccountBalance = (accounts: { balance?: number; currency?: string }[]) => {
const useTotalAccountBalance = (accounts: { balance?: number; currency?: string; account_type?: string; }[]) => {

const { traders_hub } = useStore();
const { is_demo } = traders_hub;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const { traders_hub } = useStore();
const { is_demo } = traders_hub;

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 rate = getRate(account.currency || total_assets_real_currency || '');
shontzu-deriv marked this conversation as resolved.
Show resolved Hide resolved
const base_rate = is_demo ? getRate('USD') : getRate(total_assets_real_currency || '');
const rate = getRate(total_assets_real_currency || '');
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const base_rate = is_demo ? getRate('USD') : getRate(total_assets_real_currency || '');
const 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;

return total + (account.balance || 0) * exchange_rate;
Expand Down