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

173 lines
5.2 KiB
Go
Raw Normal View History

package pathprocessor
2024-02-06 11:36:25 +00:00
import (
"context"
"fmt"
"math/big"
"strings"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"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/ierc1155"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/rpc"
"github.com/status-im/status-go/transactions"
)
type ERC1155TxArgs struct {
2024-02-06 11:36:25 +00:00
transactions.SendTxArgs
TokenID *hexutil.Big `json:"tokenId"`
Recipient common.Address `json:"recipient"`
Amount *hexutil.Big `json:"amount"`
}
type ERC1155Processor struct {
2024-02-06 11:36:25 +00:00
rpcClient *rpc.Client
transactor transactions.TransactorIface
2024-02-06 11:36:25 +00:00
}
func NewERC1155Processor(rpcClient *rpc.Client, transactor transactions.TransactorIface) *ERC1155Processor {
return &ERC1155Processor{rpcClient: rpcClient, transactor: transactor}
2024-02-06 11:36:25 +00:00
}
func createERC1155ErrorResponse(err error) error {
return createErrorResponse(ProcessorERC1155Name, err)
}
func (s *ERC1155Processor) Name() string {
return ProcessorERC1155Name
2024-02-06 11:36:25 +00:00
}
func (s *ERC1155Processor) AvailableFor(params ProcessorInputParams) (bool, error) {
2024-06-05 07:56:02 +00:00
return params.FromChain.ChainID == params.ToChain.ChainID && params.ToToken == nil, nil
2024-02-06 11:36:25 +00:00
}
func (s *ERC1155Processor) CalculateFees(params ProcessorInputParams) (*big.Int, *big.Int, error) {
return ZeroBigIntValue, ZeroBigIntValue, nil
2024-02-06 11:36:25 +00:00
}
func (s *ERC1155Processor) PackTxInputData(params ProcessorInputParams) ([]byte, error) {
2024-03-06 10:16:20 +00:00
abi, err := abi.JSON(strings.NewReader(ierc1155.Ierc1155ABI))
2024-02-06 11:36:25 +00:00
if err != nil {
return []byte{}, createERC1155ErrorResponse(err)
2024-02-06 11:36:25 +00:00
}
2024-06-05 07:56:02 +00:00
id, success := big.NewInt(0).SetString(params.FromToken.Symbol, 0)
2024-02-06 11:36:25 +00:00
if !success {
return []byte{}, createERC1155ErrorResponse(fmt.Errorf("failed to convert %s to big.Int", params.FromToken.Symbol))
2024-02-06 11:36:25 +00:00
}
return abi.Pack("safeTransferFrom",
2024-06-05 07:56:02 +00:00
params.FromAddr,
params.ToAddr,
2024-02-06 11:36:25 +00:00
id,
2024-06-05 07:56:02 +00:00
params.AmountIn,
2024-02-06 11:36:25 +00:00
[]byte{},
)
}
func (s *ERC1155Processor) EstimateGas(params ProcessorInputParams) (uint64, error) {
if params.TestsMode {
if params.TestEstimationMap != nil {
if val, ok := params.TestEstimationMap[s.Name()]; ok {
return val.Value, val.Err
}
}
return 0, ErrNoEstimationFound
}
2024-06-05 07:56:02 +00:00
ethClient, err := s.rpcClient.EthClient(params.FromChain.ChainID)
if err != nil {
return 0, createERC1155ErrorResponse(err)
}
value := new(big.Int)
2024-02-06 11:36:25 +00:00
input, err := s.PackTxInputData(params)
2024-02-06 11:36:25 +00:00
if err != nil {
return 0, createERC1155ErrorResponse(err)
2024-02-06 11:36:25 +00:00
}
msg := ethereum.CallMsg{
2024-06-05 07:56:02 +00:00
From: params.FromAddr,
To: &params.FromToken.Address,
2024-02-06 11:36:25 +00:00
Value: value,
Data: input,
}
2024-05-30 13:03:28 +00:00
estimation, err := ethClient.EstimateGas(context.Background(), msg)
2024-02-06 11:36:25 +00:00
if err != nil {
return 0, createERC1155ErrorResponse(err)
2024-02-06 11:36:25 +00:00
}
increasedEstimation := float64(estimation) * IncreaseEstimatedGasFactor
return uint64(increasedEstimation), nil
}
func (s *ERC1155Processor) sendOrBuild(sendArgs *MultipathProcessorTxArgs, signerFn bind.SignerFn, lastUsedNonce int64) (tx *ethTypes.Transaction, err error) {
2024-02-06 11:36:25 +00:00
ethClient, err := s.rpcClient.EthClient(sendArgs.ChainID)
if err != nil {
return tx, createERC1155ErrorResponse(err)
2024-02-06 11:36:25 +00:00
}
contract, err := ierc1155.NewIerc1155(common.Address(*sendArgs.ERC1155TransferTx.To), ethClient)
if err != nil {
return tx, createERC1155ErrorResponse(err)
2024-02-06 11:36:25 +00:00
}
var nonce uint64
if lastUsedNonce < 0 {
nonce, err = s.transactor.NextNonce(s.rpcClient, sendArgs.ChainID, sendArgs.ERC1155TransferTx.From)
if err != nil {
return tx, createERC1155ErrorResponse(err)
}
} else {
nonce = uint64(lastUsedNonce) + 1
2024-02-06 11:36:25 +00:00
}
argNonce := hexutil.Uint64(nonce)
sendArgs.ERC1155TransferTx.Nonce = &argNonce
txOpts := sendArgs.ERC1155TransferTx.ToTransactOpts(signerFn)
2024-07-08 09:02:06 +00:00
from := common.Address(sendArgs.ERC1155TransferTx.From)
2024-02-06 11:36:25 +00:00
tx, err = contract.SafeTransferFrom(
2024-07-08 09:02:06 +00:00
txOpts, from,
2024-02-06 11:36:25 +00:00
sendArgs.ERC1155TransferTx.Recipient,
sendArgs.ERC1155TransferTx.TokenID.ToInt(),
sendArgs.ERC1155TransferTx.Amount.ToInt(),
[]byte{},
)
if err != nil {
return tx, createERC1155ErrorResponse(err)
}
2024-07-08 09:02:06 +00:00
err = s.transactor.StoreAndTrackPendingTx(from, sendArgs.ERC1155TransferTx.Symbol, sendArgs.ChainID, sendArgs.ERC1155TransferTx.MultiTransactionID, tx)
if err != nil {
return tx, createERC1155ErrorResponse(err)
2024-07-08 09:02:06 +00:00
}
return tx, nil
2024-02-06 11:36:25 +00:00
}
func (s *ERC1155Processor) Send(sendArgs *MultipathProcessorTxArgs, lastUsedNonce int64, verifiedAccount *account.SelectedExtKey) (hash types.Hash, usedNonce uint64, err error) {
tx, err := s.sendOrBuild(sendArgs, getSigner(sendArgs.ChainID, sendArgs.ERC1155TransferTx.From, verifiedAccount), lastUsedNonce)
2024-02-06 11:36:25 +00:00
if err != nil {
return hash, 0, createERC1155ErrorResponse(err)
2024-02-06 11:36:25 +00:00
}
return types.Hash(tx.Hash()), tx.Nonce(), nil
2024-02-06 11:36:25 +00:00
}
func (s *ERC1155Processor) BuildTransaction(sendArgs *MultipathProcessorTxArgs, lastUsedNonce int64) (*ethTypes.Transaction, uint64, error) {
tx, err := s.sendOrBuild(sendArgs, nil, lastUsedNonce)
return tx, tx.Nonce(), err
2024-02-06 11:36:25 +00:00
}
func (s *ERC1155Processor) CalculateAmountOut(params ProcessorInputParams) (*big.Int, error) {
2024-06-05 07:56:02 +00:00
return params.AmountIn, nil
2024-02-06 11:36:25 +00:00
}
func (s *ERC1155Processor) GetContractAddress(params ProcessorInputParams) (common.Address, error) {
2024-06-05 07:56:02 +00:00
return params.FromToken.Address, nil
2024-02-06 11:36:25 +00:00
}