From e1d2b9b1c10077aeea3980f9d04a46b8affc5656 Mon Sep 17 00:00:00 2001 From: Mohsen Date: Fri, 6 Dec 2024 12:48:24 +0300 Subject: [PATCH] [#21709] fix: big number issue in swap max value (#21752) --- src/utils/number.cljs | 8 ++++---- src/utils/number_test.cljs | 7 ++++++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/utils/number.cljs b/src/utils/number.cljs index 27496d2bab..1499e74fb6 100644 --- a/src/utils/number.cljs +++ b/src/utils/number.cljs @@ -64,10 +64,10 @@ Example usage: (convert-to-whole-number 12345 2) ; => 123.45" [amount decimals] - (-> amount - (/ (Math/pow 10 decimals)) - (.toFixed decimals) - remove-trailing-zeroes)) + (when-let [[amount-bn divisor-bn] (money/->bignumbers amount (money/from-decimal decimals))] + (-> (money/div amount-bn divisor-bn) + (money/to-fixed decimals) + remove-trailing-zeroes))) (defn hex->whole [num decimals] diff --git a/src/utils/number_test.cljs b/src/utils/number_test.cljs index 9d8f426d6a..44a1b51904 100644 --- a/src/utils/number_test.cljs +++ b/src/utils/number_test.cljs @@ -1,6 +1,7 @@ (ns utils.number-test (:require [cljs.test :refer [deftest is testing]] + [utils.money :as money] [utils.number])) (deftest convert-to-whole-number-test @@ -21,7 +22,11 @@ (testing "handles zero amount" (is (= "0" (utils.number/convert-to-whole-number 0 2))) - (is (= "0" (utils.number/convert-to-whole-number 0 0))))) + (is (= "0" (utils.number/convert-to-whole-number 0 0)))) + + (testing "handles BigNumber amounts with 18 decimals" + (let [amount (money/bignumber "3141969777175276657")] + (is (= "3.141969777175276657" (utils.number/convert-to-whole-number amount 18)))))) (deftest parse-int-test (testing "defaults to zero"