github.com/gagliardetto/solana-go@v1.11.0/rpc/getLeaderSchedule.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  // GetLeaderSchedule returns the leader schedule for current epoch.
    24  func (cl *Client) GetLeaderSchedule(
    25  	ctx context.Context,
    26  ) (out GetLeaderScheduleResult, err error) {
    27  	return cl.GetLeaderScheduleWithOpts(
    28  		ctx,
    29  		nil,
    30  	)
    31  }
    32  
    33  type GetLeaderScheduleOpts struct {
    34  	Commitment CommitmentType
    35  
    36  	// Fetch the leader schedule for the epoch that corresponds
    37  	// to the provided slot.
    38  	// If unspecified, the leader schedule for the current epoch is fetched
    39  	Epoch *uint64
    40  
    41  	// TODO: is identity a pubkey?
    42  	Identity *solana.PublicKey // Only return results for this validator identity
    43  }
    44  
    45  // GetLeaderScheduleWithOpts returns the leader schedule for an epoch.
    46  func (cl *Client) GetLeaderScheduleWithOpts(
    47  	ctx context.Context,
    48  	opts *GetLeaderScheduleOpts,
    49  ) (out GetLeaderScheduleResult, err error) {
    50  	params := []interface{}{}
    51  	if opts != nil {
    52  		if opts.Epoch != nil {
    53  			params = append(params, opts.Epoch)
    54  		}
    55  		obj := M{}
    56  		if opts.Commitment != "" {
    57  			obj["commitment"] = opts.Commitment
    58  		}
    59  		if opts.Identity != nil {
    60  			obj["identity"] = opts.Identity
    61  		}
    62  		if len(obj) > 0 {
    63  			params = append(params, obj)
    64  		}
    65  	}
    66  	err = cl.rpcClient.CallForInto(ctx, &out, "getLeaderSchedule", params)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	// TODO: check that this behaviour is implemented everywhere:
    71  	if out == nil {
    72  		return nil, ErrNotFound
    73  	}
    74  	return
    75  }
    76  
    77  // The result field will be a dictionary of validator identities,
    78  // and their corresponding leader slot indices as values
    79  // (indices are relative to the first slot in the requested epoch).
    80  type GetLeaderScheduleResult map[solana.PublicKey][]uint64