diff --git a/packages/oraidex-common/package.json b/packages/oraidex-common/package.json index 49023ef5..d41e0ebe 100644 --- a/packages/oraidex-common/package.json +++ b/packages/oraidex-common/package.json @@ -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", diff --git a/packages/oraidex-common/src/helper.ts b/packages/oraidex-common/src/helper.ts index 828f3db0..b3a9af9c 100644 --- a/packages/oraidex-common/src/helper.ts +++ b/packages/oraidex-common/src/helper.ts @@ -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); }; diff --git a/packages/oraidex-common/tests/helper.spec.ts b/packages/oraidex-common/tests/helper.spec.ts index 1b2bd6f6..2d938e70 100644 --- a/packages/oraidex-common/tests/helper.spec.ts +++ b/packages/oraidex-common/tests/helper.spec.ts @@ -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) => { @@ -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) => { diff --git a/yarn.lock b/yarn.lock index 40f18fbe..824ac645 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3757,7 +3757,7 @@ ethers "^5.5.1" lodash "^4.17.21" -"@oraichain/common@1.2.7", "@oraichain/common@^1.2.1-beta13": +"@oraichain/common@1.2.7": version "1.2.7" resolved "https://registry.yarnpkg.com/@oraichain/common/-/common-1.2.7.tgz#5063b20967eac14b5fc94e75f02afa6685565507" integrity sha512-yOaEpjxtYwGkOxzSiqYwULfK0KSGdBj0ziQLZjQhM6yhW3MzTK70nAORW64ATKomSlBQFy9I/6MFsGkQBHiv3w== @@ -3820,10 +3820,10 @@ resolved "https://registry.yarnpkg.com/@oraichain/immutable/-/immutable-4.3.9.tgz#ff8d5a7b39b5b01f3f72a902cffbfea32ccb20c3" integrity sha512-INpHnhL970OCkR7I71Kssb2aLl2l4Y/x8W6FlyRO0KmC8GHjxc/hlNB1t44BiI7lkOYmcWMRQoC8dwParsp1RQ== -"@oraichain/oraidex-common@2.0.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/oraidex-common@2.0.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" @@ -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"