Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FRE-1186] fix decimal issue #406

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/widget-v2/src/utils/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { BigNumber } from "bignumber.js";

export const convertHumanReadableAmountToCryptoAmount = (
humanReadableAmount: number | string,
decimals = 6
): string => {
if (typeof humanReadableAmount === "string") {
humanReadableAmount = parseFloat(humanReadableAmount);
}
const cryptoAmount = humanReadableAmount * Math.pow(10, decimals);
return cryptoAmount.toString();
const cryptoAmount = new BigNumber(humanReadableAmount).shiftedBy(decimals);
return cryptoAmount.toFixed(0);
};

export const convertTokenAmountToHumanReadableAmount = (
Expand All @@ -17,7 +19,7 @@ export const convertTokenAmountToHumanReadableAmount = (
if (typeof tokenAmount === "string") {
tokenAmount = parseFloat(tokenAmount);
}
const humanReadableAmount = tokenAmount / Math.pow(10, decimals);
const humanReadableAmount = new BigNumber(tokenAmount).shiftedBy(-decimals);
return humanReadableAmount.toFixed(decimals).replace(/(\.\d*?[1-9])(?:0+|\.0*)$/, "$1");
};

Expand Down
1 change: 1 addition & 0 deletions packages/widget-v2/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default defineConfig({
force: true, // This forces Vite to optimize this dependency
},
build: {
minify: true,
lib: {
// Could also be a dictionary or array of multiple entry points
entry: resolve(__dirname, "src/index.tsx"),
Expand Down
Loading