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

84 lines
2.8 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"
"github.com/cosmos/cosmos-sdk/x/stake"
)
// 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-10 15:37:58 -07:00
// withdraw all the rewards for a single delegation
func (k Keeper) WithdrawDelegationReward(ctx sdk.Context, delegatorAddr,
withdrawAddr sdk.AccAddress, validatorAddr sdk.ValAddress) {
height := ctx.BlockHeight()
pool := stake.GetPool()
feePool := GetFeePool()
delInfo := GetDelegationDistInfo(delegatorAddr, validatorAddr)
valInfo := GetValidatorDistInfo(validatorAddr)
validator := GetValidator(validatorAddr)
feePool, withdraw := delInfo.WithdrawRewards(feePool, valInfo, height, pool.BondedTokens,
2018-09-04 23:41:17 -07:00
validator.Tokens, validator.DelegatorShares, validator.Commission)
SetFeePool(feePool)
AddCoins(withdrawAddr, withdraw.TruncateDecimal())
}
///////////////////////////////////////////////////////////////////////////////////////
2018-09-10 15:37:58 -07:00
// return all rewards for all delegations of a delegator
2018-09-04 23:41:17 -07:00
func (k Keeper) WithdrawDelegationRewardsAll(ctx sdk.Context, delegatorAddr, withdrawAddr sdk.AccAddress) {
2018-09-10 15:37:58 -07:00
height := ctx.BlockHeight()
withdraw = GetDelegatorRewardsAll(ctx, delegatorAddr, height)
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-04 23:41:17 -07:00
func (k Keeper) GetDelegatorRewardsAll(ctx sdk.Context, delAddr sdk.AccAddress, height int64) DecCoins {
2018-09-10 15:37:58 -07:00
withdraw := sdk.NewDec(0)
pool := stake.GetPool()
feePool := GetFeePool()
2018-09-04 23:41:17 -07:00
// iterate over all the delegations
operationAtDelegation := func(_ int64, del types.Delegation) (stop bool) {
2018-09-10 15:37:58 -07:00
delInfo := GetDelegationDistInfo(delAddr, del.ValidatorAddr)
valInfo := GetValidatorDistInfo(del.ValidatorAddr)
validator := GetValidator(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)
SetFeePool(feePool)
return withdraw
}