status-go/services/wallet/router/pathprocessor/processor_ens_release.go

136 lines
4.0 KiB
Go
Raw Normal View History

package pathprocessor
2024-06-05 12:46:10 +00:00
import (
"context"
"errors"
2024-06-05 12:46:10 +00:00
"math/big"
"strings"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"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/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 ENSReleaseProcessor struct {
2024-06-05 12:46:10 +00:00
contractMaker *contracts.ContractMaker
transactor transactions.TransactorIface
ensService *ens.Service
}
func NewENSReleaseProcessor(rpcClient *rpc.Client, transactor transactions.TransactorIface, ensService *ens.Service) *ENSReleaseProcessor {
return &ENSReleaseProcessor{
2024-06-05 12:46:10 +00:00
contractMaker: &contracts.ContractMaker{
RPCClient: rpcClient,
},
transactor: transactor,
ensService: ensService,
}
}
func (s *ENSReleaseProcessor) Name() string {
return ProcessorENSReleaseName
2024-06-05 12:46:10 +00:00
}
func (s *ENSReleaseProcessor) AvailableFor(params ProcessorInputParams) (bool, error) {
2024-06-05 12:46:10 +00:00
return params.FromChain.ChainID == walletCommon.EthereumMainnet || params.FromChain.ChainID == walletCommon.EthereumSepolia, nil
}
func (s *ENSReleaseProcessor) CalculateFees(params ProcessorInputParams) (*big.Int, *big.Int, error) {
2024-06-05 12:46:10 +00:00
return ZeroBigIntValue, ZeroBigIntValue, nil
}
func (s *ENSReleaseProcessor) PackTxInputData(params ProcessorInputParams) ([]byte, error) {
2024-06-05 12:46:10 +00:00
registrarABI, err := abi.JSON(strings.NewReader(registrar.UsernameRegistrarABI))
if err != nil {
return []byte{}, err
}
return registrarABI.Pack("release", ens.UsernameToLabel(params.Username))
2024-06-05 12:46:10 +00:00
}
func (s *ENSReleaseProcessor) EstimateGas(params ProcessorInputParams) (uint64, error) {
2024-06-05 12:46:10 +00:00
contractAddress, err := s.GetContractAddress(params)
if err != nil {
return 0, err
}
input, err := s.PackTxInputData(params)
2024-06-05 12:46:10 +00:00
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 *ENSReleaseProcessor) BuildTx(params ProcessorInputParams) (*ethTypes.Transaction, error) {
2024-06-05 12:46:10 +00:00
toAddr := types.Address(params.ToAddr)
inputData, err := s.PackTxInputData(params)
2024-06-05 12:46:10 +00:00
if err != nil {
return nil, err
}
sendArgs := &MultipathProcessorTxArgs{
2024-06-05 12:46:10 +00:00
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 *ENSReleaseProcessor) Send(sendArgs *MultipathProcessorTxArgs, verifiedAccount *account.SelectedExtKey) (hash types.Hash, err error) {
2024-06-05 12:46:10 +00:00
return s.transactor.SendTransactionWithChainID(sendArgs.ChainID, *sendArgs.TransferTx, verifiedAccount)
}
func (s *ENSReleaseProcessor) BuildTransaction(sendArgs *MultipathProcessorTxArgs) (*ethTypes.Transaction, error) {
2024-06-05 12:46:10 +00:00
return s.transactor.ValidateAndBuildTransaction(sendArgs.ChainID, *sendArgs.TransferTx)
}
func (s *ENSReleaseProcessor) CalculateAmountOut(params ProcessorInputParams) (*big.Int, error) {
2024-06-05 12:46:10 +00:00
return params.AmountIn, nil
}
func (s *ENSReleaseProcessor) GetContractAddress(params ProcessorInputParams) (common.Address, error) {
addr, err := s.ensService.API().GetRegistrarAddress(context.Background(), params.FromChain.ChainID)
if err != nil {
return common.Address{}, err
}
if addr == ZeroAddress {
return common.Address{}, errors.New("ENS registar not found")
}
return addr, nil
2024-06-05 12:46:10 +00:00
}