Communities: Share airdrop address (#18505)

This commit is contained in:
Ajay Sivan 2024-01-17 23:53:20 +05:30 committed by GitHub
parent 72bf8a8702
commit c888c512a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 213 additions and 50 deletions

View File

@ -10,25 +10,21 @@
[utils.re-frame :as rf])) [utils.re-frame :as rf]))
(defn- join-community-and-navigate-back (defn- join-community-and-navigate-back
[id addresses-for-permissions] [id]
(rf/dispatch [:password-authentication/show (rf/dispatch [:password-authentication/show
{:content (fn [] [password-authentication/view])} {:content (fn [] [password-authentication/view])}
{:label (i18n/label :t/join-open-community) {:label (i18n/label :t/join-open-community)
:on-press #(rf/dispatch [:communities/request-to-join :on-press #(rf/dispatch [:communities/request-to-join-with-addresses
{:community-id id {:community-id id
:password % :password %}])}])
:addresses-to-reveal addresses-for-permissions}])}])
(rf/dispatch [:navigate-back])) (rf/dispatch [:navigate-back]))
(defn f-view-internal (defn f-view-internal
[] []
(let [{id :community-id} (rf/sub [:get-screen-params]) (let [{id :community-id} (rf/sub [:get-screen-params])
{:keys [name color images]} (rf/sub [:communities/community id]) {:keys [name color images]} (rf/sub [:communities/community id])
accounts (rf/sub [:wallet/accounts-with-customization-color]) airdrop-account (rf/sub [:communities/airdrop-account])
selected-permission-addresses (rf/sub [:communities/selected-permission-addresses]) selected-accounts (rf/sub [:communities/selected-permission-accounts])]
selected-accounts (filter #(contains? selected-permission-addresses
(:address %))
accounts)]
(rn/use-effect (fn [] (rn/use-effect (fn []
(rf/dispatch [:communities/initialize-permission-addresses])) (rf/dispatch [:communities/initialize-permission-addresses]))
[]) [])
@ -71,8 +67,8 @@
:action :arrow :action :arrow
:label :preview :label :preview
:label-props {:type :accounts :label-props {:type :accounts
:data [(first accounts)]} :data [airdrop-account]}
:description-props {:text (-> accounts first :name)}}]}] :description-props {:text (:name airdrop-account)}}]}]
[quo/text [quo/text
{:style style/section-title {:style style/section-title
:accessibility-label :community-rules-title :accessibility-label :community-rules-title
@ -86,7 +82,7 @@
:track-text (i18n/label :t/slide-to-request-to-join) :track-text (i18n/label :t/slide-to-request-to-join)
:track-icon :i/face-id :track-icon :i/face-id
:customization-color color :customization-color color
:on-complete #(join-community-and-navigate-back id selected-permission-addresses)}]]])) :on-complete #(join-community-and-navigate-back id)}]]]))
(defn view (defn view
[] []

View File

@ -1,6 +1,5 @@
(ns status-im.contexts.communities.actions.airdrop-addresses.style) (ns status-im.contexts.communities.actions.airdrop-addresses.style)
(def account-list-container (def account-list-container
{:flex 1 {:padding-horizontal 8
:padding-top 12
:padding-bottom 8}) :padding-bottom 8})

View File

@ -8,26 +8,21 @@
[utils.re-frame :as rf])) [utils.re-frame :as rf]))
(defn- render-item (defn- render-item
[item] [item _ _ airdrop-address]
[quo/account-item [quo/account-item
{:account-props item {:account-props item
:state (when (= airdrop-address (:address item)) :selected)
:on-press (fn []
(rf/dispatch [:communities/set-airdrop-address (:address item)])
(rf/dispatch [:navigate-back]))
:emoji (:emoji item)}]) :emoji (:emoji item)}])
(defn- accounts-list
[{:keys [accounts]}]
[rn/view {:style style/account-list-container}
(when (seq accounts)
[rn/flat-list
{:data accounts
:render-fn render-item
:key-fn :address}])])
(defn view (defn view
[] []
(let [{id :community-id} (rf/sub [:get-screen-params]) (let [{id :community-id} (rf/sub [:get-screen-params])
{:keys [name images color]} (rf/sub [:communities/community id]) {:keys [name images color]} (rf/sub [:communities/community id])
logo-uri (get-in images [:thumbnail :uri]) selected-accounts (rf/sub [:communities/selected-permission-accounts])
accounts (rf/sub [:wallet/accounts-with-customization-color])] airdrop-address (rf/sub [:communities/airdrop-address])]
[:<> [:<>
[quo/drawer-top [quo/drawer-top
{:type :context-tag {:type :context-tag
@ -37,7 +32,9 @@
:on-button-press not-implemented/alert :on-button-press not-implemented/alert
:community-logo (get-in images [:thumbnail :uri]) :community-logo (get-in images [:thumbnail :uri])
:customization-color color}] :customization-color color}]
[accounts-list [rn/flat-list
{:accounts accounts {:data selected-accounts
:logo-uri logo-uri :render-fn render-item
:community-name name}]])) :render-data airdrop-address
:content-container-style style/account-list-container
:key-fn :address}]]))

View File

@ -191,19 +191,34 @@
(defn initialize-permission-addresses (defn initialize-permission-addresses
[{:keys [db]}] [{:keys [db]}]
(let [accounts (get-in db [:wallet :accounts]) (let [accounts (get-in db [:wallet :accounts])
addresses (set (map :address (vals accounts)))] sorted-accounts (sort-by :position (vals accounts))
addresses (set (map :address sorted-accounts))]
{:db (assoc db {:db (assoc db
:communities/previous-permission-addresses addresses :communities/previous-permission-addresses addresses
:communities/selected-permission-addresses addresses)})) :communities/selected-permission-addresses addresses
:communities/airdrop-address (:address (first sorted-accounts)))}))
(rf/reg-event-fx :communities/initialize-permission-addresses (rf/reg-event-fx :communities/initialize-permission-addresses
initialize-permission-addresses) initialize-permission-addresses)
(rf/reg-event-fx :communities/update-previous-permission-addresses (defn update-previous-permission-addresses
(fn [{:keys [db]}] [{:keys [db]}]
(let [accounts (get-in db [:wallet :accounts])
sorted-accounts (sort-by :position (vals accounts))
selected-permission-addresses (get-in db [:communities/selected-permission-addresses])
selected-accounts (filter #(contains? selected-permission-addresses
(:address %))
sorted-accounts)
current-airdrop-address (get-in db [:communities/airdrop-address])]
{:db (assoc db {:db (assoc db
:communities/previous-permission-addresses :communities/previous-permission-addresses selected-permission-addresses
(get-in db [:communities/selected-permission-addresses]))})) :communities/airdrop-address (if (contains? selected-permission-addresses
current-airdrop-address)
current-airdrop-address
(:address (first selected-accounts))))}))
(rf/reg-event-fx :communities/update-previous-permission-addresses
update-previous-permission-addresses)
(defn toggle-selected-permission-address (defn toggle-selected-permission-address
[{:keys [db]} [address]] [{:keys [db]} [address]]
@ -248,3 +263,7 @@
{:error err {:error err
:chat-id chat-id :chat-id chat-id
:event "share-community-channel-url-with-data"}))}]}))) :event "share-community-channel-url-with-data"}))}]})))
(rf/reg-event-fx :communities/set-airdrop-address
(fn [{:keys [db]} [address]]
{:db (assoc db :communities/airdrop-address address)}))

View File

@ -4,11 +4,14 @@
[status-im.contexts.communities.events :as events])) [status-im.contexts.communities.events :as events]))
(deftest initialize-permission-addresses-test (deftest initialize-permission-addresses-test
(let [initial-db {:db {:wallet {:accounts {"0x1" {:address "0x1"} (let [initial-db {:db {:wallet {:accounts {"0x1" {:address "0x1"
"0x2" {:address "0x2"}}}}} :position 0}
"0x2" {:address "0x2"
:position 1}}}}}
expected-db {:db (assoc (:db initial-db) expected-db {:db (assoc (:db initial-db)
:communities/previous-permission-addresses #{"0x1" "0x2"} :communities/previous-permission-addresses #{"0x1" "0x2"}
:communities/selected-permission-addresses #{"0x1" "0x2"})}] :communities/selected-permission-addresses #{"0x1" "0x2"}
:communities/airdrop-address "0x1")}]
(is (match? expected-db (events/initialize-permission-addresses initial-db))))) (is (match? expected-db (events/initialize-permission-addresses initial-db)))))
(deftest toggle-selected-permission-address-test (deftest toggle-selected-permission-address-test
@ -17,3 +20,33 @@
(events/toggle-selected-permission-address initial-db ["0x2"]))) (events/toggle-selected-permission-address initial-db ["0x2"])))
(is (match? {:db {:communities/selected-permission-addresses #{"0x1" "0x2" "0x3"}}} (is (match? {:db {:communities/selected-permission-addresses #{"0x1" "0x2" "0x3"}}}
(events/toggle-selected-permission-address initial-db ["0x3"]))))) (events/toggle-selected-permission-address initial-db ["0x3"])))))
(deftest update-previous-permission-addresses-test
(let [wallet {:accounts {"0x1" {:address "0x1"
:position 0}
"0x2" {:address "0x2"
:position 1}
"0x3" {:address "0x3"
:position 2}}}]
(let [initial-db {:db {:wallet wallet
:communities/previous-permission-addresses #{"0x1" "0x2"}
:communities/selected-permission-addresses #{"0x1" "0x2" "0x3"}
:communities/airdrop-address "0x1"}}
expected-db {:db {:wallet wallet
:communities/previous-permission-addresses #{"0x1" "0x2" "0x3"}
:communities/selected-permission-addresses #{"0x1" "0x2" "0x3"}
:communities/airdrop-address "0x1"}}]
(is
(match? expected-db
(events/update-previous-permission-addresses initial-db))))
(let [initial-db {:db {:wallet wallet
:communities/previous-permission-addresses #{"0x1" "0x2"}
:communities/selected-permission-addresses #{"0x2" "0x3"}
:communities/airdrop-address "0x1"}}
expected-db {:db {:wallet wallet
:communities/previous-permission-addresses #{"0x2" "0x3"}
:communities/selected-permission-addresses #{"0x2" "0x3"}
:communities/airdrop-address "0x2"}}]
(is
(match? expected-db
(events/update-previous-permission-addresses initial-db))))))

View File

@ -64,12 +64,11 @@
(defn request-to-join (defn request-to-join
[{:keys [db]} [{:keys [db]}
[{:keys [community-id password addresses-to-reveal] [{:keys [community-id password]}]]
:or {addresses-to-reveal []}}]]
(let [pub-key (get-in db [:profile/profile :public-key])] (let [pub-key (get-in db [:profile/profile :public-key])]
{:fx [[:json-rpc/call {:fx [[:json-rpc/call
[{:method "wakuext_generateJoiningCommunityRequestsForSigning" [{:method "wakuext_generateJoiningCommunityRequestsForSigning"
:params [pub-key community-id addresses-to-reveal] :params [pub-key community-id []]
:on-success [:communities/sign-data community-id password] :on-success [:communities/sign-data community-id password]
:on-error [:communities/requested-to-join-error community-id]}]]]})) :on-error [:communities/requested-to-join-error community-id]}]]]}))
@ -116,9 +115,6 @@
:params [{:communityId community-id :params [{:communityId community-id
:signatures signatures :signatures signatures
:addressesToReveal addresses-to-reveal :addressesToReveal addresses-to-reveal
;; NOTE: At least one airdrop address is required.
;; This is a temporary solution while the address
;; selection feature is not implemented in mobile.
:airdropAddress (first addresses-to-reveal)}] :airdropAddress (first addresses-to-reveal)}]
:js-response true :js-response true
:on-success [:communities/requested-to-join] :on-success [:communities/requested-to-join]
@ -145,3 +141,43 @@
:event :communities/toggle-collapsed-category :event :communities/toggle-collapsed-category
:category-id category-id :category-id category-id
:collapse? collapse?})}]})) :collapse? collapse?})}]}))
(defn request-to-join-with-signatures-and-addresses
[{:keys [db]} [community-id signatures]]
(let [airdrop-address (get-in db [:communities/airdrop-address])
addresses-to-reveal (get-in db [:communities/selected-permission-addresses])]
{:fx [[:json-rpc/call
[{:method "wakuext_requestToJoinCommunity"
:params [{:communityId community-id
:signatures signatures
:addressesToReveal addresses-to-reveal
:airdropAddress airdrop-address}]
:js-response true
:on-success [:communities/requested-to-join]
:on-error [:communities/requested-to-join-error community-id]}]]]}))
(rf/reg-event-fx :communities/request-to-join-with-signatures-and-addresses
request-to-join-with-signatures-and-addresses)
(defn sign-data-with-addresses
[_ [community-id password sign-params]]
{:fx [[:json-rpc/call
[{:method "wakuext_signData"
:params [(map #(assoc % :password password) sign-params)]
:on-success [:communities/request-to-join-with-signatures-and-addresses community-id]
:on-error [:communities/requested-to-join-error community-id]}]]]})
(rf/reg-event-fx :communities/sign-data-with-addresses sign-data-with-addresses)
(defn request-to-join-with-addresses
[{:keys [db]}
[{:keys [community-id password]}]]
(let [pub-key (get-in db [:profile/profile :public-key])
addresses-to-reveal (get-in db [:communities/selected-permission-addresses])]
{:fx [[:json-rpc/call
[{:method "wakuext_generateJoiningCommunityRequestsForSigning"
:params [pub-key community-id addresses-to-reveal]
:on-success [:communities/sign-data-with-addresses community-id password]
:on-error [:communities/requested-to-join-error community-id]}]]]}))
(rf/reg-event-fx :communities/request-to-join-with-addresses request-to-join-with-addresses)

View File

@ -335,3 +335,17 @@
:<- [:communities] :<- [:communities]
(fn [communities [_ community-id]] (fn [communities [_ community-id]]
(get-in communities [community-id :intro-message]))) (get-in communities [community-id :intro-message])))
(re-frame/reg-sub
:communities/airdrop-account
:<- [:communities/airdrop-address]
:<- [:wallet/accounts-with-customization-color]
(fn [[airdrop-address accounts]]
(first (filter #(= (:address %) airdrop-address) accounts))))
(re-frame/reg-sub
:communities/selected-permission-accounts
:<- [:communities/selected-permission-addresses]
:<- [:wallet/accounts-with-customization-color]
(fn [[selected-permission-addresses accounts]]
(filter #(contains? selected-permission-addresses (:address %)) accounts)))

View File

@ -459,3 +459,49 @@
:loading? checking-permissions? :loading? checking-permissions?
:img-src token-image-eth}]]} :img-src token-image-eth}]]}
(rf/sub [sub-name community-id]))))) (rf/sub [sub-name community-id])))))
(h/deftest-sub :communities/airdrop-account
[sub-name]
(testing "returns airdrop account"
(swap! rf-db/app-db assoc
:communities/airdrop-address
"0x1"
:wallet {:accounts {"0x1" {:address "0x1"
:color :blue
:name "account1"}
"0x2" {:address "0x2"
:color :orange
:name "account2"}}})
(is (match? {:address "0x1"
:network-preferences-names #{}
:name "account1"
:color :blue
:customization-color :blue}
(rf/sub [sub-name])))))
(h/deftest-sub :communities/selected-permission-accounts
[sub-name]
(testing "returns selected permission accounts"
(swap! rf-db/app-db assoc
:communities/selected-permission-addresses
#{"0x1" "0x3"}
:wallet {:accounts {"0x1" {:address "0x1"
:color :blue
:name "account1"}
"0x2" {:address "0x2"
:color :orange
:name "account2"}
"0x3" {:address "0x3"
:color :purple
:name "account3"}}})
(is (match? [{:address "0x1"
:color :blue
:customization-color :blue
:network-preferences-names #{}
:name "account1"}
{:address "0x3"
:color :purple
:customization-color :purple
:network-preferences-names #{}
:name "account3"}]
(rf/sub [sub-name])))))

View File

@ -146,6 +146,7 @@
(reg-root-key-sub :communities/selected-tab :communities/selected-tab) (reg-root-key-sub :communities/selected-tab :communities/selected-tab)
(reg-root-key-sub :contract-communities :contract-communities) (reg-root-key-sub :contract-communities :contract-communities)
(reg-root-key-sub :communities/selected-permission-addresses :communities/selected-permission-addresses) (reg-root-key-sub :communities/selected-permission-addresses :communities/selected-permission-addresses)
(reg-root-key-sub :communities/airdrop-address :communities/airdrop-address)
;;activity center ;;activity center
(reg-root-key-sub :activity-center :activity-center) (reg-root-key-sub :activity-center :activity-center)

View File

@ -459,3 +459,25 @@
(let [{:keys [formatted-balance tokens]} (rf/sub [sub-name])] (let [{:keys [formatted-balance tokens]} (rf/sub [sub-name])]
(is (match? 2 (count tokens))) (is (match? 2 (count tokens)))
(is (match? "$4506.00" formatted-balance))))) (is (match? "$4506.00" formatted-balance)))))
(h/deftest-sub :wallet/accounts-with-customization-color
[sub-name]
(testing "returns all accounts with customization color"
(swap! rf-db/app-db
#(-> %
(assoc-in [:wallet :accounts] accounts)
(assoc-in [:wallet :networks] network-data)))
(is
(= [(-> accounts
(get "0x1")
(assoc :customization-color :blue)
(assoc :network-preferences-names #{:ethereum :arbitrum :optimism}))
(-> accounts
(get "0x2")
(assoc :customization-color :purple)
(assoc :network-preferences-names #{:ethereum :arbitrum :optimism}))
(-> accounts
(get "0x3")
(assoc :customization-color :magenta)
(assoc :network-preferences-names #{}))]
(rf/sub [sub-name])))))