github.com/gagliardetto/solana-go@v1.11.0/rpc/getBlock.go (about)

     1  // Copyright 2021 github.com/gagliardetto
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package rpc
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"github.com/gagliardetto/solana-go"
    22  )
    23  
    24  type TransactionDetailsType string
    25  
    26  const (
    27  	TransactionDetailsFull       TransactionDetailsType = "full"
    28  	TransactionDetailsSignatures TransactionDetailsType = "signatures"
    29  	TransactionDetailsNone       TransactionDetailsType = "none"
    30  )
    31  
    32  type GetBlockOpts struct {
    33  	// Encoding for each returned Transaction, either "json", "jsonParsed", "base58" (slow), "base64".
    34  	// If parameter not provided, the default encoding is "json".
    35  	// - "jsonParsed" encoding attempts to use program-specific instruction parsers to return
    36  	//   more human-readable and explicit data in the transaction.message.instructions list.
    37  	// - If "jsonParsed" is requested but a parser cannot be found, the instruction falls back
    38  	//   to regular JSON encoding (accounts, data, and programIdIndex fields).
    39  	//
    40  	// This parameter is optional.
    41  	Encoding solana.EncodingType
    42  
    43  	// Level of transaction detail to return.
    44  	// If parameter not provided, the default detail level is "full".
    45  	//
    46  	// This parameter is optional.
    47  	TransactionDetails TransactionDetailsType
    48  
    49  	// Whether to populate the rewards array.
    50  	// If parameter not provided, the default includes rewards.
    51  	//
    52  	// This parameter is optional.
    53  	Rewards *bool
    54  
    55  	// "processed" is not supported.
    56  	// If parameter not provided, the default is "finalized".
    57  	//
    58  	// This parameter is optional.
    59  	Commitment CommitmentType
    60  
    61  	// Max transaction version to return in responses.
    62  	// If the requested block contains a transaction with a higher version, an error will be returned.
    63  	MaxSupportedTransactionVersion *uint64
    64  }
    65  
    66  // GetBlock returns identity and transaction information about a confirmed block in the ledger.
    67  func (cl *Client) GetBlock(
    68  	ctx context.Context,
    69  	slot uint64,
    70  ) (out *GetBlockResult, err error) {
    71  	return cl.GetBlockWithOpts(
    72  		ctx,
    73  		slot,
    74  		nil,
    75  	)
    76  }
    77  
    78  // GetBlock returns identity and transaction information about a confirmed block in the ledger.
    79  //
    80  // NEW: This method is only available in solana-core v1.7 or newer.
    81  // Please use `getConfirmedBlock` for solana-core v1.6
    82  func (cl *Client) GetBlockWithOpts(
    83  	ctx context.Context,
    84  	slot uint64,
    85  	opts *GetBlockOpts,
    86  ) (out *GetBlockResult, err error) {
    87  
    88  	obj := M{
    89  		"encoding": solana.EncodingBase64,
    90  	}
    91  
    92  	if opts != nil {
    93  		if opts.TransactionDetails != "" {
    94  			obj["transactionDetails"] = opts.TransactionDetails
    95  		}
    96  		if opts.Rewards != nil {
    97  			obj["rewards"] = opts.Rewards
    98  		}
    99  		if opts.Commitment != "" {
   100  			obj["commitment"] = opts.Commitment
   101  		}
   102  		if opts.Encoding != "" {
   103  			if !solana.IsAnyOfEncodingType(
   104  				opts.Encoding,
   105  				// Valid encodings:
   106  				// solana.EncodingJSON, // TODO
   107  				// solana.EncodingJSONParsed, // TODO
   108  				solana.EncodingBase58,
   109  				solana.EncodingBase64,
   110  				solana.EncodingBase64Zstd,
   111  			) {
   112  				return nil, fmt.Errorf("provided encoding is not supported: %s", opts.Encoding)
   113  			}
   114  			obj["encoding"] = opts.Encoding
   115  		}
   116  		if opts.MaxSupportedTransactionVersion != nil {
   117  			obj["maxSupportedTransactionVersion"] = *opts.MaxSupportedTransactionVersion
   118  		}
   119  	}
   120  
   121  	params := []interface{}{slot, obj}
   122  
   123  	err = cl.rpcClient.CallForInto(ctx, &out, "getBlock", params)
   124  
   125  	if err != nil {
   126  		return nil, err
   127  	}
   128  	if out == nil {
   129  		// Block is not confirmed.
   130  		return nil, ErrNotConfirmed
   131  	}
   132  	return
   133  }
   134  
   135  type GetBlockResult struct {
   136  	// The blockhash of this block.
   137  	Blockhash solana.Hash `json:"blockhash"`
   138  
   139  	// The blockhash of this block's parent;
   140  	// if the parent block is not available due to ledger cleanup,
   141  	// this field will return "11111111111111111111111111111111".
   142  	PreviousBlockhash solana.Hash `json:"previousBlockhash"`
   143  
   144  	// The slot index of this block's parent.
   145  	ParentSlot uint64 `json:"parentSlot"`
   146  
   147  	// Present if "full" transaction details are requested.
   148  	Transactions []TransactionWithMeta `json:"transactions"`
   149  
   150  	// Present if "signatures" are requested for transaction details;
   151  	// an array of signatures, corresponding to the transaction order in the block.
   152  	Signatures []solana.Signature `json:"signatures"`
   153  
   154  	// Present if rewards are requested.
   155  	Rewards []BlockReward `json:"rewards"`
   156  
   157  	// Estimated production time, as Unix timestamp (seconds since the Unix epoch).
   158  	// Nil if not available.
   159  	BlockTime *solana.UnixTimeSeconds `json:"blockTime"`
   160  
   161  	// The number of blocks beneath this block.
   162  	BlockHeight *uint64 `json:"blockHeight"`
   163  }