github.com/gagliardetto/solana-go@v1.11.0/rpc/getBlockProduction.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  
    20  	"github.com/gagliardetto/solana-go"
    21  )
    22  
    23  type GetBlockProductionResult struct {
    24  	RPCContext
    25  	Value BlockProductionResult `json:"value"`
    26  }
    27  
    28  type GetBlockProductionOpts struct {
    29  	//
    30  	// This parameter is optional.
    31  	Commitment CommitmentType `json:"commitment,omitempty"`
    32  
    33  	// Slot range to return block production for.
    34  	// If parameter not provided, defaults to current epoch.
    35  	//
    36  	// This parameter is optional.
    37  	Range *SlotRangeRequest `json:"range,omitempty"`
    38  
    39  	// Only return results for this validator identity.
    40  	//
    41  	// This parameter is optional.
    42  	Identity *solana.PublicKey `json:"identity,omitempty"`
    43  }
    44  
    45  type SlotRangeRequest struct {
    46  	// First slot to return block production information for (inclusive)
    47  	FirstSlot uint64 `json:"firstSlot"`
    48  
    49  	// Last slot to return block production information for (inclusive).
    50  	// If parameter not provided, defaults to the highest slot
    51  	//
    52  	// This parameter is optional.
    53  	LastSlot *uint64 `json:"lastSlot,omitempty"`
    54  }
    55  
    56  // GetBlockProduction returns recent block production information from the current or previous epoch.
    57  func (cl *Client) GetBlockProduction(
    58  	ctx context.Context,
    59  ) (out *GetBlockProductionResult, err error) {
    60  	return cl.GetBlockProductionWithOpts(
    61  		ctx,
    62  		nil,
    63  	)
    64  }
    65  
    66  // GetBlockProduction returns recent block production information from the current or previous epoch.
    67  func (cl *Client) GetBlockProductionWithOpts(
    68  	ctx context.Context,
    69  	opts *GetBlockProductionOpts,
    70  ) (out *GetBlockProductionResult, err error) {
    71  	params := []interface{}{}
    72  
    73  	if opts != nil {
    74  		obj := M{}
    75  		if opts.Commitment != "" {
    76  			obj["commitment"] = opts.Commitment
    77  		}
    78  		if opts.Range != nil {
    79  			rngObj := M{}
    80  			rngObj["firstSlot"] = opts.Range.FirstSlot
    81  			if opts.Range.LastSlot != nil {
    82  				rngObj["lastSlot"] = opts.Range.LastSlot
    83  			}
    84  			obj["range"] = rngObj
    85  		}
    86  		if opts.Identity != nil {
    87  			obj["identity"] = opts.Identity
    88  		}
    89  		if len(obj) != 0 {
    90  			params = append(params, obj)
    91  		}
    92  	}
    93  	err = cl.rpcClient.CallForInto(ctx, &out, "getBlockProduction", params)
    94  
    95  	return
    96  }
    97  
    98  type BlockProductionResult struct {
    99  	ByIdentity IdentityToSlotsBlocks `json:"byIdentity"`
   100  
   101  	Range SlotRangeResponse `json:"range"`
   102  }
   103  
   104  // A dictionary of validator identities.
   105  // Value is a two element array containing the number
   106  // of leader slots and the number of blocks produced.
   107  type IdentityToSlotsBlocks map[solana.PublicKey][2]int64
   108  
   109  type SlotRangeResponse struct {
   110  	// First slot of the block production information (inclusive)
   111  	FirstSlot uint64 `json:"firstSlot"`
   112  
   113  	// Last slot of block production information (inclusive)
   114  	LastSlot uint64 `json:"lastSlot"`
   115  }