code.vegaprotocol.io/vega@v0.79.0/core/examples/nullchain/connection.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 nullchain
    17  
    18  import (
    19  	"context"
    20  	"time"
    21  
    22  	"github.com/pkg/errors"
    23  	"google.golang.org/grpc"
    24  
    25  	"code.vegaprotocol.io/vega/core/examples/nullchain/config"
    26  	v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
    27  	"code.vegaprotocol.io/vega/protos/vega"
    28  	api "code.vegaprotocol.io/vega/protos/vega/api/v1"
    29  )
    30  
    31  type Connection struct {
    32  	conn     *grpc.ClientConn
    33  	core     api.CoreServiceClient
    34  	datanode v2.TradingDataServiceClient
    35  	timeout  time.Duration
    36  }
    37  
    38  func NewConnection() (*Connection, error) {
    39  	conn, err := grpc.Dial(config.GRCPAddress, grpc.WithInsecure())
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  
    44  	return &Connection{
    45  		conn:     conn,
    46  		core:     api.NewCoreServiceClient(conn),
    47  		datanode: v2.NewTradingDataServiceClient(conn),
    48  		timeout:  5 * time.Second,
    49  	}, nil
    50  }
    51  
    52  func (c *Connection) Close() error {
    53  	return c.conn.Close()
    54  }
    55  
    56  func (c *Connection) LastBlockHeight() (uint64, error) {
    57  	ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
    58  	defer cancel()
    59  
    60  	bhReq := &api.LastBlockHeightRequest{}
    61  	resp, err := c.core.LastBlockHeight(ctx, bhReq)
    62  	if err != nil {
    63  		return 0, errors.WithStack(err)
    64  	}
    65  	return resp.Height, nil
    66  }
    67  
    68  func (c *Connection) NetworkChainID() (string, error) {
    69  	ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
    70  	defer cancel()
    71  
    72  	bhReq := &api.StatisticsRequest{}
    73  	resp, err := c.core.Statistics(ctx, bhReq)
    74  	if err != nil {
    75  		return "", errors.WithStack(err)
    76  	}
    77  	return resp.Statistics.ChainId, nil
    78  }
    79  
    80  func (c *Connection) VegaTime() (time.Time, error) {
    81  	ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
    82  	defer cancel()
    83  
    84  	gvtReq := &v2.GetVegaTimeRequest{}
    85  	response, err := c.datanode.GetVegaTime(ctx, gvtReq)
    86  	if err != nil {
    87  		return time.Time{}, errors.WithStack(err)
    88  	}
    89  
    90  	t := time.Unix(0, response.Timestamp)
    91  	return t, nil
    92  }
    93  
    94  func (c *Connection) GetProposalsByParty(party *Party) ([]*vega.GovernanceData, error) {
    95  	ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
    96  	defer cancel()
    97  
    98  	r, err := c.datanode.ListGovernanceData(ctx,
    99  		&v2.ListGovernanceDataRequest{
   100  			ProposerPartyId: &party.pubkey,
   101  		})
   102  	if err != nil {
   103  		return nil, errors.WithStack(err)
   104  	}
   105  
   106  	var proposals []*vega.GovernanceData
   107  	for _, gd := range r.GetConnection().GetEdges() {
   108  		proposals = append(proposals, gd.GetNode())
   109  	}
   110  
   111  	return proposals, nil
   112  }
   113  
   114  func (c *Connection) GetProposalByReference(ref string) (*vega.Proposal, error) {
   115  	ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
   116  	defer cancel()
   117  
   118  	r, err := c.datanode.GetGovernanceData(ctx,
   119  		&v2.GetGovernanceDataRequest{
   120  			Reference: &ref,
   121  		})
   122  	if err != nil {
   123  		return nil, errors.WithStack(err)
   124  	}
   125  
   126  	return r.Data.Proposal, nil
   127  }
   128  
   129  func (c *Connection) GetMarkets() ([]*vega.Market, error) {
   130  	r, err := c.datanode.ListMarkets(context.Background(), &v2.ListMarketsRequest{})
   131  	if err != nil {
   132  		return nil, errors.WithStack(err)
   133  	}
   134  
   135  	var markets []*vega.Market
   136  	for _, m := range r.GetMarkets().GetEdges() {
   137  		markets = append(markets, m.GetNode())
   138  	}
   139  
   140  	return markets, nil
   141  }