Skip to content

Commit

Permalink
Deduplicate data
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrkhalil committed Sep 13, 2022
1 parent fdfff5d commit 4d34e4e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 35 deletions.
62 changes: 31 additions & 31 deletions src/status_im/ethereum/subscriptions.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,42 @@
(log/debug "[wallet-subs] recent-history-fetching-started"
"accounts" accounts)
(let [event (get db :wallet/on-recent-history-fetching)]
(fx/merge cofx
(wallet-connect-legacy/get-connector-session-from-db) ;; This is to refresh wallet connect session on each wallet refresh.
(cond-> {:db (-> db
(transactions/update-fetching-status accounts :recent? true)
(assoc :wallet/recent-history-fetching-started? true)
(dissoc :wallet/on-recent-history-fetching))}
event
(assoc :dispatch event)))))
(cond-> {:db (-> db
(transactions/update-fetching-status accounts :recent? true)
(assoc :wallet/recent-history-fetching-started? true)
(dissoc :wallet/on-recent-history-fetching))}
event
(assoc :dispatch event))))

(fx/defn recent-history-fetching-ended
[{:keys [db]} {:keys [accounts blockNumber]}]
[{:keys [db] :as cofx} {:keys [accounts blockNumber]}]
(log/debug "[wallet-subs] recent-history-fetching-ended"
"accounts" accounts
"block" blockNumber)
{:db (-> db
(assoc :ethereum/current-block blockNumber)
(update-in [:wallet :accounts]
wallet/remove-transactions-since-block blockNumber)
(transactions/update-fetching-status accounts :recent? false)
(dissoc :wallet/waiting-for-recent-history?
:wallet/refreshing-history?
:wallet/fetching-error
:wallet/recent-history-fetching-started?))
:transactions/get-transfers
{:chain-tokens (:wallet/all-tokens db)
:addresses (reduce
(fn [v address]
(let [normalized-address
(eip55/address->checksum address)]
(if (contains? v normalized-address)
v
(conj v address))))
[]
accounts)
:before-block blockNumber
:limit 20}})
(fx/merge cofx
(wallet-connect-legacy/get-connector-session-from-db) ;; This is to refresh wallet connect session on each wallet refresh.
{:db (-> db
(assoc :ethereum/current-block blockNumber)
(update-in [:wallet :accounts]
wallet/remove-transactions-since-block blockNumber)
(transactions/update-fetching-status accounts :recent? false)
(dissoc :wallet/waiting-for-recent-history?
:wallet/refreshing-history?
:wallet/fetching-error
:wallet/recent-history-fetching-started?))
:transactions/get-transfers
{:chain-tokens (:wallet/all-tokens db)
:addresses (reduce
(fn [v address]
(let [normalized-address
(eip55/address->checksum address)]
(if (contains? v normalized-address)
v
(conj v address))))
[]
accounts)
:before-block blockNumber
:limit 20}}))

(fx/defn fetching-error
[{:keys [db] :as cofx} {:keys [message]}]
Expand Down
38 changes: 35 additions & 3 deletions src/status_im/signing/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,35 @@
(defn normalize-tx-obj [db tx]
(update-in tx [:tx-obj :from] #(eip55/address->checksum (or % (ethereum/default-address db)))))

(defn- filter-out-repeated-signs [queue]
(map first (->> queue
(group-by #(get-in % [:tx-obj :value]))
vals)))

(re-frame/reg-fx
:sign/check-if-signing-queue-match
(fn [{:keys [current-signing-tx-value signing-queue]}]
(js/alert current-signing-tx-value)))

(fx/defn check-if-current-signing-is-in-queue
{:events [:signing/check-if-current-signing-is-in-queue]}
[{:keys [db]
:as cofx} {:keys [current-signing-tx-value signing-queue]}]
(fx/merge cofx
{:db db
:sign/check-if-signing-queue-match {:current-signing-tx-value current-signing-tx-value
:signing-queue signing-queue}}))

(fx/defn check-if-currently-signing-exists
[{:keys [db]
:as cofx}]
(let [current-signing-tx-value (get-in db [:signing/tx :tx-obj :value])
signing-queue (:signing/queue db)]
(fx/merge cofx
{:db db}
(check-if-current-signing-is-in-queue {:current-signing-tx-value current-signing-tx-value
:signing-queue signing-queue}))))

(fx/defn sign
"Signing transaction or message, shows signing sheet
tx
Expand All @@ -441,9 +470,12 @@
:on-error - re-frame event vector}"
{:events [:signing.ui/sign]}
[{:keys [db] :as cofx} tx]
(fx/merge cofx
{:db (update db :signing/queue conj (normalize-tx-obj db tx))}
(check-queue)))
(fx/merge cofx
{:db (-> db
(update :signing/queue #(->> (conj % (normalize-tx-obj db tx))
filter-out-repeated-signs)))}
(check-if-currently-signing-exists)
(check-queue)))

(fx/defn sign-transaction-button-clicked-from-chat
{:events [:wallet.ui/sign-transaction-button-clicked-from-chat]}
Expand Down
3 changes: 2 additions & 1 deletion src/status_im/wallet_connect_legacy/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
(doall
(map
(fn [^js session]
(let [connector (wallet-connect-legacy/create-connector-from-session session)]
(let [connector (or (.-connector session)
(wallet-connect-legacy/create-connector-from-session session))]
;; Update session so we are sure it's on the same network
(.updateSession connector (clj->js {:chainId chain-id
:accounts (.-accounts session)}))
Expand Down

0 comments on commit 4d34e4e

Please sign in to comment.