2023-08-24 08:45:14 +00:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
2023-11-16 10:12:01 +00:00
|
|
|
"context"
|
2023-08-24 08:45:14 +00:00
|
|
|
"math/big"
|
2023-11-16 10:12:01 +00:00
|
|
|
"strings"
|
2023-08-24 08:45:14 +00:00
|
|
|
|
2023-11-16 10:12:01 +00:00
|
|
|
"github.com/ethereum/go-ethereum"
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
2023-08-24 08:45:14 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2024-03-25 12:40:00 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2023-09-29 17:56:27 +00:00
|
|
|
ethTypes "github.com/ethereum/go-ethereum/core/types"
|
2023-08-24 08:45:14 +00:00
|
|
|
"github.com/status-im/status-go/account"
|
2023-11-16 10:12:01 +00:00
|
|
|
"github.com/status-im/status-go/contracts/ierc20"
|
2023-08-24 08:45:14 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2023-11-16 10:12:01 +00:00
|
|
|
"github.com/status-im/status-go/rpc"
|
2023-08-24 08:45:14 +00:00
|
|
|
"github.com/status-im/status-go/transactions"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TransferBridge struct {
|
2023-11-16 10:12:01 +00:00
|
|
|
rpcClient *rpc.Client
|
2024-05-26 08:31:13 +00:00
|
|
|
transactor transactions.TransactorIface
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
|
|
|
|
2024-05-26 08:31:13 +00:00
|
|
|
func NewTransferBridge(rpcClient *rpc.Client, transactor transactions.TransactorIface) *TransferBridge {
|
2023-11-16 10:12:01 +00:00
|
|
|
return &TransferBridge{rpcClient: rpcClient, transactor: transactor}
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TransferBridge) Name() string {
|
2024-06-05 07:56:02 +00:00
|
|
|
return TransferName
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
|
|
|
|
2024-06-05 07:56:02 +00:00
|
|
|
func (s *TransferBridge) AvailableFor(params BridgeParams) (bool, error) {
|
|
|
|
return params.FromChain.ChainID == params.ToChain.ChainID && params.FromToken != nil && params.ToToken == nil, nil
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
|
|
|
|
2024-06-05 07:56:02 +00:00
|
|
|
func (s *TransferBridge) CalculateFees(params BridgeParams) (*big.Int, *big.Int, error) {
|
2023-08-24 08:45:14 +00:00
|
|
|
return big.NewInt(0), big.NewInt(0), nil
|
|
|
|
}
|
|
|
|
|
2024-06-05 07:56:02 +00:00
|
|
|
func (s *TransferBridge) PackTxInputData(params BridgeParams, contractType string) ([]byte, error) {
|
|
|
|
if params.FromToken.IsNative() {
|
2024-05-21 14:33:36 +00:00
|
|
|
return []byte("eth_sendRawTransaction"), nil
|
2023-11-16 10:12:01 +00:00
|
|
|
} else {
|
|
|
|
abi, err := abi.JSON(strings.NewReader(ierc20.IERC20ABI))
|
|
|
|
if err != nil {
|
2024-05-21 14:33:36 +00:00
|
|
|
return []byte{}, err
|
2023-11-16 10:12:01 +00:00
|
|
|
}
|
2024-05-21 14:33:36 +00:00
|
|
|
return abi.Pack("transfer",
|
2024-06-05 07:56:02 +00:00
|
|
|
params.ToAddr,
|
|
|
|
params.AmountIn,
|
2023-11-16 10:12:01 +00:00
|
|
|
)
|
2024-05-21 14:33:36 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-16 10:12:01 +00:00
|
|
|
|
2024-06-05 07:56:02 +00:00
|
|
|
func (s *TransferBridge) EstimateGas(params BridgeParams) (uint64, error) {
|
2024-05-21 14:33:36 +00:00
|
|
|
estimation := uint64(0)
|
|
|
|
var err error
|
|
|
|
|
2024-06-05 07:56:02 +00:00
|
|
|
input, err := s.PackTxInputData(params, "")
|
2024-05-21 14:33:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2024-06-05 07:56:02 +00:00
|
|
|
if params.FromToken.IsNative() {
|
|
|
|
estimation, err = s.transactor.EstimateGas(params.FromChain, params.FromAddr, params.ToAddr, params.AmountIn, input)
|
2024-05-21 14:33:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
} else {
|
2024-06-05 07:56:02 +00:00
|
|
|
ethClient, err := s.rpcClient.EthClient(params.FromChain.ChainID)
|
2023-11-16 10:12:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
msg := ethereum.CallMsg{
|
2024-06-05 07:56:02 +00:00
|
|
|
From: params.FromAddr,
|
|
|
|
To: ¶ms.FromToken.Address,
|
2023-11-16 10:12:01 +00:00
|
|
|
Data: input,
|
|
|
|
}
|
|
|
|
|
|
|
|
estimation, err = ethClient.EstimateGas(ctx, msg)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
2024-05-21 14:33:36 +00:00
|
|
|
|
2023-11-02 11:35:28 +00:00
|
|
|
increasedEstimation := float64(estimation) * IncreaseEstimatedGasFactor
|
|
|
|
return uint64(increasedEstimation), nil
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
|
|
|
|
2024-06-05 07:56:02 +00:00
|
|
|
func (s *TransferBridge) BuildTx(params BridgeParams) (*ethTypes.Transaction, error) {
|
|
|
|
toAddr := types.Address(params.ToAddr)
|
|
|
|
if params.FromToken.IsNative() {
|
2024-04-04 09:32:22 +00:00
|
|
|
sendArgs := &TransactionBridge{
|
|
|
|
TransferTx: &transactions.SendTxArgs{
|
2024-06-05 07:56:02 +00:00
|
|
|
From: types.Address(params.FromAddr),
|
2024-04-04 09:32:22 +00:00
|
|
|
To: &toAddr,
|
2024-06-05 07:56:02 +00:00
|
|
|
Value: (*hexutil.Big)(params.AmountIn),
|
2024-04-04 09:32:22 +00:00
|
|
|
Data: types.HexBytes("0x0"),
|
|
|
|
},
|
2024-06-05 07:56:02 +00:00
|
|
|
ChainID: params.FromChain.ChainID,
|
2024-04-04 09:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.BuildTransaction(sendArgs)
|
|
|
|
}
|
|
|
|
abi, err := abi.JSON(strings.NewReader(ierc20.IERC20ABI))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
input, err := abi.Pack("transfer",
|
2024-06-05 07:56:02 +00:00
|
|
|
params.ToAddr,
|
|
|
|
params.AmountIn,
|
2024-04-04 09:32:22 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-03-25 12:40:00 +00:00
|
|
|
sendArgs := &TransactionBridge{
|
|
|
|
TransferTx: &transactions.SendTxArgs{
|
2024-06-05 07:56:02 +00:00
|
|
|
From: types.Address(params.FromAddr),
|
2024-03-25 12:40:00 +00:00
|
|
|
To: &toAddr,
|
2024-04-04 09:32:22 +00:00
|
|
|
Value: (*hexutil.Big)(big.NewInt(0)),
|
|
|
|
Data: input,
|
2024-03-25 12:40:00 +00:00
|
|
|
},
|
2024-06-05 07:56:02 +00:00
|
|
|
ChainID: params.FromChain.ChainID,
|
2024-03-25 12:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.BuildTransaction(sendArgs)
|
|
|
|
}
|
|
|
|
|
2023-08-24 08:45:14 +00:00
|
|
|
func (s *TransferBridge) Send(sendArgs *TransactionBridge, verifiedAccount *account.SelectedExtKey) (types.Hash, error) {
|
|
|
|
return s.transactor.SendTransactionWithChainID(sendArgs.ChainID, *sendArgs.TransferTx, verifiedAccount)
|
|
|
|
}
|
|
|
|
|
2023-12-19 13:38:01 +00:00
|
|
|
func (s *TransferBridge) BuildTransaction(sendArgs *TransactionBridge) (*ethTypes.Transaction, error) {
|
2023-09-29 17:56:27 +00:00
|
|
|
return s.transactor.ValidateAndBuildTransaction(sendArgs.ChainID, *sendArgs.TransferTx)
|
|
|
|
}
|
|
|
|
|
2024-06-05 07:56:02 +00:00
|
|
|
func (s *TransferBridge) CalculateAmountOut(params BridgeParams) (*big.Int, error) {
|
|
|
|
return params.AmountIn, nil
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
|
|
|
|
2024-06-05 07:56:02 +00:00
|
|
|
func (s *TransferBridge) GetContractAddress(params BridgeParams) (common.Address, error) {
|
2024-05-23 12:38:39 +00:00
|
|
|
return common.Address{}, nil
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|