Skip to content

Commit

Permalink
feat: integrate send transaction endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Brian Sztamfater <brian@status.im>
  • Loading branch information
briansztamfater committed Dec 8, 2023
1 parent 4c604ab commit b5a48dc
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 59 deletions.
59 changes: 57 additions & 2 deletions src/status_im2/contexts/wallet/send/events.cljs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
(ns status-im2.contexts.wallet.send.events
(:require
[native-module.core :as native-module]
[status-im2.constants :as constants]
[taoensso.timbre :as log]
[utils.datetime :as datetime]
[utils.money :as money]
[utils.number]
[utils.re-frame :as rf]))
[utils.re-frame :as rf]
[utils.security.core :as security]))

(rf/reg-event-fx :wallet/suggested-routes-success
(fn [{:keys [db]} [suggested-routes timestamp]]
Expand Down Expand Up @@ -38,14 +40,19 @@
{:db (assoc-in db [:wallet :ui :send :token] token)
:fx [[:navigate-to-within-stack [:wallet-send-input-amount stack-id]]]}))

(rf/reg-event-fx :wallet/send-select-amount
(fn [{:keys [db]} [amount stack-id]]
{:db (assoc-in db [:wallet :ui :send :amount] amount)
:fx [[:navigate-to-within-stack [:wallet-transaction-confirmation stack-id]]]}))

(rf/reg-event-fx :wallet/get-suggested-routes
(fn [{:keys [db]} [amount]]
(let [wallet-address (get-in db [:wallet :current-viewing-account-address])
token (get-in db [:wallet :ui :send :token])
to-address (get-in db [:wallet :ui :send :to-address])
token-decimal (:decimals token)
token-id (:symbol token)
network-preferences [constants/mainnet-chain-id] ; TODO: don't hardcode network preferences
network-preferences [constants/mainnet-chain-id]
gas-rates constants/gas-rate-low
amount-in (money/mul (money/bignumber amount)
(money/from-decimal token-decimal))
Expand Down Expand Up @@ -76,3 +83,51 @@
{:event :wallet/get-suggested-routes
:error error
:params request-params}))}]})))

(rf/reg-event-fx :wallet/send-transaction
(fn [{:keys [db]} [password]]
(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])
from (:From route)
token (get-in db [:wallet :ui :send :token])
token-id (:symbol token)
from-asset token-id
to-asset token-id
bridge-name (:BridgeName route)
chain-id (:chainId from)
multi-transaction-command {:fromAddress from-address
:toAddress to-address
:fromAsset from-asset
:toAsset to-asset
:fromAmount (:AmountOut route)
:type 0}
transaction-bridge
[{:BridgeName bridge-name
:ChainID chain-id
:TransferTx {:From from-address
:To to-address
:Gas (money/to-hex (:GasAmount route))
:GasPrice (money/to-hex (money/->wei :gwei
(:gasPrice (:GasFees route))))
:Value (:AmountOut route)
:Nonce nil
:MaxFeePerGas (money/to-hex
(money/->wei :gwei
(:maxFeePerGasMedium (:GasFees route))))
:MaxPriorityFeePerGas (money/to-hex (money/->wei :gwei
(:maxPriorityFeePerGas
(:GasFees
route))))
:Input ""
:Data "0x"}}]
sha3-pwd (native-module/sha3 (str (security/safe-unmask-data password)))
request-params [multi-transaction-command transaction-bridge sha3-pwd]]
{:json-rpc/call [{:method "wallet_createMultiTransaction"
:params request-params
:on-success #(rf/dispatch [:dismiss-modal :wallet-select-address])
:on-error (fn [error]
(log/error "failed to send transaction"
{:event :wallet/send-transaction
:error error
:params request-params}))}]})))
120 changes: 63 additions & 57 deletions src/status_im2/contexts/wallet/send/transaction_confirmation/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
[quo.theme :as quo.theme]
[react-native.core :as rn]
[react-native.safe-area :as safe-area]
[reagent.core :as reagent]
[status-im.utils.utils :as utils]
[status-im2.common.resources :as resources]
[status-im2.common.standard-authentication.core :as standard-auth]
[status-im2.contexts.wallet.send.transaction-confirmation.style :as style]
[utils.i18n :as i18n]
[utils.re-frame :as rf]))

(defn- transaction-title
[]
[token amount account to-address]
[rn/view {:style style/content-container}
[rn/view {:style {:flex-direction :row}}
[quo/text
Expand All @@ -22,7 +23,7 @@
:accessibility-label :send-label}
(i18n/label :t/send)]
[quo/summary-tag
{:label "150 ETH"
{:label (str amount " " (:symbol token))
:type :token
:image-source (quo.resources/get-token :eth)}]]
[rn/view
Expand All @@ -35,10 +36,10 @@
:accessibility-label :send-label}
(i18n/label :t/from)]
[quo/summary-tag
{:label "Collectibles vault"
{:label (:name account)
:type :account
:emoji "🍑"
:customization-color :purple}]]
:emoji (:emoji account)
:customization-color (:color account)}]]
[rn/view
{:style {:flex-direction :row
:margin-top 4}}
Expand All @@ -49,13 +50,11 @@
:accessibility-label :send-label}
(i18n/label :t/to)]
[quo/summary-tag
{:label "Mark Libot"
:type :user
:image-source (resources/get-mock-image :user-picture-male4)
:customization-color :magenta}]]])
{:type :address
:label (utils/get-shortened-address to-address)}]]])

(defn- transaction-from
[status-account-props theme]
[amount account-props theme]
[rn/view
{:style {:padding-horizontal 20
:padding-bottom 16}}
Expand All @@ -68,13 +67,13 @@
[quo/summary-info
{:type :status-account
:networks? true
:values {:ethereum 150
:optimism 50
:arbitrum 25}
:account-props status-account-props}]])
:values {:ethereum amount
:optimism 0
:arbitrum 0}
:account-props account-props}]])

(defn- transaction-to
[user-props theme]
[amount user-props theme]
[rn/view
{:style {:padding-horizontal 20
:padding-bottom 16}}
Expand All @@ -87,13 +86,13 @@
[quo/summary-info
{:type :user
:networks? true
:values {:ethereum 150
:optimism 50
:arbitrum 25}
:values {:ethereum amount
:optimism 0
:arbitrum 0}
:account-props user-props}]])

(defn- transaction-details
[theme]
[estimated-time-min max-fees-eth token amount to-address theme]
[rn/view
{:style {:padding-horizontal 20
:padding-bottom 16}}
Expand All @@ -116,7 +115,7 @@
:status :default
:size :small
:title (i18n/label :t/est-time)
:subtitle "3-5 min"}]
:subtitle (str estimated-time-min " min")}]
[quo/data-item
{:container-style {:flex 1
:height 36}
Expand All @@ -128,7 +127,7 @@
:status :default
:size :small
:title (i18n/label :t/max-fees)
:subtitle "€188,70"}]
:subtitle (str "$" max-fees-eth)}]
[quo/data-item
{:container-style {:flex 1
:height 36}
Expand All @@ -139,35 +138,42 @@
:label :none
:status :default
:size :small
:title (i18n/label :t/user-gets {:name "Mark"})
:subtitle "149.99 ETH"}]]])
:title (i18n/label :t/user-gets {:name (utils/get-shortened-address to-address)})
:subtitle (str amount " " (:symbol token))}]]])

(defn- f-view-internal
[_]
(let [reset-slider? (reagent/atom false)
margin-top (safe-area/get-top)
biometric-auth? true
on-close #(rf/dispatch [:navigate-back-within-stack :wallet-select-asset])
status-account-props {:customization-color :purple
:size 32
:emoji "🍑"
:type :default
:name "Collectibles vault"
:address "0x0ah...78b"}
user-props {:full-name "M L"
:status-indicator? false
:size :small
:ring-background (resources/get-mock-image :ring)
:customization-color :blue
:name "Mark Libot"
:address "0x0ah...78b"
:status-account (merge status-account-props
{:size 16
:name "New house"
:emoji "🍔"})}]
(let [margin-top (safe-area/get-top)
biometric-auth? false
on-close #(rf/dispatch [:navigate-back-within-stack :wallet-select-asset])
send-transaction-data (get-in (rf/sub [:wallet]) [:ui :send])
token (:token send-transaction-data)
amount (:amount send-transaction-data)
route (:route send-transaction-data)
estimated-time-min (:EstimatedTime route)
max-fees-eth 5.28 ; calculate gas price in fiat
to-address (:to-address send-transaction-data)
account (rf/sub [:wallet/current-viewing-account])
account-props {:customization-color (:color account)
:size 32
:emoji (:emoji account)
:type :default
:name (:name account)
:address (utils/get-shortened-address (:address account))}
user-props {:full-name "M L"
:status-indicator? false
:size :small
:ring-background (resources/get-mock-image :ring)
:customization-color :blue
:name "Mark Libot"
:address "0x0ah...78b"
:status-account (merge account-props
{:size 16
:name "New house"
:emoji "🍔"})}]
(fn [{:keys [theme]}]
[rn/view {:style {:flex 1}}
[quo/gradient-cover {:customization-color :purple}]
[quo/gradient-cover {:customization-color (:color account)}]
[rn/view {:style (style/container margin-top)}
[quo/page-nav
{:icon-name :i/arrow-left
Expand All @@ -176,18 +182,18 @@
:right-side [{:icon-name :i/advanced
:on-press (fn callback [] nil)
:accessibility-label "Advanced"}]}]
[transaction-title]
[transaction-from status-account-props theme]
[transaction-to user-props theme]
[transaction-details theme]
[transaction-title token amount account to-address]
[transaction-from amount account-props theme]
[transaction-to amount user-props theme]
[transaction-details estimated-time-min max-fees-eth token amount to-address theme]
[rn/view {:style style/slide-button-container}
[quo/slide-button
{:size :size/s-48
:customization-color :purple
:on-reset (when @reset-slider? #(reset! reset-slider? false))
:on-complete #(js/alert "Not implemented yet")
:track-icon (if biometric-auth? :i/face-id :password)
:track-text (i18n/label :t/slide-to-send)}]]]])))
[standard-auth/slide-button
{:size :size-40
:track-text (i18n/label :t/slide-to-send)
:customization-color (:color account)
:on-enter-password #(rf/dispatch [:wallet/send-transaction %])
:biometric-auth? biometric-auth?
:auth-button-label (i18n/label :t/confirm)}]]]])))

(defn view-internal
[props]
Expand Down

0 comments on commit b5a48dc

Please sign in to comment.