Skip to content

Commit

Permalink
fix: (ibc)send big numbers (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmorency authored Nov 27, 2024
1 parent 5d19dc3 commit 2ed7ed7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 67 deletions.
6 changes: 2 additions & 4 deletions components/bank/forms/ibcSendForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState, useMemo } from 'react';
import { chainName } from '@/config';
import { useFeeEstimation, useTx } from '@/hooks';
import { ibc } from '@liftedinit/manifestjs';
import { getIbcInfo } from '@/utils';
import { getIbcInfo, parseNumberToBigInt } from '@/utils';
import { PiCaretDownBold } from 'react-icons/pi';
import { MdContacts } from 'react-icons/md';
import { CombinedBalanceInfo } from '@/utils/types';
Expand Down Expand Up @@ -108,9 +108,7 @@ export default function IbcSendForm({
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 { source_port, source_channel } = getIbcInfo(chainName ?? '', destinationChain ?? '');

Expand Down
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
68 changes: 10 additions & 58 deletions utils/maths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,69 +31,21 @@ 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 = new BigNumber(v);
if (!amount.isFinite()) {
console.error(`Invalid input passed to parseNumberToBigInt: ${v}`);
return BigInt(0);
}
const precision = new BigNumber(10).pow(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 2ed7ed7

Please sign in to comment.