-
Notifications
You must be signed in to change notification settings - Fork 199
/
query_account_info.go
148 lines (132 loc) · 4.84 KB
/
query_account_info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package cli
import (
"context"
"strconv"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/lavanet/lava/utils"
"github.com/lavanet/lava/utils/sigs"
dualstakingtypes "github.com/lavanet/lava/x/dualstaking/types"
epochstoragetypes "github.com/lavanet/lava/x/epochstorage/types"
"github.com/lavanet/lava/x/pairing/types"
projecttypes "github.com/lavanet/lava/x/projects/types"
spectypes "github.com/lavanet/lava/x/spec/types"
subscriptiontypes "github.com/lavanet/lava/x/subscription/types"
"github.com/spf13/cobra"
)
var _ = strconv.Itoa(0)
func CmdAccountInfo() *cobra.Command {
cmd := &cobra.Command{
Use: "account-info {[lava_address] | --from wallet}",
Short: "Query account information on an address",
Args: cobra.RangeArgs(0, 1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
var address string
if len(args) == 0 {
clientCtxForTx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
keyName, err := sigs.GetKeyName(clientCtxForTx)
if err != nil {
utils.LavaFormatFatal("failed getting key name from clientCtx", err)
}
clientKey, err := clientCtxForTx.Keyring.Key(keyName)
if err != nil {
return err
}
addressAccount, err := clientKey.GetAddress()
if err != nil {
return err
}
address = addressAccount.String()
} else {
address = args[0]
}
specQuerier := spectypes.NewQueryClient(clientCtx)
ctx := context.Background()
allChains, err := specQuerier.ShowAllChains(ctx, &spectypes.QueryShowAllChainsRequest{})
if err != nil {
return utils.LavaFormatError("failed getting key name from clientCtx, either provide the address in an argument or verify the --from wallet exists", err)
}
pairingQuerier := types.NewQueryClient(clientCtx)
subscriptionQuerier := subscriptiontypes.NewQueryClient(clientCtx)
projectQuerier := projecttypes.NewQueryClient(clientCtx)
epochStorageQuerier := epochstoragetypes.NewQueryClient(clientCtx)
dualstakingQuerier := dualstakingtypes.NewQueryClient(clientCtx)
stakingQuerier := stakingtypes.NewQueryClient(clientCtx)
resultStatus, err := clientCtx.Client.Status(ctx)
if err != nil {
return err
}
currentBlock := resultStatus.SyncInfo.LatestBlockHeight
// gather information for printing
var info types.QueryAccountInfoResponse
// fill the objects
for _, chainStructInfo := range allChains.ChainInfoList {
chainID := chainStructInfo.ChainID
response, err := pairingQuerier.Providers(ctx, &types.QueryProvidersRequest{
ChainID: chainID,
ShowFrozen: true,
})
if err == nil && len(response.StakeEntry) > 0 {
for _, provider := range response.StakeEntry {
if provider.Address == address {
if provider.StakeAppliedBlock > uint64(currentBlock) {
info.Frozen = append(info.Frozen, provider)
} else {
info.Provider = append(info.Provider, provider)
}
break
}
}
}
}
unstakeEntriesAllChains, err := epochStorageQuerier.StakeStorage(ctx, &epochstoragetypes.QueryGetStakeStorageRequest{
Index: epochstoragetypes.StakeStorageKeyUnstakeConst,
})
if err == nil {
if len(unstakeEntriesAllChains.StakeStorage.StakeEntries) > 0 {
for _, unstakingProvider := range unstakeEntriesAllChains.StakeStorage.StakeEntries {
if unstakingProvider.Address == address {
info.Unstaked = append(info.Unstaked, unstakingProvider)
}
}
}
}
response, err := subscriptionQuerier.Current(cmd.Context(), &subscriptiontypes.QueryCurrentRequest{
Consumer: address,
})
if err == nil {
info.Subscription = response.Sub
}
developer, err := projectQuerier.Developer(cmd.Context(), &projecttypes.QueryDeveloperRequest{Developer: address})
if err == nil {
info.Project = developer.Project
}
providers, err := dualstakingQuerier.DelegatorProviders(cmd.Context(), &dualstakingtypes.QueryDelegatorProvidersRequest{Delegator: address, WithPending: true})
if err == nil {
info.DelegationsProviders = providers.Delegations
}
var totalDelegations uint64
for _, p := range providers.Delegations {
totalDelegations += p.Amount.Amount.Uint64()
}
info.TotalDelegations = totalDelegations
validators, err := stakingQuerier.DelegatorDelegations(ctx, &stakingtypes.QueryDelegatorDelegationsRequest{DelegatorAddr: address})
if err == nil {
info.DelegationsValidators = validators.DelegationResponses
}
// we finished gathering information, now print it
return clientCtx.PrintProto(&info)
},
}
cmd.Flags().String(flags.FlagFrom, "", "Name or address of private key with which to sign")
flags.AddQueryFlagsToCmd(cmd)
return cmd
}