From 33350b263e68cb8000f0a8f280dbd6f1ff59b936 Mon Sep 17 00:00:00 2001 From: Adw8 Date: Thu, 6 Feb 2025 16:37:05 +0530 Subject: [PATCH] Update BN round up logic --- src/app/page.tsx | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/app/page.tsx b/src/app/page.tsx index 8d7fe42..1b25ee5 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -62,18 +62,17 @@ const Page: React.FC = (): React.ReactElement => { } } - const roundUpBN = (bnValue: BN) : BN => { - // Divide by 10^6 - const decimalBN = bnValue.div(new BN (10 ** 6)); - // Conver to Big decimal - const bigValue = new Big(decimalBN.toString()) - // Ceil the result - const ceiledValue = bigValue.add(1); - // Convert back to BN - const ceiledBN = new BN(ceiledValue.toString()); - // Multiple by 10^6 - return ceiledBN.mul(new BN (10 ** 6)) - } + const roundUpBigNumber = (bnValue: BN): BN => { + const bigNumber = new Big(bnValue.toString()); + const bigNumberInUnits = bigNumber.div(new Big(10 ** 6)); + + const roundedUpValue = bigNumberInUnits.round(0, Big.roundUp); + + // Multiply by 10^6 to revert back to original scale + const scaledValue = roundedUpValue.mul(new Big(10 ** 6)); + + return new BN(scaledValue.toString()); + }; const handleFluxGeneration = (modelId: string, cost: BN) => { return async (prompt: string): Promise => { @@ -89,7 +88,7 @@ const Page: React.FC = (): React.ReactElement => { // Convert cost in USDC to MTM tokens using the price ratio const paymentResult = await processMTMPayment( publicKey, - roundUpBN(cost), + roundUpBigNumber(cost), type )