From 76cf30ab3c4efd185f544a5d5a853faf58a09d41 Mon Sep 17 00:00:00 2001 From: "Felix C. Morency" <1102868+fmorency@users.noreply.github.com> Date: Tue, 26 Nov 2024 13:51:02 -0500 Subject: [PATCH] fix: send big numbers --- components/bank/forms/sendForm.tsx | 7 +--- utils/maths.ts | 66 ++++-------------------------- 2 files changed, 9 insertions(+), 64 deletions(-) diff --git a/components/bank/forms/sendForm.tsx b/components/bank/forms/sendForm.tsx index 3bfecfb6..14345770 100644 --- a/components/bank/forms/sendForm.tsx +++ b/components/bank/forms/sendForm.tsx @@ -3,7 +3,7 @@ import { chainName } from '@/config'; import { useFeeEstimation, useTx } from '@/hooks'; import { cosmos } from '@liftedinit/manifestjs'; import { PiCaretDownBold } from 'react-icons/pi'; -import { shiftDigits, truncateString } from '@/utils'; +import { parseNumberToBigInt, shiftDigits, truncateString } from '@/utils'; import { CombinedBalanceInfo } from '@/utils/types'; import { DenomImage } from '@/components/factory'; import { Formik, Form } from 'formik'; @@ -96,10 +96,7 @@ export default function SendForm({ setIsSending(true); try { const exponent = values.selectedToken.metadata?.denom_units[1]?.exponent ?? 6; - const amountInBaseUnits = Math.floor( - parseFloat(values.amount) * Math.pow(10, exponent) - ).toString(); - + const amountInBaseUnits = parseNumberToBigInt(values.amount, exponent).toString(); const msg = send({ fromAddress: address, toAddress: values.recipient, diff --git a/utils/maths.ts b/utils/maths.ts index 1f37d734..7dc09011 100644 --- a/utils/maths.ts +++ b/utils/maths.ts @@ -31,69 +31,17 @@ export const shiftDigits = ( } }; -export const toNumber = (val: string, decimals: number = 6) => { - return new BigNumber(val).decimalPlaces(decimals).toNumber(); -}; - -export const formatNumber = (num: number) => { - if (num === 0) return '0'; - if (num < 0.001) return '<0.001'; - - const truncate = (number: number, decimalPlaces: number) => { - const numStr = number.toString(); - const dotIndex = numStr.indexOf('.'); - if (dotIndex === -1) return numStr; - const endIndex = decimalPlaces > 0 ? dotIndex + decimalPlaces + 1 : dotIndex; - return numStr.substring(0, endIndex); - }; - - if (num < 1) { - return truncate(num, 3); - } - if (num < 100) { - return truncate(num, 1); - } - if (num < 1000) { - return truncate(num, 0); - } - if (num >= 1000 && num < 1000000) { - return truncate(num / 1000, 0) + 'K'; - } - if (num >= 1000000 && num < 1000000000) { - return truncate(num / 1000000, 0) + 'M'; - } - if (num >= 1000000000) { - return truncate(num / 1000000000, 0) + 'B'; - } +export const parseNumberToBigInt = (v: string, maxDigits: number = 6) => { + const amount = BigNumber(v); + const precision = BigNumber(`1e${maxDigits}`); + const b = amount.times(precision).toFixed(); + return BigInt(b); }; -export function truncateToTwoDecimals(num: number) { - const multiplier = Math.pow(10, 2); - return Math.floor(num * multiplier) / multiplier; -} - -export const sum = (...args: string[]) => { - return args.reduce((prev, cur) => prev.plus(cur), new BigNumber(0)).toString(); +export const toNumber = (val: string, decimals: number = 6) => { + return new BigNumber(val).decimalPlaces(decimals).toNumber(); }; -export function abbreviateNumber(value: number): string { - if (value < 1000) { - return Number(value.toFixed(1)).toString(); - } - - const suffixes = ['', 'k', 'M', 'B', 'T']; - - const suffixNum = Math.floor(Math.log10(value) / 3); - - let shortValue = value / Math.pow(1000, suffixNum); - - shortValue = Math.round(shortValue * 10) / 10; - - let newValue = shortValue % 1 === 0 ? shortValue.toString() : shortValue.toFixed(1); - - return newValue + suffixes[suffixNum]; -} - export const calculateIsUnsafe = ( newPower: string | number, currentPower: string | number,