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

feat: add ability to send collectibles from wallet #18473

Merged
merged 1 commit into from
Jan 16, 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
8 changes: 4 additions & 4 deletions src/quo/components/wallet/summary_info/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@
[rn/view
{:style style/networks-container
:accessibility-label :networks}
(when (pos? ethereum)
(when (and ethereum (pos? (:amount ethereum)))
[network-amount
{:network :ethereum
:amount (str ethereum " ETH")
:amount (str (:amount ethereum) " " (or (:token-symbol ethereum) "ETH"))
:divider? (or show-arbitrum? show-optimism?)
:theme theme}])
(when show-optimism?
[network-amount
{:network :optimism
:amount (str optimism " OPT")
:amount (str (:amount optimism) " " (or (:token-symbol optimism) "OPT"))
:divider? show-arbitrum?
:theme theme}])
(when show-arbitrum?
[network-amount
{:network :arbitrum
:amount (str arbitrum " ARB")
:amount (str (:amount arbitrum) " " (or (:token-symbol arbitrum) "ARB"))
:theme theme}])]))
Comment on lines -36 to 52
Copy link
Contributor

@OmarBasem OmarBasem Jan 12, 2024

Choose a reason for hiding this comment

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

There seems to be unnecessary duplication here. I think this can be a loop that includes the networks and which ones needs to be shown

Copy link
Member Author

Choose a reason for hiding this comment

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

I totally agree @OmarBasem, we can definitely improve this component, but I think that is beyond the scope of this PR so I'll create an issue to keep track of this improvement!

Copy link
Contributor

Choose a reason for hiding this comment

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

yep let's track this in a separate issue 👍


(defn- view-internal
Expand Down
3 changes: 3 additions & 0 deletions src/status_im/constants.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -432,3 +432,6 @@
(def ^:const send-type-stickers-buy 4)
(def ^:const send-type-bridge 5)
(def ^:const send-type-erc-721-transfer 6)

(def ^:const bridge-name-transfer "Transfer")
(def ^:const bridge-name-erc-721-transfer "ERC721Transfer")
6 changes: 3 additions & 3 deletions src/status_im/contexts/preview/quo/wallet/summary_info.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
[]
(let [state (reagent/atom {:type :status-account
:networks? true
:values {:ethereum 150
:optimism 50
:arbitrum 25}})
:values {:ethereum {:amount 150}
:optimism {:amount 50}
:arbitrum {:amount 25}}})
status-account-props {:customization-color :purple
:size 32
:emoji "🍑"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
:style {:flex 1}
:content-container-style {:align-items :center}
:num-columns 2
:render-fn (fn [{:keys [preview-url id]}]
:render-fn (fn [{:keys [preview-url] :as collectible}]
[quo/collectible
{:images [preview-url]
:on-press #(on-collectible-press id)}])}])))
:on-press #(on-collectible-press collectible)}])}])))

(def view (quo.theme/with-theme view-internal))
8 changes: 1 addition & 7 deletions src/status_im/contexts/wallet/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@
(rf/defn clean-scanned-address
{:events [:wallet/clean-scanned-address]}
[{:keys [db]}]
{:db (-> db
(dissoc :wallet/scanned-address :wallet/send-address)
(update-in [:wallet :ui :send] dissoc :to-address))})
{:db (dissoc db :wallet/scanned-address :wallet/send-address)})

(rf/reg-event-fx :wallet/create-derived-addresses
(fn [{:keys [db]} [{:keys [sha3-pwd path]} on-success]]
Expand Down Expand Up @@ -301,10 +299,6 @@
(background-timer/clear-timeout current-timeout)
{:db (assoc db :wallet/local-suggestions [] :wallet/valid-ens-or-address? false)})))

(rf/reg-event-fx :wallet/clean-account-selection
(fn [{:keys [db]}]
{:db (update-in db [:wallet :ui :send] dissoc :send-account-address)}))

(rf/reg-event-fx :wallet/get-address-details-success
(fn [{:keys [db]} [{:keys [hasActivity]}]]
{:db (assoc-in db
Expand Down
135 changes: 93 additions & 42 deletions src/status_im/contexts/wallet/send/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@
(update-in [:wallet :ui :send] dissoc :route)
(update-in [:wallet :ui :send] dissoc :loading-suggested-routes?))}))


(rf/reg-event-fx :wallet/select-send-account-address
(fn [{:keys [db]} [{:keys [address stack-id]}]]
{:db (-> db
(assoc-in [:wallet :ui :send :send-account-address] address)
(update-in [:wallet :ui :send] dissoc :to-address))
:fx [[:navigate-to-within-stack [:wallet-select-asset stack-id]]]}))
(rf/reg-event-fx :wallet/clean-send-address
Copy link
Member

Choose a reason for hiding this comment

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

Much simpler 🙌

(fn [{:keys [db]}]
{:db (update-in db [:wallet :ui :send] dissoc :recipient :to-address)}))

(rf/reg-event-fx :wallet/select-send-address
(fn [{:keys [db]} [{:keys [address token stack-id]}]]
{:db (assoc-in db [:wallet :ui :send :to-address] address)
(fn [{:keys [db]} [{:keys [address token recipient stack-id]}]]
{:db (-> db
(assoc-in [:wallet :ui :send :recipient] (or recipient address))
(assoc-in [:wallet :ui :send :to-address] address))
:fx [[:navigate-to-within-stack
(if token [:wallet-send-input-amount stack-id] [:wallet-select-asset stack-id])]]}))

(rf/reg-event-fx :wallet/send-select-token
(fn [{:keys [db]} [{:keys [token stack-id]}]]
{:db (assoc-in db [:wallet :ui :send :token] token)
{:db (-> db
(update-in [:wallet :ui :send] dissoc :collectible)
(assoc-in [:wallet :ui :send :token] token))
:fx [[:navigate-to-within-stack [:wallet-send-input-amount stack-id]]]}))

(rf/reg-event-fx :wallet/send-select-token-drawer
Expand All @@ -67,6 +67,15 @@
(fn [{:keys [db]}]
{:db (assoc-in db [:wallet :ui :send :token] nil)}))

(rf/reg-event-fx :wallet/send-select-collectible
(fn [{:keys [db]} [{:keys [collectible stack-id]}]]
{:db (-> db
(update-in [:wallet :ui :send] dissoc :token)
(assoc-in [:wallet :ui :send :collectible] collectible)
(assoc-in [:wallet :ui :send :amount] 1))
:fx [[:dispatch [:wallet/get-suggested-routes 1]]
[:navigate-to-within-stack [:wallet-transaction-confirmation stack-id]]]}))

(rf/reg-event-fx :wallet/send-select-amount
(fn [{:keys [db]} [{:keys [amount stack-id]}]]
{:db (assoc-in db [:wallet :ui :send :amount] amount)
Expand All @@ -76,18 +85,25 @@
(fn [{:keys [db now]} [amount]]
(let [wallet-address (get-in db [:wallet :current-viewing-account-address])
token (get-in db [:wallet :ui :send :token])
account-address (get-in db [:wallet :ui :send :send-account-address])
to-address (or account-address (get-in db [:wallet :ui :send :to-address]))
token-decimal (:decimals token)
token-id (:symbol token)
network-preferences []
collectible (get-in db [:wallet :ui :send :collectible])
to-address (get-in db [:wallet :ui :send :to-address])
token-decimal (when token (:decimals token))
token-id (if token
(:symbol token)
(str (get-in collectible [:id :contract-id :address])
":"
(get-in collectible [:id :token-id])))
network-preferences (if token [] [(get-in collectible [:id :contract-id :chain-id])])
gas-rates constants/gas-rate-medium
amount-in (send-utils/amount-in-hex amount token-decimal)
amount-in (send-utils/amount-in-hex amount (if token token-decimal 0))
from-address wallet-address
disabled-from-chain-ids []
disabled-to-chain-ids []
from-locked-amount {}
request-params [constants/send-type-transfer
transaction-type (if token
constants/send-type-transfer
constants/send-type-erc-721-transfer)
request-params [transaction-type
from-address
to-address
amount-in
Expand Down Expand Up @@ -124,21 +140,46 @@
:fx [[:dispatch [:navigate-to :wallet-transaction-progress]]]})))

(defn- transaction-bridge
[{:keys [from-address to-address route]}]
(let [{:keys [from bridge-name amount-out gas-amount gas-fees]} route
{:keys [gas-price max-fee-per-gas-medium max-priority-fee-per-gas]} gas-fees]
[{:BridgeName bridge-name
:ChainID (:chain-id from)
:TransferTx {:From from-address
:To to-address
:Gas (money/to-hex gas-amount)
:GasPrice (money/to-hex (money/->wei :gwei gas-price))
:Value amount-out
:Nonce nil
:MaxFeePerGas (money/to-hex (money/->wei :gwei max-fee-per-gas-medium))
:MaxPriorityFeePerGas (money/to-hex (money/->wei :gwei max-priority-fee-per-gas))
:Input ""
:Data "0x"}}]))
[{:keys [from-address to-address token-id token-address route]}]
(let [{:keys [from bridge-name amount-out gas-amount
gas-fees]} route
eip-1559-enabled? (:eip-1559-enabled gas-fees)
{:keys [gas-price max-fee-per-gas-medium
max-priority-fee-per-gas]} gas-fees
transfer-tx (cond-> {:From from-address
:To (or token-address to-address)
:Gas (money/to-hex gas-amount)
:Value amount-out
:Nonce nil
:Input ""
:Data "0x"}
eip-1559-enabled? (assoc :TxType "0x02"
:MaxFeePerGas
(money/to-hex
(money/->wei
:gwei
max-fee-per-gas-medium))
:MaxPriorityFeePerGas
(money/to-hex
(money/->wei
:gwei
max-priority-fee-per-gas)))
(not eip-1559-enabled?) (assoc :TxType "0x00"
:GasPrice (money/to-hex
(money/->wei
:gwei
gas-price))))]
[(cond-> {:BridgeName bridge-name
:ChainID (:chain-id from)}

(= bridge-name constants/bridge-name-erc-721-transfer)
(assoc :ERC721TransferTx
(assoc transfer-tx
:Recipient to-address
:TokenID token-id))

(= bridge-name constants/bridge-name-transfer)
(assoc :TransferTx transfer-tx))]))

(defn- multi-transaction-command
[{:keys [from-address to-address from-asset to-asset amount-out transfer-type]
Expand All @@ -154,17 +195,27 @@
(fn [{:keys [db]} [sha3-pwd]]
(let [route (get-in db [:wallet :ui :send :route])
from-address (get-in db [:wallet :current-viewing-account-address])
to-address (get-in db [:wallet :ui :send :to-address])
token (get-in db [:wallet :ui :send :token])
token-id (:symbol token)
request-params [(multi-transaction-command {:from-address from-address
:to-address to-address
:from-asset token-id
:to-asset token-id
:amount-out (:amount-out route)})
(transaction-bridge {:to-address to-address
:from-address from-address
:route route})
collectible (get-in db [:wallet :ui :send :collectible])
token-address (when collectible
(get-in collectible
[:id :contract-id :address]))
to-address (get-in db [:wallet :ui :send :to-address])
token-id (if token
(:symbol token)
(get-in collectible [:id :token-id]))
request-params [(multi-transaction-command
{:from-address from-address
:to-address to-address
:from-asset token-id
:to-asset token-id
:amount-out (:amount-out route)})
(transaction-bridge {:to-address to-address
:from-address from-address
:route route
:token-address token-address
:token-id (when collectible
(money/to-hex (js/parseInt token-id)))})
sha3-pwd]]
{:json-rpc/call [{:method "wallet_createMultiTransaction"
:params request-params
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
[{:keys [color address] :as account}]
[quo/account-item
{:account-props (assoc account :customization-color color)
:on-press #(rf/dispatch [:wallet/select-send-account-address
{:address address
:stack-id :wallet-select-address}])}])
:on-press #(rf/dispatch [:wallet/select-send-address
{:address address
:recipient account
:stack-id :wallet-select-address}])}])

(defn my-accounts
[theme]
Expand Down
24 changes: 13 additions & 11 deletions src/status_im/contexts/wallet/send/select_address/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
(defn- address-input
[input-value input-focused?]
(fn []
(let [scanned-address (rf/sub [:wallet/scanned-address])
send-address (rf/sub [:wallet/wallet-send-to-address])
valid-ens-or-address? (rf/sub [:wallet/valid-ens-or-address?])
chain-id (rf/sub [:chain-id])
contacts (rf/sub [:contacts/active])]
(let [scanned-address (rf/sub [:wallet/scanned-address])
send-address (rf/sub [:wallet/wallet-send-to-address])
recipient (rf/sub [:wallet/wallet-send-recipient])
recipient-plain-address? (= send-address recipient)
valid-ens-or-address? (rf/sub [:wallet/valid-ens-or-address?])
chain-id (rf/sub [:chain-id])
contacts (rf/sub [:contacts/active])]
[quo/address-input
{:on-focus #(reset! input-focused? true)
:on-blur #(reset! input-focused? false)
Expand All @@ -37,7 +39,7 @@
(rf/dispatch [:open-modal :scan-address]))
:ens-regex constants/regx-ens
:address-regex constants/regx-address
:scanned-value (or send-address scanned-address)
:scanned-value (or (when recipient-plain-address? send-address) scanned-address)
:on-detect-address #(debounce/debounce-and-dispatch
[:wallet/validate-address %]
300)
Expand Down Expand Up @@ -80,8 +82,9 @@
(let [address (if accounts (:address (first accounts)) address)]
(when-not ens
(rf/dispatch [:wallet/select-send-address
{:address address
:stack-id :wallet-select-address}]))))
{:address address
:recipient local-suggestion
:stack-id :wallet-select-address}]))))
:active-state? false}]
(cond
(= type types/saved-address)
Expand Down Expand Up @@ -117,8 +120,8 @@
(let [on-close (fn []
(rf/dispatch [:wallet/clean-scanned-address])
(rf/dispatch [:wallet/clean-local-suggestions])
(rf/dispatch [:wallet/clean-account-selection])
(rf/dispatch [:wallet/clean-selected-token])
(rf/dispatch [:wallet/clean-send-address])
(rf/dispatch [:wallet/select-address-tab nil])
(rf/dispatch [:navigate-back]))
on-change-tab #(rf/dispatch [:wallet/select-address-tab %])
Expand All @@ -131,8 +134,7 @@
(rn/use-effect (fn []
(fn []
(rf/dispatch [:wallet/clean-scanned-address])
(rf/dispatch [:wallet/clean-local-suggestions])
(rf/dispatch [:wallet/clean-account-selection]))))
(rf/dispatch [:wallet/clean-local-suggestions]))))
[floating-button-page/view
{:header [account-switcher/view
{:on-press on-close
Expand Down
7 changes: 3 additions & 4 deletions src/status_im/contexts/wallet/send/select_asset/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@
[collectibles-tab/view
{:collectibles collectibles
:filtered? search-performed?
:on-collectible-press (fn [collectible-id]
(js/alert (str "Collectible to send: \n"
collectible-id
"\nNavigation not implemented yet")))}]))
:on-collectible-press #(rf/dispatch [:wallet/send-select-collectible
{:collectible %
:stack-id :wallet-select-asset}])}]))

(defn- tab-view
[search-text selected-tab on-change-text]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
{:margin-right 4})

(defn details-container
[theme]
[route? theme]
{:flex-direction :row
:justify-content :space-between
:justify-content (if route? :space-between :center)
:height 52
:padding-horizontal 12
:padding-top 7
Expand Down
Loading