code.vegaprotocol.io/vega@v0.79.0/wallet/api/client_get_chain_id.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 api
    17  
    18  import (
    19  	"context"
    20  
    21  	"code.vegaprotocol.io/vega/libs/jsonrpc"
    22  	walletnode "code.vegaprotocol.io/vega/wallet/api/node"
    23  )
    24  
    25  type ClientGetChainIDResult struct {
    26  	ChainID string `json:"chainID"`
    27  }
    28  
    29  type ClientGetChainID struct {
    30  	nodeSelector walletnode.Selector
    31  }
    32  
    33  func (h *ClientGetChainID) Handle(ctx context.Context) (jsonrpc.Result, *jsonrpc.ErrorDetails) {
    34  	currentNode, err := h.nodeSelector.Node(ctx, noNodeSelectionReporting)
    35  	if err != nil {
    36  		return nil, NodeCommunicationError(ErrNoHealthyNodeAvailable)
    37  	}
    38  
    39  	lastBlockData, err := currentNode.LastBlock(ctx)
    40  	if err != nil {
    41  		return nil, NodeCommunicationError(ErrCouldNotGetLastBlockInformation)
    42  	}
    43  
    44  	return ClientGetChainIDResult{
    45  		ChainID: lastBlockData.ChainID,
    46  	}, nil
    47  }
    48  
    49  func NewGetChainID(nodeSelector walletnode.Selector) *ClientGetChainID {
    50  	return &ClientGetChainID{
    51  		nodeSelector: nodeSelector,
    52  	}
    53  }