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"
|
|
|
|
)
|
|
|
|
|
2018-10-18 12:58:57 -07:00
|
|
|
// 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-10-26 04:42:53 -07:00
|
|
|
// return all rewards for a delegation
|
|
|
|
func (k Keeper) withdrawDelegationReward(ctx sdk.Context,
|
|
|
|
delAddr sdk.AccAddress, valAddr sdk.ValAddress) (types.FeePool,
|
|
|
|
types.ValidatorDistInfo, types.DelegationDistInfo, types.DecCoins) {
|
2018-10-18 12:58:57 -07:00
|
|
|
|
2018-10-26 04:42:53 -07:00
|
|
|
wc := k.GetWithdrawContext(ctx, valAddr)
|
|
|
|
valInfo := k.GetValidatorDistInfo(ctx, valAddr)
|
|
|
|
delInfo := k.GetDelegationDistInfo(ctx, delAddr, valAddr)
|
|
|
|
validator := k.stakeKeeper.Validator(ctx, valAddr)
|
|
|
|
delegation := k.stakeKeeper.Delegation(ctx, delAddr, valAddr)
|
|
|
|
|
|
|
|
delInfo, valInfo, feePool, withdraw := delInfo.WithdrawRewards(wc, valInfo,
|
|
|
|
validator.GetDelegatorShares(), delegation.GetShares())
|
|
|
|
|
|
|
|
return feePool, valInfo, delInfo, withdraw
|
|
|
|
}
|
|
|
|
|
|
|
|
// get all rewards for all delegations of a delegator
|
|
|
|
func (k Keeper) currentDelegationReward(ctx sdk.Context, delAddr sdk.AccAddress,
|
|
|
|
valAddr sdk.ValAddress) types.DecCoins {
|
|
|
|
|
|
|
|
wc := k.GetWithdrawContext(ctx, valAddr)
|
2018-09-10 15:37:58 -07:00
|
|
|
|
2018-10-22 01:18:10 -07:00
|
|
|
valInfo := k.GetValidatorDistInfo(ctx, valAddr)
|
2018-10-26 04:42:53 -07:00
|
|
|
delInfo := k.GetDelegationDistInfo(ctx, delAddr, valAddr)
|
2018-10-22 01:18:10 -07:00
|
|
|
validator := k.stakeKeeper.Validator(ctx, valAddr)
|
2018-10-26 04:42:53 -07:00
|
|
|
delegation := k.stakeKeeper.Delegation(ctx, delAddr, valAddr)
|
2018-09-10 15:37:58 -07:00
|
|
|
|
2018-10-26 04:42:53 -07:00
|
|
|
estimation := delInfo.CurrentRewards(wc, valInfo,
|
|
|
|
validator.GetDelegatorShares(), delegation.GetShares())
|
2018-09-04 23:41:17 -07:00
|
|
|
|
2018-10-26 04:42:53 -07:00
|
|
|
return estimation
|
|
|
|
}
|
|
|
|
|
|
|
|
//___________________________________________________________________________________________
|
|
|
|
|
|
|
|
// withdraw all rewards for a single delegation
|
|
|
|
// NOTE: This gets called "onDelegationSharesModified",
|
|
|
|
// meaning any changes to bonded coins
|
|
|
|
func (k Keeper) WithdrawToDelegator(ctx sdk.Context, feePool types.FeePool,
|
|
|
|
delAddr sdk.AccAddress, amount types.DecCoins) {
|
|
|
|
|
|
|
|
withdrawAddr := k.GetDelegatorWithdrawAddr(ctx, delAddr)
|
|
|
|
coinsToAdd, change := amount.TruncateDecimal()
|
2018-10-18 12:58:57 -07:00
|
|
|
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-17 20:02:15 -07:00
|
|
|
//___________________________________________________________________________________________
|
2018-09-04 23:41:17 -07:00
|
|
|
|
2018-10-26 04:42:53 -07:00
|
|
|
// withdraw all rewards for a single delegation
|
|
|
|
// NOTE: This gets called "onDelegationSharesModified",
|
|
|
|
// meaning any changes to bonded coins
|
|
|
|
func (k Keeper) WithdrawDelegationReward(ctx sdk.Context, delAddr sdk.AccAddress,
|
|
|
|
valAddr sdk.ValAddress) sdk.Error {
|
|
|
|
|
|
|
|
if !k.HasDelegationDistInfo(ctx, delAddr, valAddr) {
|
|
|
|
return types.ErrNoDelegationDistInfo(k.codespace)
|
2018-09-28 01:02:07 -07:00
|
|
|
}
|
2018-10-26 04:42:53 -07:00
|
|
|
|
|
|
|
feePool, valInfo, delInfo, withdraw :=
|
|
|
|
k.withdrawDelegationReward(ctx, delAddr, valAddr)
|
|
|
|
|
|
|
|
k.SetValidatorDistInfo(ctx, valInfo)
|
|
|
|
k.SetDelegationDistInfo(ctx, delInfo)
|
|
|
|
k.WithdrawToDelegator(ctx, feePool, delAddr, withdraw)
|
|
|
|
return nil
|
2018-09-04 23:41:17 -07:00
|
|
|
}
|
|
|
|
|
2018-10-26 04:42:53 -07:00
|
|
|
// current rewards for a single delegation
|
|
|
|
func (k Keeper) CurrentDelegationReward(ctx sdk.Context, delAddr sdk.AccAddress,
|
|
|
|
valAddr sdk.ValAddress) (sdk.Coins, sdk.Error) {
|
|
|
|
|
|
|
|
if !k.HasDelegationDistInfo(ctx, delAddr, valAddr) {
|
|
|
|
return sdk.Coins{}, types.ErrNoDelegationDistInfo(k.codespace)
|
|
|
|
}
|
|
|
|
estCoins := k.currentDelegationReward(ctx, delAddr, valAddr)
|
|
|
|
trucate, _ := estCoins.TruncateDecimal()
|
|
|
|
return trucate, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//___________________________________________________________________________________________
|
|
|
|
|
2018-09-10 15:37:58 -07:00
|
|
|
// return all rewards for all delegations of a delegator
|
2018-10-26 04:42:53 -07:00
|
|
|
func (k Keeper) WithdrawDelegationRewardsAll(ctx sdk.Context, delAddr sdk.AccAddress) {
|
|
|
|
withdraw := k.withdrawDelegationRewardsAll(ctx, delAddr)
|
|
|
|
feePool := k.GetFeePool(ctx)
|
|
|
|
k.WithdrawToDelegator(ctx, feePool, delAddr, withdraw)
|
|
|
|
}
|
2018-09-04 23:41:17 -07:00
|
|
|
|
2018-10-26 04:42:53 -07:00
|
|
|
func (k Keeper) withdrawDelegationRewardsAll(ctx sdk.Context,
|
|
|
|
delAddr sdk.AccAddress) types.DecCoins {
|
2018-09-04 23:41:17 -07:00
|
|
|
|
|
|
|
// iterate over all the delegations
|
2018-10-26 04:42:53 -07:00
|
|
|
withdraw := types.DecCoins{}
|
2018-09-19 19:54:46 -07:00
|
|
|
operationAtDelegation := func(_ int64, del sdk.Delegation) (stop bool) {
|
2018-10-26 04:42:53 -07:00
|
|
|
|
2018-10-22 01:18:10 -07:00
|
|
|
valAddr := del.GetValidatorAddr()
|
2018-10-26 04:42:53 -07:00
|
|
|
feePool, valInfo, delInfo, diWithdraw :=
|
|
|
|
k.withdrawDelegationReward(ctx, delAddr, valAddr)
|
2018-09-19 19:54:46 -07:00
|
|
|
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
|
|
|
|
}
|
2018-10-26 04:42:53 -07:00
|
|
|
|
|
|
|
// get all rewards for all delegations of a delegator
|
|
|
|
func (k Keeper) CurrentDelegationRewardsAll(ctx sdk.Context,
|
|
|
|
delAddr sdk.AccAddress) types.DecCoins {
|
|
|
|
|
|
|
|
// iterate over all the delegations
|
|
|
|
total := types.DecCoins{}
|
|
|
|
operationAtDelegation := func(_ int64, del sdk.Delegation) (stop bool) {
|
|
|
|
valAddr := del.GetValidatorAddr()
|
|
|
|
est := k.currentDelegationReward(ctx, delAddr, valAddr)
|
|
|
|
total = total.Plus(est)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
k.stakeKeeper.IterateDelegations(ctx, delAddr, operationAtDelegation)
|
|
|
|
return total
|
|
|
|
}
|