2024-06-06 20:08:25 +00:00
|
|
|
package pathprocessor
|
2024-06-05 12:46:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/big"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum"
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
|
|
|
"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/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"
|
|
|
|
)
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
type ENSReleaseProcessor struct {
|
2024-06-05 12:46:10 +00:00
|
|
|
contractMaker *contracts.ContractMaker
|
|
|
|
transactor transactions.TransactorIface
|
|
|
|
ensService *ens.Service
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-18 12:20:54 +00:00
|
|
|
func createENSReleaseErrorResponse(err error) error {
|
|
|
|
return createErrorResponse(ProcessorENSReleaseName, err)
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *ENSReleaseProcessor) Name() string {
|
|
|
|
return ProcessorENSReleaseName
|
2024-06-05 12:46:10 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +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
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *ENSReleaseProcessor) CalculateFees(params ProcessorInputParams) (*big.Int, *big.Int, error) {
|
2024-09-23 06:35:34 +00:00
|
|
|
return walletCommon.ZeroBigIntValue(), walletCommon.ZeroBigIntValue(), nil
|
2024-06-05 12:46:10 +00:00
|
|
|
}
|
|
|
|
|
2024-06-10 12:52:47 +00:00
|
|
|
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 {
|
2024-07-18 12:20:54 +00:00
|
|
|
return []byte{}, createENSReleaseErrorResponse(err)
|
2024-06-05 12:46:10 +00:00
|
|
|
}
|
|
|
|
|
2024-10-03 19:26:29 +00:00
|
|
|
name := getNameFromEnsUsername(params.Username)
|
|
|
|
return registrarABI.Pack("release", ens.UsernameToLabel(name))
|
2024-06-05 12:46:10 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *ENSReleaseProcessor) 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-05 12:46:10 +00:00
|
|
|
contractAddress, err := s.GetContractAddress(params)
|
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return 0, createENSReleaseErrorResponse(err)
|
2024-06-05 12:46:10 +00:00
|
|
|
}
|
|
|
|
|
2024-06-10 12:52:47 +00:00
|
|
|
input, err := s.PackTxInputData(params)
|
2024-06-05 12:46:10 +00:00
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return 0, createENSReleaseErrorResponse(err)
|
2024-06-05 12:46:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ethClient, err := s.contractMaker.RPCClient.EthClient(params.FromChain.ChainID)
|
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return 0, createENSReleaseErrorResponse(err)
|
2024-06-05 12:46:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := ethereum.CallMsg{
|
|
|
|
From: params.FromAddr,
|
|
|
|
To: &contractAddress,
|
2024-09-23 06:35:34 +00:00
|
|
|
Value: walletCommon.ZeroBigIntValue(),
|
2024-06-05 12:46:10 +00:00
|
|
|
Data: input,
|
|
|
|
}
|
|
|
|
|
|
|
|
estimation, err := ethClient.EstimateGas(context.Background(), msg)
|
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return 0, createENSReleaseErrorResponse(err)
|
2024-06-05 12:46:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
increasedEstimation := float64(estimation) * IncreaseEstimatedGasFactor
|
|
|
|
|
|
|
|
return uint64(increasedEstimation), nil
|
|
|
|
}
|
|
|
|
|
2024-08-12 12:07:32 +00:00
|
|
|
func (s *ENSReleaseProcessor) 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-05 12:46:10 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 12:07:32 +00:00
|
|
|
func (s *ENSReleaseProcessor) BuildTransaction(sendArgs *MultipathProcessorTxArgs, lastUsedNonce int64) (*ethTypes.Transaction, uint64, error) {
|
|
|
|
return s.transactor.ValidateAndBuildTransaction(sendArgs.ChainID, *sendArgs.TransferTx, lastUsedNonce)
|
2024-06-05 12:46:10 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 07:16:03 +00:00
|
|
|
func (s *ENSReleaseProcessor) BuildTransactionV2(sendArgs *transactions.SendTxArgs, lastUsedNonce int64) (*ethTypes.Transaction, uint64, error) {
|
|
|
|
return s.transactor.ValidateAndBuildTransaction(sendArgs.FromChainID, *sendArgs, lastUsedNonce)
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *ENSReleaseProcessor) CalculateAmountOut(params ProcessorInputParams) (*big.Int, error) {
|
2024-06-05 12:46:10 +00:00
|
|
|
return params.AmountIn, nil
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
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
|
|
|
|
}
|
2024-09-23 06:35:34 +00:00
|
|
|
if addr == walletCommon.ZeroAddress() {
|
2024-06-18 10:31:23 +00:00
|
|
|
return common.Address{}, ErrENSRegistrarNotFound
|
2024-06-06 20:08:25 +00:00
|
|
|
}
|
|
|
|
return addr, nil
|
2024-06-05 12:46:10 +00:00
|
|
|
}
|