github.com/status-im/status-go@v1.1.0/services/wallet/router/pathprocessor/processor_ens_release.go (about)

     1  package pathprocessor
     2  
     3  import (
     4  	"context"
     5  	"math/big"
     6  	"strings"
     7  
     8  	"github.com/ethereum/go-ethereum"
     9  	"github.com/ethereum/go-ethereum/accounts/abi"
    10  	"github.com/ethereum/go-ethereum/common"
    11  	ethTypes "github.com/ethereum/go-ethereum/core/types"
    12  	"github.com/status-im/status-go/account"
    13  	"github.com/status-im/status-go/contracts"
    14  	"github.com/status-im/status-go/contracts/registrar"
    15  	"github.com/status-im/status-go/eth-node/types"
    16  	"github.com/status-im/status-go/rpc"
    17  	"github.com/status-im/status-go/services/ens"
    18  	walletCommon "github.com/status-im/status-go/services/wallet/common"
    19  	"github.com/status-im/status-go/transactions"
    20  )
    21  
    22  type ENSReleaseProcessor struct {
    23  	contractMaker *contracts.ContractMaker
    24  	transactor    transactions.TransactorIface
    25  	ensService    *ens.Service
    26  }
    27  
    28  func NewENSReleaseProcessor(rpcClient *rpc.Client, transactor transactions.TransactorIface, ensService *ens.Service) *ENSReleaseProcessor {
    29  	return &ENSReleaseProcessor{
    30  		contractMaker: &contracts.ContractMaker{
    31  			RPCClient: rpcClient,
    32  		},
    33  		transactor: transactor,
    34  		ensService: ensService,
    35  	}
    36  }
    37  
    38  func createENSReleaseErrorResponse(err error) error {
    39  	return createErrorResponse(ProcessorENSReleaseName, err)
    40  }
    41  
    42  func (s *ENSReleaseProcessor) Name() string {
    43  	return ProcessorENSReleaseName
    44  }
    45  
    46  func (s *ENSReleaseProcessor) AvailableFor(params ProcessorInputParams) (bool, error) {
    47  	return params.FromChain.ChainID == walletCommon.EthereumMainnet || params.FromChain.ChainID == walletCommon.EthereumSepolia, nil
    48  }
    49  
    50  func (s *ENSReleaseProcessor) CalculateFees(params ProcessorInputParams) (*big.Int, *big.Int, error) {
    51  	return ZeroBigIntValue, ZeroBigIntValue, nil
    52  }
    53  
    54  func (s *ENSReleaseProcessor) PackTxInputData(params ProcessorInputParams) ([]byte, error) {
    55  	registrarABI, err := abi.JSON(strings.NewReader(registrar.UsernameRegistrarABI))
    56  	if err != nil {
    57  		return []byte{}, createENSReleaseErrorResponse(err)
    58  	}
    59  
    60  	return registrarABI.Pack("release", ens.UsernameToLabel(params.Username))
    61  }
    62  
    63  func (s *ENSReleaseProcessor) EstimateGas(params ProcessorInputParams) (uint64, error) {
    64  	if params.TestsMode {
    65  		if params.TestEstimationMap != nil {
    66  			if val, ok := params.TestEstimationMap[s.Name()]; ok {
    67  				return val.Value, val.Err
    68  			}
    69  		}
    70  		return 0, ErrNoEstimationFound
    71  	}
    72  
    73  	contractAddress, err := s.GetContractAddress(params)
    74  	if err != nil {
    75  		return 0, createENSReleaseErrorResponse(err)
    76  	}
    77  
    78  	input, err := s.PackTxInputData(params)
    79  	if err != nil {
    80  		return 0, createENSReleaseErrorResponse(err)
    81  	}
    82  
    83  	ethClient, err := s.contractMaker.RPCClient.EthClient(params.FromChain.ChainID)
    84  	if err != nil {
    85  		return 0, createENSReleaseErrorResponse(err)
    86  	}
    87  
    88  	msg := ethereum.CallMsg{
    89  		From:  params.FromAddr,
    90  		To:    &contractAddress,
    91  		Value: ZeroBigIntValue,
    92  		Data:  input,
    93  	}
    94  
    95  	estimation, err := ethClient.EstimateGas(context.Background(), msg)
    96  	if err != nil {
    97  		return 0, createENSReleaseErrorResponse(err)
    98  	}
    99  
   100  	increasedEstimation := float64(estimation) * IncreaseEstimatedGasFactor
   101  
   102  	return uint64(increasedEstimation), nil
   103  }
   104  
   105  func (s *ENSReleaseProcessor) Send(sendArgs *MultipathProcessorTxArgs, lastUsedNonce int64, verifiedAccount *account.SelectedExtKey) (hash types.Hash, usedNonce uint64, err error) {
   106  	return s.transactor.SendTransactionWithChainID(sendArgs.ChainID, *sendArgs.TransferTx, lastUsedNonce, verifiedAccount)
   107  }
   108  
   109  func (s *ENSReleaseProcessor) BuildTransaction(sendArgs *MultipathProcessorTxArgs, lastUsedNonce int64) (*ethTypes.Transaction, uint64, error) {
   110  	return s.transactor.ValidateAndBuildTransaction(sendArgs.ChainID, *sendArgs.TransferTx, lastUsedNonce)
   111  }
   112  
   113  func (s *ENSReleaseProcessor) CalculateAmountOut(params ProcessorInputParams) (*big.Int, error) {
   114  	return params.AmountIn, nil
   115  }
   116  
   117  func (s *ENSReleaseProcessor) GetContractAddress(params ProcessorInputParams) (common.Address, error) {
   118  	addr, err := s.ensService.API().GetRegistrarAddress(context.Background(), params.FromChain.ChainID)
   119  	if err != nil {
   120  		return common.Address{}, err
   121  	}
   122  	if addr == ZeroAddress {
   123  		return common.Address{}, ErrENSRegistrarNotFound
   124  	}
   125  	return addr, nil
   126  }