2021-07-15 14:05:54 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
|
|
|
|
|
|
"github.com/kava-labs/kava/x/incentive/types"
|
|
|
|
validatorvesting "github.com/kava-labs/kava/x/validator-vesting"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ClaimUSDXMintingReward pays out funds from a claim to a receiver account.
|
|
|
|
// Rewards are removed from a claim and paid out according to the multiplier, which reduces the reward amount in exchange for shorter vesting times.
|
2021-08-04 14:55:29 +00:00
|
|
|
func (k Keeper) ClaimUSDXMintingReward(ctx sdk.Context, owner, receiver sdk.AccAddress, multiplierName string) error {
|
2021-07-15 14:05:54 +00:00
|
|
|
claim, found := k.GetUSDXMintingClaim(ctx, owner)
|
|
|
|
if !found {
|
|
|
|
return sdkerrors.Wrapf(types.ErrClaimNotFound, "address: %s", owner)
|
|
|
|
}
|
|
|
|
|
2021-08-04 14:55:29 +00:00
|
|
|
name, err := types.ParseMultiplierName(multiplierName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
multiplier, found := k.GetMultiplierByDenom(ctx, types.USDXMintingRewardDenom, name)
|
2021-07-15 14:05:54 +00:00
|
|
|
if !found {
|
2021-08-04 14:55:29 +00:00
|
|
|
return sdkerrors.Wrapf(types.ErrInvalidMultiplier, "denom '%s' has no multiplier '%s'", types.USDXMintingRewardDenom, name)
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
claimEnd := k.GetClaimEnd(ctx)
|
|
|
|
|
|
|
|
if ctx.BlockTime().After(claimEnd) {
|
|
|
|
return sdkerrors.Wrapf(types.ErrClaimExpired, "block time %s > claim end time %s", ctx.BlockTime(), claimEnd)
|
|
|
|
}
|
|
|
|
|
2021-08-04 14:55:29 +00:00
|
|
|
claim, err = k.SynchronizeUSDXMintingClaim(ctx, claim)
|
2021-07-15 14:05:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rewardAmount := claim.Reward.Amount.ToDec().Mul(multiplier.Factor).RoundInt()
|
|
|
|
if rewardAmount.IsZero() {
|
|
|
|
return types.ErrZeroClaim
|
|
|
|
}
|
|
|
|
rewardCoin := sdk.NewCoin(claim.Reward.Denom, rewardAmount)
|
|
|
|
length, err := k.GetPeriodLength(ctx, multiplier)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = k.SendTimeLockedCoinsToAccount(ctx, types.IncentiveMacc, receiver, sdk.NewCoins(rewardCoin), length)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
k.ZeroUSDXMintingClaim(ctx, claim)
|
|
|
|
|
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
types.EventTypeClaim,
|
|
|
|
sdk.NewAttribute(types.AttributeKeyClaimedBy, owner.String()),
|
|
|
|
sdk.NewAttribute(types.AttributeKeyClaimAmount, claim.Reward.String()),
|
2021-08-10 17:59:19 +00:00
|
|
|
sdk.NewAttribute(types.AttributeKeyClaimType, claim.GetType()),
|
2021-07-15 14:05:54 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClaimHardReward pays out funds from a claim to a receiver account.
|
|
|
|
// Rewards are removed from a claim and paid out according to the multiplier, which reduces the reward amount in exchange for shorter vesting times.
|
2021-08-04 14:55:29 +00:00
|
|
|
func (k Keeper) ClaimHardReward(ctx sdk.Context, owner, receiver sdk.AccAddress, denom string, multiplierName string) error {
|
|
|
|
name, err := types.ParseMultiplierName(multiplierName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 14:55:29 +00:00
|
|
|
multiplier, found := k.GetMultiplierByDenom(ctx, denom, name)
|
2021-07-15 14:05:54 +00:00
|
|
|
if !found {
|
2021-08-04 14:55:29 +00:00
|
|
|
return sdkerrors.Wrapf(types.ErrInvalidMultiplier, "denom '%s' has no multiplier '%s'", denom, name)
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
claimEnd := k.GetClaimEnd(ctx)
|
|
|
|
|
|
|
|
if ctx.BlockTime().After(claimEnd) {
|
|
|
|
return sdkerrors.Wrapf(types.ErrClaimExpired, "block time %s > claim end time %s", ctx.BlockTime(), claimEnd)
|
|
|
|
}
|
|
|
|
|
|
|
|
k.SynchronizeHardLiquidityProviderClaim(ctx, owner)
|
|
|
|
|
|
|
|
syncedClaim, found := k.GetHardLiquidityProviderClaim(ctx, owner)
|
|
|
|
if !found {
|
|
|
|
return sdkerrors.Wrapf(types.ErrClaimNotFound, "address: %s", owner)
|
|
|
|
}
|
|
|
|
|
2021-08-04 14:55:29 +00:00
|
|
|
amt := syncedClaim.Reward.AmountOf(denom)
|
|
|
|
|
|
|
|
claimingCoins := sdk.NewCoins(sdk.NewCoin(denom, amt))
|
|
|
|
rewardCoins := sdk.NewCoins(sdk.NewCoin(denom, amt.ToDec().Mul(multiplier.Factor).RoundInt()))
|
2021-07-15 14:05:54 +00:00
|
|
|
if rewardCoins.IsZero() {
|
|
|
|
return types.ErrZeroClaim
|
|
|
|
}
|
|
|
|
length, err := k.GetPeriodLength(ctx, multiplier)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = k.SendTimeLockedCoinsToAccount(ctx, types.IncentiveMacc, receiver, rewardCoins, length)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove claimed coins (NOT reward coins)
|
|
|
|
syncedClaim.Reward = syncedClaim.Reward.Sub(claimingCoins)
|
|
|
|
k.SetHardLiquidityProviderClaim(ctx, syncedClaim)
|
|
|
|
|
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
types.EventTypeClaim,
|
|
|
|
sdk.NewAttribute(types.AttributeKeyClaimedBy, owner.String()),
|
|
|
|
sdk.NewAttribute(types.AttributeKeyClaimAmount, claimingCoins.String()),
|
|
|
|
sdk.NewAttribute(types.AttributeKeyClaimType, syncedClaim.GetType()),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClaimDelegatorReward pays out funds from a claim to a receiver account.
|
|
|
|
// Rewards are removed from a claim and paid out according to the multiplier, which reduces the reward amount in exchange for shorter vesting times.
|
2021-08-04 14:55:29 +00:00
|
|
|
func (k Keeper) ClaimDelegatorReward(ctx sdk.Context, owner, receiver sdk.AccAddress, denom string, multiplierName string) error {
|
2021-07-15 14:05:54 +00:00
|
|
|
claim, found := k.GetDelegatorClaim(ctx, owner)
|
|
|
|
if !found {
|
|
|
|
return sdkerrors.Wrapf(types.ErrClaimNotFound, "address: %s", owner)
|
|
|
|
}
|
|
|
|
|
2021-08-04 14:55:29 +00:00
|
|
|
name, err := types.ParseMultiplierName(multiplierName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
multiplier, found := k.GetMultiplierByDenom(ctx, denom, name)
|
2021-07-15 14:05:54 +00:00
|
|
|
if !found {
|
2021-08-04 14:55:29 +00:00
|
|
|
return sdkerrors.Wrapf(types.ErrInvalidMultiplier, "denom '%s' has no multiplier '%s'", denom, name)
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
claimEnd := k.GetClaimEnd(ctx)
|
|
|
|
|
|
|
|
if ctx.BlockTime().After(claimEnd) {
|
|
|
|
return sdkerrors.Wrapf(types.ErrClaimExpired, "block time %s > claim end time %s", ctx.BlockTime(), claimEnd)
|
|
|
|
}
|
|
|
|
|
|
|
|
syncedClaim, err := k.SynchronizeDelegatorClaim(ctx, claim)
|
|
|
|
if err != nil {
|
|
|
|
return sdkerrors.Wrapf(types.ErrClaimNotFound, "address: %s", owner)
|
|
|
|
}
|
|
|
|
|
2021-08-04 14:55:29 +00:00
|
|
|
amt := syncedClaim.Reward.AmountOf(denom)
|
|
|
|
|
|
|
|
claimingCoins := sdk.NewCoins(sdk.NewCoin(denom, amt))
|
|
|
|
rewardCoins := sdk.NewCoins(sdk.NewCoin(denom, amt.ToDec().Mul(multiplier.Factor).RoundInt()))
|
2021-07-15 14:05:54 +00:00
|
|
|
if rewardCoins.IsZero() {
|
|
|
|
return types.ErrZeroClaim
|
|
|
|
}
|
|
|
|
|
|
|
|
length, err := k.GetPeriodLength(ctx, multiplier)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = k.SendTimeLockedCoinsToAccount(ctx, types.IncentiveMacc, receiver, rewardCoins, length)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove claimed coins (NOT reward coins)
|
|
|
|
syncedClaim.Reward = syncedClaim.Reward.Sub(claimingCoins)
|
|
|
|
k.SetDelegatorClaim(ctx, syncedClaim)
|
|
|
|
|
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
types.EventTypeClaim,
|
|
|
|
sdk.NewAttribute(types.AttributeKeyClaimedBy, owner.String()),
|
|
|
|
sdk.NewAttribute(types.AttributeKeyClaimAmount, claimingCoins.String()),
|
|
|
|
sdk.NewAttribute(types.AttributeKeyClaimType, syncedClaim.GetType()),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClaimSwapReward pays out funds from a claim to a receiver account.
|
|
|
|
// Rewards are removed from a claim and paid out according to the multiplier, which reduces the reward amount in exchange for shorter vesting times.
|
2021-08-04 14:55:29 +00:00
|
|
|
func (k Keeper) ClaimSwapReward(ctx sdk.Context, owner, receiver sdk.AccAddress, denom string, multiplierName string) error {
|
|
|
|
name, err := types.ParseMultiplierName(multiplierName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
2021-08-04 14:55:29 +00:00
|
|
|
multiplier, found := k.GetMultiplierByDenom(ctx, denom, name)
|
2021-07-15 14:05:54 +00:00
|
|
|
if !found {
|
2021-08-04 14:55:29 +00:00
|
|
|
return sdkerrors.Wrapf(types.ErrInvalidMultiplier, "denom '%s' has no multiplier '%s'", denom, name)
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
claimEnd := k.GetClaimEnd(ctx)
|
|
|
|
|
|
|
|
if ctx.BlockTime().After(claimEnd) {
|
|
|
|
return sdkerrors.Wrapf(types.ErrClaimExpired, "block time %s > claim end time %s", ctx.BlockTime(), claimEnd)
|
|
|
|
}
|
|
|
|
|
|
|
|
syncedClaim, found := k.GetSynchronizedSwapClaim(ctx, owner)
|
|
|
|
if !found {
|
|
|
|
return sdkerrors.Wrapf(types.ErrClaimNotFound, "address: %s", owner)
|
|
|
|
}
|
|
|
|
|
2021-08-04 14:55:29 +00:00
|
|
|
amt := syncedClaim.Reward.AmountOf(denom)
|
|
|
|
|
|
|
|
claimingCoins := sdk.NewCoins(sdk.NewCoin(denom, amt))
|
|
|
|
rewardCoins := sdk.NewCoins(sdk.NewCoin(denom, amt.ToDec().Mul(multiplier.Factor).RoundInt()))
|
2021-07-15 14:05:54 +00:00
|
|
|
if rewardCoins.IsZero() {
|
|
|
|
return types.ErrZeroClaim
|
|
|
|
}
|
|
|
|
length, err := k.GetPeriodLength(ctx, multiplier)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = k.SendTimeLockedCoinsToAccount(ctx, types.IncentiveMacc, receiver, rewardCoins, length)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove claimed coins (NOT reward coins)
|
|
|
|
syncedClaim.Reward = syncedClaim.Reward.Sub(claimingCoins)
|
|
|
|
k.SetSwapClaim(ctx, syncedClaim)
|
|
|
|
|
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
types.EventTypeClaim,
|
|
|
|
sdk.NewAttribute(types.AttributeKeyClaimedBy, owner.String()),
|
|
|
|
sdk.NewAttribute(types.AttributeKeyClaimAmount, claimingCoins.String()),
|
|
|
|
sdk.NewAttribute(types.AttributeKeyClaimType, syncedClaim.GetType()),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k Keeper) ValidateIsValidatorVestingAccount(ctx sdk.Context, address sdk.AccAddress) error {
|
|
|
|
acc := k.accountKeeper.GetAccount(ctx, address)
|
|
|
|
if acc == nil {
|
|
|
|
return sdkerrors.Wrapf(types.ErrAccountNotFound, "address not found: %s", address)
|
|
|
|
}
|
|
|
|
_, ok := acc.(*validatorvesting.ValidatorVestingAccount)
|
|
|
|
if !ok {
|
|
|
|
return sdkerrors.Wrapf(types.ErrInvalidAccountType, "account is not validator vesting account, %s", address)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|