diff --git a/src/status_im/chat/models/message.cljs b/src/status_im/chat/models/message.cljs index b0da59f896..6aa059ce99 100644 --- a/src/status_im/chat/models/message.cljs +++ b/src/status_im/chat/models/message.cljs @@ -17,7 +17,9 @@ [status-im.transport.message.v1.protocol :as protocol] [status-im.data-store.messages :as messages-store] [status-im.data-store.user-statuses :as user-statuses-store] - [status-im.utils.datetime :as datetime])) + [status-im.ui.screens.currency-settings.subs :as currency-settings] + [status-im.utils.datetime :as datetime] + [clojure.string :as string])) (def receive-interceptors [(re-frame/inject-cofx :random-id) @@ -385,19 +387,30 @@ :as request} {:keys [params command handler-data content-type]} network + currency prices tx-hash] (let [content (if request {:request-command request-command ;; TODO janherich this is technically not correct, but works for now :request-command-ref (:ref command) - :params (assoc request-params :bot-db (:bot-db params)) + :params (assoc request-params + :bot-db (:bot-db params) + :fiat-amount (money/fiat-amount-value (:amount request-params) + (-> request-params :asset keyword) + currency + prices) + :currency (name currency)) :prefill prefill :prefill-bot-db prefillBotDb} {:params (cond-> params (= (:name command) constants/command-send) (assoc :network (ethereum/network-names network) - :fiat-amount (money/usd-amount (:amount params) (-> params :asset keyword) prices) + :fiat-amount (money/fiat-amount-value (:amount params) + (-> params :asset keyword) + currency + prices) + :currency (name currency) :tx-hash tx-hash))}) content' (assoc content :command (:name command) @@ -434,12 +447,14 @@ [{{:keys [current-public-key chats network prices] :as db} :db :keys [now] :as cofx} params] (let [{{:keys [handler-data to-message command] :as content} :command chat-id :chat-id} params ;; We send commands to deleted chats as well, i.e. signed later transactions - chat (or (get chats chat-id) {:chat-id chat-id}) - request (:request handler-data) - command-name (:name command) - tx-hash (get-in db [:wallet :send-transaction :tx-hash])] + chat (or (get chats chat-id) {:chat-id chat-id}) + request (:request handler-data) + command-name (:name command) + tx-hash (get-in db [:wallet :send-transaction :tx-hash]) + currency (-> (currency-settings/get-user-currency db) name string/upper-case keyword)] (handlers-macro/merge-fx cofx - (upsert-and-send (prepare-command-message current-public-key chat now request content network prices tx-hash)) + (upsert-and-send (prepare-command-message current-public-key chat now request content + network currency prices tx-hash)) (console-events/console-respond-command-messages command handler-data) (requests-events/request-answered chat-id to-message) (update-transactions command-name tx-hash {:with-delay? false})))) diff --git a/src/status_im/chat/views/message/message.cljs b/src/status_im/chat/views/message/message.cljs index 9771d9c9c8..f4833e2d79 100644 --- a/src/status_im/chat/views/message/message.cljs +++ b/src/status_im/chat/views/message/message.cljs @@ -77,7 +77,7 @@ (defview message-content-command-send [{:keys [content timestamp-str outgoing group-chat]}] (letsubs [network [:network-name]] - (let [{{:keys [amount fiat-amount tx-hash asset] send-network :network} :params} content + (let [{{:keys [amount fiat-amount tx-hash asset currency] send-network :network} :params} content recipient-name (get-in content [:params :bot-db :public :recipient]) amount-text-long? (< 10 (count amount)) network-mismatch? (and (seq send-network) (not= network send-network))] @@ -96,7 +96,7 @@ (when fiat-amount [react/view style/command-send-fiat-amount [react/text {:style style/command-send-fiat-amount-text} - (str "~ " fiat-amount " " (i18n/label :usd-currency))]]) + (str "~ " fiat-amount " " (or currency (i18n/label :usd-currency)))]]) (when (and group-chat recipient-name) [react/text {:style style/command-send-recipient-text} diff --git a/src/status_im/chat/views/message/request_message.cljs b/src/status_im/chat/views/message/request_message.cljs index f52a2c84d0..b0c91db5e0 100644 --- a/src/status_im/chat/views/message/request_message.cljs +++ b/src/status_im/chat/views/message/request_message.cljs @@ -89,9 +89,8 @@ (merge command {:prefill prefill :prefill-bot-db (or prefill-bot-db prefillBotDb)}) command) - {:keys [amount asset] request-network :network} params + {:keys [amount asset fiat-amount currency] request-network :network} params recipient-name (get-in params [:bot-db :public :recipient]) - usd-amount (money/usd-amount amount (keyword asset) prices) network-mismatch? (and request-network (not= request-network network)) on-press-handler (cond network-mismatch? nil @@ -119,7 +118,7 @@ asset]]] [view st/command-request-fiat-amount-row [text {:style st/command-request-fiat-amount-text} - (str "~ " usd-amount " " (i18n/label :usd-currency))]] + (str "~ " fiat-amount " " (or currency (i18n/label :usd-currency)))]] (when (and group-chat recipient-name) [text {:style st/command-request-recipient-text} diff --git a/src/status_im/ui/screens/currency_settings/events.cljs b/src/status_im/ui/screens/currency_settings/events.cljs index a3b4644714..ae6da94cf6 100644 --- a/src/status_im/ui/screens/currency_settings/events.cljs +++ b/src/status_im/ui/screens/currency_settings/events.cljs @@ -1,10 +1,14 @@ (ns status-im.ui.screens.currency-settings.events (:require [status-im.ui.screens.accounts.events :as accounts] - [status-im.utils.handlers :as handlers])) + [status-im.utils.handlers :as handlers] + [status-im.utils.handlers-macro :as handlers-macro] + [status-im.ui.screens.wallet.events :as wallet.events])) (handlers/register-handler-fx :wallet.settings/set-currency (fn [{:keys [db] :as cofx} [_ currency]] (let [settings (get-in db [:account/account :settings]) new-settings (assoc-in settings [:wallet :currency] currency)] - (accounts/update-settings new-settings cofx)))) + (handlers-macro/merge-fx cofx + (accounts/update-settings new-settings) + (wallet.events/update-wallet))))) diff --git a/src/status_im/ui/screens/currency_settings/subs.cljs b/src/status_im/ui/screens/currency_settings/subs.cljs index 29681943c7..6340677a34 100644 --- a/src/status_im/ui/screens/currency_settings/subs.cljs +++ b/src/status_im/ui/screens/currency_settings/subs.cljs @@ -1,8 +1,10 @@ (ns status-im.ui.screens.currency-settings.subs (:require [re-frame.core :as re-frame])) +(defn get-user-currency [db] + (get-in db [:account/account :settings :wallet :currency] :usd)) + (re-frame/reg-sub :wallet.settings/currency - :<- [:get-current-account] - (fn [current-account] - (or (get-in current-account [:settings :wallet :currency]) :usd))) + (fn [db] + (get-user-currency db))) diff --git a/src/status_im/ui/screens/wallet/events.cljs b/src/status_im/ui/screens/wallet/events.cljs index 3ec14c42e0..42d70fe176 100644 --- a/src/status_im/ui/screens/wallet/events.cljs +++ b/src/status_im/ui/screens/wallet/events.cljs @@ -104,37 +104,40 @@ (defn tokens-symbols [v chain] (set/difference (set v) (set (map :symbol (tokens/nfts-for chain))))) +(defn update-wallet [{{:keys [web3 network network-status] {:keys [address settings]} :account/account :as db} :db}] + (let [network (get-in db [:account/account :networks network]) + chain (ethereum/network->chain-keyword network) + mainnet? (= :mainnet chain) + assets (get-in settings [:wallet :visible-tokens chain]) + tokens (tokens-symbols (get-in settings [:wallet :visible-tokens chain]) chain) + currency-id (or (get-in settings [:wallet :currency]) :usd) + currency (get constants/currencies currency-id)] + (when (not= network-status :offline) + {:get-balance {:web3 web3 + :account-id address + :success-event :update-balance-success + :error-event :update-balance-fail} + :get-tokens-balance {:web3 web3 + :account-id address + :symbols assets + :chain chain + :success-event :update-token-balance-success + :error-event :update-token-balance-fail} + :get-prices {:from (if mainnet? (conj tokens "ETH") ["ETH"]) + :to [(:code currency)] + :success-event :update-prices-success + :error-event :update-prices-fail} + :db (-> db + (clear-error-message :prices-update) + (clear-error-message :balance-update) + (assoc-in [:wallet :balance-loading?] true) + (assoc :prices-loading? true))}))) + ;; Handlers (handlers/register-handler-fx :update-wallet - (fn [{{:keys [web3 network network-status] {:keys [address settings]} :account/account :as db} :db} _] - (let [network (get-in db [:account/account :networks network]) - chain (ethereum/network->chain-keyword network) - mainnet? (= :mainnet chain) - assets (get-in settings [:wallet :visible-tokens chain]) - tokens (tokens-symbols (get-in settings [:wallet :visible-tokens chain]) chain) - currency-id (or (get-in settings [:wallet :currency]) :usd) - currency (get constants/currencies currency-id)] - (when (not= network-status :offline) - {:get-balance {:web3 web3 - :account-id address - :success-event :update-balance-success - :error-event :update-balance-fail} - :get-tokens-balance {:web3 web3 - :account-id address - :symbols assets - :chain chain - :success-event :update-token-balance-success - :error-event :update-token-balance-fail} - :get-prices {:from (if mainnet? (conj tokens "ETH") ["ETH"]) - :to [(:code currency)] - :success-event :update-prices-success - :error-event :update-prices-fail} - :db (-> db - (clear-error-message :prices-update) - (clear-error-message :balance-update) - (assoc-in [:wallet :balance-loading?] true) - (assoc :prices-loading? true))})))) + (fn [cofx _] + (update-wallet cofx))) (handlers/register-handler-fx :update-transactions diff --git a/src/status_im/utils/money.cljs b/src/status_im/utils/money.cljs index c625cd100e..e912d884f7 100644 --- a/src/status_im/utils/money.cljs +++ b/src/status_im/utils/money.cljs @@ -143,10 +143,10 @@ (when (and amount balance) (.greaterThanOrEqualTo balance amount))) -(defn usd-amount [amount-str from prices] +(defn fiat-amount-value [amount-str from to prices] (-> amount-str (js/parseFloat) bignumber - (crypto->fiat (get-in prices [from :USD :price] (bignumber 0))) + (crypto->fiat (get-in prices [from to :price] (bignumber 0))) (with-precision 2) str))