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

feat(frontend): validateConvertAmount util #3585

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
41 changes: 41 additions & 0 deletions src/frontend/src/lib/utils/convert.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { ZERO } from '$lib/constants/app.constants';
import type { ConvertAmountErrorType } from '$lib/types/convert';
import { formatToken } from '$lib/utils/format.utils';
import { isNullish, nonNullish } from '@dfinity/utils';
import { BigNumber } from '@ethersproject/bignumber';
import { Utils } from 'alchemy-sdk';

export const validateConvertAmount = ({
userAmount,
decimals,
balance,
totalFee
}: {
userAmount: BigNumber;
decimals: number;
balance?: BigNumber;
totalFee?: bigint;
}): ConvertAmountErrorType => {
if (isNullish(totalFee)) {
return;
}
// We should align balance and userAmount to avoid issues caused by comparing formatted and unformatted BN
const parsedSendBalance = nonNullish(balance)
? Utils.parseUnits(
formatToken({
value: balance,
unitName: decimals,
displayDecimals: decimals
}),
decimals
)
: ZERO;

if (userAmount.gt(parsedSendBalance)) {
return 'insufficient-funds';
}

if (nonNullish(totalFee) && userAmount.add(totalFee).gt(parsedSendBalance)) {
return 'insufficient-funds-for-fee';
}
};
53 changes: 53 additions & 0 deletions src/frontend/src/tests/lib/utils/convert.utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { validateConvertAmount } from '$lib/utils/convert.utils';
import { BigNumber } from 'alchemy-sdk';
import { describe } from 'vitest';

describe('validateConvertAmount', () => {
const userAmount = BigNumber.from(200000n);
const decimals = 8;
const balance = BigNumber.from(9000000n);
const totalFee = 10000n;

it('should return undefined if all data satisfies the conditions', () => {
expect(
validateConvertAmount({
userAmount,
decimals,
balance,
totalFee
})
).toBeUndefined();
});

it('should return undefined if totalFee is undefined', () => {
expect(
validateConvertAmount({
userAmount,
decimals,
balance
})
).toBeUndefined();
});

it('should return insufficient funds error', () => {
expect(
validateConvertAmount({
userAmount: balance.add(userAmount),
decimals,
balance,
totalFee
})
).toBe('insufficient-funds');
});

it('should return insufficient funds for fee error', () => {
expect(
validateConvertAmount({
userAmount: balance.sub(BigNumber.from(totalFee).div(2)),
decimals,
balance,
totalFee
})
).toBe('insufficient-funds-for-fee');
});
});