code.vegaprotocol.io/vega@v0.79.0/cmd/vega/commands/watch.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package commands
    17  
    18  import (
    19  	"context"
    20  	"encoding/json"
    21  	"errors"
    22  	"fmt"
    23  
    24  	"code.vegaprotocol.io/vega/core/blockchain/abci"
    25  
    26  	tmctypes "github.com/cometbft/cometbft/rpc/core/types"
    27  	"github.com/jessevdk/go-flags"
    28  )
    29  
    30  type watch struct {
    31  	Address    string `default:"tcp://0.0.0.0:26657" description:"Node address" long:"address" short:"a"`
    32  	Positional struct {
    33  		Filters []string `positional-arg-name:"<FILTERS>"`
    34  	} `positional-args:"true"`
    35  }
    36  
    37  func (opts *watch) Execute(_ []string) error {
    38  	args := opts.Positional.Filters
    39  	if len(args) == 0 {
    40  		return errors.New("error: watch requires at least one filter")
    41  	}
    42  
    43  	c, err := abci.NewClient(opts.Address)
    44  	if err != nil {
    45  		return fmt.Errorf("could not instantiate abci client: %w", err)
    46  	}
    47  
    48  	ctx := context.Background()
    49  	fn := func(e tmctypes.ResultEvent) error {
    50  		bz, err := json.Marshal(e.Data)
    51  		if err != nil {
    52  			return err
    53  		}
    54  		fmt.Printf("%s\n", bz)
    55  		return nil
    56  	}
    57  	if err := c.Subscribe(ctx, fn, args...); err != nil {
    58  		return err
    59  	}
    60  
    61  	return nil
    62  }
    63  
    64  func Watch(ctx context.Context, parser *flags.Parser) error {
    65  	var (
    66  		shortDesc = "Watches events from Tendermint"
    67  		longDesc  = `Events results are encoded in JSON and can be filtered
    68  using a simple query language.  You can use one or more filters.
    69  See https://docs.tendermint.com/master/app-dev/subscribing-to-events-via-websocket.html
    70  for more information about the query syntax.
    71  
    72  Example:
    73  watch "tm.event = 'NewBlock'" "tm.event = 'Transaction'"`
    74  	)
    75  	_, err := parser.AddCommand("watch", shortDesc, longDesc, &watch{})
    76  	return err
    77  }