feat: enable sending erc20 tokens (#18481)

Signed-off-by: Brian Sztamfater <brian@status.im>
This commit is contained in:
Brian Sztamfater 2024-01-16 12:02:31 -03:00 committed by GitHub
parent ac07109e86
commit 455a739318
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 47 additions and 30 deletions

View File

@ -2,9 +2,11 @@
(:require (:require
[camel-snake-kebab.core :as csk] [camel-snake-kebab.core :as csk]
[camel-snake-kebab.extras :as cske] [camel-snake-kebab.extras :as cske]
[native-module.core :as native-module]
[status-im.constants :as constants] [status-im.constants :as constants]
[status-im.contexts.wallet.send.utils :as send-utils] [status-im.contexts.wallet.send.utils :as send-utils]
[taoensso.timbre :as log] [taoensso.timbre :as log]
[utils.address :as address]
[utils.money :as money] [utils.money :as money]
[utils.number] [utils.number]
[utils.re-frame :as rf])) [utils.re-frame :as rf]))
@ -140,8 +142,8 @@
:fx [[:dispatch [:navigate-to :wallet-transaction-progress]]]}))) :fx [[:dispatch [:navigate-to :wallet-transaction-progress]]]})))
(defn- transaction-bridge (defn- transaction-bridge
[{:keys [from-address to-address token-id token-address route]}] [{:keys [from-address from-chain-id to-address token-id token-address route data eth-transfer?]}]
(let [{:keys [from bridge-name amount-out gas-amount (let [{:keys [bridge-name amount-out gas-amount
gas-fees]} route gas-fees]} route
eip-1559-enabled? (:eip-1559-enabled gas-fees) eip-1559-enabled? (:eip-1559-enabled gas-fees)
{:keys [gas-price max-fee-per-gas-medium {:keys [gas-price max-fee-per-gas-medium
@ -149,10 +151,10 @@
transfer-tx (cond-> {:From from-address transfer-tx (cond-> {:From from-address
:To (or token-address to-address) :To (or token-address to-address)
:Gas (money/to-hex gas-amount) :Gas (money/to-hex gas-amount)
:Value amount-out :Value (when eth-transfer? amount-out)
:Nonce nil :Nonce nil
:Input "" :Input ""
:Data "0x"} :Data (or data "0x")}
eip-1559-enabled? (assoc :TxType "0x02" eip-1559-enabled? (assoc :TxType "0x02"
:MaxFeePerGas :MaxFeePerGas
(money/to-hex (money/to-hex
@ -170,7 +172,7 @@
:gwei :gwei
gas-price))))] gas-price))))]
[(cond-> {:BridgeName bridge-name [(cond-> {:BridgeName bridge-name
:ChainID (:chain-id from)} :ChainID from-chain-id}
(= bridge-name constants/bridge-name-erc-721-transfer) (= bridge-name constants/bridge-name-erc-721-transfer)
(assoc :ERC721TransferTx (assoc :ERC721TransferTx
@ -193,35 +195,50 @@
(rf/reg-event-fx :wallet/send-transaction (rf/reg-event-fx :wallet/send-transaction
(fn [{:keys [db]} [sha3-pwd]] (fn [{:keys [db]} [sha3-pwd]]
(let [route (get-in db [:wallet :ui :send :route]) (let [route (get-in db [:wallet :ui :send :route])
from-address (get-in db [:wallet :current-viewing-account-address]) from-address (get-in db [:wallet :current-viewing-account-address])
token (get-in db [:wallet :ui :send :token]) token (get-in db [:wallet :ui :send :token])
collectible (get-in db [:wallet :ui :send :collectible]) collectible (get-in db [:wallet :ui :send :collectible])
token-address (when collectible from-chain-id (get-in route [:from :chain-id])
(get-in collectible token-id (if token
[:id :contract-id :address])) (:symbol token)
to-address (get-in db [:wallet :ui :send :to-address]) (get-in collectible [:id :token-id]))
token-id (if token erc20-transfer? (and token (not= token-id "ETH"))
(:symbol token) eth-transfer? (and token (not erc20-transfer?))
(get-in collectible [:id :token-id])) token-address (cond collectible
request-params [(multi-transaction-command (get-in collectible
{:from-address from-address [:id :contract-id :address])
:to-address to-address erc20-transfer?
:from-asset token-id (get-in token [:balances-per-chain from-chain-id :address]))
:to-asset token-id to-address (get-in db [:wallet :ui :send :to-address])
:amount-out (:amount-out route)}) data (when erc20-transfer?
(transaction-bridge {:to-address to-address (native-module/encode-transfer (address/normalized-hex to-address)
:from-address from-address (:amount-out route)))
:route route request-params [(multi-transaction-command
:token-address token-address {:from-address from-address
:token-id (when collectible :to-address to-address
(money/to-hex (js/parseInt token-id)))}) :from-asset token-id
sha3-pwd]] :to-asset token-id
:amount-out (if eth-transfer? (:amount-out route) "0x0")})
(transaction-bridge {:to-address to-address
:from-address from-address
:route route
:from-chain-id from-chain-id
:token-address token-address
:token-id (when collectible
(money/to-hex (js/parseInt token-id)))
:data data
:eth-transfer? eth-transfer?})
sha3-pwd]]
{:json-rpc/call [{:method "wallet_createMultiTransaction" {:json-rpc/call [{:method "wallet_createMultiTransaction"
:params request-params :params request-params
:on-success (fn [result] :on-success (fn [result]
(rf/dispatch [:hide-bottom-sheet]) (rf/dispatch [:hide-bottom-sheet])
(rf/dispatch [:wallet/add-authorized-transaction result])) (rf/dispatch [:wallet/add-authorized-transaction result])
(rf/dispatch [:wallet/clean-scanned-address])
(rf/dispatch [:wallet/clean-local-suggestions])
(rf/dispatch [:wallet/clean-send-address])
(rf/dispatch [:wallet/select-address-tab nil]))
:on-error (fn [error] :on-error (fn [error]
(log/error "failed to send transaction" (log/error "failed to send transaction"
{:event :wallet/send-transaction {:event :wallet/send-transaction