Skip to content

Commit

Permalink
some refactor based on feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
briansztamfater committed May 12, 2024
1 parent ea2796b commit 8f43abc
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 122 deletions.
2 changes: 0 additions & 2 deletions src/quo/foundations/colors.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,6 @@
(def danger "#E95460")

;; Danger with transparency
(def danger-opa-5 (alpha danger 0.05))
(def danger-opa-10 (alpha danger 0.1))
(def danger-opa-40 (alpha danger 0.4))

;;Solid
Expand Down
9 changes: 5 additions & 4 deletions src/status_im/contexts/wallet/common/utils.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,16 @@

(defn make-network-item
"This function generates props for quo/category component item"
[{:keys [network-name color on-change networks state label-props normal-checkbox?]}]
[{:keys [network-name color on-change networks state label-props type]}]
(cond-> {:title (string/capitalize (name network-name))
:image :icon-avatar
:image-props {:icon (resources/get-network network-name)
:size :size-20}
:action :selector
:action-props {:type (if (and (= :default state) (not normal-checkbox?))
:filled-checkbox
:checkbox)
:action-props {:type (or type
(if (= :default state)
:filled-checkbox
:checkbox))
:customization-color color
:checked? (contains? networks network-name)
:on-change on-change}}
Expand Down
12 changes: 6 additions & 6 deletions src/status_im/contexts/wallet/send/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,22 @@
token-decimals
:native-token?
native-token?
:to? false})
:receiver? false})
from-network-values-for-ui (send-utils/network-values-for-ui from-network-amounts-by-chain)
to-network-amounts-by-chain (send-utils/network-amounts-by-chain {:route chosen-route
:token-decimals
token-decimals
:native-token?
native-token?
:to? true})
:receiver? true})
to-network-values-for-ui (send-utils/network-values-for-ui to-network-amounts-by-chain)
sender-network-values (if routes-available?
(send-utils/network-amounts
{:network-values from-network-values-for-ui
:disabled-chain-ids disabled-from-chain-ids
:receiver-networks receiver-networks
:token-networks-ids token-networks-ids
:to? false})
:receiver? false})
(send-utils/reset-network-amounts-to-zero
sender-network-values))
receiver-network-values (if routes-available?
Expand All @@ -69,7 +69,7 @@
:disabled-chain-ids disabled-from-chain-ids
:receiver-networks receiver-networks
:token-networks-ids token-networks-ids
:to? true})
:receiver? true})
(send-utils/reset-network-amounts-to-zero
receiver-network-values))
network-links (when routes-available?
Expand Down Expand Up @@ -298,14 +298,14 @@
:disabled-chain-ids disabled-from-chain-ids
:receiver-networks receiver-networks
:token-networks-ids token-networks-ids
:to? false}))
:receiver? false}))
receiver-network-values (when token-available-networks-for-suggested-routes
(send-utils/loading-network-amounts
{:valid-networks token-available-networks-for-suggested-routes
:disabled-chain-ids disabled-from-chain-ids
:receiver-networks receiver-networks
:token-networks-ids token-networks-ids
:to? true}))
:receiver? true}))
request-params [transaction-type-param
from-address
to-address
Expand Down
12 changes: 7 additions & 5 deletions src/status_im/contexts/wallet/send/input_amount/style.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@
:width "100%"
:align-items :center})

(def token-not-available-container
(defn token-not-available-container
[theme]
{:height 90
:flex-direction :row
:background-color colors/danger-opa-5
:border-color colors/danger-opa-10
:background-color (colors/resolve-color :danger theme 5)
:border-color (colors/resolve-color :danger theme 10)
:border-width 1
:border-radius 12
:margin-horizontal 20
Expand All @@ -54,7 +55,8 @@
{:margin-left 8
:align-items :flex-start})

(def token-not-available-text
(defn token-not-available-text
[theme]
{:height 36
:flex 1
:color colors/danger-50})
:color (colors/resolve-color :danger theme 50)})
8 changes: 5 additions & 3 deletions src/status_im/contexts/wallet/send/input_amount/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[clojure.string :as string]
[quo.core :as quo]
[quo.foundations.colors :as colors]
[quo.theme]
[react-native.core :as rn]
[react-native.safe-area :as safe-area]
[reagent.core :as reagent]
Expand Down Expand Up @@ -81,18 +82,19 @@

(defn- token-not-available
[token-symbol receiver-networks token-networks]
(let [add-token-networks (fn []
(let [theme (quo.theme/use-theme)
add-token-networks (fn []
(let [chain-ids (concat receiver-networks
(mapv #(:chain-id %) token-networks))]
(rf/dispatch [:wallet/update-receiver-networks chain-ids])))]
[rn/view {:style style/token-not-available-container}
[rn/view {:style (style/token-not-available-container theme)}
[rn/view
[quo/icon :i/alert
{:size 16
:color colors/danger-50}]]
[rn/view {:style style/token-not-available-content-container}
[quo/text
{:style style/token-not-available-text
{:style (style/token-not-available-text theme)
:size :paragraph-2}
(i18n/label :t/token-not-available-on-receiver-networks {:token-symbol token-symbol})]
[quo/button
Expand Down
8 changes: 4 additions & 4 deletions src/status_im/contexts/wallet/send/routes/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@
chain-ids]))}])}])))

(defn render-network-values
[{:keys [network-values token-symbol on-press to? loading-routes?
[{:keys [network-values token-symbol on-press receiver? loading-routes?
token-not-supported-in-receiver-networks?]}]
[rn/view
(map-indexed (fn [index {:keys [chain-id total-amount type]}]
[rn/view
{:key (str (if to? "to" "from") "-" chain-id)
{:key (str (if receiver? "to" "from") "-" chain-id)
:style {:margin-top (if (pos? index) 11 7.5)}}
[quo/network-bridge
{:amount (if (= type :not-available)
Expand Down Expand Up @@ -192,7 +192,7 @@
%1
disabled-from-chain-ids
token-available-networks-for-suggested-routes)
:to? false
:receiver? false
:theme theme
:loading-routes? loading-routes?
:token-not-supported-in-receiver-networks? false}]
Expand All @@ -203,7 +203,7 @@
{:token-symbol token-symbol
:network-values receiver-network-values
:on-press on-press-to-network
:to? true
:receiver? true
:loading-routes? loading-routes?
:theme theme
:token-not-supported-in-receiver-networks? token-not-supported-in-receiver-networks?
Expand Down
118 changes: 62 additions & 56 deletions src/status_im/contexts/wallet/send/utils.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,22 @@
(money/wei->ether (reduce money/add (map calculate-gas-fee route))))

(defn network-amounts-by-chain
[{:keys [route token-decimals native-token? to?]}]
(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 native-token?
(money/wei->ether amount-units)
(money/token->unit amount-units
token-decimals))
6)
chain-id (if to? (get-in path [:to :chain-id]) (get-in path [:from :chain-id]))]
(update acc chain-id money/add amount)))
{}
route))
[{:keys [route token-decimals native-token? receiver?]}]
(reduce
(fn [acc path]
(let [amount-hex (if receiver? (:amount-in path) (:amount-out path))
amount-units (native-module/hex-to-number
(utils.hex/normalize-hex amount-hex))
amount (money/with-precision
(if native-token?
(money/wei->ether amount-units)
(money/token->unit amount-units
token-decimals))
6)
chain-id (if receiver? (get-in path [:to :chain-id]) (get-in path [:from :chain-id]))]
(update acc chain-id money/add amount)))
{}
route))

(defn network-values-for-ui
[amounts]
Expand Down Expand Up @@ -93,13 +94,13 @@
network-amounts))

(defn network-amounts
[{:keys [network-values disabled-chain-ids receiver-networks token-networks-ids to?]}]
[{:keys [network-values disabled-chain-ids receiver-networks token-networks-ids receiver?]}]
(let [disabled-set (set disabled-chain-ids)
receiver-networks-set (set receiver-networks)
network-values-keys (set (keys network-values))
routes-found? (pos? (count network-values-keys))
token-networks-ids-set (set token-networks-ids)
not-available-networks (if to?
not-available-networks (if receiver?
(filter #(not (token-networks-ids-set %))
receiver-networks)
[])
Expand All @@ -108,14 +109,14 @@
(reduce
(fn [acc k]
(if (or (contains? network-values-keys k)
(and to?
(and receiver?
(not (contains? receiver-networks-set
k))))
acc
(assoc acc k (money/bignumber "0"))))
network-values
disabled-chain-ids))
network-values-with-not-available-chains (if (and to? routes-found?)
network-values-with-not-available-chains (if (and receiver? routes-found?)
(let [network-values-keys
(set (keys
network-values-with-disabled-chains))]
Expand All @@ -127,64 +128,69 @@
network-values-with-disabled-chains
not-available-networks))
network-values-with-disabled-chains)]
(cond-> (->> network-values-with-not-available-chains
(map
(fn [[k v]]
{:chain-id k
:total-amount v
:type (cond
(contains? not-available-networks-set k) :not-available
(or to? (not (contains? disabled-set k))) :default
(and (not to?) (contains? disabled-set k)) :disabled)}))
(sort-by #(get network-priority-score (network-utils/id->network (:chain-id %))))
(filter
#(or (and to?
(or (contains? receiver-networks-set (:chain-id %))
(money/greater-than (:total-amount %) (money/bignumber "0"))))
(not to?)))
(vec))
(and to?
(cond-> (->>
network-values-with-not-available-chains
(map
(fn [[chain-id amount]]
{:chain-id chain-id
:total-amount amount
:type (cond
(contains? not-available-networks-set chain-id) :not-available
(or receiver? (not (contains? disabled-set chain-id))) :default
(and (not receiver?) (contains? disabled-set chain-id)) :disabled)}))
(sort-by (fn [network-amount]
(get network-priority-score
(network-utils/id->network (:chain-id network-amount)))))
(filter
(fn [network-amount]
(or (and receiver?
(or (contains? receiver-networks-set (:chain-id network-amount))
(money/greater-than (:total-amount network-amount) (money/bignumber "0"))))
(not receiver?))))
(vec))
(and receiver?
routes-found?
(< (count network-values-with-not-available-chains) available-networks-count))
(conj {:type :add}))))

(defn loading-network-amounts
[{:keys [valid-networks disabled-chain-ids receiver-networks token-networks-ids to?]}]
[{:keys [valid-networks disabled-chain-ids receiver-networks token-networks-ids receiver?]}]
(let [disabled-set (set disabled-chain-ids)
receiver-networks-set (set receiver-networks)
receiver-networks-count (count receiver-networks)
token-networks-ids-set (set token-networks-ids)
valid-networks-set (set valid-networks)
not-available-networks (if to?
not-available-networks (if receiver?
(filter #(not (token-networks-ids-set %)) receiver-networks)
[])
not-available-networks-set (set not-available-networks)
valid-networks (concat valid-networks
disabled-chain-ids
(when to?
(when receiver?
(filter #(not (valid-networks-set %))
not-available-networks)))]
(cond-> (->> valid-networks
(map (fn [k]
(cond->
{:chain-id k
:type (cond
(contains? not-available-networks-set k) :not-available
(or to?
(not (contains? disabled-set k))) :loading
(and (not to?) (contains? disabled-set k)) :disabled)}
(and (not to?) (contains? disabled-set k))
(assoc :total-amount (money/bignumber "0")))))
(sort-by (fn [item]
(map
(fn [chain-id]
(cond->
{:chain-id chain-id
:type (cond
(contains? not-available-networks-set chain-id) :not-available
(or receiver?
(not (contains? disabled-set chain-id))) :loading
(and (not receiver?) (contains? disabled-set chain-id)) :disabled)}
(and (not receiver?) (contains? disabled-set chain-id))
(assoc :total-amount (money/bignumber "0")))))
(sort-by (fn [network-amount]
(get network-priority-score
(network-utils/id->network (:chain-id item)))))
(network-utils/id->network (:chain-id network-amount)))))
(filter
(fn [%]
(or (and to? (contains? receiver-networks-set (:chain-id %)))
(and (not to?)
(not (contains? disabled-chain-ids (:chain-id %)))))))
(fn [network-amount]
(or (and receiver? (contains? receiver-networks-set (:chain-id network-amount)))
(and (not receiver?)
(not (contains? disabled-chain-ids (:chain-id network-amount)))))))
(vec))
(and to? (< receiver-networks-count available-networks-count)) (conj {:type :add}))))
(and receiver? (< receiver-networks-count available-networks-count)) (conj {:type :add}))))

(defn network-links
[route from-values-by-chain to-values-by-chain]
Expand Down

0 comments on commit 8f43abc

Please sign in to comment.