fix: approval of amounts with big decimals (#21326)

Signed-off-by: Brian Sztamfater <brian@status.im>
This commit is contained in:
Brian Sztamfater 2024-09-27 09:18:54 -03:00 committed by GitHub
parent 0cbb6819b4
commit 9c73d9d03c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 14 additions and 1 deletions

View File

@ -391,7 +391,7 @@
approval-amount-required (:approval-amount-required route) approval-amount-required (:approval-amount-required route)
approval-amount-required-sanitized (-> approval-amount-required approval-amount-required-sanitized (-> approval-amount-required
(utils.hex/normalize-hex) (utils.hex/normalize-hex)
(native-module/hex-to-number)) (money/from-hex))
approval-contract-address (:approval-contract-address route) approval-contract-address (:approval-contract-address route)
data (native-module/encode-function-call data (native-module/encode-function-call
constants/contract-function-signature-erc20-approve constants/contract-function-signature-erc20-approve

View File

@ -151,6 +151,12 @@
[^js bn] [^js bn]
(str "0x" (to-string bn 16))) (str "0x" (to-string bn 16)))
(defn from-hex
[hex-str]
(try
(new BigNumber hex-str 16)
(catch :default _ nil)))
(defn wei->str (defn wei->str
([unit n display-unit] ([unit n display-unit]
(str (to-fixed (wei-> unit n)) " " display-unit)) (str (to-fixed (wei-> unit n)) " " display-unit))

View File

@ -83,3 +83,10 @@
1000001 "1M" 1000001 "1M"
10000000 "10M" 10000000 "10M"
100000000 "100M")) 100000000 "100M"))
(deftest from-hex-test
(is (= (money/bignumber "2840425681744351250") (money/from-hex "276b381bbb44d012")))
(is (= (money/bignumber "0") (money/from-hex "0")))
(is (= (money/bignumber "255") (money/from-hex "ff")))
(is (= (money/bignumber "4294967295") (money/from-hex "ffffffff")))
(is (= (money/bignumber "12345678901234567890") (money/from-hex "ab54a98ceb1f0ad2"))))