github.com/gagliardetto/solana-go@v1.11.0/rpc/ws/voteSubscribe.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 ( 18 "github.com/gagliardetto/solana-go" 19 ) 20 21 type VoteResult struct { 22 // The vote hash. 23 Hash solana.Hash `json:"hash"` 24 // The slots covered by the vote. 25 Slots []uint64 `json:"slots"` 26 // The timestamp of the vote. 27 Timestamp *solana.UnixTimeSeconds `json:"timestamp,omitempty"` 28 } 29 30 // VoteSubscribe (UNSTABLE, disabled by default) subscribes 31 // to receive notification anytime a new vote is observed in gossip. 32 // These votes are pre-consensus therefore there is 33 // no guarantee these votes will enter the ledger. 34 // 35 // This subscription is unstable and only available if the validator 36 // was started with the --rpc-pubsub-enable-vote-subscription flag. 37 // The format of this subscription may change in the future. 38 func (cl *Client) VoteSubscribe() (*VoteSubscription, error) { 39 genSub, err := cl.subscribe( 40 nil, 41 nil, 42 "voteSubscribe", 43 "voteUnsubscribe", 44 func(msg []byte) (interface{}, error) { 45 var res VoteResult 46 err := decodeResponseFromMessage(msg, &res) 47 return &res, err 48 }, 49 ) 50 if err != nil { 51 return nil, err 52 } 53 return &VoteSubscription{ 54 sub: genSub, 55 }, nil 56 } 57 58 type VoteSubscription struct { 59 sub *Subscription 60 } 61 62 func (sw *VoteSubscription) Recv() (*VoteResult, error) { 63 select { 64 case d := <-sw.sub.stream: 65 return d.(*VoteResult), nil 66 case err := <-sw.sub.err: 67 return nil, err 68 } 69 } 70 71 func (sw *VoteSubscription) Err() <-chan error { 72 return sw.sub.err 73 } 74 75 func (sw *VoteSubscription) Response() <-chan *VoteResult { 76 typedChan := make(chan *VoteResult, 1) 77 go func(ch chan *VoteResult) { 78 // TODO: will this subscription yield more than one result? 79 d, ok := <-sw.sub.stream 80 if !ok { 81 return 82 } 83 ch <- d.(*VoteResult) 84 }(typedChan) 85 return typedChan 86 } 87 88 func (sw *VoteSubscription) Unsubscribe() { 89 sw.sub.Unsubscribe() 90 }