2018-09-04 23:41:17 -07:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/distribution/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// get the validator distribution info
|
|
|
|
func (k Keeper) GetValidatorDistInfo(ctx sdk.Context,
|
|
|
|
operatorAddr sdk.ValAddress) (vdi types.ValidatorDistInfo) {
|
|
|
|
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
|
|
|
|
2018-09-13 23:35:02 -07:00
|
|
|
b := store.Get(GetValidatorDistInfoKey(ctx, operatorAddr))
|
2018-09-04 23:41:17 -07:00
|
|
|
if b == nil {
|
|
|
|
panic("Stored delegation-distribution info should not have been nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
k.cdc.MustUnmarshalBinary(b, &vdi)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the validator distribution info
|
|
|
|
func (k Keeper) SetValidatorDistInfo(ctx sdk.Context, vdi types.ValidatorDistInfo) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
|
|
|
b := k.cdc.MustMarshalBinary(vdi)
|
2018-09-13 23:35:02 -07:00
|
|
|
store.Set(GetValidatorDistInfoKey(ctx, vdi.OperatorAddr), b)
|
2018-09-04 23:41:17 -07:00
|
|
|
}
|
|
|
|
|
2018-09-17 20:02:15 -07:00
|
|
|
// remove a validator distribution info
|
|
|
|
func (k Keeper) RemoveValidatorDistInfo(ctx sdk.Context, valAddr sdk.ValAddress) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
|
|
|
store.Delete(GetValidatorDistInfoKey(ctx, vdi.OperatorAddr))
|
|
|
|
}
|
|
|
|
|
|
|
|
// withdrawal all the validator rewards including the commission
|
2018-09-18 20:30:00 -07:00
|
|
|
func (k Keeper) WithdrawValidatorRewardsAll(ctx sdk.Context, operatorAddr sdk.ValAddress) {
|
2018-09-04 23:41:17 -07:00
|
|
|
|
|
|
|
// withdraw self-delegation
|
2018-09-10 15:37:58 -07:00
|
|
|
height := ctx.BlockHeight()
|
2018-09-13 23:35:02 -07:00
|
|
|
validator := k.GetValidator(ctx, operatorAddr)
|
2018-09-18 09:46:04 -07:00
|
|
|
accAddr := sdk.AccAddress{operatorAddr.Bytes()}
|
|
|
|
withdraw := k.GetDelegatorRewardsAll(ctx, accAddr, height)
|
2018-09-04 23:41:17 -07:00
|
|
|
|
|
|
|
// withdrawal validator commission rewards
|
2018-09-10 15:37:58 -07:00
|
|
|
pool := k.stakeKeeper.GetPool(ctx)
|
2018-09-13 23:35:02 -07:00
|
|
|
valInfo := k.GetValidatorDistInfo(ctx, operatorAddr)
|
2018-09-10 15:37:58 -07:00
|
|
|
feePool := k.GetFeePool(ctx)
|
|
|
|
feePool, commission := valInfo.WithdrawCommission(feePool, valInfo, height, pool.BondedTokens,
|
2018-09-04 23:41:17 -07:00
|
|
|
validator.Tokens, validator.Commission)
|
2018-09-10 15:37:58 -07:00
|
|
|
withdraw = withdraw.Add(commission)
|
|
|
|
k.SetFeePool(feePool)
|
2018-09-04 23:41:17 -07:00
|
|
|
|
2018-09-18 09:46:04 -07:00
|
|
|
withdrawAddr := k.GetDelegatorWithdrawAddr(accAddr)
|
2018-09-04 23:41:17 -07:00
|
|
|
k.coinKeeper.AddCoins(withdrawAddr, withdraw.TruncateDecimal())
|
|
|
|
}
|