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

149 lines
5.4 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"
)
// check whether a delegator distribution info exists
func (k Keeper) HasDelegationDistInfo(ctx sdk.Context, delAddr sdk.AccAddress,
valOperatorAddr sdk.ValAddress) (has bool) {
store := ctx.KVStore(k.storeKey)
return store.Has(GetDelegationDistInfoKey(delAddr, valOperatorAddr))
}
2018-09-04 23:41:17 -07:00
// get the delegator distribution info
2018-10-05 17:32:06 -07:00
func (k Keeper) GetDelegationDistInfo(ctx sdk.Context, delAddr sdk.AccAddress,
valOperatorAddr sdk.ValAddress) (ddi types.DelegationDistInfo) {
2018-09-04 23:41:17 -07:00
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
2018-10-05 17:32:06 -07:00
func (k Keeper) SetDelegationDistInfo(ctx sdk.Context, ddi types.DelegationDistInfo) {
2018-09-04 23:41:17 -07:00
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
2018-10-05 17:32:06 -07:00
func (k Keeper) RemoveDelegationDistInfo(ctx sdk.Context, delAddr sdk.AccAddress,
2018-09-17 20:02:15 -07:00
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) sdk.Error {
if !k.HasDelegationDistInfo(ctx, delegatorAddr, validatorAddr) {
return types.ErrNoDelegationDistInfo(k.codespace)
}
2018-09-10 15:37:58 -07:00
// TODO: Reconcile with duplicate code in getDelegatorRewardsAll.
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-10-05 17:32:06 -07:00
delInfo := k.GetDelegationDistInfo(ctx, delegatorAddr, validatorAddr)
2018-09-13 23:35:02 -07:00
valInfo := k.GetValidatorDistInfo(ctx, validatorAddr)
2018-09-19 21:53:48 -07:00
validator := k.stakeKeeper.Validator(ctx, validatorAddr)
delegation := k.stakeKeeper.Delegation(ctx, delegatorAddr, validatorAddr)
2018-09-10 15:37:58 -07:00
2018-10-04 00:26:02 -07:00
delInfo, valInfo, feePool, withdraw := delInfo.WithdrawRewards(feePool, valInfo, height, bondedTokens,
2018-10-19 14:41:39 -07:00
validator.GetPower(), validator.GetDelegatorShares(), delegation.GetShares(), validator.GetCommission())
2018-09-04 23:41:17 -07:00
2018-10-04 00:26:02 -07:00
k.SetValidatorDistInfo(ctx, valInfo)
k.SetDelegationDistInfo(ctx, delInfo)
2018-09-19 19:13:12 -07:00
withdrawAddr := k.GetDelegatorWithdrawAddr(ctx, delegatorAddr)
coinsToAdd, change := withdraw.TruncateDecimal()
feePool.CommunityPool = feePool.CommunityPool.Plus(change)
k.SetFeePool(ctx, feePool)
_, _, err := k.bankKeeper.AddCoins(ctx, withdrawAddr, coinsToAdd)
2018-09-28 01:02:07 -07:00
if err != nil {
panic(err)
}
return nil
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-10-12 01:11:09 -07:00
withdraw := k.getDelegatorRewardsAll(ctx, delegatorAddr, height)
feePool := k.GetFeePool(ctx)
withdrawAddr := k.GetDelegatorWithdrawAddr(ctx, delegatorAddr)
coinsToAdd, change := withdraw.TruncateDecimal()
feePool.CommunityPool = feePool.CommunityPool.Plus(change)
k.SetFeePool(ctx, feePool)
_, _, err := k.bankKeeper.AddCoins(ctx, withdrawAddr, coinsToAdd)
2018-09-28 01:02:07 -07:00
if err != nil {
panic(err)
}
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-10-12 01:11:09 -07:00
func (k Keeper) getDelegatorRewardsAll(ctx sdk.Context, delAddr sdk.AccAddress, height int64) types.DecCoins {
2018-09-04 23:41:17 -07:00
withdraw := types.DecCoins{}
bondedTokens := k.stakeKeeper.TotalPower(ctx)
2018-09-04 23:41:17 -07:00
// iterate over all the delegations
// TODO: Reconcile with duplicate code in WithdrawDelegationReward.
operationAtDelegation := func(_ int64, del sdk.Delegation) (stop bool) {
2018-10-19 11:58:37 -07:00
feePool := k.GetFeePool(ctx)
valAddr := del.GetValidator()
2018-10-05 17:32:06 -07:00
delInfo := k.GetDelegationDistInfo(ctx, delAddr, valAddr)
valInfo := k.GetValidatorDistInfo(ctx, valAddr)
2018-09-19 21:53:48 -07:00
validator := k.stakeKeeper.Validator(ctx, valAddr)
delegation := k.stakeKeeper.Delegation(ctx, delAddr, valAddr)
2018-10-04 00:26:02 -07:00
delInfo, valInfo, feePool, diWithdraw := delInfo.WithdrawRewards(feePool, valInfo, height, bondedTokens,
2018-10-19 14:41:39 -07:00
validator.GetPower(), validator.GetDelegatorShares(), delegation.GetShares(), validator.GetCommission())
withdraw = withdraw.Plus(diWithdraw)
k.SetFeePool(ctx, feePool)
2018-10-04 00:26:02 -07:00
k.SetValidatorDistInfo(ctx, valInfo)
2018-10-05 17:32:06 -07:00
k.SetDelegationDistInfo(ctx, delInfo)
2018-09-04 23:41:17 -07:00
return false
}
k.stakeKeeper.IterateDelegations(ctx, delAddr, operationAtDelegation)
return withdraw
}