create subscriptions for wallet data bindings

This commit is contained in:
Eric Dvorsak 2017-08-23 02:01:19 +02:00 committed by Julien Eluard
parent e169949e0a
commit 37a67b3ccf
6 changed files with 147 additions and 88 deletions

View File

@ -7,6 +7,7 @@
status-im.ui.screens.discover.subs status-im.ui.screens.discover.subs
status-im.ui.screens.contacts.subs status-im.ui.screens.contacts.subs
status-im.ui.screens.group.subs status-im.ui.screens.group.subs
status-im.ui.screens.wallet.subs
status-im.transactions.subs status-im.transactions.subs
status-im.bots.subs)) status-im.bots.subs))

View File

@ -5,3 +5,15 @@
;; TODO(oskarth): spec for balance as BigNumber ;; TODO(oskarth): spec for balance as BigNumber
;; TODO(oskarth): Spec for prices as as: {:from ETH, :to USD, :price 290.11, :last-day 304.17} ;; TODO(oskarth): Spec for prices as as: {:from ETH, :to USD, :price 290.11, :last-day 304.17}
(def dummy-transaction-data
[{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "0,4908" :symbol "ETH"} :state :unsigned}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "10000" :symbol "SGT"} :state :unsigned}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "10000" :symbol "SGT"} :state :unsigned}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "0,4908" :symbol "ETH"} :state :pending}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "10000" :symbol "SGT"} :state :pending}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "10000" :symbol "SGT"} :state :sent}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "10000" :symbol "SGT"} :state :postponed}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "0,4908" :symbol "ETH"} :state :pending}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "10000" :symbol "SGT"} :state :pending}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "10000" :symbol "SGT"} :state :sent}])

View File

@ -1,7 +1,9 @@
(ns status-im.ui.screens.wallet.events (ns status-im.ui.screens.wallet.events
(:require [re-frame.core :as re-frame :refer [dispatch reg-fx]] (:require [re-frame.core :as re-frame :refer [dispatch reg-fx]]
[status-im.utils.handlers :as handlers] [status-im.utils.handlers :as handlers]
[status-im.utils.prices :as prices])) [status-im.utils.prices :as prices]
[status-im.ui.screens.wallet.db :as wallet.db]
[status-im.components.status :as status]))
(defn get-balance [{:keys [web3 account-id on-success on-error]}] (defn get-balance [{:keys [web3 account-id on-success on-error]}]
(if (and web3 account-id) (if (and web3 account-id)
@ -17,20 +19,20 @@
;; FX ;; FX
(reg-fx (reg-fx
:get-balance :get-balance
(fn [{:keys [web3 account-id success-event error-event]}] (fn [{:keys [web3 account-id success-event error-event]}]
(get-balance (get-balance
{:web3 web3 {:web3 web3
:account-id account-id :account-id account-id
:on-success #(dispatch [success-event %]) :on-success #(dispatch [success-event %])
:on-error #(dispatch [error-event %])}))) :on-error #(dispatch [error-event %])})))
(reg-fx (reg-fx
:get-prices :get-prices
(fn [{:keys [from to success-event error-event]}] (fn [{:keys [from to success-event error-event]}]
(prices/get-prices (prices/get-prices
from from
to to
#(dispatch [success-event %]) #(dispatch [success-event %])
#(dispatch [error-event %])))) #(dispatch [error-event %]))))
@ -38,38 +40,39 @@
;; TODO(oskarth): At some point we want to get list of relevant assets to get prices for ;; TODO(oskarth): At some point we want to get list of relevant assets to get prices for
(handlers/register-handler-fx (handlers/register-handler-fx
:load-prices :load-prices
(fn [{{:keys [wallet] :as db} :db} [_ a]] (fn [{{:keys [wallet] :as db} :db} [_ a]]
{:get-prices {:from "ETH" {:get-prices {:from "ETH"
:to "USD" :to "USD"
:success-event :update-prices :success-event :update-prices
:error-event :update-prices-fail}})) :error-event :update-prices-fail}}))
(handlers/register-handler-fx (handlers/register-handler-fx
:init-wallet :init-wallet
(fn [{{:keys [web3 accounts/current-account-id] :as db} :db} [_ a]] (fn [{{:keys [web3 accounts/current-account-id] :as db} :db} [_ a]]
{:get-balance {:web3 web3 {:get-balance {:web3 web3
:account-id current-account-id :account-id current-account-id
:success-event :update-balance :success-event :update-balance
:error-event :update-balance-fail} :error-event :update-balance-fail}
:dispatch [:load-prices]})) :dispatch [:load-prices]
:db (assoc-in db [:wallet :transactions] wallet.db/dummy-transaction-data)}))
(handlers/register-handler-db (handlers/register-handler-db
:update-balance :update-balance
(fn [db [_ balance]] (fn [db [_ balance]]
(assoc db :wallet {:balance balance}))) (assoc-in db [:wallet :balance] balance)))
(handlers/register-handler-db (handlers/register-handler-db
:update-prices :update-prices
(fn [db [_ prices]] (fn [db [_ prices]]
(assoc db :prices prices))) (assoc db :prices prices)))
(handlers/register-handler-fx (handlers/register-handler-fx
:update-balance-fail :update-balance-fail
(fn [_ [_ err]] (fn [_ [_ err]]
(.log js/console "Unable to get balance: " err))) (.log js/console "Unable to get balance: " err)))
(handlers/register-handler-fx (handlers/register-handler-fx
:update-prices-fail :update-prices-fail
(fn [_ [_ err]] (fn [_ [_ err]]
(.log js/console "Unable to get prices: " err))) (.log js/console "Unable to get prices: " err)))

View File

@ -59,37 +59,32 @@
[action-buttons m])] [action-buttons m])]
[list/item-icon :icons/forward]]) [list/item-icon :icons/forward]])
(def dummy-transaction-data
[{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "0,4909" :symbol "ETH"} :state :unsigned}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "10000" :symbol "SGT"} :state :unsigned}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "10000" :symbol "SGT"} :state :unsigned}])
(def dummy-transaction-data-sorted
[{:title "Postponed"
:key :postponed
:data [{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "0,4909" :symbol "ETH"} :state :pending}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "10000" :symbol "SGT"} :state :pending}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "10000" :symbol "SGT"} :state :sent}]}
{:title "Pending"
:key :pending
:data [{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "0,4909" :symbol "ETH"} :state :pending}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "10000" :symbol "SGT"} :state :pending}
{:to "0x829bd824b016326a401d083b33d092293333a830" :content {:value "10000" :symbol "SGT"} :state :sent}]}])
;; TODO(yenda) hook with re-frame ;; TODO(yenda) hook with re-frame
(defn empty-text [s] (defn empty-text [s]
[rn/text {:style st/empty-text} s]) [rn/text {:style st/empty-text} s])
(defview history-list [] (defview history-list []
[list/section-list {:sections dummy-transaction-data-sorted (letsubs [pending-transactions [:wallet/pending-transactions]
:render-fn render-transaction postponed-transactions [:wallet/postponed-transactions]
:empty-component (empty-text (i18n/label :t/transactions-history-empty))}]) sent-transactions [:wallet/sent-transactions]]
[list/section-list {:sections [{:title "Postponed"
:key :postponed
:data postponed-transactions}
{:title "Pending"
:key :pending
:data pending-transactions}
{:title "Sent"
:key :sent
:data sent-transactions}]
:render-fn render-transaction
:empty-component (empty-text (i18n/label :t/transactions-history-empty))}]))
(defview unsigned-list [] (defview unsigned-list []
[list/flat-list {:data dummy-transaction-data (letsubs [transactions [:wallet/unsigned-transactions]]
:render-fn render-transaction [list/flat-list {:data transactions
:empty-component (empty-text (i18n/label :t/transactions-unsigned-empty))}]) :render-fn render-transaction
:empty-component (empty-text (i18n/label :t/transactions-unsigned-empty))}]))
(def tab-list (def tab-list
[{:view-id :wallet-transactions-unsigned [{:view-id :wallet-transactions-unsigned

View File

@ -88,31 +88,12 @@
[list/flat-list {:data assets [list/flat-list {:data assets
:render-fn render-asset-fn}]])) :render-fn render-asset-fn}]]))
(defn eth-balance [{:keys [balance]}]
(when balance
(money/wei->ether balance)))
(defn portfolio-value [{:keys [balance]} {:keys [price]}]
(when (and balance price)
(-> (money/wei->ether balance)
(money/eth->usd price)
(money/with-precision 2)
str)))
(defn portfolio-change [{:keys [price last-day]}]
(when (and price last-day)
(-> (money/percent-change price last-day)
(money/with-precision 2)
(str "%"))))
(defview wallet [] (defview wallet []
(letsubs [wallet [:get :wallet] (letsubs [eth-balance [:eth-balance]
prices [:get :prices]] portfolio-value [:portfolio-value]
(let [eth (or (eth-balance wallet) "...") portfolio-change [:portfolio-change]]
usd (or (portfolio-value wallet prices) "...") [rn/view {:style st/wallet-container}
change (or (portfolio-change prices) "-%")] [toolbar-view]
[rn/view {:style st/wallet-container} [rn/scroll-view
[toolbar-view] [main-section portfolio-value portfolio-change]
[rn/scroll-view [asset-section eth-balance]]]))
[main-section usd change]
[asset-section eth]]])))

View File

@ -0,0 +1,67 @@
(ns status-im.ui.screens.wallet.subs
(:require [re-frame.core :refer [reg-sub subscribe]]
[status-im.utils.money :as money]))
(reg-sub :balance
(fn [db]
(get-in db [:wallet :balance])))
(reg-sub :price
(fn [db]
(get-in db [:prices :price])))
(reg-sub :last-day
(fn [db]
(get-in db [:prices :last-day])))
(reg-sub :eth-balance
:<- [:balance]
(fn [balance]
(if balance
(money/wei->ether balance)
"...")))
(reg-sub :portfolio-value
:<- [:balance]
:<- [:price]
(fn [[balance price]]
(if (and balance price)
(-> (money/wei->ether balance)
(money/eth->usd price)
(money/with-precision 2)
str)
"...")))
(reg-sub :portfolio-change
:<- [:price]
:<- [:last-day]
(fn [[price last-day]]
(if (and price last-day)
(-> (money/percent-change price last-day)
(money/with-precision 2)
(str "%"))
"-%")))
(reg-sub :wallet/transactions
(fn [db]
(get-in db [:wallet :transactions])))
(reg-sub :wallet/unsigned-transactions
:<- [:wallet/transactions]
(fn [transactions]
(filter #(= (:state %) :unsigned) transactions)))
(reg-sub :wallet/pending-transactions
:<- [:wallet/transactions]
(fn [transactions]
(filter #(= (:state %) :pending) transactions)))
(reg-sub :wallet/postponed-transactions
:<- [:wallet/transactions]
(fn [transactions]
(filter #(= (:state %) :postponed) transactions)))
(reg-sub :wallet/sent-transactions
:<- [:wallet/transactions]
(fn [transactions]
(filter #(= (:state %) :sent) transactions)))