status-im/status-go

View on GitHub
services/wallet/router/pathprocessor/processor_ens_register.go

Summary

Maintainability
A
0 mins
Test Coverage
F
10%
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/registrar"
    "github.com/status-im/status-go/contracts/snt"
    statusErrors "github.com/status-im/status-go/errors"
    "github.com/status-im/status-go/eth-node/types"
    "github.com/status-im/status-go/rpc"
    "github.com/status-im/status-go/services/ens"
    walletCommon "github.com/status-im/status-go/services/wallet/common"
    "github.com/status-im/status-go/transactions"
)

type ENSRegisterProcessor struct {
    contractMaker *contracts.ContractMaker
    transactor    transactions.TransactorIface
    ensService    *ens.Service
}

func NewENSRegisterProcessor(rpcClient *rpc.Client, transactor transactions.TransactorIface, ensService *ens.Service) *ENSRegisterProcessor {
    return &ENSRegisterProcessor{
        contractMaker: &contracts.ContractMaker{
            RPCClient: rpcClient,
        },
        transactor: transactor,
        ensService: ensService,
    }
}

func (s *ENSRegisterProcessor) Name() string {
    return ProcessorENSRegisterName
}

func (s *ENSRegisterProcessor) GetPriceForRegisteringEnsName(chainID uint64) (*big.Int, error) {
    registryAddr, err := s.ensService.API().GetRegistrarAddress(context.Background(), chainID)
    if err != nil {
        return nil, statusErrors.CreateErrorResponseFromError(err)
    }
    registrar, err := s.contractMaker.NewUsernameRegistrar(chainID, registryAddr)
    if err != nil {
        return nil, statusErrors.CreateErrorResponseFromError(err)
    }

    callOpts := &bind.CallOpts{Context: context.Background(), Pending: false}
    return registrar.GetPrice(callOpts)
}

func (s *ENSRegisterProcessor) AvailableFor(params ProcessorInputParams) (bool, error) {
    return params.FromChain.ChainID == walletCommon.EthereumMainnet || params.FromChain.ChainID == walletCommon.EthereumSepolia, nil
}

func (s *ENSRegisterProcessor) CalculateFees(params ProcessorInputParams) (*big.Int, *big.Int, error) {
    return ZeroBigIntValue, ZeroBigIntValue, nil
}

func (s *ENSRegisterProcessor) PackTxInputData(params ProcessorInputParams) ([]byte, error) {
    price, err := s.GetPriceForRegisteringEnsName(params.FromChain.ChainID)
    if err != nil {
        return []byte{}, statusErrors.CreateErrorResponseFromError(err)
    }

    registrarABI, err := abi.JSON(strings.NewReader(registrar.UsernameRegistrarABI))
    if err != nil {
        return []byte{}, statusErrors.CreateErrorResponseFromError(err)
    }

    x, y := ens.ExtractCoordinates(params.PublicKey)
    extraData, err := registrarABI.Pack("register", ens.UsernameToLabel(params.Username), params.FromAddr, x, y)
    if err != nil {
        return []byte{}, statusErrors.CreateErrorResponseFromError(err)
    }

    sntABI, err := abi.JSON(strings.NewReader(snt.SNTABI))
    if err != nil {
        return []byte{}, statusErrors.CreateErrorResponseFromError(err)
    }

    registryAddr, err := s.ensService.API().GetRegistrarAddress(context.Background(), params.FromChain.ChainID)
    if err != nil {
        return []byte{}, statusErrors.CreateErrorResponseFromError(err)
    }

    return sntABI.Pack("approveAndCall", registryAddr, price, extraData)
}

func (s *ENSRegisterProcessor) EstimateGas(params ProcessorInputParams) (uint64, error) {
    if params.TestsMode {
        if params.TestEstimationMap != nil {
            if val, ok := params.TestEstimationMap[s.Name()]; ok {
                return val, nil
            }
        }
        return 0, ErrNoEstimationFound
    }

    contractAddress, err := s.GetContractAddress(params)
    if err != nil {
        return 0, statusErrors.CreateErrorResponseFromError(err)
    }

    input, err := s.PackTxInputData(params)
    if err != nil {
        return 0, statusErrors.CreateErrorResponseFromError(err)
    }

    ethClient, err := s.contractMaker.RPCClient.EthClient(params.FromChain.ChainID)
    if err != nil {
        return 0, statusErrors.CreateErrorResponseFromError(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, statusErrors.CreateErrorResponseFromError(err)
    }

    increasedEstimation := float64(estimation) * IncreaseEstimatedGasFactor

    return uint64(increasedEstimation), nil
}

func (s *ENSRegisterProcessor) BuildTx(params ProcessorInputParams) (*ethTypes.Transaction, error) {
    toAddr := types.Address(params.ToAddr)
    inputData, err := s.PackTxInputData(params)
    if err != nil {
        return nil, statusErrors.CreateErrorResponseFromError(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 *ENSRegisterProcessor) Send(sendArgs *MultipathProcessorTxArgs, verifiedAccount *account.SelectedExtKey) (hash types.Hash, err error) {
    return s.transactor.SendTransactionWithChainID(sendArgs.ChainID, *sendArgs.TransferTx, verifiedAccount)
}

func (s *ENSRegisterProcessor) BuildTransaction(sendArgs *MultipathProcessorTxArgs) (*ethTypes.Transaction, error) {
    return s.transactor.ValidateAndBuildTransaction(sendArgs.ChainID, *sendArgs.TransferTx)
}

func (s *ENSRegisterProcessor) CalculateAmountOut(params ProcessorInputParams) (*big.Int, error) {
    return params.AmountIn, nil
}

func (s *ENSRegisterProcessor) GetContractAddress(params ProcessorInputParams) (common.Address, error) {
    return snt.ContractAddress(params.FromChain.ChainID)
}