Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add query and cli for lock reward receiver #5373

Merged
merged 2 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -940,8 +940,6 @@ github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3 h1:Ylmch
github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3/go.mod h1:lV6KnqXYD/ayTe7310MHtM3I2q8Z6bBfMAi+bhwPYtI=
github.com/osmosis-labs/osmosis/osmomath v0.0.3-dev.0.20230516205127-c213fddde069 h1:ZgDrTJ2GCH4CJGbV6rudw4O9rPMAuwWoLVZnG6cUr+A=
github.com/osmosis-labs/osmosis/osmomath v0.0.3-dev.0.20230516205127-c213fddde069/go.mod h1:a7lhiXRpn8QJ21OhFpaEnUNErTSIafaYpp02q6uI/Dk=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230516205127-c213fddde069 h1:9C/n+Nx5rre/AHPMlPsQrk1isgydrCNB68aqer86ygE=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230516205127-c213fddde069/go.mod h1:hk/o9/kmTSZmZqwXcSrPuwj/gpRMCqbE/d3vj6teL2A=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230529060317-d6d2dda0fb22 h1:I14d+U4gDSL5dHoQ3G+kGLhZP5Zj3mOxMb/97Xflusc=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230529060317-d6d2dda0fb22/go.mod h1:GIvgXqD8NOtrpu5bJ052tZxWLFj4ekpi1BMwEHIvXVU=
github.com/osmosis-labs/osmosis/x/epochs v0.0.0-20230328024000-175ec88e4304 h1:RIrWLzIiZN5Xd2JOfSOtGZaf6V3qEQYg6EaDTAkMnCo=
Expand Down
10 changes: 10 additions & 0 deletions proto/osmosis/lockup/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ service Query {
"/osmosis/lockup/v1beta1/locked_by_id/{lock_id}";
}

// Returns lock record by id
rpc LockRewardReceiver(LockRewardReceiverRequest)
returns (LockRewardReceiverResponse) {
option (google.api.http).get =
"/osmosis/lockup/v1beta1/lock_reward_receiver/{lock_id}";
}

// Returns next lock ID
rpc NextLockID(NextLockIDRequest) returns (NextLockIDResponse) {
option (google.api.http).get = "/osmosis/lockup/v1beta1/next_lock_id";
Expand Down Expand Up @@ -253,6 +260,9 @@ message LockedDenomResponse {
message LockedRequest { uint64 lock_id = 1; };
message LockedResponse { PeriodLock lock = 1; };

message LockRewardReceiverRequest { uint64 lock_id = 1; };
message LockRewardReceiverResponse { string reward_receiver = 1; };

message NextLockIDRequest {};
message NextLockIDResponse { uint64 lock_id = 1; };

Expand Down
1 change: 1 addition & 0 deletions wasmbinding/stargate_whitelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func init() {
setWhitelistedQuery("/osmosis.lockup.Query/LockedDenom", &lockuptypes.LockedDenomResponse{})
setWhitelistedQuery("/osmosis.lockup.Query/LockedByID", &lockuptypes.LockedResponse{})
setWhitelistedQuery("/osmosis.lockup.Query/NextLockID", &lockuptypes.NextLockIDResponse{})
setWhitelistedQuery("/osmosis.lockup.Query/LockRewardReceiver", &lockuptypes.LockRewardReceiverResponse{})

// mint
setWhitelistedQuery("/osmosis.mint.v1beta1.Query/EpochProvisions", &minttypes.QueryEpochProvisionsResponse{})
Expand Down
14 changes: 14 additions & 0 deletions x/lockup/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func GetQueryCmd() *cobra.Command {
GetCmdAccountUnlockedBeforeTime(),
GetCmdAccountLockedPastTimeDenom(),
GetCmdLockedByID(),
GetCmdLockRewardReceiver(),
GetCmdAccountLockedLongerDuration(),
GetCmdAccountLockedLongerDurationNotUnlockingOnly(),
GetCmdAccountLockedLongerDurationDenom(),
Expand Down Expand Up @@ -192,6 +193,19 @@ func GetCmdLockedByID() *cobra.Command {
return osmocli.BuildQueryCli[*types.LockedRequest](&q, types.NewQueryClient)
}

// GetCmdLockRewardReceiver returns reward receiver for the given lock id
func GetCmdLockRewardReceiver() *cobra.Command {
q := osmocli.QueryDescriptor{
Use: "lock-reward-receiver <id>",
Short: "Query lock's reward receiver",
Long: `{{.Short}}{{.ExampleHeader}}
{{.CommandPrefix}} lock-reward-receiver 1`,
QueryFnName: "LockedByID",
}
q.Long = osmocli.FormatLongDesc(q.Long, osmocli.NewLongMetadata(types.ModuleName).WithShort(q.Short))
return osmocli.BuildQueryCli[*types.LockRewardReceiverRequest](&q, types.NewQueryClient)
}

// GetCmdNextLockID returns next lock id to be created.
func GetCmdNextLockID() *cobra.Command {
return osmocli.SimpleQueryCmd[*types.NextLockIDRequest](
Expand Down
11 changes: 11 additions & 0 deletions x/lockup/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ func (q Querier) LockedByID(goCtx context.Context, req *types.LockedRequest) (*t
return &types.LockedResponse{Lock: lock}, err
}

// LockRewardReceiver returns lock reward receiver of the lock.
func (q Querier) LockRewardReceiver(goCtx context.Context, req *types.LockRewardReceiverRequest) (*types.LockRewardReceiverResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

ctx := sdk.UnwrapSDKContext(goCtx)
rewardReceiver, err := q.Keeper.GetLockRewardReceiver(ctx, req.LockId)
return &types.LockRewardReceiverResponse{RewardReceiver: rewardReceiver}, err
}

// NextLockID returns next lock ID to be created.
func (q Querier) NextLockID(goCtx context.Context, req *types.NextLockIDRequest) (*types.NextLockIDResponse, error) {
if req == nil {
Expand Down
25 changes: 25 additions & 0 deletions x/lockup/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,31 @@ func (s *KeeperTestSuite) TestLockedByID() {
s.Require().Equal(res.Lock.IsUnlocking(), false)
}

func (s *KeeperTestSuite) TestLockRewardReceiver() {
s.SetupTest()
addr1 := sdk.AccAddress([]byte("addr1---------------"))
addr2 := sdk.AccAddress([]byte("addr2---------------"))

// lock coins
coins := sdk.Coins{sdk.NewInt64Coin("stake", 10)}
s.LockTokens(addr1, coins, time.Second)

res, err := s.querier.LockRewardReceiver(sdk.WrapSDKContext(s.Ctx), &types.LockRewardReceiverRequest{LockId: 1})
s.Require().NoError(err)
s.Require().Equal(res.RewardReceiver, addr1.String())

// now change lock reward receiver and then query again
s.App.LockupKeeper.SetLockRewardReceiverAddress(s.Ctx, 1, addr1, addr2.String())
res, err = s.querier.LockRewardReceiver(sdk.WrapSDKContext(s.Ctx), &types.LockRewardReceiverRequest{LockId: 1})
s.Require().NoError(err)
s.Require().Equal(res.RewardReceiver, addr2.String())

// try getting lock reward receiver for invalid lock id, this should error
res, err = s.querier.LockRewardReceiver(sdk.WrapSDKContext(s.Ctx), &types.LockRewardReceiverRequest{LockId: 10})
s.Require().Error(err)
s.Require().Equal(res.RewardReceiver, "")
}

func (s *KeeperTestSuite) TestNextLockID() {
s.SetupTest()
addr1 := sdk.AccAddress([]byte("addr1---------------"))
Expand Down
18 changes: 18 additions & 0 deletions x/lockup/keeper/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,24 @@ func (k Keeper) GetLockByID(ctx sdk.Context, lockID uint64) (*types.PeriodLock,
return &lock, err
}

// GetLockRewardReceiver returns the reward receiver stored in state.
// Note that if the lock reward receiver address in state is an empty string literal,
// it indicates that the lock reward receiver is the owner of the lock, thus
// returns the lock owner address.
func (k Keeper) GetLockRewardReceiver(ctx sdk.Context, lockID uint64) (string, error) {
lock, err := k.GetLockByID(ctx, lockID)
if err != nil {
return "", err
}

rewardReceiverAddress := lock.RewardReceiverAddress
if rewardReceiverAddress == "" {
rewardReceiverAddress = lock.Owner
}

return rewardReceiverAddress, nil
}

// GetPeriodLocks Returns the period locks on pool.
func (k Keeper) GetPeriodLocks(ctx sdk.Context) ([]types.PeriodLock, error) {
unlockings := k.getLocksFromIterator(ctx, k.LockIterator(ctx, true))
Expand Down
Loading