github.com/gagliardetto/solana-go@v1.11.0/rpc/ws/logsSubscribe.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  	"github.com/gagliardetto/solana-go/rpc"
    20  )
    21  
    22  type LogResult struct {
    23  	Context struct {
    24  		Slot uint64
    25  	} `json:"context"`
    26  	Value struct {
    27  		// The transaction signature.
    28  		Signature solana.Signature `json:"signature"`
    29  		// Error if transaction failed, null if transaction succeeded.
    30  		Err interface{} `json:"err"`
    31  		// Array of log messages the transaction instructions output
    32  		// during execution, null if simulation failed before the transaction
    33  		// was able to execute (for example due to an invalid blockhash
    34  		// or signature verification failure)
    35  		Logs []string `json:"logs"`
    36  	} `json:"value"`
    37  }
    38  
    39  type LogsSubscribeFilterType string
    40  
    41  const (
    42  	// Subscribe to all transactions except for simple vote transactions.
    43  	LogsSubscribeFilterAll LogsSubscribeFilterType = "all"
    44  	// Subscribe to all transactions including simple vote transactions.
    45  	LogsSubscribeFilterAllWithVotes LogsSubscribeFilterType = "allWithVotes"
    46  )
    47  
    48  // LogsSubscribe subscribes to transaction logging.
    49  func (cl *Client) LogsSubscribe(
    50  	// Filter criteria for the logs to receive results by account type.
    51  	filter LogsSubscribeFilterType,
    52  	commitment rpc.CommitmentType, // (optional)
    53  ) (*LogSubscription, error) {
    54  	return cl.logsSubscribe(
    55  		filter,
    56  		commitment,
    57  	)
    58  }
    59  
    60  // LogsSubscribe subscribes to all transactions that mention the provided Pubkey.
    61  func (cl *Client) LogsSubscribeMentions(
    62  	// Subscribe to all transactions that mention the provided Pubkey.
    63  	mentions solana.PublicKey,
    64  	// (optional)
    65  	commitment rpc.CommitmentType,
    66  ) (*LogSubscription, error) {
    67  	return cl.logsSubscribe(
    68  		rpc.M{
    69  			"mentions": []string{mentions.String()},
    70  		},
    71  		commitment,
    72  	)
    73  }
    74  
    75  // LogsSubscribe subscribes to transaction logging.
    76  func (cl *Client) logsSubscribe(
    77  	filter interface{},
    78  	commitment rpc.CommitmentType,
    79  ) (*LogSubscription, error) {
    80  
    81  	params := []interface{}{filter}
    82  	conf := map[string]interface{}{}
    83  	if commitment != "" {
    84  		conf["commitment"] = commitment
    85  	}
    86  
    87  	genSub, err := cl.subscribe(
    88  		params,
    89  		conf,
    90  		"logsSubscribe",
    91  		"logsUnsubscribe",
    92  		func(msg []byte) (interface{}, error) {
    93  			var res LogResult
    94  			err := decodeResponseFromMessage(msg, &res)
    95  			return &res, err
    96  		},
    97  	)
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  	return &LogSubscription{
   102  		sub: genSub,
   103  	}, nil
   104  }
   105  
   106  type LogSubscription struct {
   107  	sub *Subscription
   108  }
   109  
   110  func (sw *LogSubscription) Recv() (*LogResult, error) {
   111  	select {
   112  	case d := <-sw.sub.stream:
   113  		return d.(*LogResult), nil
   114  	case err := <-sw.sub.err:
   115  		return nil, err
   116  	}
   117  }
   118  
   119  func (sw *LogSubscription) Err() <-chan error {
   120  	return sw.sub.err
   121  }
   122  
   123  func (sw *LogSubscription) Response() <-chan *LogResult {
   124  	typedChan := make(chan *LogResult, 1)
   125  	go func(ch chan *LogResult) {
   126  		// TODO: will this subscription yield more than one result?
   127  		d, ok := <-sw.sub.stream
   128  		if !ok {
   129  			return
   130  		}
   131  		ch <- d.(*LogResult)
   132  	}(typedChan)
   133  	return typedChan
   134  }
   135  
   136  func (sw *LogSubscription) Unsubscribe() {
   137  	sw.sub.Unsubscribe()
   138  }