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

Update tests and log format. #22

Merged
merged 1 commit into from
Aug 9, 2024
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
7 changes: 4 additions & 3 deletions src/helpers/parser.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { ethers } from "ethers";
import {BigNumber} from "bignumber.js";

export default function parseAmount(numberString: string): string {
export function parseAmount(numberString: string): string {
try {
const number = BigInt(numberString);
const number = BigNumber(numberString).toFixed();
return ethers.formatEther(number).toString();
} catch (e) {
return "";
}
}
}
16 changes: 15 additions & 1 deletion src/tests/parse.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from "assert/strict";
import { describe, it } from "node:test";
import { BigNumber } from "bignumber.js";
import parseAmount from "../helpers/parser.js";
import {parseAmount} from "../helpers/parser.js";

describe("Test tx amount parse", () => {
it("Should parse rawFungibleTokenAmount on 18 digit BigNumber value", () => {
Expand All @@ -19,4 +19,18 @@ describe("Test tx amount parse", () => {
const expectedParsedAmount = "114119.157542431558142674";
assert.deepStrictEqual(amountPrettified, expectedParsedAmount);
});

it("Should parse scientific notation", () => {
const scientificNotation = "9.268745046813357e+22";
const amountPrettified = parseAmount(scientificNotation);
const expectedParsedAmount = "92687.45046813357";
assert.deepStrictEqual(amountPrettified, expectedParsedAmount);
});

it("Should parse regular number", () => {
const scientificNotation = "1000000000000000000";
const amountPrettified = parseAmount(scientificNotation);
const expectedParsedAmount = "1.0";
assert.deepStrictEqual(amountPrettified, expectedParsedAmount);
});
});