2024-06-06 20:08:25 +00:00
|
|
|
package pathprocessor
|
2024-04-01 13:39:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/big"
|
|
|
|
"strconv"
|
2024-06-14 11:27:53 +00:00
|
|
|
"sync"
|
2024-04-01 13:39:17 +00:00
|
|
|
|
|
|
|
"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
|
2024-06-12 20:14:30 +00:00
|
|
|
ChainID uint64 `json:"chainId"`
|
2024-06-30 21:38:32 +00:00
|
|
|
ChainIDTo uint64 `json:"chainIdTo"`
|
2024-06-14 11:27:53 +00:00
|
|
|
TokenIDFrom string `json:"tokenIdFrom"`
|
|
|
|
TokenIDTo string `json:"tokenIdTo"`
|
2024-06-12 20:14:30 +00:00
|
|
|
SlippagePercentage float32 `json:"slippagePercentage"`
|
2024-04-01 13:39:17 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
type SwapParaswapProcessor struct {
|
2024-08-07 22:33:44 +00:00
|
|
|
paraswapClient paraswap.ClientInterface
|
2024-05-26 08:31:13 +00:00
|
|
|
transactor transactions.TransactorIface
|
2024-06-14 11:27:53 +00:00
|
|
|
priceRoute sync.Map // [fromChainName-toChainName-fromTokenSymbol-toTokenSymbol, paraswap.Route]
|
2024-04-01 13:39:17 +00:00
|
|
|
}
|
|
|
|
|
2024-07-18 20:14:42 +00:00
|
|
|
const (
|
|
|
|
partnerID = "status.app"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getPartnerAddressAndFeePcnt(chainID uint64) (common.Address, float64) {
|
|
|
|
const partnerFeePcnt = 0.7
|
|
|
|
|
|
|
|
switch chainID {
|
|
|
|
case walletCommon.EthereumMainnet:
|
|
|
|
return common.HexToAddress("0xd9abc564bfabefa88a6C2723d78124579600F568"), partnerFeePcnt
|
|
|
|
case walletCommon.OptimismMainnet:
|
|
|
|
return common.HexToAddress("0xE9B59dC0b30cd4646430c25de0111D651c395775"), partnerFeePcnt
|
|
|
|
case walletCommon.ArbitrumMainnet:
|
|
|
|
return common.HexToAddress("0x9a8278e856C0B191B9daa2d7DD1f7B28268E4DA2"), partnerFeePcnt
|
|
|
|
}
|
|
|
|
return common.Address{}, 0
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func NewSwapParaswapProcessor(rpcClient *rpc.Client, transactor transactions.TransactorIface, tokenManager *walletToken.Manager) *SwapParaswapProcessor {
|
2024-07-18 20:14:42 +00:00
|
|
|
defaultChainID := walletCommon.EthereumMainnet
|
|
|
|
partnerAddress, partnerFeePcnt := getPartnerAddressAndFeePcnt(defaultChainID)
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
return &SwapParaswapProcessor{
|
2024-07-18 20:14:42 +00:00
|
|
|
paraswapClient: paraswap.NewClientV5(
|
|
|
|
defaultChainID,
|
|
|
|
partnerID,
|
|
|
|
partnerAddress,
|
|
|
|
partnerFeePcnt,
|
|
|
|
),
|
|
|
|
transactor: transactor,
|
|
|
|
priceRoute: sync.Map{},
|
2024-04-01 13:39:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-18 12:20:54 +00:00
|
|
|
func createSwapParaswapErrorResponse(err error) error {
|
|
|
|
switch err.Error() {
|
|
|
|
case "Price Timeout":
|
|
|
|
return ErrPriceTimeout
|
|
|
|
case "No routes found with enough liquidity":
|
|
|
|
return ErrNotEnoughLiquidity
|
2024-08-07 22:33:44 +00:00
|
|
|
case "ESTIMATED_LOSS_GREATER_THAN_MAX_IMPACT":
|
|
|
|
return ErrPriceImpactTooHigh
|
2024-07-18 12:20:54 +00:00
|
|
|
}
|
|
|
|
return createErrorResponse(ProcessorSwapParaswapName, err)
|
|
|
|
}
|
|
|
|
|
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-14 11:27:53 +00:00
|
|
|
func (s *SwapParaswapProcessor) Clear() {
|
|
|
|
s.priceRoute = sync.Map{}
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *SwapParaswapProcessor) AvailableFor(params ProcessorInputParams) (bool, error) {
|
2024-06-18 10:31:23 +00:00
|
|
|
if params.FromChain == nil || params.ToChain == nil {
|
|
|
|
return false, ErrNoChainSet
|
|
|
|
}
|
2024-06-05 07:56:02 +00:00
|
|
|
if params.FromToken == nil || params.ToToken == nil {
|
2024-06-18 10:31:23 +00:00
|
|
|
return false, ErrToAndFromTokensMustBeSet
|
2024-04-01 13:39:17 +00:00
|
|
|
}
|
|
|
|
|
2024-06-05 07:56:02 +00:00
|
|
|
if params.FromChain.ChainID != params.ToChain.ChainID {
|
2024-06-18 10:31:23 +00:00
|
|
|
return false, ErrFromAndToChainsMustBeSame
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.FromToken.Symbol == params.ToToken.Symbol {
|
|
|
|
return false, ErrFromAndToTokensMustBeDifferent
|
2024-04-01 13:39:17 +00:00
|
|
|
}
|
|
|
|
|
2024-07-18 20:14:42 +00:00
|
|
|
chainID := params.FromChain.ChainID
|
|
|
|
partnerAddress, partnerFeePcnt := getPartnerAddressAndFeePcnt(chainID)
|
|
|
|
s.paraswapClient.SetChainID(chainID)
|
|
|
|
s.paraswapClient.SetPartnerAddress(partnerAddress)
|
|
|
|
s.paraswapClient.SetPartnerFeePcnt(partnerFeePcnt)
|
2024-04-01 13:39:17 +00:00
|
|
|
|
2024-09-23 06:35:34 +00:00
|
|
|
searchForToken := params.FromToken.Address == walletCommon.ZeroAddress()
|
|
|
|
searchForToToken := params.ToToken.Address == walletCommon.ZeroAddress()
|
2024-04-01 13:39:17 +00:00
|
|
|
if searchForToToken || searchForToken {
|
|
|
|
tokensList, err := s.paraswapClient.FetchTokensList(context.Background())
|
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return false, createSwapParaswapErrorResponse(err)
|
2024-04-01 13:39:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-09-23 06:35:34 +00:00
|
|
|
if params.FromToken.Address == walletCommon.ZeroAddress() || params.ToToken.Address == walletCommon.ZeroAddress() {
|
2024-06-18 10:31:23 +00:00
|
|
|
return false, ErrCannotResolveTokens
|
2024-04-01 13:39:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2024-07-18 20:14:42 +00:00
|
|
|
func calcReceivedAmountAndFee(baseDestAmount *big.Int, feePcnt float64) (destAmount *big.Int, destFee *big.Int) {
|
|
|
|
destAmount = new(big.Int).Set(baseDestAmount)
|
|
|
|
destFee = new(big.Int).SetUint64(0)
|
|
|
|
|
|
|
|
if feePcnt > 0 {
|
|
|
|
baseDestAmountFloat := new(big.Float).SetInt(baseDestAmount)
|
|
|
|
feePcntFloat := big.NewFloat(feePcnt / 100.0)
|
|
|
|
|
|
|
|
destFeeFloat := new(big.Float).Set(baseDestAmountFloat)
|
|
|
|
destFeeFloat = destFeeFloat.Mul(destFeeFloat, feePcntFloat)
|
|
|
|
destFeeFloat.Int(destFee)
|
|
|
|
|
|
|
|
destAmount = destAmount.Sub(destAmount, destFee)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *SwapParaswapProcessor) CalculateFees(params ProcessorInputParams) (*big.Int, *big.Int, error) {
|
2024-09-23 06:35:34 +00:00
|
|
|
return walletCommon.ZeroBigIntValue(), walletCommon.ZeroBigIntValue(), nil
|
2024-04-01 13:39:17 +00:00
|
|
|
}
|
|
|
|
|
2024-06-10 12:52:47 +00:00
|
|
|
func (s *SwapParaswapProcessor) PackTxInputData(params ProcessorInputParams) ([]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-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-06-12 20:14:30 +00:00
|
|
|
swapSide := paraswap.SellSide
|
2024-09-23 06:35:34 +00:00
|
|
|
if params.AmountOut != nil && params.AmountOut.Cmp(walletCommon.ZeroBigIntValue()) > 0 {
|
2024-06-12 20:14:30 +00:00
|
|
|
swapSide = paraswap.BuySide
|
|
|
|
}
|
|
|
|
|
2024-06-05 07:56:02 +00:00
|
|
|
priceRoute, err := s.paraswapClient.FetchPriceRoute(context.Background(), params.FromToken.Address, params.FromToken.Decimals,
|
2024-06-12 20:14:30 +00:00
|
|
|
params.ToToken.Address, params.ToToken.Decimals, params.AmountIn, params.FromAddr, params.ToAddr, swapSide)
|
2024-04-01 13:39:17 +00:00
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return 0, createSwapParaswapErrorResponse(err)
|
2024-04-01 13:39:17 +00:00
|
|
|
}
|
|
|
|
|
2024-06-14 11:27:53 +00:00
|
|
|
key := makeKey(params.FromChain.ChainID, params.ToChain.ChainID, params.FromToken.Symbol, params.ToToken.Symbol)
|
|
|
|
s.priceRoute.Store(key, &priceRoute)
|
2024-04-01 13:39:17 +00:00
|
|
|
|
|
|
|
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-07-24 16:21:11 +00:00
|
|
|
key := makeKey(params.FromChain.ChainID, params.ToChain.ChainID, params.FromToken.Symbol, params.ToToken.Symbol)
|
|
|
|
priceRouteIns, ok := s.priceRoute.Load(key)
|
|
|
|
if !ok {
|
|
|
|
err = ErrPriceRouteNotFound
|
|
|
|
return
|
2024-04-01 13:39:17 +00:00
|
|
|
}
|
2024-07-24 16:21:11 +00:00
|
|
|
priceRoute := priceRouteIns.(*paraswap.Route)
|
|
|
|
|
|
|
|
return priceRoute.TokenTransferProxy, nil
|
2024-04-01 13:39:17 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *SwapParaswapProcessor) prepareTransaction(sendArgs *MultipathProcessorTxArgs) error {
|
2024-06-12 20:14:30 +00:00
|
|
|
slippageBP := uint(sendArgs.SwapTx.SlippagePercentage * 100) // convert to basis points
|
|
|
|
|
2024-06-14 11:27:53 +00:00
|
|
|
key := makeKey(sendArgs.SwapTx.ChainID, sendArgs.SwapTx.ChainIDTo, sendArgs.SwapTx.TokenIDFrom, sendArgs.SwapTx.TokenIDTo)
|
|
|
|
priceRouteIns, ok := s.priceRoute.Load(key)
|
|
|
|
if !ok {
|
2024-06-18 10:31:23 +00:00
|
|
|
return ErrPriceRouteNotFound
|
2024-06-14 11:27:53 +00:00
|
|
|
}
|
|
|
|
priceRoute := priceRouteIns.(*paraswap.Route)
|
|
|
|
|
|
|
|
tx, err := s.paraswapClient.BuildTransaction(context.Background(), priceRoute.SrcTokenAddress, priceRoute.SrcTokenDecimals, priceRoute.SrcAmount.Int,
|
|
|
|
priceRoute.DestTokenAddress, priceRoute.DestTokenDecimals, priceRoute.DestAmount.Int, slippageBP,
|
2024-06-12 20:14:30 +00:00
|
|
|
common.Address(sendArgs.SwapTx.From), common.Address(*sendArgs.SwapTx.To),
|
2024-06-14 11:27:53 +00:00
|
|
|
priceRoute.RawPriceRoute, priceRoute.Side)
|
2024-04-01 13:39:17 +00:00
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return createSwapParaswapErrorResponse(err)
|
2024-04-01 13:39:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
value, ok := new(big.Int).SetString(tx.Value, 10)
|
|
|
|
if !ok {
|
2024-06-18 10:31:23 +00:00
|
|
|
return ErrConvertingAmountToBigInt
|
2024-04-01 13:39:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gas, err := strconv.ParseUint(tx.Gas, 10, 64)
|
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return createSwapParaswapErrorResponse(err)
|
2024-04-01 13:39:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gasPrice, ok := new(big.Int).SetString(tx.GasPrice, 10)
|
|
|
|
if !ok {
|
2024-06-18 10:31:23 +00:00
|
|
|
return ErrConvertingAmountToBigInt
|
2024-04-01 13:39:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-08-12 12:07:32 +00:00
|
|
|
func (s *SwapParaswapProcessor) BuildTransaction(sendArgs *MultipathProcessorTxArgs, lastUsedNonce int64) (*ethTypes.Transaction, uint64, error) {
|
2024-04-01 13:39:17 +00:00
|
|
|
err := s.prepareTransaction(sendArgs)
|
|
|
|
if err != nil {
|
2024-08-12 12:07:32 +00:00
|
|
|
return nil, 0, createSwapParaswapErrorResponse(err)
|
2024-04-01 13:39:17 +00:00
|
|
|
}
|
2024-08-12 12:07:32 +00:00
|
|
|
return s.transactor.ValidateAndBuildTransaction(sendArgs.ChainID, sendArgs.SwapTx.SendTxArgs, lastUsedNonce)
|
2024-04-01 13:39:17 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 12:07:32 +00:00
|
|
|
func (s *SwapParaswapProcessor) Send(sendArgs *MultipathProcessorTxArgs, lastUsedNonce int64, verifiedAccount *account.SelectedExtKey) (types.Hash, uint64, error) {
|
2024-06-30 21:38:32 +00:00
|
|
|
err := s.prepareTransaction(sendArgs)
|
2024-04-01 13:39:17 +00:00
|
|
|
if err != nil {
|
2024-08-12 12:07:32 +00:00
|
|
|
return types.Hash{}, 0, createSwapParaswapErrorResponse(err)
|
2024-04-01 13:39:17 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 12:07:32 +00:00
|
|
|
return s.transactor.SendTransactionWithChainID(sendArgs.ChainID, sendArgs.SwapTx.SendTxArgs, lastUsedNonce, verifiedAccount)
|
2024-04-01 13:39:17 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *SwapParaswapProcessor) CalculateAmountOut(params ProcessorInputParams) (*big.Int, error) {
|
2024-06-14 11:27:53 +00:00
|
|
|
key := makeKey(params.FromChain.ChainID, params.ToChain.ChainID, params.FromToken.Symbol, params.ToToken.Symbol)
|
|
|
|
priceRouteIns, ok := s.priceRoute.Load(key)
|
|
|
|
if !ok {
|
2024-06-18 10:31:23 +00:00
|
|
|
return nil, ErrPriceRouteNotFound
|
2024-06-14 11:27:53 +00:00
|
|
|
}
|
|
|
|
priceRoute := priceRouteIns.(*paraswap.Route)
|
|
|
|
|
2024-07-18 20:14:42 +00:00
|
|
|
_, partnerFeePcnt := getPartnerAddressAndFeePcnt(params.FromChain.ChainID)
|
|
|
|
destAmount, _ := calcReceivedAmountAndFee(priceRoute.DestAmount.Int, partnerFeePcnt)
|
|
|
|
|
|
|
|
return destAmount, nil
|
2024-04-01 13:39:17 +00:00
|
|
|
}
|