Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
Yarom Swisa authored and Yarom Swisa committed Feb 13, 2024
1 parent 878e0f6 commit caa4c63
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
26 changes: 13 additions & 13 deletions x/subscription/keeper/cu_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
legacyerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/lavanet/lava/utils"
epochstoragetypes "github.com/lavanet/lava/x/epochstorage/types"
rewardstypes "github.com/lavanet/lava/x/rewards/types"
"github.com/lavanet/lava/x/subscription/types"
)

Expand Down Expand Up @@ -234,24 +235,23 @@ func (k Keeper) RewardAndResetCuTracker(ctx sdk.Context, cuTrackerTimerKeyBytes
var latestSub types.Subscription
latestEntryBlock, _, _, found := k.subsFS.FindEntryDetailed(ctx, sub, uint64(ctx.BlockHeight()), &latestSub)
if found {
// return rewards remainder to credit
if !rewardsRemainder.IsZero() {
latestSub.Credit = latestSub.Credit.AddAmount(rewardsRemainder)
}

// subscription not expired - update credit according to usage
updatedCredit := latestSub.Credit.Amount.Sub(totalTokenAmount)
if updatedCredit.IsNegative() {
if latestSub.Credit.Amount.LT(totalTokenRewarded) {
latestSub.Credit.Amount = sdk.ZeroInt()
utils.LavaFormatWarning("providers rewarded more than the subscription credit", nil,
utils.LogAttr("credit", latestSub.Credit.String()),
utils.LogAttr("rewarded", totalTokenRewarded),
utils.LogAttr("subscription", sub),
)
} else {
latestSub.Credit.Amount = updatedCredit
latestSub.Credit.Amount = latestSub.Credit.Amount.Sub(totalTokenRewarded)
}

k.subsFS.ModifyEntry(ctx, latestSub.Consumer, latestEntryBlock, &latestSub)
} else if !rewardsRemainder.IsZero() {
} else if !rewardsRemainder.IsPositive() {
{
// sub expired (no need to update credit), send rewards remainder to the community pool
err = k.rewardsKeeper.FundCommunityPoolFromModule(ctx, rewardsRemainder, types.ModuleName)
// sub expired (no need to update credit), send rewards remainder to the validators
pool := rewardstypes.ValidatorsRewardsDistributionPoolName
err = k.bankKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, string(pool), sdk.NewCoins(sdk.NewCoin(k.stakingKeeper.BondDenom(ctx), rewardsRemainder)))
if err != nil {
utils.LavaFormatError("failed sending remainder of rewards to the community pool", err,
utils.Attribute{Key: "rewards_remainder", Value: rewardsRemainder.String()},
Expand All @@ -263,7 +263,7 @@ func (k Keeper) RewardAndResetCuTracker(ctx sdk.Context, cuTrackerTimerKeyBytes
"sub": sub,
"credit_remaining": latestSub.Credit.String(),
"block": strconv.FormatInt(ctx.BlockHeight(), 10),
}, "CU tracker reward and reset executed successfully, printing remaining subscription credit")
}, "CU tracker reward and reset executed")
}

func (k Keeper) CalcTotalMonthlyReward(ctx sdk.Context, totalAmount math.Int, trackedCu uint64, totalCuUsedBySub uint64) math.Int {
Expand Down
1 change: 1 addition & 0 deletions x/subscription/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type AccountKeeper interface {
type BankKeeper interface {
GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin
SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error
SendCoinsFromModuleToModule(ctx sdk.Context, senderPool, recipientPool string, amt sdk.Coins) error
// Methods imported from bank should be defined here
}

Expand Down

0 comments on commit caa4c63

Please sign in to comment.