2024-06-06 22:08:25 +02:00
|
|
|
package pathprocessor
|
2023-08-24 10:45:14 +02:00
|
|
|
|
|
|
|
import (
|
2023-11-02 12:35:28 +01:00
|
|
|
"context"
|
2023-08-24 10:45:14 +02:00
|
|
|
"math/big"
|
2023-11-02 12:35:28 +01:00
|
|
|
"strings"
|
2023-08-24 10:45:14 +02:00
|
|
|
|
2023-11-02 12:35:28 +01:00
|
|
|
"github.com/ethereum/go-ethereum"
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
2023-09-29 19:56:27 +02:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
2023-08-24 10:45:14 +02:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2023-09-29 19:56:27 +02:00
|
|
|
ethTypes "github.com/ethereum/go-ethereum/core/types"
|
2023-08-24 10:45:14 +02:00
|
|
|
"github.com/status-im/status-go/account"
|
|
|
|
"github.com/status-im/status-go/contracts/community-tokens/collectibles"
|
2024-08-07 14:38:04 +02:00
|
|
|
"github.com/status-im/status-go/contracts/erc721"
|
2023-08-24 10:45:14 +02:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2024-08-18 21:16:09 +02:00
|
|
|
"github.com/status-im/status-go/params"
|
2023-08-24 10:45:14 +02:00
|
|
|
"github.com/status-im/status-go/rpc"
|
2024-11-20 08:28:18 +01:00
|
|
|
"github.com/status-im/status-go/services/utils"
|
2024-09-23 08:35:34 +02:00
|
|
|
walletCommon "github.com/status-im/status-go/services/wallet/common"
|
2024-11-20 08:28:18 +01:00
|
|
|
pathProcessorCommon "github.com/status-im/status-go/services/wallet/router/pathprocessor/common"
|
2024-08-07 14:38:04 +02:00
|
|
|
"github.com/status-im/status-go/services/wallet/token"
|
2024-11-05 22:28:24 +01:00
|
|
|
"github.com/status-im/status-go/services/wallet/wallettypes"
|
2023-08-24 10:45:14 +02:00
|
|
|
"github.com/status-im/status-go/transactions"
|
|
|
|
)
|
|
|
|
|
2024-08-07 14:38:04 +02:00
|
|
|
const (
|
|
|
|
functionNameSafeTransferFrom = "safeTransferFrom"
|
|
|
|
functionNameTransferFrom = "transferFrom"
|
|
|
|
)
|
|
|
|
|
2024-06-06 22:08:25 +02:00
|
|
|
type ERC721TxArgs struct {
|
2024-11-05 22:28:24 +01:00
|
|
|
wallettypes.SendTxArgs
|
2023-08-24 10:45:14 +02:00
|
|
|
TokenID *hexutil.Big `json:"tokenId"`
|
|
|
|
Recipient common.Address `json:"recipient"`
|
|
|
|
}
|
|
|
|
|
2024-06-06 22:08:25 +02:00
|
|
|
type ERC721Processor struct {
|
2023-08-24 10:45:14 +02:00
|
|
|
rpcClient *rpc.Client
|
2024-05-26 10:31:13 +02:00
|
|
|
transactor transactions.TransactorIface
|
2023-08-24 10:45:14 +02:00
|
|
|
}
|
|
|
|
|
2024-06-06 22:08:25 +02:00
|
|
|
func NewERC721Processor(rpcClient *rpc.Client, transactor transactions.TransactorIface) *ERC721Processor {
|
|
|
|
return &ERC721Processor{rpcClient: rpcClient, transactor: transactor}
|
2023-08-24 10:45:14 +02:00
|
|
|
}
|
|
|
|
|
2024-07-18 09:20:54 -03:00
|
|
|
func createERC721ErrorResponse(err error) error {
|
2024-11-20 08:28:18 +01:00
|
|
|
return createErrorResponse(pathProcessorCommon.ProcessorERC721Name, err)
|
2024-07-18 09:20:54 -03:00
|
|
|
}
|
|
|
|
|
2024-06-06 22:08:25 +02:00
|
|
|
func (s *ERC721Processor) Name() string {
|
2024-11-20 08:28:18 +01:00
|
|
|
return pathProcessorCommon.ProcessorERC721Name
|
2023-08-24 10:45:14 +02:00
|
|
|
}
|
|
|
|
|
2024-06-06 22:08:25 +02:00
|
|
|
func (s *ERC721Processor) AvailableFor(params ProcessorInputParams) (bool, error) {
|
2024-06-05 09:56:02 +02:00
|
|
|
return params.FromChain.ChainID == params.ToChain.ChainID && params.ToToken == nil, nil
|
2023-08-24 10:45:14 +02:00
|
|
|
}
|
|
|
|
|
2024-06-06 22:08:25 +02:00
|
|
|
func (s *ERC721Processor) CalculateFees(params ProcessorInputParams) (*big.Int, *big.Int, error) {
|
2024-09-23 08:35:34 +02:00
|
|
|
return walletCommon.ZeroBigIntValue(), walletCommon.ZeroBigIntValue(), nil
|
2023-08-24 10:45:14 +02:00
|
|
|
}
|
|
|
|
|
2024-08-07 14:38:04 +02:00
|
|
|
func (s *ERC721Processor) packTxInputDataInternally(params ProcessorInputParams, functionName string) ([]byte, error) {
|
|
|
|
abi, err := abi.JSON(strings.NewReader(erc721.Erc721MetaData.ABI))
|
2023-11-02 12:35:28 +01:00
|
|
|
if err != nil {
|
2024-07-18 09:20:54 -03:00
|
|
|
return []byte{}, createERC721ErrorResponse(err)
|
2023-11-02 12:35:28 +01:00
|
|
|
}
|
2024-05-21 16:33:36 +02:00
|
|
|
|
2024-09-23 09:06:55 +02:00
|
|
|
id, err := walletCommon.GetTokenIdFromSymbol(params.FromToken.Symbol)
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, createERC721ErrorResponse(err)
|
2023-11-14 13:33:44 +01:00
|
|
|
}
|
2024-05-21 16:33:36 +02:00
|
|
|
|
2024-08-07 14:38:04 +02:00
|
|
|
return abi.Pack(functionName,
|
2024-06-05 09:56:02 +02:00
|
|
|
params.FromAddr,
|
|
|
|
params.ToAddr,
|
2023-11-14 13:33:44 +01:00
|
|
|
id,
|
|
|
|
)
|
2024-05-21 16:33:36 +02:00
|
|
|
}
|
|
|
|
|
2024-08-07 14:38:04 +02: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)
|
|
|
|
}
|
|
|
|
|
2025-01-20 11:30:27 +01:00
|
|
|
func (s *ERC721Processor) EstimateGas(params ProcessorInputParams, input []byte) (uint64, error) {
|
2024-06-18 12:31:23 +02:00
|
|
|
if params.TestsMode {
|
|
|
|
if params.TestEstimationMap != nil {
|
|
|
|
if val, ok := params.TestEstimationMap[s.Name()]; ok {
|
2024-08-23 16:01:49 +02:00
|
|
|
return val.Value, val.Err
|
2024-06-18 12:31:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, ErrNoEstimationFound
|
|
|
|
}
|
|
|
|
|
2024-06-05 09:56:02 +02:00
|
|
|
ethClient, err := s.rpcClient.EthClient(params.FromChain.ChainID)
|
2024-05-21 16:33:36 +02:00
|
|
|
if err != nil {
|
2024-07-18 09:20:54 -03:00
|
|
|
return 0, createERC721ErrorResponse(err)
|
2024-05-21 16:33:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
value := new(big.Int)
|
2023-11-02 12:35:28 +01:00
|
|
|
|
|
|
|
msg := ethereum.CallMsg{
|
2024-06-05 09:56:02 +02:00
|
|
|
From: params.FromAddr,
|
|
|
|
To: ¶ms.FromToken.Address,
|
2023-11-02 12:35:28 +01:00
|
|
|
Value: value,
|
|
|
|
Data: input,
|
|
|
|
}
|
|
|
|
|
2024-05-30 15:03:28 +02:00
|
|
|
estimation, err := ethClient.EstimateGas(context.Background(), msg)
|
2023-11-02 12:35:28 +01:00
|
|
|
if err != nil {
|
2024-07-18 09:20:54 -03:00
|
|
|
return 0, createERC721ErrorResponse(err)
|
2023-11-02 12:35:28 +01:00
|
|
|
}
|
2024-06-05 09:56:02 +02:00
|
|
|
|
2024-11-20 08:28:18 +01:00
|
|
|
increasedEstimation := float64(estimation) * pathProcessorCommon.IncreaseEstimatedGasFactor
|
2023-11-02 12:35:28 +01:00
|
|
|
return uint64(increasedEstimation), nil
|
2023-08-24 10:45:14 +02:00
|
|
|
}
|
|
|
|
|
2024-09-23 09:16:03 +02:00
|
|
|
// TODO: remove this struct once mobile switches to the new approach
|
2024-08-12 14:07:32 +02:00
|
|
|
func (s *ERC721Processor) sendOrBuild(sendArgs *MultipathProcessorTxArgs, signerFn bind.SignerFn, lastUsedNonce int64) (tx *ethTypes.Transaction, err error) {
|
2024-08-07 14:38:04 +02:00
|
|
|
from := common.Address(sendArgs.ERC721TransferTx.From)
|
|
|
|
|
|
|
|
useSafeTransferFrom := true
|
|
|
|
inputParams := ProcessorInputParams{
|
2024-08-18 21:16:09 +02:00
|
|
|
FromChain: ¶ms.Network{
|
|
|
|
ChainID: sendArgs.ChainID,
|
|
|
|
},
|
2024-08-07 14:38:04 +02:00
|
|
|
FromAddr: from,
|
|
|
|
ToAddr: sendArgs.ERC721TransferTx.Recipient,
|
|
|
|
FromToken: &token.Token{
|
2024-09-23 09:16:03 +02:00
|
|
|
Symbol: sendArgs.ERC721TransferTx.TokenID.String(),
|
|
|
|
Address: common.Address(*sendArgs.ERC721TransferTx.To),
|
2024-08-07 14:38:04 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
err = s.checkIfFunctionExists(inputParams, functionNameSafeTransferFrom)
|
|
|
|
if err != nil {
|
|
|
|
useSafeTransferFrom = false
|
|
|
|
}
|
|
|
|
|
2023-08-24 10:45:14 +02:00
|
|
|
ethClient, err := s.rpcClient.EthClient(sendArgs.ChainID)
|
|
|
|
if err != nil {
|
2024-07-18 09:20:54 -03:00
|
|
|
return tx, createERC721ErrorResponse(err)
|
2023-08-24 10:45:14 +02:00
|
|
|
}
|
2023-09-29 19:56:27 +02:00
|
|
|
|
2023-08-24 10:45:14 +02:00
|
|
|
contract, err := collectibles.NewCollectibles(common.Address(*sendArgs.ERC721TransferTx.To), ethClient)
|
|
|
|
if err != nil {
|
2024-07-18 09:20:54 -03:00
|
|
|
return tx, createERC721ErrorResponse(err)
|
2023-08-24 10:45:14 +02:00
|
|
|
}
|
2023-09-29 19:56:27 +02:00
|
|
|
|
2024-08-12 14:07:32 +02:00
|
|
|
var nonce uint64
|
|
|
|
if lastUsedNonce < 0 {
|
feat(wallet)!: allowing client to set custom values for fees, nonce, gas
Removed properties from `Path` type:
- `MaxFeesPerGas`, based on the sending flow progress appropriate new properties (`TxMaxFeesPerGas`, `ApprovalMaxFeesPerGas`) should be used
Added new properties to `Path` type:
- `RouterInputParamsUuid`, used to identify from which router input params this path was created
- `TxNonce`, used to set nonce for the tx
- `TxMaxFeesPerGas`, used to set max fees per gas for the tx
- `TxEstimatedTime`, used to estimate time for executing the tx
- `ApprovalTxNonce`, used to set nonce for the approval tx
- `ApprovalTxMaxFeesPerGas`, used to set max fees per gas for the approval tx
- `ApprovalTxEstimatedTime`, used to estimate time for executing the approval tx
New request types added:
- `PathTxCustomParams`, used to pass tx custom params from the client
- `PathTxIdentity`, used to uniquely identify path (tx) to which the custom params need to be applied
New endpoints added:
- `SetFeeMode` used to set fee mode (`GasFeeLow`, `GasFeeMedium` or `GasFeeHigh`)
- `SetCustomTxDetails` used to set custom fee mode (`SetCustomTxDetails`), if this mode is set, client needs to provide:
- Max fees per gas
- Max priority fee
- Nonce
- Gas amount
2024-11-26 12:29:00 +01:00
|
|
|
nonce, err = s.transactor.NextNonce(context.Background(), s.rpcClient, sendArgs.ChainID, sendArgs.ERC721TransferTx.From)
|
2024-08-12 14:07:32 +02:00
|
|
|
if err != nil {
|
|
|
|
return tx, createERC721ErrorResponse(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nonce = uint64(lastUsedNonce) + 1
|
2023-08-24 10:45:14 +02:00
|
|
|
}
|
2023-11-06 10:26:02 +01:00
|
|
|
|
2023-08-24 10:45:14 +02:00
|
|
|
argNonce := hexutil.Uint64(nonce)
|
|
|
|
sendArgs.ERC721TransferTx.Nonce = &argNonce
|
2023-09-29 19:56:27 +02:00
|
|
|
txOpts := sendArgs.ERC721TransferTx.ToTransactOpts(signerFn)
|
2024-08-07 14:38:04 +02: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 12:31:23 +02:00
|
|
|
if err != nil {
|
2024-07-18 09:20:54 -03:00
|
|
|
return tx, createERC721ErrorResponse(err)
|
2024-06-18 12:31:23 +02:00
|
|
|
}
|
2024-07-08 11:02:06 +02:00
|
|
|
err = s.transactor.StoreAndTrackPendingTx(from, sendArgs.ERC721TransferTx.Symbol, sendArgs.ChainID, sendArgs.ERC721TransferTx.MultiTransactionID, tx)
|
|
|
|
if err != nil {
|
2024-07-18 09:20:54 -03:00
|
|
|
return tx, createERC721ErrorResponse(err)
|
2024-07-08 11:02:06 +02:00
|
|
|
}
|
2024-06-18 12:31:23 +02:00
|
|
|
return tx, nil
|
2023-09-29 19:56:27 +02:00
|
|
|
}
|
|
|
|
|
2024-08-12 14:07:32 +02:00
|
|
|
func (s *ERC721Processor) Send(sendArgs *MultipathProcessorTxArgs, lastUsedNonce int64, verifiedAccount *account.SelectedExtKey) (hash types.Hash, usedNonce uint64, err error) {
|
2024-11-20 08:28:18 +01:00
|
|
|
tx, err := s.sendOrBuild(sendArgs, utils.GetSigner(sendArgs.ChainID, sendArgs.ERC721TransferTx.From, verifiedAccount.AccountKey.PrivateKey), lastUsedNonce)
|
2023-08-24 10:45:14 +02:00
|
|
|
if err != nil {
|
2024-08-12 14:07:32 +02:00
|
|
|
return hash, 0, createERC721ErrorResponse(err)
|
2023-08-24 10:45:14 +02:00
|
|
|
}
|
2024-08-12 14:07:32 +02:00
|
|
|
return types.Hash(tx.Hash()), tx.Nonce(), nil
|
2023-08-24 10:45:14 +02:00
|
|
|
}
|
|
|
|
|
2024-08-12 14:07:32 +02: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 19:56:27 +02:00
|
|
|
}
|
|
|
|
|
2024-11-05 22:28:24 +01:00
|
|
|
func (s *ERC721Processor) BuildTransactionV2(sendArgs *wallettypes.SendTxArgs, lastUsedNonce int64) (*ethTypes.Transaction, uint64, error) {
|
2024-10-30 19:43:46 -03:00
|
|
|
return s.transactor.ValidateAndBuildTransaction(sendArgs.FromChainID, *sendArgs, lastUsedNonce)
|
2024-09-23 09:16:03 +02:00
|
|
|
}
|
|
|
|
|
2024-06-06 22:08:25 +02:00
|
|
|
func (s *ERC721Processor) CalculateAmountOut(params ProcessorInputParams) (*big.Int, error) {
|
2024-06-05 09:56:02 +02:00
|
|
|
return params.AmountIn, nil
|
2023-08-24 10:45:14 +02:00
|
|
|
}
|
|
|
|
|
2024-06-06 22:08:25 +02:00
|
|
|
func (s *ERC721Processor) GetContractAddress(params ProcessorInputParams) (common.Address, error) {
|
2024-06-05 09:56:02 +02:00
|
|
|
return params.FromToken.Address, nil
|
2023-08-24 10:45:14 +02:00
|
|
|
}
|