2024-06-11 08:00:17 +00:00
|
|
|
package pathprocessor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/big"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum"
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
ethTypes "github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/status-im/status-go/account"
|
|
|
|
"github.com/status-im/status-go/contracts"
|
|
|
|
"github.com/status-im/status-go/contracts/snt"
|
|
|
|
stickersContracts "github.com/status-im/status-go/contracts/stickers"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
"github.com/status-im/status-go/rpc"
|
|
|
|
"github.com/status-im/status-go/services/stickers"
|
|
|
|
walletCommon "github.com/status-im/status-go/services/wallet/common"
|
|
|
|
"github.com/status-im/status-go/transactions"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StickersBuyProcessor struct {
|
|
|
|
contractMaker *contracts.ContractMaker
|
|
|
|
transactor transactions.TransactorIface
|
|
|
|
stickersService *stickers.Service
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStickersBuyProcessor(rpcClient *rpc.Client, transactor transactions.TransactorIface, stickersService *stickers.Service) *StickersBuyProcessor {
|
|
|
|
return &StickersBuyProcessor{
|
|
|
|
contractMaker: &contracts.ContractMaker{
|
|
|
|
RPCClient: rpcClient,
|
|
|
|
},
|
|
|
|
transactor: transactor,
|
|
|
|
stickersService: stickersService,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-18 12:20:54 +00:00
|
|
|
func createStickersBuyErrorResponse(err error) error {
|
|
|
|
return createErrorResponse(ProcessorStickersBuyName, err)
|
|
|
|
}
|
|
|
|
|
2024-06-11 08:00:17 +00:00
|
|
|
func (s *StickersBuyProcessor) Name() string {
|
|
|
|
return ProcessorStickersBuyName
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StickersBuyProcessor) AvailableFor(params ProcessorInputParams) (bool, error) {
|
|
|
|
return params.FromChain.ChainID == walletCommon.EthereumMainnet || params.FromChain.ChainID == walletCommon.EthereumSepolia, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StickersBuyProcessor) CalculateFees(params ProcessorInputParams) (*big.Int, *big.Int, error) {
|
2024-09-23 06:35:34 +00:00
|
|
|
return walletCommon.ZeroBigIntValue(), walletCommon.ZeroBigIntValue(), nil
|
2024-06-11 08:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StickersBuyProcessor) PackTxInputData(params ProcessorInputParams) ([]byte, error) {
|
|
|
|
stickerType, err := s.contractMaker.NewStickerType(params.FromChain.ChainID)
|
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return []byte{}, createStickersBuyErrorResponse(err)
|
2024-06-11 08:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
callOpts := &bind.CallOpts{Context: context.Background(), Pending: false}
|
|
|
|
|
|
|
|
packInfo, err := stickerType.GetPackData(callOpts, params.PackID)
|
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return []byte{}, createStickersBuyErrorResponse(err)
|
2024-06-11 08:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stickerMarketABI, err := abi.JSON(strings.NewReader(stickersContracts.StickerMarketABI))
|
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return []byte{}, createStickersBuyErrorResponse(err)
|
2024-06-11 08:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extraData, err := stickerMarketABI.Pack("buyToken", params.PackID, params.FromAddr, packInfo.Price)
|
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return []byte{}, createStickersBuyErrorResponse(err)
|
2024-06-11 08:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sntABI, err := abi.JSON(strings.NewReader(snt.SNTABI))
|
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return []byte{}, createStickersBuyErrorResponse(err)
|
2024-06-11 08:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stickerMarketAddress, err := stickersContracts.StickerMarketContractAddress(params.FromChain.ChainID)
|
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return []byte{}, createStickersBuyErrorResponse(err)
|
2024-06-11 08:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return sntABI.Pack("approveAndCall", stickerMarketAddress, packInfo.Price, extraData)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StickersBuyProcessor) 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-11 08:00:17 +00:00
|
|
|
contractAddress, err := s.GetContractAddress(params)
|
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return 0, createStickersBuyErrorResponse(err)
|
2024-06-11 08:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
input, err := s.PackTxInputData(params)
|
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return 0, createStickersBuyErrorResponse(err)
|
2024-06-11 08:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ethClient, err := s.contractMaker.RPCClient.EthClient(params.FromChain.ChainID)
|
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return 0, createStickersBuyErrorResponse(err)
|
2024-06-11 08:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := ethereum.CallMsg{
|
|
|
|
From: params.FromAddr,
|
|
|
|
To: &contractAddress,
|
2024-09-23 06:35:34 +00:00
|
|
|
Value: walletCommon.ZeroBigIntValue(),
|
2024-06-11 08:00:17 +00:00
|
|
|
Data: input,
|
|
|
|
}
|
|
|
|
|
|
|
|
estimation, err := ethClient.EstimateGas(context.Background(), msg)
|
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return 0, createStickersBuyErrorResponse(err)
|
2024-06-11 08:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
increasedEstimation := float64(estimation) * IncreaseEstimatedGasFactor
|
|
|
|
|
|
|
|
return uint64(increasedEstimation), nil
|
|
|
|
}
|
|
|
|
|
2024-08-12 12:07:32 +00:00
|
|
|
func (s *StickersBuyProcessor) Send(sendArgs *MultipathProcessorTxArgs, lastUsedNonce int64, verifiedAccount *account.SelectedExtKey) (hash types.Hash, usedNonce uint64, err error) {
|
|
|
|
return s.transactor.SendTransactionWithChainID(sendArgs.ChainID, *sendArgs.TransferTx, lastUsedNonce, verifiedAccount)
|
2024-06-11 08:00:17 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 12:07:32 +00:00
|
|
|
func (s *StickersBuyProcessor) BuildTransaction(sendArgs *MultipathProcessorTxArgs, lastUsedNonce int64) (*ethTypes.Transaction, uint64, error) {
|
|
|
|
return s.transactor.ValidateAndBuildTransaction(sendArgs.ChainID, *sendArgs.TransferTx, lastUsedNonce)
|
2024-06-11 08:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StickersBuyProcessor) CalculateAmountOut(params ProcessorInputParams) (*big.Int, error) {
|
|
|
|
return params.AmountIn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StickersBuyProcessor) GetContractAddress(params ProcessorInputParams) (common.Address, error) {
|
|
|
|
return snt.ContractAddress(params.FromChain.ChainID)
|
|
|
|
}
|