|
1 | 1 | (ns status-im.contexts.wallet.swap.events
|
2 | 2 | (:require [re-frame.core :as rf]
|
3 | 3 | [status-im.constants :as constants]
|
| 4 | + [status-im.contexts.wallet.send.utils :as send-utils] |
4 | 5 | [status-im.contexts.wallet.sheets.network-selection.view :as network-selection]
|
| 6 | + [taoensso.timbre :as log] |
5 | 7 | [utils.number]))
|
6 | 8 |
|
7 | 9 | (rf/reg-event-fx :wallet.swap/start
|
|
14 | 16 | (assoc-in [:wallet :ui :swap :asset-to-pay] token)
|
15 | 17 | (assoc-in [:wallet :ui :swap :network] network))
|
16 | 18 | :fx (if network
|
17 |
| - [[:dispatch [:navigate-to :screen/wallet.swap-propasal]] |
| 19 | + [[:dispatch |
| 20 | + [:navigate-to-within-stack |
| 21 | + [:screen/wallet.setup-swap :screen/wallet.swap-select-asset-to-pay]]] |
18 | 22 | [:dispatch [:wallet.swap/set-default-slippage]]]
|
19 | 23 | [[:dispatch
|
20 | 24 | [:show-bottom-sheet
|
|
30 | 34 | :stack-id
|
31 | 35 | :screen/wallet.swap-select-asset-to-pay}]))}])}]]])}))
|
32 | 36 |
|
33 |
| -(rf/reg-event-fx :wallet.swap/clean-asset-to-pay |
34 |
| - (fn [{:keys [db]}] |
35 |
| - {:db (update-in db [:wallet :ui :swap] dissoc :asset-to-pay)})) |
36 |
| - |
37 | 37 | (rf/reg-event-fx :wallet.swap/set-default-slippage
|
38 | 38 | (fn [{:keys [db]}]
|
39 | 39 | {:db
|
|
47 | 47 | (fn [{:keys [db]} [{:keys [token]}]]
|
48 | 48 | {:db (assoc-in db [:wallet :ui :swap :asset-to-receive] token)}))
|
49 | 49 |
|
50 |
| -(rf/reg-event-fx :wallet.swap/set-pay-amount |
51 |
| - (fn [{:keys [db]} [amount]] |
52 |
| - {:db (assoc-in db [:wallet :ui :swap :pay-amount] amount)})) |
| 50 | +(rf/reg-event-fx :wallet.swap/recalculate-fees |
| 51 | + (fn [{:keys [db]} [loading-fees?]] |
| 52 | + {:db (assoc-in db [:wallet :ui :swap :loading-fees?] loading-fees?)})) |
| 53 | + |
| 54 | +(rf/reg-event-fx :wallet/start-get-swap-proposal |
| 55 | + (fn [{:keys [db]} [{:keys [amount-in amount-out]}]] |
| 56 | + (let [wallet-address (get-in db [:wallet :current-viewing-account-address]) |
| 57 | + {:keys [asset-to-pay asset-to-receive |
| 58 | + network]} (get-in db [:wallet :ui :swap]) |
| 59 | + test-networks-enabled? (get-in db [:profile/profile :test-networks-enabled?]) |
| 60 | + networks ((if test-networks-enabled? :test :prod) |
| 61 | + (get-in db [:wallet :networks])) |
| 62 | + network-chain-ids (map :chain-id networks) |
| 63 | + pay-token-decimal (:decimals asset-to-pay) |
| 64 | + pay-token-id (:symbol asset-to-pay) |
| 65 | + receive-token-id (:symbol asset-to-receive) |
| 66 | + receive-token-decimals (:decimals asset-to-receive) |
| 67 | + gas-rates constants/gas-rate-medium |
| 68 | + amount-in-hex (if amount-in |
| 69 | + (send-utils/amount-in-hex amount-in pay-token-decimal) |
| 70 | + 0) |
| 71 | + amount-out-hex (when amount-out |
| 72 | + (send-utils/amount-in-hex amount-out receive-token-decimals)) |
| 73 | + to-address wallet-address |
| 74 | + from-address wallet-address |
| 75 | + swap-chain-id (:chain-id network) |
| 76 | + disabled-to-chain-ids (filter #(not= % swap-chain-id) network-chain-ids) |
| 77 | + disabled-from-chain-ids (filter #(not= % swap-chain-id) network-chain-ids) |
| 78 | + from-locked-amount {} |
| 79 | + send-type constants/send-type-swap |
| 80 | + request-uuid (str (random-uuid)) |
| 81 | + params [(cond-> |
| 82 | + {:uuid request-uuid |
| 83 | + :sendType send-type |
| 84 | + :addrFrom from-address |
| 85 | + :addrTo to-address |
| 86 | + :tokenID pay-token-id |
| 87 | + :toTokenID receive-token-id |
| 88 | + :disabledFromChainIDs disabled-from-chain-ids |
| 89 | + :disabledToChainIDs disabled-to-chain-ids |
| 90 | + :gasFeeMode gas-rates |
| 91 | + :fromLockedAmount from-locked-amount} |
| 92 | + amount-in (assoc :amountIn amount-in-hex) |
| 93 | + amount-out (assoc :amountOut amount-out-hex))]] |
| 94 | + (when-let [amount (or amount-in amount-out)] |
| 95 | + {:db (update-in db |
| 96 | + [:wallet :ui :swap] |
| 97 | + #(-> % |
| 98 | + (assoc |
| 99 | + :last-request-uuid request-uuid |
| 100 | + :amount amount |
| 101 | + :loading-swap-proposal? true) |
| 102 | + (dissoc :error-response))) |
| 103 | + :json-rpc/call [{:method "wallet_getSuggestedRoutesV2Async" |
| 104 | + :params params |
| 105 | + :on-error (fn [error] |
| 106 | + (rf/dispatch [:wallet/swap-proposal-error error]) |
| 107 | + (log/error "failed to get suggested routes (async)" |
| 108 | + {:event :wallet/start-get-swap-proposal |
| 109 | + :error (:message error) |
| 110 | + :params params}))}]})))) |
53 | 111 |
|
54 |
| -(rf/reg-event-fx :wallet.swap/set-swap-proposal |
| 112 | +(rf/reg-event-fx :wallet/swap-proposal-success |
55 | 113 | (fn [{:keys [db]} [swap-proposal]]
|
56 |
| - {:db (assoc-in db [:wallet :ui :swap :swap-proposal] swap-proposal)})) |
| 114 | + (let [last-request-uuid (get-in db [:wallet :ui :swap :last-request-uuid]) |
| 115 | + request-uuid (:uuid swap-proposal) |
| 116 | + best-routes (:best swap-proposal) |
| 117 | + error-response (:error-response swap-proposal)] |
| 118 | + (when (= request-uuid last-request-uuid) |
| 119 | + {:db (update-in db |
| 120 | + [:wallet :ui :swap] |
| 121 | + assoc |
| 122 | + :swap-proposal (first best-routes) |
| 123 | + :error-response (when (empty? best-routes) error-response) |
| 124 | + :loading-swap-proposal? false)})))) |
57 | 125 |
|
58 |
| -(rf/reg-event-fx :wallet.swap/set-provider |
| 126 | +(rf/reg-event-fx :wallet/swap-proposal-error |
| 127 | + (fn [{:keys [db]} [error-message]] |
| 128 | + {:db (-> db |
| 129 | + (update-in [:wallet :ui :swap] dissoc :route :swap-proposal) |
| 130 | + (assoc-in [:wallet :ui :swap :loading-swap-proposal?] false) |
| 131 | + (assoc-in [:wallet :ui :swap :error-response] error-message)) |
| 132 | + :fx [[:dispatch |
| 133 | + [:toasts/upsert |
| 134 | + {:id :swap-proposal-error |
| 135 | + :type :negative |
| 136 | + :text error-message}]]]})) |
| 137 | + |
| 138 | +(rf/reg-event-fx :wallet/stop-get-swap-proposal |
| 139 | + (fn [] |
| 140 | + {:json-rpc/call [{:method "wallet_stopSuggestedRoutesV2AsyncCalcualtion" |
| 141 | + :params [] |
| 142 | + :on-error (fn [error] |
| 143 | + (log/error "failed to stop fetching swap proposals" |
| 144 | + {:event :wallet/stop-get-swap-proposal |
| 145 | + :error error}))}]})) |
| 146 | + |
| 147 | +(rf/reg-event-fx :wallet/clean-swap-proposal |
59 | 148 | (fn [{:keys [db]}]
|
60 |
| - {:db (assoc-in db [:wallet :ui :swap :providers] [constants/swap-default-provider])})) |
| 149 | + {:db (update-in db |
| 150 | + [:wallet :ui :swap] |
| 151 | + dissoc |
| 152 | + :last-request-uuid |
| 153 | + :swap-proposal |
| 154 | + :error-response |
| 155 | + :loading-swap-proposal?)})) |
61 | 156 |
|
62 |
| -(rf/reg-event-fx :wallet.swap/recalculate-fees |
63 |
| - (fn [{:keys [db]} [loading-fees?]] |
64 |
| - {:db (assoc-in db [:wallet :ui :swap :loading-fees?] loading-fees?)})) |
| 157 | +(rf/reg-event-fx :wallet/clean-swap |
| 158 | + (fn [{:keys [db]}] |
| 159 | + {:db (update-in db [:wallet :ui] dissoc :swap)})) |
0 commit comments