|
90 | 90 |
|
91 | 91 | (rf/reg-event-fx :wallet/clean-selected-token
|
92 | 92 | (fn [{:keys [db]}]
|
93 |
| - {:db (assoc-in db [:wallet :ui :send :token] nil)})) |
| 93 | + {:db (update-in db [:wallet :ui :send] dissoc :token :tx-type)})) |
94 | 94 |
|
95 | 95 | (rf/reg-event-fx :wallet/clean-selected-collectible
|
96 | 96 | (fn [{:keys [db]}]
|
97 |
| - (let [type (get-in db [:wallet :ui :send :type])] |
| 97 | + (let [transaction-type (get-in db [:wallet :ui :send :tx-type])] |
98 | 98 | {:db (update-in db
|
99 | 99 | [:wallet :ui :send]
|
100 | 100 | dissoc
|
101 | 101 | :collectible
|
102 | 102 | :amount
|
103 |
| - (when (= type :collecible) :type))}))) |
| 103 | + (when (= transaction-type :collecible) :tx-type))}))) |
104 | 104 |
|
105 | 105 | (rf/reg-event-fx :wallet/send-select-collectible
|
106 | 106 | (fn [{:keys [db]} [{:keys [collectible stack-id]}]]
|
107 | 107 | {:db (-> db
|
108 | 108 | (update-in [:wallet :ui :send] dissoc :token)
|
109 | 109 | (assoc-in [:wallet :ui :send :collectible] collectible)
|
110 |
| - (assoc-in [:wallet :ui :send :type] :collectible) |
| 110 | + (assoc-in [:wallet :ui :send :tx-type] :collectible) |
111 | 111 | (assoc-in [:wallet :ui :send :amount] 1))
|
112 | 112 | :fx [[:dispatch [:wallet/get-suggested-routes {:amount 1}]]
|
113 | 113 | [:navigate-to-within-stack [:screen/wallet.transaction-confirmation stack-id]]]}))
|
|
121 | 121 | (fn [{:keys [db now]} [{:keys [amount]}]]
|
122 | 122 | (let [wallet-address (get-in db [:wallet :current-viewing-account-address])
|
123 | 123 | token (get-in db [:wallet :ui :send :token])
|
124 |
| - transaction-type (get-in db [:wallet :ui :send :type]) |
| 124 | + transaction-type (get-in db [:wallet :ui :send :tx-type]) |
125 | 125 | collectible (get-in db [:wallet :ui :send :collectible])
|
126 | 126 | to-address (get-in db [:wallet :ui :send :to-address])
|
127 | 127 | test-networks-enabled? (get-in db [:profile/profile :test-networks-enabled?])
|
|
191 | 191 | (fn [_]
|
192 | 192 | {:fx [[:dispatch [:dismiss-modal :screen/wallet.transaction-progress]]]}))
|
193 | 193 |
|
194 |
| -(defn- transaction-bridge |
195 |
| - [{:keys [from-address from-chain-id to-address token-id token-address route data eth-transfer?]}] |
196 |
| - (let [{:keys [bridge-name amount-out gas-amount |
197 |
| - gas-fees]} route |
198 |
| - eip-1559-enabled? (:eip-1559-enabled gas-fees) |
| 194 | +(defn- transaction-data |
| 195 | + [{:keys [from-address to-address token-address route data eth-transfer?]}] |
| 196 | + (let [{:keys [amount-in gas-amount gas-fees]} route |
| 197 | + eip-1559-enabled? (:eip-1559-enabled gas-fees) |
199 | 198 | {:keys [gas-price max-fee-per-gas-medium
|
200 |
| - max-priority-fee-per-gas]} gas-fees |
201 |
| - transfer-tx (cond-> {:From from-address |
202 |
| - :To (or token-address to-address) |
203 |
| - :Gas (money/to-hex gas-amount) |
204 |
| - :Value (when eth-transfer? amount-out) |
205 |
| - :Nonce nil |
206 |
| - :Input "" |
207 |
| - :Data (or data "0x")} |
208 |
| - eip-1559-enabled? (assoc :TxType "0x02" |
209 |
| - :MaxFeePerGas |
210 |
| - (money/to-hex |
211 |
| - (money/->wei |
212 |
| - :gwei |
213 |
| - max-fee-per-gas-medium)) |
214 |
| - :MaxPriorityFeePerGas |
215 |
| - (money/to-hex |
216 |
| - (money/->wei |
217 |
| - :gwei |
218 |
| - max-priority-fee-per-gas))) |
219 |
| - (not eip-1559-enabled?) (assoc :TxType "0x00" |
220 |
| - :GasPrice (money/to-hex |
221 |
| - (money/->wei |
222 |
| - :gwei |
223 |
| - gas-price))))] |
224 |
| - [(cond-> {:BridgeName bridge-name |
225 |
| - :ChainID from-chain-id} |
| 199 | + max-priority-fee-per-gas]} gas-fees] |
| 200 | + (cond-> {:From from-address |
| 201 | + :To (or token-address to-address) |
| 202 | + :Gas (money/to-hex gas-amount) |
| 203 | + :Value (when eth-transfer? amount-in) |
| 204 | + :Nonce nil |
| 205 | + :Input "" |
| 206 | + :Data (or data "0x")} |
| 207 | + eip-1559-enabled? (assoc |
| 208 | + :TxType "0x02" |
| 209 | + :MaxFeePerGas |
| 210 | + (money/to-hex |
| 211 | + (money/->wei |
| 212 | + :gwei |
| 213 | + max-fee-per-gas-medium)) |
| 214 | + :MaxPriorityFeePerGas |
| 215 | + (money/to-hex |
| 216 | + (money/->wei |
| 217 | + :gwei |
| 218 | + max-priority-fee-per-gas))) |
| 219 | + (not eip-1559-enabled?) (assoc :TxType "0x00" |
| 220 | + :GasPrice |
| 221 | + (money/to-hex |
| 222 | + (money/->wei |
| 223 | + :gwei |
| 224 | + gas-price)))))) |
226 | 225 |
|
227 |
| - (= bridge-name constants/bridge-name-erc-721-transfer) |
228 |
| - (assoc :ERC721TransferTx |
229 |
| - (assoc transfer-tx |
230 |
| - :Recipient to-address |
231 |
| - :TokenID token-id)) |
| 226 | +(defn- transaction-path |
| 227 | + [{:keys [from-address to-address token-id token-address route data eth-transfer?]}] |
| 228 | + (let [{:keys [bridge-name amount-in bonder-fees from |
| 229 | + to]} route |
| 230 | + tx-data (transaction-data {:from-address from-address |
| 231 | + :to-address to-address |
| 232 | + :token-address token-address |
| 233 | + :route route |
| 234 | + :data data |
| 235 | + :eth-transfer? eth-transfer?}) |
| 236 | + to-chain-id (:chain-id to) |
| 237 | + from-chain-id (:chain-id from)] |
| 238 | + (cond-> {:BridgeName bridge-name |
| 239 | + :ChainID from-chain-id} |
232 | 240 |
|
233 |
| - (= bridge-name constants/bridge-name-transfer) |
234 |
| - (assoc :TransferTx transfer-tx))])) |
| 241 | + (= bridge-name constants/bridge-name-erc-721-transfer) |
| 242 | + (assoc :ERC721TransferTx |
| 243 | + (assoc tx-data |
| 244 | + :Recipient to-address |
| 245 | + :TokenID token-id |
| 246 | + :ChainID to-chain-id)) |
| 247 | + |
| 248 | + (= bridge-name constants/bridge-name-transfer) |
| 249 | + (assoc :TransferTx tx-data) |
| 250 | + |
| 251 | + (= bridge-name constants/bridge-name-hop) |
| 252 | + (assoc :HopTx |
| 253 | + (assoc tx-data |
| 254 | + :ChainID to-chain-id |
| 255 | + :Symbol token-id |
| 256 | + :Recipient to-address |
| 257 | + :Amount amount-in |
| 258 | + :BonderFee bonder-fees)) |
| 259 | + |
| 260 | + (not (or (= bridge-name constants/bridge-name-erc-721-transfer) |
| 261 | + (= bridge-name constants/bridge-name-transfer) |
| 262 | + (= bridge-name constants/bridge-name-hop))) |
| 263 | + (assoc :CbridgeTx |
| 264 | + (assoc tx-data |
| 265 | + :ChainID to-chain-id |
| 266 | + :Symbol token-id |
| 267 | + :Recipient to-address |
| 268 | + :Amount amount-in))))) |
235 | 269 |
|
236 | 270 | (defn- multi-transaction-command
|
237 | 271 | [{:keys [from-address to-address from-asset to-asset amount-out transfer-type]
|
|
245 | 279 |
|
246 | 280 | (rf/reg-event-fx :wallet/send-transaction
|
247 | 281 | (fn [{:keys [db]} [sha3-pwd]]
|
248 |
| - (let [route (first (get-in db [:wallet :ui :send :route])) |
249 |
| - from-address (get-in db [:wallet :current-viewing-account-address]) |
250 |
| - token (get-in db [:wallet :ui :send :token]) |
251 |
| - collectible (get-in db [:wallet :ui :send :collectible]) |
252 |
| - from-chain-id (get-in route [:from :chain-id]) |
253 |
| - token-id (if token |
254 |
| - (:symbol token) |
255 |
| - (get-in collectible [:id :token-id])) |
| 282 | + (let [routes (get-in db [:wallet :ui :send :route]) |
| 283 | + first-route (first routes) |
| 284 | + from-address (get-in db [:wallet :current-viewing-account-address]) |
| 285 | + transaction-type (get-in db [:wallet :ui :send :tx-type]) |
| 286 | + transaction-type-param (case transaction-type |
| 287 | + :collectible constants/send-type-erc-721-transfer |
| 288 | + :bridge constants/send-type-bridge |
| 289 | + constants/send-type-transfer) |
| 290 | + token (get-in db [:wallet :ui :send :token]) |
| 291 | + collectible (get-in db [:wallet :ui :send :collectible]) |
| 292 | + first-route-from-chain-id (get-in first-route [:from :chain-id]) |
| 293 | + token-id (if token |
| 294 | + (:symbol token) |
| 295 | + (get-in collectible [:id :token-id])) |
256 | 296 | erc20-transfer? (and token (not= token-id "ETH"))
|
257 |
| - eth-transfer? (and token (not erc20-transfer?)) |
258 |
| - token-address (cond collectible |
259 |
| - (get-in collectible |
260 |
| - [:id :contract-id :address]) |
261 |
| - erc20-transfer? |
262 |
| - (get-in token [:balances-per-chain from-chain-id :address])) |
263 |
| - to-address (get-in db [:wallet :ui :send :to-address]) |
264 |
| - data (when erc20-transfer? |
265 |
| - (native-module/encode-transfer (address/normalized-hex to-address) |
266 |
| - (:amount-out route))) |
267 |
| - request-params [(multi-transaction-command |
268 |
| - {:from-address from-address |
269 |
| - :to-address to-address |
270 |
| - :from-asset token-id |
271 |
| - :to-asset token-id |
272 |
| - :amount-out (if eth-transfer? (:amount-out route) "0x0")}) |
273 |
| - (transaction-bridge {:to-address to-address |
274 |
| - :from-address from-address |
275 |
| - :route route |
276 |
| - :from-chain-id from-chain-id |
277 |
| - :token-address token-address |
278 |
| - :token-id (when collectible |
279 |
| - (money/to-hex (js/parseInt token-id))) |
280 |
| - :data data |
281 |
| - :eth-transfer? eth-transfer?}) |
282 |
| - sha3-pwd]] |
| 297 | + eth-transfer? (and token (not erc20-transfer?)) |
| 298 | + token-address (cond collectible |
| 299 | + (get-in collectible |
| 300 | + [:id :contract-id :address]) |
| 301 | + erc20-transfer? |
| 302 | + (get-in token [:balances-per-chain first-route-from-chain-id :address])) |
| 303 | + to-address (get-in db [:wallet :ui :send :to-address]) |
| 304 | + transaction-paths (mapv (fn [route] |
| 305 | + (let [data (when erc20-transfer? |
| 306 | + (native-module/encode-transfer |
| 307 | + (address/normalized-hex to-address) |
| 308 | + (:amount-in route)))] |
| 309 | + (transaction-path {:to-address to-address |
| 310 | + :from-address from-address |
| 311 | + :route route |
| 312 | + :token-address token-address |
| 313 | + :token-id (if collectible |
| 314 | + (money/to-hex (js/parseInt |
| 315 | + token-id)) |
| 316 | + token-id) |
| 317 | + :data data |
| 318 | + :eth-transfer? eth-transfer?}))) |
| 319 | + routes) |
| 320 | + request-params |
| 321 | + [(multi-transaction-command |
| 322 | + {:from-address from-address |
| 323 | + :to-address to-address |
| 324 | + :from-asset token-id |
| 325 | + :to-asset token-id |
| 326 | + :amount-out (if eth-transfer? (:amount-out first-route) "0x0") |
| 327 | + :transfer-type transaction-type-param}) |
| 328 | + transaction-paths |
| 329 | + sha3-pwd]] |
283 | 330 | {:json-rpc/call [{:method "wallet_createMultiTransaction"
|
284 | 331 | :params request-params
|
285 | 332 | :on-success (fn [result]
|
|
293 | 340 | (log/error "failed to send transaction"
|
294 | 341 | {:event :wallet/send-transaction
|
295 | 342 | :error error
|
296 |
| - :params request-params}))}]}))) |
| 343 | + :params request-params}) |
| 344 | + (rf/dispatch [:toasts/upsert |
| 345 | + {:id :send-transaction-error |
| 346 | + :type :negative |
| 347 | + :text (:message error)}]))}]}))) |
0 commit comments