Skip to content

Commit

Permalink
Replace original getRealAmount
Browse files Browse the repository at this point in the history
  • Loading branch information
ryutamago committed Sep 29, 2023
1 parent 8c5ca79 commit f5ccff1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/components/CSVFileUploader/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/components/SendFlow/Token/FormPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
import { FA12TokenBalance, FA2TokenBalance } from "../../../types/TokenBalance";
import {
formatTokenAmount,
getRealAmountInString,
getRealAmount,
tokenDecimals,
tokenSymbolSafe,
} from "../../../types/Token";
Expand All @@ -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") {
Expand Down
10 changes: 5 additions & 5 deletions src/types/Token.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
artifactUri,
formatTokenAmount,
fromRaw,
getRealAmountInString,
getRealAmount,
metadataUri,
mimeType,
royalties,
Expand Down Expand Up @@ -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");
});

Expand All @@ -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");
});

Expand All @@ -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");
});
});
10 changes: 3 additions & 7 deletions src/types/Token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down

0 comments on commit f5ccff1

Please sign in to comment.