Resolve contract by ens name

Do not show referrals if contract is not available

Do not check contract for referral accept

Rename invite event namespace require

Remove dispatch from raw subs

Do nothing if there is no contract

Signed-off-by: Gheorghe Pinzaru <feross95@gmail.com>
This commit is contained in:
Gheorghe Pinzaru 2020-08-07 17:24:09 +03:00
parent ac4f65be97
commit c644ab3501
No known key found for this signature in database
GPG Key ID: C9A094959935A952
5 changed files with 82 additions and 69 deletions

View File

@ -1,6 +1,10 @@
(ns status-im.acquisition.core (ns status-im.acquisition.core
(:require [re-frame.core :as re-frame] (:require [re-frame.core :as re-frame]
[clojure.string :as string]
[status-im.utils.fx :as fx] [status-im.utils.fx :as fx]
[status-im.ethereum.core :as ethereum]
[status-im.ethereum.ens :as ens]
[status-im.ethereum.contracts :as contracts]
[status-im.acquisition.chat :as chat] [status-im.acquisition.chat :as chat]
[status-im.acquisition.advertiser :as advertiser] [status-im.acquisition.advertiser :as advertiser]
[status-im.acquisition.persistance :as persistence] [status-im.acquisition.persistance :as persistence]
@ -84,12 +88,23 @@
(when-not (nil? tx) (when-not (nil? tx)
(re-frame/dispatch [::add-tx-watcher tx])))}))))) (re-frame/dispatch [::add-tx-watcher tx])))})))))
(fx/defn create (re-frame/reg-fx
{} ::resolve-contract
[_] (fn [{:keys [chain contract on-success]}]
{::get-referrer nil}) (let [register (get ens/ens-registries chain)]
(when contract
(if (string/starts-with? contract "0x")
(on-success contract)
(ens/resolver register contract on-success))))))
(fx/defn login (fx/defn create [{:keys [db]}]
{} {::resolve-contract {:chain (ethereum/chain-keyword db)
[_] :contract (contracts/get-address db :status/acquisition)
{::check-referrer nil}) :on-success #(re-frame/dispatch [:set-in [:acquisition :contract] %])}
::get-referrer nil})
(fx/defn login [{:keys [db]}]
{::resolve-contract {:chain (ethereum/chain-keyword db)
:contract (contracts/get-address db :status/acquisition)
:on-success #(re-frame/dispatch [:set-in [:acquisition :contract] %])}
::check-referrer nil})

View File

@ -17,8 +17,8 @@
{:testnet "0xf852198d0385c4b871e0b91804ecd47c6ba97351" {:testnet "0xf852198d0385c4b871e0b91804ecd47c6ba97351"
:mainnet "0x110101156e8F0743948B2A61aFcf3994A8Fb172e"} :mainnet "0x110101156e8F0743948B2A61aFcf3994A8Fb172e"}
:status/acquisition :status/acquisition
{:rinkeby "0x719C016d16ff0396274d488Efc4D8aEaAA126b48" {:rinkeby "0xf8Ea093cE1441ad51EDD4C0FD15410b457e88906"
:mainnet "0x81142267f927bffc6dba200a928c376fbf6675a8"}}) :mainnet "statusreferrals.eth"}})
(defn get-address (defn get-address
[db contract] [db contract]

View File

@ -180,6 +180,7 @@
(and platform/android? (and platform/android?
notifications-enabled?) notifications-enabled?)
(assoc ::notifications/enable nil)) (assoc ::notifications/enable nil))
(acquisition/login)
(initialize-appearance) (initialize-appearance)
;; NOTE: initializing mailserver depends on user mailserver ;; NOTE: initializing mailserver depends on user mailserver
;; preference which is why we wait for config callback ;; preference which is why we wait for config callback
@ -228,24 +229,31 @@
(fx/defn create-only-events (fx/defn create-only-events
[{:keys [db] :as cofx}] [{:keys [db] :as cofx}]
(let [{:keys [multiaccount :multiaccount/accounts]} db] (let [{:keys [multiaccount multiaccounts :multiaccount/accounts]} db
{:keys [creating?]} (:multiaccounts/login db)
recovering? (get-in db [:intro-wizard :recovering?])
first-account? (and creating?
(not recovering?)
(empty? multiaccounts))]
(fx/merge cofx (fx/merge cofx
{:db (-> db {:db (-> db
(dissoc :multiaccounts/login) (dissoc :multiaccounts/login)
(assoc (assoc
;;NOTE when login the filters are initialized twice ;;NOTE when login the filters are initialized twice
;;once for contacts and once for chats ;;once for contacts and once for chats
;;when creating an account we do it only once by calling ;;when creating an account we do it only once by calling
;;load-filters directly because we don't have chats and contacts ;;load-filters directly because we don't have chats and contacts
;;later on there is a check that filters have been initialized twice ;;later on there is a check that filters have been initialized twice
;;so here we set it at 1 already so that it passes the check once it has ;;so here we set it at 1 already so that it passes the check once it has
;;been initialized ;;been initialized
:filters/initialized 1)) :filters/initialized 1))
:filters/load-filters [[(:waku-enabled multiaccount) []]]} :filters/load-filters [[(:waku-enabled multiaccount) []]]}
(finish-keycard-setup) (finish-keycard-setup)
(protocol/initialize-protocol {:mailservers [] (when first-account?
:mailserver-ranges {} (acquisition/create))
:mailserver-topics {} (protocol/initialize-protocol {:mailservers []
:mailserver-ranges {}
:mailserver-topics {}
:default-mailserver true}) :default-mailserver true})
(multiaccounts/switch-preview-privacy-mode-flag) (multiaccounts/switch-preview-privacy-mode-flag)
(logging/set-log-level (:log-level multiaccount)) (logging/set-log-level (:log-level multiaccount))
@ -259,9 +267,6 @@
(let [{:keys [key-uid password save-password? creating?]} (:multiaccounts/login db) (let [{:keys [key-uid password save-password? creating?]} (:multiaccounts/login db)
multiaccounts (:multiaccounts/multiaccounts db) multiaccounts (:multiaccounts/multiaccounts db)
recovering? (get-in db [:intro-wizard :recovering?]) recovering? (get-in db [:intro-wizard :recovering?])
first-account? (and creating?
(not recovering?)
(empty? multiaccounts))
login-only? (not (or creating? login-only? (not (or creating?
recovering? recovering?
(keycard-setup? cofx))) (keycard-setup? cofx)))
@ -288,9 +293,6 @@
(if login-only? (if login-only?
(login-only-events key-uid password save-password?) (login-only-events key-uid password save-password?)
(create-only-events)) (create-only-events))
(if first-account?
(acquisition/create)
(acquisition/login))
(when recovering? (when recovering?
(navigation/navigate-to-cofx :tabs {:screen :chat-stack (navigation/navigate-to-cofx :tabs {:screen :chat-stack
:params {:screen :home}}))))) :params {:screen :home}})))))

View File

@ -2,8 +2,8 @@
(:require [re-frame.core :as re-frame] (:require [re-frame.core :as re-frame]
[reagent.ratom :refer [make-reaction]] [reagent.ratom :refer [make-reaction]]
[status-im.utils.fx :as fx] [status-im.utils.fx :as fx]
[status-im.utils.config :as config]
[status-im.ethereum.json-rpc :as json-rpc] [status-im.ethereum.json-rpc :as json-rpc]
[status-im.ethereum.contracts :as contracts]
[status-im.ethereum.core :as ethereum] [status-im.ethereum.core :as ethereum]
[status-im.ui.components.react :as react] [status-im.ui.components.react :as react]
[status-im.navigation :as navigation] [status-im.navigation :as navigation]
@ -53,7 +53,7 @@
(fx/defn go-to-invite (fx/defn go-to-invite
{:events [::open-invite]} {:events [::open-invite]}
[{:keys [db] :as cofx}] [{:keys [db] :as cofx}]
(let [contract (contracts/get-address db :status/acquisition) (let [contract (get-in db [:acquisition :contract])
accounts (filter #(not= (:type %) :watch) (get db :multiaccount/accounts))] accounts (filter #(not= (:type %) :watch) (get db :multiaccount/accounts))]
(fx/merge cofx (fx/merge cofx
{::get-rewards (mapv (fn [{:keys [address]}] {::get-rewards (mapv (fn [{:keys [address]}]
@ -135,18 +135,13 @@
(let [[address amount] data] (let [[address amount] data]
{:db (assoc-in db [:acquisition :accounts account :tokens address] (money/wei->ether amount))}))) {:db (assoc-in db [:acquisition :accounts account :tokens address] (money/wei->ether amount))})))
(fx/defn get-default-reward
{:events [::get-default-reward]}
[{:keys [db]}]
{::get-rewards [{:contract (contracts/get-address db :status/acquisition)
:address (ethereum/default-address db)
:on-success (fn [type data]
(re-frame/dispatch [::default-reward-success type data]))}]})
(re-frame/reg-sub-raw (re-frame/reg-sub-raw
::default-reward ::default-reward
(fn [db] (fn [db]
(re-frame/dispatch [::get-default-reward]) (get-reward (get-in @db [:acquisition :contract])
(ethereum/default-address @db)
(fn [type data]
(re-frame/dispatch [::default-reward-success type data])))
(make-reaction (make-reaction
(fn [] (fn []
(get-in @db [:acquisition :referral-reward]))))) (get-in @db [:acquisition :referral-reward])))))
@ -162,19 +157,21 @@
(map money/wei->ether tokens-amount)) (map money/wei->ether tokens-amount))
:sticker-packs sticker-packs})}) :sticker-packs sticker-packs})})
(fx/defn starter-pack
{:events [::starter-pack]}
[{:keys [db]}]
(let [contract (contracts/get-address db :status/acquisition)]
{::json-rpc/eth-call [{:contract contract
:method "getDefaultPack()"
:outputs ["address" "uint256" "address[]" "uint256[]" "uint256[]"]
:on-success #(re-frame/dispatch [::starter-pack-amount (vec %) (prn %)])}]}))
(re-frame/reg-sub-raw (re-frame/reg-sub-raw
::starter-pack ::starter-pack
(fn [db] (fn [db]
(re-frame/dispatch [::starter-pack]) (let [contract (get-in @db [:acquisition :contract])]
(make-reaction (json-rpc/eth-call
(fn [] {:contract contract
(get-in @db [:acquisition :starter-pack :pack]))))) :method "getDefaultPack()"
:outputs ["address" "uint256" "address[]" "uint256[]" "uint256[]"]
:on-success #(re-frame/dispatch [::starter-pack-amount (vec %)])})
(make-reaction
(fn []
(get-in @db [:acquisition :starter-pack :pack]))))))
(re-frame/reg-sub
::enabled
(fn [db]
(and config/referrals-invite-enabled?
(get-in db [:acquisition :contract]))))

View File

@ -11,9 +11,8 @@
[quo.design-system.colors :as colors] [quo.design-system.colors :as colors]
[status-im.ui.components.invite.style :as styles] [status-im.ui.components.invite.style :as styles]
[status-im.ui.components.topbar :as topbar] [status-im.ui.components.topbar :as topbar]
[status-im.ui.components.invite.events :as events] [status-im.ui.components.invite.events :as invite.events]
[status-im.ui.components.invite.utils :refer [transform-tokens]] [status-im.ui.components.invite.utils :refer [transform-tokens]]
[status-im.utils.config :as config]
[quo.react-native :as rn] [quo.react-native :as rn]
[clojure.string :as cstr])) [clojure.string :as cstr]))
@ -191,8 +190,8 @@
(let [accounts @(re-frame/subscribe [:accounts-without-watch-only]) (let [accounts @(re-frame/subscribe [:accounts-without-watch-only])
default-account @(re-frame/subscribe [:multiaccount/default-account]) default-account @(re-frame/subscribe [:multiaccount/default-account])
account (or @account* default-account) account (or @account* default-account)
reward @(re-frame/subscribe [::events/default-reward]) reward @(re-frame/subscribe [::invite.events/default-reward])
starter-pack @(re-frame/subscribe [::events/starter-pack])] starter-pack @(re-frame/subscribe [::invite.events/starter-pack])]
[rn/view {:flex 1} [rn/view {:flex 1}
[topbar/topbar {:modal? true [topbar/topbar {:modal? true
:show-border? true :show-border? true
@ -210,27 +209,27 @@
(i18n/label :t/invite-privacy-policy1) (i18n/label :t/invite-privacy-policy1)
" " " "
[quo/text {:color :link [quo/text {:color :link
:on-press #(re-frame/dispatch [::events/terms-and-conditions])} :on-press #(re-frame/dispatch [::invite.events/terms-and-conditions])}
(i18n/label :t/invite-privacy-policy2)]]]] (i18n/label :t/invite-privacy-policy2)]]]]
[toolbar/toolbar [toolbar/toolbar
{:show-border? true {:show-border? true
:center :center
[quo/button {:type :secondary [quo/button {:type :secondary
:on-press #(re-frame/dispatch [::events/generate-invite :on-press #(re-frame/dispatch [::invite.events/generate-invite
{:address (get account :address)}])} {:address (get account :address)}])}
(i18n/label :t/invite-button)]}]])))) (i18n/label :t/invite-button)]}]]))))
(defn button [] (defn button []
(if-not config/referrals-invite-enabled? (if-not @(re-frame/subscribe [::invite.events/enabled])
[rn/view {:style {:align-items :center}} [rn/view {:style {:align-items :center}}
[rn/view {:style (:tiny spacing/padding-vertical)} [rn/view {:style (:tiny spacing/padding-vertical)}
[quo/button {:on-press #(re-frame/dispatch [::events/share-link nil]) [quo/button {:on-press #(re-frame/dispatch [::invite.events/share-link nil])
:accessibility-label :invite-friends-button} :accessibility-label :invite-friends-button}
(i18n/label :t/invite-friends)]]] (i18n/label :t/invite-friends)]]]
(let [reward @(re-frame/subscribe [::events/default-reward])] (let [reward @(re-frame/subscribe [::invite.events/default-reward])]
[rn/view {:style {:align-items :center}} [rn/view {:style {:align-items :center}}
[rn/view {:style (:tiny spacing/padding-vertical)} [rn/view {:style (:tiny spacing/padding-vertical)}
[quo/button {:on-press #(re-frame/dispatch [::events/open-invite]) [quo/button {:on-press #(re-frame/dispatch [::invite.events/open-invite])
:accessibility-label :invite-friends-button} :accessibility-label :invite-friends-button}
(i18n/label :t/invite-friends)]] (i18n/label :t/invite-friends)]]
[rn/view {:style (merge (:tiny spacing/padding-vertical) [rn/view {:style (merge (:tiny spacing/padding-vertical)
@ -248,7 +247,7 @@
(i18n/label :t/invite-reward {:value (str (get reward :eth-amount) " ETH")})]])]]))) (i18n/label :t/invite-reward {:value (str (get reward :eth-amount) " ETH")})]])]])))
(defn list-item [{:keys [accessibility-label]}] (defn list-item [{:keys [accessibility-label]}]
(if-not config/referrals-invite-enabled? (if-not @(re-frame/subscribe [::invite.events/enabled])
[quo/list-item [quo/list-item
{:theme :accent {:theme :accent
:title (i18n/label :t/invite-friends) :title (i18n/label :t/invite-friends)
@ -257,8 +256,8 @@
:on-press (fn [] :on-press (fn []
(re-frame/dispatch [:bottom-sheet/hide]) (re-frame/dispatch [:bottom-sheet/hide])
(js/setTimeout (js/setTimeout
#(re-frame/dispatch [::events/share-link nil]) 250))}] #(re-frame/dispatch [::invite.events/share-link nil]) 250))}]
(let [amount @(re-frame/subscribe [::events/default-reward])] (let [amount @(re-frame/subscribe [::invite.events/default-reward])]
[quo/list-item [quo/list-item
{:theme :accent {:theme :accent
:title (i18n/label :t/invite-friends) :title (i18n/label :t/invite-friends)
@ -267,7 +266,7 @@
:accessibility-label accessibility-label :accessibility-label accessibility-label
:on-press #(do :on-press #(do
(re-frame/dispatch [:bottom-sheet/hide]) (re-frame/dispatch [:bottom-sheet/hide])
(re-frame/dispatch [::events/open-invite]))}]))) (re-frame/dispatch [::invite.events/open-invite]))}])))