2021-07-15 14:05:54 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2023-04-05 23:21:59 +00:00
|
|
|
errorsmod "cosmossdk.io/errors"
|
2021-07-15 14:05:54 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
2024-08-03 09:51:56 +00:00
|
|
|
"github.com/0glabs/0g-chain/x/incentive/types"
|
2021-07-15 14:05:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
2023-04-05 23:21:59 +00:00
|
|
|
return errorsmod.Wrapf(types.ErrClaimNotFound, "address: %s", owner)
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 02:46:14 +00:00
|
|
|
multiplier, found := k.GetMultiplierByDenom(ctx, types.USDXMintingRewardDenom, multiplierName)
|
2021-07-15 14:05:54 +00:00
|
|
|
if !found {
|
2023-04-05 23:21:59 +00:00
|
|
|
return errorsmod.Wrapf(types.ErrInvalidMultiplier, "denom '%s' has no multiplier '%s'", types.USDXMintingRewardDenom, multiplierName)
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
claimEnd := k.GetClaimEnd(ctx)
|
|
|
|
|
|
|
|
if ctx.BlockTime().After(claimEnd) {
|
2023-04-05 23:21:59 +00:00
|
|
|
return errorsmod.Wrapf(types.ErrClaimExpired, "block time %s > claim end time %s", ctx.BlockTime(), claimEnd)
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
claim, err := k.SynchronizeUSDXMintingClaim(ctx, claim)
|
2021-07-15 14:05:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-04 00:08:45 +00:00
|
|
|
rewardAmount := sdk.NewDecFromInt(claim.Reward.Amount).Mul(multiplier.Factor).RoundInt()
|
2021-07-15 14:05:54 +00:00
|
|
|
if rewardAmount.IsZero() {
|
|
|
|
return types.ErrZeroClaim
|
|
|
|
}
|
|
|
|
rewardCoin := sdk.NewCoin(claim.Reward.Denom, rewardAmount)
|
2022-01-08 00:39:27 +00:00
|
|
|
length := k.GetPeriodLength(ctx.BlockTime(), multiplier.MonthsLockup)
|
2021-07-15 14:05:54 +00:00
|
|
|
|
|
|
|
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 {
|
2022-09-28 02:46:14 +00:00
|
|
|
multiplier, found := k.GetMultiplierByDenom(ctx, denom, multiplierName)
|
2021-07-15 14:05:54 +00:00
|
|
|
if !found {
|
2023-04-05 23:21:59 +00:00
|
|
|
return errorsmod.Wrapf(types.ErrInvalidMultiplier, "denom '%s' has no multiplier '%s'", denom, multiplierName)
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
claimEnd := k.GetClaimEnd(ctx)
|
|
|
|
|
|
|
|
if ctx.BlockTime().After(claimEnd) {
|
2023-04-05 23:21:59 +00:00
|
|
|
return errorsmod.Wrapf(types.ErrClaimExpired, "block time %s > claim end time %s", ctx.BlockTime(), claimEnd)
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
k.SynchronizeHardLiquidityProviderClaim(ctx, owner)
|
|
|
|
|
|
|
|
syncedClaim, found := k.GetHardLiquidityProviderClaim(ctx, owner)
|
|
|
|
if !found {
|
2023-04-05 23:21:59 +00:00
|
|
|
return errorsmod.Wrapf(types.ErrClaimNotFound, "address: %s", owner)
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 14:55:29 +00:00
|
|
|
amt := syncedClaim.Reward.AmountOf(denom)
|
|
|
|
|
|
|
|
claimingCoins := sdk.NewCoins(sdk.NewCoin(denom, amt))
|
2023-04-04 00:08:45 +00:00
|
|
|
rewardCoins := sdk.NewCoins(sdk.NewCoin(denom, sdk.NewDecFromInt(amt).Mul(multiplier.Factor).RoundInt()))
|
2021-07-15 14:05:54 +00:00
|
|
|
if rewardCoins.IsZero() {
|
|
|
|
return types.ErrZeroClaim
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
length := k.GetPeriodLength(ctx.BlockTime(), multiplier.MonthsLockup)
|
2021-07-15 14:05:54 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
err := k.SendTimeLockedCoinsToAccount(ctx, types.IncentiveMacc, receiver, rewardCoins, length)
|
2021-07-15 14:05:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove claimed coins (NOT reward coins)
|
2023-04-04 00:08:45 +00:00
|
|
|
syncedClaim.Reward = syncedClaim.Reward.Sub(claimingCoins...)
|
2021-07-15 14:05:54 +00:00
|
|
|
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 {
|
2023-04-05 23:21:59 +00:00
|
|
|
return errorsmod.Wrapf(types.ErrClaimNotFound, "address: %s", owner)
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 02:46:14 +00:00
|
|
|
multiplier, found := k.GetMultiplierByDenom(ctx, denom, multiplierName)
|
2021-07-15 14:05:54 +00:00
|
|
|
if !found {
|
2023-04-05 23:21:59 +00:00
|
|
|
return errorsmod.Wrapf(types.ErrInvalidMultiplier, "denom '%s' has no multiplier '%s'", denom, multiplierName)
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
claimEnd := k.GetClaimEnd(ctx)
|
|
|
|
|
|
|
|
if ctx.BlockTime().After(claimEnd) {
|
2023-04-05 23:21:59 +00:00
|
|
|
return errorsmod.Wrapf(types.ErrClaimExpired, "block time %s > claim end time %s", ctx.BlockTime(), claimEnd)
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
syncedClaim, err := k.SynchronizeDelegatorClaim(ctx, claim)
|
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return errorsmod.Wrapf(types.ErrClaimNotFound, "address: %s", owner)
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 14:55:29 +00:00
|
|
|
amt := syncedClaim.Reward.AmountOf(denom)
|
|
|
|
|
|
|
|
claimingCoins := sdk.NewCoins(sdk.NewCoin(denom, amt))
|
2023-04-04 00:08:45 +00:00
|
|
|
rewardCoins := sdk.NewCoins(sdk.NewCoin(denom, sdk.NewDecFromInt(amt).Mul(multiplier.Factor).RoundInt()))
|
2021-07-15 14:05:54 +00:00
|
|
|
if rewardCoins.IsZero() {
|
|
|
|
return types.ErrZeroClaim
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
length := k.GetPeriodLength(ctx.BlockTime(), multiplier.MonthsLockup)
|
2021-07-15 14:05:54 +00:00
|
|
|
|
|
|
|
err = k.SendTimeLockedCoinsToAccount(ctx, types.IncentiveMacc, receiver, rewardCoins, length)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove claimed coins (NOT reward coins)
|
2023-04-04 00:08:45 +00:00
|
|
|
syncedClaim.Reward = syncedClaim.Reward.Sub(claimingCoins...)
|
2021-07-15 14:05:54 +00:00
|
|
|
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 {
|
2022-09-28 02:46:14 +00:00
|
|
|
multiplier, found := k.GetMultiplierByDenom(ctx, denom, multiplierName)
|
2021-07-15 14:05:54 +00:00
|
|
|
if !found {
|
2023-04-05 23:21:59 +00:00
|
|
|
return errorsmod.Wrapf(types.ErrInvalidMultiplier, "denom '%s' has no multiplier '%s'", denom, multiplierName)
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
claimEnd := k.GetClaimEnd(ctx)
|
|
|
|
|
|
|
|
if ctx.BlockTime().After(claimEnd) {
|
2023-04-05 23:21:59 +00:00
|
|
|
return errorsmod.Wrapf(types.ErrClaimExpired, "block time %s > claim end time %s", ctx.BlockTime(), claimEnd)
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
syncedClaim, found := k.GetSynchronizedSwapClaim(ctx, owner)
|
|
|
|
if !found {
|
2023-04-05 23:21:59 +00:00
|
|
|
return errorsmod.Wrapf(types.ErrClaimNotFound, "address: %s", owner)
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 14:55:29 +00:00
|
|
|
amt := syncedClaim.Reward.AmountOf(denom)
|
|
|
|
|
|
|
|
claimingCoins := sdk.NewCoins(sdk.NewCoin(denom, amt))
|
2023-04-04 00:08:45 +00:00
|
|
|
rewardCoins := sdk.NewCoins(sdk.NewCoin(denom, sdk.NewDecFromInt(amt).Mul(multiplier.Factor).RoundInt()))
|
2021-07-15 14:05:54 +00:00
|
|
|
if rewardCoins.IsZero() {
|
|
|
|
return types.ErrZeroClaim
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
length := k.GetPeriodLength(ctx.BlockTime(), multiplier.MonthsLockup)
|
2021-07-15 14:05:54 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
err := k.SendTimeLockedCoinsToAccount(ctx, types.IncentiveMacc, receiver, rewardCoins, length)
|
2021-07-15 14:05:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove claimed coins (NOT reward coins)
|
2023-04-04 00:08:45 +00:00
|
|
|
syncedClaim.Reward = syncedClaim.Reward.Sub(claimingCoins...)
|
2021-07-15 14:05:54 +00:00
|
|
|
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
|
|
|
|
}
|
2022-04-01 14:17:03 +00:00
|
|
|
|
|
|
|
// ClaimSavingsReward is a stub method for MsgServer interface compliance
|
|
|
|
func (k Keeper) ClaimSavingsReward(ctx sdk.Context, owner, receiver sdk.AccAddress, denom string, multiplierName string) error {
|
2022-09-28 02:46:14 +00:00
|
|
|
multiplier, found := k.GetMultiplierByDenom(ctx, denom, multiplierName)
|
2022-04-20 11:08:57 +00:00
|
|
|
if !found {
|
2023-04-05 23:21:59 +00:00
|
|
|
return errorsmod.Wrapf(types.ErrInvalidMultiplier, "denom '%s' has no multiplier '%s'", denom, multiplierName)
|
2022-04-20 11:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
claimEnd := k.GetClaimEnd(ctx)
|
|
|
|
|
|
|
|
if ctx.BlockTime().After(claimEnd) {
|
2023-04-05 23:21:59 +00:00
|
|
|
return errorsmod.Wrapf(types.ErrClaimExpired, "block time %s > claim end time %s", ctx.BlockTime(), claimEnd)
|
2022-04-20 11:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
k.SynchronizeSavingsClaim(ctx, owner)
|
|
|
|
|
|
|
|
syncedClaim, found := k.GetSavingsClaim(ctx, owner)
|
|
|
|
if !found {
|
2023-04-05 23:21:59 +00:00
|
|
|
return errorsmod.Wrapf(types.ErrClaimNotFound, "address: %s", owner)
|
2022-04-20 11:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
amt := syncedClaim.Reward.AmountOf(denom)
|
|
|
|
|
|
|
|
claimingCoins := sdk.NewCoins(sdk.NewCoin(denom, amt))
|
2023-04-04 00:08:45 +00:00
|
|
|
rewardCoins := sdk.NewCoins(sdk.NewCoin(denom, sdk.NewDecFromInt(amt).Mul(multiplier.Factor).RoundInt()))
|
2022-04-20 11:08:57 +00:00
|
|
|
if rewardCoins.IsZero() {
|
|
|
|
return types.ErrZeroClaim
|
|
|
|
}
|
|
|
|
length := k.GetPeriodLength(ctx.BlockTime(), multiplier.MonthsLockup)
|
|
|
|
|
|
|
|
err := k.SendTimeLockedCoinsToAccount(ctx, types.IncentiveMacc, receiver, rewardCoins, length)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove claimed coins (NOT reward coins)
|
2023-04-04 00:08:45 +00:00
|
|
|
syncedClaim.Reward = syncedClaim.Reward.Sub(claimingCoins...)
|
2022-04-20 11:08:57 +00:00
|
|
|
k.SetSavingsClaim(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()),
|
|
|
|
),
|
|
|
|
)
|
2022-04-01 14:17:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-09-22 18:26:08 +00:00
|
|
|
|
|
|
|
// ClaimEarnReward 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.
|
|
|
|
func (k Keeper) ClaimEarnReward(ctx sdk.Context, owner, receiver sdk.AccAddress, denom string, multiplierName string) error {
|
2022-09-28 02:46:14 +00:00
|
|
|
multiplier, found := k.GetMultiplierByDenom(ctx, denom, multiplierName)
|
2022-09-22 18:26:08 +00:00
|
|
|
if !found {
|
2023-04-05 23:21:59 +00:00
|
|
|
return errorsmod.Wrapf(types.ErrInvalidMultiplier, "denom '%s' has no multiplier '%s'", denom, multiplierName)
|
2022-09-22 18:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
claimEnd := k.GetClaimEnd(ctx)
|
|
|
|
|
|
|
|
if ctx.BlockTime().After(claimEnd) {
|
2023-04-05 23:21:59 +00:00
|
|
|
return errorsmod.Wrapf(types.ErrClaimExpired, "block time %s > claim end time %s", ctx.BlockTime(), claimEnd)
|
2022-09-22 18:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
syncedClaim, found := k.GetSynchronizedEarnClaim(ctx, owner)
|
|
|
|
if !found {
|
2023-04-05 23:21:59 +00:00
|
|
|
return errorsmod.Wrapf(types.ErrClaimNotFound, "address: %s", owner)
|
2022-09-22 18:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
amt := syncedClaim.Reward.AmountOf(denom)
|
|
|
|
|
|
|
|
claimingCoins := sdk.NewCoins(sdk.NewCoin(denom, amt))
|
2023-04-04 00:08:45 +00:00
|
|
|
rewardCoins := sdk.NewCoins(sdk.NewCoin(denom, sdk.NewDecFromInt(amt).Mul(multiplier.Factor).RoundInt()))
|
2022-09-22 18:26:08 +00:00
|
|
|
if rewardCoins.IsZero() {
|
|
|
|
return types.ErrZeroClaim
|
|
|
|
}
|
|
|
|
length := k.GetPeriodLength(ctx.BlockTime(), multiplier.MonthsLockup)
|
|
|
|
|
|
|
|
err := k.SendTimeLockedCoinsToAccount(ctx, types.IncentiveMacc, receiver, rewardCoins, length)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove claimed coins (NOT reward coins)
|
2023-04-04 00:08:45 +00:00
|
|
|
syncedClaim.Reward = syncedClaim.Reward.Sub(claimingCoins...)
|
2022-09-22 18:26:08 +00:00
|
|
|
k.SetEarnClaim(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
|
|
|
|
}
|