Skip to content

Commit

Permalink
chore(wallet)_: Update Activity RPC
Browse files Browse the repository at this point in the history
Signed-off-by: Mohamed Javid <[email protected]>
  • Loading branch information
smohamedjavid committed Nov 7, 2024
1 parent ca22b67 commit e3409e6
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 78 deletions.
2 changes: 0 additions & 2 deletions src/status_im/contexts/wallet/account/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
(hot-reload/use-safe-unmount (fn []
(rf/dispatch [:wallet/close-account-page])
(rf/dispatch [:wallet/clean-current-viewing-account])))
(rn/use-mount
#(rf/dispatch [:wallet/fetch-activities-for-current-account]))
[rn/view {:style {:flex 1}}
[account-switcher/view
{:type :wallet-networks
Expand Down
139 changes: 101 additions & 38 deletions src/status_im/contexts/wallet/common/activity_tab/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@
[utils.re-frame :as rf]
[utils.transforms :as transforms]))

(defonce ^:private request-id-atom (atom 0))
(def ^:const limit-per-request 20)

(defn- get-unique-request-id
[]
(swap! request-id-atom inc)
@request-id-atom)
(def ^:private activity-transaction-id (comp hash :transaction))
(def ^:private nested-merge (partial merge-with merge))

(rf/reg-event-fx
:wallet/store-session-id-for-activity-filter-session
(fn [{:keys [db]} [address request-id]]
{:db (-> db
(assoc-in [:wallet :ui :activity-tab :request :request-id] request-id)
(assoc-in [:wallet :ui :activity-tab :request :address] address)
(assoc-in [:wallet :ui :activity-tab :request :initial-request?] true))}))

(rf/reg-event-fx
:wallet/fetch-activities-for-current-account
(fn [{:keys [db]}]
(let [address (-> db :wallet :current-viewing-account-address)
chain-ids (chain/chain-ids db)
request-id (get-unique-request-id)
filters {:period {:startTimestamp 0
:endTimestamp 0}
:types []
Expand All @@ -26,45 +31,103 @@
:collectibles []
:filterOutAssets false
:filterOutCollectibles false}
offset 0
limit 50
request-params [request-id [address] chain-ids filters offset limit]]
{:db (assoc-in db [:wallet :ui :activity-tab :request request-id] address)
limit limit-per-request
request-params [[address] chain-ids filters limit]]
{:db (-> db
(update-in [:wallet :activities] dissoc address)
(update-in [:wallet :ui :activity-tab] dissoc :request))
:fx [[:json-rpc/call
[{;; This method is deprecated and will be replaced by
;; "wallet_startActivityFilterSession"
;; https://github.com/status-im/status-mobile/issues/19864
:method "wallet_filterActivityAsync"
:params request-params
:on-error [:wallet/log-rpc-error
{:event :wallet/fetch-activities-for-current-account
:params request-params}]}]]]})))
[{:method "wallet_startActivityFilterSession"
:params request-params
:on-success [:wallet/store-session-id-for-activity-filter-session address]
:on-error [:wallet/log-rpc-error
{:event :wallet/fetch-activities-for-current-account
:params request-params}]}]]]})))

(def ^:private activity-transaction-id (comp hash :transaction))
(rf/reg-event-fx
:wallet/stop-activity-filter-session
(fn [{:keys [db]}]
(let [session-id (get-in db [:wallet :ui :activity-tab :request :request-id])
has-active-session? (some? session-id)
params [session-id]]
{:fx [(when has-active-session?
[:json-rpc/call
[{:method "wallet_stopActivityFilterSession"
:params params
:on-error [:wallet/log-rpc-error
{:event :wallet/stop-activity-filter-session
:params params}]}]])]})))

(rf/reg-event-fx
:wallet/get-more-for-activities-filter-session
(fn [{:keys [db]}]
(let [session-id (get-in db [:wallet :ui :activity-tab :request :request-id])
has-more? (get-in db [:wallet :ui :activity-tab :request :has-more?])
params [session-id limit-per-request]]
{:fx [(when has-more?
[:json-rpc/call
[{:method "wallet_getMoreForActivityFilterSession"
:params params
:on-error [:wallet/log-rpc-error
{:event :wallet/get-more-for-activities-filter-session
:params params}]}]])]})))

(rf/reg-event-fx
:wallet/reset-activities-filter-session
(fn [{:keys [db]}]
(let [session-id (get-in db [:wallet :ui :activity-tab :request :request-id])
has-active-session? (some? session-id)
params [session-id limit-per-request]]
{:fx [(when has-active-session?
[:json-rpc/call
[{:method "wallet_resetActivityFilterSession"
:params params
:on-error [:wallet/log-rpc-error
{:event :wallet/reset-activities-filter-session
:params params}]}]])]})))

(rf/reg-event-fx
:wallet/activity-filtering-for-current-account-done
(fn [{:keys [db]} [{:keys [message requestId]}]]
(let [address (get-in db [:wallet :ui :activity-tab :request requestId])
activities (->> message
(transforms/json->clj)
(:activities)
(cske/transform-keys transforms/->kebab-case-keyword))
activities-indexed (zipmap (map activity-transaction-id activities)
activities)]
{:db (assoc-in db [:wallet :activities address] activities-indexed)})))

(def ^:private nested-merge (partial merge-with merge))
(when (= requestId
(get-in db [:wallet :ui :activity-tab :request :request-id]))
(let [{:keys [address initial-request?]} (get-in db [:wallet :ui :activity-tab :request])
{:keys [activities offset hasMore]} (transforms/json->clj message)
activities (cske/transform-keys transforms/->kebab-case-keyword
activities)
activities-indexed (zipmap (map activity-transaction-id activities)
activities)
existing-activities (get-in db [:wallet :activities address])
updated-activities (if initial-request?
activities-indexed
(nested-merge existing-activities activities-indexed))]
{:db (-> db
(assoc-in [:wallet :activities address] updated-activities)
(assoc-in [:wallet :ui :activity-tab :request :offset] offset)
(assoc-in [:wallet :ui :activity-tab :request :has-more?] hasMore)
(assoc-in [:wallet :ui :activity-tab :request :initial-request?] false))}))))

(rf/reg-event-fx
:wallet/activities-filtering-entries-updated
(fn [{:keys [db]} [{:keys [message requestId]}]]
(let [address (get-in db [:wallet :ui :activity-tab :request requestId])
activities (->> message
(transforms/json->clj)
(cske/transform-keys transforms/->kebab-case-keyword))
activities-indexed (zipmap (map activity-transaction-id activities)
activities)]
{:db (-> db
(update-in [:wallet :ui :activity-tab :request] dissoc requestId)
(update-in [:wallet :activities address] nested-merge activities-indexed))})))
(when (= requestId
(get-in db [:wallet :ui :activity-tab :request :request-id]))
(let [address (get-in db [:wallet :ui :activity-tab :request :address])
activities (->> message
(transforms/json->clj)
(cske/transform-keys transforms/->kebab-case-keyword))
activities-indexed (zipmap (map activity-transaction-id activities)
activities)]
{:db (update-in db [:wallet :activities address] nested-merge activities-indexed)}))))

(rf/reg-event-fx
:wallet/activities-session-updated
(fn [{:keys [db]} [{:keys [message]}]]
(let [{:keys [hasNewOnTop]} (transforms/json->clj message)
session-id (get-in db [:wallet :ui :activity-tab :request :request-id])
has-active-session? (some? session-id)
address (get-in db [:wallet :ui :activity-tab :request :address])
no-activities? (empty? (get-in db [:wallet :activities address]))
must-fetch? (and hasNewOnTop has-active-session? no-activities?)]
{:fx [(when must-fetch?
[:dispatch [:wallet/reset-activities-filter-session]])]})))
7 changes: 6 additions & 1 deletion src/status_im/contexts/wallet/common/activity_tab/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@
(let [theme (quo.theme/use-theme)
address (rf/sub [:wallet/current-viewing-account-address])
activity-list (rf/sub [:wallet/activities-for-current-viewing-account])
on-end-reached (rn/use-callback
#(rf/dispatch
[:wallet/get-more-for-activities-filter-session]))
open-eth-chain-explorer (rn/use-callback
#(rf/dispatch [:wallet/navigate-to-chain-explorer
{:address address
Expand Down Expand Up @@ -154,4 +157,6 @@
:padding-horizontal 8}
:content-container-style {:padding-bottom jump-to.constants/floating-shell-button-height}
:render-fn activity-item
:render-section-header-fn section-header}])]))
:render-section-header-fn section-header
:on-end-reached on-end-reached
:on-end-reached-threshold 2}])]))
29 changes: 4 additions & 25 deletions src/status_im/contexts/wallet/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
[status-im.feature-flags :as ff]
[taoensso.timbre :as log]
[utils.collection]
[utils.ethereum.chain :as chain]
[utils.ethereum.eip.eip55 :as eip55]
[utils.i18n :as i18n]
[utils.number]
Expand Down Expand Up @@ -93,7 +92,8 @@
(let [just-completed-transaction? (get-in db [:wallet :ui :send :just-completed-transaction?])]
{:db (update db :wallet dissoc :current-viewing-account-address)
:fx [(when-not just-completed-transaction?
[:dispatch [:wallet/clear-account-tab]])]})))
[:dispatch [:wallet/clear-account-tab]])
[:dispatch [:wallet/stop-activity-filter-session]]]})))

(defn log-rpc-error
[_ [{:keys [event params]} error]]
Expand All @@ -105,15 +105,13 @@

(def refresh-accounts-fx-dispatches
[[:dispatch [:wallet/get-wallet-token-for-all-accounts]]
[:dispatch [:wallet/request-collectibles-for-all-accounts {:new-request? true}]]
[:dispatch [:wallet/check-recent-history-for-all-accounts]]])
[:dispatch [:wallet/request-collectibles-for-all-accounts {:new-request? true}]]])

(rf/reg-event-fx
:wallet/fetch-assets-for-address
(fn [_ [address]]
{:fx [[:dispatch [:wallet/get-wallet-token-for-account address]]
[:dispatch [:wallet/request-collectibles-for-account address]]
[:dispatch [:wallet/check-recent-history-for-account address]]]}))
[:dispatch [:wallet/request-collectibles-for-account address]]]}))

(defn- reconcile-accounts
[db-accounts-by-address new-accounts]
Expand Down Expand Up @@ -450,25 +448,6 @@
[{:method "wallet_startWallet"
:on-error [:wallet/log-rpc-error {:event :wallet/start-wallet}]}]]]}))

(rf/reg-event-fx :wallet/check-recent-history-for-all-accounts
(fn [{:keys [db]}]
{:fx (->> (get-in db [:wallet :accounts])
vals
(mapv (fn [{:keys [address]}]
[:dispatch [:wallet/check-recent-history-for-account address]])))}))

(rf/reg-event-fx
:wallet/check-recent-history-for-account
(fn [{:keys [db]} [address]]
(let [chain-ids (chain/chain-ids db)
params [chain-ids [address]]]
{:fx [[:json-rpc/call
[{:method "wallet_checkRecentHistoryForChainIDs"
:params params
:on-error [:wallet/log-rpc-error
{:event :wallet/check-recent-history-for-account
:params params}]}]]]})))

(rf/reg-event-fx :wallet/initialize
(fn []
{:fx [[:dispatch [:wallet/start-wallet]]
Expand Down
12 changes: 0 additions & 12 deletions src/status_im/contexts/wallet/events_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,6 @@
address]}]]]}]
(is (match? expected-effects (dispatch [event-id address])))))

(h/deftest-event :wallet/check-recent-history-for-all-accounts
[event-id dispatch]
(testing "check recent history for all accounts"
(let [address-1 "0x1"
address-2 "0x2"
expected-fx [[:dispatch [:wallet/check-recent-history-for-account address-1]]
[:dispatch [:wallet/check-recent-history-for-account address-2]]]]
(reset! rf-db/app-db
{:wallet {:accounts {address-1 {:address address-1}
address-2 {:address address-2}}}})
(is (match? expected-fx (:fx (dispatch [event-id])))))))

(h/deftest-event :wallet/reconcile-keypairs
[event-id dispatch]
(let [keypair-key-uid (:key-uid raw-account)]
Expand Down
5 changes: 5 additions & 0 deletions src/status_im/contexts/wallet/signals.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,9 @@
[:wallet/activities-filtering-entries-updated
(transforms/js->clj event-js)]]]}

"wallet-activity-session-updated"
{:fx [[:dispatch
[:wallet/activities-session-updated
(transforms/js->clj event-js)]]]}

(log/debug ::unknown-wallet-event :type event-type)))))

0 comments on commit e3409e6

Please sign in to comment.