Skip to content

Commit

Permalink
some refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
briansztamfater committed Dec 11, 2023
1 parent cc6a754 commit 3250a21
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 49 deletions.
4 changes: 2 additions & 2 deletions src/quo/components/wallet/token_input/component_spec.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
(h/describe "Wallet: Token Input"
(h/test "Token label renders"
(h/render [token-input/view
{:token {:symbol "snt"}
{:token :snt
:currency :eur
:conversion 1}])
(h/is-truthy (h/get-by-text "SNT")))

(h/test "Amount renders"
(h/render [token-input/view
{:token {:symbol "snt"}
{:token :snt
:currency :eur
:conversion 1}])
(h/is-truthy (h/get-by-text "€0.00"))))
5 changes: 3 additions & 2 deletions src/status_im2/contexts/wallet/common/utils.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@

(defn calculate-balance-for-token
[token]
(* (total-token-units-in-all-chains token)
(-> token :market-values-per-currency :usd :price)))
(money/bignumber
(money/mul (total-token-units-in-all-chains token)
(-> token :market-values-per-currency :usd :price))))

(defn calculate-balance
[tokens-in-account]
Expand Down
8 changes: 3 additions & 5 deletions src/status_im2/contexts/wallet/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@
(rf/defn clean-scanned-address
{:events [:wallet/clean-scanned-address]}
[{:keys [db]}]
{:db (dissoc db :wallet/scanned-address :wallet/send-address)})
{:db (-> db
(dissoc :wallet/scanned-address :wallet/send-address)
(update-in [:wallet :ui :send] dissoc :to-address))})

(rf/reg-event-fx :wallet/create-derived-addresses
(fn [{:keys [db]} [{:keys [sha3-pwd path]} on-success]]
Expand Down Expand Up @@ -426,10 +428,6 @@
(background-timer/clear-timeout current-timeout)
{:db (assoc db :wallet/local-suggestions [] :wallet/valid-ens-or-address? false)})))

(rf/reg-event-fx :wallet/select-send-address
(fn [{:keys [db]} [address]]
{:db (assoc db :wallet/send-address address)}))

(rf/reg-event-fx :wallet/get-address-details-success
(fn [{:keys [db]} [{:keys [hasActivity]}]]
{:db (assoc-in db
Expand Down
59 changes: 31 additions & 28 deletions src/status_im2/contexts/wallet/send/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -30,46 +30,49 @@
(update-in [:wallet :ui :send] dissoc :loading-suggested-routes?))}))

(rf/reg-event-fx :wallet/select-send-address
(fn [{:keys [db]} [address]]
{:db (assoc-in db [:wallet :ui :send :to-address] address)}))
(fn [{:keys [db]} [{:keys [address stack-id]}]]
(println "dsadadasd" address stack-id)
{:db (assoc-in db [:wallet :ui :send :to-address] address)
:fx [[:navigate-to-within-stack [:wallet-select-asset stack-id]]]}))

(rf/reg-event-fx :wallet/send-select-token
(fn [{:keys [db]} [token stack-id]]
(fn [{:keys [db]} [{:keys [token stack-id]}]]
{:db (assoc-in db [:wallet :ui :send :token] token)
:fx [[:navigate-to-within-stack [:wallet-send-input-amount stack-id]]]}))

(rf/reg-event-fx :wallet/get-suggested-routes
(fn [{:keys [db]} [amount]]
(let [wallet-address (get-in db [:wallet :current-viewing-account-address])
token (get-in db [:wallet :ui :send :token])
to-address (get-in db [:wallet :ui :send :to-address])
token-decimal (:decimals token)
token-id (:symbol token)
network-preferences [constants/mainnet-chain-id] ; TODO: don't hardcode network preferences
gas-rates constants/gas-rate-low
amount-in (money/mul (money/bignumber amount)
(money/from-decimal token-decimal))
from-address wallet-address
request-params [constants/send-type-transfer
from-address
to-address
(money/to-hex amount-in)
token-id
[]
[]
network-preferences
gas-rates
{}]
timestamp (datetime/timestamp)]
(fn [{:keys [db now]} [amount]]
(let [wallet-address (get-in db [:wallet :current-viewing-account-address])
token (get-in db [:wallet :ui :send :token])
to-address (get-in db [:wallet :ui :send :to-address])
token-decimal (:decimals token)
token-id (:symbol token)
network-preferences [constants/mainnet-chain-id] ; TODO: don't hardcode network
; preferences
gas-rates constants/gas-rate-low
amount-in (money/amount-in-hex amount token-decimal)
from-address wallet-address
disabled-from-chain-ids []
disabled-to-chain-ids []
from-locked-amount {}
request-params [constants/send-type-transfer
from-address
to-address
amount-in
token-id
disabled-from-chain-ids
disabled-to-chain-ids
network-preferences
gas-rates
from-locked-amount]]
{:db (-> db
(assoc-in [:wallet :ui :send :loading-suggested-routes?] true)
(assoc-in [:wallet :ui :send :suggested-routes-call-timestamp]
timestamp))
(assoc-in [:wallet :ui :send :suggested-routes-call-timestamp] now))
:json-rpc/call [{:method "wallet_getSuggestedRoutes"
:params request-params
:on-success (fn [suggested-routes]
(rf/dispatch [:wallet/suggested-routes-success suggested-routes
timestamp]))
now]))
:on-error (fn [error]
(rf/dispatch [:wallet/suggested-routes-error error])
(log/error "failed to get suggested routes"
Expand Down
9 changes: 4 additions & 5 deletions src/status_im2/contexts/wallet/send/input_amount/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,15 @@
(let [bottom (safe-area/get-bottom)
{:keys [currency]} (rf/sub [:profile/profile])
networks (rf/sub [:wallet/network-details])
wallet-send (get-in (rf/sub [:wallet]) [:ui :send])
token (:token wallet-send)
token (rf/sub [:wallet/wallet-send-token])
loading-suggested-routes? (rf/sub [:wallet/wallet-send-loading-suggested-routes?])
token-symbol (:symbol token)
limit-crypto (:total-balance token)
limit-fiat (:total-balance-fiat token)
conversion-rate (or rate 10)
input-value (reagent/atom "")
current-limit (reagent/atom {:amount limit-crypto
:currency token-symbol})
loading-suggested-routes? (:loading-suggested-routes? wallet-send)
handle-swap (fn [crypto?]
(let [num-value (parse-double @input-value)]
(reset! current-limit (if crypto?
Expand Down Expand Up @@ -96,8 +95,8 @@
:wallet-send-input-amount])}}]
(let [limit-label (make-limit-label @current-limit)
input-num-value (parse-double @input-value)
route (get-in (rf/sub [:wallet]) [:ui :send :route])
loading-suggested-routes? (get-in (rf/sub [:wallet]) [:ui :send :loading-suggested-routes?])
route (rf/sub [:wallet/wallet-send-route])
loading-suggested-routes? (rf/sub [:wallet/wallet-send-loading-suggested-routes?])
confirm-disabled? (or
(nil? route)
(empty? @input-value)
Expand Down
11 changes: 8 additions & 3 deletions src/status_im2/contexts/wallet/send/select_address/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
[input-value input-focused?]
(fn []
(let [scanned-address (rf/sub [:wallet/scanned-address])
send-address (get-in (rf/sub [:wallet]) [:ui :send :to-address])
send-address (rf/sub [:wallet/wallet-send-to-address])
valid-ens-or-address? (rf/sub [:wallet/valid-ens-or-address?])
chain-id (rf/sub [:chain-id])
contacts (rf/sub [:contacts/active])]
Expand Down Expand Up @@ -100,7 +100,10 @@
_ _]
(let [props {:on-press (fn []
(let [address (if accounts (:address (first accounts)) address)]
(when-not ens (rf/dispatch [:wallet/select-send-address address]))))
(when-not ens
(rf/dispatch [:wallet/select-send-address
{:address address
:stack-id :wallet-select-address}]))))
:active-state? false}]
(cond
(= type types/saved-address)
Expand Down Expand Up @@ -172,7 +175,9 @@
:type :primary
:disabled? (not valid-ens-or-address?)
:container-style style/button
:on-press #(js/alert "Not implemented yet")}
:on-press #(rf/dispatch [:wallet/select-send-address
{:address @input-value
:stack-id :wallet-select-address}])}
(i18n/label :t/continue)])]
[:<>
[quo/tabs
Expand Down
6 changes: 3 additions & 3 deletions src/status_im2/contexts/wallet/send/select_asset/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
[]
(fn [token _ _ _]
(let [on-press
#(if (= (:symbol token) "ETH")
(rf/dispatch [:wallet/send-select-token token :wallet-select-asset])
(js/alert "Only ETH transfers are allowed."))
#(rf/dispatch [:wallet/send-select-token
{:token token
:stack-id :wallet-select-asset}])
total-balance-formatted (.toFixed (:total-balance token) 2)
balance-fiat-formatted (.toFixed (:total-balance-fiat token) 2)
currency-symbol "$"]
Expand Down
32 changes: 31 additions & 1 deletion src/status_im2/subs/wallet/wallet.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,41 @@
:<- [:wallet]
:-> :ui)

(rf/reg-sub
:wallet/wallet-send
:<- [:wallet/ui]
:-> :send)

(rf/reg-sub
:wallet/tokens-loading?
:<- [:wallet/ui]
:-> :tokens-loading?)

(rf/reg-sub
:wallet/wallet-send-to-address
:<- [:wallet/wallet-send]
:-> :to-address)

(rf/reg-sub
:wallet/wallet-send-route
:<- [:wallet/wallet-send]
:-> :route)

(rf/reg-sub
:wallet/wallet-send-token
:<- [:wallet/wallet-send]
:-> :token)

(rf/reg-sub
:wallet/wallet-send-amount
:<- [:wallet/wallet-send]
:-> :amount)

(rf/reg-sub
:wallet/wallet-send-loading-suggested-routes?
:<- [:wallet/wallet-send]
:-> :loading-suggested-routes?)

(rf/reg-sub
:wallet/watch-address-activity-state
:<- [:wallet/ui]
Expand Down Expand Up @@ -48,7 +78,7 @@
:<- [:wallet/balances]
:<- [:wallet/tokens-loading?]
(fn [[accounts balances tokens-loading?]]
(mapv (fn [{:keys [color address] :as account}]
(mapv (fn [{:keys [color address type] :as account}]
(assoc account
:customization-color color
:type (if (= type :watch) :watch-only :empty)
Expand Down
4 changes: 4 additions & 0 deletions src/utils/money.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,7 @@
(schema/=> format-amount
[:=> [:cat [:maybe :int]]
[:maybe :string]])

(defn amount-in-hex
[amount token-decimal]
(to-hex (mul (bignumber amount) (from-decimal token-decimal))))

0 comments on commit 3250a21

Please sign in to comment.