diff --git a/src/components/CSVFileUploader/utils.ts b/src/components/CSVFileUploader/utils.ts index ef4b19eebc..381069d7ef 100644 --- a/src/components/CSVFileUploader/utils.ts +++ b/src/components/CSVFileUploader/utils.ts @@ -6,7 +6,7 @@ import { parsePkh, } from "../../types/Address"; import { Operation } from "../../types/Operation"; -import { getRealAmountInString } from "../../types/Token"; +import { getRealAmount } from "../../types/Token"; import { tezToMutez } from "../../utils/format"; import { validateNonNegativeNumber } from "../../utils/helpers"; import { TokenLookup } from "../../utils/hooks/tokensHooks"; @@ -53,7 +53,7 @@ export const parseOperation = ( if (!token) { throw new Error(`Unknown token ${contractPkh} ${tokenId}`); } - const amount = getRealAmountInString(token, prettyAmount); + const amount = getRealAmount(token, prettyAmount); if (token.type === "fa1.2") { return { diff --git a/src/components/SendFlow/Token/FormPage.tsx b/src/components/SendFlow/Token/FormPage.tsx index 79be025b6c..62270838ad 100644 --- a/src/components/SendFlow/Token/FormPage.tsx +++ b/src/components/SendFlow/Token/FormPage.tsx @@ -29,7 +29,7 @@ import { import { FA12TokenBalance, FA2TokenBalance } from "../../../types/TokenBalance"; import { formatTokenAmount, - getRealAmountInString, + getRealAmount, tokenDecimals, tokenSymbolSafe, } from "../../../types/Token"; @@ -55,7 +55,7 @@ const toOperation = recipient: parsePkh(formValues.recipient), contract: parseContractPkh(token.contract), tokenId: token.tokenId, - amount: getRealAmountInString(token, formValues.prettyAmount), + amount: getRealAmount(token, formValues.prettyAmount), }; if (token.type === "fa2") { diff --git a/src/types/Token.test.tsx b/src/types/Token.test.tsx index c9c63f6220..35e0cad787 100644 --- a/src/types/Token.test.tsx +++ b/src/types/Token.test.tsx @@ -9,7 +9,7 @@ import { artifactUri, formatTokenAmount, fromRaw, - getRealAmountInString, + getRealAmount, metadataUri, mimeType, royalties, @@ -377,14 +377,14 @@ describe("formatTokenAmount", () => { }); }); -describe("getRealAmountInString", () => { +describe("getRealAmount", () => { it("returns the same amount for token with no decimals", () => { const fa2token: FA2Token = { type: "fa2", contract: "KT1QTcAXeefhJ3iXLurRt81WRKdv7YqyYFmo", tokenId: "123", }; - const amount = getRealAmountInString(fa2token, "10000"); + const amount = getRealAmount(fa2token, "10000"); expect(amount).toEqual("10000"); }); @@ -395,7 +395,7 @@ describe("getRealAmountInString", () => { tokenId: "123", metadata: { decimals: "2" }, }; - const amount = getRealAmountInString(fa2token, "10000"); + const amount = getRealAmount(fa2token, "10000"); expect(amount).toEqual("1000000"); }); @@ -406,7 +406,7 @@ describe("getRealAmountInString", () => { tokenId: "123", metadata: { decimals: "18" }, }; - const amount = getRealAmountInString(fa2token, "1000"); + const amount = getRealAmount(fa2token, "1000"); expect(amount).toEqual("1000000000000000000000"); }); }); diff --git a/src/types/Token.ts b/src/types/Token.ts index 5b8803c4bf..38eb17a561 100644 --- a/src/types/Token.ts +++ b/src/types/Token.ts @@ -189,22 +189,18 @@ export const tokenDecimals = (asset: Token): string => { return asset.metadata?.decimals === undefined ? DEFAULT_TOKEN_DECIMALS : asset.metadata.decimals; }; -export const getRealAmount = (asset: Token, prettyAmount: string): BigNumber => { +export const getRealAmount = (asset: Token, prettyAmount: string): string => { const amount = new BigNumber(prettyAmount); if (asset.type === "nft") { - return amount; + return amount.toFixed(); } const decimals = tokenDecimals(asset); - return amount.multipliedBy(new BigNumber(10).exponentiatedBy(decimals)); + return amount.multipliedBy(new BigNumber(10).exponentiatedBy(decimals)).toFixed(); }; -// To avoid scientific notation for BigNumber with value > 1e+21, we should use `toFixed()` over `toString()` -export const getRealAmountInString = (asset: Token, prettyAmount: string): string => - getRealAmount(asset, prettyAmount).toFixed(); - export const formatTokenAmount = (amount: string, decimals = DEFAULT_TOKEN_DECIMALS): string => { const realAmount = BigNumber(amount).dividedBy(BigNumber(10).pow(decimals)); const formatter = new Intl.NumberFormat("en-US", {