Skip to content

Commit

Permalink
refactor #7571 - decoupled request transaction from send transaction …
Browse files Browse the repository at this point in the history
…flow
  • Loading branch information
goranjovic committed Mar 14, 2019
1 parent 8f0955b commit db7bbb1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/status_im/chat/commands/impl/transactions.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@
:amount-text amount
:amount-error error)
(choose-recipient.events/fill-request-details
(transaction-details recipient-contact symbol))
(transaction-details recipient-contact symbol) false)
(update-in [:wallet :send-transaction]
dissoc :id :password :wrong-password?))
;; TODO(janherich) - refactor wallet send events, updating gas price
Expand Down
38 changes: 20 additions & 18 deletions src/status_im/ui/screens/wallet/choose_recipient/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,24 @@
(defn- find-address-name [db address]
(:name (contact.db/find-contact-by-address (:contacts/contacts db) address)))

(defn- fill-request-details [db {:keys [address name value symbol gas gasPrice public-key from-chat?]}]
(defn- fill-request-details [db {:keys [address name value symbol gas gasPrice public-key from-chat?]} request?]
{:pre [(not (nil? address))]}
(let [name (or name (find-address-name db address))]
(update-in
db [:wallet :send-transaction]
(fn [{old-symbol :symbol :as old-transaction}]
(let [symbol-changed? (not= old-symbol symbol)]
(cond-> (assoc old-transaction :to address :to-name name :public-key public-key)
value (assoc :amount value)
symbol (assoc :symbol symbol)
(and gas symbol-changed?) (assoc :gas (money/bignumber gas))
from-chat? (assoc :from-chat? from-chat?)
(and gasPrice symbol-changed?)
(assoc :gas-price (money/bignumber gasPrice))
(and symbol (not gasPrice) symbol-changed?)
(assoc :gas-price (ethereum/estimate-gas symbol))))))))
(let [name (or name (find-address-name db address))
data-path (if request?
[:wallet :request-transaction]
[:wallet :send-transaction])]
(update-in db data-path
(fn [{old-symbol :symbol :as old-transaction}]
(let [symbol-changed? (not= old-symbol symbol)]
(cond-> (assoc old-transaction :to address :to-name name :public-key public-key)
value (assoc :amount value)
symbol (assoc :symbol symbol)
(and gas symbol-changed?) (assoc :gas (money/bignumber gas))
from-chat? (assoc :from-chat? from-chat?)
(and gasPrice symbol-changed?)
(assoc :gas-price (money/bignumber gasPrice))
(and symbol (not gasPrice) symbol-changed?)
(assoc :gas-price (ethereum/estimate-gas symbol))))))))

(defn- extract-details
"First try to parse as EIP681 URI, if not assume this is an address directly.
Expand Down Expand Up @@ -105,7 +107,7 @@
symbol-changed? (and old-symbol new-symbol (not= old-symbol new-symbol))]
(cond-> {:db db
:dispatch [:navigate-back]}
(and address valid-network?) (update :db #(fill-request-details % details))
(and address valid-network?) (update :db #(fill-request-details % details false))
symbol-changed? (changed-asset old-symbol new-symbol)
(and old-amount new-amount (not= old-amount new-amount)) (changed-amount-warning old-amount new-amount)
;; NOTE(goranjovic) - the next line is there is because QR code scanning switches the amount to ETH
Expand All @@ -119,6 +121,6 @@

(handlers/register-handler-fx
:wallet/fill-request-from-contact
(fn [{db :db} [_ {:keys [address name public-key]}]]
{:db (fill-request-details db {:address address :name name :public-key public-key})
(fn [{db :db} [_ {:keys [address name public-key]} request?]]
{:db (fill-request-details db {:address address :name name :public-key public-key} request?)
:dispatch [:navigate-back]}))
15 changes: 8 additions & 7 deletions src/status_im/ui/screens/wallet/components/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@
:accessibility-label (if request? :contact-address-text :recipient-address-text)}
(ethereum/normalized-address address)]]])))

(defn render-contact [contact]
[list/touchable-item #(re-frame/dispatch [:wallet/fill-request-from-contact contact])
(defn render-contact [contact request?]
[list/touchable-item #(re-frame/dispatch [:wallet/fill-request-from-contact contact request?])
[list/item
[photos/photo (:photo-path contact) {:size list.styles/image-size}]
[list/item-content
Expand All @@ -210,13 +210,14 @@
(ethereum/normalized-address (:address contact))]]]])

(views/defview recent-recipients []
(views/letsubs [contacts [:contacts/active]]
(views/letsubs [contacts [:contacts/active]
{:keys [request?]} [:get-screen-params :recent-recipients]]
[simple-screen
[toolbar (i18n/label :t/recipient)]
[react/view styles/recent-recipients
[list/flat-list {:data contacts
:key-fn :address
:render-fn render-contact}]]]))
:render-fn #(render-contact % request?)}]]]))

(defn contact-code []
(let [content (reagent/atom nil)]
Expand Down Expand Up @@ -251,19 +252,19 @@
(i18n/label :t/camera-access-error)))
50)}]))

(defn- on-choose-recipient [contact-only?]
(defn- on-choose-recipient [contact-only? request?]
(list-selection/show {:title (i18n/label :t/wallet-choose-recipient)
:options (concat
[{:label (i18n/label :t/recent-recipients)
:action #(re-frame/dispatch [:navigate-to :recent-recipients])}]
:action #(re-frame/dispatch [:navigate-to :recent-recipients {:request? request?}])}]
(when-not contact-only?
[{:label (i18n/label :t/scan-qr)
:action request-camera-permissions}
{:label (i18n/label :t/recipient-code)
:action #(re-frame/dispatch [:navigate-to :contact-code])}]))}))

(defn recipient-selector [{:keys [name address disabled? contact-only? request? modal?]}]
[cartouche {:on-press #(on-choose-recipient contact-only?)
[cartouche {:on-press #(on-choose-recipient contact-only? request?)
:disabled? disabled?
:icon :main-icons/more
:icon-opts {:accessibility-label :choose-contact-button}}
Expand Down
10 changes: 6 additions & 4 deletions src/status_im/ui/screens/wallet/request/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@
[status-im.utils.ethereum.eip681 :as eip681]
[status-im.utils.utils :as utils]
[status-im.utils.ethereum.tokens :as tokens]
[status-im.ui.screens.wallet.utils :as wallet.utils]))
[status-im.ui.screens.wallet.utils :as wallet.utils]
[status-im.ui.screens.chat.photos :as photos]
[status-im.ui.components.list.styles :as list.styles]))

;; Request screen

(views/defview send-transaction-request []
;; TODO(jeluard) both send and request flows should be merged
(views/letsubs [network [:account/network]
{:keys [to to-name public-key]} [:wallet.send/transaction]
{:keys [amount amount-error amount-text symbol]} [:wallet.request/transaction]
{:keys [amount amount-error amount-text symbol
to to-name public-key]} [:wallet.request/transaction]
network-status [:network-status]
all-tokens [:wallet/all-tokens]
scroll (atom nil)]
Expand All @@ -43,8 +46,7 @@
[components/recipient-selector {:contact-only? true
:address to
:name to-name
:request? true
:modal? false}]
:request? true}]
[components/asset-selector {:disabled? false
:type :request
:symbol symbol}]
Expand Down

0 comments on commit db7bbb1

Please sign in to comment.