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-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-08-07 12:38:04 +00:00
|
|
|
"github.com/status-im/status-go/contracts/erc721"
|
2023-08-24 08:45:14 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2024-08-18 19:16:09 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2023-08-24 08:45:14 +00:00
|
|
|
"github.com/status-im/status-go/rpc"
|
2024-09-23 06:35:34 +00:00
|
|
|
walletCommon "github.com/status-im/status-go/services/wallet/common"
|
2024-08-07 12:38:04 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/token"
|
2023-08-24 08:45:14 +00:00
|
|
|
"github.com/status-im/status-go/transactions"
|
|
|
|
)
|
|
|
|
|
2024-08-07 12:38:04 +00:00
|
|
|
const (
|
|
|
|
functionNameSafeTransferFrom = "safeTransferFrom"
|
|
|
|
functionNameTransferFrom = "transferFrom"
|
|
|
|
)
|
|
|
|
|
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-07-18 12:20:54 +00:00
|
|
|
func createERC721ErrorResponse(err error) error {
|
|
|
|
return createErrorResponse(ProcessorERC721Name, err)
|
|
|
|
}
|
|
|
|
|
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) {
|
2024-09-23 06:35:34 +00:00
|
|
|
return walletCommon.ZeroBigIntValue(), walletCommon.ZeroBigIntValue(), nil
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
|
|
|
|
2024-08-07 12:38:04 +00:00
|
|
|
func (s *ERC721Processor) packTxInputDataInternally(params ProcessorInputParams, functionName string) ([]byte, error) {
|
|
|
|
abi, err := abi.JSON(strings.NewReader(erc721.Erc721MetaData.ABI))
|
2023-11-02 11:35:28 +00:00
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return []byte{}, createERC721ErrorResponse(err)
|
2023-11-02 11:35:28 +00:00
|
|
|
}
|
2024-05-21 14:33:36 +00:00
|
|
|
|
2024-09-23 07:06:55 +00:00
|
|
|
id, err := walletCommon.GetTokenIdFromSymbol(params.FromToken.Symbol)
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, createERC721ErrorResponse(err)
|
2023-11-14 12:33:44 +00:00
|
|
|
}
|
2024-05-21 14:33:36 +00:00
|
|
|
|
2024-08-07 12:38:04 +00:00
|
|
|
return abi.Pack(functionName,
|
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-08-07 12:38:04 +00:00
|
|
|
func (s *ERC721Processor) checkIfFunctionExists(params ProcessorInputParams, functionName string) error {
|
|
|
|
data, err := s.packTxInputDataInternally(params, functionName)
|
|
|
|
if err != nil {
|
|
|
|
return createERC721ErrorResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ethClient, err := s.rpcClient.EthClient(params.FromChain.ChainID)
|
|
|
|
if err != nil {
|
|
|
|
return createERC721ErrorResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
value := new(big.Int)
|
|
|
|
msg := ethereum.CallMsg{
|
|
|
|
From: params.FromAddr,
|
|
|
|
To: ¶ms.FromToken.Address,
|
|
|
|
Value: value,
|
|
|
|
Data: data,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = ethClient.CallContract(context.Background(), msg, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ERC721Processor) PackTxInputData(params ProcessorInputParams) ([]byte, error) {
|
|
|
|
err := s.checkIfFunctionExists(params, functionNameSafeTransferFrom)
|
|
|
|
if err == nil {
|
|
|
|
return s.packTxInputDataInternally(params, functionNameSafeTransferFrom)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.packTxInputDataInternally(params, functionNameTransferFrom)
|
|
|
|
}
|
|
|
|
|
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 {
|
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 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-07-18 12:20:54 +00:00
|
|
|
return 0, createERC721ErrorResponse(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-07-18 12:20:54 +00:00
|
|
|
return 0, createERC721ErrorResponse(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-07-18 12:20:54 +00:00
|
|
|
return 0, createERC721ErrorResponse(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-09-23 07:16:03 +00:00
|
|
|
// TODO: remove this struct once mobile switches to the new approach
|
2024-08-12 12:07:32 +00:00
|
|
|
func (s *ERC721Processor) sendOrBuild(sendArgs *MultipathProcessorTxArgs, signerFn bind.SignerFn, lastUsedNonce int64) (tx *ethTypes.Transaction, err error) {
|
2024-08-07 12:38:04 +00:00
|
|
|
from := common.Address(sendArgs.ERC721TransferTx.From)
|
|
|
|
|
|
|
|
useSafeTransferFrom := true
|
|
|
|
inputParams := ProcessorInputParams{
|
2024-08-18 19:16:09 +00:00
|
|
|
FromChain: ¶ms.Network{
|
|
|
|
ChainID: sendArgs.ChainID,
|
|
|
|
},
|
2024-08-07 12:38:04 +00:00
|
|
|
FromAddr: from,
|
|
|
|
ToAddr: sendArgs.ERC721TransferTx.Recipient,
|
|
|
|
FromToken: &token.Token{
|
2024-09-23 07:16:03 +00:00
|
|
|
Symbol: sendArgs.ERC721TransferTx.TokenID.String(),
|
|
|
|
Address: common.Address(*sendArgs.ERC721TransferTx.To),
|
2024-08-07 12:38:04 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
err = s.checkIfFunctionExists(inputParams, functionNameSafeTransferFrom)
|
|
|
|
if err != nil {
|
|
|
|
useSafeTransferFrom = false
|
|
|
|
}
|
|
|
|
|
2023-08-24 08:45:14 +00:00
|
|
|
ethClient, err := s.rpcClient.EthClient(sendArgs.ChainID)
|
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return tx, createERC721ErrorResponse(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-07-18 12:20:54 +00:00
|
|
|
return tx, createERC721ErrorResponse(err)
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
2023-09-29 17:56:27 +00:00
|
|
|
|
2024-08-12 12:07:32 +00:00
|
|
|
var nonce uint64
|
|
|
|
if lastUsedNonce < 0 {
|
|
|
|
nonce, err = s.transactor.NextNonce(s.rpcClient, sendArgs.ChainID, sendArgs.ERC721TransferTx.From)
|
|
|
|
if err != nil {
|
|
|
|
return tx, createERC721ErrorResponse(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nonce = uint64(lastUsedNonce) + 1
|
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-08-07 12:38:04 +00:00
|
|
|
if useSafeTransferFrom {
|
|
|
|
tx, err = contract.SafeTransferFrom(txOpts, from,
|
|
|
|
sendArgs.ERC721TransferTx.Recipient,
|
|
|
|
sendArgs.ERC721TransferTx.TokenID.ToInt())
|
|
|
|
} else {
|
|
|
|
tx, err = contract.TransferFrom(txOpts, from,
|
|
|
|
sendArgs.ERC721TransferTx.Recipient,
|
|
|
|
sendArgs.ERC721TransferTx.TokenID.ToInt())
|
|
|
|
}
|
2024-06-18 10:31:23 +00:00
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return tx, createERC721ErrorResponse(err)
|
2024-06-18 10:31:23 +00:00
|
|
|
}
|
2024-07-08 09:02:06 +00:00
|
|
|
err = s.transactor.StoreAndTrackPendingTx(from, sendArgs.ERC721TransferTx.Symbol, sendArgs.ChainID, sendArgs.ERC721TransferTx.MultiTransactionID, tx)
|
|
|
|
if err != nil {
|
2024-07-18 12:20:54 +00:00
|
|
|
return tx, createERC721ErrorResponse(err)
|
2024-07-08 09:02:06 +00:00
|
|
|
}
|
2024-06-18 10:31:23 +00:00
|
|
|
return tx, nil
|
2023-09-29 17:56:27 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 07:16:03 +00:00
|
|
|
func (s *ERC721Processor) sendOrBuildV2(sendArgs *transactions.SendTxArgs, signerFn bind.SignerFn, lastUsedNonce int64) (tx *ethTypes.Transaction, err error) {
|
|
|
|
from := common.Address(sendArgs.From)
|
|
|
|
to := common.Address(*sendArgs.To)
|
|
|
|
|
|
|
|
useSafeTransferFrom := true
|
|
|
|
inputParams := ProcessorInputParams{
|
|
|
|
FromChain: ¶ms.Network{
|
|
|
|
ChainID: sendArgs.FromChainID,
|
|
|
|
},
|
|
|
|
FromAddr: from,
|
|
|
|
ToAddr: to,
|
|
|
|
FromToken: &token.Token{
|
|
|
|
Symbol: sendArgs.FromTokenID,
|
|
|
|
Address: common.Address(sendArgs.ToContractAddress),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = s.checkIfFunctionExists(inputParams, functionNameSafeTransferFrom)
|
|
|
|
if err != nil {
|
|
|
|
useSafeTransferFrom = false
|
|
|
|
}
|
|
|
|
|
|
|
|
ethClient, err := s.rpcClient.EthClient(sendArgs.FromChainID)
|
|
|
|
if err != nil {
|
|
|
|
return tx, createERC721ErrorResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
contract, err := collectibles.NewCollectibles(common.Address(sendArgs.ToContractAddress), ethClient)
|
|
|
|
if err != nil {
|
|
|
|
return tx, createERC721ErrorResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := walletCommon.GetTokenIdFromSymbol(sendArgs.FromTokenID)
|
|
|
|
if err != nil {
|
|
|
|
return tx, createERC1155ErrorResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var nonce uint64
|
|
|
|
if lastUsedNonce < 0 {
|
|
|
|
nonce, err = s.transactor.NextNonce(s.rpcClient, sendArgs.FromChainID, sendArgs.From)
|
|
|
|
if err != nil {
|
|
|
|
return tx, createERC721ErrorResponse(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nonce = uint64(lastUsedNonce) + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
argNonce := hexutil.Uint64(nonce)
|
|
|
|
sendArgs.Nonce = &argNonce
|
|
|
|
txOpts := sendArgs.ToTransactOpts(signerFn)
|
|
|
|
if useSafeTransferFrom {
|
|
|
|
tx, err = contract.SafeTransferFrom(txOpts,
|
|
|
|
from,
|
|
|
|
to,
|
|
|
|
id)
|
|
|
|
} else {
|
|
|
|
tx, err = contract.TransferFrom(txOpts,
|
|
|
|
from,
|
|
|
|
to,
|
|
|
|
id)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return tx, createERC721ErrorResponse(err)
|
|
|
|
}
|
|
|
|
err = s.transactor.StoreAndTrackPendingTx(from, sendArgs.FromTokenID, sendArgs.FromChainID, sendArgs.MultiTransactionID, tx)
|
|
|
|
if err != nil {
|
|
|
|
return tx, createERC721ErrorResponse(err)
|
|
|
|
}
|
|
|
|
return tx, nil
|
|
|
|
}
|
|
|
|
|
2024-08-12 12:07:32 +00:00
|
|
|
func (s *ERC721Processor) Send(sendArgs *MultipathProcessorTxArgs, lastUsedNonce int64, verifiedAccount *account.SelectedExtKey) (hash types.Hash, usedNonce uint64, err error) {
|
|
|
|
tx, err := s.sendOrBuild(sendArgs, getSigner(sendArgs.ChainID, sendArgs.ERC721TransferTx.From, verifiedAccount), lastUsedNonce)
|
2023-08-24 08:45:14 +00:00
|
|
|
if err != nil {
|
2024-08-12 12:07:32 +00:00
|
|
|
return hash, 0, createERC721ErrorResponse(err)
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
2024-08-12 12:07:32 +00:00
|
|
|
return types.Hash(tx.Hash()), tx.Nonce(), nil
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 12:07:32 +00:00
|
|
|
func (s *ERC721Processor) BuildTransaction(sendArgs *MultipathProcessorTxArgs, lastUsedNonce int64) (*ethTypes.Transaction, uint64, error) {
|
|
|
|
tx, err := s.sendOrBuild(sendArgs, nil, lastUsedNonce)
|
|
|
|
return tx, tx.Nonce(), err
|
2023-09-29 17:56:27 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 07:16:03 +00:00
|
|
|
func (s *ERC721Processor) BuildTransactionV2(sendArgs *transactions.SendTxArgs, lastUsedNonce int64) (*ethTypes.Transaction, uint64, error) {
|
|
|
|
tx, err := s.sendOrBuildV2(sendArgs, nil, lastUsedNonce)
|
|
|
|
return tx, tx.Nonce(), err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|