chore_: stickers buy added to the router

This commit is contained in:
Sale Djenic 2024-06-11 10:00:17 +02:00 committed by saledjenic
parent 74e9ce93bf
commit ddc75815eb
7 changed files with 186 additions and 12 deletions

View File

@ -27,4 +27,5 @@ const (
ProcessorENSRegisterName = "ENSRegister"
ProcessorENSReleaseName = "ENSRelease"
ProcessorENSPublicKeyName = "ENSPublicKey"
ProcessorStickersBuyName = "StickersBuy"
)

View File

@ -42,4 +42,5 @@ type ProcessorInputParams struct {
BonderFee *big.Int
Username string
PublicKey string
PackID *big.Int
}

View File

@ -33,7 +33,6 @@ func NewENSRegisterProcessor(rpcClient *rpc.Client, transactor transactions.Tran
contractMaker: &contracts.ContractMaker{
RPCClient: rpcClient,
},
// rpcClient: rpcClient,
transactor: transactor,
ensService: ensService,
}

View File

@ -0,0 +1,156 @@
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"
"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/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,
}
}
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) {
return ZeroBigIntValue, ZeroBigIntValue, nil
}
func (s *StickersBuyProcessor) PackTxInputData(params ProcessorInputParams) ([]byte, error) {
stickerType, err := s.contractMaker.NewStickerType(params.FromChain.ChainID)
if err != nil {
return []byte{}, err
}
callOpts := &bind.CallOpts{Context: context.Background(), Pending: false}
packInfo, err := stickerType.GetPackData(callOpts, params.PackID)
if err != nil {
return []byte{}, err
}
stickerMarketABI, err := abi.JSON(strings.NewReader(stickersContracts.StickerMarketABI))
if err != nil {
return []byte{}, err
}
extraData, err := stickerMarketABI.Pack("buyToken", params.PackID, params.FromAddr, packInfo.Price)
if err != nil {
return []byte{}, err
}
sntABI, err := abi.JSON(strings.NewReader(snt.SNTABI))
if err != nil {
return []byte{}, err
}
stickerMarketAddress, err := stickersContracts.StickerMarketContractAddress(params.FromChain.ChainID)
if err != nil {
return []byte{}, err
}
return sntABI.Pack("approveAndCall", stickerMarketAddress, packInfo.Price, extraData)
}
func (s *StickersBuyProcessor) EstimateGas(params ProcessorInputParams) (uint64, error) {
contractAddress, err := s.GetContractAddress(params)
if err != nil {
return 0, err
}
input, err := s.PackTxInputData(params)
if err != nil {
return 0, err
}
ethClient, err := s.contractMaker.RPCClient.EthClient(params.FromChain.ChainID)
if err != nil {
return 0, err
}
msg := ethereum.CallMsg{
From: params.FromAddr,
To: &contractAddress,
Value: ZeroBigIntValue,
Data: input,
}
estimation, err := ethClient.EstimateGas(context.Background(), msg)
if err != nil {
return 0, err
}
increasedEstimation := float64(estimation) * IncreaseEstimatedGasFactor
return uint64(increasedEstimation), nil
}
func (s *StickersBuyProcessor) BuildTx(params ProcessorInputParams) (*ethTypes.Transaction, error) {
toAddr := types.Address(params.ToAddr)
inputData, err := s.PackTxInputData(params)
if err != nil {
return nil, err
}
sendArgs := &MultipathProcessorTxArgs{
TransferTx: &transactions.SendTxArgs{
From: types.Address(params.FromAddr),
To: &toAddr,
Value: (*hexutil.Big)(ZeroBigIntValue),
Data: inputData,
},
ChainID: params.FromChain.ChainID,
}
return s.BuildTransaction(sendArgs)
}
func (s *StickersBuyProcessor) Send(sendArgs *MultipathProcessorTxArgs, verifiedAccount *account.SelectedExtKey) (hash types.Hash, err error) {
return s.transactor.SendTransactionWithChainID(sendArgs.ChainID, *sendArgs.TransferTx, verifiedAccount)
}
func (s *StickersBuyProcessor) BuildTransaction(sendArgs *MultipathProcessorTxArgs) (*ethTypes.Transaction, error) {
return s.transactor.ValidateAndBuildTransaction(sendArgs.ChainID, *sendArgs.TransferTx)
}
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)
}

View File

@ -286,6 +286,7 @@ func NewRouter(rpcClient *rpc.Client, transactor *transactions.Transactor, token
ensRegister := pathprocessor.NewENSRegisterProcessor(rpcClient, transactor, ensService)
ensRelease := pathprocessor.NewENSReleaseProcessor(rpcClient, transactor, ensService)
ensPublicKey := pathprocessor.NewENSPublicKeyProcessor(rpcClient, transactor, ensService)
buyStickers := pathprocessor.NewStickersBuyProcessor(rpcClient, transactor, stickersService)
processors[transfer.Name()] = transfer
processors[erc721Transfer.Name()] = erc721Transfer
@ -296,6 +297,7 @@ func NewRouter(rpcClient *rpc.Client, transactor *transactions.Transactor, token
processors[ensRegister.Name()] = ensRegister
processors[ensRelease.Name()] = ensRelease
processors[ensPublicKey.Name()] = ensPublicKey
processors[buyStickers.Name()] = buyStickers
return &Router{
rpcClient: rpcClient,
@ -342,7 +344,7 @@ type Router struct {
func (r *Router) requireApproval(ctx context.Context, sendType SendType, approvalContractAddress *common.Address, account common.Address, network *params.Network, token *token.Token, amountIn *big.Int) (
bool, *big.Int, uint64, uint64, error) {
if sendType.IsCollectiblesTransfer() || sendType.IsEnsTransfer() {
if sendType.IsCollectiblesTransfer() || sendType.IsEnsTransfer() || sendType.IsStickersTransfer() {
return false, nil, 0, 0, nil
}
@ -550,7 +552,8 @@ func (r *Router) SuggestedRoutes(
// Skip processors that are added because of the Router V2, to not break the current functionality
if pProcessor.Name() == pathprocessor.ProcessorENSRegisterName ||
pProcessor.Name() == pathprocessor.ProcessorENSReleaseName ||
pProcessor.Name() == pathprocessor.ProcessorENSPublicKeyName {
pProcessor.Name() == pathprocessor.ProcessorENSPublicKeyName ||
pProcessor.Name() == pathprocessor.ProcessorStickersBuyName {
continue
}

View File

@ -43,6 +43,10 @@ func (s SendType) IsEnsTransfer() bool {
return s == ENSRegister || s == ENSRelease || s == ENSSetPubKey
}
func (s SendType) IsStickersTransfer() bool {
return s == StickersBuy
}
func (s SendType) FetchPrices(marketManager *market.Manager, tokenID string) (map[string]float64, error) {
symbols := []string{tokenID, "ETH"}
if s.IsCollectiblesTransfer() {
@ -96,7 +100,7 @@ func (s SendType) isTransfer(routerV2Logic bool) bool {
}
func (s SendType) needL1Fee() bool {
return s != ENSRegister && s != ENSRelease && s != ENSSetPubKey && s != StickersBuy
return !s.IsEnsTransfer() && !s.IsStickersTransfer()
}
func (s SendType) canUseProcessor(p pathprocessor.PathProcessor) bool {
@ -112,13 +116,18 @@ func (s SendType) canUseProcessor(p pathprocessor.PathProcessor) bool {
return pathProcessorName == pathprocessor.ProcessorENSReleaseName
case ENSSetPubKey:
return pathProcessorName == pathprocessor.ProcessorENSPublicKeyName
case StickersBuy:
return pathProcessorName == pathprocessor.ProcessorStickersBuyName
default:
return true
}
}
func (s SendType) isAvailableBetween(from, to *params.Network) bool {
if s.IsCollectiblesTransfer() || s.IsEnsTransfer() {
if s.IsCollectiblesTransfer() ||
s.IsEnsTransfer() ||
s.IsStickersTransfer() ||
s == Swap {
return from.ChainID == to.ChainID
}
@ -126,10 +135,6 @@ func (s SendType) isAvailableBetween(from, to *params.Network) bool {
return from.ChainID != to.ChainID
}
if s == Swap {
return from.ChainID == to.ChainID
}
return true
}
@ -153,7 +158,7 @@ func (s SendType) isAvailableFor(network *params.Network) bool {
return swapAllowedNetworks[network.ChainID]
}
if s.IsEnsTransfer() {
if s.IsEnsTransfer() || s.IsStickersTransfer() {
return network.ChainID == walletCommon.EthereumMainnet || network.ChainID == walletCommon.EthereumSepolia
}

View File

@ -13,6 +13,7 @@ import (
"github.com/status-im/status-go/params"
"github.com/status-im/status-go/services/ens"
"github.com/status-im/status-go/services/wallet/async"
"github.com/status-im/status-go/services/wallet/bigint"
walletCommon "github.com/status-im/status-go/services/wallet/common"
"github.com/status-im/status-go/services/wallet/router/pathprocessor"
walletToken "github.com/status-im/status-go/services/wallet/token"
@ -47,8 +48,9 @@ type RouteInputParams struct {
TestnetMode bool `json:"testnetMode"`
// For send types like EnsRegister, EnsRelease, EnsSetPubKey, StickersBuy
Username string `json:"username"`
PublicKey string `json:"publicKey"`
Username string `json:"username"`
PublicKey string `json:"publicKey"`
PackID *bigint.BigInt `json:"packID"`
}
type PathV2 struct {
@ -318,6 +320,12 @@ func validateInputData(input *RouteInputParams) error {
}
}
if input.SendType == StickersBuy {
if input.PackID == nil {
return errors.New("packID is required for StickersBuy")
}
}
if input.FromLockedAmount != nil && len(input.FromLockedAmount) > 0 {
for chainID, amount := range input.FromLockedAmount {
if input.TestnetMode {
@ -442,6 +450,7 @@ func (r *Router) SuggestedRoutesV2(ctx context.Context, input *RouteInputParams)
Username: input.Username,
PublicKey: input.PublicKey,
PackID: input.PackID.Int,
}
can, err := pProcessor.AvailableFor(ProcessorInputParams)