-
Notifications
You must be signed in to change notification settings - Fork 984
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#17896] Raw balances using big number #17920
Conversation
(defn- calculate-raw-balance | ||
[raw-balance decimals] | ||
(if-let [n (utils.number/parse-int raw-balance nil)] | ||
(/ n (Math/pow 10 (utils.number/parse-int decimals))) | ||
0)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utils.money/token->unit
is able to do the same and works with big numbers, so now it is being used
(* (total-token-value-in-all-chains token) | ||
(-> token :market-values-per-currency :usd :price)))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utils.money/crypto->fiat
does the same but supports bignumbers, so now it's being used
@@ -1,6 +1,7 @@ | |||
(ns status-im2.contexts.wallet.common.utils |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Besides using functions in utils.money, this file also had a refactor
(defn- fix-chain-id-keys | ||
(defn- fix-balances-per-chain | ||
[token] | ||
(update token :balances-per-chain update-keys (comp utils.number/parse-int name))) | ||
(-> token | ||
(update :balances-per-chain update-vals #(update % :raw-balance money/bignumber)) | ||
(update :balances-per-chain update-keys (comp utils.number/parse-int name)))) | ||
|
||
(rf/reg-event-fx | ||
:wallet/store-wallet-token | ||
(fn [{:keys [db]} [raw-tokens-data]] | ||
(let [tokens (-> raw-tokens-data | ||
(update-keys name) | ||
(update-vals #(cske/transform-keys csk/->kebab-case %)) | ||
(update-vals #(mapv fix-chain-id-keys %))) | ||
(update-vals #(mapv fix-balances-per-chain %))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, when the token raw-balance is received it is being stored as bignumber instances
(let [result (rf/sub [sub-name]) | ||
balance-0x1 (money/bignumber 3250) | ||
balance-0x2 (money/bignumber 2100)] | ||
|
||
(is (= {"0x1" 3250 "0x2" 2100} | ||
(rf/sub [sub-name]))))) | ||
(is (money/equal-to balance-0x1 (get result "0x1"))) | ||
(is (money/equal-to balance-0x2 (get result "0x2")))))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is now testing against bignumber instances. the function utils.money/equal-to
is needed to check equality between them.
Jenkins BuildsClick to see older builds (20)
|
(defn add | ||
(defn- add* | ||
[bn1 n2] | ||
(.add ^js bn1 n2)) | ||
|
||
(def add | ||
"Add with defaults, this version is able to receive `nil` and takes them as 0." | ||
(fnil add* (bignumber 0) (bignumber 0))) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a more flexible version of add
.
I'd really like to get your feedback about this change.
Comparison between this version and the previous one:
(reduce money/add [])
;; Before:
;; Exception about null has no .add method
;; Now:
;; => (bignumber 0)
(reduce money/add [(money/bignumber 10)])
;; Before:
;; => (bignumber 10)
;; Now:
;; => (bignumber 10)
(reduce money/add [(money/bignumber 10) (money/bignumber 20) (money/bignumber 30)])
;; Before:
;; => (bignumber 60)
;; Now:
;; => (bignumber 60)
(money/add (money/bignumber 10))
;; Before:
;; Exception about no method plus() found in null
;; Now:
;; => (bignumber 10)
(money/add (money/bignumber 10) nil)
;; Before:
;; Exception about no method plus() found in null
;; Now:
;; => (bignumber 10)
(money/add nil (money/bignumber 10))
;; Before:
;; Exception about null has no .add method
;; Now:
;; => (bignumber 10)
(money/add nil nil)
;; Before:
;; Exception about null has no .add method
;; Now:
;; => (bignumber 0)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a perfect solution to me for dealing with nil
values as we should not allow the app to crash on nil
values.
Thanks for comparsion @ulisesmac!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀
(map (comp :raw-balance val)) | ||
(reduce money/add))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️
total-units-in-all-chains (-> balances-per-chain | ||
(total-raw-balance-in-all-chains) | ||
(money/token->unit decimals))] | ||
(money/crypto->fiat total-units-in-all-chains usd-price))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love the naming of the methods. Clean and neat. 🚀
(defn add | ||
(defn- add* | ||
[bn1 n2] | ||
(.add ^js bn1 n2)) | ||
|
||
(def add | ||
"Add with defaults, this version is able to receive `nil` and takes them as 0." | ||
(fnil add* (bignumber 0) (bignumber 0))) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a perfect solution to me for dealing with nil
values as we should not allow the app to crash on nil
values.
Thanks for comparsion @ulisesmac!
@ulisesmac can you please resolve conflicts and update it? |
930bc66
to
4b5cb07
Compare
@churik No problem! done! |
@ulisesmac |
4b5cb07
to
2fb8a5c
Compare
(let [tokens (map (fn [token] | ||
(assoc token | ||
:networks (utils/network-list token networks) | ||
:total-balance (utils/total-token-value-in-all-chains token) | ||
:total-balance-fiat (utils/calculate-balance token))) | ||
(:tokens account)) | ||
|
||
sorted-tokens | ||
(sort-by :name compare tokens) | ||
filtered-tokens | ||
(filter #(or (string/starts-with? (string/lower-case (:name %)) | ||
(string/lower-case query)) | ||
(string/starts-with? (string/lower-case (:symbol %)) | ||
(string/lower-case query))) | ||
sorted-tokens)] | ||
(let [tokens (map (fn [token] | ||
(assoc token | ||
:networks (utils/network-list token networks) | ||
:total-balance (utils/total-token-units-in-all-chains token) | ||
:total-balance-fiat 0)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@churik
For the wallet flow:
- Select an account
- Select Send button
- Write an address (0x123..)
- Select an address
the fiat balance is being returned as zero as part of this PR.
The reason is that the value has a bug in develop, and its calculation was perfomed using code that I erased in this PR, so instead of fixing that bug here, I created an issue:
to properly address it. And set it as zero for this PR.
cc @briansztamfater
@ulisesmac as you mentioned that balance is 0 now (I checked it) I do not think that it is testable right now, is it true? I'm blocking PR for testing until the issue is resolved, let me know when it is ready, thanks! |
@ulisesmac Nightly: FILE.2023-12-06.17.49.05.mp4PR: IMG_1884.MP4 |
62% of end-end tests have passed
Not executed tests (1)Failed tests (14)Click to expandClass TestGroupChatMultipleDeviceMergedNewUI:
Class TestCommunityMultipleDeviceMerged:
Class TestCommunityOneDeviceMerged:
Expected to fail tests (4)Click to expandClass TestOneToOneChatMultipleSharedDevicesNewUi:
Class TestGroupChatMultipleDeviceMergedNewUI:
Class TestCommunityOneDeviceMerged:
Passed tests (30)Click to expandClass TestOneToOneChatMultipleSharedDevicesNewUiTwo:
Class TestActivityMultipleDevicePRTwo:
Class TestGroupChatMultipleDeviceMergedNewUI:
Class TestCommunityMultipleDeviceMergedTwo:
Class TestOneToOneChatMultipleSharedDevicesNewUi:
Class TestDeepLinksOneDevice:
Class TestActivityCenterContactRequestMultipleDevicePR:
Class TestActivityMultipleDevicePR:
Class TestCommunityOneDeviceMerged:
|
Thanks @churik Will check it and fix it 👍 |
2fb8a5c
to
866f5d1
Compare
556036f
to
8b0d40b
Compare
@churik Sorry for the delay. We had a problem showing the balances, but now it's fixed! About what I said related to balances:
To test it, we have two options:
A random address I found in etherscan: is less than the total balance in this branch: since, as I said, the tokens being more than 1000 are not calculated correctly. This is just a small example to show the difference in calculations. |
Another account to test: https://etherscan.io/address/0x7bdf634f898ad124a9adb7a1aff95a95aa864cbb Just keeping here for the history, that only |
78% of end-end tests have passed
Failed tests (3)Click to expandClass TestOneToOneChatMultipleSharedDevicesNewUi:
Class TestCommunityMultipleDeviceMergedTwo:
Class TestCommunityOneDeviceMerged:
Expected to fail tests (8)Click to expandClass TestCommunityMultipleDeviceMergedTwo:
Class TestOneToOneChatMultipleSharedDevicesNewUi:
Class TestCommunityMultipleDeviceMerged:
Class TestGroupChatMultipleDeviceMergedNewUI:
Class TestOneToOneChatMultipleSharedDevicesNewUiTwo:
Class TestCommunityOneDeviceMerged:
Passed tests (38)Click to expandClass TestCommunityMultipleDeviceMergedTwo:
Class TestDeepLinksOneDevice:
Class TestOneToOneChatMultipleSharedDevicesNewUi:
Class TestCommunityMultipleDeviceMerged:
Class TestActivityMultipleDevicePR:
Class TestGroupChatMultipleDeviceMergedNewUI:
Class TestActivityMultipleDevicePRTwo:
Class TestOneToOneChatMultipleSharedDevicesNewUiTwo:
Class TestActivityCenterContactRequestMultipleDevicePR:
Class TestCommunityOneDeviceMerged:
|
* Add `utils.money/add` version able to work with `nil`s * Store token raw-balance as big-number * Improve account balance calculations using `utils.money` existing functions * Update `:wallet/tokens-filtered` sub * Make `prettify-balance` able to work with bignumbers
@OmarBasem Since you are going to modify that code again, this might interest you. I think it's due to two reasons:
In this PR I'm using bignumbers for the total balance calculation -it may give you a hint-, also testing with more than 1000 units per token can help. |
fixes #17896
Summary
The previous implementation was using js/parseInt to process the raw-balances, this PR stores them as big-number instances in re-frame.
This had some problems, if we had 100 or more units of a token, the calculations resulted in a
0
, now using bignumbers that issue has been fixed.Changes
utils.money/add
able to receivenil
valuesutils.money
functions.Testing notes
The app should work as expected. Previously, if we had 100 or more token units, the result listed in the account cards in the wallet home page was
$0.00
. Now it shows the proper value.Platforms
Areas that maybe impacted
Wallet home page in the account cards.
Steps to test
status: ready