-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
96 lines (81 loc) · 3.28 KB
/
common.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
package common
import (
"fmt"
"github.com/blockgenx/blockgen-sdk/client"
sdk "github.com/blockgenx/blockgen-sdk/types"
"github.com/blockgenx/blockgen-sdk/x/distribution/types"
)
// QueryDelegationRewards queries a delegation rewards between a delegator and a
// validator.
func QueryDelegationRewards(clientCtx client.Context, delAddr, valAddr string) ([]byte, int64, error) {
delegatorAddr, err := sdk.AccAddressFromBech32(delAddr)
if err != nil {
return nil, 0, err
}
validatorAddr, err := sdk.ValAddressFromBech32(valAddr)
if err != nil {
return nil, 0, err
}
params := types.NewQueryDelegationRewardsParams(delegatorAddr, validatorAddr)
bz, err := clientCtx.LegacyAmino.MarshalJSON(params)
if err != nil {
return nil, 0, fmt.Errorf("failed to marshal params: %w", err)
}
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryDelegationRewards)
return clientCtx.QueryWithData(route, bz)
}
// QueryDelegatorValidators returns delegator's list of validators
// it submitted delegations to.
func QueryDelegatorValidators(clientCtx client.Context, delegatorAddr sdk.AccAddress) ([]byte, error) {
res, _, err := clientCtx.QueryWithData(
fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryDelegatorValidators),
clientCtx.LegacyAmino.MustMarshalJSON(types.NewQueryDelegatorParams(delegatorAddr)),
)
return res, err
}
// QueryValidatorCommission returns a validator's commission.
func QueryValidatorCommission(clientCtx client.Context, validatorAddr sdk.ValAddress) ([]byte, error) {
res, _, err := clientCtx.QueryWithData(
fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryValidatorCommission),
clientCtx.LegacyAmino.MustMarshalJSON(types.NewQueryValidatorCommissionParams(validatorAddr)),
)
return res, err
}
// WithdrawAllDelegatorRewards builds a multi-message slice to be used
// to withdraw all delegations rewards for the given delegator.
func WithdrawAllDelegatorRewards(clientCtx client.Context, delegatorAddr sdk.AccAddress) ([]sdk.Msg, error) {
// retrieve the comprehensive list of all validators which the
// delegator had submitted delegations to
bz, err := QueryDelegatorValidators(clientCtx, delegatorAddr)
if err != nil {
return nil, err
}
var validators []sdk.ValAddress
if err := clientCtx.LegacyAmino.UnmarshalJSON(bz, &validators); err != nil {
return nil, err
}
// build multi-message transaction
msgs := make([]sdk.Msg, 0, len(validators))
for _, valAddr := range validators {
msg := types.NewMsgWithdrawDelegatorReward(delegatorAddr, valAddr)
if err := msg.ValidateBasic(); err != nil {
return nil, err
}
msgs = append(msgs, msg)
}
return msgs, nil
}
// WithdrawValidatorRewardsAndCommission builds a two-message message slice to be
// used to withdraw both validation's commission and self-delegation reward.
func WithdrawValidatorRewardsAndCommission(validatorAddr sdk.ValAddress) ([]sdk.Msg, error) {
commissionMsg := types.NewMsgWithdrawValidatorCommission(validatorAddr)
if err := commissionMsg.ValidateBasic(); err != nil {
return nil, err
}
// build and validate MsgWithdrawDelegatorReward
rewardMsg := types.NewMsgWithdrawDelegatorReward(sdk.AccAddress(validatorAddr.Bytes()), validatorAddr)
if err := rewardMsg.ValidateBasic(); err != nil {
return nil, err
}
return []sdk.Msg{commissionMsg, rewardMsg}, nil
}