Skip to content

Commit

Permalink
wip refactor network from / to values to calculate it on event get su…
Browse files Browse the repository at this point in the history
…ggested routes success
  • Loading branch information
briansztamfater committed Mar 29, 2024
1 parent f22ca46 commit 5f2ea84
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
21 changes: 20 additions & 1 deletion src/status_im/contexts/wallet/send/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,26 @@
(fn [{:keys [db]} [suggested-routes timestamp]]
(when (= (get-in db [:wallet :ui :send :suggested-routes-call-timestamp]) timestamp)
(let [suggested-routes-data (cske/transform-keys transforms/->kebab-case-keyword suggested-routes)
chosen-route (:best suggested-routes-data)]
chosen-route (:best suggested-routes-data)
#_#_token (get-in db [:wallet :ui :send :token])
#_#_collectible (get-in db [:wallet :ui :send :collectible])
#_#_collection-data (:collection-data collectible)
#_#_collectible-data (:collectible-data collectible)
#_#_collectible-id (get-in collectible [:id :token-id])
#_#_token-symbol
(if collectible
(first (remove
string/blank?
[(:name collectible-data)
(str (:name collection-data) " #" collectible-id)]))
(:symbol token))
#_#_token-decimals (if collectible 0 (:decimals token))
#_#_network-amounts
(send-utils/network-amounts {:route chosen-route
:token-symbol token-symbol
:token-decimals token-decimals
:network-name network-name
:to? false})]
{:db (-> db
(assoc-in [:wallet :ui :send :suggested-routes] suggested-routes-data)
(assoc-in [:wallet :ui :send :route] chosen-route)
Expand Down
40 changes: 39 additions & 1 deletion src/status_im/contexts/wallet/send/utils.cljs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
(ns status-im.contexts.wallet.send.utils
(:require [utils.money :as money]))
(:require [utils.money :as money]
[native-module.core :as native-module]
[legacy.status-im.utils.hex :as utils.hex]))

(defn amount-in-hex
[amount token-decimal]
Expand All @@ -20,3 +22,39 @@
value1)))
{}
transaction-hashes))

(defn network-amounts
[{:keys [route token-symbol token-decimals network-name to?]}]
(letfn [(merge-sum [a b]
(merge-with money/add a b))]
(reduce (fn [acc path]
(let [amount-hex (if to? (:amount-in path) (:amount-out path))
amount-units (native-module/hex-to-number
(utils.hex/normalize-hex amount-hex))
amount (money/with-precision
(if (= token-symbol "ETH")
(money/wei->ether amount-units)
(money/token->unit amount-units
token-decimals))
6)
network-name (if (= network-name :mainnet) :ethereum network-name)]
(merge-sum acc {network-name amount})))
{}
route)))

(defn network-values
[amounts token-symbol]
(reduce-kv (fn [acc k v]
(assoc acc k {:amount v :token-symbol token-symbol}))
{}
amounts))

(defn network-values-sanitized
[amounts]
(into {}
(map (fn [[k v]]
[k
(if (money/equal-to (v :amount) 0)
(assoc v :amount "<0.01")
v)])
amounts)))

0 comments on commit 5f2ea84

Please sign in to comment.