cleanup fetching transactions

This commit is contained in:
yenda 2019-08-24 01:21:38 +02:00
parent 4283c675d1
commit b45e7ccafb
No known key found for this signature in database
GPG Key ID: 0095623C0069DCE6
6 changed files with 37 additions and 36 deletions

View File

@ -59,7 +59,6 @@
"status_startOneOnOneChat" {}
"status_removeChat" {}
"wallet_getTransfers" {}
"wallet_getTransfersByAddress" {}
"browsers_getBrowsers" {}
"browsers_addBrowser" {}
"browsers_deleteBrowser" {}

View File

@ -26,7 +26,6 @@
(fx/defn new-block
[{:keys [db] :as cofx} historical? block-number accounts]
(let [{:keys [:wallet/all-tokens]} db
accounts (get-in db [:multiaccount :accounts]) ;;TODO https://github.com/status-im/status-go/issues/1566
chain (ethereum/chain-keyword db)
chain-tokens (into {} (map (juxt :address identity)
(tokens/tokens-for all-tokens chain)))]
@ -35,23 +34,23 @@
(not historical?)
(assoc :db (assoc db :ethereum/current-block block-number))
true ;(not-empty accounts) ;;TODO https://github.com/status-im/status-go/issues/1566
(assoc ::transactions/get-transfers {:accounts accounts
:chain-tokens chain-tokens
:from-block block-number}))
;;NOTE only get transfers if the new block contains some
;; from/to one of the multiaccount accounts
(not-empty accounts)
(assoc ::transactions/get-transfers {:chain-tokens chain-tokens
:from-block block-number
:historical? historical?}))
(transactions/check-watched-transactions))))
(fx/defn reorg
[{:keys [db] :as cofx} block-number accounts]
(let [{:keys [:wallet/all-tokens]} db
accounts (get-in db [:multiaccount :accounts]) ;;TODO https://github.com/status-im/status-go/issues/1566
chain (ethereum/chain-keyword db)
chain-tokens (into {} (map (juxt :address identity)
(tokens/tokens-for all-tokens chain)))]
{:db (update-in db [:wallet :transactions]
wallet/remove-transactions-since-block block-number)
::transactions/get-transfers {:accounts accounts
:chain-tokens chain-tokens
::transactions/get-transfers {:chain-tokens chain-tokens
:from-block block-number}}))
(fx/defn new-wallet-event

View File

@ -5,6 +5,7 @@
[status-im.ethereum.encode :as encode]
[status-im.ethereum.json-rpc :as json-rpc]
[status-im.ethereum.core :as ethereum]
[status-im.ethereum.eip55 :as eip55]
[status-im.ethereum.tokens :as tokens]
[status-im.utils.fx :as fx]
[status-im.utils.money :as money]
@ -52,7 +53,8 @@
txHash gasPrice gasUsed contract value gasLimit input nonce to type id] :as transfer}]
(let [erc20? (= type "erc20")
failed? (= txStatus "0x0")]
(merge {:id id
(merge {:address (eip55/address->checksum address)
:id id
:block (str (decode/uint blockNumber))
:timestamp (* (decode/uint timestamp) 1000)
:gas-used (str (decode/uint gasUsed))
@ -122,7 +124,7 @@
"We determine a unique id for the transfer before adding it because some
transaction can contain multiple transfers and they would overwrite each other
in the transfer map if identified by hash"
[{:keys [db] :as cofx} {:keys [hash id] :as transfer} address]
[{:keys [db] :as cofx} {:keys [hash id address] :as transfer}]
(let [transfer-by-hash (get-in db [:wallet :accounts address :transactions hash])
transfer-by-id (get-in db [:wallet :accounts address :transactions id])]
(when-let [unique-id (when-not (or transfer-by-id
@ -139,10 +141,14 @@
(fx/defn new-transfers
{:events [::new-transfers]}
[{:keys [db] :as cofx} address transfers]
(let [add-transfers-fx (map #(add-transfer % address) transfers)]
(apply fx/merge cofx (conj add-transfers-fx
(wallet/update-balances [address])))))
[{:keys [db] :as cofx} transfers historical?]
(let [effects (cond-> (map add-transfer transfers)
;;NOTE: we only update the balance for new transfers and not historical ones
(not historical?) (conj (wallet/update-balances (into [] (reduce (fn [acc {:keys [address]}]
(conj acc address))
#{}
transfers)))))]
(apply fx/merge cofx effects)))
(fx/defn handle-token-history
[{:keys [db]} transactions]
@ -152,22 +158,19 @@
(re-frame/reg-fx
::get-transfers
(fn [{:keys [accounts chain-tokens from-block to-block]
(fn [{:keys [chain-tokens from-block to-block historical?]
:or {from-block "0"
to-block nil}}]
;; start inbound token transaction subscriptions
;; outbound token transactions are already caught in new blocks filter
(doseq [{:keys [address]} accounts]
(json-rpc/call
{:method "wallet_getTransfersByAddress"
:params [address (encode/uint from-block) (encode/uint to-block)]
:on-success #(re-frame/dispatch [::new-transfers address (enrich-transfers chain-tokens %)])}))))
(json-rpc/call
{:method "wallet_getTransfers"
:params [(encode/uint from-block) (encode/uint to-block)]
:on-success #(re-frame/dispatch [::new-transfers (enrich-transfers chain-tokens %) historical?])})))
(fx/defn initialize
[{:keys [db] :as cofx}]
(let [accounts (get-in db [:multiaccount :accounts]) ;;TODO https://github.com/status-im/status-go/issues/1566
{:keys [:wallet/all-tokens]} db
(let [{:keys [:wallet/all-tokens]} db
chain (ethereum/chain-keyword db)
chain-tokens (into {} (map (juxt :address identity)
(tokens/tokens-for all-tokens chain)))]
{::get-transfers {:accounts accounts :chain-tokens chain-tokens}}))
{::get-transfers {:chain-tokens chain-tokens
:historical? true}}))

View File

@ -1639,8 +1639,7 @@
(fx/merge cofx
(when on-close
{:dispatch on-close})
(navigation/navigate-back)
(wallet/update-balances nil))))
(navigation/navigate-back))))
(handlers/register-handler-fx
:wallet.callback/update-balance-success

View File

@ -1012,7 +1012,7 @@
(re-frame/reg-sub
:account-portfolio-value
(fn [[_ address] _]
[(re-frame/subscribe [:balance address])
[(re-frame/subscribe [:balance (string/lower-case address)])
(re-frame/subscribe [:prices])
(re-frame/subscribe [:wallet/currency])
(re-frame/subscribe [:ethereum/chain-keyword])
@ -1073,7 +1073,7 @@
(re-frame/reg-sub
:wallet/visible-assets-with-amount
(fn [[_ address] _]
[(re-frame/subscribe [:balance address])
[(re-frame/subscribe [:balance (string/lower-case address)])
(re-frame/subscribe [:wallet/visible-assets])])
(fn [[balance visible-assets]]
(map #(assoc % :amount (get balance (:symbol %))) visible-assets)))
@ -1093,7 +1093,7 @@
(re-frame/reg-sub
:wallet/visible-assets-with-values
(fn [[_ address] _]
[(re-frame/subscribe [:wallet/visible-assets-with-amount address])
[(re-frame/subscribe [:wallet/visible-assets-with-amount (string/lower-case address)])
(re-frame/subscribe [:prices])
(re-frame/subscribe [:wallet/currency])])
(fn [[assets prices currency]]
@ -1130,7 +1130,7 @@
(re-frame/reg-sub
:wallet/transferrable-assets-with-amount
(fn [[_ address]]
(re-frame/subscribe [:wallet/visible-assets-with-amount address]))
(re-frame/subscribe [:wallet/visible-assets-with-amount (string/lower-case address)]))
(fn [all-assets]
(filter #(not (:nft? %)) all-assets)))
@ -1146,7 +1146,7 @@
:wallet/transactions
:<- [:wallet]
(fn [wallet [_ address]]
(get-in wallet [:accounts address :transactions])))
(get-in wallet [:accounts (string/lower-case address) :transactions])))
(re-frame/reg-sub
:wallet/filters

View File

@ -18,7 +18,8 @@
[taoensso.timbre :as log]
[status-im.wallet.db :as wallet.db]
[status-im.ethereum.abi-spec :as abi-spec]
[status-im.signing.core :as signing]))
[status-im.signing.core :as signing]
[clojure.string :as string]))
(re-frame/reg-fx
:wallet/get-balance
@ -186,9 +187,9 @@
{:wallet/validate-tokens (get tokens/all-default-tokens chain)})))))
(fx/defn update-balances
[{{:keys [network-status :wallet/all-tokens]
[{{:keys [network-status :wallet/all-tokens]
{:keys [settings accounts]} :multiaccount :as db} :db :as cofx} addresses]
(let [addresses (or addresses (map :address accounts))
(let [addresses (or addresses (map (comp string/lower-case :address) accounts))
chain (ethereum/chain-keyword db)
assets (get-in settings [:wallet :visible-tokens chain])
tokens (->> (tokens/tokens-for all-tokens chain)