2024-06-06 20:08:25 +00:00
|
|
|
package pathprocessor
|
2022-09-13 07:10:59 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-05-14 19:11:16 +00:00
|
|
|
"encoding/json"
|
2024-04-25 14:43:58 +00:00
|
|
|
"fmt"
|
2022-09-13 07:10:59 +00:00
|
|
|
"math/big"
|
2024-05-14 19:11:16 +00:00
|
|
|
netUrl "net/url"
|
2023-11-02 11:35:28 +00:00
|
|
|
"strings"
|
2024-06-14 11:27:53 +00:00
|
|
|
"sync"
|
2024-06-18 10:31:23 +00:00
|
|
|
"time"
|
2022-09-13 07:10:59 +00:00
|
|
|
|
2023-11-02 11:35:28 +00:00
|
|
|
"github.com/ethereum/go-ethereum"
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
2022-09-13 07:10:59 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2023-09-29 17:56:27 +00:00
|
|
|
ethTypes "github.com/ethereum/go-ethereum/core/types"
|
2022-09-13 07:10:59 +00:00
|
|
|
"github.com/status-im/status-go/account"
|
|
|
|
"github.com/status-im/status-go/contracts"
|
2022-12-19 12:37:37 +00:00
|
|
|
"github.com/status-im/status-go/contracts/hop"
|
2024-05-23 12:38:39 +00:00
|
|
|
hopL1CctpImplementation "github.com/status-im/status-go/contracts/hop/l1Contracts/l1CctpImplementation"
|
|
|
|
hopL1Erc20Bridge "github.com/status-im/status-go/contracts/hop/l1Contracts/l1Erc20Bridge"
|
|
|
|
hopL1EthBridge "github.com/status-im/status-go/contracts/hop/l1Contracts/l1EthBridge"
|
|
|
|
hopL1HopBridge "github.com/status-im/status-go/contracts/hop/l1Contracts/l1HopBridge"
|
|
|
|
hopL2AmmWrapper "github.com/status-im/status-go/contracts/hop/l2Contracts/l2AmmWrapper"
|
|
|
|
hopL2ArbitrumBridge "github.com/status-im/status-go/contracts/hop/l2Contracts/l2ArbitrumBridge"
|
|
|
|
hopL2CctpImplementation "github.com/status-im/status-go/contracts/hop/l2Contracts/l2CctpImplementation"
|
|
|
|
hopL2OptimismBridge "github.com/status-im/status-go/contracts/hop/l2Contracts/l2OptimismBridge"
|
2022-09-13 07:10:59 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
"github.com/status-im/status-go/rpc"
|
2024-05-23 12:38:39 +00:00
|
|
|
"github.com/status-im/status-go/rpc/chain"
|
2024-06-27 21:27:09 +00:00
|
|
|
"github.com/status-im/status-go/rpc/network"
|
2024-05-21 14:33:36 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/bigint"
|
2024-05-14 19:11:16 +00:00
|
|
|
walletCommon "github.com/status-im/status-go/services/wallet/common"
|
|
|
|
"github.com/status-im/status-go/services/wallet/thirdparty"
|
2022-09-13 07:10:59 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/token"
|
|
|
|
"github.com/status-im/status-go/transactions"
|
|
|
|
)
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
type HopBridgeTxArgs struct {
|
2022-09-13 07:10:59 +00:00
|
|
|
transactions.SendTxArgs
|
|
|
|
ChainID uint64 `json:"chainId"`
|
2024-06-14 11:27:53 +00:00
|
|
|
ChainIDTo uint64 `json:"chainIdTo"`
|
2022-09-13 07:10:59 +00:00
|
|
|
Symbol string `json:"symbol"`
|
|
|
|
Recipient common.Address `json:"recipient"`
|
|
|
|
Amount *hexutil.Big `json:"amount"`
|
|
|
|
BonderFee *hexutil.Big `json:"bonderFee"`
|
|
|
|
}
|
|
|
|
|
2024-05-14 19:11:16 +00:00
|
|
|
type BonderFee struct {
|
2024-05-21 14:33:36 +00:00
|
|
|
AmountIn *bigint.BigInt `json:"amountIn"`
|
|
|
|
Slippage float32 `json:"slippage"`
|
|
|
|
AmountOutMin *bigint.BigInt `json:"amountOutMin"`
|
|
|
|
DestinationAmountOutMin *bigint.BigInt `json:"destinationAmountOutMin"`
|
|
|
|
BonderFee *bigint.BigInt `json:"bonderFee"`
|
|
|
|
EstimatedRecieved *bigint.BigInt `json:"estimatedRecieved"`
|
|
|
|
Deadline int64 `json:"deadline"`
|
|
|
|
DestinationDeadline int64 `json:"destinationDeadline"`
|
2024-05-14 19:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bf *BonderFee) UnmarshalJSON(data []byte) error {
|
|
|
|
type Alias BonderFee
|
|
|
|
aux := &struct {
|
|
|
|
AmountIn string `json:"amountIn"`
|
|
|
|
Slippage float32 `json:"slippage"`
|
|
|
|
AmountOutMin string `json:"amountOutMin"`
|
|
|
|
DestinationAmountOutMin string `json:"destinationAmountOutMin"`
|
|
|
|
BonderFee string `json:"bonderFee"`
|
|
|
|
EstimatedRecieved string `json:"estimatedRecieved"`
|
|
|
|
Deadline int64 `json:"deadline"`
|
|
|
|
DestinationDeadline *int64 `json:"destinationDeadline"`
|
|
|
|
*Alias
|
|
|
|
}{
|
|
|
|
Alias: (*Alias)(bf),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(data, aux); err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return createBridgeHopErrorResponse(err)
|
2024-05-14 19:11:16 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 14:33:36 +00:00
|
|
|
bf.AmountIn = &bigint.BigInt{Int: new(big.Int)}
|
2024-05-14 19:11:16 +00:00
|
|
|
bf.AmountIn.SetString(aux.AmountIn, 10)
|
|
|
|
|
2024-06-14 11:27:53 +00:00
|
|
|
bf.Slippage = aux.Slippage
|
|
|
|
|
2024-05-21 14:33:36 +00:00
|
|
|
bf.AmountOutMin = &bigint.BigInt{Int: new(big.Int)}
|
2024-05-14 19:11:16 +00:00
|
|
|
bf.AmountOutMin.SetString(aux.AmountOutMin, 10)
|
|
|
|
|
2024-05-21 14:33:36 +00:00
|
|
|
bf.DestinationAmountOutMin = &bigint.BigInt{Int: new(big.Int)}
|
2024-05-14 19:11:16 +00:00
|
|
|
bf.DestinationAmountOutMin.SetString(aux.DestinationAmountOutMin, 10)
|
|
|
|
|
2024-05-21 14:33:36 +00:00
|
|
|
bf.BonderFee = &bigint.BigInt{Int: new(big.Int)}
|
2024-05-14 19:11:16 +00:00
|
|
|
bf.BonderFee.SetString(aux.BonderFee, 10)
|
|
|
|
|
2024-05-21 14:33:36 +00:00
|
|
|
bf.EstimatedRecieved = &bigint.BigInt{Int: new(big.Int)}
|
2024-05-14 19:11:16 +00:00
|
|
|
bf.EstimatedRecieved.SetString(aux.EstimatedRecieved, 10)
|
|
|
|
|
2024-05-21 14:33:36 +00:00
|
|
|
bf.Deadline = aux.Deadline
|
|
|
|
|
2024-05-14 19:11:16 +00:00
|
|
|
if aux.DestinationDeadline != nil {
|
|
|
|
bf.DestinationDeadline = *aux.DestinationDeadline
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
type HopBridgeProcessor struct {
|
2024-06-27 21:27:09 +00:00
|
|
|
transactor transactions.TransactorIface
|
|
|
|
httpClient *thirdparty.HTTPClient
|
|
|
|
tokenManager *token.Manager
|
|
|
|
contractMaker *contracts.ContractMaker
|
|
|
|
networkManager network.ManagerInterface
|
|
|
|
bonderFee *sync.Map // [fromChainName-toChainName]BonderFee
|
2022-09-13 07:10:59 +00:00
|
|
|
}
|
|
|
|
|
2024-06-27 21:27:09 +00:00
|
|
|
func NewHopBridgeProcessor(rpcClient rpc.ClientInterface, transactor transactions.TransactorIface, tokenManager *token.Manager, networkManager network.ManagerInterface) *HopBridgeProcessor {
|
2024-06-06 20:08:25 +00:00
|
|
|
return &HopBridgeProcessor{
|
2024-06-27 21:27:09 +00:00
|
|
|
contractMaker: &contracts.ContractMaker{RPCClient: rpcClient},
|
|
|
|
httpClient: thirdparty.NewHTTPClient(),
|
|
|
|
transactor: transactor,
|
|
|
|
tokenManager: tokenManager,
|
|
|
|
networkManager: networkManager,
|
|
|
|
bonderFee: &sync.Map{},
|
2022-09-13 07:10:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-18 12:20:54 +00:00
|
|
|
func createBridgeHopErrorResponse(err error) error {
|
2024-11-05 21:20:27 +00:00
|
|
|
return createErrorResponse(walletCommon.ProcessorBridgeHopName, err)
|
2024-07-18 12:20:54 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (h *HopBridgeProcessor) Name() string {
|
2024-11-05 21:20:27 +00:00
|
|
|
return walletCommon.ProcessorBridgeHopName
|
2022-09-13 07:10:59 +00:00
|
|
|
}
|
|
|
|
|
2024-06-14 11:27:53 +00:00
|
|
|
func (h *HopBridgeProcessor) Clear() {
|
|
|
|
h.bonderFee = &sync.Map{}
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (h *HopBridgeProcessor) AvailableFor(params ProcessorInputParams) (bool, error) {
|
2024-06-18 10:31:23 +00:00
|
|
|
if params.FromChain == nil || params.ToChain == nil {
|
|
|
|
return false, ErrNoChainSet
|
|
|
|
}
|
|
|
|
if params.FromToken == nil {
|
|
|
|
return false, ErrNoTokenSet
|
|
|
|
}
|
|
|
|
if params.ToToken != nil {
|
|
|
|
return false, ErrToTokenShouldNotBeSet
|
|
|
|
}
|
|
|
|
if params.FromChain.ChainID == params.ToChain.ChainID {
|
|
|
|
return false, ErrFromAndToChainsMustBeDifferent
|
2024-06-14 11:27:53 +00:00
|
|
|
}
|
2024-05-23 12:38:39 +00:00
|
|
|
// We chcek if the contract is available on the network for the token
|
2024-06-05 07:56:02 +00:00
|
|
|
_, err := h.GetContractAddress(params)
|
2024-05-23 12:38:39 +00:00
|
|
|
// toToken is not nil only if the send type is Swap
|
2024-06-18 10:31:23 +00:00
|
|
|
return err == nil, err
|
2024-05-23 12:38:39 +00:00
|
|
|
}
|
2022-09-13 07:10:59 +00:00
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (c *HopBridgeProcessor) getAppropriateABI(contractType string, chainID uint64, token *token.Token) (abi.ABI, error) {
|
2024-05-23 12:38:39 +00:00
|
|
|
switch contractType {
|
|
|
|
case hop.CctpL1Bridge:
|
|
|
|
return abi.JSON(strings.NewReader(hopL1CctpImplementation.HopL1CctpImplementationABI))
|
|
|
|
case hop.L1Bridge:
|
|
|
|
if token.IsNative() {
|
|
|
|
return abi.JSON(strings.NewReader(hopL1EthBridge.HopL1EthBridgeABI))
|
|
|
|
}
|
2024-11-05 21:20:27 +00:00
|
|
|
if token.Symbol == walletCommon.HopSymbol {
|
2024-05-23 12:38:39 +00:00
|
|
|
return abi.JSON(strings.NewReader(hopL1HopBridge.HopL1HopBridgeABI))
|
|
|
|
}
|
|
|
|
return abi.JSON(strings.NewReader(hopL1Erc20Bridge.HopL1Erc20BridgeABI))
|
|
|
|
case hop.L2AmmWrapper:
|
|
|
|
return abi.JSON(strings.NewReader(hopL2AmmWrapper.HopL2AmmWrapperABI))
|
|
|
|
case hop.CctpL2Bridge:
|
|
|
|
return abi.JSON(strings.NewReader(hopL2CctpImplementation.HopL2CctpImplementationABI))
|
|
|
|
case hop.L2Bridge:
|
|
|
|
if chainID == walletCommon.OptimismMainnet ||
|
|
|
|
chainID == walletCommon.OptimismSepolia {
|
|
|
|
return abi.JSON(strings.NewReader(hopL2OptimismBridge.HopL2OptimismBridgeABI))
|
|
|
|
}
|
|
|
|
if chainID == walletCommon.ArbitrumMainnet ||
|
|
|
|
chainID == walletCommon.ArbitrumSepolia {
|
|
|
|
return abi.JSON(strings.NewReader(hopL2ArbitrumBridge.HopL2ArbitrumBridgeABI))
|
|
|
|
}
|
2022-09-13 07:10:59 +00:00
|
|
|
}
|
|
|
|
|
2024-06-18 10:31:23 +00:00
|
|
|
return abi.ABI{}, ErrNotAvailableForContractType
|
2022-09-13 07:10:59 +00:00
|
|
|
}
|
|
|
|
|
2024-06-10 12:52:47 +00:00
|
|
|
func (h *HopBridgeProcessor) PackTxInputData(params ProcessorInputParams) ([]byte, error) {
|
|
|
|
_, contractType, err := hop.GetContractAddress(params.FromChain.ChainID, params.FromToken.Symbol)
|
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return []byte{}, createBridgeHopErrorResponse(err)
|
2024-06-10 12:39:49 +00:00
|
|
|
}
|
|
|
|
|
2024-06-10 12:52:47 +00:00
|
|
|
return h.packTxInputDataInternally(params, contractType)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HopBridgeProcessor) packTxInputDataInternally(params ProcessorInputParams, contractType string) ([]byte, error) {
|
2024-06-05 07:56:02 +00:00
|
|
|
abi, err := h.getAppropriateABI(contractType, params.FromChain.ChainID, params.FromToken)
|
2024-05-23 12:38:39 +00:00
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return []byte{}, createBridgeHopErrorResponse(err)
|
2024-05-23 12:38:39 +00:00
|
|
|
}
|
2023-11-02 11:35:28 +00:00
|
|
|
|
2024-10-29 16:57:27 +00:00
|
|
|
bonderKey := makeKey(params.FromChain.ChainID, params.ToChain.ChainID, "", "", params.AmountIn)
|
2024-06-14 11:27:53 +00:00
|
|
|
bonderFeeIns, ok := h.bonderFee.Load(bonderKey)
|
|
|
|
if !ok {
|
2024-06-18 10:31:23 +00:00
|
|
|
return nil, ErrNoBonderFeeFound
|
2024-06-14 11:27:53 +00:00
|
|
|
}
|
|
|
|
bonderFee := bonderFeeIns.(*BonderFee)
|
|
|
|
|
2024-05-23 12:38:39 +00:00
|
|
|
switch contractType {
|
|
|
|
case hop.CctpL1Bridge:
|
2024-06-14 11:27:53 +00:00
|
|
|
return h.packCctpL1BridgeTx(abi, params.ToChain.ChainID, params.ToAddr, bonderFee)
|
2024-05-23 12:38:39 +00:00
|
|
|
case hop.L1Bridge:
|
2024-06-14 11:27:53 +00:00
|
|
|
return h.packL1BridgeTx(abi, params.ToChain.ChainID, params.ToAddr, bonderFee)
|
2024-05-23 12:38:39 +00:00
|
|
|
case hop.L2AmmWrapper:
|
2024-06-14 11:27:53 +00:00
|
|
|
return h.packL2AmmWrapperTx(abi, params.ToChain.ChainID, params.ToAddr, bonderFee)
|
2024-05-23 12:38:39 +00:00
|
|
|
case hop.CctpL2Bridge:
|
2024-06-14 11:27:53 +00:00
|
|
|
return h.packCctpL2BridgeTx(abi, params.ToChain.ChainID, params.ToAddr, bonderFee)
|
2024-05-23 12:38:39 +00:00
|
|
|
case hop.L2Bridge:
|
2024-06-14 11:27:53 +00:00
|
|
|
return h.packL2BridgeTx(abi, params.ToChain.ChainID, params.ToAddr, bonderFee)
|
2024-05-21 14:33:36 +00:00
|
|
|
}
|
2024-05-23 12:38:39 +00:00
|
|
|
|
2024-06-18 10:31:23 +00:00
|
|
|
return []byte{}, ErrContractTypeNotSupported
|
2024-05-21 14:33:36 +00:00
|
|
|
}
|
2023-11-02 11:35:28 +00:00
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (h *HopBridgeProcessor) EstimateGas(params ProcessorInputParams) (uint64, error) {
|
2024-06-18 10:31:23 +00:00
|
|
|
if params.TestsMode {
|
|
|
|
if params.TestEstimationMap != nil {
|
|
|
|
if val, ok := params.TestEstimationMap[h.Name()]; ok {
|
2024-08-23 14:01:49 +00:00
|
|
|
return val.Value, val.Err
|
2024-06-18 10:31:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, ErrNoEstimationFound
|
|
|
|
}
|
|
|
|
|
2024-05-23 12:38:39 +00:00
|
|
|
value := big.NewInt(0)
|
2024-06-05 07:56:02 +00:00
|
|
|
if params.FromToken.IsNative() {
|
|
|
|
value = params.AmountIn
|
2024-05-21 14:33:36 +00:00
|
|
|
}
|
|
|
|
|
2024-06-05 07:56:02 +00:00
|
|
|
contractAddress, contractType, err := hop.GetContractAddress(params.FromChain.ChainID, params.FromToken.Symbol)
|
2024-05-21 14:33:36 +00:00
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return 0, createBridgeHopErrorResponse(err)
|
2023-11-02 11:35:28 +00:00
|
|
|
}
|
|
|
|
|
2024-06-10 12:52:47 +00:00
|
|
|
input, err := h.packTxInputDataInternally(params, contractType)
|
2023-11-02 11:35:28 +00:00
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return 0, createBridgeHopErrorResponse(err)
|
2023-11-02 11:35:28 +00:00
|
|
|
}
|
|
|
|
|
2024-06-05 07:56:02 +00:00
|
|
|
ethClient, err := h.contractMaker.RPCClient.EthClient(params.FromChain.ChainID)
|
2024-05-23 12:38:39 +00:00
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return 0, createBridgeHopErrorResponse(err)
|
2023-11-02 11:35:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := ethereum.CallMsg{
|
2024-06-05 07:56:02 +00:00
|
|
|
From: params.FromAddr,
|
2024-05-23 12:38:39 +00:00
|
|
|
To: &contractAddress,
|
2023-11-02 11:35:28 +00:00
|
|
|
Value: value,
|
|
|
|
Data: input,
|
|
|
|
}
|
|
|
|
|
2024-05-23 12:38:39 +00:00
|
|
|
estimation, err := ethClient.EstimateGas(context.Background(), msg)
|
2023-11-02 11:35:28 +00:00
|
|
|
if err != nil {
|
2024-06-05 07:56:02 +00:00
|
|
|
if !params.FromToken.IsNative() {
|
2024-05-28 10:29:02 +00:00
|
|
|
// TODO: this is a temporary solution until we find a better way to estimate the gas
|
|
|
|
// hardcoding the estimation for other than ETH, cause we cannot get a proper estimation without having an approval placed first
|
|
|
|
// this is an error we're facing otherwise: `execution reverted: ERC20: transfer amount exceeds allowance`
|
|
|
|
estimation = 350000
|
|
|
|
} else {
|
2024-07-18 12:20:54 +00:00
|
|
|
return 0, createBridgeHopErrorResponse(err)
|
2024-05-28 10:29:02 +00:00
|
|
|
}
|
2023-11-02 11:35:28 +00:00
|
|
|
}
|
2024-05-23 12:38:39 +00:00
|
|
|
|
2023-11-02 11:35:28 +00:00
|
|
|
increasedEstimation := float64(estimation) * IncreaseEstimatedGasFactor
|
|
|
|
return uint64(increasedEstimation), nil
|
2022-09-13 07:10:59 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (h *HopBridgeProcessor) GetContractAddress(params ProcessorInputParams) (common.Address, error) {
|
2024-06-05 07:56:02 +00:00
|
|
|
address, _, err := hop.GetContractAddress(params.FromChain.ChainID, params.FromToken.Symbol)
|
2024-07-18 12:20:54 +00:00
|
|
|
return address, createBridgeHopErrorResponse(err)
|
2022-12-19 12:37:37 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 07:16:03 +00:00
|
|
|
// TODO: remove this struct once mobile switches to the new approach
|
2024-08-12 12:07:32 +00:00
|
|
|
func (h *HopBridgeProcessor) sendOrBuild(sendArgs *MultipathProcessorTxArgs, signerFn bind.SignerFn, lastUsedNonce int64) (tx *ethTypes.Transaction, err error) {
|
2024-07-25 12:15:30 +00:00
|
|
|
fromChain := h.networkManager.Find(sendArgs.HopTx.ChainID)
|
2024-06-05 07:56:02 +00:00
|
|
|
if fromChain == nil {
|
2024-07-25 12:15:30 +00:00
|
|
|
return tx, fmt.Errorf("ChainID not supported %d", sendArgs.HopTx.ChainID)
|
2022-09-13 07:10:59 +00:00
|
|
|
}
|
2022-12-19 12:37:37 +00:00
|
|
|
|
2024-06-05 07:56:02 +00:00
|
|
|
token := h.tokenManager.FindToken(fromChain, sendArgs.HopTx.Symbol)
|
2024-05-23 12:38:39 +00:00
|
|
|
|
2024-08-12 12:07:32 +00:00
|
|
|
var nonce uint64
|
|
|
|
if lastUsedNonce < 0 {
|
|
|
|
nonce, err = h.transactor.NextNonce(h.contractMaker.RPCClient, fromChain.ChainID, sendArgs.HopTx.From)
|
|
|
|
if err != nil {
|
|
|
|
return tx, createBridgeHopErrorResponse(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nonce = uint64(lastUsedNonce) + 1
|
2022-09-13 07:10:59 +00:00
|
|
|
}
|
2023-11-06 09:26:02 +00:00
|
|
|
|
2022-12-19 12:37:37 +00:00
|
|
|
argNonce := hexutil.Uint64(nonce)
|
|
|
|
sendArgs.HopTx.Nonce = &argNonce
|
2022-09-13 07:10:59 +00:00
|
|
|
|
2024-05-23 12:38:39 +00:00
|
|
|
txOpts := sendArgs.HopTx.ToTransactOpts(signerFn)
|
|
|
|
if token.IsNative() {
|
|
|
|
txOpts.Value = (*big.Int)(sendArgs.HopTx.Amount)
|
2023-09-29 17:56:27 +00:00
|
|
|
}
|
|
|
|
|
2024-06-05 07:56:02 +00:00
|
|
|
ethClient, err := h.contractMaker.RPCClient.EthClient(fromChain.ChainID)
|
2023-09-29 17:56:27 +00:00
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return tx, createBridgeHopErrorResponse(err)
|
2022-09-13 07:10:59 +00:00
|
|
|
}
|
|
|
|
|
2024-06-05 07:56:02 +00:00
|
|
|
contractAddress, contractType, err := hop.GetContractAddress(fromChain.ChainID, sendArgs.HopTx.Symbol)
|
2022-09-13 07:10:59 +00:00
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return tx, createBridgeHopErrorResponse(err)
|
2022-09-13 07:10:59 +00:00
|
|
|
}
|
2024-05-21 14:33:36 +00:00
|
|
|
|
2024-10-29 16:57:27 +00:00
|
|
|
bonderKey := makeKey(sendArgs.HopTx.ChainID, sendArgs.HopTx.ChainIDTo, "", "", (*big.Int)(sendArgs.HopTx.Amount))
|
2024-06-14 11:27:53 +00:00
|
|
|
bonderFeeIns, ok := h.bonderFee.Load(bonderKey)
|
|
|
|
if !ok {
|
2024-06-18 10:31:23 +00:00
|
|
|
return nil, ErrNoBonderFeeFound
|
2024-06-14 11:27:53 +00:00
|
|
|
}
|
|
|
|
bonderFee := bonderFeeIns.(*BonderFee)
|
|
|
|
|
2024-05-23 12:38:39 +00:00
|
|
|
switch contractType {
|
|
|
|
case hop.CctpL1Bridge:
|
2024-07-25 12:15:30 +00:00
|
|
|
tx, err = h.sendCctpL1BridgeTx(contractAddress, ethClient, sendArgs.HopTx.ChainIDTo, sendArgs.HopTx.Recipient, txOpts, bonderFee)
|
2024-05-23 12:38:39 +00:00
|
|
|
case hop.L1Bridge:
|
2024-07-25 12:15:30 +00:00
|
|
|
tx, err = h.sendL1BridgeTx(contractAddress, ethClient, sendArgs.HopTx.ChainIDTo, sendArgs.HopTx.Recipient, txOpts, token, bonderFee)
|
2024-05-23 12:38:39 +00:00
|
|
|
case hop.L2AmmWrapper:
|
2024-07-25 12:15:30 +00:00
|
|
|
tx, err = h.sendL2AmmWrapperTx(contractAddress, ethClient, sendArgs.HopTx.ChainIDTo, sendArgs.HopTx.Recipient, txOpts, bonderFee)
|
2024-05-23 12:38:39 +00:00
|
|
|
case hop.CctpL2Bridge:
|
2024-07-25 12:15:30 +00:00
|
|
|
tx, err = h.sendCctpL2BridgeTx(contractAddress, ethClient, sendArgs.HopTx.ChainIDTo, sendArgs.HopTx.Recipient, txOpts, bonderFee)
|
2024-05-23 12:38:39 +00:00
|
|
|
case hop.L2Bridge:
|
2024-07-25 12:15:30 +00:00
|
|
|
tx, err = h.sendL2BridgeTx(contractAddress, ethClient, sendArgs.HopTx.ChainIDTo, sendArgs.HopTx.Recipient, txOpts, bonderFee)
|
2024-07-08 09:02:06 +00:00
|
|
|
default:
|
|
|
|
return tx, ErrContractTypeNotSupported
|
2024-05-21 14:33:36 +00:00
|
|
|
}
|
2024-07-08 09:02:06 +00:00
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return tx, createBridgeHopErrorResponse(err)
|
2024-07-08 09:02:06 +00:00
|
|
|
}
|
2024-07-25 12:15:30 +00:00
|
|
|
err = h.transactor.StoreAndTrackPendingTx(txOpts.From, sendArgs.HopTx.Symbol, sendArgs.HopTx.ChainID, sendArgs.HopTx.MultiTransactionID, tx)
|
2024-07-08 09:02:06 +00:00
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return tx, createBridgeHopErrorResponse(err)
|
2024-07-08 09:02:06 +00:00
|
|
|
}
|
|
|
|
return tx, nil
|
2022-09-13 07:10:59 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 07:16:03 +00:00
|
|
|
func (h *HopBridgeProcessor) sendOrBuildV2(sendArgs *transactions.SendTxArgs, signerFn bind.SignerFn, lastUsedNonce int64) (tx *ethTypes.Transaction, err error) {
|
|
|
|
fromChain := h.networkManager.Find(sendArgs.FromChainID)
|
|
|
|
if fromChain == nil {
|
|
|
|
return tx, fmt.Errorf("ChainID not supported %d", sendArgs.FromChainID)
|
|
|
|
}
|
|
|
|
|
|
|
|
token := h.tokenManager.FindToken(fromChain, sendArgs.FromTokenID)
|
|
|
|
|
|
|
|
var nonce uint64
|
|
|
|
if lastUsedNonce < 0 {
|
|
|
|
nonce, err = h.transactor.NextNonce(h.contractMaker.RPCClient, fromChain.ChainID, sendArgs.From)
|
|
|
|
if err != nil {
|
|
|
|
return tx, createBridgeHopErrorResponse(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nonce = uint64(lastUsedNonce) + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
argNonce := hexutil.Uint64(nonce)
|
|
|
|
sendArgs.Nonce = &argNonce
|
|
|
|
|
|
|
|
txOpts := sendArgs.ToTransactOpts(signerFn)
|
|
|
|
if token.IsNative() {
|
|
|
|
txOpts.Value = (*big.Int)(sendArgs.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
ethClient, err := h.contractMaker.RPCClient.EthClient(fromChain.ChainID)
|
|
|
|
if err != nil {
|
|
|
|
return tx, createBridgeHopErrorResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
contractAddress, contractType, err := hop.GetContractAddress(fromChain.ChainID, sendArgs.FromTokenID)
|
|
|
|
if err != nil {
|
|
|
|
return tx, createBridgeHopErrorResponse(err)
|
|
|
|
}
|
|
|
|
|
2024-11-07 13:16:00 +00:00
|
|
|
bonderKey := makeKey(sendArgs.FromChainID, sendArgs.ToChainID, "", "", (*big.Int)(sendArgs.ValueIn))
|
2024-09-23 07:16:03 +00:00
|
|
|
bonderFeeIns, ok := h.bonderFee.Load(bonderKey)
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrNoBonderFeeFound
|
|
|
|
}
|
|
|
|
bonderFee := bonderFeeIns.(*BonderFee)
|
|
|
|
|
|
|
|
switch contractType {
|
|
|
|
case hop.CctpL1Bridge:
|
|
|
|
tx, err = h.sendCctpL1BridgeTx(contractAddress, ethClient, sendArgs.ToChainID, common.Address(*sendArgs.To), txOpts, bonderFee)
|
|
|
|
case hop.L1Bridge:
|
|
|
|
tx, err = h.sendL1BridgeTx(contractAddress, ethClient, sendArgs.ToChainID, common.Address(*sendArgs.To), txOpts, token, bonderFee)
|
|
|
|
case hop.L2AmmWrapper:
|
|
|
|
tx, err = h.sendL2AmmWrapperTx(contractAddress, ethClient, sendArgs.ToChainID, common.Address(*sendArgs.To), txOpts, bonderFee)
|
|
|
|
case hop.CctpL2Bridge:
|
|
|
|
tx, err = h.sendCctpL2BridgeTx(contractAddress, ethClient, sendArgs.ToChainID, common.Address(*sendArgs.To), txOpts, bonderFee)
|
|
|
|
case hop.L2Bridge:
|
|
|
|
tx, err = h.sendL2BridgeTx(contractAddress, ethClient, sendArgs.ToChainID, common.Address(*sendArgs.To), txOpts, bonderFee)
|
|
|
|
default:
|
|
|
|
return tx, ErrContractTypeNotSupported
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return tx, createBridgeHopErrorResponse(err)
|
|
|
|
}
|
|
|
|
err = h.transactor.StoreAndTrackPendingTx(txOpts.From, sendArgs.FromTokenID, sendArgs.FromChainID, sendArgs.MultiTransactionID, tx)
|
|
|
|
if err != nil {
|
|
|
|
return tx, createBridgeHopErrorResponse(err)
|
|
|
|
}
|
|
|
|
return tx, nil
|
|
|
|
}
|
|
|
|
|
2024-08-12 12:07:32 +00:00
|
|
|
func (h *HopBridgeProcessor) Send(sendArgs *MultipathProcessorTxArgs, lastUsedNonce int64, verifiedAccount *account.SelectedExtKey) (hash types.Hash, nonce uint64, err error) {
|
|
|
|
tx, err := h.sendOrBuild(sendArgs, getSigner(sendArgs.HopTx.ChainID, sendArgs.HopTx.From, verifiedAccount), lastUsedNonce)
|
2022-09-13 07:10:59 +00:00
|
|
|
if err != nil {
|
2024-08-12 12:07:32 +00:00
|
|
|
return types.Hash{}, 0, createBridgeHopErrorResponse(err)
|
2024-02-28 13:51:52 +00:00
|
|
|
}
|
2024-08-12 12:07:32 +00:00
|
|
|
return types.Hash(tx.Hash()), tx.Nonce(), nil
|
2024-05-23 12:38:39 +00:00
|
|
|
}
|
2024-02-28 13:51:52 +00:00
|
|
|
|
2024-08-12 12:07:32 +00:00
|
|
|
func (h *HopBridgeProcessor) BuildTransaction(sendArgs *MultipathProcessorTxArgs, lastUsedNonce int64) (*ethTypes.Transaction, uint64, error) {
|
|
|
|
tx, err := h.sendOrBuild(sendArgs, nil, lastUsedNonce)
|
2024-10-29 16:57:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, createBridgeHopErrorResponse(err)
|
|
|
|
}
|
2024-08-12 12:07:32 +00:00
|
|
|
return tx, tx.Nonce(), createBridgeHopErrorResponse(err)
|
2022-09-13 07:10:59 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 07:16:03 +00:00
|
|
|
func (h *HopBridgeProcessor) BuildTransactionV2(sendArgs *transactions.SendTxArgs, lastUsedNonce int64) (*ethTypes.Transaction, uint64, error) {
|
|
|
|
tx, err := h.sendOrBuildV2(sendArgs, nil, lastUsedNonce)
|
2024-10-31 11:53:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, createBridgeHopErrorResponse(err)
|
|
|
|
}
|
2024-09-23 07:16:03 +00:00
|
|
|
return tx, tx.Nonce(), createBridgeHopErrorResponse(err)
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (h *HopBridgeProcessor) CalculateFees(params ProcessorInputParams) (*big.Int, *big.Int, error) {
|
2024-10-29 16:57:27 +00:00
|
|
|
bonderKey := makeKey(params.FromChain.ChainID, params.ToChain.ChainID, "", "", params.AmountIn)
|
2024-06-18 10:31:23 +00:00
|
|
|
if params.TestsMode {
|
|
|
|
if val, ok := params.TestBonderFeeMap[params.FromToken.Symbol]; ok {
|
|
|
|
res := new(big.Int).Sub(params.AmountIn, val)
|
|
|
|
bonderFee := &BonderFee{
|
|
|
|
AmountIn: &bigint.BigInt{Int: params.AmountIn},
|
|
|
|
Slippage: 5,
|
|
|
|
AmountOutMin: &bigint.BigInt{Int: res},
|
|
|
|
DestinationAmountOutMin: &bigint.BigInt{Int: res},
|
|
|
|
BonderFee: &bigint.BigInt{Int: val},
|
|
|
|
EstimatedRecieved: &bigint.BigInt{Int: res},
|
|
|
|
Deadline: time.Now().Add(SevenDaysInSeconds).Unix(),
|
|
|
|
}
|
|
|
|
h.bonderFee.Store(bonderKey, bonderFee)
|
2024-09-23 06:35:34 +00:00
|
|
|
return val, walletCommon.ZeroBigIntValue(), nil
|
2024-06-18 10:31:23 +00:00
|
|
|
}
|
|
|
|
return nil, nil, ErrNoBonderFeeFound
|
|
|
|
}
|
|
|
|
|
2024-05-16 07:37:36 +00:00
|
|
|
hopChainsMap := map[uint64]string{
|
|
|
|
walletCommon.EthereumMainnet: "ethereum",
|
|
|
|
walletCommon.OptimismMainnet: "optimism",
|
|
|
|
walletCommon.ArbitrumMainnet: "arbitrum",
|
|
|
|
}
|
2022-09-13 07:10:59 +00:00
|
|
|
|
2024-06-05 07:56:02 +00:00
|
|
|
fromChainName, ok := hopChainsMap[params.FromChain.ChainID]
|
2024-05-16 07:37:36 +00:00
|
|
|
if !ok {
|
2024-06-18 10:31:23 +00:00
|
|
|
return nil, nil, ErrFromChainNotSupported
|
2022-09-13 07:10:59 +00:00
|
|
|
}
|
|
|
|
|
2024-06-05 07:56:02 +00:00
|
|
|
toChainName, ok := hopChainsMap[params.ToChain.ChainID]
|
2024-05-16 07:37:36 +00:00
|
|
|
if !ok {
|
2024-06-18 10:31:23 +00:00
|
|
|
return nil, nil, ErrToChainNotSupported
|
2022-09-13 07:10:59 +00:00
|
|
|
}
|
|
|
|
|
2024-06-05 07:56:02 +00:00
|
|
|
reqParams := netUrl.Values{}
|
|
|
|
reqParams.Add("amount", params.AmountIn.String())
|
|
|
|
reqParams.Add("token", params.FromToken.Symbol)
|
|
|
|
reqParams.Add("fromChain", fromChainName)
|
|
|
|
reqParams.Add("toChain", toChainName)
|
|
|
|
reqParams.Add("slippage", "0.5") // menas 0.5%
|
2024-05-14 19:11:16 +00:00
|
|
|
|
|
|
|
url := "https://api.hop.exchange/v1/quote"
|
2024-07-30 13:48:22 +00:00
|
|
|
response, err := h.httpClient.DoGetRequest(context.Background(), url, reqParams, nil)
|
2022-09-13 07:10:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2024-06-14 11:27:53 +00:00
|
|
|
bonderFee := &BonderFee{}
|
|
|
|
err = json.Unmarshal(response, bonderFee)
|
2022-09-13 07:10:59 +00:00
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return nil, nil, createBridgeHopErrorResponse(err)
|
2022-09-13 07:10:59 +00:00
|
|
|
}
|
|
|
|
|
2024-06-14 11:27:53 +00:00
|
|
|
h.bonderFee.Store(bonderKey, bonderFee)
|
|
|
|
|
2024-05-23 12:38:39 +00:00
|
|
|
// Remove token fee from bonder fee as said here:
|
|
|
|
// https://docs.hop.exchange/v/developer-docs/api/api#get-v1-quote
|
|
|
|
// `bonderFee` - The suggested bonder fee for the amount in. The bonder fee also includes the cost of the destination transaction fee.
|
2024-07-23 10:15:29 +00:00
|
|
|
tokenFee := new(big.Int).Sub(bonderFee.AmountIn.Int, bonderFee.EstimatedRecieved.Int)
|
2022-09-13 07:10:59 +00:00
|
|
|
|
2024-06-14 11:27:53 +00:00
|
|
|
return bonderFee.BonderFee.Int, tokenFee, nil
|
2022-09-13 07:10:59 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (h *HopBridgeProcessor) CalculateAmountOut(params ProcessorInputParams) (*big.Int, error) {
|
2024-10-29 16:57:27 +00:00
|
|
|
bonderKey := makeKey(params.FromChain.ChainID, params.ToChain.ChainID, "", "", params.AmountIn)
|
2024-06-14 11:27:53 +00:00
|
|
|
bonderFeeIns, ok := h.bonderFee.Load(bonderKey)
|
|
|
|
if !ok {
|
2024-06-18 10:31:23 +00:00
|
|
|
return nil, ErrNoBonderFeeFound
|
2024-06-14 11:27:53 +00:00
|
|
|
}
|
|
|
|
bonderFee := bonderFeeIns.(*BonderFee)
|
2024-07-25 12:15:30 +00:00
|
|
|
return bonderFee.AmountOutMin.Int, nil
|
2022-09-13 07:10:59 +00:00
|
|
|
}
|
2024-05-23 12:38:39 +00:00
|
|
|
|
2024-06-14 11:27:53 +00:00
|
|
|
func (h *HopBridgeProcessor) packCctpL1BridgeTx(abi abi.ABI, toChainID uint64, to common.Address, bonderFee *BonderFee) ([]byte, error) {
|
2024-05-23 12:38:39 +00:00
|
|
|
return abi.Pack("send",
|
|
|
|
big.NewInt(int64(toChainID)),
|
|
|
|
to,
|
2024-06-14 11:27:53 +00:00
|
|
|
bonderFee.AmountIn.Int,
|
|
|
|
bonderFee.BonderFee.Int)
|
2024-05-23 12:38:39 +00:00
|
|
|
}
|
|
|
|
|
2024-06-14 11:27:53 +00:00
|
|
|
func (h *HopBridgeProcessor) sendCctpL1BridgeTx(contractAddress common.Address, ethClient chain.ClientInterface, toChainID uint64,
|
|
|
|
to common.Address, txOpts *bind.TransactOpts, bonderFee *BonderFee) (tx *ethTypes.Transaction, err error) {
|
2024-05-23 12:38:39 +00:00
|
|
|
contractInstance, err := hopL1CctpImplementation.NewHopL1CctpImplementation(
|
|
|
|
contractAddress,
|
|
|
|
ethClient,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return tx, createBridgeHopErrorResponse(err)
|
2024-05-23 12:38:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return contractInstance.Send(
|
|
|
|
txOpts,
|
|
|
|
big.NewInt(int64(toChainID)),
|
|
|
|
to,
|
2024-06-14 11:27:53 +00:00
|
|
|
bonderFee.AmountIn.Int,
|
|
|
|
bonderFee.BonderFee.Int)
|
2024-05-23 12:38:39 +00:00
|
|
|
}
|
|
|
|
|
2024-06-14 11:27:53 +00:00
|
|
|
func (h *HopBridgeProcessor) packL1BridgeTx(abi abi.ABI, toChainID uint64, to common.Address, bonderFee *BonderFee) ([]byte, error) {
|
2024-05-23 12:38:39 +00:00
|
|
|
return abi.Pack("sendToL2",
|
|
|
|
big.NewInt(int64(toChainID)),
|
|
|
|
to,
|
2024-06-14 11:27:53 +00:00
|
|
|
bonderFee.AmountIn.Int,
|
|
|
|
bonderFee.AmountOutMin.Int,
|
|
|
|
big.NewInt(bonderFee.Deadline),
|
2024-05-23 12:38:39 +00:00
|
|
|
common.Address{},
|
2024-10-04 14:28:44 +00:00
|
|
|
walletCommon.ZeroBigIntValue())
|
2024-05-23 12:38:39 +00:00
|
|
|
}
|
|
|
|
|
2024-06-14 11:27:53 +00:00
|
|
|
func (h *HopBridgeProcessor) sendL1BridgeTx(contractAddress common.Address, ethClient chain.ClientInterface, toChainID uint64,
|
|
|
|
to common.Address, txOpts *bind.TransactOpts, token *token.Token, bonderFee *BonderFee) (tx *ethTypes.Transaction, err error) {
|
2024-05-23 12:38:39 +00:00
|
|
|
if token.IsNative() {
|
|
|
|
contractInstance, err := hopL1EthBridge.NewHopL1EthBridge(
|
|
|
|
contractAddress,
|
|
|
|
ethClient,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return tx, createBridgeHopErrorResponse(err)
|
2024-05-23 12:38:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return contractInstance.SendToL2(
|
|
|
|
txOpts,
|
|
|
|
big.NewInt(int64(toChainID)),
|
|
|
|
to,
|
2024-06-14 11:27:53 +00:00
|
|
|
bonderFee.AmountIn.Int,
|
|
|
|
bonderFee.AmountOutMin.Int,
|
|
|
|
big.NewInt(bonderFee.Deadline),
|
2024-05-23 12:38:39 +00:00
|
|
|
common.Address{},
|
2024-09-23 06:35:34 +00:00
|
|
|
walletCommon.ZeroBigIntValue())
|
2024-05-23 12:38:39 +00:00
|
|
|
}
|
|
|
|
|
2024-11-05 21:20:27 +00:00
|
|
|
if token.Symbol == walletCommon.HopSymbol {
|
2024-05-23 12:38:39 +00:00
|
|
|
contractInstance, err := hopL1HopBridge.NewHopL1HopBridge(
|
|
|
|
contractAddress,
|
|
|
|
ethClient,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return tx, createBridgeHopErrorResponse(err)
|
2024-05-23 12:38:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return contractInstance.SendToL2(
|
|
|
|
txOpts,
|
|
|
|
big.NewInt(int64(toChainID)),
|
|
|
|
to,
|
2024-06-14 11:27:53 +00:00
|
|
|
bonderFee.AmountIn.Int,
|
|
|
|
bonderFee.AmountOutMin.Int,
|
|
|
|
big.NewInt(bonderFee.Deadline),
|
2024-05-23 12:38:39 +00:00
|
|
|
common.Address{},
|
2024-09-23 06:35:34 +00:00
|
|
|
walletCommon.ZeroBigIntValue())
|
2024-05-23 12:38:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
contractInstance, err := hopL1Erc20Bridge.NewHopL1Erc20Bridge(
|
|
|
|
contractAddress,
|
|
|
|
ethClient,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return tx, createBridgeHopErrorResponse(err)
|
2024-05-23 12:38:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return contractInstance.SendToL2(
|
|
|
|
txOpts,
|
|
|
|
big.NewInt(int64(toChainID)),
|
|
|
|
to,
|
2024-06-14 11:27:53 +00:00
|
|
|
bonderFee.AmountIn.Int,
|
|
|
|
bonderFee.AmountOutMin.Int,
|
|
|
|
big.NewInt(bonderFee.Deadline),
|
2024-05-23 12:38:39 +00:00
|
|
|
common.Address{},
|
2024-09-23 06:35:34 +00:00
|
|
|
walletCommon.ZeroBigIntValue())
|
2024-05-23 12:38:39 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-06-14 11:27:53 +00:00
|
|
|
func (h *HopBridgeProcessor) packCctpL2BridgeTx(abi abi.ABI, toChainID uint64, to common.Address, bonderFee *BonderFee) ([]byte, error) {
|
2024-05-23 12:38:39 +00:00
|
|
|
return abi.Pack("send",
|
|
|
|
big.NewInt(int64(toChainID)),
|
|
|
|
to,
|
2024-06-14 11:27:53 +00:00
|
|
|
bonderFee.AmountIn.Int,
|
|
|
|
bonderFee.BonderFee.Int)
|
2024-05-23 12:38:39 +00:00
|
|
|
}
|
|
|
|
|
2024-06-14 11:27:53 +00:00
|
|
|
func (h *HopBridgeProcessor) sendCctpL2BridgeTx(contractAddress common.Address, ethClient chain.ClientInterface, toChainID uint64,
|
|
|
|
to common.Address, txOpts *bind.TransactOpts, bonderFee *BonderFee) (tx *ethTypes.Transaction, err error) {
|
2024-05-23 12:38:39 +00:00
|
|
|
contractInstance, err := hopL2CctpImplementation.NewHopL2CctpImplementation(
|
|
|
|
contractAddress,
|
|
|
|
ethClient,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return tx, createBridgeHopErrorResponse(err)
|
2024-05-23 12:38:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return contractInstance.Send(
|
|
|
|
txOpts,
|
|
|
|
big.NewInt(int64(toChainID)),
|
|
|
|
to,
|
2024-06-14 11:27:53 +00:00
|
|
|
bonderFee.AmountIn.Int,
|
|
|
|
bonderFee.BonderFee.Int,
|
2024-05-23 12:38:39 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-06-14 11:27:53 +00:00
|
|
|
func (h *HopBridgeProcessor) packL2AmmWrapperTx(abi abi.ABI, toChainID uint64, to common.Address, bonderFee *BonderFee) ([]byte, error) {
|
2024-05-23 12:38:39 +00:00
|
|
|
return abi.Pack("swapAndSend",
|
|
|
|
big.NewInt(int64(toChainID)),
|
|
|
|
to,
|
2024-06-14 11:27:53 +00:00
|
|
|
bonderFee.AmountIn.Int,
|
|
|
|
bonderFee.BonderFee.Int,
|
|
|
|
bonderFee.AmountOutMin.Int,
|
|
|
|
big.NewInt(bonderFee.Deadline),
|
|
|
|
bonderFee.DestinationAmountOutMin.Int,
|
|
|
|
big.NewInt(bonderFee.DestinationDeadline))
|
2024-05-23 12:38:39 +00:00
|
|
|
}
|
|
|
|
|
2024-06-14 11:27:53 +00:00
|
|
|
func (h *HopBridgeProcessor) sendL2AmmWrapperTx(contractAddress common.Address, ethClient chain.ClientInterface, toChainID uint64,
|
|
|
|
to common.Address, txOpts *bind.TransactOpts, bonderFee *BonderFee) (tx *ethTypes.Transaction, err error) {
|
2024-05-23 12:38:39 +00:00
|
|
|
contractInstance, err := hopL2AmmWrapper.NewHopL2AmmWrapper(
|
|
|
|
contractAddress,
|
|
|
|
ethClient,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return tx, createBridgeHopErrorResponse(err)
|
2024-05-23 12:38:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return contractInstance.SwapAndSend(
|
|
|
|
txOpts,
|
|
|
|
big.NewInt(int64(toChainID)),
|
|
|
|
to,
|
2024-06-14 11:27:53 +00:00
|
|
|
bonderFee.AmountIn.Int,
|
|
|
|
bonderFee.BonderFee.Int,
|
|
|
|
bonderFee.AmountOutMin.Int,
|
|
|
|
big.NewInt(bonderFee.Deadline),
|
|
|
|
bonderFee.DestinationAmountOutMin.Int,
|
|
|
|
big.NewInt(bonderFee.DestinationDeadline))
|
2024-05-23 12:38:39 +00:00
|
|
|
}
|
|
|
|
|
2024-06-14 11:27:53 +00:00
|
|
|
func (h *HopBridgeProcessor) packL2BridgeTx(abi abi.ABI, toChainID uint64, to common.Address, bonderFee *BonderFee) ([]byte, error) {
|
2024-05-23 12:38:39 +00:00
|
|
|
return abi.Pack("send",
|
|
|
|
big.NewInt(int64(toChainID)),
|
|
|
|
to,
|
2024-06-14 11:27:53 +00:00
|
|
|
bonderFee.AmountIn.Int,
|
|
|
|
bonderFee.BonderFee.Int,
|
|
|
|
bonderFee.AmountOutMin.Int,
|
|
|
|
big.NewInt(bonderFee.Deadline))
|
2024-05-23 12:38:39 +00:00
|
|
|
}
|
|
|
|
|
2024-06-14 11:27:53 +00:00
|
|
|
func (h *HopBridgeProcessor) sendL2BridgeTx(contractAddress common.Address, ethClient chain.ClientInterface, toChainID uint64,
|
|
|
|
to common.Address, txOpts *bind.TransactOpts, bonderFee *BonderFee) (tx *ethTypes.Transaction, err error) {
|
2024-05-23 12:38:39 +00:00
|
|
|
fromChainID := ethClient.NetworkID()
|
|
|
|
if fromChainID == walletCommon.OptimismMainnet ||
|
|
|
|
fromChainID == walletCommon.OptimismSepolia {
|
|
|
|
contractInstance, err := hopL2OptimismBridge.NewHopL2OptimismBridge(
|
|
|
|
contractAddress,
|
|
|
|
ethClient,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return tx, createBridgeHopErrorResponse(err)
|
2024-05-23 12:38:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return contractInstance.Send(
|
|
|
|
txOpts,
|
|
|
|
big.NewInt(int64(toChainID)),
|
|
|
|
to,
|
2024-06-14 11:27:53 +00:00
|
|
|
bonderFee.AmountIn.Int,
|
|
|
|
bonderFee.BonderFee.Int,
|
|
|
|
bonderFee.AmountOutMin.Int,
|
|
|
|
big.NewInt(bonderFee.Deadline))
|
2024-05-23 12:38:39 +00:00
|
|
|
}
|
|
|
|
if fromChainID == walletCommon.ArbitrumMainnet ||
|
|
|
|
fromChainID == walletCommon.ArbitrumSepolia {
|
|
|
|
contractInstance, err := hopL2ArbitrumBridge.NewHopL2ArbitrumBridge(
|
|
|
|
contractAddress,
|
|
|
|
ethClient,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return tx, createBridgeHopErrorResponse(err)
|
2024-05-23 12:38:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return contractInstance.Send(
|
|
|
|
txOpts,
|
|
|
|
big.NewInt(int64(toChainID)),
|
|
|
|
to,
|
2024-06-14 11:27:53 +00:00
|
|
|
bonderFee.AmountIn.Int,
|
|
|
|
bonderFee.BonderFee.Int,
|
|
|
|
bonderFee.AmountOutMin.Int,
|
|
|
|
big.NewInt(bonderFee.Deadline))
|
2024-05-23 12:38:39 +00:00
|
|
|
}
|
|
|
|
|
2024-06-18 10:31:23 +00:00
|
|
|
return tx, ErrTxForChainNotSupported
|
2024-05-23 12:38:39 +00:00
|
|
|
}
|