Skip to content

Commit

Permalink
Merge pull request #2 from fbsobreira/fix/rewards-allowance
Browse files Browse the repository at this point in the history
Fix/rewards allowance
  • Loading branch information
fbsobreira authored Sep 1, 2020
2 parents 3babdbc + 4175a18 commit 0f55063
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
8 changes: 7 additions & 1 deletion cmd/subcommands/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,17 @@ func accountSub() []*cobra.Command {
return nil
}

rewards, err := conn.GetRewardsInfo(addr.String())
if err != nil {
return err
}

result := make(map[string]interface{})
result["address"] = addr.String()
result["type"] = acc.GetType()
result["balance"] = float64(acc.GetBalance()) / 1000000
result["allowance"] = float64(acc.GetAllowance()) / 1000000
result["allowance"] = float64(acc.GetAllowance()+rewards) / 1000000
result["rewards"] = float64(acc.GetAllowance()) / 1000000
asJSON, _ := json.Marshal(result)
fmt.Println(common.JSONPrettyFormat(string(asJSON)))
return nil
Expand Down
24 changes: 23 additions & 1 deletion pkg/client/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ func (g *GrpcClient) GetAccount(addr string) (*core.Account, error) {
return acc, nil
}

// GetRewardsInfo from BASE58 address
func (g *GrpcClient) GetRewardsInfo(addr string) (int64, error) {
addrBytes, err := common.DecodeCheck(addr)
if err != nil {
return 0, err
}

ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

rewards, err := g.Client.GetRewardInfo(ctx, GetMessageBytes(addrBytes))
if err != nil {
return 0, err
}
return rewards.Num, nil
}

// GetAccountNet return account resources from BASE58 address
func (g *GrpcClient) GetAccountNet(addr string) (*api.AccountNetMessage, error) {
account := new(core.Account)
Expand Down Expand Up @@ -123,6 +140,11 @@ func (g *GrpcClient) GetAccountDetailed(addr string) (*account.Account, error) {
return nil, err
}

rewards, err := g.GetRewardsInfo(addr)
if err != nil {
return nil, err
}

// SUM Total freeze
totalFrozen := int64(0)
frozenList := make([]account.FrozenResource, 0)
Expand Down Expand Up @@ -183,7 +205,7 @@ func (g *GrpcClient) GetAccountDetailed(addr string) (*account.Account, error) {
Name: string(acc.GetAccountName()),
ID: string(acc.GetAccountId()),
Balance: acc.GetBalance(),
Allowance: acc.GetAllowance(),
Allowance: acc.GetAllowance() + rewards,
LastWithdraw: acc.LatestWithdrawTime,
IsWitness: acc.IsWitness,
IsElected: acc.IsCommittee,
Expand Down

0 comments on commit 0f55063

Please sign in to comment.