Clean up after recent tx history changes:

- unused code is removed
- "load more" button is hidden for a new account
- each next page of tx history contains 20 transfers
  unless thare is no enough transfers in history
This commit is contained in:
Roman Volosovskyi 2020-02-06 19:17:05 +02:00
parent 46bd2b2e4c
commit 807c8377d4
No known key found for this signature in database
GPG Key ID: 0238A4B5ECEE70DE
4 changed files with 78 additions and 73 deletions

View File

@ -88,7 +88,7 @@
[] []
accounts) accounts)
:before-block blockNumber :before-block blockNumber
:page-size 20 :limit 20
:historical? true}})) :historical? true}}))
(fx/defn new-wallet-event (fx/defn new-wallet-event

View File

@ -85,6 +85,8 @@
;; transactions api ;; transactions api
;; ----------------------------------------------- ;; -----------------------------------------------
(def default-transfers-limit 20)
(fx/defn watch-transaction (fx/defn watch-transaction
"Set a watch for the given transaction "Set a watch for the given transaction
`watch-params` needs to contain a `trigger-fn` and `on-trigger` functions `watch-params` needs to contain a `trigger-fn` and `on-trigger` functions
@ -145,17 +147,41 @@
(defn get-min-known-block [db address] (defn get-min-known-block [db address]
(get-in db [:wallet :accounts (eip55/address->checksum address) :min-block])) (get-in db [:wallet :accounts (eip55/address->checksum address) :min-block]))
(defn min-block-transfers-count [db address]
(get-in db [:wallet :accounts
(eip55/address->checksum address)
:min-block-transfers-count]))
(fx/defn set-lowest-fetched-block (fx/defn set-lowest-fetched-block
[{:keys [db]} address transfers] [{:keys [db]} address transfers]
(let [min-block (reduce (let [checksum (eip55/address->checksum address)
(fn [min-block {:keys [block]}] {:keys [min-block min-block-transfers-count]}
(min (or min-block block) block)) (reduce
(get-min-known-block db address) (fn [{:keys [min-block] :as acc}
transfers)] {:keys [block hash]}]
{:db (assoc-in (cond
db (or (nil? min-block) (> min-block block))
[:wallet :accounts (eip55/address->checksum address) :min-block] {:min-block block
min-block)})) :min-block-transfers-count 1}
(and (= min-block block)
(nil? (get-in db [:wallet :accounts checksum :transactions hash])))
(update acc :min-block-transfers-count inc)
:else acc))
{:min-block
(get-min-known-block db address)
:min-block-transfers-count
(min-block-transfers-count db address)}
transfers)]
(log/debug "[transactions] set-lowest-fetched-block"
"address" address
"min-block" min-block
"min-block-transfers-count" min-block-transfers-count)
{:db (update-in db [:wallet :accounts checksum] assoc
:min-block min-block
:min-block-transfers-count min-block-transfers-count)}))
(defn update-fetching-status (defn update-fetching-status
[db addresses fetching-type state] [db addresses fetching-type state]
@ -184,10 +210,14 @@
(fx/defn new-transfers (fx/defn new-transfers
{:events [::new-transfers]} {:events [::new-transfers]}
[{:keys [db] :as cofx} transfers {:keys [address historical? before-block]}] [{:keys [db] :as cofx} transfers {:keys [address historical? limit]}]
(let [min-block (get-in db [:wallet :accounts address :min-block]) (log/debug "[transfers] new-transfers"
"address" address
"count" (count transfers)
"limit" limit)
(let [checksum (eip55/address->checksum address)
effects (cond-> [(when (seq transfers) effects (cond-> [(when (seq transfers)
(set-lowest-fetched-block address transfers))] (set-lowest-fetched-block checksum transfers))]
(seq transfers) (seq transfers)
(concat (mapv add-transfer transfers)) (concat (mapv add-transfer transfers))
@ -200,10 +230,9 @@
#{} #{}
transfers)))) transfers))))
(and (= min-block before-block) (< (count transfers) limit)
(<= (count transfers) 1)) (conj (tx-history-end-reached checksum)))]
(conj (tx-history-end-reached address)))] (apply fx/merge cofx (tx-fetching-ended [checksum]) effects)))
(apply fx/merge cofx (tx-fetching-ended [address]) effects)))
(fx/defn tx-fetching-failed (fx/defn tx-fetching-failed
{:events [::tx-fetching-failed]} {:events [::tx-fetching-failed]}
@ -213,59 +242,32 @@
"error" error) "error" error)
(tx-fetching-ended cofx [address])) (tx-fetching-ended cofx [address]))
(re-frame/reg-fx
:transactions/get-transfers-from-block
(fn [{:keys [chain-tokens addresses block] :as params}]
(log/debug "[transactions] get-transfers-from-block"
"addresses" addresses
"block" block)
(doseq [address addresses]
(json-rpc/call
{:method "wallet_getTransfersFromBlock"
:params [address (encode/uint block)]
:on-success #(re-frame/dispatch
[::new-transfers
(enrich-transfers chain-tokens %)
(assoc params :address address)])
:on-error #(re-frame/dispatch [::tx-fetching-failed % address])}))))
(re-frame/reg-fx (re-frame/reg-fx
:transactions/get-transfers :transactions/get-transfers
(fn [{:keys [chain-tokens addresses before-block page-size (fn [{:keys [chain-tokens addresses before-block limit
transactions-per-address] limit-per-address]
:as params :as params
:or {page-size 20}}] :or {limit default-transfers-limit}}]
{:pre [(cljs.spec.alpha/valid? {:pre [(cljs.spec.alpha/valid?
(cljs.spec.alpha/coll-of string?) (cljs.spec.alpha/coll-of string?)
addresses)]} addresses)]}
(log/debug "[transactions] get-transfers" (log/debug "[transactions] get-transfers"
"addresses" addresses "addresses" addresses
"block" before-block "block" before-block
"page-size" page-size "limit" limit
"transactions-per-address" transactions-per-address) "limit-per-address" limit-per-address)
(when before-block (doseq [address addresses]
(doseq [address addresses] (let [limit (or (get limit-per-address address)
(let [page-size (or (get transactions-per-address address) limit)]
page-size)] (json-rpc/call
(json-rpc/call {:method "wallet_getTransfersByAddress"
{:method "wallet_getTransfersByAddress" :params [address (encode/uint before-block) (encode/uint limit)]
:params [address (encode/uint before-block) (encode/uint page-size)] :on-success #(re-frame/dispatch
:on-success #(re-frame/dispatch [::new-transfers
[::new-transfers (enrich-transfers chain-tokens %)
(enrich-transfers chain-tokens %) (assoc params :address address
(assoc params :address address)]) :limit limit)])
:on-error #(re-frame/dispatch [::tx-fetching-failed address])})))))) :on-error #(re-frame/dispatch [::tx-fetching-failed address])})))))
(fx/defn initialize
[{:keys [db]} addresses]
(let [{:keys [:wallet/all-tokens]} db
chain (ethereum/chain-keyword db)
chain-tokens (into {} (map (juxt :address identity)
(tokens/tokens-for all-tokens chain)))]
{:transactions/get-transfers
{:chain-tokens chain-tokens
:addresses (map eip55/address->checksum addresses)
:historical? true}}))
(fx/defn fetch-more-tx (fx/defn fetch-more-tx
{:events [:transactions/fetch-more]} {:events [:transactions/fetch-more]}
@ -278,12 +280,20 @@
(tokens/tokens-for (tokens/tokens-for
all-tokens chain))) all-tokens chain)))
min-known-block (or (get-min-known-block db address) min-known-block (or (get-min-known-block db address)
(:ethereum/current-block db))] (:ethereum/current-block db))
min-block-transfers-count (or (min-block-transfers-count db address) 0)]
(fx/merge (fx/merge
cofx cofx
{:transactions/get-transfers {:transactions/get-transfers
{:chain-tokens chain-tokens {:chain-tokens chain-tokens
:addresses [address] :addresses [address]
:before-block min-known-block :before-block min-known-block
:historical? true}} :historical? true
;; Transfers are requested before and including `min-known-block` because
;; there is no guarantee that all transfers from that block are shown
;; already. To make sure that we fetch the whole `default-transfers-limit`
;; of transfers the number of transfers already received for
;; `min-known-block` is added to the page size.
:limit-per-address {address (+ default-transfers-limit
min-block-transfers-count)}}}
(tx-fetching-in-progress [address])))) (tx-fetching-in-progress [address]))))

View File

@ -88,11 +88,7 @@
(rpc->accounts accounts))} (rpc->accounts accounts))}
(wallet/initialize-tokens custom-tokens) (wallet/initialize-tokens custom-tokens)
(wallet/update-balances nil) (wallet/update-balances nil)
(wallet/update-prices) (wallet/update-prices)))
(transactions/initialize
(->> accounts
(filter :wallet)
(map :address)))))
(fx/defn login (fx/defn login
{:events [:multiaccounts.login.ui/password-input-submitted]} {:events [:multiaccounts.login.ui/password-input-submitted]}

View File

@ -90,8 +90,7 @@
{:color colors/blue {:color colors/blue
:container-style {:margin-right 5}}] :container-style {:margin-right 5}}]
[react/text [react/text
{:style {:marging-left 10 {:style {:color colors/blue}}
:color colors/blue}}
(i18n/label :t/check-on-etherscan)]]])) (i18n/label :t/check-on-etherscan)]]]))
(defn history-list (defn history-list