github.com/0xsequence/ethkit@v1.25.0/ethwallet/provider.go (about)

     1  package ethwallet
     2  
     3  import (
     4  	"context"
     5  	"math/big"
     6  
     7  	"github.com/0xsequence/ethkit/ethrpc"
     8  	"github.com/0xsequence/ethkit/go-ethereum/accounts/abi/bind"
     9  )
    10  
    11  // WalletProvider is a helper to query the provider in context of the wallet address
    12  type WalletProvider struct {
    13  	wallet   *Wallet
    14  	provider *ethrpc.Provider
    15  }
    16  
    17  func (w *WalletProvider) Backend() *ethrpc.Provider {
    18  	return w.provider
    19  }
    20  
    21  func (w *WalletProvider) NewTransactor(ctx context.Context) (*bind.TransactOpts, error) {
    22  	// Get suggested gas price, the user can change this on their own too
    23  	gasPrice, err := w.provider.SuggestGasPrice(ctx)
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  
    28  	auth, err := w.wallet.Transactor(ctx)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  
    33  	auth.Value = big.NewInt(0)
    34  	auth.GasLimit = 0 // (0 = estimate)
    35  	auth.GasPrice = gasPrice
    36  	auth.Nonce = nil // remains unset, will be auto-set or user can specify
    37  
    38  	return auth, nil
    39  }
    40  
    41  func (w *WalletProvider) GetEtherBalanceAt(ctx context.Context, blockNum *big.Int) (*big.Int, error) {
    42  	balance, err := w.provider.BalanceAt(ctx, w.wallet.Address(), blockNum)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  	return balance, nil
    47  }
    48  
    49  func (w *WalletProvider) GetTransactionCount(ctx context.Context) (uint64, error) {
    50  	nonce, err := w.provider.PendingNonceAt(ctx, w.wallet.Address())
    51  	if err != nil {
    52  		return 0, err
    53  	}
    54  	return nonce, nil
    55  }