[wallet] Store pending transactions

This commit is contained in:
Roman Volosovskyi 2021-04-15 11:19:33 +03:00
parent 1bd62f1199
commit 693c41850c
No known key found for this signature in database
GPG Key ID: 0238A4B5ECEE70DE
7 changed files with 126 additions and 28 deletions

View File

@ -148,6 +148,9 @@
"wallet_getTransfersByAddress" {} "wallet_getTransfersByAddress" {}
"wallet_watchTransaction" {} "wallet_watchTransaction" {}
"wallet_checkRecentHistory" {} "wallet_checkRecentHistory" {}
"wallet_storePendingTransaction" {}
"wallet_deletePendingTransaction" {}
"wallet_getPendingTransactions" {}
"wallet_getTokensBalances" {} "wallet_getTokensBalances" {}
"wallet_getCustomTokens" {} "wallet_getCustomTokens" {}
"wallet_addCustomToken" {} "wallet_addCustomToken" {}

View File

@ -215,6 +215,19 @@
:all :all
:all-preloaded))})) :all-preloaded))}))
(fx/defn delete-pending-transactions
[{:keys [db]} address transactions]
(let [all-transactions
(get-in db [:wallet :accounts (eip55/address->checksum address) :transactions])
pending-tx-hashes (keep (fn [{:keys [hash]}]
(let [{:keys [type] :as old-tx}
(get all-transactions hash)]
(when (and (get all-transactions hash)
(= type :pending))
hash)))
transactions)]
{:wallet/delete-pending-transactions pending-tx-hashes}))
(fx/defn handle-new-transfer (fx/defn handle-new-transfer
[{:keys [db] :as cofx} transfers {:keys [address limit]}] [{:keys [db] :as cofx} transfers {:keys [address limit]}]
(log/debug "[transfers] new-transfers" (log/debug "[transfers] new-transfers"
@ -228,7 +241,9 @@
(wallet/set-max-block-with-transfers checksum transfers)] (wallet/set-max-block-with-transfers checksum transfers)]
(seq transfers) (seq transfers)
(concat (mapv add-transfer transfers)) (concat
[(delete-pending-transactions address transfers)]
(mapv add-transfer transfers))
(and max-known-block (and max-known-block
(some #(> (:block %) max-known-block) transfers)) (some #(> (:block %) max-known-block) transfers))

View File

@ -87,6 +87,7 @@
::enable-local-notifications nil} ::enable-local-notifications nil}
(wallet/initialize-tokens custom-tokens) (wallet/initialize-tokens custom-tokens)
(wallet/initialize-favourites favourites) (wallet/initialize-favourites favourites)
(wallet/get-pending-transactions)
(cond (and new-account? (cond (and new-account?
(not scan-all-tokens?)) (not scan-all-tokens?))
(wallet/set-zero-balances (first accounts)) (wallet/set-zero-balances (first accounts))

View File

@ -109,16 +109,13 @@
(fx/defn prepare-unconfirmed-transaction (fx/defn prepare-unconfirmed-transaction
[{:keys [db now]} new-tx-hash {:keys [value gasPrice gas data to from hash]} symbol amount] [{:keys [db now]} new-tx-hash {:keys [value gasPrice gas data to from hash]} symbol amount]
(log/debug "[signing] prepare-unconfirmed-transaction")
(let [token (tokens/symbol->token (:wallet/all-tokens db) symbol) (let [token (tokens/symbol->token (:wallet/all-tokens db) symbol)
from (eip55/address->checksum from) from (eip55/address->checksum from)
;;if there is a hash in the tx object that means we resending transaction ;;if there is a hash in the tx object that means we resending transaction
old-tx-hash hash] old-tx-hash hash
{:db (-> db gas-price (money/to-fixed (money/bignumber gasPrice))
;;remove old transaction, because we replace it with the new one gas-limit (money/to-fixed (money/bignumber gas))
(update-in [:wallet :accounts from :transactions] dissoc old-tx-hash) tx {:timestamp now
(assoc-in [:wallet :accounts from :transactions new-tx-hash]
{:timestamp (str now)
:to to :to to
:from from :from from
:type :pending :type :pending
@ -127,10 +124,24 @@
:token token :token token
:symbol symbol :symbol symbol
:value (if token :value (if token
(money/unit->token amount (:decimals token)) (money/to-fixed (money/unit->token amount (:decimals token)))
(money/to-fixed (money/bignumber value))) (money/to-fixed (money/bignumber value)))
:gas-price (money/to-fixed (money/bignumber gasPrice)) :gas-price gas-price
:gas-limit (money/to-fixed (money/bignumber gas))}))})) :gas-limit gas-limit}]
(log/info "[signing] prepare-unconfirmed-transaction" tx)
{:db (-> db
;;remove old transaction, because we replace it with the new one
(update-in [:wallet :accounts from :transactions] dissoc old-tx-hash)
(assoc-in [:wallet :accounts from :transactions new-tx-hash] tx))
::json-rpc/call [{:method "wallet_storePendingTransaction"
:params [(-> tx
(dissoc :gas-price :gas-limit)
(assoc :gasPrice
(money/to-fixed (money/bignumber gasPrice))
:gasLimit (money/to-fixed (money/bignumber gas)))
clj->js)]
:on-success #(log/info "pending transfer is saved")
:on-failure #(log/info "pending transfer was not saved" %)}]}))
(defn get-method-type [data] (defn get-method-type [data]
(cond (cond

View File

@ -70,6 +70,16 @@
(when bn (when bn
(.toNumber bn))) (.toNumber bn)))
(defn to-string
([^js bn]
(to-string bn 10))
([^js bn base]
(when bn
(.toString bn base))))
(defn to-hex [^js bn]
(str "0x" (to-string bn 16)))
(defn wei->str (defn wei->str
([unit n display-unit] ([unit n display-unit]
(str (to-fixed (wei-> unit n)) " " display-unit)) (str (to-fixed (wei-> unit n)) " " display-unit))

View File

@ -686,18 +686,23 @@
(re-frame/reg-fx (re-frame/reg-fx
::start-watching ::start-watching
(fn [hash] (fn [hashes]
(log/info "[wallet] watch transaction" hash) (log/info "[wallet] watch transactions" hashes)
(doseq [hash hashes]
(json-rpc/call (json-rpc/call
{:method "wallet_watchTransaction" {:method "wallet_watchTransaction"
:params [hash] :params [hash]
:on-success #(re-frame.core/dispatch [::restart true]) :on-success #(re-frame.core/dispatch [::restart true])
:on-error #(log/info "[wallet] watch transaction error" % "hash" hash)}))) :on-error #(log/info "[wallet] watch transaction error" % "hash" hash)}))))
(fx/defn watch-tx (fx/defn watch-tx
{:events [:watch-tx]} {:events [:watch-tx]}
[{:keys [db] :as cofx} tx-id] [{:keys [db] :as cofx} tx-id]
{::start-watching tx-id}) {::start-watching [tx-id]})
(fx/defn watch-transsactions
[_ hashes]
{::start-watching hashes})
(fx/defn clear-timeouts (fx/defn clear-timeouts
[{:keys [db]}] [{:keys [db]}]
@ -849,3 +854,56 @@
{:view :share-account {:view :share-account
:address address}) :address address})
(keep-watching-history)))) (keep-watching-history))))
(re-frame/reg-fx
::get-pending-transactions
(fn []
(json-rpc/call
{:method "wallet_getPendingTransactions"
:params []
:on-success #(re-frame/dispatch [:wallet/on-retreiving-pending-transactions %])})))
(fx/defn get-pending-transactions
{:events [:wallet/get-pending-transactions]}
[_]
(log/info "[wallet] get pending transactions")
{::get-pending-transactions nil})
(defn normalize-transaction
[db {:keys [symbol gasPrice gasLimit value from to] :as transaction}]
(let [symbol (keyword symbol)
token (tokens/symbol->token (:wallet/all-tokens db) symbol)]
(-> transaction
(select-keys [:timestamp :hash :data])
(assoc :from (eip55/address->checksum from)
:to (eip55/address->checksum to)
:type :pending
:symbol symbol
:token token
:value (money/bignumber value)
:gas-price (money/bignumber gasPrice)
:gas-limit (money/bignumber gasLimit)))))
(fx/defn on-retriving-pending-transactions
{:events [:wallet/on-retreiving-pending-transactions]}
[{:keys [db]} raw-transactions]
(log/info "[wallet] pending transactions")
{:db
(reduce (fn [db {:keys [from hash] :as transaction}]
(let [path [:wallet :accounts from :transactions hash]]
(if-not (get-in db path)
(assoc-in db path transaction)
db)))
db
(map (partial normalize-transaction db) raw-transactions))
::start-watching (map :hash raw-transactions)})
(re-frame/reg-fx
:wallet/delete-pending-transactions
(fn [hashes]
(log/info "[wallet] delete pending transactions")
(doseq [hash hashes]
(json-rpc/call
{:method "wallet_deletePendingTransaction"
:params [hash]
:on-success #(log/info "[wallet] pending transaction deleted" hash)}))))

View File

@ -2,7 +2,7 @@
"_comment": "DO NOT EDIT THIS FILE BY HAND. USE 'scripts/update-status-go.sh <tag>' instead", "_comment": "DO NOT EDIT THIS FILE BY HAND. USE 'scripts/update-status-go.sh <tag>' instead",
"owner": "status-im", "owner": "status-im",
"repo": "status-go", "repo": "status-go",
"version": "v0.76.6", "version": "v0.77.0",
"commit-sha1": "fccee2230fcc8b7b7fca069765b8584dbe921d3f", "commit-sha1": "6037570901233477cbef85d5494b257076a3acd9",
"src-sha256": "0gpimg5cwh6vawf3fsclbyimb3y9c8mpdhwd017p47g4fds697cj" "src-sha256": "1f5pr4w5a8lby2sqginddbjff2fp1d906nnr1y7zbsgafpi82j83"
} }