Skip to content

Commit

Permalink
chore: fix estimateGasFee math rounding issue (#397)
Browse files Browse the repository at this point in the history
* chore: fix edge case for math rounding issue

* chore: refactor

* docs(changeset): chore: fix rounding issue for estimateGasFee api
  • Loading branch information
npty authored Jul 11, 2024
1 parent 0983c58 commit e987e82
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/forty-birds-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@axelarjs/api": patch
---

chore: fix rounding issue for estimateGasFee api
8 changes: 8 additions & 0 deletions packages/api/src/axelar-query/utils/bigint.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@ describe("bigint", () => {
const result = multiplyFloatByBigInt(floatNum, bigIntNum);
expect(result).toEqual(1234n);
});

it("should handle floating points result", () => {
const floatNum = 1.16;
const bigIntNum = 169973114000000n;

const result = multiplyFloatByBigInt(floatNum, bigIntNum);
expect(result).toEqual(197168812240000n);
});
});
5 changes: 4 additions & 1 deletion packages/api/src/axelar-query/utils/bigint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ export function multiplyFloatByBigInt(floatNum: number, bigIntNum: bigint) {
// Scaling factor
const scalingFactor = 10 ** decimalPlaces;

// Rounded multiplication of the float by the scaling factor
const scaledFloat = Math.round(floatNum * scalingFactor);

// Convert float to scaled BigInt (as before)
const scaledBigInt = BigInt(floatNum * scalingFactor);
const scaledBigInt = BigInt(scaledFloat);

// Perform the multiplication (both are now BigInts)
const result = scaledBigInt * bigIntNum;
Expand Down

0 comments on commit e987e82

Please sign in to comment.