code.vegaprotocol.io/vega@v0.79.0/wallet/network/network.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 network
    17  
    18  import (
    19  	"errors"
    20  	"fmt"
    21  )
    22  
    23  var ErrNetworkDoesNotHaveGRPCHostConfigured = errors.New("network configuration does not have any gRPC host set")
    24  
    25  type Network struct {
    26  	Name     string     `json:"name"`
    27  	Metadata []Metadata `json:"metadata"`
    28  	API      APIConfig  `json:"api"`
    29  	Apps     AppsConfig `json:"apps"`
    30  }
    31  
    32  type APIConfig struct {
    33  	GRPC    HostConfig `json:"grpc"`
    34  	REST    HostConfig `json:"rest"`
    35  	GraphQL HostConfig `json:"graphQL"`
    36  }
    37  
    38  type HostConfig struct {
    39  	Hosts []string `json:"hosts"`
    40  }
    41  
    42  type AppsConfig struct {
    43  	Console    string `json:"console"`
    44  	Governance string `json:"governance"`
    45  	Explorer   string `json:"explorer"`
    46  }
    47  
    48  type Metadata struct {
    49  	Key   string `json:"key"`
    50  	Value string `json:"value"`
    51  }
    52  
    53  func (n *Network) EnsureCanConnectGRPCNode() error {
    54  	if len(n.API.GRPC.Hosts) > 0 && len(n.API.GRPC.Hosts[0]) > 0 {
    55  		return nil
    56  	}
    57  	return ErrNetworkDoesNotHaveGRPCHostConfigured
    58  }
    59  
    60  func GetNetwork(store Store, name string) (*Network, error) {
    61  	exists, err := store.NetworkExists(name)
    62  	if err != nil {
    63  		return nil, fmt.Errorf("couldn't verify network exists: %w", err)
    64  	}
    65  	if !exists {
    66  		return nil, NewDoesNotExistError(name)
    67  	}
    68  	n, err := store.GetNetwork(name)
    69  	if err != nil {
    70  		return nil, fmt.Errorf("couldn't get network %s: %w", name, err)
    71  	}
    72  
    73  	return n, nil
    74  }
    75  
    76  //go:generate go run github.com/golang/mock/mockgen -destination mocks/store_mock.go -package mocks code.vegaprotocol.io/vega/wallet/network Store
    77  type Store interface {
    78  	NetworkExists(string) (bool, error)
    79  	GetNetwork(string) (*Network, error)
    80  }