[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,28 +109,39 @@
(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
gas-price (money/to-fixed (money/bignumber gasPrice))
gas-limit (money/to-fixed (money/bignumber gas))
tx {:timestamp now
:to to
:from from
:type :pending
:hash new-tx-hash
:data data
:token token
:symbol symbol
:value (if token
(money/to-fixed (money/unit->token amount (:decimals token)))
(money/to-fixed (money/bignumber value)))
:gas-price gas-price
:gas-limit gas-limit}]
(log/info "[signing] prepare-unconfirmed-transaction" tx)
{:db (-> db {:db (-> db
;;remove old transaction, because we replace it with the new one ;;remove old transaction, because we replace it with the new one
(update-in [:wallet :accounts from :transactions] dissoc old-tx-hash) (update-in [:wallet :accounts from :transactions] dissoc old-tx-hash)
(assoc-in [:wallet :accounts from :transactions new-tx-hash] (assoc-in [:wallet :accounts from :transactions new-tx-hash] tx))
{:timestamp (str now) ::json-rpc/call [{:method "wallet_storePendingTransaction"
:to to :params [(-> tx
:from from (dissoc :gas-price :gas-limit)
:type :pending (assoc :gasPrice
:hash new-tx-hash (money/to-fixed (money/bignumber gasPrice))
:data data :gasLimit (money/to-fixed (money/bignumber gas)))
:token token clj->js)]
:symbol symbol :on-success #(log/info "pending transfer is saved")
:value (if token :on-failure #(log/info "pending transfer was not saved" %)}]}))
(money/unit->token amount (:decimals token))
(money/to-fixed (money/bignumber value)))
:gas-price (money/to-fixed (money/bignumber gasPrice))
:gas-limit (money/to-fixed (money/bignumber gas))}))}))
(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)
(json-rpc/call (doseq [hash hashes]
{:method "wallet_watchTransaction" (json-rpc/call
:params [hash] {:method "wallet_watchTransaction"
:on-success #(re-frame.core/dispatch [::restart true]) :params [hash]
:on-error #(log/info "[wallet] watch transaction error" % "hash" hash)}))) :on-success #(re-frame.core/dispatch [::restart true])
: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"
} }