2024-06-06 20:08:25 +00:00
|
|
|
package pathprocessor
|
2023-08-24 08:45:14 +00:00
|
|
|
|
|
|
|
import (
|
2023-11-02 11:35:28 +00:00
|
|
|
"context"
|
2023-11-14 12:33:44 +00:00
|
|
|
"fmt"
|
2023-08-24 08:45:14 +00:00
|
|
|
"math/big"
|
2023-11-02 11:35:28 +00:00
|
|
|
"strings"
|
2023-08-24 08:45:14 +00:00
|
|
|
|
2023-11-02 11:35:28 +00:00
|
|
|
"github.com/ethereum/go-ethereum"
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
2023-09-29 17:56:27 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
2023-08-24 08:45:14 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2023-09-29 17:56:27 +00:00
|
|
|
ethTypes "github.com/ethereum/go-ethereum/core/types"
|
2023-08-24 08:45:14 +00:00
|
|
|
"github.com/status-im/status-go/account"
|
|
|
|
"github.com/status-im/status-go/contracts/community-tokens/collectibles"
|
2024-06-18 10:31:23 +00:00
|
|
|
statusErrors "github.com/status-im/status-go/errors"
|
2023-08-24 08:45:14 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
"github.com/status-im/status-go/rpc"
|
|
|
|
"github.com/status-im/status-go/transactions"
|
|
|
|
)
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
type ERC721TxArgs struct {
|
2023-08-24 08:45:14 +00:00
|
|
|
transactions.SendTxArgs
|
|
|
|
TokenID *hexutil.Big `json:"tokenId"`
|
|
|
|
Recipient common.Address `json:"recipient"`
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
type ERC721Processor struct {
|
2023-08-24 08:45:14 +00:00
|
|
|
rpcClient *rpc.Client
|
2024-05-26 08:31:13 +00:00
|
|
|
transactor transactions.TransactorIface
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func NewERC721Processor(rpcClient *rpc.Client, transactor transactions.TransactorIface) *ERC721Processor {
|
|
|
|
return &ERC721Processor{rpcClient: rpcClient, transactor: transactor}
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *ERC721Processor) Name() string {
|
|
|
|
return ProcessorERC721Name
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *ERC721Processor) AvailableFor(params ProcessorInputParams) (bool, error) {
|
2024-06-05 07:56:02 +00:00
|
|
|
return params.FromChain.ChainID == params.ToChain.ChainID && params.ToToken == nil, nil
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *ERC721Processor) CalculateFees(params ProcessorInputParams) (*big.Int, *big.Int, error) {
|
|
|
|
return ZeroBigIntValue, ZeroBigIntValue, nil
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
|
|
|
|
2024-06-10 12:52:47 +00:00
|
|
|
func (s *ERC721Processor) PackTxInputData(params ProcessorInputParams) ([]byte, error) {
|
2023-11-02 11:35:28 +00:00
|
|
|
abi, err := abi.JSON(strings.NewReader(collectibles.CollectiblesMetaData.ABI))
|
|
|
|
if err != nil {
|
2024-06-18 10:31:23 +00:00
|
|
|
return []byte{}, statusErrors.CreateErrorResponseFromError(err)
|
2023-11-02 11:35:28 +00:00
|
|
|
}
|
2024-05-21 14:33:36 +00:00
|
|
|
|
2024-06-05 07:56:02 +00:00
|
|
|
id, success := big.NewInt(0).SetString(params.FromToken.Symbol, 0)
|
2023-11-14 12:33:44 +00:00
|
|
|
if !success {
|
2024-06-18 10:31:23 +00:00
|
|
|
return []byte{}, statusErrors.CreateErrorResponseFromError(fmt.Errorf("failed to convert %s to big.Int", params.FromToken.Symbol))
|
2023-11-14 12:33:44 +00:00
|
|
|
}
|
2024-05-21 14:33:36 +00:00
|
|
|
|
|
|
|
return abi.Pack("safeTransferFrom",
|
2024-06-05 07:56:02 +00:00
|
|
|
params.FromAddr,
|
|
|
|
params.ToAddr,
|
2023-11-14 12:33:44 +00:00
|
|
|
id,
|
|
|
|
)
|
2024-05-21 14:33:36 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *ERC721Processor) 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 {
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, ErrNoEstimationFound
|
|
|
|
}
|
|
|
|
|
2024-06-05 07:56:02 +00:00
|
|
|
ethClient, err := s.rpcClient.EthClient(params.FromChain.ChainID)
|
2024-05-21 14:33:36 +00:00
|
|
|
if err != nil {
|
2024-06-18 10:31:23 +00:00
|
|
|
return 0, statusErrors.CreateErrorResponseFromError(err)
|
2024-05-21 14:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
value := new(big.Int)
|
2023-11-02 11:35:28 +00:00
|
|
|
|
2024-06-10 12:52:47 +00:00
|
|
|
input, err := s.PackTxInputData(params)
|
2023-11-02 11:35:28 +00:00
|
|
|
if err != nil {
|
2024-06-18 10:31:23 +00:00
|
|
|
return 0, statusErrors.CreateErrorResponseFromError(err)
|
2023-11-02 11:35:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := ethereum.CallMsg{
|
2024-06-05 07:56:02 +00:00
|
|
|
From: params.FromAddr,
|
|
|
|
To: ¶ms.FromToken.Address,
|
2023-11-02 11:35:28 +00:00
|
|
|
Value: value,
|
|
|
|
Data: input,
|
|
|
|
}
|
|
|
|
|
2024-05-30 13:03:28 +00:00
|
|
|
estimation, err := ethClient.EstimateGas(context.Background(), msg)
|
2023-11-02 11:35:28 +00:00
|
|
|
if err != nil {
|
2024-06-18 10:31:23 +00:00
|
|
|
return 0, statusErrors.CreateErrorResponseFromError(err)
|
2023-11-02 11:35:28 +00:00
|
|
|
}
|
2024-06-05 07:56:02 +00:00
|
|
|
|
2023-11-02 11:35:28 +00:00
|
|
|
increasedEstimation := float64(estimation) * IncreaseEstimatedGasFactor
|
|
|
|
return uint64(increasedEstimation), nil
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *ERC721Processor) BuildTx(params ProcessorInputParams) (*ethTypes.Transaction, error) {
|
2024-06-05 07:56:02 +00:00
|
|
|
contractAddress := types.Address(params.FromToken.Address)
|
2024-03-30 11:41:02 +00:00
|
|
|
|
|
|
|
// We store ERC721 Token ID using big.Int.String() in token.Symbol
|
2024-06-05 07:56:02 +00:00
|
|
|
tokenID, success := new(big.Int).SetString(params.FromToken.Symbol, 10)
|
2024-03-30 11:41:02 +00:00
|
|
|
if !success {
|
2024-06-18 10:31:23 +00:00
|
|
|
return nil, statusErrors.CreateErrorResponseFromError(fmt.Errorf("failed to convert ERC721's Symbol %s to big.Int", params.FromToken.Symbol))
|
2024-03-30 11:41:02 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
sendArgs := &MultipathProcessorTxArgs{
|
|
|
|
ERC721TransferTx: &ERC721TxArgs{
|
2024-03-25 12:40:00 +00:00
|
|
|
SendTxArgs: transactions.SendTxArgs{
|
2024-06-05 07:56:02 +00:00
|
|
|
From: types.Address(params.FromAddr),
|
2024-03-30 11:41:02 +00:00
|
|
|
To: &contractAddress,
|
2024-06-05 07:56:02 +00:00
|
|
|
Value: (*hexutil.Big)(params.AmountIn),
|
2024-03-25 12:40:00 +00:00
|
|
|
Data: types.HexBytes("0x0"),
|
|
|
|
},
|
2024-03-30 11:41:02 +00:00
|
|
|
TokenID: (*hexutil.Big)(tokenID),
|
2024-06-05 07:56:02 +00:00
|
|
|
Recipient: params.ToAddr,
|
2024-03-25 12:40:00 +00:00
|
|
|
},
|
2024-06-05 07:56:02 +00:00
|
|
|
ChainID: params.FromChain.ChainID,
|
2024-03-25 12:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.BuildTransaction(sendArgs)
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *ERC721Processor) sendOrBuild(sendArgs *MultipathProcessorTxArgs, signerFn bind.SignerFn) (tx *ethTypes.Transaction, err error) {
|
2023-08-24 08:45:14 +00:00
|
|
|
ethClient, err := s.rpcClient.EthClient(sendArgs.ChainID)
|
|
|
|
if err != nil {
|
2024-06-18 10:31:23 +00:00
|
|
|
return tx, statusErrors.CreateErrorResponseFromError(err)
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
2023-09-29 17:56:27 +00:00
|
|
|
|
2023-08-24 08:45:14 +00:00
|
|
|
contract, err := collectibles.NewCollectibles(common.Address(*sendArgs.ERC721TransferTx.To), ethClient)
|
|
|
|
if err != nil {
|
2024-06-18 10:31:23 +00:00
|
|
|
return tx, statusErrors.CreateErrorResponseFromError(err)
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
2023-09-29 17:56:27 +00:00
|
|
|
|
2023-12-19 13:38:01 +00:00
|
|
|
nonce, err := s.transactor.NextNonce(s.rpcClient, sendArgs.ChainID, sendArgs.ERC721TransferTx.From)
|
2023-08-24 08:45:14 +00:00
|
|
|
if err != nil {
|
2024-06-18 10:31:23 +00:00
|
|
|
return tx, statusErrors.CreateErrorResponseFromError(err)
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
2023-11-06 09:26:02 +00:00
|
|
|
|
2023-08-24 08:45:14 +00:00
|
|
|
argNonce := hexutil.Uint64(nonce)
|
|
|
|
sendArgs.ERC721TransferTx.Nonce = &argNonce
|
2023-09-29 17:56:27 +00:00
|
|
|
txOpts := sendArgs.ERC721TransferTx.ToTransactOpts(signerFn)
|
2024-06-05 07:56:02 +00:00
|
|
|
|
2023-11-06 09:26:02 +00:00
|
|
|
tx, err = contract.SafeTransferFrom(txOpts, common.Address(sendArgs.ERC721TransferTx.From),
|
|
|
|
sendArgs.ERC721TransferTx.Recipient,
|
2023-09-29 17:56:27 +00:00
|
|
|
sendArgs.ERC721TransferTx.TokenID.ToInt())
|
2024-06-18 10:31:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return tx, statusErrors.CreateErrorResponseFromError(err)
|
|
|
|
}
|
|
|
|
return tx, nil
|
2023-09-29 17:56:27 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *ERC721Processor) Send(sendArgs *MultipathProcessorTxArgs, verifiedAccount *account.SelectedExtKey) (hash types.Hash, err error) {
|
2023-12-19 13:38:01 +00:00
|
|
|
tx, err := s.sendOrBuild(sendArgs, getSigner(sendArgs.ChainID, sendArgs.ERC721TransferTx.From, verifiedAccount))
|
2023-08-24 08:45:14 +00:00
|
|
|
if err != nil {
|
2024-06-18 10:31:23 +00:00
|
|
|
return hash, statusErrors.CreateErrorResponseFromError(err)
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
|
|
|
return types.Hash(tx.Hash()), nil
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *ERC721Processor) BuildTransaction(sendArgs *MultipathProcessorTxArgs) (*ethTypes.Transaction, error) {
|
2023-09-29 17:56:27 +00:00
|
|
|
return s.sendOrBuild(sendArgs, nil)
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *ERC721Processor) CalculateAmountOut(params ProcessorInputParams) (*big.Int, error) {
|
2024-06-05 07:56:02 +00:00
|
|
|
return params.AmountIn, nil
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (s *ERC721Processor) GetContractAddress(params ProcessorInputParams) (common.Address, error) {
|
2024-06-05 07:56:02 +00:00
|
|
|
return params.FromToken.Address, nil
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|