Skip to content

Commit 6876874

Browse files
committed
optimize farming reward estimation
1 parent 98cd74f commit 6876874

File tree

7 files changed

+682
-517
lines changed

7 files changed

+682
-517
lines changed

api/side/farming/query.pulsar.go

Lines changed: 416 additions & 323 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/side/farming/query_grpc.pb.go

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/side/farming/query.proto

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ service Query {
4949
option (google.api.http).get = "/side/farming/current_epoch/pending_reward/{address}";
5050
}
5151

52-
rpc EstimateReward(QueryEstimateRewardRequest) returns (QueryEstimateRewardResponse) {
53-
option (google.api.http).get = "/side/farming/estimate_reward";
52+
rpc EstimatedReward(QueryEstimatedRewardRequest) returns (QueryEstimatedRewardResponse) {
53+
option (google.api.http).get = "/side/farming/estimated_reward";
5454
}
5555
}
5656

@@ -139,15 +139,16 @@ message QueryPendingRewardByAddressResponse {
139139
AccountRewardPerEpoch pending_reward = 1;
140140
}
141141

142-
// QueryEstimateRewardRequest is request type for the Query/EstimateReward RPC method.
143-
message QueryEstimateRewardRequest {
144-
string amount = 1;
145-
google.protobuf.Duration lock_duration = 2 [(gogoproto.nullable) = false, (gogoproto.stdduration) = true];
142+
// QueryEstimatedRewardRequest is request type for the Query/EstimatedReward RPC method.
143+
message QueryEstimatedRewardRequest {
144+
string address = 1;
145+
string amount = 2;
146+
google.protobuf.Duration lock_duration = 3 [(gogoproto.nullable) = false, (gogoproto.stdduration) = true];
146147
}
147148

148-
// QueryEstimateRewardResponse is response type for the Query/EstimateReward RPC method.
149-
message QueryEstimateRewardResponse {
150-
string reward = 1;
149+
// QueryEstimatedRewardResponse is response type for the Query/EstimatedReward RPC method.
150+
message QueryEstimatedRewardResponse {
151+
AccountRewardPerEpoch reward = 1;
151152
}
152153

153154
// QueryParamsRequest is request type for the Query/Params RPC method.

x/farming/keeper/queries.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,18 @@ func (k Keeper) PendingRewardByAddress(goCtx context.Context, req *types.QueryPe
137137
return &types.QueryPendingRewardByAddressResponse{PendingReward: k.GetPendingRewardByAddress(ctx, req.Address)}, nil
138138
}
139139

140-
func (k Keeper) EstimateReward(goCtx context.Context, req *types.QueryEstimateRewardRequest) (*types.QueryEstimateRewardResponse, error) {
140+
func (k Keeper) EstimatedReward(goCtx context.Context, req *types.QueryEstimatedRewardRequest) (*types.QueryEstimatedRewardResponse, error) {
141141
if req == nil {
142142
return nil, status.Error(codes.InvalidArgument, "invalid request")
143143
}
144144

145145
ctx := sdk.UnwrapSDKContext(goCtx)
146146

147+
_, err := sdk.AccAddressFromBech32(req.Address)
148+
if err != nil {
149+
return nil, status.Error(codes.InvalidArgument, "invalid address")
150+
}
151+
147152
amount, err := sdk.ParseCoinNormalized(req.Amount)
148153
if err != nil || !amount.IsPositive() {
149154
return nil, status.Error(codes.InvalidArgument, "invalid amount")
@@ -157,7 +162,7 @@ func (k Keeper) EstimateReward(goCtx context.Context, req *types.QueryEstimateRe
157162
return nil, status.Error(codes.InvalidArgument, "invalid lock duration")
158163
}
159164

160-
estimatedReward := k.GetEstimatedReward(ctx, amount, req.LockDuration)
165+
estimatedReward := k.GetEstimatedReward(ctx, req.Address, amount, req.LockDuration)
161166

162-
return &types.QueryEstimateRewardResponse{Reward: estimatedReward.String()}, nil
167+
return &types.QueryEstimatedRewardResponse{Reward: estimatedReward}, nil
163168
}

x/farming/keeper/reward.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,25 @@ func (k Keeper) GetRewards(ctx sdk.Context, address string) (sdk.Coin, sdk.Coin)
5454

5555
// GetEstimatedReward gets the estimated epoch reward for the given params
5656
// Assume that the given params are valid
57-
func (k Keeper) GetEstimatedReward(ctx sdk.Context, amount sdk.Coin, lockDuration time.Duration) sdk.Coin {
57+
func (k Keeper) GetEstimatedReward(ctx sdk.Context, address string, amount sdk.Coin, lockDuration time.Duration) *types.AccountRewardPerEpoch {
5858
nextEpoch := k.GetNextEpochSnapshot(ctx)
5959

6060
staking := &types.Staking{
61+
Address: address,
6162
Amount: amount,
6263
EffectiveAmount: types.GetEffectiveAmount(amount, types.GetLockMultiplier(lockDuration)),
6364
}
6465

6566
types.UpdateEpochTotalStakings(nextEpoch, staking)
6667

67-
asset := types.GetAsset(k.EligibleAssets(ctx), staking.Amount.Denom)
68+
totalStakings := []types.TotalStaking{}
69+
for _, staking := range k.GetStakingsByAddress(ctx, address) {
70+
if !staking.StartTime.Add(staking.LockDuration).Before(nextEpoch.StartTime) {
71+
totalStakings = types.UpdateAccountTotalStakings(totalStakings, staking)
72+
}
73+
}
6874

69-
return types.GetEpochReward(ctx, staking, nextEpoch, k.RewardPerEpoch(ctx), asset.RewardRatio)
75+
return types.GetAccountRewardPerEpoch(address, totalStakings, nextEpoch, k.RewardPerEpoch(ctx), k.EligibleAssets(ctx))
7076
}
7177

7278
// ClaimAllRewards claims all pending rewards of the given address

0 commit comments

Comments
 (0)