github.com/InjectiveLabs/sdk-go@v1.53.0/client/tm/tmclient.go (about)

     1  package tm
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  
     7  	log "github.com/InjectiveLabs/suplog"
     8  
     9  	rpcclient "github.com/cometbft/cometbft/rpc/client"
    10  	rpchttp "github.com/cometbft/cometbft/rpc/client/http"
    11  	ctypes "github.com/cometbft/cometbft/rpc/core/types"
    12  )
    13  
    14  type TendermintClient interface {
    15  	GetBlock(ctx context.Context, height int64) (*ctypes.ResultBlock, error)
    16  	GetLatestBlockHeight(ctx context.Context) (int64, error)
    17  	GetTxs(ctx context.Context, block *ctypes.ResultBlock) ([]*ctypes.ResultTx, error)
    18  	GetBlockResults(ctx context.Context, height int64) (*ctypes.ResultBlockResults, error)
    19  	GetValidatorSet(ctx context.Context, height int64) (*ctypes.ResultValidators, error)
    20  	GetABCIInfo(ctx context.Context) (*ctypes.ResultABCIInfo, error)
    21  }
    22  
    23  type tmClient struct {
    24  	rpcClient rpcclient.Client
    25  }
    26  
    27  func NewRPCClient(rpcNodeAddr string) TendermintClient {
    28  	rpcClient, err := rpchttp.NewWithTimeout(rpcNodeAddr, "/websocket", 10)
    29  	if err != nil {
    30  		log.WithError(err).Fatalln("failed to init rpcClient")
    31  	}
    32  
    33  	return &tmClient{
    34  		rpcClient: rpcClient,
    35  	}
    36  }
    37  
    38  // GetBlock queries for a block by height. An error is returned if the query fails.
    39  func (c *tmClient) GetBlock(ctx context.Context, height int64) (*ctypes.ResultBlock, error) {
    40  	return c.rpcClient.Block(ctx, &height)
    41  }
    42  
    43  // GetBlock queries for a block by height. An error is returned if the query fails.
    44  func (c *tmClient) GetBlockResults(ctx context.Context, height int64) (*ctypes.ResultBlockResults, error) {
    45  	return c.rpcClient.BlockResults(ctx, &height)
    46  }
    47  
    48  // GetLatestBlockHeight returns the latest block height on the active chain.
    49  func (c *tmClient) GetLatestBlockHeight(ctx context.Context) (int64, error) {
    50  	status, err := c.rpcClient.Status(ctx)
    51  	if err != nil {
    52  		return -1, err
    53  	}
    54  
    55  	height := status.SyncInfo.LatestBlockHeight
    56  
    57  	return height, nil
    58  }
    59  
    60  // GetTxs queries for all the transactions in a block height.
    61  // It uses `Tx` RPC method to query for the transaction.
    62  func (c *tmClient) GetTxs(ctx context.Context, block *ctypes.ResultBlock) ([]*ctypes.ResultTx, error) {
    63  	txs := make([]*ctypes.ResultTx, 0, len(block.Block.Txs))
    64  
    65  	for _, tmTx := range block.Block.Txs {
    66  		tx, err := c.rpcClient.Tx(ctx, tmTx.Hash(), true)
    67  		if err != nil {
    68  			if strings.HasSuffix(err.Error(), "not found") {
    69  				log.WithError(err).Errorln("failed to get Tx by hash")
    70  				continue
    71  			}
    72  
    73  			return nil, err
    74  		}
    75  
    76  		txs = append(txs, tx)
    77  	}
    78  
    79  	return txs, nil
    80  }
    81  
    82  // GetValidatorSet returns all the known Tendermint validators for a given block
    83  // height. An error is returned if the query fails.
    84  func (c *tmClient) GetValidatorSet(ctx context.Context, height int64) (*ctypes.ResultValidators, error) {
    85  	return c.rpcClient.Validators(ctx, &height, nil, nil)
    86  }
    87  
    88  // GetABCIInfo returns the node abci version
    89  func (c *tmClient) GetABCIInfo(ctx context.Context) (*ctypes.ResultABCIInfo, error) {
    90  	return c.rpcClient.ABCIInfo(ctx)
    91  }