Skip to content

Commit 468015b

Browse files
authored
[Masterchef]: add query, transfer funds (#1279)
* add query, transfer funds * coverage * log
1 parent ad7942d commit 468015b

File tree

9 files changed

+4023
-403
lines changed

9 files changed

+4023
-403
lines changed

api/elys/masterchef/query.pulsar.go

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

api/elys/masterchef/query_grpc.pb.go

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

app/setup_handlers.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import (
88
storetypes "cosmossdk.io/store/types"
99
upgradetypes "cosmossdk.io/x/upgrade/types"
1010

11+
errorsmod "cosmossdk.io/errors"
12+
sdkmath "cosmossdk.io/math"
13+
1114
sdk "github.com/cosmos/cosmos-sdk/types"
15+
"github.com/elys-network/elys/x/masterchef/types"
1216

1317
m "github.com/cosmos/cosmos-sdk/types/module"
1418
"github.com/cosmos/cosmos-sdk/version"
@@ -72,6 +76,25 @@ func (app *ElysApp) setUpgradeHandler() {
7276
// }
7377
//}
7478

79+
// 250USDC from protocol account to masterchef
80+
params := app.MasterchefKeeper.GetParams(ctx)
81+
protocolRevenueAddress, err := sdk.AccAddressFromBech32(params.ProtocolRevenueAddress)
82+
if err != nil {
83+
return vm, errorsmod.Wrapf(err, "invalid protocol revenue address")
84+
}
85+
86+
// Create 250 USDC coin
87+
// get usdc denom
88+
usdcDenom, _ := app.AssetprofileKeeper.GetUsdcDenom(ctx)
89+
usdcAmount := sdk.NewCoin(usdcDenom, sdkmath.NewInt(250000000)) // 250 USDC with 6 decimals
90+
91+
// Send coins from protocol revenue address to masterchef module
92+
err = app.BankKeeper.SendCoinsFromAccountToModule(ctx, protocolRevenueAddress, types.ModuleName, sdk.NewCoins(usdcAmount))
93+
if err != nil {
94+
// log error
95+
app.Logger().Error("failed to send USDC to masterchef", "error", err)
96+
}
97+
7598
return vm, vmErr
7699
},
77100
)

proto/elys/masterchef/query.proto

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,18 @@ service Query {
9696
rpc ChainTVL(QueryChainTVLRequest) returns (QueryChainTVLResponse) {
9797
option (google.api.http).get = "/elys-network/elys/masterchef/chain_tvl";
9898
}
99+
100+
rpc TotalPendingRewards(QueryTotalPendingRewardsRequest)
101+
returns (QueryTotalPendingRewardsResponse) {
102+
option (google.api.http).get =
103+
"/elys-network/elys/masterchef/total_pending_rewards";
104+
}
105+
106+
rpc PendingRewards(QueryPendingRewardsRequest)
107+
returns (QueryPendingRewardsResponse) {
108+
option (google.api.http).get =
109+
"/elys-network/elys/masterchef/pending_rewards";
110+
}
99111
}
100112

101113
message QueryAllLiquidityPoolTVLRequest {}
@@ -382,3 +394,26 @@ message PoolRewards {
382394
message QueryPoolRewardsResponse {
383395
repeated PoolRewards pools = 1 [ (gogoproto.nullable) = false ];
384396
}
397+
398+
message QueryTotalPendingRewardsRequest {
399+
cosmos.base.query.v1beta1.PageRequest pagination = 1;
400+
}
401+
402+
message QueryTotalPendingRewardsResponse {
403+
repeated cosmos.base.v1beta1.Coin total_pending_rewards = 1 [
404+
(gogoproto.nullable) = false,
405+
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
406+
];
407+
uint64 count = 2;
408+
cosmos.base.query.v1beta1.PageResponse pagination = 3;
409+
}
410+
411+
message QueryPendingRewardsRequest {}
412+
413+
message QueryPendingRewardsResponse {
414+
repeated cosmos.base.v1beta1.Coin total_pending_rewards = 1 [
415+
(gogoproto.nullable) = false,
416+
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
417+
];
418+
uint64 count = 2;
419+
}

x/masterchef/autocli.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,17 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
113113
Example: "elysd q masterchef pool-rewards [ids]",
114114
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "pool_ids", Varargs: true}},
115115
},
116+
{
117+
RpcMethod: "TotalPendingRewards",
118+
Use: "total-pending-rewards",
119+
Short: "show total pending rewards",
120+
},
121+
{
122+
RpcMethod: "PendingRewards",
123+
Use: "pending-rewards",
124+
Short: "show pending rewards",
125+
Example: "elysd q masterchef pending-rewards",
126+
},
116127
},
117128
},
118129
Tx: &autocliv1.ServiceCommandDescriptor{

x/masterchef/keeper/query.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package keeper
22

33
import (
44
"context"
5+
56
"cosmossdk.io/store/prefix"
7+
storetypes "cosmossdk.io/store/types"
68
"github.com/cosmos/cosmos-sdk/runtime"
79
"github.com/cosmos/cosmos-sdk/types/query"
810

@@ -178,3 +180,69 @@ func (k Keeper) PoolAprs(goCtx context.Context, req *types.QueryPoolAprsRequest)
178180
data := k.CalculatePoolAprs(ctx, req.PoolIds)
179181
return &types.QueryPoolAprsResponse{Data: data}, nil
180182
}
183+
184+
func (k Keeper) TotalPendingRewards(goCtx context.Context, req *types.QueryTotalPendingRewardsRequest) (*types.QueryTotalPendingRewardsResponse, error) {
185+
ctx := sdk.UnwrapSDKContext(goCtx)
186+
187+
if req == nil {
188+
return nil, status.Error(codes.InvalidArgument, "invalid request")
189+
}
190+
191+
var totalRewards sdk.Coins
192+
store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
193+
positionStore := prefix.NewStore(store, types.UserRewardInfoKeyPrefix)
194+
195+
if req.Pagination == nil {
196+
req.Pagination = &query.PageRequest{
197+
Limit: 100000,
198+
}
199+
}
200+
201+
count := uint64(0)
202+
203+
pageRes, err := query.Paginate(positionStore, req.Pagination, func(key []byte, value []byte) error {
204+
var reward types.UserRewardInfo
205+
k.cdc.MustUnmarshal(value, &reward)
206+
k.AfterWithdraw(ctx, reward.PoolId, sdk.MustAccAddressFromBech32(reward.User), sdkmath.ZeroInt())
207+
if reward.RewardPending.IsPositive() {
208+
totalRewards = totalRewards.Add(sdk.NewCoin(reward.RewardDenom, reward.RewardPending.TruncateInt()))
209+
}
210+
count++
211+
return nil
212+
})
213+
if err != nil {
214+
return nil, err
215+
}
216+
217+
return &types.QueryTotalPendingRewardsResponse{
218+
TotalPendingRewards: totalRewards,
219+
Count: count,
220+
Pagination: pageRes,
221+
}, nil
222+
}
223+
224+
func (k Keeper) PendingRewards(goCtx context.Context, req *types.QueryPendingRewardsRequest) (*types.QueryPendingRewardsResponse, error) {
225+
ctx := sdk.UnwrapSDKContext(goCtx)
226+
227+
store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
228+
iterator := storetypes.KVStorePrefixIterator(store, types.UserRewardInfoKeyPrefix)
229+
230+
defer iterator.Close()
231+
232+
var totalRewards sdk.Coins
233+
count := uint64(0)
234+
235+
for ; iterator.Valid(); iterator.Next() {
236+
var val types.UserRewardInfo
237+
k.cdc.MustUnmarshal(iterator.Value(), &val)
238+
if val.RewardPending.IsPositive() {
239+
totalRewards = totalRewards.Add(sdk.NewCoin(val.RewardDenom, val.RewardPending.TruncateInt()))
240+
}
241+
count++
242+
}
243+
244+
return &types.QueryPendingRewardsResponse{
245+
TotalPendingRewards: totalRewards,
246+
Count: count,
247+
}, nil
248+
}

0 commit comments

Comments
 (0)