-
Notifications
You must be signed in to change notification settings - Fork 4
/
hooks.go
62 lines (51 loc) · 2.05 KB
/
hooks.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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/zenchainprotocol/zenchain-node/x/distribution/types"
stakingtypes "github.com/zenchainprotocol/zenchain-node/x/staking/types"
)
// Hooks is struct of keepers from other module
type Hooks struct {
k Keeper
}
var _ stakingtypes.StakingHooks = Hooks{}
// Hooks creates new distribution hooks
func (k Keeper) Hooks() Hooks { return Hooks{k} }
// AfterValidatorCreated initializes validator distribution record
func (h Hooks) AfterValidatorCreated(ctx sdk.Context, valAddr sdk.ValAddress) {
val := h.k.stakingKeeper.Validator(ctx, valAddr)
h.k.initializeValidator(ctx, val)
}
// AfterValidatorRemoved cleans up for after validator is removed
func (h Hooks) AfterValidatorRemoved(ctx sdk.Context, _ sdk.ConsAddress, valAddr sdk.ValAddress) {
// force-withdraw commission
commission := h.k.GetValidatorAccumulatedCommission(ctx, valAddr)
if !commission.IsZero() {
// split into integral & remainder
coins, remainder := commission.TruncateDecimal()
// remainder to community pool
if !remainder.IsZero() {
feePool := h.k.GetFeePool(ctx)
feePool.CommunityPool = feePool.CommunityPool.Add(remainder...)
h.k.SetFeePool(ctx, feePool)
}
// add to validator account
if !coins.IsZero() {
accAddr := sdk.AccAddress(valAddr)
withdrawAddr := h.k.GetDelegatorWithdrawAddr(ctx, accAddr)
err := h.k.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, withdrawAddr, coins)
if err != nil {
panic(err)
}
}
}
// remove commission record
h.k.deleteValidatorAccumulatedCommission(ctx, valAddr)
}
// AfterValidatorDestroyed nothing to do
func (h Hooks) AfterValidatorDestroyed(ctx sdk.Context, consAddr sdk.ConsAddress, valAddr sdk.ValAddress) {
}
// nolint - unused hooks
func (h Hooks) BeforeValidatorModified(_ sdk.Context, _ sdk.ValAddress) {}
func (h Hooks) AfterValidatorBonded(_ sdk.Context, _ sdk.ConsAddress, _ sdk.ValAddress) {}
func (h Hooks) AfterValidatorBeginUnbonding(_ sdk.Context, _ sdk.ConsAddress, _ sdk.ValAddress) {}