code.vegaprotocol.io/vega@v0.79.0/cmd/vega/commands/query/accounts.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 query 17 18 import ( 19 "errors" 20 "fmt" 21 22 apipb "code.vegaprotocol.io/vega/protos/vega/api/v1" 23 24 "github.com/golang/protobuf/jsonpb" 25 ) 26 27 type AccountsCmd struct { 28 Party AccountsPartyCmd `command:"party" description:"List accounts for a given party"` 29 Market AccountsMarketCmd `command:"market" description:"List accounts for a given market"` 30 Network AccountsNetworkCmd `command:"network" description:"List accounts owned by the network"` 31 Help bool `description:"Show this help message" long:"help" short:"h"` 32 } 33 34 type AccountsPartyCmd struct { 35 NodeAddress string `default:"0.0.0.0:3002" description:"The address of the vega node to use" long:"node-address"` 36 Market string `description:"An optional market" long:"market"` 37 Help bool `description:"Show this help message" long:"help" short:"h"` 38 } 39 40 type AccountsMarketCmd struct { 41 NodeAddress string `default:"0.0.0.0:3002" description:"The address of the vega node to use" long:"node-address"` 42 Help bool `description:"Show this help message" long:"help" short:"h"` 43 } 44 45 type AccountsNetworkCmd struct { 46 NodeAddress string `default:"0.0.0.0:3002" description:"The address of the vega node to use" long:"node-address"` 47 Help bool `description:"Show this help message" long:"help" short:"h"` 48 } 49 50 func (opts *AccountsPartyCmd) Execute(params []string) error { 51 if len(params) > 1 { 52 return errors.New("only one party needs to be specified") 53 } 54 55 if len(params) < 1 { 56 return errors.New("one party is required") 57 } 58 59 req := apipb.ListAccountsRequest{ 60 Party: params[0], 61 Market: opts.Market, // at most empty anyway 62 } 63 64 return getPrintAccounts(opts.NodeAddress, &req) 65 } 66 67 func (opts *AccountsMarketCmd) Execute(params []string) error { 68 if len(params) > 1 { 69 return errors.New("only one market needs to be specified") 70 } 71 72 return nil 73 } 74 75 func (opts *AccountsNetworkCmd) Execute(_ []string) error { 76 req := apipb.ListAccountsRequest{} 77 return getPrintAccounts(opts.NodeAddress, &req) 78 } 79 80 func getPrintAccounts(nodeAddress string, req *apipb.ListAccountsRequest) error { 81 clt, err := getClient(nodeAddress) 82 if err != nil { 83 return fmt.Errorf("could not connect to the vega node: %w", err) 84 } 85 86 ctx, cancel := timeoutContext() 87 defer cancel() 88 res, err := clt.ListAccounts(ctx, req) 89 if err != nil { 90 return fmt.Errorf("error querying the vega node: %w", err) 91 } 92 93 m := jsonpb.Marshaler{ 94 Indent: " ", 95 } 96 buf, err := m.MarshalToString(res) 97 if err != nil { 98 return fmt.Errorf("invalid response from vega node: %w", err) 99 } 100 101 fmt.Printf("%v", buf) 102 103 return nil 104 }