Skip to content

Commit

Permalink
CNS-limit-rewards (#959)
Browse files Browse the repository at this point in the history
* limit plan price

* add to what omer wants

* now 100

* Update x/pairing/keeper/grpc_query_monthly_payout.go

Co-authored-by: Omer <[email protected]>

---------

Co-authored-by: Yarom Swisa <[email protected] git config --global user.name Yarom>
Co-authored-by: Omer <[email protected]>
  • Loading branch information
3 people authored Nov 9, 2023
1 parent 5a250e9 commit c168a04
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
9 changes: 8 additions & 1 deletion x/pairing/keeper/grpc_query_monthly_payout.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/lavanet/lava/utils"
"github.com/lavanet/lava/x/pairing/types"
subsciption "github.com/lavanet/lava/x/subscription/keeper"
subsciptiontypes "github.com/lavanet/lava/x/subscription/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -53,7 +54,13 @@ func (k Keeper) MonthlyPayout(goCtx context.Context, req *types.QueryMonthlyPayo
if !found {
continue
}
totalMonthlyReward := k.subscriptionKeeper.CalcTotalMonthlyReward(ctx, plan, providerCu, subObj.MonthCuTotal-subObj.MonthCuLeft)
totalTokenAmount := plan.Price.Amount
totalCuTracked := subObj.MonthCuTotal - subObj.MonthCuLeft
if plan.Price.Amount.Quo(sdk.NewIntFromUint64(totalCuTracked)).GT(sdk.NewIntFromUint64(subsciption.LIMIT_TOKEN_PER_CU)) {
totalTokenAmount = sdk.NewIntFromUint64(subsciption.LIMIT_TOKEN_PER_CU * totalCuTracked)
}

totalMonthlyReward := k.subscriptionKeeper.CalcTotalMonthlyReward(ctx, totalTokenAmount, providerCu, totalCuTracked)

// calculate only the provider reward
providerReward, err := k.dualstakingKeeper.RewardProvidersAndDelegators(ctx, providerAddr, chainID, totalMonthlyReward, subsciptiontypes.ModuleName, true)
Expand Down
2 changes: 1 addition & 1 deletion x/pairing/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ type SubscriptionKeeper interface {
GetSubscription(ctx sdk.Context, consumer string) (val subscriptiontypes.Subscription, found bool)
GetAllSubTrackedCuIndices(ctx sdk.Context, sub string) []string
GetTrackedCu(ctx sdk.Context, sub string, provider string, chainID string, block uint64) (cu uint64, found bool, key string)
CalcTotalMonthlyReward(ctx sdk.Context, plan planstypes.Plan, trackedCu uint64, totalCuUsedBySub uint64) math.Int
CalcTotalMonthlyReward(ctx sdk.Context, totalAmount math.Int, trackedCu uint64, totalCuUsedBySub uint64) math.Int
AddTrackedCu(ctx sdk.Context, sub string, provider string, chainID string, cu uint64, block uint64) error
GetAllSubscriptionsIndices(ctx sdk.Context) []string
}
Expand Down
15 changes: 11 additions & 4 deletions x/subscription/keeper/cu_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
legacyerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/lavanet/lava/utils"
epochstoragetypes "github.com/lavanet/lava/x/epochstorage/types"
planstypes "github.com/lavanet/lava/x/plans/types"
"github.com/lavanet/lava/x/subscription/types"
)

const LIMIT_TOKEN_PER_CU = 100

// GetTrackedCu gets the tracked CU counter (with QoS influence) and the trackedCu entry's block
func (k Keeper) GetTrackedCu(ctx sdk.Context, sub string, provider string, chainID string, subBlock uint64) (cu uint64, found bool, key string) {
cuTrackerKey := types.CuTrackerKey(sub, provider, chainID)
Expand Down Expand Up @@ -143,6 +144,11 @@ func (k Keeper) RewardAndResetCuTracker(ctx sdk.Context, cuTrackerTimerKeyBytes
return
}

totalTokenAmount := plan.Price.Amount
if plan.Price.Amount.Quo(sdk.NewIntFromUint64(totalCuTracked)).GT(sdk.NewIntFromUint64(LIMIT_TOKEN_PER_CU)) {
totalTokenAmount = sdk.NewIntFromUint64(LIMIT_TOKEN_PER_CU * totalCuTracked)
}

for _, trackedCuInfo := range trackedCuList {
trackedCu := trackedCuInfo.trackedCu
provider := trackedCuInfo.provider
Expand All @@ -162,7 +168,8 @@ func (k Keeper) RewardAndResetCuTracker(ctx sdk.Context, cuTrackerTimerKeyBytes

// monthly reward = (tracked_CU / total_CU_used_in_sub_this_month) * plan_price
// TODO: deal with the reward's remainder (uint division...)
totalMonthlyReward := k.CalcTotalMonthlyReward(ctx, plan, trackedCu, totalCuTracked)

totalMonthlyReward := k.CalcTotalMonthlyReward(ctx, totalTokenAmount, trackedCu, totalCuTracked)

// calculate the provider reward (smaller than totalMonthlyReward
// because it's shared with delegators)
Expand Down Expand Up @@ -207,12 +214,12 @@ func (k Keeper) RewardAndResetCuTracker(ctx sdk.Context, cuTrackerTimerKeyBytes
}
}

func (k Keeper) CalcTotalMonthlyReward(ctx sdk.Context, plan planstypes.Plan, trackedCu uint64, totalCuUsedBySub uint64) math.Int {
func (k Keeper) CalcTotalMonthlyReward(ctx sdk.Context, totalAmount math.Int, trackedCu uint64, totalCuUsedBySub uint64) math.Int {
// TODO: deal with the reward's remainder (uint division...)
// monthly reward = (tracked_CU / total_CU_used_in_sub_this_month) * plan_price
if totalCuUsedBySub == 0 {
return math.ZeroInt()
}
totalMonthlyReward := plan.Price.Amount.MulRaw(int64(trackedCu)).QuoRaw(int64(totalCuUsedBySub))
totalMonthlyReward := totalAmount.MulRaw(int64(trackedCu)).QuoRaw(int64(totalCuUsedBySub))
return totalMonthlyReward
}

0 comments on commit c168a04

Please sign in to comment.