diff --git a/src/status_im/constants.cljs b/src/status_im/constants.cljs index e2ec395ba9..3ada1e1a75 100644 --- a/src/status_im/constants.cljs +++ b/src/status_im/constants.cljs @@ -639,3 +639,5 @@ :more-than-five-minutes 4}) (def ^:const wallet-connect-transaction-refresh-interval-ms 10000) + +(def ^:const native-token-symbol "ETH") diff --git a/src/status_im/contexts/wallet/send/utils.cljs b/src/status_im/contexts/wallet/send/utils.cljs index ec55be45da..9d390fc676 100644 --- a/src/status_im/contexts/wallet/send/utils.cljs +++ b/src/status_im/contexts/wallet/send/utils.cljs @@ -55,7 +55,7 @@ (money/add bonder-fees))) (-> path :amount-out money/from-hex))) -(defn- convert-wei-to-eth +(defn convert-wei-to-eth [amount native-token? token-decimals] (money/with-precision (if native-token? diff --git a/src/status_im/subs/wallet/activities.cljs b/src/status_im/subs/wallet/activities.cljs index 2cdfa398ab..ba26119b33 100644 --- a/src/status_im/subs/wallet/activities.cljs +++ b/src/status_im/subs/wallet/activities.cljs @@ -4,13 +4,12 @@ [re-frame.core :as rf] [status-im.constants :as constants] [status-im.contexts.wallet.common.activity-tab.constants :as activity-constants] + [status-im.contexts.wallet.send.utils :as send-utils] [utils.collection :as collection] [utils.datetime :as datetime] [utils.hex :as hex] [utils.money :as money])) -(def ^:private precision 6) - (defn- hex->number [hex-str] (-> hex-str @@ -19,20 +18,20 @@ str)) (defn- hex->amount - [hex-str] - (-> hex-str - hex->number - money/wei->ether - (money/with-precision precision) - str)) + [hex-str token-symbol token-decimals] + (let [native-token? (= token-symbol constants/native-token-symbol)] + (-> hex-str + hex->number + (send-utils/convert-wei-to-eth native-token? token-decimals) + str))) (defn- get-token-amount - [{:keys [token-type]} amount] + [{:keys [token-type]} token-symbol amount decimals] (if (#{activity-constants/wallet-activity-token-type-erc-721 activity-constants/wallet-activity-token-type-erc-1155} token-type) (hex->number amount) - (hex->amount amount))) + (hex->amount amount token-symbol decimals))) (defn- get-address-context-tag [accounts-and-saved-addresses address] @@ -65,12 +64,25 @@ {:type :address :address address}))) (defn- process-base-activity - [{:keys [timestamp sender recipient token-in token-out chain-id-in chain-id-out activity-status] + [{:keys [timestamp sender recipient token-in token-out amount-in amount-out symbol-in symbol-out + chain-id-in chain-id-out activity-status] :as activity} - {:keys [chain-id->network-details accounts-and-saved-addresses]}] + {:keys [chain-id->network-details accounts-and-saved-addresses tokens-by-symbol]}] (let [token-id (some-> (or token-in token-out) :token-id hex->number) + amount-out (when amount-out + (get-token-amount token-out + symbol-out + amount-out + (get-in tokens-by-symbol + [symbol-out :decimals]))) + amount-in (when amount-in + (get-token-amount token-in + symbol-in + amount-in + (get-in tokens-by-symbol + [symbol-in :decimals]))) network-in (chain-id->network-details chain-id-in) network-out (chain-id->network-details chain-id-out)] (assoc activity @@ -82,32 +94,28 @@ :network-name-out (:full-name network-out) :network-logo-out (resources/get-network (:network-name network-out)) :status (activity-constants/wallet-activity-status->name activity-status) - :token-id token-id))) + :token-id token-id + :amount-out amount-out + :amount-in amount-in))) (defn- process-activity - [{:keys [activity-type token-out amount-out token-in amount-in approval-spender] :as activity} context] + [{:keys [activity-type approval-spender] + :as activity} + context] (let [base-activity (process-base-activity activity context)] (condp = activity-type activity-constants/wallet-activity-type-send - (assoc base-activity - :tx-type :send - :amount-out (get-token-amount token-out amount-out)) + (assoc base-activity :tx-type :send) activity-constants/wallet-activity-type-bridge - (assoc base-activity - :tx-type :bridge - :amount-out (get-token-amount token-out amount-out)) + (assoc base-activity :tx-type :bridge) activity-constants/wallet-activity-type-swap - (assoc base-activity - :tx-type :swap - :amount-in (get-token-amount token-in amount-in) - :amount-out (get-token-amount token-out amount-out)) + (assoc base-activity :tx-type :swap) activity-constants/wallet-activity-type-approval (assoc base-activity :tx-type :approval - :amount-out (get-token-amount token-out amount-out) :spender-tag (get-spender-context-tag approval-spender)) nil))) @@ -145,11 +153,13 @@ (rf/reg-sub :wallet/activities-for-current-viewing-account :<- [:wallet/all-activities] + :<- [:wallet/tokens-by-symbol] :<- [:wallet/current-viewing-account-address] :<- [:wallet/network-details] :<- [:wallet/accounts-and-saved-addresses] - (fn [[activities current-address network-details accounts-and-saved-addresses]] - (let [context {:chain-id->network-details (collection/index-by :chain-id network-details) + (fn [[activities tokens-by-symbol current-address network-details accounts-and-saved-addresses]] + (let [context {:tokens-by-symbol tokens-by-symbol + :chain-id->network-details (collection/index-by :chain-id network-details) :accounts-and-saved-addresses accounts-and-saved-addresses} activities (->> (get activities current-address) vals diff --git a/src/status_im/subs/wallet/activities_test.cljs b/src/status_im/subs/wallet/activities_test.cljs index f9b58e0089..6005e1791c 100644 --- a/src/status_im/subs/wallet/activities_test.cljs +++ b/src/status_im/subs/wallet/activities_test.cljs @@ -23,34 +23,45 @@ (swap! rf-db/app-db (fn [db] (-> db + (assoc-in [:wallet :tokens :by-symbols] + (list {:symbol "ETH" :decimals 18} + {:symbol "DAI" :decimals 18} + {:symbol "SNT" :decimals 18} + {:symbol "USDT" :decimals 6})) (assoc-in [:wallet :activities] - {"acc1" {1 {:activity-type constants/wallet-activity-type-send - :amount-out "0x1" - :sender "acc1" - :recipient "acc2" - :timestamp 1588291200} - 3 {:activity-type constants/wallet-activity-type-bridge - :amount-out "0x1" - :sender "acc1" - :recipient "acc4" - :timestamp 1588464000} - 4 {:activity-type constants/wallet-activity-type-swap - :amount-out "0x1" - :amount-in "0x1" - :sender "acc1" - :recipient "acc4" - :timestamp 1588464100} - 5 {:activity-type constants/wallet-activity-type-send - :amount-out "0x1" - :sender "acc1" - :recipient "acc4" - :timestamp 1588464050}} - "acc3" {6 {:activity-type constants/wallet-activity-type-receive - :amount-in "0x1" - :sender "acc4" - :recipient "acc3" - :timestamp 1588464000}}}) - (assoc-in [:wallet :current-viewing-account-address] "acc1")))) + {"0x1" {1 {:activity-type constants/wallet-activity-type-send + :amount-out "0x1" + :symbol-out "ETH" + :sender "0x1" + :recipient "0x2" + :timestamp 1588291200} + 3 {:activity-type constants/wallet-activity-type-bridge + :amount-out "0x1" + :symbol-out "ETH" + :sender "0x1" + :recipient "0x1" + :timestamp 1588464000} + 4 {:activity-type constants/wallet-activity-type-swap + :amount-out "0x1" + :symbol-out "ETH" + :amount-in "0x1" + :symbol-in "SNT" + :sender "0x1" + :recipient "0x1" + :timestamp 1588464100} + 5 {:activity-type constants/wallet-activity-type-send + :amount-out "0x1" + :symbol-out "ETH" + :sender "0x1" + :recipient "0x4" + :timestamp 1588464050}} + "0x3" {6 {:activity-type constants/wallet-activity-type-receive + :amount-in "0x1" + :symbol-out "ETH" + :sender "0x4" + :recipient "0x3" + :timestamp 1588464000}}}) + (assoc-in [:wallet :current-viewing-account-address] "0x1")))) (is (match? [{:title "May 3, 2020" @@ -58,39 +69,39 @@ :data [{:relative-date "May 3, 2020" :amount-out "0" :network-logo-out nil - :recipient "acc4" + :recipient "0x1" :tx-type :swap :network-name-out nil :status nil - :sender "acc1" + :sender "0x1" :timestamp 1588464100} {:relative-date "May 3, 2020" :amount-out "0" :network-logo-out nil - :recipient "acc4" + :recipient "0x4" :tx-type :send :network-name-out nil :status nil - :sender "acc1" + :sender "0x1" :timestamp 1588464050} {:relative-date "May 3, 2020" :amount-out "0" :network-logo-out nil - :recipient "acc4" + :recipient "0x1" :tx-type :bridge :network-name-out nil :status nil - :sender "acc1" + :sender "0x1" :timestamp 1588464000}]} {:title "May 1, 2020" :timestamp 1588291200 :data [{:relative-date "May 1, 2020" :amount-out "0" :network-logo-out nil - :recipient "acc2" + :recipient "0x2" :tx-type :send :network-name-out nil :status nil - :sender "acc1" + :sender "0x1" :timestamp 1588291200}]}] (rf/sub [sub-name]))))) diff --git a/src/status_im/subs/wallet/wallet.cljs b/src/status_im/subs/wallet/wallet.cljs index 6aef60beaa..71e3219c5d 100644 --- a/src/status_im/subs/wallet/wallet.cljs +++ b/src/status_im/subs/wallet/wallet.cljs @@ -8,6 +8,7 @@ [status-im.contexts.wallet.send.utils :as send-utils] [status-im.contexts.wallet.sheets.missing-keypair.view :as missing-keypair] [status-im.subs.wallet.add-account.address-to-watch] + [utils.collection] [utils.money :as money] [utils.number] [utils.security.core :as security])) @@ -73,6 +74,12 @@ :<- [:wallet] :-> :tokens) +(rf/reg-sub + :wallet/tokens-by-symbol + :<- [:wallet/tokens] + (fn [{:keys [by-symbol]}] + (utils.collection/index-by :symbol by-symbol))) + (rf/reg-sub :wallet/prices-per-token :<- [:wallet/tokens]