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
@ -197,31 +199,46 @@
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
[:id :contract-id :address]))
to-address (get-in db [:wallet :ui :send :to-address])
token-id (if token token-id (if token
(:symbol token) (:symbol token)
(get-in collectible [:id :token-id])) (get-in collectible [:id :token-id]))
erc20-transfer? (and token (not= token-id "ETH"))
eth-transfer? (and token (not erc20-transfer?))
token-address (cond collectible
(get-in collectible
[:id :contract-id :address])
erc20-transfer?
(get-in token [:balances-per-chain from-chain-id :address]))
to-address (get-in db [:wallet :ui :send :to-address])
data (when erc20-transfer?
(native-module/encode-transfer (address/normalized-hex to-address)
(:amount-out route)))
request-params [(multi-transaction-command request-params [(multi-transaction-command
{:from-address from-address {:from-address from-address
:to-address to-address :to-address to-address
:from-asset token-id :from-asset token-id
:to-asset token-id :to-asset token-id
:amount-out (:amount-out route)}) :amount-out (if eth-transfer? (:amount-out route) "0x0")})
(transaction-bridge {:to-address to-address (transaction-bridge {:to-address to-address
:from-address from-address :from-address from-address
:route route :route route
:from-chain-id from-chain-id
:token-address token-address :token-address token-address
:token-id (when collectible :token-id (when collectible
(money/to-hex (js/parseInt token-id)))}) (money/to-hex (js/parseInt token-id)))
:data data
:eth-transfer? eth-transfer?})
sha3-pwd]] 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