cosmos-sdk/x/distribution/keeper/delegation.go

118 lines
4.2 KiB
Go
Raw Normal View History

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 delegator distribution info
func (k Keeper) GetDelegatorDistInfo(ctx sdk.Context, delAddr sdk.AccAddress,
valOperatorAddr sdk.ValAddress) (ddi types.DelegatorDistInfo) {
store := ctx.KVStore(k.storeKey)
b := store.Get(GetDelegationDistInfoKey(delAddr, valOperatorAddr))
if b == nil {
panic("Stored delegation-distribution info should not have been nil")
}
k.cdc.MustUnmarshalBinary(b, &ddi)
return
}
// set the delegator distribution info
func (k Keeper) SetDelegatorDistInfo(ctx sdk.Context, ddi types.DelegatorDistInfo) {
store := ctx.KVStore(k.storeKey)
b := k.cdc.MustMarshalBinary(ddi)
store.Set(GetDelegationDistInfoKey(ddi.DelegatorAddr, ddi.ValOperatorAddr), b)
}
2018-09-17 20:02:15 -07:00
// remove a delegator distribution info
func (k Keeper) RemoveDelegatorDistInfo(ctx sdk.Context, delAddr sdk.AccAddress,
valOperatorAddr sdk.ValAddress) {
store := ctx.KVStore(k.storeKey)
2018-09-19 19:13:12 -07:00
store.Delete(GetDelegationDistInfoKey(delAddr, valOperatorAddr))
2018-09-17 20:02:15 -07:00
}
//___________________________________________________________________________________________
// get the delegator withdraw address, return the delegator address if not set
func (k Keeper) GetDelegatorWithdrawAddr(ctx sdk.Context, delAddr sdk.AccAddress) sdk.AccAddress {
store := ctx.KVStore(k.storeKey)
b := store.Get(GetDelegatorWithdrawAddrKey(delAddr))
if b == nil {
return delAddr
}
2018-09-19 19:13:12 -07:00
return sdk.AccAddress(b)
2018-09-17 20:02:15 -07:00
}
// set the delegator withdraw address
func (k Keeper) SetDelegatorWithdrawAddr(ctx sdk.Context, delAddr, withdrawAddr sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
2018-09-19 19:13:12 -07:00
store.Set(GetDelegatorWithdrawAddrKey(delAddr), withdrawAddr.Bytes())
2018-09-17 20:02:15 -07:00
}
// remove a delegator withdraw info
func (k Keeper) RemoveDelegatorWithdrawAddr(ctx sdk.Context, delAddr, withdrawAddr sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
store.Delete(GetDelegatorWithdrawAddrKey(delAddr))
}
2018-09-04 23:41:17 -07:00
//___________________________________________________________________________________________
2018-09-10 15:37:58 -07:00
// withdraw all the rewards for a single delegation
2018-09-17 20:02:15 -07:00
func (k Keeper) WithdrawDelegationReward(ctx sdk.Context, delegatorAddr sdk.AccAddress,
validatorAddr sdk.ValAddress) {
2018-09-10 15:37:58 -07:00
height := ctx.BlockHeight()
2018-09-19 19:13:12 -07:00
bondedTokens := k.stakeKeeper.TotalPower(ctx)
2018-09-13 23:35:02 -07:00
feePool := k.GetFeePool(ctx)
2018-09-19 19:13:12 -07:00
delInfo := k.GetDelegatorDistInfo(ctx, delegatorAddr, validatorAddr)
2018-09-13 23:35:02 -07:00
valInfo := k.GetValidatorDistInfo(ctx, validatorAddr)
2018-09-19 19:13:12 -07:00
validator := k.stakeKeeper.GetValidator(ctx, validatorAddr)
2018-09-10 15:37:58 -07:00
2018-09-19 19:13:12 -07:00
delInfo, feePool, withdraw := delInfo.WithdrawRewards(ctx, feePool, valInfo, height, bondedTokens,
2018-09-04 23:41:17 -07:00
validator.Tokens, validator.DelegatorShares, validator.Commission)
2018-09-13 23:35:02 -07:00
k.SetFeePool(ctx, feePool)
2018-09-19 19:13:12 -07:00
withdrawAddr := k.GetDelegatorWithdrawAddr(ctx, delegatorAddr)
k.bankKeeper.AddCoins(ctx, withdrawAddr, withdraw.TruncateDecimal())
2018-09-04 23:41:17 -07:00
}
2018-09-17 20:02:15 -07:00
//___________________________________________________________________________________________
2018-09-04 23:41:17 -07:00
2018-09-10 15:37:58 -07:00
// return all rewards for all delegations of a delegator
2018-09-17 20:02:15 -07:00
func (k Keeper) WithdrawDelegationRewardsAll(ctx sdk.Context, delegatorAddr sdk.AccAddress) {
2018-09-10 15:37:58 -07:00
height := ctx.BlockHeight()
2018-09-17 20:02:15 -07:00
withdraw = k.GetDelegatorRewardsAll(ctx, delegatorAddr, height)
withdrawAddr := k.GetDelegatorWithdrawAddr(delegatorAddr)
2018-09-04 23:41:17 -07:00
k.coinsKeeper.AddCoins(withdrawAddr, withdraw.Amount.TruncateDecimal())
}
2018-09-10 15:37:58 -07:00
// return all rewards for all delegations of a delegator
2018-09-19 19:13:12 -07:00
func (k Keeper) GetDelegatorRewardsAll(ctx sdk.Context, delAddr sdk.AccAddress, height int64) types.DecCoins {
2018-09-04 23:41:17 -07:00
2018-09-10 15:37:58 -07:00
withdraw := sdk.NewDec(0)
2018-09-13 23:35:02 -07:00
pool := k.sk.GetPool(ctx)
feePool := k.GetFeePool(ctx)
2018-09-04 23:41:17 -07:00
// iterate over all the delegations
operationAtDelegation := func(_ int64, del types.Delegation) (stop bool) {
2018-09-13 23:35:02 -07:00
delInfo := k.GetDelegationDistInfo(ctx, delAddr, del.ValidatorAddr)
valInfo := k.GetValidatorDistInfo(ctx, del.ValidatorAddr)
validator := k.sk.GetValidator(ctx, del.ValidatorAddr)
2018-09-04 23:41:17 -07:00
2018-09-10 15:37:58 -07:00
feePool, diWithdraw := delInfo.WithdrawRewards(feePool, valInfo, height, pool.BondedTokens,
2018-09-04 23:41:17 -07:00
validator.Tokens, validator.DelegatorShares, validator.Commission)
2018-09-10 15:37:58 -07:00
withdraw = withdraw.Add(diWithdraw)
SetFeePool(feePool)
2018-09-04 23:41:17 -07:00
return false
}
k.stakeKeeper.IterateDelegations(ctx, delAddr, operationAtDelegation)
2018-09-13 23:35:02 -07:00
k.SetFeePool(ctx, feePool)
2018-09-04 23:41:17 -07:00
return withdraw
}