Skip to content

Commit

Permalink
[WALL] george / WALL-4126 / If the wallet balance is less than the mi…
Browse files Browse the repository at this point in the history
…nimum withdrawal amount, the withdrawal limit shown below the amount field displays in a Max-Min pattern (#15265)

* feat(wallets): 🚑 add balanceLessThanMinWithdrawalLimit cehck to crypto withdraw

* test(wallets): 🧪 add test case
  • Loading branch information
heorhi-deriv authored May 23, 2024
1 parent d4109f8 commit 013bcd7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,30 @@ import { THooks } from '../../../../../../types';
import { validateCryptoAddress, validateCryptoInput, validateFiatInput } from '../withdrawalCryptoValidators';

describe('withdrawalCryptoValidator', () => {
let mockValue = '2.5';
let mockIsClientVerified = true;
let mockCryptoAddress = 'jds93e9f8wefun9w8efrn98wefn09inf0';

const mockActiveWallet = {
balance: 10,
currency: 'BTC',
} as THooks.ActiveWalletAccount;

const mockFractionalDigits = {
crypto: 7,
fiat: 2,
};
const mockRemainder = 9;
const mockMinimumWithdrawal = 1;
let mockValue: string,
mockIsClientVerified: boolean,
mockCryptoAddress: string,
mockActiveWallet: THooks.ActiveWalletAccount,
mockFractionalDigits: { crypto: number; fiat: number },
mockRemainder: number,
mockMinimumWithdrawal: number;

beforeEach(() => {
mockValue = '2.5';
mockIsClientVerified = true;
mockCryptoAddress = 'jds93e9f8wefun9w8efrn98wefn09inf0';
//@ts-expect-error since this is a mock, we only need partial properties of data
mockActiveWallet = {
balance: 10,
currency: 'BTC',
};
mockFractionalDigits = {
crypto: 7,
fiat: 2,
};
mockRemainder = 9;
mockMinimumWithdrawal = 1;
});

it('should check if no errors are returned when valid inputs are provided for crypto address', () => {
const cryptoAddressMessages = validateCryptoAddress(mockCryptoAddress);
Expand Down Expand Up @@ -157,6 +166,30 @@ describe('withdrawalCryptoValidator', () => {
expect(cryptoAmountMessages).toEqual('Insufficient funds');
});

it('should return `balanceLessThanMinWithdrawalLimit` error', () => {
//@ts-expect-error since this is a mock, we only need partial properties of data
mockActiveWallet = {
balance: 0.3,
currency: 'BTC',
display_balance: '0.3000000 BTC',
};
mockValue = '0.2000000';
mockMinimumWithdrawal = 0.5;

const cryptoAmountMessages = validateCryptoInput(
mockActiveWallet,
mockFractionalDigits,
mockIsClientVerified,
mockRemainder,
mockValue,
mockMinimumWithdrawal
);

expect(cryptoAmountMessages).toEqual(
'Your balance (0.3000000 BTC) is less than the current minimum withdrawal allowed (0.5000000 BTC). Please top up your wallet to continue with your withdrawal.'
);
});

it('should return limit error if amount < min withdrawal limit but is still less than the balance for verified user', () => {
mockValue = '0.5000000';
mockIsClientVerified = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { TWithdrawalCryptoContext } from '../provider';

const helperMessageMapper = {
balanceLessThanMinWithdrawalLimit: (balance: string, min: string) =>
`Your balance (${balance}) is less than the current minimum withdrawal allowed (${min}). Please top up your wallet to continue with your withdrawal.`,
decimalPlacesExceeded: (limit: number) => `Up to ${limit} decimal places are allowed.`,
fieldRequired: 'This field is required.',
insufficientFunds: 'Insufficient funds',
Expand Down Expand Up @@ -71,6 +73,13 @@ const validateCryptoInput = (

const MIN_WITHDRAWAL_AMOUNT = minimumWithdrawal;

if (MIN_WITHDRAWAL_AMOUNT && activeWallet.balance < MIN_WITHDRAWAL_AMOUNT) {
return helperMessageMapper.balanceLessThanMinWithdrawalLimit(
activeWallet.display_balance,
`${MIN_WITHDRAWAL_AMOUNT.toFixed(fractionalDigits.crypto)} ${activeWallet.currency}`
);
}

const MAX_WITHDRAWAL_AMOUNT =
!isClientVerified && remainder < activeWallet.balance ? remainder : activeWallet.balance;

Expand Down

0 comments on commit 013bcd7

Please sign in to comment.