Skip to content

Commit

Permalink
Merge pull request #22 from availproject/update-amount-format
Browse files Browse the repository at this point in the history
Update tests and log format.
  • Loading branch information
moraesjeremias authored Aug 9, 2024
2 parents 3c631f2 + 4b0e674 commit 49be12b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
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);
});
});

0 comments on commit 49be12b

Please sign in to comment.