Skip to content

Commit

Permalink
chore: bump oraidex-common version to 2.0.4 and enhance toDisplay fun…
Browse files Browse the repository at this point in the history
…ction for improved precision and validation
  • Loading branch information
trungbach committed Dec 23, 2024
1 parent f3d8e48 commit 61a4654
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/oraidex-common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oraichain/oraidex-common",
"version": "2.0.3",
"version": "2.0.4",
"main": "./build/index.js",
"module": "./build/index.js",
"types": "./build/index.d.ts",
Expand Down
7 changes: 6 additions & 1 deletion packages/oraidex-common/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ export const toDisplay = (amount: string | bigint, sourceDecimals: number = 6, d
// guarding conditions to prevent crashing
const validatedAmount = typeof amount === "string" ? BigInt(amount || "0") : amount;
const displayDecimals = Math.min(truncDecimals, desDecimals);
const returnAmount = validatedAmount / BigInt(10 ** (sourceDecimals - displayDecimals));
const returnAmount = BigInt(
new BigDecimal(validatedAmount)
.div(10 ** (sourceDecimals - displayDecimals))
.toString()
.split(".")[0]
);
// save calculation by using cached atomic
return Number(returnAmount) / (displayDecimals === truncDecimals ? atomic : 10 ** displayDecimals);
};
Expand Down
6 changes: 4 additions & 2 deletions packages/oraidex-common/tests/helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ describe("should helper functions in helper run exactly", () => {
[0.0006863532, 6, "686"],
[1, 1, "10"],
[1.5555, 1, "15"],
[1.5555, 0, "1"]
[1.5555, 0, "1"],
[null, 0, "0"]
])(
"toAmount number %.7f with decimal %d should return %s",
(amount: number, decimal: number, expectedAmount: string) => {
Expand All @@ -238,7 +239,8 @@ describe("should helper functions in helper run exactly", () => {
["1000", 6, "0.001", 6],
["454136345353413531", 15, "454.136345", 6],
["454136345353413531", 15, "454.13", 2],
["100000000000000", 18, "0.0001", 6]
["100000000000000", 18, "0.0001", 6],
["100", 0, "100", 6]
])(
"toDisplay number %d with decimal %d should return %s",
(amount: string, decimal: number, expectedAmount: string, desDecimal: number) => {
Expand Down
12 changes: 6 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3757,7 +3757,7 @@
ethers "^5.5.1"
lodash "^4.17.21"

"@oraichain/[email protected]", "@oraichain/common@^1.2.1-beta13":
"@oraichain/[email protected]":
version "1.2.7"
resolved "https://registry.yarnpkg.com/@oraichain/common/-/common-1.2.7.tgz#5063b20967eac14b5fc94e75f02afa6685565507"
integrity sha512-yOaEpjxtYwGkOxzSiqYwULfK0KSGdBj0ziQLZjQhM6yhW3MzTK70nAORW64ATKomSlBQFy9I/6MFsGkQBHiv3w==
Expand Down Expand Up @@ -3820,10 +3820,10 @@
resolved "https://registry.yarnpkg.com/@oraichain/immutable/-/immutable-4.3.9.tgz#ff8d5a7b39b5b01f3f72a902cffbfea32ccb20c3"
integrity sha512-INpHnhL970OCkR7I71Kssb2aLl2l4Y/x8W6FlyRO0KmC8GHjxc/hlNB1t44BiI7lkOYmcWMRQoC8dwParsp1RQ==

"@oraichain/[email protected].0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@oraichain/oraidex-common/-/oraidex-common-2.0.0.tgz#4498d1e470717bddab45502dcaaec47738a14516"
integrity sha512-IUZLBIiA89dXMrAoRNtg9yAAHYnysC1LE4gPa5ZGyyPuqXpRLMyAOwdHkgnOuFtglQyL+F2Q7cSV6ixM55AdVw==
"@oraichain/[email protected].2":
version "2.0.2"
resolved "https://registry.yarnpkg.com/@oraichain/oraidex-common/-/oraidex-common-2.0.2.tgz#3f15ca75b92a0cf55948671958dd3e21a84f0701"
integrity sha512-JA8znyCnpZdenxGJU06WisJBa7aeDeqL4ZQHAlC/i9mS1KzIPUvaArgpT7e5eVM8RNj1mz/Xq1yVxjXQ9QUajA==
dependencies:
"@cosmjs/amino" "^0.32.4"
"@cosmjs/cosmwasm-stargate" "^0.32.4"
Expand All @@ -3834,7 +3834,7 @@
"@ethersproject/providers" "^5.0.10"
"@injectivelabs/sdk-ts" "1.12.1"
"@keplr-wallet/types" "^0.11.38"
"@oraichain/common" "^1.2.1-beta13"
"@oraichain/common" "1.2.7"
"@oraichain/oraidex-contracts-sdk" latest
"@ton/core" "0.56.3"
"@ton/crypto" "3.3.0"
Expand Down

0 comments on commit 61a4654

Please sign in to comment.