Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Communities: Share airdrop address #18505

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,21 @@
[utils.re-frame :as rf]))

(defn- join-community-and-navigate-back
[id addresses-for-permissions]
[id]
(rf/dispatch [:password-authentication/show
{:content (fn [] [password-authentication/view])}
{:label (i18n/label :t/join-open-community)
:on-press #(rf/dispatch [:communities/request-to-join
{:community-id id
:password %
:addresses-to-reveal addresses-for-permissions}])}])
:on-press #(rf/dispatch [:communities/request-to-join-with-addresses
{:community-id id
:password %}])}])
(rf/dispatch [:navigate-back]))

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

(defn view
[]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(ns status-im.contexts.communities.actions.airdrop-addresses.style)

(def account-list-container
{:flex 1
:padding-top 12
:padding-bottom 8})
{:padding-horizontal 8
:padding-bottom 8})
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,21 @@
[utils.re-frame :as rf]))

(defn- render-item
[item]
[item _ _ airdrop-address]
[quo/account-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)}])

(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
[]
(let [{id :community-id} (rf/sub [:get-screen-params])
{:keys [name images color]} (rf/sub [:communities/community id])
logo-uri (get-in images [:thumbnail :uri])
accounts (rf/sub [:wallet/accounts-with-customization-color])]
selected-accounts (rf/sub [:communities/selected-permission-accounts])
airdrop-address (rf/sub [:communities/airdrop-address])]
[:<>
[quo/drawer-top
{:type :context-tag
Expand All @@ -37,7 +32,9 @@
:on-button-press not-implemented/alert
:community-logo (get-in images [:thumbnail :uri])
:customization-color color}]
[accounts-list
{:accounts accounts
:logo-uri logo-uri
:community-name name}]]))
[rn/flat-list
{:data selected-accounts
:render-fn render-item
:render-data airdrop-address
:content-container-style style/account-list-container
:key-fn :address}]]))
33 changes: 26 additions & 7 deletions src/status_im/contexts/communities/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -190,20 +190,35 @@

(defn initialize-permission-addresses
[{:keys [db]}]
(let [accounts (get-in db [:wallet :accounts])
addresses (set (map :address (vals accounts)))]
(let [accounts (get-in db [:wallet :accounts])
sorted-accounts (sort-by :position (vals accounts))
addresses (set (map :address sorted-accounts))]
{:db (assoc db
: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
initialize-permission-addresses)

(defn update-previous-permission-addresses
[{: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
:communities/previous-permission-addresses 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
(fn [{:keys [db]}]
{:db (assoc db
:communities/previous-permission-addresses
(get-in db [:communities/selected-permission-addresses]))}))
update-previous-permission-addresses)

(defn toggle-selected-permission-address
[{:keys [db]} [address]]
Expand Down Expand Up @@ -248,3 +263,7 @@
{:error err
:chat-id chat-id
: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)}))
39 changes: 36 additions & 3 deletions src/status_im/contexts/communities/events_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
[status-im.contexts.communities.events :as events]))

(deftest initialize-permission-addresses-test
(let [initial-db {:db {:wallet {:accounts {"0x1" {:address "0x1"}
"0x2" {:address "0x2"}}}}}
(let [initial-db {:db {:wallet {:accounts {"0x1" {:address "0x1"
:position 0}
"0x2" {:address "0x2"
:position 1}}}}}
expected-db {:db (assoc (:db initial-db)
: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)))))

(deftest toggle-selected-permission-address-test
Expand All @@ -17,3 +20,33 @@
(events/toggle-selected-permission-address initial-db ["0x2"])))
(is (match? {:db {:communities/selected-permission-addresses #{"0x1" "0x2" "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))))))
48 changes: 42 additions & 6 deletions src/status_im/contexts/communities/overview/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,11 @@

(defn request-to-join
[{:keys [db]}
[{:keys [community-id password addresses-to-reveal]
:or {addresses-to-reveal []}}]]
[{:keys [community-id password]}]]
(let [pub-key (get-in db [:profile/profile :public-key])]
{:fx [[:json-rpc/call
[{: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-error [:communities/requested-to-join-error community-id]}]]]}))

Expand Down Expand Up @@ -116,9 +115,6 @@
:params [{:communityId community-id
:signatures signatures
: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)}]
:js-response true
:on-success [:communities/requested-to-join]
Expand All @@ -145,3 +141,43 @@
:event :communities/toggle-collapsed-category
:category-id category-id
: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)
14 changes: 14 additions & 0 deletions src/status_im/subs/communities.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,17 @@
:<- [:communities]
(fn [communities [_ community-id]]
(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)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to write tests for layer-3 subs @ajayesivan. Layer-3 subs are the ones that do more than just extract data (often extractors just use get or get-in). Guideline https://github.com/status-im/status-mobile/blob/a2bf23cc1281fa10a5b44e65c4cf41c2efeda293/doc/new-guidelines.md#subscription-tests

By using deftest-sub we also make sure changes in subscriptions are interconnected. For example, introducing breaking changes in any sub that's a dependency of another sub should theoretically break both of their subscription tests. That's why it's so important to test all layer-3 subs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ilmotta, Thanks for the review and suggestion. I have added tests, could you please have a look?

Thank you!

46 changes: 46 additions & 0 deletions src/status_im/subs/communities_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -459,3 +459,49 @@
:loading? checking-permissions?
:img-src token-image-eth}]]}
(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])))))
1 change: 1 addition & 0 deletions src/status_im/subs/root.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
(reg-root-key-sub :communities/selected-tab :communities/selected-tab)
(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/airdrop-address :communities/airdrop-address)

;;activity center
(reg-root-key-sub :activity-center :activity-center)
Expand Down
22 changes: 22 additions & 0 deletions src/status_im/subs/wallet/wallet_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -459,3 +459,25 @@
(let [{:keys [formatted-balance tokens]} (rf/sub [sub-name])]
(is (match? 2 (count tokens)))
(is (match? "$4506.00" formatted-balance)))))

(h/deftest-sub :wallet/accounts-with-customization-color
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ajayesivan, I think it's kind of hard to understand what's being tested due to the amount of setup data. The subscription only does one assoc that copies color into a new key customization-color, so the test should try to reflect that simple behavior.

While reviewing this test I just noticed match? has a bug because = works perfectly. cc @yqrashawn if you're not aware already.

(h/deftest-sub :wallet/accounts-with-customization-color
  [sub-name]
  (testing "returns all accounts with `customization-color` copied from `color`"
    (swap! rf-db/app-db
      (fn [db]
        (-> 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])))))

[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])))))