github.com/0chain/gosdk@v1.17.11/znft/transaction.go (about)

     1  package znft
     2  
     3  import (
     4  	"context"
     5  
     6  	eth "github.com/ethereum/go-ethereum"
     7  	"github.com/ethereum/go-ethereum/accounts/abi/bind"
     8  	"github.com/ethereum/go-ethereum/common"
     9  	"github.com/pkg/errors"
    10  )
    11  
    12  func (app *Znft) createTransactionWithGasPrice(ctx context.Context, address string, pack []byte) (*bind.TransactOpts, error) { //nolint
    13  	gasLimitUnits, err := app.estimateGas(ctx, address, pack)
    14  	if err != nil {
    15  		return nil, err
    16  	}
    17  
    18  	transactOpts, err := app.createSignedTransactionFromKeyStoreWithGasPrice(ctx, gasLimitUnits)
    19  
    20  	return transactOpts, err
    21  }
    22  
    23  func (app *Znft) createTransaction(ctx context.Context) (*bind.TransactOpts, error) {
    24  	transactOpts, err := app.createSignedTransactionFromKeyStore(ctx)
    25  
    26  	return transactOpts, err
    27  }
    28  
    29  func (app *Znft) estimateGas(ctx context.Context, address string, pack []byte) (uint64, error) { //nolint
    30  	etherClient, err := CreateEthClient(app.cfg.EthereumNodeURL)
    31  	if err != nil {
    32  		return 0, errors.Wrap(err, "failed to create etherClient")
    33  	}
    34  
    35  	// To (contract)
    36  	contractAddress := common.HexToAddress(address)
    37  
    38  	// Gas limits in units
    39  	fromAddress := common.HexToAddress(app.cfg.WalletAddress)
    40  
    41  	// Estimate gas
    42  	gasLimitUnits, err := etherClient.EstimateGas(ctx, eth.CallMsg{
    43  		To:   &contractAddress,
    44  		From: fromAddress,
    45  		Data: pack,
    46  	})
    47  	if err != nil {
    48  		return 0, errors.Wrap(err, "failed to estimate gas")
    49  	}
    50  
    51  	return gasLimitUnits, nil
    52  }