Skip to content

Commit

Permalink
refacto getAmountToReceive and edit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pivilartisant committed Jun 11, 2024
1 parent 0af22b2 commit 76b5804
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
22 changes: 9 additions & 13 deletions jest/__tests__/getAmountToReceive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ describe('should calculate service fees', () => {
jest.clearAllMocks();
});

test('should return 0.01% of 0.004', () => {
test('should return 0.004 - a 0.1% service fee', () => {
const amount = '0.004';
const serviceFee = 10n;
const decimals = 6;
const result = getAmountToReceive(amount, serviceFee, decimals);
expect(result).toBe('0.003996');
expect(result).toBe(3996n);
});

test('should return input amount when service fee is 0n', () => {
Expand All @@ -23,28 +23,28 @@ describe('should calculate service fees', () => {
expect(result).toBe(amount);
});

test('should return 0.01% of 100', () => {
test('should return 100 - 0.1% of service fees', () => {
const amount = '100';
const serviceFee = 10n;
const decimals = 6;
const result = getAmountToReceive(amount, serviceFee, decimals);
expect(result).toBe('99.900000');
expect(result).toBe(99900000n);
});

test('should return 0.02% of 5618.897000', () => {
test('should return 5618.897000 - 0.02% of service fee', () => {
const amount = '5618.897000';
const serviceFee = 20n;
const decimals = 6;
const result = getAmountToReceive(amount, serviceFee, decimals);
expect(result).toBe('5,607.659206');
expect(result).toBe(5607659206n);
});

test('should return 0.02% of 101299120121.128893', () => {
test('should return 101299120121.128893 - 0.02% of service fees', () => {
const amount = '101299120121.128893';
const serviceFee = 20n;
const decimals = 6;
const result = getAmountToReceive(amount, serviceFee, decimals);
expect(result).toBe('101,096,521,880.886640');
expect(result).toBe(101096521880886636n);
});

test('should calculate 0.02% of MAX SAFE INT', () => {
Expand All @@ -56,12 +56,8 @@ describe('should calculate service fees', () => {
const _amount = parseUnits(amount, decimals);
const redeemFee = (_amount * serviceFee) / 10000n;
const expectedReceivedAmount = _amount - redeemFee;
const expected = formatFTAmount(
expectedReceivedAmount,
decimals,
).amountFormattedFull;

expect(result).toBe(expected);
expect(result).toBe(expectedReceivedAmount);
});

describe('serviceFeeToPercent', () => {
Expand Down
13 changes: 3 additions & 10 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { formatFTAmount, removeTrailingZeros } from '@massalabs/react-ui-kit';
import { parseUnits } from 'viem';
import {
MASSA_EXPLORER_URL,
Expand Down Expand Up @@ -55,14 +54,13 @@ export function getMinConfirmation(
* @param serviceFee - bigint service fee received from the read sc
* @param decimals - IToken selectedToken.decimals
* @param inFull - boolean to return the full amount or amount with no trailing zeros
* @returns string
* @returns bigint of the amount to be received
*/
export function getAmountToReceive(
amount: string | undefined,
serviceFee: bigint,
decimals: number | undefined,
inFull = true,
): string {
): bigint | string {
if (!amount || !decimals) {
return '';
}
Expand All @@ -71,12 +69,7 @@ export function getAmountToReceive(
}
const _amount = parseUnits(amount, decimals);
const redeemFee = (_amount * serviceFee) / 10000n;
const receivedAmount = _amount - redeemFee;

const { amountFormattedFull } = formatFTAmount(receivedAmount, decimals);
return inFull
? amountFormattedFull
: removeTrailingZeros(amountFormattedFull);
return _amount - redeemFee;
}

/**
Expand Down

0 comments on commit 76b5804

Please sign in to comment.