2024-06-06 20:08:25 +00:00
|
|
|
package pathprocessor
|
2023-08-24 08:45:14 +00:00
|
|
|
|
|
|
|
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"
|
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"
|
2024-09-23 06:35:34 +00:00
|
|
|
walletCommon "github.com/status-im/status-go/services/wallet/common"
|
2023-08-24 08:45:14 +00:00
|
|
|
"github.com/status-im/status-go/transactions"
|
|
|
|
)
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
type TransferProcessor 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-06-06 20:08:25 +00:00
|
|
|
func NewTransferProcessor(rpcClient *rpc.Client, transactor transactions.TransactorIface) *TransferProcessor {
|
|
|
|
return &TransferProcessor{rpcClient: rpcClient, transactor: transactor}
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
|
|
|
|
2024-07-18 12:20:54 +00:00
|
|
|
func createTransferErrorResponse(err error) error {
|
|
|
|
return createErrorResponse(ProcessorTransferName, err)
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *TransferProcessor) Name() string {
|
|
|
|
return ProcessorTransferName
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *TransferProcessor) 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
|
|
|
|
}
|
|
|
|
return params.FromChain.ChainID == params.ToChain.ChainID, nil
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *TransferProcessor) CalculateFees(params ProcessorInputParams) (*big.Int, *big.Int, error) {
|
2024-09-23 06:35:34 +00:00
|
|
|
return walletCommon.ZeroBigIntValue(), walletCommon.ZeroBigIntValue(), nil
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
|
|
|
|
2024-06-10 12:52:47 +00:00
|
|
|
func (s *TransferProcessor) PackTxInputData(params ProcessorInputParams) ([]byte, error) {
|
2024-06-05 07:56:02 +00:00
|
|
|
if params.FromToken.IsNative() {
|
2024-09-23 07:16:03 +00:00
|
|
|
return []byte{}, nil
|
2023-11-16 10:12:01 +00:00
|
|
|
} else {
|
|
|
|
abi, err := abi.JSON(strings.NewReader(ierc20.IERC20ABI))
|
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return []byte{}, createTransferErrorResponse(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-06 20:08:25 +00:00
|
|
|
func (s *TransferProcessor) 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[s.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-21 14:33:36 +00:00
|
|
|
estimation := uint64(0)
|
|
|
|
var err error
|
|
|
|
|
2024-06-10 12:52:47 +00:00
|
|
|
input, err := s.PackTxInputData(params)
|
2024-05-21 14:33:36 +00:00
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return 0, createTransferErrorResponse(err)
|
2024-05-21 14:33:36 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2024-07-18 12:20:54 +00:00
|
|
|
return 0, createTransferErrorResponse(err)
|
2024-05-21 14:33:36 +00:00
|
|
|
}
|
|
|
|
} 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 {
|
2024-07-18 12:20:54 +00:00
|
|
|
return 0, createTransferErrorResponse(err)
|
2023-11-16 10:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2024-07-18 12:20:54 +00:00
|
|
|
return 0, createTransferErrorResponse(err)
|
2023-11-16 10:12:01 +00:00
|
|
|
}
|
|
|
|
|
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-08-12 12:07:32 +00:00
|
|
|
func (s *TransferProcessor) Send(sendArgs *MultipathProcessorTxArgs, lastUsedNonce int64, verifiedAccount *account.SelectedExtKey) (types.Hash, uint64, error) {
|
|
|
|
return s.transactor.SendTransactionWithChainID(sendArgs.ChainID, *sendArgs.TransferTx, lastUsedNonce, verifiedAccount)
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 12:07:32 +00:00
|
|
|
func (s *TransferProcessor) BuildTransaction(sendArgs *MultipathProcessorTxArgs, lastUsedNonce int64) (*ethTypes.Transaction, uint64, error) {
|
|
|
|
return s.transactor.ValidateAndBuildTransaction(sendArgs.ChainID, *sendArgs.TransferTx, lastUsedNonce)
|
2023-09-29 17:56:27 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 07:16:03 +00:00
|
|
|
func (s *TransferProcessor) BuildTransactionV2(sendArgs *transactions.SendTxArgs, lastUsedNonce int64) (*ethTypes.Transaction, uint64, error) {
|
|
|
|
return s.transactor.ValidateAndBuildTransaction(sendArgs.FromChainID, *sendArgs, lastUsedNonce)
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *TransferProcessor) CalculateAmountOut(params ProcessorInputParams) (*big.Int, error) {
|
2024-06-05 07:56:02 +00:00
|
|
|
return params.AmountIn, nil
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *TransferProcessor) GetContractAddress(params ProcessorInputParams) (common.Address, error) {
|
2024-05-23 12:38:39 +00:00
|
|
|
return common.Address{}, nil
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|