From 12d2a1ac2d2052f079ed8e8cd0ec604775fe4315 Mon Sep 17 00:00:00 2001 From: Ivan Belyakov Date: Thu, 25 Apr 2024 16:43:58 +0200 Subject: [PATCH] fix(wallet)_: fixed crash when bridging and chain id not set --- services/wallet/bridge/bridge.go | 2 +- services/wallet/bridge/cbridge.go | 5 +++-- services/wallet/bridge/erc1155_transfer.go | 2 +- services/wallet/bridge/erc721_transfer.go | 2 +- services/wallet/bridge/hop.go | 13 ++++++++----- services/wallet/bridge/swap_paraswap.go | 2 +- services/wallet/bridge/transfer.go | 2 +- services/wallet/router.go | 2 +- 8 files changed, 17 insertions(+), 13 deletions(-) diff --git a/services/wallet/bridge/bridge.go b/services/wallet/bridge/bridge.go index 22cd66a0a..042c7c2e0 100644 --- a/services/wallet/bridge/bridge.go +++ b/services/wallet/bridge/bridge.go @@ -109,5 +109,5 @@ type Bridge interface { Send(sendArgs *TransactionBridge, verifiedAccount *account.SelectedExtKey) (types.Hash, error) GetContractAddress(network *params.Network, token *token.Token) *common.Address BuildTransaction(sendArgs *TransactionBridge) (*ethTypes.Transaction, error) - BuildTx(network *params.Network, fromAddress common.Address, toAddress common.Address, token *token.Token, amountIn *big.Int) (*ethTypes.Transaction, error) + BuildTx(fromNetwork, toNetwork *params.Network, fromAddress common.Address, toAddress common.Address, token *token.Token, amountIn *big.Int, bonderFee *big.Int) (*ethTypes.Transaction, error) } diff --git a/services/wallet/bridge/cbridge.go b/services/wallet/bridge/cbridge.go index 22fdf00c8..025dafbb3 100644 --- a/services/wallet/bridge/cbridge.go +++ b/services/wallet/bridge/cbridge.go @@ -288,7 +288,7 @@ func (s *CBridge) EstimateGas(fromNetwork *params.Network, toNetwork *params.Net return uint64(increasedEstimation), nil } -func (s *CBridge) BuildTx(network *params.Network, fromAddress common.Address, toAddress common.Address, token *token.Token, amountIn *big.Int) (*ethTypes.Transaction, error) { +func (s *CBridge) BuildTx(fromNetwork, toNetwork *params.Network, fromAddress common.Address, toAddress common.Address, token *token.Token, amountIn *big.Int, bonderFee *big.Int) (*ethTypes.Transaction, error) { toAddr := types.Address(toAddress) sendArgs := &TransactionBridge{ CbridgeTx: &CBridgeTxArgs{ @@ -298,11 +298,12 @@ func (s *CBridge) BuildTx(network *params.Network, fromAddress common.Address, t Value: (*hexutil.Big)(amountIn), Data: types.HexBytes("0x0"), }, - ChainID: network.ChainID, + ChainID: toNetwork.ChainID, Symbol: token.Symbol, Recipient: toAddress, Amount: (*hexutil.Big)(amountIn), }, + ChainID: fromNetwork.ChainID, } return s.BuildTransaction(sendArgs) diff --git a/services/wallet/bridge/erc1155_transfer.go b/services/wallet/bridge/erc1155_transfer.go index 2e6e043a6..6baa8581a 100644 --- a/services/wallet/bridge/erc1155_transfer.go +++ b/services/wallet/bridge/erc1155_transfer.go @@ -101,7 +101,7 @@ func (s *ERC1155TransferBridge) EstimateGas(fromNetwork *params.Network, toNetwo return uint64(increasedEstimation), nil } -func (s *ERC1155TransferBridge) BuildTx(network *params.Network, fromAddress common.Address, toAddress common.Address, token *token.Token, amountIn *big.Int) (*ethTypes.Transaction, error) { +func (s *ERC1155TransferBridge) BuildTx(network, _ *params.Network, fromAddress common.Address, toAddress common.Address, token *token.Token, amountIn *big.Int, _ *big.Int) (*ethTypes.Transaction, error) { contractAddress := types.Address(token.Address) // We store ERC1155 Token ID using big.Int.String() in token.Symbol diff --git a/services/wallet/bridge/erc721_transfer.go b/services/wallet/bridge/erc721_transfer.go index bacaa138b..333ba2154 100644 --- a/services/wallet/bridge/erc721_transfer.go +++ b/services/wallet/bridge/erc721_transfer.go @@ -98,7 +98,7 @@ func (s *ERC721TransferBridge) EstimateGas(fromNetwork *params.Network, toNetwor return uint64(increasedEstimation), nil } -func (s *ERC721TransferBridge) BuildTx(network *params.Network, fromAddress common.Address, toAddress common.Address, token *token.Token, amountIn *big.Int) (*ethTypes.Transaction, error) { +func (s *ERC721TransferBridge) BuildTx(network, _ *params.Network, fromAddress common.Address, toAddress common.Address, token *token.Token, amountIn *big.Int, _ *big.Int) (*ethTypes.Transaction, error) { contractAddress := types.Address(token.Address) // We store ERC721 Token ID using big.Int.String() in token.Symbol diff --git a/services/wallet/bridge/hop.go b/services/wallet/bridge/hop.go index 5149d83ef..e89ef7d1e 100644 --- a/services/wallet/bridge/hop.go +++ b/services/wallet/bridge/hop.go @@ -3,6 +3,7 @@ package bridge import ( "context" "errors" + "fmt" "math" "math/big" "strings" @@ -215,7 +216,7 @@ func (h *HopBridge) EstimateGas(fromNetwork *params.Network, toNetwork *params.N return uint64(increasedEstimation), nil } -func (h *HopBridge) BuildTx(network *params.Network, fromAddress common.Address, toAddress common.Address, token *token.Token, amountIn *big.Int) (*ethTypes.Transaction, error) { +func (h *HopBridge) BuildTx(fromNetwork, toNetwork *params.Network, fromAddress common.Address, toAddress common.Address, token *token.Token, amountIn *big.Int, bonderFee *big.Int) (*ethTypes.Transaction, error) { toAddr := types.Address(toAddress) sendArgs := &TransactionBridge{ HopTx: &HopTxArgs{ @@ -225,11 +226,13 @@ func (h *HopBridge) BuildTx(network *params.Network, fromAddress common.Address, Value: (*hexutil.Big)(amountIn), Data: types.HexBytes("0x0"), }, - ChainID: network.ChainID, Symbol: token.Symbol, Recipient: toAddress, Amount: (*hexutil.Big)(amountIn), + BonderFee: (*hexutil.Big)(bonderFee), + ChainID: toNetwork.ChainID, }, + ChainID: fromNetwork.ChainID, } return h.BuildTransaction(sendArgs) @@ -249,10 +252,10 @@ func (h *HopBridge) GetContractAddress(network *params.Network, token *token.Tok func (h *HopBridge) sendOrBuild(sendArgs *TransactionBridge, signerFn bind.SignerFn) (tx *ethTypes.Transaction, err error) { fromNetwork := h.contractMaker.RPCClient.NetworkManager.Find(sendArgs.ChainID) if fromNetwork == nil { - return tx, err + return tx, fmt.Errorf("ChainID not supported %d", sendArgs.ChainID) } - nonce, err := h.transactor.NextNonce(h.contractMaker.RPCClient, sendArgs.ChainID, sendArgs.HopTx.From) + nonce, err := h.transactor.NextNonce(h.contractMaker.RPCClient, fromNetwork.ChainID, sendArgs.HopTx.From) if err != nil { return tx, err } @@ -333,7 +336,7 @@ func (h *HopBridge) swapAndSend(chainID uint64, hopArgs *HopTxArgs, signerFn bin tx, err = ammWrapper.SwapAndSend( txOpts, - big.NewInt(int64(hopArgs.ChainID)), + new(big.Int).SetUint64(hopArgs.ChainID), hopArgs.Recipient, hopArgs.Amount.ToInt(), hopArgs.BonderFee.ToInt(), diff --git a/services/wallet/bridge/swap_paraswap.go b/services/wallet/bridge/swap_paraswap.go index 0e14730a5..a511fd3f6 100644 --- a/services/wallet/bridge/swap_paraswap.go +++ b/services/wallet/bridge/swap_paraswap.go @@ -115,7 +115,7 @@ func (s *SwapParaswap) GetContractAddress(network *params.Network, token *token. return &address } -func (s *SwapParaswap) BuildTx(network *params.Network, fromAddress common.Address, toAddress common.Address, token *token.Token, amountIn *big.Int) (*ethTypes.Transaction, error) { +func (s *SwapParaswap) BuildTx(network, _ *params.Network, fromAddress common.Address, toAddress common.Address, token *token.Token, amountIn *big.Int, _ *big.Int) (*ethTypes.Transaction, error) { toAddr := types.Address(toAddress) sendArgs := &TransactionBridge{ SwapTx: &SwapTxArgs{ diff --git a/services/wallet/bridge/transfer.go b/services/wallet/bridge/transfer.go index 448b87a89..704607635 100644 --- a/services/wallet/bridge/transfer.go +++ b/services/wallet/bridge/transfer.go @@ -85,7 +85,7 @@ func (s *TransferBridge) EstimateGas(fromNetwork *params.Network, toNetwork *par return uint64(increasedEstimation), nil } -func (s *TransferBridge) BuildTx(network *params.Network, fromAddress common.Address, toAddress common.Address, token *token.Token, amountIn *big.Int) (*ethTypes.Transaction, error) { +func (s *TransferBridge) BuildTx(network, _ *params.Network, fromAddress common.Address, toAddress common.Address, token *token.Token, amountIn *big.Int, bonderFee *big.Int) (*ethTypes.Transaction, error) { toAddr := types.Address(toAddress) if strings.EqualFold(token.Symbol, "ETH") { sendArgs := &TransactionBridge{ diff --git a/services/wallet/router.go b/services/wallet/router.go index 72a96e2ee..7e6998cf1 100644 --- a/services/wallet/router.go +++ b/services/wallet/router.go @@ -742,7 +742,7 @@ func (r *Router) suggestedRoutes( var l1GasFeeWei uint64 if sendType.needL1Fee() { - tx, err := bridge.BuildTx(network, addrFrom, addrTo, token, amountIn) + tx, err := bridge.BuildTx(network, dest, addrFrom, addrTo, token, amountIn, bonderFees) if err != nil { continue }