Skip to content

Commit

Permalink
[#21483] fix: update collectible amounts after sending
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsen-ghafouri committed Jan 7, 2025
1 parent ba8cf8e commit c1cb39d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 14 deletions.
46 changes: 43 additions & 3 deletions src/status_im/contexts/wallet/collectible/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
(let [current-collectible-idx (get-in db [:wallet :accounts account :current-collectible-idx] 0)
collectibles-filter nil
data-type (collectible-data-types :header)
fetch-criteria {:fetch-type (fetch-type :fetch-if-not-cached)
fetch-criteria {:fetch-type (fetch-type :always-fetch)
:max-cache-age-seconds max-cache-age-seconds}
chain-ids (chain/chain-ids db)
request-params [request-id
Expand Down Expand Up @@ -182,6 +182,15 @@
:fx [(when all-chain-updated?
[:dispatch [:wallet/request-collectibles-for-account address]])]})))

(rf/reg-event-fx :wallet/collectible-amount-update-for-owners
(fn [{:keys [db]} [from-address]]
(let [to-address (get-in db
[:wallet :ui :send :transaction-for-signing :sendDetails :toAddress])
exist-to-account (contains? (get-in db [:wallet :accounts]) to-address)]
{:fx [[:dispatch [:wallet/request-collectibles-for-account from-address]]
(when exist-to-account
[:dispatch [:wallet/request-collectibles-for-account to-address]])]})))

(defn- update-collectibles-in-account
[existing-collectibles updated-collectibles]
(let [indexed-existing (utils.collection/index-by
Expand Down Expand Up @@ -318,19 +327,50 @@
(transforms/json->clj message))
{[collectible] :collectibles} response
known-collectible-info (get-in db [:wallet :ui :collectible :details])
current-account-address (get-in db [:wallet :current-viewing-account-address])
total-owned (collectible-utils/collectible-balance collectible
current-account-address)
merged-collectible (as-> known-collectible-info c
(merge-with keep-not-empty-value
c
collectible)
(update c
:ownership
collectible-utils/remove-duplicates-in-ownership))]
collectible-utils/remove-duplicates-in-ownership))
updated-collectible (assoc merged-collectible
:total-owned
total-owned)]
(if collectible
{:db (assoc-in db [:wallet :ui :collectible :details] merged-collectible)}
{:db (assoc-in db [:wallet :ui :collectible :details] updated-collectible)
:fx [[:dispatch [:wallet/update-collectibles-data updated-collectible]]]}
(log/error "failed to get collectible details"
{:event :wallet/get-collectible-details-done
:response response})))))

(defn group-collectibles-by-ownership-address
[collectible]
(->> (:ownership collectible)
(map (fn [{:keys [address]}]
[address collectible]))
(into {})))

(rf/reg-event-fx
:wallet/update-collectibles-data
(fn [{:keys [db]} [collectible]]
(let [collectibles-by-address (group-collectibles-by-ownership-address collectible)]
{:db (update-in db
[:wallet :accounts]
#(reduce-kv
(fn [accounts address updated-collectibles]
(if (contains? accounts address)
(update-in accounts
[address :collectibles]
update-collectibles-in-account
[updated-collectibles])
accounts))
%
collectibles-by-address))})))

(rf/reg-event-fx
:wallet/clear-collectible-details
(fn [{:keys [db]}]
Expand Down
17 changes: 13 additions & 4 deletions src/status_im/contexts/wallet/collectible/utils.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@
[taoensso.timbre :as log]
[utils.number :as utils.number]))

(defn total-collectible-balance
([ownership]
(reduce (fn [total {:keys [balance]}]
(+ total (or (js/parseInt balance) 0)))
0
ownership)))

(defn collectible-balance
([{:keys [ownership]} address]
(->> ownership
(some #(when (= address (:address %))
(:balance %)))
utils.number/parse-int)))
(let [balance (if address
(some #(when (= address (:address %))
(:balance %))
ownership)
(total-collectible-balance ownership))]
(utils.number/parse-int balance))))

(def supported-collectible-types
#{"image/jpeg"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
[status-im.contexts.wallet.collectible.utils :as utils]
[status-im.contexts.wallet.common.collectibles-tab.style :as style]
[status-im.contexts.wallet.common.empty-tab.view :as empty-tab]
[utils.i18n :as i18n]
[utils.re-frame :as rf]))
[utils.i18n :as i18n]))

(defn- loading-collectible-item
[_ index]
Expand Down Expand Up @@ -82,9 +81,9 @@
;; 1. If possible, move `collectibles-data` calculation to a subscription
;; 2. Optimization: do not recalculate all the collectibles, process only the new ones
(let [collectibles-data (map
(fn [{:keys [ownership] :as collectible}]
(let [total-owned (rf/sub [:wallet/total-owned-collectible ownership
current-account-address])]
(fn [collectible]
(let [total-owned (utils/collectible-balance collectible
current-account-address)]
(assoc collectible
:total-owned total-owned
:on-long-press on-collectible-long-press
Expand Down
10 changes: 8 additions & 2 deletions src/status_im/contexts/wallet/send/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,9 @@
transactions (utils/transactions->hash-to-transaction-map sent-transactions)
transaction-ids (->> transactions
vals
(map :hash))]
(map :hash))
collectible-tx? (send-utils/tx-type-collectible?
(-> db :wallet :ui :send :tx-type))]
{:db (-> db
(assoc-in [:wallet :ui :send :just-completed-transaction?] true)
(assoc-in [:wallet :ui :send :transaction-ids] transaction-ids)
Expand All @@ -572,7 +574,11 @@
:dispatch [:wallet/stop-and-clean-suggested-routes]}]]
[:dispatch-later
[{:ms 2000
:dispatch [:wallet/clean-just-completed-transaction]}]]]})))
:dispatch [:wallet/clean-just-completed-transaction]}]]
(when collectible-tx?
[:dispatch
[:wallet/collectible-amount-update-for-owners
(get-in db [:wallet :ui :send :transaction-for-signing :sendDetails :fromAddress])]])]})))

(rf/reg-event-fx
:wallet/transaction-failure
Expand Down

0 comments on commit c1cb39d

Please sign in to comment.