Skip to content

Commit 721d1ed

Browse files
author
Lucas Araujo
committed
[DW-1086] Fix hasAssetsAfterTransaction function
1 parent 5830c41 commit 721d1ed

File tree

4 files changed

+124
-6
lines changed

4 files changed

+124
-6
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import BigNumber from 'bignumber.js';
2+
import { hasAssetsAfterTransaction } from './helpers';
3+
4+
const assetWithOneQuantity = {
5+
policyId: 'policyId1',
6+
assetName: '54657374636f696e',
7+
quantity: new BigNumber(1),
8+
fingerprint: 'policyId154657374636f696e',
9+
metadata: {
10+
name: 'Testcoin',
11+
description: 'Test coin',
12+
},
13+
uniqueId: 'uniqueId1',
14+
decimals: 1,
15+
recommendedDecimals: null,
16+
};
17+
18+
const assetWithTenQuantity = {
19+
policyId: 'policyId2',
20+
assetName: '436f696e74657374',
21+
quantity: new BigNumber(10),
22+
fingerprint: 'policyId2436f696e74657374',
23+
uniqueId: 'uniqueId2',
24+
decimals: 1,
25+
recommendedDecimals: null,
26+
};
27+
28+
describe('hasAssetsAfterTransaction', () => {
29+
test('Should be true when wallet has assets and tx sending no assets', () => {
30+
expect(
31+
hasAssetsAfterTransaction({
32+
assetTokens: [assetWithOneQuantity, assetWithTenQuantity],
33+
selectedAssets: [],
34+
assetsAmounts: [],
35+
})
36+
).toEqual(true);
37+
});
38+
39+
test('Should be true when wallet has assets and tx is not sending all asset types', () => {
40+
expect(
41+
hasAssetsAfterTransaction({
42+
assetTokens: [assetWithOneQuantity, assetWithTenQuantity],
43+
selectedAssets: [assetWithTenQuantity],
44+
assetsAmounts: ['5'],
45+
})
46+
).toEqual(true);
47+
});
48+
49+
test('Should be true when wallet has assets and tx is not sending all assets', () => {
50+
expect(
51+
hasAssetsAfterTransaction({
52+
assetTokens: [assetWithOneQuantity, assetWithTenQuantity],
53+
selectedAssets: [assetWithOneQuantity, assetWithTenQuantity],
54+
assetsAmounts: ['1', '5'],
55+
})
56+
).toEqual(true);
57+
});
58+
59+
test('Should be false when wallet has no assets', () => {
60+
expect(
61+
hasAssetsAfterTransaction({
62+
assetTokens: [],
63+
selectedAssets: [],
64+
assetsAmounts: [],
65+
})
66+
).toEqual(false);
67+
});
68+
69+
test('Should be false when tx is sending all assets (1 asset)', () => {
70+
expect(
71+
hasAssetsAfterTransaction({
72+
assetTokens: [assetWithOneQuantity],
73+
selectedAssets: [assetWithOneQuantity],
74+
assetsAmounts: ['1'],
75+
})
76+
).toEqual(false);
77+
});
78+
79+
test('Should be false when tx is sending all assets (2 assets)', () => {
80+
expect(
81+
hasAssetsAfterTransaction({
82+
assetTokens: [assetWithOneQuantity, assetWithTenQuantity],
83+
selectedAssets: [assetWithOneQuantity, assetWithTenQuantity],
84+
assetsAmounts: ['1', '10'],
85+
})
86+
).toEqual(false);
87+
});
88+
});

source/renderer/app/containers/wallet/dialogs/send-confirmation/helpers.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import { AssetToken } from '../../../../api/assets/types';
77
import { HwDeviceStatuses } from '../../../../domains/Wallet';
88
import { FORM_VALIDATION_DEBOUNCE_WAIT } from '../../../../config/timingConfig';
99
import { formattedTokenWalletAmount } from '../../../../utils/formatters';
10-
import { CreateForm, FormFields, HasAssetsAfterTransaction } from './types';
10+
import {
11+
CreateForm,
12+
FormFields,
13+
HasAssetsAfterTransaction,
14+
IsSendingAllFromSelected,
15+
} from './types';
1116

1217
export const doTermsNeedAcceptance = ({ isFlight, areTermsAccepted }) =>
1318
!areTermsAccepted && isFlight;
@@ -25,13 +30,32 @@ export const isNotEnoughFundsForTokenError = (errorId) =>
2530
export const isPasswordValid = ({ isHardwareWallet, isValid }) =>
2631
isHardwareWallet || isValid;
2732

33+
export const isSendingAllFromSelected = ({
34+
selectedAssets = [],
35+
assetsAmounts = [],
36+
}: IsSendingAllFromSelected) =>
37+
Boolean(selectedAssets.length) &&
38+
selectedAssets.every(({ quantity }, i) =>
39+
quantity.isEqualTo(assetsAmounts?.[i])
40+
);
41+
2842
export const hasAssetsAfterTransaction = ({
2943
selectedAssets = [],
3044
assetTokens = [],
31-
}: HasAssetsAfterTransaction) =>
32-
selectedAssets.length
33-
? selectedAssets.length < assetTokens.length
34-
: assetTokens.length > 0;
45+
assetsAmounts = [],
46+
}: HasAssetsAfterTransaction) => {
47+
const sendingTokens = Boolean(selectedAssets.length);
48+
const hasTokens = Boolean(assetTokens.length);
49+
const sendingAllTokenTypes = selectedAssets.length === assetTokens.length;
50+
const sendingAllFromSelected = isSendingAllFromSelected({
51+
assetsAmounts,
52+
selectedAssets,
53+
});
54+
55+
return sendingTokens
56+
? !(sendingAllTokenTypes && sendingAllFromSelected)
57+
: hasTokens;
58+
};
3559

3660
export const getFormattedAssetAmount = (
3761
{ metadata, decimals }: AssetToken,

source/renderer/app/containers/wallet/dialogs/send-confirmation/hooks.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export const useForm = ({
3939
hasAssetsRemainingAfterTransaction: hasAssetsAfterTransaction({
4040
assetTokens,
4141
selectedAssets,
42+
assetsAmounts,
4243
}),
4344
...(selectedAssets.length
4445
? { assets: selectedAssets, assetsAmounts }

source/renderer/app/containers/wallet/dialogs/send-confirmation/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,10 @@ export type CreateForm = Pick<ViewProps, 'intl' | 'isHardwareWallet'>;
9494

9595
export type HasAssetsAfterTransaction = Pick<
9696
ViewProps,
97-
'assetTokens' | 'selectedAssets'
97+
'assetTokens' | 'selectedAssets' | 'assetsAmounts'
98+
>;
99+
100+
export type IsSendingAllFromSelected = Pick<
101+
ViewProps,
102+
'selectedAssets' | 'assetsAmounts'
98103
>;

0 commit comments

Comments
 (0)