2024-06-06 20:08:25 +00:00
|
|
|
package pathprocessor
|
2024-04-01 13:39:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"math/big"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
ethTypes "github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/status-im/status-go/account"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
"github.com/status-im/status-go/rpc"
|
|
|
|
walletCommon "github.com/status-im/status-go/services/wallet/common"
|
|
|
|
"github.com/status-im/status-go/services/wallet/thirdparty/paraswap"
|
|
|
|
walletToken "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 SwapParaswapTxArgs struct {
|
2024-04-01 13:39:17 +00:00
|
|
|
transactions.SendTxArgs
|
|
|
|
ChainID uint64 `json:"chainId"`
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
type SwapParaswapProcessor struct {
|
2024-04-01 13:39:17 +00:00
|
|
|
paraswapClient *paraswap.ClientV5
|
|
|
|
priceRoute paraswap.Route
|
2024-05-26 08:31:13 +00:00
|
|
|
transactor transactions.TransactorIface
|
2024-04-01 13:39:17 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func NewSwapParaswapProcessor(rpcClient *rpc.Client, transactor transactions.TransactorIface, tokenManager *walletToken.Manager) *SwapParaswapProcessor {
|
|
|
|
return &SwapParaswapProcessor{
|
2024-04-01 13:39:17 +00:00
|
|
|
paraswapClient: paraswap.NewClientV5(walletCommon.EthereumMainnet),
|
|
|
|
transactor: transactor,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *SwapParaswapProcessor) Name() string {
|
|
|
|
return ProcessorSwapParaswapName
|
2024-04-01 13:39:17 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *SwapParaswapProcessor) AvailableFor(params ProcessorInputParams) (bool, error) {
|
2024-06-05 07:56:02 +00:00
|
|
|
if params.FromToken == nil || params.ToToken == nil {
|
2024-04-01 13:39:17 +00:00
|
|
|
return false, errors.New("token and toToken cannot be nil")
|
|
|
|
}
|
|
|
|
|
2024-06-05 07:56:02 +00:00
|
|
|
if params.FromChain.ChainID != params.ToChain.ChainID {
|
2024-04-01 13:39:17 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2024-06-05 07:56:02 +00:00
|
|
|
s.paraswapClient.SetChainID(params.FromChain.ChainID)
|
2024-04-01 13:39:17 +00:00
|
|
|
|
2024-06-05 07:56:02 +00:00
|
|
|
searchForToken := params.FromToken.Address == ZeroAddress
|
|
|
|
searchForToToken := params.ToToken.Address == ZeroAddress
|
2024-04-01 13:39:17 +00:00
|
|
|
if searchForToToken || searchForToken {
|
|
|
|
tokensList, err := s.paraswapClient.FetchTokensList(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range tokensList {
|
2024-06-05 07:56:02 +00:00
|
|
|
if searchForToken && t.Symbol == params.FromToken.Symbol {
|
|
|
|
params.FromToken.Address = common.HexToAddress(t.Address)
|
|
|
|
params.FromToken.Decimals = t.Decimals
|
2024-04-01 13:39:17 +00:00
|
|
|
if !searchForToToken {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-05 07:56:02 +00:00
|
|
|
if searchForToToken && t.Symbol == params.ToToken.Symbol {
|
|
|
|
params.ToToken.Address = common.HexToAddress(t.Address)
|
|
|
|
params.ToToken.Decimals = t.Decimals
|
2024-04-01 13:39:17 +00:00
|
|
|
if !searchForToken {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-05 07:56:02 +00:00
|
|
|
if params.FromToken.Address == ZeroAddress || params.ToToken.Address == ZeroAddress {
|
2024-04-01 13:39:17 +00:00
|
|
|
return false, errors.New("cannot resolve token/s")
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *SwapParaswapProcessor) CalculateFees(params ProcessorInputParams) (*big.Int, *big.Int, error) {
|
|
|
|
return ZeroBigIntValue, ZeroBigIntValue, nil
|
2024-04-01 13:39:17 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *SwapParaswapProcessor) PackTxInputData(params ProcessorInputParams, contractType string) ([]byte, error) {
|
2024-05-21 14:33:36 +00:00
|
|
|
// not sure what we can do here since we're using the api to build the transaction
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *SwapParaswapProcessor) EstimateGas(params ProcessorInputParams) (uint64, error) {
|
2024-06-05 07:56:02 +00:00
|
|
|
priceRoute, err := s.paraswapClient.FetchPriceRoute(context.Background(), params.FromToken.Address, params.FromToken.Decimals,
|
|
|
|
params.ToToken.Address, params.ToToken.Decimals, params.AmountIn, params.FromAddr, params.ToAddr)
|
2024-04-01 13:39:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.priceRoute = priceRoute
|
|
|
|
|
|
|
|
return priceRoute.GasCost.Uint64(), nil
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *SwapParaswapProcessor) GetContractAddress(params ProcessorInputParams) (address common.Address, err error) {
|
2024-06-05 07:56:02 +00:00
|
|
|
if params.FromChain.ChainID == walletCommon.EthereumMainnet {
|
2024-04-01 13:39:17 +00:00
|
|
|
address = common.HexToAddress("0x216b4b4ba9f3e719726886d34a177484278bfcae")
|
2024-06-05 07:56:02 +00:00
|
|
|
} else if params.FromChain.ChainID == walletCommon.ArbitrumMainnet {
|
2024-04-01 13:39:17 +00:00
|
|
|
address = common.HexToAddress("0x216b4b4ba9f3e719726886d34a177484278bfcae")
|
2024-06-05 07:56:02 +00:00
|
|
|
} else if params.FromChain.ChainID == walletCommon.OptimismMainnet {
|
2024-04-01 13:39:17 +00:00
|
|
|
address = common.HexToAddress("0x216b4b4ba9f3e719726886d34a177484278bfcae")
|
2024-05-23 12:38:39 +00:00
|
|
|
} else {
|
|
|
|
err = errors.New("unsupported network")
|
2024-04-01 13:39:17 +00:00
|
|
|
}
|
2024-05-23 12:38:39 +00:00
|
|
|
return
|
2024-04-01 13:39:17 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *SwapParaswapProcessor) BuildTx(params ProcessorInputParams) (*ethTypes.Transaction, error) {
|
2024-06-05 07:56:02 +00:00
|
|
|
toAddr := types.Address(params.ToAddr)
|
2024-06-06 20:08:25 +00:00
|
|
|
sendArgs := &MultipathProcessorTxArgs{
|
|
|
|
SwapTx: &SwapParaswapTxArgs{
|
2024-04-01 13:39:17 +00:00
|
|
|
SendTxArgs: transactions.SendTxArgs{
|
2024-06-05 07:56:02 +00:00
|
|
|
From: types.Address(params.FromAddr),
|
2024-04-01 13:39:17 +00:00
|
|
|
To: &toAddr,
|
2024-06-05 07:56:02 +00:00
|
|
|
Value: (*hexutil.Big)(params.AmountIn),
|
2024-04-01 13:39:17 +00:00
|
|
|
Data: types.HexBytes("0x0"),
|
2024-06-05 07:56:02 +00:00
|
|
|
Symbol: params.FromToken.Symbol,
|
2024-04-01 13:39:17 +00:00
|
|
|
},
|
2024-06-05 07:56:02 +00:00
|
|
|
ChainID: params.FromChain.ChainID,
|
2024-04-01 13:39:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.BuildTransaction(sendArgs)
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *SwapParaswapProcessor) prepareTransaction(sendArgs *MultipathProcessorTxArgs) error {
|
2024-04-01 13:39:17 +00:00
|
|
|
tx, err := s.paraswapClient.BuildTransaction(context.Background(), s.priceRoute.SrcTokenAddress, s.priceRoute.SrcTokenDecimals, s.priceRoute.SrcAmount.Int,
|
|
|
|
s.priceRoute.DestTokenAddress, s.priceRoute.DestTokenDecimals, s.priceRoute.DestAmount.Int, common.Address(sendArgs.SwapTx.From), common.Address(*sendArgs.SwapTx.To), s.priceRoute.RawPriceRoute)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
value, ok := new(big.Int).SetString(tx.Value, 10)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("error converting amount to big.Int")
|
|
|
|
}
|
|
|
|
|
|
|
|
gas, err := strconv.ParseUint(tx.Gas, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
gasPrice, ok := new(big.Int).SetString(tx.GasPrice, 10)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("error converting amount to big.Int")
|
|
|
|
}
|
|
|
|
|
|
|
|
sendArgs.ChainID = tx.ChainID
|
|
|
|
sendArgs.SwapTx.ChainID = tx.ChainID
|
|
|
|
toAddr := types.HexToAddress(tx.To)
|
|
|
|
sendArgs.SwapTx.From = types.HexToAddress(tx.From)
|
|
|
|
sendArgs.SwapTx.To = &toAddr
|
|
|
|
sendArgs.SwapTx.Value = (*hexutil.Big)(value)
|
|
|
|
sendArgs.SwapTx.Gas = (*hexutil.Uint64)(&gas)
|
|
|
|
sendArgs.SwapTx.GasPrice = (*hexutil.Big)(gasPrice)
|
|
|
|
sendArgs.SwapTx.Data = types.Hex2Bytes(tx.Data)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *SwapParaswapProcessor) BuildTransaction(sendArgs *MultipathProcessorTxArgs) (*ethTypes.Transaction, error) {
|
2024-04-01 13:39:17 +00:00
|
|
|
err := s.prepareTransaction(sendArgs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return s.transactor.ValidateAndBuildTransaction(sendArgs.ChainID, sendArgs.SwapTx.SendTxArgs)
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *SwapParaswapProcessor) Send(sendArgs *MultipathProcessorTxArgs, verifiedAccount *account.SelectedExtKey) (types.Hash, error) {
|
2024-04-01 13:39:17 +00:00
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
txBridgeArgs := &MultipathProcessorTxArgs{
|
|
|
|
SwapTx: &SwapParaswapTxArgs{
|
2024-04-01 13:39:17 +00:00
|
|
|
SendTxArgs: transactions.SendTxArgs{
|
|
|
|
From: sendArgs.SwapTx.From,
|
|
|
|
To: sendArgs.SwapTx.To,
|
|
|
|
MultiTransactionID: sendArgs.SwapTx.MultiTransactionID,
|
|
|
|
Symbol: sendArgs.SwapTx.Symbol,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := s.prepareTransaction(txBridgeArgs)
|
|
|
|
if err != nil {
|
|
|
|
return types.Hash{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.transactor.SendTransactionWithChainID(txBridgeArgs.ChainID, txBridgeArgs.SwapTx.SendTxArgs, verifiedAccount)
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *SwapParaswapProcessor) CalculateAmountOut(params ProcessorInputParams) (*big.Int, error) {
|
2024-04-01 13:39:17 +00:00
|
|
|
return s.priceRoute.DestAmount.Int, nil
|
|
|
|
}
|