Skip to content

Commit

Permalink
[wallet] use status-go wallet service
Browse files Browse the repository at this point in the history
- remove use of etherscan and subscriptions
- replace by status-go
  • Loading branch information
yenda committed Jul 11, 2019
1 parent e282025 commit 2d3636f
Show file tree
Hide file tree
Showing 15 changed files with 163 additions and 500 deletions.
2 changes: 1 addition & 1 deletion STATUS_GO_SHA256
@@ -1,3 +1,3 @@
## DO NOT EDIT THIS FILE BY HAND. USE `scripts/update-status-go.sh <tag>` instead

1nm0b1nbz4ly2fx6n2pybrfa1m9x0zzj7kkkqf7fwjxg6hjnp6y5
140wi961rjgf9zcl0lim8pzvzlw0xxbipnz9f17cra9qchgvhan2
2 changes: 1 addition & 1 deletion STATUS_GO_VERSION
@@ -1,3 +1,3 @@
## DO NOT EDIT THIS FILE BY HAND. USE `scripts/update-status-go.sh <tag>` instead

v0.30.0-beta.0
v0.30.0-beta.2
3 changes: 1 addition & 2 deletions src/status_im/accounts/login/core.cljs
Expand Up @@ -88,8 +88,7 @@
(wallet/initialize-tokens)
(wallet/update-balances)
(wallet/update-prices)
(transactions/initialize)
(ethereum.subscriptions/initialize)))
(transactions/initialize)))

(fx/defn user-login-without-creating-db
{:events [:accounts.login.ui/password-input-submitted]}
Expand Down
8 changes: 8 additions & 0 deletions src/status_im/ethereum/encode.cljs
@@ -0,0 +1,8 @@
(ns status-im.ethereum.encode
(:require [status-im.js-dependencies :as dependencies]))

(defn utils [] (dependencies/web3-utils))

(defn uint
[x]
(.numberToHex (utils) x))
3 changes: 2 additions & 1 deletion src/status_im/ethereum/json_rpc.cljs
Expand Up @@ -35,7 +35,8 @@
"status_joinPublicChat" {}
"status_chats" {}
"status_startOneOnOneChat" {}
"status_removeChat" {}})
"status_removeChat" {}
"wallet_getTransfers" {}})

(defn call
[{:keys [method params on-success on-error]}]
Expand Down
106 changes: 31 additions & 75 deletions src/status_im/ethereum/subscriptions.cljs
Expand Up @@ -4,6 +4,7 @@
[status-im.ethereum.core :as ethereum]
[status-im.ethereum.json-rpc :as json-rpc]
[status-im.ethereum.tokens :as tokens]
[status-im.wallet.db :as wallet]
[status-im.ethereum.transactions.core :as transactions]
[status-im.utils.fx :as fx]
[taoensso.timbre :as log]))
Expand All @@ -22,82 +23,37 @@
[{:keys [db]} id handler]
{:db (assoc-in db [:ethereum/subscriptions id] handler)})

(defn keep-user-transactions
[wallet-address transactions]
(keep (fn [{:keys [to from] :as transaction}]
(when-let [direction (cond
(= wallet-address to) :inbound
(= wallet-address from) :outbound)]
(assoc transaction :direction direction)))
transactions))

(fx/defn new-block
[{:keys [db] :as cofx} {:keys [number transactions] :as block}]
(when number
(let [{:keys [:wallet/all-tokens :ethereum/current-block]} db
chain (ethereum/chain-keyword db)
chain-tokens (into {} (map (juxt :address identity)
(tokens/tokens-for all-tokens chain)))
wallet-address (ethereum/current-address db)
token-contracts-addresses (into #{} (keys chain-tokens))]
(fx/merge cofx
{:db (assoc-in db [:ethereum/current-block] number)
:ethereum.transactions/enrich-transactions-from-new-blocks
{:chain-tokens chain-tokens
:block block
:transactions (keep-user-transactions wallet-address
transactions)}}
(transactions/check-watched-transactions)
(when (or (not current-block)
(not= number (inc current-block)))
;; in case we skipped some blocks or got an uncle, re-fetch history
;; from etherscan
(transactions/initialize))))))

(defn new-token-transaction-filter
[{:keys [chain-tokens from to] :as args}]
(json-rpc/call
{:method "eth_newFilter"
:params [{:fromBlock "latest"
:toBlock "latest"
:topics [constants/event-transfer-hash from to]}]
:on-success (transactions/inbound-token-transfer-handler chain-tokens)}))

(re-frame/reg-fx
:ethereum.subscriptions/token-transactions
(fn [{:keys [address] :as args}]
;; start inbound token transaction subscriptions
;; outbound token transactions are already caught in new blocks filter
(new-token-transaction-filter (merge args
{:direction :inbound
:to address}))))

(defn new-block-filter
[]
(json-rpc/call
{:method "eth_newBlockFilter"
:on-success
(fn [[block-hash]]
(json-rpc/call
{:method "eth_getBlockByHash"
:params [block-hash true]
:on-success
(fn [block]
(re-frame/dispatch [:ethereum.signal/new-block block]))}))}))

(re-frame/reg-fx
:ethereum.subscriptions/new-block
new-block-filter)

(fx/defn initialize
[{:keys [db] :as cofx}]
[{:keys [db] :as cofx} historical? block-number accounts]
(let [{:keys [:wallet/all-tokens]} db
chain (ethereum/chain-keyword db)
chain-tokens (into {} (map (juxt :address identity)
(tokens/tokens-for all-tokens chain)))]
(fx/merge cofx
(cond-> {}
(not historical?)
(assoc :db (assoc db [:ethereum/current-block] block-number))

(not-empty accounts)
(assoc ::transactions/get-transfers {:chain-tokens chain-tokens
:from-block block-number}))
(transactions/check-watched-transactions))))

(fx/defn reorg
[{:keys [db] :as cofx} block-number accounts]
(let [{:keys [:wallet/all-tokens]} db
chain (ethereum/chain-keyword db)
chain-tokens (into {} (map (juxt :address identity)
(tokens/tokens-for all-tokens chain)))
normalized-address (ethereum/current-address db)
padded-address (transactions/add-padding normalized-address)]
{:ethereum.subscriptions/new-block nil
:ethereum.subscriptions/token-transactions
{:chain-tokens chain-tokens
:address padded-address}}))
(tokens/tokens-for all-tokens chain)))]
{:db (update-in db [:wallet :transactions]
wallet/remove-transactions-since-block block-number)
::transactions/get-transfers {:chain-tokens chain-tokens
:from-block block-number}}))

(fx/defn new-wallet-event
[{:keys [db] :as cofx} {:keys [type blockNumber accounts] :as event}]
(case type
"newblock" (new-block cofx false blockNumber accounts)
"history" (new-block cofx true blockNumber accounts)
"reorg" (reorg cofx blockNumber accounts)
(log/warn ::unknown-wallet-event :type type :event event)))

0 comments on commit 2d3636f

Please sign in to comment.