github.com/gagliardetto/solana-go@v1.11.0/rpc/ws/slotsUpdatesSubscribe.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 ws
    16  
    17  import "github.com/gagliardetto/solana-go"
    18  
    19  type SlotsUpdatesResult struct {
    20  	// The parent slot.
    21  	Parent uint64 `json:"parent"`
    22  	// The newly updated slot.
    23  	Slot uint64 `json:"slot"`
    24  	// The Unix timestamp of the update.
    25  	Timestamp *solana.UnixTimeMilliseconds `json:"timestamp"`
    26  	// The update type.
    27  	Type SlotsUpdatesType `json:"type"`
    28  	// Extra stats provided when a bank is frozen.
    29  	Stats *BankStats `json:"stats"`
    30  }
    31  
    32  type BankStats struct {
    33  	NumTransactionEntries     uint64 `json:"numTransactionEntries"`
    34  	NumSuccessfulTransactions uint64 `json:"numSuccessfulTransactions"`
    35  	NumFailedTransactions     uint64 `json:"numFailedTransactions"`
    36  	MaxTransactionsPerEntry   uint64 `json:"maxTransactionsPerEntry"`
    37  }
    38  
    39  type SlotsUpdatesType string
    40  
    41  const (
    42  	SlotsUpdatesFirstShredReceived     SlotsUpdatesType = "firstShredReceived"
    43  	SlotsUpdatesCompleted              SlotsUpdatesType = "completed"
    44  	SlotsUpdatesCreatedBank            SlotsUpdatesType = "createdBank"
    45  	SlotsUpdatesFrozen                 SlotsUpdatesType = "frozen"
    46  	SlotsUpdatesDead                   SlotsUpdatesType = "dead"
    47  	SlotsUpdatesOptimisticConfirmation SlotsUpdatesType = "optimisticConfirmation"
    48  	SlotsUpdatesRoot                   SlotsUpdatesType = "root"
    49  )
    50  
    51  // SlotsUpdatesSubscribe (UNSTABLE) subscribes to receive a notification
    52  // from the validator on a variety of updates on every slot.
    53  //
    54  // This subscription is unstable; the format of this subscription
    55  // may change in the future and it may not always be supported.
    56  func (cl *Client) SlotsUpdatesSubscribe() (*SlotsUpdatesSubscription, error) {
    57  	genSub, err := cl.subscribe(
    58  		nil,
    59  		nil,
    60  		"slotsUpdatesSubscribe",
    61  		"slotsUpdatesUnsubscribe",
    62  		func(msg []byte) (interface{}, error) {
    63  			var res SlotsUpdatesResult
    64  			err := decodeResponseFromMessage(msg, &res)
    65  			return &res, err
    66  		},
    67  	)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  	return &SlotsUpdatesSubscription{
    72  		sub: genSub,
    73  	}, nil
    74  }
    75  
    76  type SlotsUpdatesSubscription struct {
    77  	sub *Subscription
    78  }
    79  
    80  func (sw *SlotsUpdatesSubscription) Recv() (*SlotsUpdatesResult, error) {
    81  	select {
    82  	case d := <-sw.sub.stream:
    83  		return d.(*SlotsUpdatesResult), nil
    84  	case err := <-sw.sub.err:
    85  		return nil, err
    86  	}
    87  }
    88  
    89  func (sw *SlotsUpdatesSubscription) Err() <-chan error {
    90  	return sw.sub.err
    91  }
    92  
    93  func (sw *SlotsUpdatesSubscription) Response() <-chan *SlotsUpdatesResult {
    94  	typedChan := make(chan *SlotsUpdatesResult, 1)
    95  	go func(ch chan *SlotsUpdatesResult) {
    96  		// TODO: will this subscription yield more than one result?
    97  		d, ok := <-sw.sub.stream
    98  		if !ok {
    99  			return
   100  		}
   101  		ch <- d.(*SlotsUpdatesResult)
   102  	}(typedChan)
   103  	return typedChan
   104  }
   105  
   106  func (sw *SlotsUpdatesSubscription) Unsubscribe() {
   107  	sw.sub.Unsubscribe()
   108  }