Skip to content

Commit

Permalink
Clean up after recent tx history changes:
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
rasom committed Feb 7, 2020
1 parent 3d4ee8f commit cdb5a60
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 76 deletions.
2 changes: 1 addition & 1 deletion src/status_im/ethereum/subscriptions.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
[]
accounts)
:before-block blockNumber
:page-size 20
:limit 20
:historical? true}}))

(fx/defn new-wallet-event
Expand Down
144 changes: 79 additions & 65 deletions src/status_im/ethereum/transactions/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
;; transactions api
;; -----------------------------------------------

(def default-transfers-limit 20)

(fx/defn watch-transaction
"Set a watch for the given transaction
`watch-params` needs to contain a `trigger-fn` and `on-trigger` functions
Expand Down Expand Up @@ -145,17 +147,45 @@
(defn get-min-known-block [db address]
(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
[{:keys [db]} address transfers]
(let [min-block (reduce
(fn [min-block {:keys [block]}]
(min (or min-block block) block))
(get-min-known-block db address)
transfers)]
{:db (assoc-in
db
[:wallet :accounts (eip55/address->checksum address) :min-block]
min-block)}))
(let [checksum (eip55/address->checksum address)
{:keys [min-block min-block-transfers-count]}
(reduce
(fn [{:keys [min-block] :as acc}
{:keys [block hash]}]
(cond
(or (nil? min-block) (> min-block block))
{:min-block 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 (-> db
(assoc-in
[:wallet :accounts checksum :min-block]
min-block)
(assoc-in
[:wallet :accounts checksum :min-block-transfers-count]
min-block-transfers-count))}))

(defn update-fetching-status
[db addresses fetching-type state]
Expand Down Expand Up @@ -184,10 +214,14 @@

(fx/defn new-transfers
{:events [::new-transfers]}
[{:keys [db] :as cofx} transfers {:keys [address historical? before-block]}]
(let [min-block (get-in db [:wallet :accounts address :min-block])
[{:keys [db] :as cofx} transfers {:keys [address historical? limit]}]
(log/debug "[transfers] new-transfers"
"address" address
"count" (count transfers)
"limit" limit)
(let [checksum (eip55/address->checksum address)
effects (cond-> [(when (seq transfers)
(set-lowest-fetched-block address transfers))]
(set-lowest-fetched-block checksum transfers))]

(seq transfers)
(concat (mapv add-transfer transfers))
Expand All @@ -200,10 +234,9 @@
#{}
transfers))))

(and (= min-block before-block)
(<= (count transfers) 1))
(conj (tx-history-end-reached address)))]
(apply fx/merge cofx (tx-fetching-ended [address]) effects)))
(< (count transfers) limit)
(conj (tx-history-end-reached checksum)))]
(apply fx/merge cofx (tx-fetching-ended [checksum]) effects)))

(fx/defn tx-fetching-failed
{:events [::tx-fetching-failed]}
Expand All @@ -213,59 +246,32 @@
"error" error)
(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
:transactions/get-transfers
(fn [{:keys [chain-tokens addresses before-block page-size
transactions-per-address]
(fn [{:keys [chain-tokens addresses before-block limit
limit-per-address]
:as params
:or {page-size 20}}]
:or {limit default-transfers-limit}}]
{:pre [(cljs.spec.alpha/valid?
(cljs.spec.alpha/coll-of string?)
addresses)]}
(log/debug "[transactions] get-transfers"
"addresses" addresses
"block" before-block
"page-size" page-size
"transactions-per-address" transactions-per-address)
(when before-block
(doseq [address addresses]
(let [page-size (or (get transactions-per-address address)
page-size)]
(json-rpc/call
{:method "wallet_getTransfersByAddress"
:params [address (encode/uint before-block) (encode/uint page-size)]
:on-success #(re-frame/dispatch
[::new-transfers
(enrich-transfers chain-tokens %)
(assoc params :address 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}}))
"limit" limit
"limit-per-address" limit-per-address)
(doseq [address addresses]
(let [limit (or (get limit-per-address address)
limit)]
(json-rpc/call
{:method "wallet_getTransfersByAddress"
:params [address (encode/uint before-block) (encode/uint limit)]
:on-success #(re-frame/dispatch
[::new-transfers
(enrich-transfers chain-tokens %)
(assoc params :address address
:limit limit)])
:on-error #(re-frame/dispatch [::tx-fetching-failed address])})))))

(fx/defn fetch-more-tx
{:events [:transactions/fetch-more]}
Expand All @@ -278,12 +284,20 @@
(tokens/tokens-for
all-tokens chain)))
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
cofx
{:transactions/get-transfers
{:chain-tokens chain-tokens
:addresses [address]
:before-block min-known-block
:historical? true}}
{:chain-tokens chain-tokens
:addresses [address]
:before-block min-known-block
: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]))))
6 changes: 1 addition & 5 deletions src/status_im/multiaccounts/login/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,7 @@
(rpc->accounts accounts))}
(wallet/initialize-tokens custom-tokens)
(wallet/update-balances nil)
(wallet/update-prices)
(transactions/initialize
(->> accounts
(filter :wallet)
(map :address)))))
(wallet/update-prices)))

(fx/defn login
{:events [:multiaccounts.login.ui/password-input-submitted]}
Expand Down
3 changes: 1 addition & 2 deletions src/status_im/ui/screens/wallet/transactions/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@
{:color colors/blue
:container-style {:margin-right 5}}]
[react/text
{:style {:marging-left 10
:color colors/blue}}
{:style {:color colors/blue}}
(i18n/label :t/check-on-etherscan)]]]))

(defn history-list
Expand Down
6 changes: 3 additions & 3 deletions status-go-version.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"_comment": "DO NOT EDIT THIS FILE BY HAND. USE 'scripts/update-status-go.sh <tag>' instead",
"owner": "status-im",
"repo": "status-go",
"version": "v0.43.1",
"commit-sha1": "0b6ad662b519ce0bc0f0c1c65f8c5eb77ddb2904",
"src-sha256": "1rn11ig98pb6y5gp5w9vzh41grsampd8bz09zdqz2rzfdic1b66i"
"version": "fix/tx-fetching-cleanup",
"commit-sha1": "ad86b7f43363a778bdccf0610d9febb73424fd04",
"src-sha256": "12wfkqj9ifrl9q6f7idbhhws5m3iavg1v1kp050phlr9wsbl8zx6"
}

0 comments on commit cdb5a60

Please sign in to comment.