refactor :initialize-account into single event

fix tests for :initialize-account refactoring
fix destructuring typo
This commit is contained in:
Eric Dvorsak 2018-08-22 23:31:22 +02:00
parent 198596d5e6
commit 002f3e73d1
No known key found for this signature in database
GPG Key ID: 932AC1CE5F05DE0C
28 changed files with 445 additions and 466 deletions

View File

@ -1,29 +1,23 @@
(ns status-im.chat.events (ns status-im.chat.events
(:require [clojure.set :as set] (:require status-im.chat.events.input
status-im.chat.events.requests
status-im.chat.events.send-message
status-im.chat.events.receive-message
[clojure.string :as string] [clojure.string :as string]
[re-frame.core :as re-frame] [re-frame.core :as re-frame]
[status-im.constants :as constants]
[status-im.i18n :as i18n]
[status-im.chat.models :as models] [status-im.chat.models :as models]
[status-im.chat.models.message :as models.message] [status-im.chat.models.message :as models.message]
[status-im.chat.commands.core :as commands] [status-im.constants :as constants]
[status-im.data-store.user-statuses :as user-statuses-store]
[status-im.i18n :as i18n]
[status-im.transport.message.core :as transport.message]
[status-im.transport.message.v1.group-chat :as group-chat]
[status-im.transport.message.v1.protocol :as protocol]
[status-im.transport.message.v1.public-chat :as public-chat]
[status-im.ui.screens.navigation :as navigation] [status-im.ui.screens.navigation :as navigation]
[status-im.utils.handlers :as handlers] [status-im.utils.handlers :as handlers]
[status-im.utils.handlers-macro :as handlers-macro] [status-im.utils.handlers-macro :as handlers-macro]
[status-im.utils.contacts :as utils.contacts] [status-im.utils.utils :as utils]))
[status-im.utils.utils :as utils]
[status-im.transport.message.core :as transport.message]
[status-im.transport.message.v1.protocol :as protocol]
[status-im.transport.message.v1.public-chat :as public-chat]
[status-im.transport.message.v1.group-chat :as group-chat]
[status-im.data-store.chats :as chats-store]
[status-im.data-store.messages :as messages-store]
[status-im.data-store.user-statuses :as user-statuses-store]
[status-im.data-store.contacts :as contacts-store]
status-im.chat.events.input
status-im.chat.events.requests
status-im.chat.events.send-message
status-im.chat.events.receive-message))
;;;; Effects ;;;; Effects
@ -84,105 +78,6 @@
new-status) new-status)
:data-store/tx [(user-statuses-store/save-status-tx new-status)]}))) :data-store/tx [(user-statuses-store/save-status-tx new-status)]})))
;; Change status of own messages which are still in "sending" status to "not-sent"
;; (If signal from status-go has not been received)
(handlers/register-handler-fx
:process-pending-messages
[re-frame/trim-v]
(fn [{:keys [db]} []]
(let [me (:current-public-key db)
pending-statuses (->> (vals (:chats db))
(mapcat :message-statuses)
(mapcat (fn [[_ user-id->status]]
(filter (comp (partial = :sending) :status)
(get user-id->status me)))))
updated-statuses (map #(assoc % :status :not-sent) pending-statuses)]
{:data-store/tx [(user-statuses-store/save-statuses-tx updated-statuses)]
:db (reduce
(fn [acc {:keys [chat-id message-id status whisper-identity]}]
(assoc-in acc
[:chats chat-id :message-status message-id
whisper-identity :status]
status))
db
updated-statuses)})))
(defn- add-default-contacts
[{:keys [db default-contacts] :as cofx}]
(let [new-contacts (-> {}
(into (map (fn [[id props]]
(let [contact-id (name id)]
[contact-id {:whisper-identity contact-id
:address (utils.contacts/public-key->address contact-id)
:name (-> props :name :en)
:photo-path (:photo-path props)
:public-key (:public-key props)
:unremovable? (-> props :unremovable? boolean)
:hide-contact? (-> props :hide-contact? boolean)
:pending? (-> props :pending? boolean)
:dapp? (:dapp? props)
:dapp-url (-> props :dapp-url :en)
:bot-url (:bot-url props)
:description (:description props)}])))
default-contacts))
existing-contacts (:contacts/contacts db)
contacts-to-add (select-keys new-contacts (set/difference (set (keys new-contacts))
(set (keys existing-contacts))))]
{:db (update db :contacts/contacts merge contacts-to-add)
:data-store/tx [(contacts-store/save-contacts-tx (vals contacts-to-add))]}))
(defn- group-chat-messages
[{:keys [db]}]
(reduce-kv (fn [fx chat-id {:keys [messages]}]
(models.message/group-messages chat-id (vals messages) fx))
{:db db}
(:chats db)))
(handlers/register-handler-fx
:initialize-chats
[(re-frame/inject-cofx :get-default-contacts)
(re-frame/inject-cofx :get-default-dapps)
(re-frame/inject-cofx :data-store/all-chats)
(re-frame/inject-cofx :data-store/get-messages)
(re-frame/inject-cofx :data-store/get-user-statuses)
(re-frame/inject-cofx :data-store/unviewed-messages)
(re-frame/inject-cofx :data-store/message-ids)
(re-frame/inject-cofx :data-store/get-unanswered-requests)
(re-frame/inject-cofx :data-store/get-local-storage-data)]
(fn [{:keys [db
default-dapps
all-stored-chats
stored-unanswered-requests
get-stored-messages
get-stored-user-statuses
stored-unviewed-messages
stored-message-ids] :as cofx} _]
(let [chat->message-id->request (reduce (fn [acc {:keys [chat-id message-id] :as request}]
(assoc-in acc [chat-id message-id] request))
{}
stored-unanswered-requests)
chats (reduce (fn [acc {:keys [chat-id] :as chat}]
(let [chat-messages (index-messages (get-stored-messages chat-id))
message-ids (keys chat-messages)
unviewed-ids (get stored-unviewed-messages chat-id)]
(assoc acc chat-id
(assoc chat
:unviewed-messages unviewed-ids
:requests (get chat->message-id->request chat-id)
:messages chat-messages
:message-statuses (get-stored-user-statuses chat-id message-ids)
:not-loaded-message-ids (set/difference (get stored-message-ids chat-id)
(set message-ids))))))
{}
all-stored-chats)]
(handlers-macro/merge-fx cofx
{:db (assoc db
:chats chats
:contacts/dapps default-dapps)}
(group-chat-messages)
(add-default-contacts)
(commands/index-commands commands/register)))))
(defn- send-messages-seen [chat-id message-ids {:keys [db] :as cofx}] (defn- send-messages-seen [chat-id message-ids {:keys [db] :as cofx}]
(when (and (not (get-in db [:chats chat-id :public?])) (when (and (not (get-in db [:chats chat-id :public?]))
(not (models/bot-only-chat? db chat-id))) (not (models/bot-only-chat? db chat-id)))

View File

@ -1,14 +1,14 @@
(ns status-im.chat.models (ns status-im.chat.models
(:require [status-im.ui.components.styles :as styles] (:require [status-im.data-store.chats :as chats-store]
[status-im.utils.gfycat.core :as gfycat] [status-im.data-store.messages :as messages-store]
[status-im.transport.utils :as transport.utils]
[status-im.utils.platform :as platform]
[status-im.utils.clocks :as utils.clocks]
[status-im.transport.message.core :as transport.message] [status-im.transport.message.core :as transport.message]
[status-im.data-store.chats :as chats-store]
[status-im.transport.message.v1.group-chat :as transport.group-chat] [status-im.transport.message.v1.group-chat :as transport.group-chat]
[status-im.transport.utils :as transport.utils]
[status-im.ui.components.styles :as styles]
[status-im.utils.clocks :as utils.clocks]
[status-im.utils.gfycat.core :as gfycat]
[status-im.utils.handlers-macro :as handlers-macro] [status-im.utils.handlers-macro :as handlers-macro]
[status-im.data-store.messages :as messages-store])) [status-im.utils.platform :as platform]))
(defn multi-user-chat? [chat-id cofx] (defn multi-user-chat? [chat-id cofx]
(get-in cofx [:db :chats chat-id :group-chat])) (get-in cofx [:db :chats chat-id :group-chat]))

View File

@ -1,5 +1,10 @@
(ns status-im.models.account) (ns status-im.models.account
(:require [status-im.ui.screens.accounts.utils :as accounts.utils]))
(defn logged-in? [cofx] (defn logged-in? [cofx]
(boolean (boolean
(get-in cofx [:db :account/account]))) (get-in cofx [:db :account/account])))
(defn update-sign-in-time
[{db :db now :now :as cofx}]
(accounts.utils/account-update {:last-sign-in now} cofx))

View File

@ -1,9 +1,7 @@
(ns status-im.models.bootnode (ns status-im.models.bootnode
(:require (:require [clojure.string :as string]
[clojure.string :as string]
[status-im.ui.screens.accounts.utils :as accounts.utils] [status-im.ui.screens.accounts.utils :as accounts.utils]
[status-im.utils.handlers-macro :as handlers-macro] [status-im.utils.handlers-macro :as handlers-macro]))
[status-im.utils.ethereum.core :as ethereum]))
(def address-regex #"enode://[a-zA-Z0-9]+:?(.*)\@\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b:(\d{1,5})") (def address-regex #"enode://[a-zA-Z0-9]+:?(.*)\@\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b:(\d{1,5})")

View File

@ -1,11 +1,11 @@
(ns status-im.models.browser (ns status-im.models.browser
(:require [status-im.data-store.browser :as browser-store] (:require [re-frame.core :as re-frame]
[status-im.constants :as constants]
[status-im.data-store.browser :as browser-store]
[status-im.data-store.dapp-permissions :as dapp-permissions] [status-im.data-store.dapp-permissions :as dapp-permissions]
[status-im.i18n :as i18n] [status-im.i18n :as i18n]
[status-im.constants :as constants]
[status-im.ui.screens.browser.default-dapps :as default-dapps] [status-im.ui.screens.browser.default-dapps :as default-dapps]
[status-im.utils.http :as http] [status-im.utils.http :as http]))
[re-frame.core :as re-frame]))
(defn get-current-url [{:keys [history history-index]}] (defn get-current-url [{:keys [history history-index]}]
(when (and history-index history) (when (and history-index history)
@ -108,3 +108,13 @@
:messageId message-id :messageId message-id
:error %1 :error %1
:result %2}])]})) :result %2}])]}))
(defn initialize-browsers
[{:keys [db all-stored-browsers]}]
(let [browsers (into {} (map #(vector (:browser-id %) %) all-stored-browsers))]
{:db (assoc db :browser/browsers browsers)}))
(defn initialize-dapp-permissions
[{:keys [db all-dapp-permissions]}]
(let [dapp-permissions (into {} (map #(vector (:dapp %) %) all-dapp-permissions))]
{:db (assoc db :dapps/permissions dapp-permissions)}))

View File

@ -0,0 +1,96 @@
(ns status-im.models.chat
(:require [clojure.set :as set]
[status-im.chat.commands.core :as commands]
[status-im.chat.models.message :as models.message]
[status-im.data-store.contacts :as contacts-store]
[status-im.data-store.user-statuses :as user-statuses-store]
[status-im.utils.contacts :as utils.contacts]
[status-im.utils.handlers-macro :as handlers-macro]))
(def index-messages (partial into {} (map (juxt :message-id identity))))
(defn- add-default-contacts
[{:keys [db default-contacts] :as cofx}]
(let [new-contacts (-> {}
(into (map (fn [[id props]]
(let [contact-id (name id)]
[contact-id {:whisper-identity contact-id
:address (utils.contacts/public-key->address contact-id)
:name (-> props :name :en)
:photo-path (:photo-path props)
:public-key (:public-key props)
:unremovable? (-> props :unremovable? boolean)
:hide-contact? (-> props :hide-contact? boolean)
:pending? (-> props :pending? boolean)
:dapp? (:dapp? props)
:dapp-url (-> props :dapp-url :en)
:bot-url (:bot-url props)
:description (:description props)}])))
default-contacts))
existing-contacts (:contacts/contacts db)
contacts-to-add (select-keys new-contacts (set/difference (set (keys new-contacts))
(set (keys existing-contacts))))]
{:db (update db :contacts/contacts merge contacts-to-add)
:data-store/tx [(contacts-store/save-contacts-tx (vals contacts-to-add))]}))
(defn- group-chat-messages
[{:keys [db]}]
(reduce-kv (fn [fx chat-id {:keys [messages]}]
(models.message/group-messages chat-id (vals messages) fx))
{:db db}
(:chats db)))
(defn initialize-chats [{:keys [db
default-dapps
all-stored-chats
stored-unanswered-requests
get-stored-messages
get-stored-user-statuses
stored-unviewed-messages
stored-message-ids] :as cofx}]
(let [chat->message-id->request (reduce (fn [acc {:keys [chat-id message-id] :as request}]
(assoc-in acc [chat-id message-id] request))
{}
stored-unanswered-requests)
chats (reduce (fn [acc {:keys [chat-id] :as chat}]
(let [chat-messages (index-messages (get-stored-messages chat-id))
message-ids (keys chat-messages)
unviewed-ids (get stored-unviewed-messages chat-id)]
(assoc acc chat-id
(assoc chat
:unviewed-messages unviewed-ids
:requests (get chat->message-id->request chat-id)
:messages chat-messages
:message-statuses (get-stored-user-statuses chat-id message-ids)
:not-loaded-message-ids (set/difference (get stored-message-ids chat-id)
(set message-ids))))))
{}
all-stored-chats)]
(handlers-macro/merge-fx cofx
{:db (assoc db
:chats chats
:contacts/dapps default-dapps)}
(group-chat-messages)
(add-default-contacts)
(commands/index-commands commands/register))))
(defn process-pending-messages
"Change status of own messages which are still in `sending` status to `not-sent`
(If signal from status-go has not been received)"
[{:keys [db]}]
(let [me (:current-public-key db)
pending-statuses (->> (vals (:chats db))
(mapcat :message-statuses)
(mapcat (fn [[_ user-id->status]]
(filter (comp (partial = :sending) :status)
(get user-id->status me)))))
updated-statuses (map #(assoc % :status :not-sent) pending-statuses)]
{:data-store/tx [(user-statuses-store/save-statuses-tx updated-statuses)]
:db (reduce
(fn [acc {:keys [chat-id message-id status whisper-identity]}]
(assoc-in acc
[:chats chat-id :message-status message-id
whisper-identity :status]
status))
db
updated-statuses)}))

View File

@ -1,9 +1,8 @@
(ns status-im.models.contact (ns status-im.models.contact
(:require (:require [status-im.data-store.contacts :as contacts-store]
[status-im.utils.contacts :as utils.contacts]
[status-im.data-store.contacts :as contacts-store]
[status-im.transport.message.core :as transport] [status-im.transport.message.core :as transport]
[status-im.transport.message.v1.contact :as message.v1.contact] [status-im.transport.message.v1.contact :as message.v1.contact]
[status-im.utils.contacts :as utils.contacts]
[status-im.utils.handlers-macro :as handlers-macro])) [status-im.utils.handlers-macro :as handlers-macro]))
(defn can-add-to-contacts? [{:keys [pending? dapp?]}] (defn can-add-to-contacts? [{:keys [pending? dapp?]}]

View File

@ -0,0 +1,7 @@
(ns status-im.models.contacts)
(defn load-contacts
[{:keys [db all-contacts]}]
(let [contacts-list (map #(vector (:whisper-identity %) %) all-contacts)
contacts (into {} contacts-list)]
{:db (update db :contacts/contacts #(merge contacts %))}))

View File

@ -1,10 +1,9 @@
(ns status-im.models.mailserver (ns status-im.models.mailserver
(:require (:require [clojure.string :as string]
[clojure.string :as string] [status-im.data-store.mailservers :as data-store.mailservers]
[status-im.utils.handlers-macro :as handlers-macro]
[status-im.utils.ethereum.core :as ethereum]
[status-im.models.network :as models.network] [status-im.models.network :as models.network]
[status-im.data-store.mailservers :as data-store.mailservers])) [status-im.utils.ethereum.core :as ethereum]
[status-im.utils.handlers-macro :as handlers-macro]))
(def enode-address-regex #"enode://[a-zA-Z0-9]+\@\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b:(\d{1,5})") (def enode-address-regex #"enode://[a-zA-Z0-9]+\@\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b:(\d{1,5})")
(def enode-url-regex #"enode://[a-zA-Z0-9]+:(.+)\@\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b:(\d{1,5})") (def enode-url-regex #"enode://[a-zA-Z0-9]+:(.+)\@\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b:(\d{1,5})")

View File

@ -1,8 +1,8 @@
(ns status-im.models.network (ns status-im.models.network
(:require [clojure.string :as string] (:require [clojure.string :as string]
[status-im.ui.screens.accounts.utils :as accounts.utils]
[status-im.utils.ethereum.core :as ethereum] [status-im.utils.ethereum.core :as ethereum]
[status-im.utils.handlers-macro :as handlers-macro] [status-im.utils.handlers-macro :as handlers-macro]))
[status-im.ui.screens.accounts.utils :as accounts.utils]))
(def url-regex (def url-regex
#"https?://(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}(\.[a-z]{2,6})?\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)") #"https?://(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}(\.[a-z]{2,6})?\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)")

View File

@ -1,8 +1,11 @@
(ns status-im.protocol.models (ns status-im.models.protocol
(:require [status-im.constants :as constants] (:require [re-frame.core :as re-frame]
[status-im.utils.semaphores :as semaphores] [status-im.constants :as constants]
[status-im.transport.core :as transport]
[status-im.transport.inbox :as transport.inbox]
[status-im.utils.ethereum.core :as ethereum] [status-im.utils.ethereum.core :as ethereum]
[status-im.utils.handlers-macro :as handlers-macro])) [status-im.utils.handlers-macro :as handlers-macro]
[status-im.utils.semaphores :as semaphores]))
(defn update-sync-state (defn update-sync-state
[{:keys [sync-state sync-data] :as db} error sync] [{:keys [sync-state sync-data] :as db} error sync]
@ -27,7 +30,7 @@
(defn check-sync-state (defn check-sync-state
[{{:keys [web3] :as db} :db :as cofx}] [{{:keys [web3] :as db} :db :as cofx}]
(if (:account/account db) (if (:account/account db)
{::web3-get-syncing web3 {:protocol/web3-get-syncing web3
:dispatch-later [{:ms 10000 :dispatch [:check-sync-state]}]} :dispatch-later [{:ms 10000 :dispatch [:check-sync-state]}]}
(semaphores/free :check-sync-state? cofx))) (semaphores/free :check-sync-state? cofx)))
@ -38,3 +41,17 @@
(handlers-macro/merge-fx cofx (handlers-macro/merge-fx cofx
{:dispatch [:check-sync-state]} {:dispatch [:check-sync-state]}
(semaphores/lock :check-sync-state?)))) (semaphores/lock :check-sync-state?))))
(defn initialize-protocol
[address {:data-store/keys [transport mailservers] :keys [db web3] :as cofx}]
(let [network (get-in db [:account/account :network])
network-id (str (get-in db [:account/account :networks network :config :NetworkId]))]
(handlers-macro/merge-fx cofx
{:db (assoc db
:rpc-url constants/ethereum-rpc-url
:transport/chats transport)
:protocol/assert-correct-network {:web3 web3
:network-id network-id}}
(start-check-sync-state)
(transport.inbox/initialize-offline-inbox mailservers)
(transport/init-whisper address))))

View File

@ -1,8 +1,8 @@
(ns status-im.models.transactions (ns status-im.models.transactions
(:require [clojure.set :as set] (:require [clojure.set :as set]
[status-im.utils.datetime :as time] [status-im.utils.datetime :as time]
[status-im.utils.ethereum.tokens :as tokens]
[status-im.utils.ethereum.core :as ethereum] [status-im.utils.ethereum.core :as ethereum]
[status-im.utils.ethereum.tokens :as tokens]
[status-im.utils.handlers-macro :as handlers-macro] [status-im.utils.handlers-macro :as handlers-macro]
[status-im.utils.semaphores :as semaphores] [status-im.utils.semaphores :as semaphores]
[taoensso.timbre :as log])) [taoensso.timbre :as log]))
@ -55,10 +55,10 @@
chat-transactions chat-transactions
(wallet-transactions-set db)))) (wallet-transactions-set db))))
; Find missing chat transactions (defn load-missing-chat-transactions
; and store them at [:wallet :chat-transactions] "Find missing chat transactions and store them at [:wallet :chat-transactions]
; to be used later by have-missing-chat-transactions? on every sync request to be used later by have-missing-chat-transactions? on every sync request"
(defn load-missing-chat-transactions [{:keys [db] :as cofx}] [{:keys [db] :as cofx}]
(when (nil? (get-in db [:wallet :chat-transactions])) (when (nil? (get-in db [:wallet :chat-transactions]))
{:db (assoc-in db {:db (assoc-in db
[:wallet :chat-transactions] [:wallet :chat-transactions]

View File

@ -1,12 +1,13 @@
(ns status-im.models.wallet (ns status-im.models.wallet
(:require [status-im.utils.money :as money] (:require [clojure.set :as set]
[status-im.i18n :as i18n]
[status-im.utils.ethereum.core :as ethereum]
[status-im.constants :as constants] [status-im.constants :as constants]
[status-im.utils.ethereum.tokens :as tokens] [status-im.i18n :as i18n]
[status-im.transport.utils :as transport.utils]
[status-im.ui.screens.navigation :as navigation] [status-im.ui.screens.navigation :as navigation]
[status-im.utils.ethereum.core :as ethereum]
[status-im.utils.ethereum.tokens :as tokens]
[status-im.utils.hex :as utils.hex] [status-im.utils.hex :as utils.hex]
[status-im.transport.utils :as transport.utils])) [status-im.utils.money :as money]))
(def min-gas-price-wei (money/bignumber 1)) (def min-gas-price-wei (money/bignumber 1))
@ -172,3 +173,39 @@
(cond-> transaction (cond-> transaction
(= method constants/web3-personal-sign) (= method constants/web3-personal-sign)
(update :data transport.utils/to-utf8))) (update :data transport.utils/to-utf8)))
(defn clear-error-message [db error-type]
(update-in db [:wallet :errors] dissoc error-type))
(defn tokens-symbols [v chain]
(set/difference (set v) (set (map :symbol (tokens/nfts-for chain)))))
(defn update-wallet
[{{:keys [web3 network network-status] {:keys [address settings]} :account/account :as db} :db}]
(let [network (get-in db [:account/account :networks network])
chain (ethereum/network->chain-keyword network)
mainnet? (= :mainnet chain)
assets (get-in settings [:wallet :visible-tokens chain])
tokens (tokens-symbols (get-in settings [:wallet :visible-tokens chain]) chain)
currency-id (or (get-in settings [:wallet :currency]) :usd)
currency (get constants/currencies currency-id)]
(when (not= network-status :offline)
{:get-balance {:web3 web3
:account-id address
:success-event :update-balance-success
:error-event :update-balance-fail}
:get-tokens-balance {:web3 web3
:account-id address
:symbols assets
:chain chain
:success-event :update-token-balance-success
:error-event :update-token-balance-fail}
:get-prices {:from (if mainnet? (conj tokens "ETH") ["ETH"])
:to [(:code currency)]
:success-event :update-prices-success
:error-event :update-prices-fail}
:db (-> db
(clear-error-message :prices-update)
(clear-error-message :balance-update)
(assoc-in [:wallet :balance-loading?] true)
(assoc :prices-loading? true))})))

View File

@ -1,30 +1,21 @@
(ns status-im.protocol.handlers (ns status-im.protocol.handlers
(:require [cljs.core.async :as async] (:require [re-frame.core :as re-frame]
[re-frame.core :as re-frame] [status-im.models.protocol :as models]
[status-im.constants :as constants]
[status-im.native-module.core :as status]
[status-im.utils.utils :as utils]
[status-im.utils.datetime :as datetime]
[status-im.utils.handlers :as handlers]
[status-im.utils.handlers-macro :as handlers-macro]
[status-im.utils.web3-provider :as web3-provider]
[status-im.transport.core :as transport]
[status-im.transport.inbox :as transport.inbox]
[status-im.utils.ethereum.core :as ethereum] [status-im.utils.ethereum.core :as ethereum]
[status-im.protocol.models :as models])) [status-im.utils.handlers :as handlers]
[status-im.utils.utils :as utils]
[status-im.utils.web3-provider :as web3-provider]))
;;;; COFX ;;;; COFX
(re-frame/reg-cofx (re-frame/reg-cofx
::get-web3 :protocol/get-web3
(fn [coeffects _] (fn [cofx _]
(let [web3 (web3-provider/make-internal-web3) (let [web3 (web3-provider/make-internal-web3)]
address (get-in coeffects [:db :account/account :address])] (assoc cofx :web3 web3))))
(set! (.-defaultAccount (.-eth web3))
(ethereum/normalized-address address))
(assoc coeffects :web3 web3))))
;;; FX
(re-frame/reg-fx (re-frame/reg-fx
::web3-get-syncing :protocol/web3-get-syncing
(fn [web3] (fn [web3]
(when web3 (when web3
(.getSyncing (.getSyncing
@ -32,12 +23,16 @@
(fn [error sync] (fn [error sync]
(re-frame/dispatch [:update-sync-state error sync])))))) (re-frame/dispatch [:update-sync-state error sync]))))))
(defn- assert-correct-network (re-frame/reg-fx
[{:keys [db]}] :protocol/set-default-account
;; Assure that node was started correctly (fn [[web3 address]]
(let [{:keys [web3]} db] (set! (.-defaultAccount (.-eth web3))
(let [network (get-in db [:account/account :network]) (ethereum/normalized-address address))))
network-id (str (get-in db [:account/account :networks network :config :NetworkId]))]
(re-frame/reg-fx
:protocol/assert-correct-network
(fn [{:keys [web3 network-id]}]
;; ensure that node was started correctly
(when (and network-id web3) ; necessary because of the unit tests (when (and network-id web3) ; necessary because of the unit tests
(.getNetwork (.-version web3) (.getNetwork (.-version web3)
(fn [error fetched-network-id] (fn [error fetched-network-id]
@ -46,26 +41,7 @@
(utils/show-popup (utils/show-popup
"Ethereum node started incorrectly" "Ethereum node started incorrectly"
"Ethereum node was started with incorrect configuration, application will be stopped to recover from that condition." "Ethereum node was started with incorrect configuration, application will be stopped to recover from that condition."
#(re-frame/dispatch [:close-application]))))))))) #(re-frame/dispatch [:close-application]))))))))
(defn initialize-protocol
[{:data-store/keys [transport mailservers] :keys [db web3] :as cofx} [current-account-id ethereum-rpc-url]]
(handlers-macro/merge-fx cofx
{:db (assoc db
:web3 web3
:rpc-url (or ethereum-rpc-url constants/ethereum-rpc-url)
:transport/chats transport)}
(assert-correct-network)
(transport.inbox/initialize-offline-inbox mailservers)
(transport/init-whisper current-account-id)))
;;; INITIALIZE PROTOCOL
(handlers/register-handler-fx
:initialize-protocol
[re-frame/trim-v
(re-frame/inject-cofx ::get-web3)
(re-frame/inject-cofx :data-store/get-all-mailservers)
(re-frame/inject-cofx :data-store/transport)]
initialize-protocol)
;;; NODE SYNC STATE ;;; NODE SYNC STATE

View File

@ -1,12 +1,11 @@
(ns ^{:doc "Contact request and update API"} (ns ^{:doc "Contact request and update API"}
status-im.transport.message.v1.contact status-im.transport.message.v1.contact
(:require [re-frame.core :as re-frame] (:require [re-frame.core :as re-frame]
[status-im.data-store.transport :as transport-store]
[status-im.transport.message.core :as message] [status-im.transport.message.core :as message]
[status-im.transport.message.v1.protocol :as protocol] [status-im.transport.message.v1.protocol :as protocol]
[status-im.transport.utils :as transport.utils] [status-im.transport.utils :as transport.utils]
[status-im.utils.handlers-macro :as handlers-macro] [status-im.utils.handlers-macro :as handlers-macro]))
[status-im.utils.handlers :as handlers]
[status-im.data-store.transport :as transport-store]))
(defrecord ContactRequest [name profile-image address fcm-token] (defrecord ContactRequest [name profile-image address fcm-token]
message/StatusMessage message/StatusMessage

View File

@ -1,9 +1,9 @@
(ns status-im.ui.screens.accounts.events (ns status-im.ui.screens.accounts.events
(:require [re-frame.core :as re-frame] (:require [re-frame.core :as re-frame]
[status-im.utils.handlers :as handlers]
status-im.ui.screens.accounts.create.navigation status-im.ui.screens.accounts.create.navigation
[status-im.ui.screens.accounts.models :as models]
[status-im.ui.screens.accounts.utils :as accounts.utils] [status-im.ui.screens.accounts.utils :as accounts.utils]
[status-im.ui.screens.accounts.models :as models])) [status-im.utils.handlers :as handlers]))
;;;; COFX ;;;; COFX
@ -36,11 +36,6 @@
(fn [cofx [_ result password]] (fn [cofx [_ result password]]
(models/on-account-created result password false cofx))) (models/on-account-created result password false cofx)))
(handlers/register-handler-fx
:send-account-update-if-needed
(fn [cofx _]
(models/send-account-update-if-needed cofx)))
(handlers/register-handler-fx (handlers/register-handler-fx
:account-set-name :account-set-name
(fn [cofx _] (fn [cofx _]
@ -51,11 +46,6 @@
(fn [cofx [_ input-key text]] (fn [cofx [_ input-key text]]
(models/account-set-input-text input-key text cofx))) (models/account-set-input-text input-key text cofx)))
(handlers/register-handler-fx
:update-sign-in-time
(fn [{db :db now :now :as cofx} _]
(accounts.utils/account-update {:last-sign-in now} cofx)))
(handlers/register-handler-fx (handlers/register-handler-fx
:update-mainnet-warning-shown :update-mainnet-warning-shown
(fn [cofx _] (fn [cofx _]

View File

@ -1,26 +1,24 @@
(ns status-im.ui.screens.accounts.models (ns status-im.ui.screens.accounts.models
(:require [re-frame.core :as re-frame] (:require [clojure.string :as str]
[taoensso.timbre :as log] [re-frame.core :as re-frame]
[status-im.native-module.core :as status]
[status-im.utils.types :as types]
[status-im.utils.identicon :as identicon]
[clojure.string :as str]
[status-im.i18n :as i18n]
[status-im.utils.config :as config]
[status-im.utils.utils :as utils]
[status-im.utils.datetime :as time]
[status-im.utils.handlers-macro :as handlers-macro]
[status-im.ui.screens.accounts.statuses :as statuses]
[status-im.utils.signing-phrase.core :as signing-phrase]
[status-im.utils.gfycat.core :as gfycat]
[status-im.utils.hex :as utils.hex]
[status-im.constants :as constants] [status-im.constants :as constants]
status-im.ui.screens.accounts.create.navigation
[status-im.ui.screens.accounts.utils :as accounts.utils]
[status-im.data-store.accounts :as accounts-store] [status-im.data-store.accounts :as accounts-store]
[status-im.i18n :as i18n]
[status-im.native-module.core :as status]
[status-im.ui.screens.accounts.statuses :as statuses]
[status-im.ui.screens.accounts.utils :as accounts.utils]
[status-im.ui.screens.accounts.login.models :as login.models] [status-im.ui.screens.accounts.login.models :as login.models]
[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])) [status-im.ui.screens.wallet.settings.models :as wallet.settings.models]
[status-im.utils.config :as config]
[status-im.utils.gfycat.core :as gfycat]
[status-im.utils.handlers-macro :as handlers-macro]
[status-im.utils.hex :as utils.hex]
[status-im.utils.identicon :as identicon]
[status-im.utils.signing-phrase.core :as signing-phrase]
[status-im.utils.types :as types]
[status-im.utils.utils :as utils]
[taoensso.timbre :as log]))
;;;; COFX ;;;; COFX
@ -88,15 +86,6 @@
:data-store/base-tx [{:transaction (accounts-store/save-account-tx new-account) :data-store/base-tx [{:transaction (accounts-store/save-account-tx new-account)
:success-event success-event}]}))) :success-event success-event}]})))
(defn send-account-update-if-needed [{:keys [db now] :as cofx}]
(let [{:keys [last-updated]} (:account/account db)
needs-update? (> (- now last-updated) time/week)]
(log/info "Need to send account-update: " needs-update?)
(when needs-update?
;; TODO(janherich): this is very strange and misleading, need to figure out why it'd necessary to update
;; account with network update when last update was more then week ago
(accounts.utils/account-update nil cofx))))
(defn account-set-name [{{:accounts/keys [create] :as db} :db :as cofx}] (defn account-set-name [{{:accounts/keys [create] :as db} :db :as cofx}]
(handlers-macro/merge-fx cofx (handlers-macro/merge-fx cofx
{:db (assoc db :accounts/create {:show-welcome? true}) {:db (assoc db :accounts/create {:show-welcome? true})

View File

@ -1,21 +1,19 @@
(ns status-im.ui.screens.browser.events (ns status-im.ui.screens.browser.events
(:require status-im.ui.screens.browser.navigation (:require [re-frame.core :as re-frame]
[status-im.utils.handlers :as handlers]
[re-frame.core :as re-frame]
[status-im.utils.random :as random]
[status-im.ui.components.list-selection :as list-selection]
[status-im.utils.universal-links.core :as utils.universal-links]
[status-im.data-store.browser :as browser-store]
[status-im.utils.http :as http]
[status-im.models.browser :as model]
[status-im.utils.platform :as platform]
[status-im.constants :as constants] [status-im.constants :as constants]
[status-im.data-store.browser :as browser-store]
[status-im.models.browser :as model]
[status-im.native-module.core :as status] [status-im.native-module.core :as status]
[taoensso.timbre :as log] [status-im.ui.components.list-selection :as list-selection]
status-im.ui.screens.browser.navigation
[status-im.utils.handlers :as handlers]
[status-im.utils.handlers-macro :as handlers-macro] [status-im.utils.handlers-macro :as handlers-macro]
[status-im.utils.http :as http]
[status-im.utils.platform :as platform]
[status-im.utils.random :as random]
[status-im.utils.types :as types] [status-im.utils.types :as types]
[status-im.utils.handlers-macro :as handlers-macro] [status-im.utils.universal-links.core :as utils.universal-links]
[status-im.constants :as constants])) [taoensso.timbre :as log]))
(re-frame/reg-fx (re-frame/reg-fx
:browse :browse
@ -41,20 +39,6 @@
(fn [[message webview]] (fn [[message webview]]
(.sendToBridge webview (types/clj->json message)))) (.sendToBridge webview (types/clj->json message))))
(handlers/register-handler-fx
:initialize-browsers
[(re-frame/inject-cofx :data-store/all-browsers)]
(fn [{:keys [db all-stored-browsers]} _]
(let [browsers (into {} (map #(vector (:browser-id %) %) all-stored-browsers))]
{:db (assoc db :browser/browsers browsers)})))
(handlers/register-handler-fx
:initialize-dapp-permissions
[(re-frame/inject-cofx :data-store/all-dapp-permissions)]
(fn [{:keys [db all-dapp-permissions]} _]
(let [dapp-permissions (into {} (map #(vector (:dapp %) %) all-dapp-permissions))]
{:db (assoc db :dapps/permissions dapp-permissions)})))
(handlers/register-handler-fx (handlers/register-handler-fx
:browse-link-from-message :browse-link-from-message
(fn [_ [_ link]] (fn [_ [_ link]]

View File

@ -1,15 +1,15 @@
(ns status-im.ui.screens.contacts.events (ns status-im.ui.screens.contacts.events
(:require [re-frame.core :as re-frame] (:require [re-frame.core :as re-frame]
[status-im.chat.events :as chat.events]
[status-im.i18n :as i18n] [status-im.i18n :as i18n]
[status-im.models.contact :as models.contact]
[status-im.ui.screens.add-new.new-chat.db :as new-chat.db] [status-im.ui.screens.add-new.new-chat.db :as new-chat.db]
[status-im.ui.screens.browser.default-dapps :as default-dapps] [status-im.ui.screens.browser.default-dapps :as default-dapps]
[status-im.ui.screens.navigation :as navigation]
[status-im.utils.handlers :as handlers] [status-im.utils.handlers :as handlers]
[status-im.utils.handlers-macro :as handlers-macro] [status-im.utils.handlers-macro :as handlers-macro]
[status-im.utils.js-resources :as js-res] [status-im.utils.js-resources :as js-res]
[status-im.chat.events :as chat.events] [status-im.utils.utils :as utils]))
[status-im.ui.screens.navigation :as navigation]
[status-im.utils.utils :as utils]
[status-im.models.contact :as models.contact]))
(defn add-contact-and-open-chat [whisper-id cofx] (defn add-contact-and-open-chat [whisper-id cofx]
(handlers-macro/merge-fx cofx (handlers-macro/merge-fx cofx
@ -28,15 +28,6 @@
(assoc coeffects :default-dapps default-dapps/all))) (assoc coeffects :default-dapps default-dapps/all)))
;;;; Handlers ;;;; Handlers
(handlers/register-handler-fx
:load-contacts
[(re-frame/inject-cofx :data-store/get-all-contacts)]
(fn [{:keys [db all-contacts]} _]
(let [contacts-list (map #(vector (:whisper-identity %) %) all-contacts)
contacts (into {} contacts-list)]
{:db (update db :contacts/contacts #(merge contacts %))})))
(handlers/register-handler-fx (handlers/register-handler-fx
:add-contact :add-contact
[(re-frame/inject-cofx :random-id)] [(re-frame/inject-cofx :random-id)]

View File

@ -1,8 +1,8 @@
(ns status-im.ui.screens.currency-settings.events (ns status-im.ui.screens.currency-settings.events
(:require [status-im.ui.screens.accounts.models :as accounts.models] (:require [status-im.models.wallet :as wallet]
[status-im.ui.screens.accounts.models :as accounts.models]
[status-im.utils.handlers :as handlers] [status-im.utils.handlers :as handlers]
[status-im.utils.handlers-macro :as handlers-macro] [status-im.utils.handlers-macro :as handlers-macro]))
[status-im.ui.screens.wallet.events :as wallet.events]))
(handlers/register-handler-fx (handlers/register-handler-fx
:wallet.settings/set-currency :wallet.settings/set-currency
@ -11,4 +11,4 @@
new-settings (assoc-in settings [:wallet :currency] currency)] new-settings (assoc-in settings [:wallet :currency] currency)]
(handlers-macro/merge-fx cofx (handlers-macro/merge-fx cofx
(accounts.models/update-settings new-settings) (accounts.models/update-settings new-settings)
(wallet.events/update-wallet))))) (wallet/update-wallet)))))

View File

@ -1,13 +1,17 @@
(ns status-im.ui.screens.events (ns status-im.ui.screens.events
(:require status-im.chat.events (:require status-im.chat.events
[status-im.models.chat :as chat]
status-im.network.events status-im.network.events
[status-im.transport.handlers :as transport.handlers] [status-im.transport.handlers :as transport.handlers]
status-im.protocol.handlers status-im.protocol.handlers
[status-im.models.protocol :as models.protocol]
[status-im.models.account :as models.account]
[status-im.ui.screens.accounts.models :as accounts.models] [status-im.ui.screens.accounts.models :as accounts.models]
status-im.ui.screens.accounts.login.events status-im.ui.screens.accounts.login.events
[status-im.ui.screens.accounts.login.models :as login] [status-im.ui.screens.accounts.login.models :as login]
status-im.ui.screens.accounts.recover.events status-im.ui.screens.accounts.recover.events
[status-im.ui.screens.contacts.events :as contacts] [status-im.ui.screens.contacts.events :as contacts]
[status-im.models.contacts :as models.contacts]
status-im.ui.screens.add-new.new-chat.events status-im.ui.screens.add-new.new-chat.events
status-im.ui.screens.group.chat-settings.events status-im.ui.screens.group.chat-settings.events
status-im.ui.screens.group.events status-im.ui.screens.group.events
@ -20,15 +24,18 @@
status-im.ui.screens.profile.events status-im.ui.screens.profile.events
status-im.ui.screens.qr-scanner.events status-im.ui.screens.qr-scanner.events
status-im.ui.screens.wallet.events status-im.ui.screens.wallet.events
[status-im.models.wallet :as models.wallet]
status-im.ui.screens.wallet.collectibles.events status-im.ui.screens.wallet.collectibles.events
status-im.ui.screens.wallet.send.events status-im.ui.screens.wallet.send.events
status-im.ui.screens.wallet.settings.events status-im.ui.screens.wallet.settings.events
status-im.ui.screens.wallet.transactions.events status-im.ui.screens.wallet.transactions.events
[status-im.models.transactions :as transactions]
status-im.ui.screens.wallet.choose-recipient.events status-im.ui.screens.wallet.choose-recipient.events
status-im.ui.screens.wallet.collectibles.cryptokitties.events status-im.ui.screens.wallet.collectibles.cryptokitties.events
status-im.ui.screens.wallet.collectibles.cryptostrikers.events status-im.ui.screens.wallet.collectibles.cryptostrikers.events
status-im.ui.screens.wallet.collectibles.etheremon.events status-im.ui.screens.wallet.collectibles.etheremon.events
status-im.ui.screens.browser.events status-im.ui.screens.browser.events
[status-im.models.browser :as browser]
status-im.ui.screens.offline-messaging-settings.events status-im.ui.screens.offline-messaging-settings.events
status-im.ui.screens.privacy-policy.events status-im.ui.screens.privacy-policy.events
status-im.ui.screens.bootnodes-settings.events status-im.ui.screens.bootnodes-settings.events
@ -97,6 +104,14 @@
(doseq [call calls] (doseq [call calls]
(http-get call)))) (http-get call))))
(re-frame/reg-fx
:fetch-web3-node-version
(fn [web3 _]
(.. web3 -version (getNode (fn [err resp]
(when-not err
(re-frame/dispatch [:fetch-web3-node-version-callback resp])))))
nil))
;; Try to decrypt the database, move on if successful otherwise go back to ;; Try to decrypt the database, move on if successful otherwise go back to
;; initial state ;; initial state
(re-frame/reg-fx (re-frame/reg-fx
@ -149,7 +164,8 @@
(re-frame/reg-fx (re-frame/reg-fx
::get-fcm-token-fx ::get-fcm-token-fx
(fn [_] (fn [_]
(notifications/get-fcm-token))) (when platform/mobile?
(notifications/get-fcm-token))))
(re-frame/reg-fx (re-frame/reg-fx
:show-error :show-error
@ -294,18 +310,17 @@
(navigation/navigate-to-clean nil) (navigation/navigate-to-clean nil)
(transport/stop-whisper))))) (transport/stop-whisper)))))
(handlers/register-handler-db (defn initialize-account-db [address {:keys [db web3]}]
:initialize-account-db (let [{:keys [accounts/accounts accounts/create contacts/contacts networks/networks
(fn [{:keys [accounts/accounts accounts/create contacts/contacts networks/networks
network network-status peers-count peers-summary view-id navigation-stack network network-status peers-count peers-summary view-id navigation-stack
status-module-initialized? status-node-started? device-UUID status-module-initialized? status-node-started? device-UUID
push-notifications/initial? semaphores] push-notifications/initial? semaphores]
:or [network (get app-db :network)]} [_ address]] :or {network (get app-db :network)}} db
(let [console-contact (get contacts constants/console-chat-id) console-contact (get contacts constants/console-chat-id)
current-account (accounts address) current-account (accounts address)
account-network-id (get current-account :network network) account-network-id (get current-account :network network)
account-network (get-in current-account [:networks account-network-id])] account-network (get-in current-account [:networks account-network-id])]
(cond-> (assoc app-db {:db (cond-> (assoc app-db
:current-public-key (:public-key current-account) :current-public-key (:public-key current-account)
:view-id view-id :view-id view-id
:navigation-stack navigation-stack :navigation-stack navigation-stack
@ -321,29 +336,45 @@
:peers-summary peers-summary :peers-summary peers-summary
:peers-count peers-count :peers-count peers-count
:device-UUID device-UUID :device-UUID device-UUID
:semaphores semaphores) :semaphores semaphores
:web3 web3)
console-contact console-contact
(assoc :contacts/contacts {constants/console-chat-id console-contact}))))) (assoc :contacts/contacts {constants/console-chat-id console-contact}))}))
(handlers/register-handler-fx (handlers/register-handler-fx
:initialize-account :initialize-account
(fn [cofx [_ address events-after]] [(re-frame/inject-cofx :protocol/get-web3)
{:dispatch-n (cond-> [[:initialize-account-db address] (re-frame/inject-cofx :get-default-contacts)
[:initialize-protocol address] (re-frame/inject-cofx :get-default-dapps)
[:fetch-web3-node-version] (re-frame/inject-cofx :data-store/all-chats)
[:start-check-sync-state] (re-frame/inject-cofx :data-store/get-messages)
[:load-contacts] (re-frame/inject-cofx :data-store/get-user-statuses)
[:initialize-chats] (re-frame/inject-cofx :data-store/unviewed-messages)
[:initialize-browsers] (re-frame/inject-cofx :data-store/message-ids)
[:initialize-dapp-permissions] (re-frame/inject-cofx :data-store/get-unanswered-requests)
[:send-account-update-if-needed] (re-frame/inject-cofx :data-store/get-local-storage-data)
[:process-pending-messages] (re-frame/inject-cofx :data-store/get-all-contacts)
[:update-wallet] (re-frame/inject-cofx :data-store/get-all-mailservers)
[:update-transactions] (re-frame/inject-cofx :data-store/transport)
(when platform/mobile? [:get-fcm-token]) (re-frame/inject-cofx :data-store/all-browsers)
[:start-wallet-transactions-sync] (re-frame/inject-cofx :data-store/all-dapp-permissions)]
[:update-sign-in-time]] (fn [{:keys [web3] :as cofx} [_ address events-after]]
(seq events-after) (into events-after))})) (handlers-macro/merge-fx cofx
{:protocol/set-default-account [web3 address]
:fetch-web3-node-version web3
::get-fcm-token-fx nil
:dispatch-n (or events-after [])}
(initialize-account-db address)
(models.protocol/initialize-protocol address)
(models.contacts/load-contacts)
(chat/initialize-chats)
(chat/process-pending-messages)
(browser/initialize-browsers)
(browser/initialize-dapp-permissions)
(models.wallet/update-wallet)
(transactions/run-update)
(transactions/start-sync)
(models.account/update-sign-in-time))))
(handlers/register-handler-fx (handlers/register-handler-fx
:initialize-geth :initialize-geth
@ -359,19 +390,6 @@
(when-let [git-commit (nth (re-find #"-([0-9a-f]{7,})/" resp) 1)] (when-let [git-commit (nth (re-find #"-([0-9a-f]{7,})/" resp) 1)]
{:db (assoc db :web3-node-version git-commit)}))) {:db (assoc db :web3-node-version git-commit)})))
(handlers/register-handler-fx
:fetch-web3-node-version
(fn [{{:keys [web3] :as db} :db} _]
(.. web3 -version (getNode (fn [err resp]
(when-not err
(re-frame/dispatch [:fetch-web3-node-version-callback resp])))))
nil))
(handlers/register-handler-fx
:get-fcm-token
(fn [_ _]
{::get-fcm-token-fx nil}))
(handlers/register-handler-fx (handlers/register-handler-fx
:discovery/summary :discovery/summary
(fn [{:keys [db] :as cofx} [_ peers-summary]] (fn [{:keys [db] :as cofx} [_ peers-summary]]

View File

@ -1,21 +1,17 @@
(ns status-im.ui.screens.wallet.events (ns status-im.ui.screens.wallet.events
(:require [clojure.set :as set] (:require [re-frame.core :as re-frame]
[re-frame.core :as re-frame :refer [dispatch reg-fx]] [status-im.models.transactions :as wallet.transactions]
[status-im.i18n :as i18n] [status-im.models.wallet :as models]
[status-im.ui.screens.wallet.navigation] [status-im.ui.screens.navigation :as navigation]
status-im.ui.screens.wallet.navigation
[status-im.utils.ethereum.core :as ethereum] [status-im.utils.ethereum.core :as ethereum]
[status-im.utils.ethereum.erc20 :as erc20] [status-im.utils.ethereum.erc20 :as erc20]
[status-im.utils.ethereum.tokens :as tokens] [status-im.utils.ethereum.tokens :as tokens]
[status-im.utils.handlers :as handlers] [status-im.utils.handlers :as handlers]
[status-im.utils.money :as money]
[status-im.utils.prices :as prices] [status-im.utils.prices :as prices]
[status-im.utils.transactions :as transactions] [status-im.utils.transactions :as transactions]
[status-im.models.wallet :as models.wallet] [taoensso.timbre :as log]))
[taoensso.timbre :as log]
status-im.ui.screens.wallet.request.events
[status-im.constants :as constants]
[status-im.ui.screens.navigation :as navigation]
[status-im.utils.money :as money]
[status-im.models.transactions :as wallet.transactions]))
(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)
@ -43,12 +39,9 @@
(defn assoc-error-message [db error-type err] (defn assoc-error-message [db error-type err]
(assoc-in db [:wallet :errors error-type] (or err :unknown-error))) (assoc-in db [:wallet :errors error-type] (or err :unknown-error)))
(defn clear-error-message [db error-type]
(update-in db [:wallet :errors] dissoc error-type))
;; FX ;; FX
(reg-fx (re-frame/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 {:web3 web3 (get-balance {:web3 web3
@ -56,7 +49,7 @@
:on-success #(re-frame/dispatch [success-event %]) :on-success #(re-frame/dispatch [success-event %])
:on-error #(re-frame/dispatch [error-event %])}))) :on-error #(re-frame/dispatch [error-event %])})))
(reg-fx (re-frame/reg-fx
:get-tokens-balance :get-tokens-balance
(fn [{:keys [web3 symbols chain account-id success-event error-event]}] (fn [{:keys [web3 symbols chain account-id success-event error-event]}]
(doseq [symbol symbols] (doseq [symbol symbols]
@ -67,7 +60,7 @@
:on-success #(re-frame/dispatch [success-event symbol %]) :on-success #(re-frame/dispatch [success-event symbol %])
:on-error #(re-frame/dispatch [error-event symbol %])}))))) :on-error #(re-frame/dispatch [error-event symbol %])})))))
(reg-fx (re-frame/reg-fx
:get-transactions :get-transactions
(fn [{:keys [web3 chain account-id token-addresses success-event error-event]}] (fn [{:keys [web3 chain account-id token-addresses success-event error-event]}]
(transactions/get-transactions chain (transactions/get-transactions chain
@ -83,7 +76,7 @@
#(re-frame/dispatch [success-event % account-id]))))) #(re-frame/dispatch [success-event % account-id])))))
;; 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
(reg-fx (re-frame/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 from (prices/get-prices from
@ -91,53 +84,21 @@
#(re-frame/dispatch [success-event %]) #(re-frame/dispatch [success-event %])
#(re-frame/dispatch [error-event %])))) #(re-frame/dispatch [error-event %]))))
(reg-fx (re-frame/reg-fx
:update-gas-price :update-gas-price
(fn [{:keys [web3 success-event edit?]}] (fn [{:keys [web3 success-event edit?]}]
(ethereum/gas-price web3 #(re-frame/dispatch [success-event %2 edit?])))) (ethereum/gas-price web3 #(re-frame/dispatch [success-event %2 edit?]))))
(reg-fx (re-frame/reg-fx
:update-estimated-gas :update-estimated-gas
(fn [{:keys [web3 obj success-event]}] (fn [{:keys [web3 obj success-event]}]
(ethereum/estimate-gas-web3 web3 (clj->js obj) #(re-frame/dispatch [success-event %2])))) (ethereum/estimate-gas-web3 web3 (clj->js obj) #(re-frame/dispatch [success-event %2]))))
(defn tokens-symbols [v chain]
(set/difference (set v) (set (map :symbol (tokens/nfts-for chain)))))
(defn update-wallet [{{:keys [web3 network network-status] {:keys [address settings]} :account/account :as db} :db}]
(let [network (get-in db [:account/account :networks network])
chain (ethereum/network->chain-keyword network)
mainnet? (= :mainnet chain)
assets (get-in settings [:wallet :visible-tokens chain])
tokens (tokens-symbols (get-in settings [:wallet :visible-tokens chain]) chain)
currency-id (or (get-in settings [:wallet :currency]) :usd)
currency (get constants/currencies currency-id)]
(when (not= network-status :offline)
{:get-balance {:web3 web3
:account-id address
:success-event :update-balance-success
:error-event :update-balance-fail}
:get-tokens-balance {:web3 web3
:account-id address
:symbols assets
:chain chain
:success-event :update-token-balance-success
:error-event :update-token-balance-fail}
:get-prices {:from (if mainnet? (conj tokens "ETH") ["ETH"])
:to [(:code currency)]
:success-event :update-prices-success
:error-event :update-prices-fail}
:db (-> db
(clear-error-message :prices-update)
(clear-error-message :balance-update)
(assoc-in [:wallet :balance-loading?] true)
(assoc :prices-loading? true))})))
;; Handlers ;; Handlers
(handlers/register-handler-fx (handlers/register-handler-fx
:update-wallet :update-wallet
(fn [cofx _] (fn [cofx _]
(update-wallet cofx))) (models/update-wallet cofx)))
(handlers/register-handler-fx (handlers/register-handler-fx
:update-transactions :update-transactions
@ -257,7 +218,7 @@
:wallet/update-gas-price-success :wallet/update-gas-price-success
(fn [db [_ price edit?]] (fn [db [_ price edit?]]
(if edit? (if edit?
(:db (models.wallet/edit-value (:db (models/edit-value
:gas-price :gas-price
(money/to-fixed (money/to-fixed
(money/wei-> :gwei price)) (money/wei-> :gwei price))

View File

@ -1,7 +1,13 @@
(ns status-im.ui.screens.wallet.send.events (ns status-im.ui.screens.wallet.send.events
(:require [re-frame.core :as re-frame] (:require [re-frame.core :as re-frame]
[status-im.chat.commands.sending :as commands-sending]
[status-im.chat.models.message :as models.message]
[status-im.constants :as constants]
[status-im.i18n :as i18n] [status-im.i18n :as i18n]
[status-im.models.transactions :as wallet.transactions]
[status-im.models.wallet :as models.wallet]
[status-im.native-module.core :as status] [status-im.native-module.core :as status]
[status-im.ui.screens.navigation :as navigation]
[status-im.ui.screens.wallet.db :as wallet.db] [status-im.ui.screens.wallet.db :as wallet.db]
[status-im.utils.ethereum.core :as ethereum] [status-im.utils.ethereum.core :as ethereum]
[status-im.utils.ethereum.erc20 :as erc20] [status-im.utils.ethereum.erc20 :as erc20]
@ -11,13 +17,7 @@
[status-im.utils.money :as money] [status-im.utils.money :as money]
[status-im.utils.security :as security] [status-im.utils.security :as security]
[status-im.utils.types :as types] [status-im.utils.types :as types]
[status-im.utils.utils :as utils] [status-im.utils.utils :as utils]))
[status-im.models.wallet :as models.wallet]
[status-im.chat.models.message :as models.message]
[status-im.chat.commands.sending :as commands-sending]
[status-im.constants :as constants]
[status-im.ui.screens.navigation :as navigation]
[status-im.models.transactions :as wallet.transactions]))
;;;; FX ;;;; FX
@ -269,8 +269,3 @@
:sync-wallet-transactions :sync-wallet-transactions
(fn [cofx _] (fn [cofx _]
(wallet.transactions/sync cofx))) (wallet.transactions/sync cofx)))
(handlers/register-handler-fx
:start-wallet-transactions-sync
(fn [cofx _]
(wallet.transactions/start-sync cofx)))

View File

@ -1,5 +1,6 @@
(ns status-im.utils.handlers-macro (ns status-im.utils.handlers-macro
(:require-macros status-im.utils.handlers-macro) (:require-macros status-im.utils.handlers-macro
[taoensso.timbre :as log])
(:require [clojure.set :as set])) (:require [clojure.set :as set]))
(defn update-db [cofx fx] (defn update-db [cofx fx]
@ -10,7 +11,7 @@
(def ^:private mergable-keys (def ^:private mergable-keys
#{:data-store/tx :data-store/base-tx :chat-received-message/add-fx #{:data-store/tx :data-store/base-tx :chat-received-message/add-fx
:shh/add-new-sym-keys :shh/get-new-sym-keys :shh/post :shh/add-new-sym-keys :shh/get-new-sym-keys :shh/post
:confirm-messages-processed}) :confirm-messages-processed :utils/dispatch-later})
(defn safe-merge [fx new-fx] (defn safe-merge [fx new-fx]
(if (:merging-fx-with-common-keys fx) (if (:merging-fx-with-common-keys fx)
@ -23,7 +24,8 @@
(merge-with into (merge-with into
(select-keys fx mergable-keys) (select-keys fx mergable-keys)
(select-keys new-fx mergable-keys))) (select-keys new-fx mergable-keys)))
{:merging-fx-with-common-keys common-keys})))) (do (log/error "Merging fx with common-keys: " common-keys)
{:merging-fx-with-common-keys common-keys})))))
(defn merge-effects (defn merge-effects
([{:keys [db] :as cofx} handler args] ([{:keys [db] :as cofx} handler args]

View File

@ -6,7 +6,10 @@
status-im.ui.screens.subs status-im.ui.screens.subs
[re-frame.core :as re-frame] [re-frame.core :as re-frame]
[status-im.models.browser :as model] [status-im.models.browser :as model]
[status-im.utils.types :as types])) [status-im.utils.types :as types]
[status-im.utils.handlers :as handlers]
[status-im.utils.handlers-macro :as handlers-macro]
[status-im.models.browser :as browser]))
(defn test-fixtures [] (defn test-fixtures []
@ -32,7 +35,17 @@
(fn [[permission {:keys [dapp-name permissions-data index] :as params}]] (fn [[permission {:keys [dapp-name permissions-data index] :as params}]]
(if (and (= dapp-name "test.com") (#{0 1} index)) (if (and (= dapp-name "test.com") (#{0 1} index))
(re-frame/dispatch [:next-dapp-permission params permission permissions-data]) (re-frame/dispatch [:next-dapp-permission params permission permissions-data])
(re-frame/dispatch [:next-dapp-permission params]))))) (re-frame/dispatch [:next-dapp-permission params]))))
(handlers/register-handler-fx
[(re-frame/inject-cofx :data-store/all-browsers)
(re-frame/inject-cofx :data-store/all-dapp-permissions)]
:initialize-test
(fn [cofx [_]]
(handlers-macro/merge-fx cofx
(events/initialize-db)
(browser/initialize-browsers)
(browser/initialize-dapp-permissions)))))
(deftest browser-events (deftest browser-events
@ -40,16 +53,16 @@
(test-fixtures) (test-fixtures)
(re-frame/dispatch [:initialize-db]) (re-frame/dispatch [:initialize-test])
(re-frame/dispatch [:initialize-browsers]) (println :app-db @re-frame.db/app-db)
(let [browsers (re-frame/subscribe [:browsers]) (let [browsers (re-frame/subscribe [:browsers])
dapp1-url "cryptokitties.co" dapp1-url "cryptokitties.co"
dapp2-url "http://test2.com"] dapp2-url "http://test2.com"]
(testing "open and remove dapps" (testing "open and remove dapps"
(println :browsers @browsers)
(is (zero? (count @browsers))) (is (do (println :browser @browsers)
(zero? (count @browsers))))
(re-frame/dispatch [:open-url-in-browser dapp1-url]) (re-frame/dispatch [:open-url-in-browser dapp1-url])
@ -120,8 +133,6 @@
(is (= 1 (:history-index @browser))) (is (= 1 (:history-index @browser)))
(is (= [dapp2-url dapp2-url3] (:history @browser)))))) (is (= [dapp2-url dapp2-url3] (:history @browser))))))
(re-frame/dispatch [:initialize-dapp-permissions])
(let [dapps-permissions (re-frame/subscribe [:get :dapps/permissions]) (let [dapps-permissions (re-frame/subscribe [:get :dapps/permissions])
dapp-name "test.com" dapp-name "test.com"
dapp-name2 "test2.org"] dapp-name2 "test2.org"]

View File

@ -43,7 +43,7 @@
(def personal-request-command (transactions/PersonalRequestCommand.)) (def personal-request-command (transactions/PersonalRequestCommand.))
(deftest personal-send-command-test (deftest personal-request-command-test
(testing "That correct parameters are defined" (testing "That correct parameters are defined"
(is (= (into #{} (map :id) (protocol/parameters personal-request-command)) (is (= (into #{} (map :id) (protocol/parameters personal-request-command))
#{:asset :amount}))) #{:asset :amount})))
@ -72,4 +72,3 @@
:message-id "0xAA" :message-id "0xAA"
:response "send" :response "send"
:status "open"}))))) :status "open"})))))

View File

@ -1,17 +1,18 @@
(ns status-im.test.transport.core (ns status-im.test.transport.core
(:require [cljs.test :refer-macros [deftest is testing]] (:require [cljs.test :refer-macros [deftest is testing]]
[status-im.protocol.handlers :as protocol.handlers] [status-im.models.protocol :as protocol]
[status-im.transport.core :as transport])) [status-im.transport.core :as transport]))
(deftest init-whisper (deftest init-whisper
(let [cofx {:db {:network "mainnet_rpc" (let [cofx {:db {:network "mainnet_rpc"
:account/account :account/account
{:networks {"mainnet_rpc" {:config {:NetworkId 1}}} {:networks {"mainnet_rpc" {:config {:NetworkId 1}}}
:public-key "1"}}}] :public-key "1"}
:semaphores #{}}}]
(testing "it adds the discover filter" (testing "it adds the discover filter"
(is (= (:shh/add-discovery-filter (protocol.handlers/initialize-protocol cofx []))))) (is (= (:shh/add-discovery-filter (protocol/initialize-protocol "user-address" cofx)))))
(testing "it restores the sym-keys" (testing "it restores the sym-keys"
(is (= (:shh/restore-sym-keys (protocol.handlers/initialize-protocol cofx []))))) (is (= (:shh/restore-sym-keys (protocol/initialize-protocol "user-address" cofx)))))
(testing "custom mailservers" (testing "custom mailservers"
(let [ms-1 {:id "1" (let [ms-1 {:id "1"
:chain "mainnet" :chain "mainnet"
@ -44,5 +45,5 @@
ms-3])] ms-3])]
(is (= expected-wnodes (is (= expected-wnodes
(get-in (get-in
(protocol.handlers/initialize-protocol cofx-with-ms []) (protocol/initialize-protocol "user-address" cofx-with-ms)
[:db :inbox/wnodes]))))))) [:db :inbox/wnodes])))))))