Skip to content

Commit 883dceb

Browse files
testinginprodmatthiasmattNibiruHeisenbergAgentSmithMatrix
authored
add: lockup query server (#442)
* add: finalize lockup module * chore: lint * bring back msg server Co-authored-by: Mat-Cosmos <[email protected]> Co-authored-by: Walter White <[email protected]> Co-authored-by: AgentSmithMatrix <[email protected]> Co-authored-by: Agent Smith <[email protected]>
1 parent 40e064d commit 883dceb

File tree

13 files changed

+1418
-107
lines changed

13 files changed

+1418
-107
lines changed

app/app.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ type NibiruApp struct {
216216
PerpKeeper perpkeeper.Keeper
217217
PriceKeeper pricekeeper.Keeper
218218
EpochsKeeper epochskeeper.Keeper
219-
LockupKeeper lockupkeeper.LockupKeeper
219+
LockupKeeper lockupkeeper.Keeper
220220
IncentivizationKeeper incentivizationkeeper.Keeper
221221
VpoolKeeper vpoolkeeper.Keeper
222222

@@ -410,7 +410,7 @@ func NewNibiruApp(
410410
appCodec, app.StablecoinKeeper, app.AccountKeeper, app.BankKeeper,
411411
app.PriceKeeper,
412412
)
413-
lockupModule := lockup.NewAppModule(appCodec, app.GovKeeper, app.AccountKeeper, app.BankKeeper)
413+
lockupModule := lockup.NewAppModule(appCodec, app.LockupKeeper, app.AccountKeeper, app.BankKeeper)
414414
perpModule := perp.NewAppModule(
415415
appCodec, app.PerpKeeper, app.AccountKeeper, app.BankKeeper,
416416
app.PriceKeeper,

proto/lockup/v1/query.proto

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ package nibiru.lockup.v1;
44
import "gogoproto/gogo.proto";
55
import "google/api/annotations.proto";
66
import "cosmos/base/v1beta1/coin.proto";
7+
import "cosmos/base/query/v1beta1/pagination.proto";
8+
9+
import "lockup/v1/lock.proto";
710

811
option go_package = "github.com/NibiruChain/nibiru/x/lockup/types";
912

@@ -12,6 +15,13 @@ service Query {
1215
option (google.api.http).get = "/nibiru/lockup/locked_coins";
1316
}
1417

18+
rpc Lock(QueryLockRequest) returns (QueryLockResponse) {
19+
option (google.api.http).get = "/nibiru/lockup/lock";
20+
}
21+
22+
rpc LocksByAddress(QueryLocksByAddress) returns (QueryLocksByAddressResponse) {
23+
option (google.api.http).get = "/nibiru/lockup/locks_by_address";
24+
};
1525
}
1626

1727
message QueryLockedCoinsRequest {
@@ -23,4 +33,21 @@ message QueryLockedCoinsResponse {
2333
(gogoproto.nullable) = false,
2434
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
2535
];
36+
}
37+
38+
message QueryLockRequest {
39+
uint64 id = 1;
40+
}
41+
42+
message QueryLockResponse {
43+
nibiru.lockup.v1.Lock lock = 1;
44+
}
45+
46+
message QueryLocksByAddress {
47+
string address = 1;
48+
cosmos.base.query.v1beta1.PageRequest pagination = 2;
49+
}
50+
51+
message QueryLocksByAddressResponse {
52+
repeated nibiru.lockup.v1.Lock locks = 1;
2653
}

x/incentivization/keeper/keeper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const (
3131
FundsModuleAccountAddressPrefix = "incentivization_escrow_"
3232
)
3333

34-
func NewKeeper(cdc codec.Codec, storeKey sdk.StoreKey, ak authkeeper.AccountKeeper, bk bankkeeper.Keeper, dk dexkeeper.Keeper, lk lockupkeeper.LockupKeeper) Keeper {
34+
func NewKeeper(cdc codec.Codec, storeKey sdk.StoreKey, ak authkeeper.AccountKeeper, bk bankkeeper.Keeper, dk dexkeeper.Keeper, lk lockupkeeper.Keeper) Keeper {
3535
return Keeper{
3636
cdc: cdc,
3737
storeKey: storeKey,
@@ -49,7 +49,7 @@ type Keeper struct {
4949
ak authkeeper.AccountKeeper
5050
bk bankkeeper.Keeper
5151
dk dexkeeper.Keeper
52-
lk lockupkeeper.LockupKeeper
52+
lk lockupkeeper.Keeper
5353
}
5454

5555
func (k Keeper) CreateIncentivizationProgram(

x/lockup/abci.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import (
99
)
1010

1111
// BeginBlocker is called on every block.
12-
func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.LockupKeeper) {
12+
func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper) {
1313
}
1414

1515
// Called every block to automatically unlock matured locks.
16-
func EndBlocker(ctx sdk.Context, k keeper.LockupKeeper) []abci.ValidatorUpdate {
16+
func EndBlocker(ctx sdk.Context, k keeper.Keeper) []abci.ValidatorUpdate {
1717
return []abci.ValidatorUpdate{}
1818
}

x/lockup/genesis.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import (
99

1010
// InitGenesis initializes the capability module's state from a provided genesis
1111
// state.
12-
func InitGenesis(ctx sdk.Context, k keeper.LockupKeeper, genState types.GenesisState) {
12+
func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) {
1313

1414
}
1515

1616
// ExportGenesis returns the capability module's exported genesis.
17-
func ExportGenesis(ctx sdk.Context, k keeper.LockupKeeper) *types.GenesisState {
17+
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
1818
return &types.GenesisState{}
1919
}

x/lockup/keeper/keeper.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ var (
2222
MaxTime = time.Unix(253402297199, 0).UTC()
2323
)
2424

25-
// LockupKeeper provides a way to manage module storage.
26-
type LockupKeeper struct {
25+
// Keeper provides a way to manage module storage.
26+
type Keeper struct {
2727
cdc codec.Codec
2828
storeKey sdk.StoreKey
2929

@@ -34,8 +34,8 @@ type LockupKeeper struct {
3434

3535
// NewLockupKeeper returns an instance of Keeper.
3636
func NewLockupKeeper(cdc codec.Codec, storeKey sdk.StoreKey, ak types.AccountKeeper,
37-
bk types.BankKeeper, dk types.DistrKeeper) LockupKeeper {
38-
return LockupKeeper{
37+
bk types.BankKeeper, dk types.DistrKeeper) Keeper {
38+
return Keeper{
3939
cdc: cdc,
4040
storeKey: storeKey,
4141
ak: ak,
@@ -45,12 +45,12 @@ func NewLockupKeeper(cdc codec.Codec, storeKey sdk.StoreKey, ak types.AccountKee
4545
}
4646

4747
// Logger returns a logger instance.
48-
func (k LockupKeeper) Logger(ctx sdk.Context) log.Logger {
48+
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
4949
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
5050
}
5151

5252
// LockTokens lock tokens from an account for specified duration.
53-
func (k LockupKeeper) LockTokens(ctx sdk.Context, owner sdk.AccAddress,
53+
func (k Keeper) LockTokens(ctx sdk.Context, owner sdk.AccAddress,
5454
coins sdk.Coins, duration time.Duration) (*types.Lock, error) {
5555
// create new lock object
5656
lock := &types.Lock{
@@ -71,7 +71,7 @@ func (k LockupKeeper) LockTokens(ctx sdk.Context, owner sdk.AccAddress,
7171

7272
// UnlockTokens returns tokens back from the module account address to the lock owner.
7373
// The ID associated with the lock must exist, and the current block time must be after lock end time.
74-
func (k LockupKeeper) UnlockTokens(ctx sdk.Context, lockID uint64) (unlockedTokens sdk.Coins, err error) {
74+
func (k Keeper) UnlockTokens(ctx sdk.Context, lockID uint64) (unlockedTokens sdk.Coins, err error) {
7575
lock, err := k.LocksState(ctx).Get(lockID)
7676
if err != nil {
7777
return nil, err
@@ -101,7 +101,7 @@ func (k LockupKeeper) UnlockTokens(ctx sdk.Context, lockID uint64) (unlockedToke
101101
}
102102

103103
// InitiateUnlocking starts the unlocking process of a lockup.
104-
func (k LockupKeeper) InitiateUnlocking(ctx sdk.Context, lockID uint64) (updatedLock *types.Lock, err error) {
104+
func (k Keeper) InitiateUnlocking(ctx sdk.Context, lockID uint64) (updatedLock *types.Lock, err error) {
105105
// we get the lockup
106106
lock, err := k.LocksState(ctx).Get(lockID)
107107
if err != nil {
@@ -125,7 +125,7 @@ func (k LockupKeeper) InitiateUnlocking(ctx sdk.Context, lockID uint64) (updated
125125
}
126126

127127
// UnlockAvailableCoins unlocks all the available coins for the provided account sdk.AccAddress.
128-
func (k LockupKeeper) UnlockAvailableCoins(ctx sdk.Context, account sdk.AccAddress) (coins sdk.Coins, err error) {
128+
func (k Keeper) UnlockAvailableCoins(ctx sdk.Context, account sdk.AccAddress) (coins sdk.Coins, err error) {
129129
ids := k.LocksState(ctx).UnlockedIDsByAddress(account)
130130

131131
coins = sdk.NewCoins()
@@ -142,30 +142,30 @@ func (k LockupKeeper) UnlockAvailableCoins(ctx sdk.Context, account sdk.AccAddre
142142
}
143143

144144
// AccountLockedCoins returns the locked coins of the given sdk.AccAddress
145-
func (k LockupKeeper) AccountLockedCoins(ctx sdk.Context, account sdk.AccAddress) (coins sdk.Coins, err error) {
145+
func (k Keeper) AccountLockedCoins(ctx sdk.Context, account sdk.AccAddress) (coins sdk.Coins, err error) {
146146
return k.LocksState(ctx).IterateLockedCoins(account), nil
147147
}
148148

149149
// AccountUnlockedCoins returns the unlocked coins of the given sdk.AccAddress
150-
func (k LockupKeeper) AccountUnlockedCoins(ctx sdk.Context, account sdk.AccAddress) (coins sdk.Coins, err error) {
150+
func (k Keeper) AccountUnlockedCoins(ctx sdk.Context, account sdk.AccAddress) (coins sdk.Coins, err error) {
151151
return k.LocksState(ctx).IterateUnlockedCoins(account), nil
152152
}
153153

154154
// TotalLockedCoins returns the module account locked coins.
155-
func (k LockupKeeper) TotalLockedCoins(ctx sdk.Context) (coins sdk.Coins, err error) {
155+
func (k Keeper) TotalLockedCoins(ctx sdk.Context) (coins sdk.Coins, err error) {
156156
return k.LocksState(ctx).IterateTotalLockedCoins(), nil
157157
}
158158

159159
// LocksByDenom allows to iterate over types.Lock associated with a denom.
160160
// CONTRACT: no writes on store can happen until the function exits.
161-
func (k LockupKeeper) LocksByDenom(ctx sdk.Context, do func(lock *types.Lock) (stop bool)) (coins sdk.Coins, err error) {
161+
func (k Keeper) LocksByDenom(ctx sdk.Context, do func(lock *types.Lock) (stop bool)) (coins sdk.Coins, err error) {
162162
panic("impl")
163163
}
164164

165165
// LocksByDenomUnlockingAfter allows to iterate over types.Lock associated with a denom that unlock
166166
// after the provided duration.
167167
// CONTRACT: no writes on store can happen until the function exits.
168-
func (k LockupKeeper) LocksByDenomUnlockingAfter(ctx sdk.Context, denom string, duration time.Duration, do func(lock *types.Lock) (stop bool)) {
168+
func (k Keeper) LocksByDenomUnlockingAfter(ctx sdk.Context, denom string, duration time.Duration, do func(lock *types.Lock) (stop bool)) {
169169
endTime := ctx.BlockTime().Add(duration)
170170
state := k.LocksState(ctx)
171171
state.IterateCoinsByDenomUnlockingAfter(denom, endTime, func(id uint64) (stop bool) {

x/lockup/keeper/msg_server.go

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ import (
88
"github.com/NibiruChain/nibiru/x/lockup/types"
99
)
1010

11-
type msgServer struct {
12-
keeper *LockupKeeper
13-
}
14-
1511
// NewMsgServerImpl returns an instance of MsgServer.
16-
func NewMsgServerImpl(keeper *LockupKeeper) types.MsgServer {
12+
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
1713
return &msgServer{
1814
keeper: keeper,
1915
}
2016
}
2117

18+
type msgServer struct {
19+
keeper Keeper
20+
}
21+
2222
var _ types.MsgServer = msgServer{}
2323

2424
func (server msgServer) LockTokens(goCtx context.Context, msg *types.MsgLockTokens) (*types.MsgLockTokensResponse, error) {
@@ -43,3 +43,54 @@ func (server msgServer) InitiateUnlock(ctx context.Context, unlock *types.MsgIni
4343
_, err := server.keeper.UnlockTokens(sdkCtx, unlock.LockId)
4444
return &types.MsgInitiateUnlockResponse{}, err
4545
}
46+
47+
func NewQueryServerImpl(k Keeper) types.QueryServer {
48+
return queryServer{k: k}
49+
}
50+
51+
type queryServer struct {
52+
k Keeper
53+
}
54+
55+
func (q queryServer) LocksByAddress(ctx context.Context, address *types.QueryLocksByAddress) (*types.QueryLocksByAddressResponse, error) {
56+
// TODO(mercilex): make efficient with pagination
57+
sdkCtx := sdk.UnwrapSDKContext(ctx)
58+
addr, err := sdk.AccAddressFromBech32(address.Address)
59+
if err != nil {
60+
return nil, err
61+
}
62+
63+
var locks []*types.Lock
64+
state := q.k.LocksState(sdkCtx)
65+
state.IterateLocksByAddress(addr, func(id uint64) (stop bool) {
66+
lock, err := state.Get(id)
67+
if err != nil {
68+
panic(err)
69+
}
70+
locks = append(locks, lock)
71+
return false
72+
})
73+
74+
return &types.QueryLocksByAddressResponse{Locks: locks}, nil
75+
}
76+
77+
func (q queryServer) LockedCoins(ctx context.Context, request *types.QueryLockedCoinsRequest) (*types.QueryLockedCoinsResponse, error) {
78+
sdkCtx := sdk.UnwrapSDKContext(ctx)
79+
addr, err := sdk.AccAddressFromBech32(request.Address)
80+
if err != nil {
81+
return nil, err
82+
}
83+
coins := q.k.LocksState(sdkCtx).IterateLockedCoins(addr)
84+
return &types.QueryLockedCoinsResponse{LockedCoins: coins}, nil
85+
}
86+
87+
func (q queryServer) Lock(ctx context.Context, request *types.QueryLockRequest) (*types.QueryLockResponse, error) {
88+
sdkCtx := sdk.UnwrapSDKContext(ctx)
89+
90+
lock, err := q.k.LocksState(sdkCtx).Get(request.Id)
91+
if err != nil {
92+
return nil, err
93+
}
94+
95+
return &types.QueryLockResponse{Lock: lock}, nil
96+
}

0 commit comments

Comments
 (0)