@@ -2,6 +2,7 @@ package show
22
33import (
44 "context"
5+ "encoding/json"
56 "fmt"
67 "os"
78 "strings"
5859 nativeAddr , ethAddr , err := common .ResolveLocalAccountOrAddress (npa .Network , targetAddress )
5960 cobra .CheckErr (err )
6061
62+ jsonOut := map [string ]interface {}{}
6163 if name := common .FindAccountName (nativeAddr .String ()); name != "" {
62- fmt .Printf ("Name: %s\n " , name )
64+ if common .OutputFormat () == common .FormatJSON {
65+ jsonOut ["name" ] = name
66+ } else {
67+ fmt .Printf ("Name: %s\n " , name )
68+ }
6369 }
6470
6571 height , err := common .GetActualHeight (
@@ -78,15 +84,22 @@ var (
7884
7985 consensusAccount , err := c .Consensus ().Staking ().Account (ctx , ownerQuery )
8086 cobra .CheckErr (err )
81- if ethAddr != nil {
82- fmt .Printf ("Ethereum address: %s\n " , ethAddr )
83- }
84- fmt .Printf ("Native address: %s\n " , nativeAddr )
85- fmt .Println ()
86- fmt .Printf ("=== CONSENSUS LAYER (%s) ===\n " , npa .NetworkName )
87- fmt .Printf (" Nonce: %d\n " , consensusAccount .General .Nonce )
88- fmt .Println ()
8987
88+ if common .OutputFormat () == common .FormatJSON {
89+ jsonOut ["ethereum_address" ] = ethAddr
90+ jsonOut ["native_address" ] = nativeAddr
91+ jsonOut ["nonce" ] = consensusAccount .General .Nonce
92+ jsonOut ["network_name" ] = npa .NetworkName
93+ } else {
94+ if ethAddr != nil {
95+ fmt .Printf ("Ethereum address: %s\n " , ethAddr )
96+ }
97+ fmt .Printf ("Native address: %s\n " , nativeAddr )
98+ fmt .Println ()
99+ fmt .Printf ("=== CONSENSUS LAYER (%s) ===\n " , npa .NetworkName )
100+ fmt .Printf (" Nonce: %d\n " , consensusAccount .General .Nonce )
101+ fmt .Println ()
102+ }
90103 var (
91104 outgoingDelegations map [staking.Address ]* staking.DelegationInfo
92105 outgoingDebondingDelegations map [staking.Address ][]* staking.DebondingDelegationInfo
@@ -98,17 +111,23 @@ var (
98111 cobra .CheckErr (err )
99112 }
100113
101- prettyPrintAccountBalanceAndDelegationsFrom (
102- npa .Network ,
103- nativeAddr ,
104- consensusAccount .General ,
105- outgoingDelegations ,
106- outgoingDebondingDelegations ,
107- " " ,
108- os .Stdout ,
109- )
114+ if common .OutputFormat () == common .FormatJSON {
115+ jsonOut ["general" ] = & consensusAccount .General
116+ jsonOut ["outgoing_delegations" ] = outgoingDelegations
117+ jsonOut ["outgoing_debonding_delegations" ] = outgoingDebondingDelegations
118+ } else {
119+ prettyPrintAccountBalanceAndDelegationsFrom (
120+ npa .Network ,
121+ nativeAddr ,
122+ consensusAccount .General ,
123+ outgoingDelegations ,
124+ outgoingDebondingDelegations ,
125+ " " ,
126+ os .Stdout ,
127+ )
128+ }
110129
111- if len (consensusAccount .General .Allowances ) > 0 {
130+ if len (consensusAccount .General .Allowances ) > 0 && common . OutputFormat () == common . FormatText {
112131 fmt .Println (" Allowances for this Account:" )
113132 prettyPrintAllowances (
114133 npa .Network ,
@@ -127,40 +146,48 @@ var (
127146 cobra .CheckErr (err )
128147
129148 if len (incomingDelegations ) > 0 {
130- fmt .Println (" Active Delegations to this Account:" )
131- prettyPrintDelegationsTo (
132- npa .Network ,
133- nativeAddr ,
134- consensusAccount .Escrow .Active ,
135- incomingDelegations ,
136- " " ,
137- os .Stdout ,
138- )
139- fmt .Println ()
149+ if common .OutputFormat () == common .FormatJSON {
150+ jsonOut ["incoming_delegations" ] = incomingDelegations
151+ } else {
152+ fmt .Println (" Active Delegations to this Account:" )
153+ prettyPrintDelegationsTo (
154+ npa .Network ,
155+ nativeAddr ,
156+ consensusAccount .Escrow .Active ,
157+ incomingDelegations ,
158+ " " ,
159+ os .Stdout ,
160+ )
161+ fmt .Println ()
162+ }
140163 }
141164 if len (incomingDebondingDelegations ) > 0 {
142- fmt .Println (" Debonding Delegations to this Account:" )
143- prettyPrintDelegationsTo (
144- npa .Network ,
145- nativeAddr ,
146- consensusAccount .Escrow .Debonding ,
147- incomingDebondingDelegations ,
148- " " ,
149- os .Stdout ,
150- )
151- fmt .Println ()
165+ if common .OutputFormat () == common .FormatJSON {
166+ jsonOut ["incoming_debonding_delegations" ] = incomingDebondingDelegations
167+ } else {
168+ fmt .Println (" Debonding Delegations to this Account:" )
169+ prettyPrintDelegationsTo (
170+ npa .Network ,
171+ nativeAddr ,
172+ consensusAccount .Escrow .Debonding ,
173+ incomingDebondingDelegations ,
174+ " " ,
175+ os .Stdout ,
176+ )
177+ fmt .Println ()
178+ }
152179 }
153180 }
154181
155182 cs := consensusAccount .Escrow .CommissionSchedule
156- if len (cs .Rates ) > 0 || len (cs .Bounds ) > 0 {
183+ if ( len (cs .Rates ) > 0 || len (cs .Bounds ) > 0 ) && common . OutputFormat () == common . FormatText {
157184 fmt .Println (" Commission Schedule:" )
158185 cs .PrettyPrint (ctx , " " , os .Stdout )
159186 fmt .Println ()
160187 }
161188
162189 sa := consensusAccount .Escrow .StakeAccumulator
163- if len (sa .Claims ) > 0 {
190+ if len (sa .Claims ) > 0 && common . OutputFormat () == common . FormatText {
164191 fmt .Println (" Stake Accumulator:" )
165192 sa .PrettyPrint (ctx , " " , os .Stdout )
166193 fmt .Println ()
@@ -199,21 +226,30 @@ var (
199226 hasNonZeroNonce := nonce > 0
200227
201228 if hasNonZeroBalance || hasNonZeroNonce {
202- fmt .Printf ("=== %s PARATIME ===\n " , npa .ParaTimeName )
203- fmt .Printf (" Nonce: %d\n " , nonce )
204- fmt .Println ()
229+ if common .OutputFormat () == common .FormatJSON {
230+ jsonOut ["paratime_name" ] = npa .ParaTimeName
231+ jsonOut ["paratime_nonce" ] = nonce
232+ } else {
233+ fmt .Printf ("=== %s PARATIME ===\n " , npa .ParaTimeName )
234+ fmt .Printf (" Nonce: %d\n " , nonce )
235+ fmt .Println ()
236+ }
205237
206238 if hasNonZeroBalance {
207- fmt .Printf (" Balances for all denominations:\n " )
208- for denom , balance := range rtBalances .Balances {
209- fmtAmnt := helpers .FormatParaTimeDenomination (npa .ParaTime , types .NewBaseUnits (balance , denom ))
210- amnt , symbol , _ := strings .Cut (fmtAmnt , " " )
239+ if common .OutputFormat () == common .FormatJSON {
240+ jsonOut ["paratime_balances" ] = rtBalances .Balances
241+ } else {
242+ fmt .Printf (" Balances for all denominations:\n " )
243+ for denom , balance := range rtBalances .Balances {
244+ fmtAmnt := helpers .FormatParaTimeDenomination (npa .ParaTime , types .NewBaseUnits (balance , denom ))
245+ amnt , symbol , _ := strings .Cut (fmtAmnt , " " )
211246
212- fmt .Printf (" - Amount: %s\n " , amnt )
213- fmt .Printf (" Symbol: %s\n " , symbol )
214- }
247+ fmt .Printf (" - Amount: %s\n " , amnt )
248+ fmt .Printf (" Symbol: %s\n " , symbol )
249+ }
215250
216- fmt .Println ()
251+ fmt .Println ()
252+ }
217253 }
218254
219255 if showDelegations {
@@ -231,10 +267,21 @@ var (
231267 To : * nativeAddr ,
232268 },
233269 )
234- prettyPrintParaTimeDelegations (ctx , c , height , npa , nativeAddr , rtDelegations , rtUndelegations , " " , os .Stdout )
270+ if common .OutputFormat () == common .FormatJSON {
271+ jsonOut ["paratime_delegations" ] = rtDelegations
272+ jsonOut ["paratime_undelegations" ] = rtUndelegations
273+ } else {
274+ prettyPrintParaTimeDelegations (ctx , c , height , npa , nativeAddr , rtDelegations , rtUndelegations , " " , os .Stdout )
275+ }
235276 }
236277 }
237278 }
279+
280+ if common .OutputFormat () == common .FormatJSON {
281+ data , err := json .MarshalIndent (jsonOut , "" , " " )
282+ cobra .CheckErr (err )
283+ fmt .Printf ("%s\n " , data )
284+ }
238285 },
239286 }
240287)
@@ -244,5 +291,6 @@ func init() {
244291 f .BoolVar (& showDelegations , "show-delegations" , false , "show incoming and outgoing delegations" )
245292 common .AddSelectorFlags (Cmd )
246293 Cmd .Flags ().AddFlagSet (common .HeightFlag )
294+ Cmd .Flags ().AddFlagSet (common .FormatFlag )
247295 Cmd .Flags ().AddFlagSet (f )
248296}
0 commit comments