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"
|
|
|
|
"github.com/status-im/status-go/params"
|
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/services/wallet/token"
|
|
|
|
"github.com/status-im/status-go/transactions"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TransferBridge struct {
|
2023-11-16 10:12:01 +00:00
|
|
|
rpcClient *rpc.Client
|
2023-08-24 08:45:14 +00:00
|
|
|
transactor *transactions.Transactor
|
|
|
|
}
|
|
|
|
|
2023-11-16 10:12:01 +00:00
|
|
|
func NewTransferBridge(rpcClient *rpc.Client, transactor *transactions.Transactor) *TransferBridge {
|
|
|
|
return &TransferBridge{rpcClient: rpcClient, transactor: transactor}
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TransferBridge) Name() string {
|
|
|
|
return "Transfer"
|
|
|
|
}
|
|
|
|
|
2024-05-14 19:11:16 +00:00
|
|
|
func (s *TransferBridge) AvailableFor(from, to *params.Network, token *token.Token, toToken *token.Token) (bool, error) {
|
2024-04-18 10:22:28 +00:00
|
|
|
return from.ChainID == to.ChainID && token != nil && toToken == nil, nil
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
|
|
|
|
2024-05-14 19:11:16 +00:00
|
|
|
func (s *TransferBridge) CalculateFees(from, to *params.Network, token *token.Token, amountIn *big.Int) (*big.Int, *big.Int, error) {
|
2023-08-24 08:45:14 +00:00
|
|
|
return big.NewInt(0), big.NewInt(0), nil
|
|
|
|
}
|
|
|
|
|
2024-04-01 13:39:17 +00:00
|
|
|
func (s *TransferBridge) EstimateGas(fromNetwork *params.Network, toNetwork *params.Network, from common.Address, to common.Address, token *token.Token, toToken *token.Token, amountIn *big.Int) (uint64, error) {
|
2023-11-16 10:12:01 +00:00
|
|
|
estimation := uint64(0)
|
|
|
|
var err error
|
|
|
|
if token.Symbol == "ETH" {
|
|
|
|
estimation, err = s.transactor.EstimateGas(fromNetwork, from, to, amountIn, []byte("eth_sendRawTransaction"))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ethClient, err := s.rpcClient.EthClient(fromNetwork.ChainID)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
abi, err := abi.JSON(strings.NewReader(ierc20.IERC20ABI))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
input, err := abi.Pack("transfer",
|
|
|
|
to,
|
|
|
|
amountIn,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
msg := ethereum.CallMsg{
|
|
|
|
From: from,
|
|
|
|
To: &token.Address,
|
|
|
|
Data: input,
|
|
|
|
}
|
|
|
|
|
|
|
|
estimation, err = ethClient.EstimateGas(ctx, msg)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2023-08-24 08:45:14 +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-04-25 14:43:58 +00:00
|
|
|
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) {
|
2024-03-25 12:40:00 +00:00
|
|
|
toAddr := types.Address(toAddress)
|
2024-04-04 09:32:22 +00:00
|
|
|
if strings.EqualFold(token.Symbol, "ETH") {
|
|
|
|
sendArgs := &TransactionBridge{
|
|
|
|
TransferTx: &transactions.SendTxArgs{
|
|
|
|
From: types.Address(fromAddress),
|
|
|
|
To: &toAddr,
|
|
|
|
Value: (*hexutil.Big)(amountIn),
|
|
|
|
Data: types.HexBytes("0x0"),
|
|
|
|
},
|
|
|
|
ChainID: network.ChainID,
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.BuildTransaction(sendArgs)
|
|
|
|
}
|
|
|
|
abi, err := abi.JSON(strings.NewReader(ierc20.IERC20ABI))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
input, err := abi.Pack("transfer",
|
|
|
|
toAddress,
|
|
|
|
amountIn,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-03-25 12:40:00 +00:00
|
|
|
sendArgs := &TransactionBridge{
|
|
|
|
TransferTx: &transactions.SendTxArgs{
|
|
|
|
From: types.Address(fromAddress),
|
|
|
|
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
|
|
|
},
|
|
|
|
ChainID: network.ChainID,
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-08-24 08:45:14 +00:00
|
|
|
func (s *TransferBridge) CalculateAmountOut(from, to *params.Network, amountIn *big.Int, symbol string) (*big.Int, error) {
|
|
|
|
return amountIn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TransferBridge) GetContractAddress(network *params.Network, token *token.Token) *common.Address {
|
|
|
|
return nil
|
|
|
|
}
|