From 76b5804adf65c9302c34232e52d5a42bd825fcc9 Mon Sep 17 00:00:00 2001 From: Phoebe Lartisant Date: Tue, 11 Jun 2024 15:54:50 +0200 Subject: [PATCH] refacto getAmountToReceive and edit tests --- jest/__tests__/getAmountToReceive.test.ts | 22 +++++++++------------- src/utils/utils.ts | 13 +++---------- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/jest/__tests__/getAmountToReceive.test.ts b/jest/__tests__/getAmountToReceive.test.ts index 2ec1b84a..757b415a 100644 --- a/jest/__tests__/getAmountToReceive.test.ts +++ b/jest/__tests__/getAmountToReceive.test.ts @@ -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', () => { @@ -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', () => { @@ -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', () => { diff --git a/src/utils/utils.ts b/src/utils/utils.ts index e83d54c1..cef6b4ec 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -1,4 +1,3 @@ -import { formatFTAmount, removeTrailingZeros } from '@massalabs/react-ui-kit'; import { parseUnits } from 'viem'; import { MASSA_EXPLORER_URL, @@ -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 ''; } @@ -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; } /**