Skip to content

Commit

Permalink
fix: send big numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
fmorency committed Nov 26, 2024
1 parent 263a5e1 commit 76cf30a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 64 deletions.
7 changes: 2 additions & 5 deletions components/bank/forms/sendForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
66 changes: 7 additions & 59 deletions utils/maths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 76cf30a

Please sign in to comment.