code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_list_networks.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 "fmt" 21 22 "code.vegaprotocol.io/vega/libs/jsonrpc" 23 "code.vegaprotocol.io/vega/wallet/network" 24 ) 25 26 type AdminListNetworksResult struct { 27 Networks []AdminListNetworkResult `json:"networks"` 28 } 29 30 type AdminListNetworkResult struct { 31 Name string `json:"name"` 32 Metadata []network.Metadata `json:"metadata"` 33 } 34 35 type AdminListNetworks struct { 36 networkStore NetworkStore 37 } 38 39 // Handle List all registered networks. 40 func (h *AdminListNetworks) Handle(_ context.Context, _ jsonrpc.Params) (jsonrpc.Result, *jsonrpc.ErrorDetails) { 41 networks, err := h.networkStore.ListNetworks() 42 if err != nil { 43 return nil, InternalError(fmt.Errorf("could not list the networks: %w", err)) 44 } 45 46 netsWithMetadata := make([]AdminListNetworkResult, 0, len(networks)) 47 for _, networkName := range networks { 48 net, err := h.networkStore.GetNetwork(networkName) 49 if err != nil { 50 continue 51 } 52 netsWithMetadata = append(netsWithMetadata, AdminListNetworkResult{ 53 Name: networkName, 54 Metadata: net.Metadata, 55 }) 56 } 57 58 return AdminListNetworksResult{ 59 Networks: netsWithMetadata, 60 }, nil 61 } 62 63 func NewAdminListNetworks( 64 networkStore NetworkStore, 65 ) *AdminListNetworks { 66 return &AdminListNetworks{ 67 networkStore: networkStore, 68 } 69 }