[#21709] fix: big number issue in swap max value (#21752)

This commit is contained in:
Mohsen 2024-12-06 12:48:24 +03:00 committed by GitHub
parent e60c20bc10
commit e1d2b9b1c1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 5 deletions

View File

@ -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]

View File

@ -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"