feature #4842 - initially autoconfiguring visible tokens based on balance upon onboarding

Signed-off-by: Goran Jovic <goranjovic@gmail.com>
This commit is contained in:
Goran Jovic 2018-08-01 14:57:12 +02:00
parent 746d580e34
commit a8f267e662
No known key found for this signature in database
GPG Key ID: D429D1A9B2EB8A8E
4 changed files with 58 additions and 22 deletions

View File

@ -4,7 +4,6 @@
[status-im.native-module.core :as status]
[status-im.utils.types :refer [json->clj]]
[status-im.utils.identicon :refer [identicon]]
[status-im.utils.random :as random]
[clojure.string :as str]
[status-im.i18n :as i18n]
[status-im.utils.config :as config]
@ -17,12 +16,11 @@
[status-im.utils.gfycat.core :refer [generate-gfy]]
[status-im.utils.hex :as utils.hex]
[status-im.constants :as constants]
[status-im.transport.message.core :as transport]
status-im.ui.screens.accounts.create.navigation
[status-im.chat.models :as chat.models]
[status-im.ui.screens.accounts.utils :as accounts.utils]
[status-im.data-store.accounts :as accounts-store]
[status-im.ui.screens.navigation :as navigation]))
[status-im.ui.screens.navigation :as navigation]
[status-im.ui.screens.wallet.settings.models :as wallet.settings.models]))
;;;; COFX
@ -173,4 +171,5 @@
(handlers-macro/merge-fx
cofx
(wallet-set-up-passed db modal?)
(wallet.settings.models/wallet-autoconfig-tokens)
(accounts.utils/account-update {:wallet-set-up-passed? true}))))

View File

@ -207,12 +207,15 @@
(assoc-error-message :balance-update err)
(assoc-in [:wallet :balance-loading?] false))))
(handlers/register-handler-db
(defn update-token-balance-success [symbol balance {:keys [db]}]
{:db (-> db
(assoc-in [:wallet :balance symbol] balance)
(assoc-in [:wallet :balance-loading?] false))})
(handlers/register-handler-fx
:update-token-balance-success
(fn [db [_ symbol balance]]
(-> db
(assoc-in [:wallet :balance symbol] balance)
(assoc-in [:wallet :balance-loading?] false))))
(fn [cofx [_ symbol balance]]
(update-token-balance-success symbol balance cofx)))
(handlers/register-handler-db
:update-token-balance-fail

View File

@ -1,18 +1,14 @@
(ns status-im.ui.screens.wallet.settings.events
(:require [status-im.ui.screens.accounts.events :as accounts]
[status-im.utils.ethereum.core :as ethereum]
(:require [status-im.ui.screens.wallet.settings.models :as models]
[status-im.ui.screens.accounts.events :as accounts]
[status-im.utils.handlers :as handlers]))
(defn- toggle-checked [ids id checked?]
(if checked?
(conj (or ids #{}) id)
(disj ids id)))
(handlers/register-handler-fx
:wallet.settings/toggle-visible-token
(fn [{{:keys [account/account] :as db} :db :as cofx} [_ symbol checked?]]
(let [network (get (:networks account) (:network account))
chain (ethereum/network->chain-keyword network)
settings (get account :settings)
new-settings (update-in settings [:wallet :visible-tokens chain] #(toggle-checked % symbol checked?))]
(accounts/update-settings new-settings cofx))))
(fn [cofx [_ symbol checked?]]
(models/toggle-visible-token symbol checked? accounts/update-settings cofx)))
(handlers/register-handler-fx
:configure-token-balance-and-visibility
(fn [cofx [_ symbol balance]]
(models/configure-token-balance-and-visibility symbol balance accounts/update-settings cofx)))

View File

@ -0,0 +1,38 @@
(ns status-im.ui.screens.wallet.settings.models
(:require [status-im.utils.ethereum.core :as ethereum]
[status-im.utils.handlers-macro :as handlers-macro]
[status-im.ui.screens.wallet.events :as wallet.events]
[status-im.utils.ethereum.tokens :as tokens]
[re-frame.core :as re-frame]))
(defn- set-checked [ids id checked?]
(if checked?
(conj (or ids #{}) id)
(disj ids id)))
(defn toggle-visible-token [symbol checked? update-settings-fx {{:keys [account/account]} :db :as cofx}]
(let [network (get (:networks account) (:network account))
chain (ethereum/network->chain-keyword network)
settings (get account :settings)
new-settings (update-in settings [:wallet :visible-tokens chain] #(set-checked % symbol checked?))]
(update-settings-fx new-settings cofx)))
(defn configure-token-balance-and-visibility [symbol balance update-settings-fx cofx]
(handlers-macro/merge-fx cofx
(toggle-visible-token symbol true update-settings-fx)
;;TODO(goranjovic): move `update-token-balance-success` function to wallet models
(wallet.events/update-token-balance-success symbol balance)))
(defn wallet-autoconfig-tokens [{:keys [db]}]
(let [{:keys [account/account web3]} db
network (get (:networks account) (:network account))
chain (ethereum/network->chain-keyword network)
contracts (->> (tokens/tokens-for chain)
(remove :hidden?))]
(doseq [{:keys [address symbol]} contracts]
;;TODO(goranjovic): move `get-token-balance` function to wallet models
(wallet.events/get-token-balance {:web3 web3
:contract address
:account-id (:address account)
:on-success #(when (> % 0)
(re-frame/dispatch [:configure-token-balance-and-visibility symbol %]))}))))