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_startOneOnOneChat" {}
"status_removeChat" {} "status_removeChat" {}
"wallet_getTransfers" {} "wallet_getTransfers" {}
"wallet_getTransfersByAddress" {}
"browsers_getBrowsers" {} "browsers_getBrowsers" {}
"browsers_addBrowser" {} "browsers_addBrowser" {}
"browsers_deleteBrowser" {} "browsers_deleteBrowser" {}

View File

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

View File

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

View File

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

View File

@ -18,7 +18,8 @@
[taoensso.timbre :as log] [taoensso.timbre :as log]
[status-im.wallet.db :as wallet.db] [status-im.wallet.db :as wallet.db]
[status-im.ethereum.abi-spec :as abi-spec] [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 (re-frame/reg-fx
:wallet/get-balance :wallet/get-balance
@ -186,9 +187,9 @@
{:wallet/validate-tokens (get tokens/all-default-tokens chain)}))))) {:wallet/validate-tokens (get tokens/all-default-tokens chain)})))))
(fx/defn update-balances (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] {: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) chain (ethereum/chain-keyword db)
assets (get-in settings [:wallet :visible-tokens chain]) assets (get-in settings [:wallet :visible-tokens chain])
tokens (->> (tokens/tokens-for all-tokens chain) tokens (->> (tokens/tokens-for all-tokens chain)