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

Handle with token with large number #465

Merged
merged 3 commits into from
Sep 29, 2023
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
4 changes: 2 additions & 2 deletions src/components/CSVFileUploader/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const parseOperation = (
return {
type: "tez",
recipient,
amount: tezToMutez(prettyAmount).toString(),
amount: tezToMutez(prettyAmount).toFixed(),
};
}

Expand All @@ -53,7 +53,7 @@ export const parseOperation = (
if (!token) {
throw new Error(`Unknown token ${contractPkh} ${tokenId}`);
}
const amount = getRealAmount(token, prettyAmount).toString();
const amount = getRealAmount(token, prettyAmount);

if (token.type === "fa1.2") {
return {
Expand Down
2 changes: 1 addition & 1 deletion src/components/SendFlow/Tez/FormPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export type FormValues = {

const toOperation = (formValues: FormValues): TezTransfer => ({
type: "tez",
amount: tezToMutez(formValues.prettyAmount).toString(),
amount: tezToMutez(formValues.prettyAmount).toFixed(),
recipient: parsePkh(formValues.recipient),
});

Expand Down
2 changes: 1 addition & 1 deletion src/components/SendFlow/Token/FormPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const toOperation =
recipient: parsePkh(formValues.recipient),
contract: parseContractPkh(token.contract),
tokenId: token.tokenId,
amount: getRealAmount(token, formValues.prettyAmount).toString(),
amount: getRealAmount(token, formValues.prettyAmount),
};

if (token.type === "fa2") {
Expand Down
35 changes: 35 additions & 0 deletions src/types/Token.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
artifactUri,
formatTokenAmount,
fromRaw,
getRealAmount,
metadataUri,
mimeType,
royalties,
Expand Down Expand Up @@ -375,3 +376,37 @@ describe("formatTokenAmount", () => {
expect(formatTokenAmount("100000", "3")).toEqual("100.000");
});
});

describe("getRealAmount", () => {
it("returns the same amount for token with no decimals", () => {
const fa2token: FA2Token = {
type: "fa2",
contract: "KT1QTcAXeefhJ3iXLurRt81WRKdv7YqyYFmo",
tokenId: "123",
};
const amount = getRealAmount(fa2token, "10000");
expect(amount).toEqual("10000");
});

it("returns the real amount for token with decimals", () => {
const fa2token: FA2Token = {
type: "fa2",
contract: "KT1QTcAXeefhJ3iXLurRt81WRKdv7YqyYFmo",
tokenId: "123",
metadata: { decimals: "2" },
};
const amount = getRealAmount(fa2token, "10000");
expect(amount).toEqual("1000000");
});

it("does not return the scientific notation for token with large number", () => {
const fa2token: FA2Token = {
type: "fa2",
contract: "KT1QTcAXeefhJ3iXLurRt81WRKdv7YqyYFmo",
tokenId: "123",
metadata: { decimals: "18" },
};
const amount = getRealAmount(fa2token, "1000");
expect(amount).toEqual("1000000000000000000000");
});
});
6 changes: 3 additions & 3 deletions src/types/Token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,16 @@ 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();
};

export const formatTokenAmount = (amount: string, decimals = DEFAULT_TOKEN_DECIMALS): string => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function validateNonNegativeNumber(num: string): string | null {
if (val.isLessThan(0)) {
return null;
}
return val.toString();
return val.toFixed();
}

export const navigateToExternalLink = (link: string) => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/hooks/assetsHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export const useTotalBalance = () => {

const usdBalance = tezToDollar(mutezToTez(totalBalance));

return { mutez: totalBalance.toString(), usd: usdBalance };
return { mutez: totalBalance.toFixed(), usd: usdBalance };
};

export const useGetAccountBalance = () => {
Expand Down