2023-08-24 08:45:14 +00:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
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"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
"github.com/status-im/status-go/params"
|
|
|
|
"github.com/status-im/status-go/rpc"
|
|
|
|
"github.com/status-im/status-go/services/wallet/token"
|
|
|
|
"github.com/status-im/status-go/transactions"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ERC721TransferTxArgs struct {
|
|
|
|
transactions.SendTxArgs
|
|
|
|
TokenID *hexutil.Big `json:"tokenId"`
|
|
|
|
Recipient common.Address `json:"recipient"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ERC721TransferBridge struct {
|
|
|
|
rpcClient *rpc.Client
|
2024-05-26 08:31:13 +00:00
|
|
|
transactor transactions.TransactorIface
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
|
|
|
|
2024-05-26 08:31:13 +00:00
|
|
|
func NewERC721TransferBridge(rpcClient *rpc.Client, transactor transactions.TransactorIface) *ERC721TransferBridge {
|
2023-08-24 08:45:14 +00:00
|
|
|
return &ERC721TransferBridge{rpcClient: rpcClient, transactor: transactor}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ERC721TransferBridge) Name() string {
|
|
|
|
return "ERC721Transfer"
|
|
|
|
}
|
|
|
|
|
2024-05-14 19:11:16 +00:00
|
|
|
func (s *ERC721TransferBridge) AvailableFor(from, to *params.Network, token *token.Token, toToken *token.Token) (bool, error) {
|
2024-04-18 10:22:28 +00:00
|
|
|
return from.ChainID == to.ChainID && toToken == nil, nil
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
|
|
|
|
2024-05-14 19:11:16 +00:00
|
|
|
func (s *ERC721TransferBridge) CalculateFees(from, to *params.Network, token *token.Token, amountIn *big.Int) (*big.Int, *big.Int, error) {
|
2023-08-24 08:45:14 +00:00
|
|
|
return big.NewInt(0), big.NewInt(0), nil
|
|
|
|
}
|
|
|
|
|
2024-05-23 12:38:39 +00:00
|
|
|
func (s *ERC721TransferBridge) PackTxInputData(contractType string, fromNetwork *params.Network, toNetwork *params.Network, from common.Address, to common.Address, token *token.Token, amountIn *big.Int) ([]byte, error) {
|
2023-11-02 11:35:28 +00:00
|
|
|
abi, err := abi.JSON(strings.NewReader(collectibles.CollectiblesMetaData.ABI))
|
|
|
|
if err != nil {
|
2024-05-21 14:33:36 +00:00
|
|
|
return []byte{}, err
|
2023-11-02 11:35:28 +00:00
|
|
|
}
|
2024-05-21 14:33:36 +00:00
|
|
|
|
2023-11-14 12:33:44 +00:00
|
|
|
id, success := big.NewInt(0).SetString(token.Symbol, 0)
|
|
|
|
if !success {
|
2024-05-21 14:33:36 +00:00
|
|
|
return []byte{}, fmt.Errorf("failed to convert %s to big.Int", token.Symbol)
|
2023-11-14 12:33:44 +00:00
|
|
|
}
|
2024-05-21 14:33:36 +00:00
|
|
|
|
|
|
|
return abi.Pack("safeTransferFrom",
|
2023-11-02 11:35:28 +00:00
|
|
|
from,
|
|
|
|
to,
|
2023-11-14 12:33:44 +00:00
|
|
|
id,
|
|
|
|
)
|
2024-05-21 14:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ERC721TransferBridge) EstimateGas(fromNetwork *params.Network, toNetwork *params.Network, from common.Address, to common.Address, token *token.Token, toToken *token.Token, amountIn *big.Int) (uint64, error) {
|
|
|
|
ethClient, err := s.rpcClient.EthClient(fromNetwork.ChainID)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
value := new(big.Int)
|
2023-11-02 11:35:28 +00:00
|
|
|
|
2024-05-23 12:38:39 +00:00
|
|
|
input, err := s.PackTxInputData("", fromNetwork, toNetwork, from, to, token, amountIn)
|
2023-11-02 11:35:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2023-11-10 13:17:39 +00:00
|
|
|
if code, err := ethClient.PendingCodeAt(ctx, token.Address); err != nil {
|
2023-11-02 11:35:28 +00:00
|
|
|
return 0, err
|
|
|
|
} else if len(code) == 0 {
|
|
|
|
return 0, bind.ErrNoCode
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := ethereum.CallMsg{
|
|
|
|
From: from,
|
2023-11-10 13:17:39 +00:00
|
|
|
To: &token.Address,
|
2023-11-02 11:35:28 +00:00
|
|
|
Value: value,
|
|
|
|
Data: input,
|
|
|
|
}
|
|
|
|
|
|
|
|
estimation, err := ethClient.EstimateGas(ctx, msg)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
increasedEstimation := float64(estimation) * IncreaseEstimatedGasFactor
|
|
|
|
return uint64(increasedEstimation), nil
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
|
|
|
|
2024-04-25 14:43:58 +00:00
|
|
|
func (s *ERC721TransferBridge) BuildTx(network, _ *params.Network, fromAddress common.Address, toAddress common.Address, token *token.Token, amountIn *big.Int, _ *big.Int) (*ethTypes.Transaction, error) {
|
2024-03-30 11:41:02 +00:00
|
|
|
contractAddress := types.Address(token.Address)
|
|
|
|
|
|
|
|
// We store ERC721 Token ID using big.Int.String() in token.Symbol
|
|
|
|
tokenID, success := new(big.Int).SetString(token.Symbol, 10)
|
|
|
|
if !success {
|
|
|
|
return nil, fmt.Errorf("failed to convert ERC721's Symbol %s to big.Int", token.Symbol)
|
|
|
|
}
|
|
|
|
|
2024-03-25 12:40:00 +00:00
|
|
|
sendArgs := &TransactionBridge{
|
|
|
|
ERC721TransferTx: &ERC721TransferTxArgs{
|
|
|
|
SendTxArgs: transactions.SendTxArgs{
|
|
|
|
From: types.Address(fromAddress),
|
2024-03-30 11:41:02 +00:00
|
|
|
To: &contractAddress,
|
2024-03-25 12:40:00 +00:00
|
|
|
Value: (*hexutil.Big)(amountIn),
|
|
|
|
Data: types.HexBytes("0x0"),
|
|
|
|
},
|
2024-03-30 11:41:02 +00:00
|
|
|
TokenID: (*hexutil.Big)(tokenID),
|
2024-03-25 12:40:00 +00:00
|
|
|
Recipient: toAddress,
|
|
|
|
},
|
2024-03-30 11:41:02 +00:00
|
|
|
ChainID: network.ChainID,
|
2024-03-25 12:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.BuildTransaction(sendArgs)
|
|
|
|
}
|
|
|
|
|
2023-12-19 13:38:01 +00:00
|
|
|
func (s *ERC721TransferBridge) sendOrBuild(sendArgs *TransactionBridge, 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 {
|
2023-12-19 13:38:01 +00:00
|
|
|
return tx, 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 {
|
2023-12-19 13:38:01 +00:00
|
|
|
return tx, 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 {
|
2023-12-19 13:38:01 +00:00
|
|
|
return tx, 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)
|
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())
|
2023-12-19 13:38:01 +00:00
|
|
|
return tx, err
|
2023-09-29 17:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ERC721TransferBridge) Send(sendArgs *TransactionBridge, 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 {
|
|
|
|
return hash, err
|
|
|
|
}
|
|
|
|
return types.Hash(tx.Hash()), nil
|
|
|
|
}
|
|
|
|
|
2023-12-19 13:38:01 +00:00
|
|
|
func (s *ERC721TransferBridge) BuildTransaction(sendArgs *TransactionBridge) (*ethTypes.Transaction, error) {
|
2023-09-29 17:56:27 +00:00
|
|
|
return s.sendOrBuild(sendArgs, nil)
|
|
|
|
}
|
|
|
|
|
2023-08-24 08:45:14 +00:00
|
|
|
func (s *ERC721TransferBridge) CalculateAmountOut(from, to *params.Network, amountIn *big.Int, symbol string) (*big.Int, error) {
|
|
|
|
return amountIn, nil
|
|
|
|
}
|
|
|
|
|
2024-05-23 12:38:39 +00:00
|
|
|
func (s *ERC721TransferBridge) GetContractAddress(network *params.Network, token *token.Token) (common.Address, error) {
|
|
|
|
return token.Address, nil
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|