Skip to content

Commit

Permalink
add: lockup query server (#442)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
5 people authored May 22, 2022
1 parent 40e064d commit 883dceb
Show file tree
Hide file tree
Showing 13 changed files with 1,418 additions and 107 deletions.
4 changes: 2 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ type NibiruApp struct {
PerpKeeper perpkeeper.Keeper
PriceKeeper pricekeeper.Keeper
EpochsKeeper epochskeeper.Keeper
LockupKeeper lockupkeeper.LockupKeeper
LockupKeeper lockupkeeper.Keeper
IncentivizationKeeper incentivizationkeeper.Keeper
VpoolKeeper vpoolkeeper.Keeper

Expand Down Expand Up @@ -410,7 +410,7 @@ func NewNibiruApp(
appCodec, app.StablecoinKeeper, app.AccountKeeper, app.BankKeeper,
app.PriceKeeper,
)
lockupModule := lockup.NewAppModule(appCodec, app.GovKeeper, app.AccountKeeper, app.BankKeeper)
lockupModule := lockup.NewAppModule(appCodec, app.LockupKeeper, app.AccountKeeper, app.BankKeeper)
perpModule := perp.NewAppModule(
appCodec, app.PerpKeeper, app.AccountKeeper, app.BankKeeper,
app.PriceKeeper,
Expand Down
27 changes: 27 additions & 0 deletions proto/lockup/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ package nibiru.lockup.v1;
import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "cosmos/base/v1beta1/coin.proto";
import "cosmos/base/query/v1beta1/pagination.proto";

import "lockup/v1/lock.proto";

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

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

rpc Lock(QueryLockRequest) returns (QueryLockResponse) {
option (google.api.http).get = "/nibiru/lockup/lock";
}

rpc LocksByAddress(QueryLocksByAddress) returns (QueryLocksByAddressResponse) {
option (google.api.http).get = "/nibiru/lockup/locks_by_address";
};
}

message QueryLockedCoinsRequest {
Expand All @@ -23,4 +33,21 @@ message QueryLockedCoinsResponse {
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
];
}

message QueryLockRequest {
uint64 id = 1;
}

message QueryLockResponse {
nibiru.lockup.v1.Lock lock = 1;
}

message QueryLocksByAddress {
string address = 1;
cosmos.base.query.v1beta1.PageRequest pagination = 2;
}

message QueryLocksByAddressResponse {
repeated nibiru.lockup.v1.Lock locks = 1;
}
4 changes: 2 additions & 2 deletions x/incentivization/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const (
FundsModuleAccountAddressPrefix = "incentivization_escrow_"
)

func NewKeeper(cdc codec.Codec, storeKey sdk.StoreKey, ak authkeeper.AccountKeeper, bk bankkeeper.Keeper, dk dexkeeper.Keeper, lk lockupkeeper.LockupKeeper) Keeper {
func NewKeeper(cdc codec.Codec, storeKey sdk.StoreKey, ak authkeeper.AccountKeeper, bk bankkeeper.Keeper, dk dexkeeper.Keeper, lk lockupkeeper.Keeper) Keeper {
return Keeper{
cdc: cdc,
storeKey: storeKey,
Expand All @@ -49,7 +49,7 @@ type Keeper struct {
ak authkeeper.AccountKeeper
bk bankkeeper.Keeper
dk dexkeeper.Keeper
lk lockupkeeper.LockupKeeper
lk lockupkeeper.Keeper
}

func (k Keeper) CreateIncentivizationProgram(
Expand Down
4 changes: 2 additions & 2 deletions x/lockup/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
)

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

// Called every block to automatically unlock matured locks.
func EndBlocker(ctx sdk.Context, k keeper.LockupKeeper) []abci.ValidatorUpdate {
func EndBlocker(ctx sdk.Context, k keeper.Keeper) []abci.ValidatorUpdate {
return []abci.ValidatorUpdate{}
}
4 changes: 2 additions & 2 deletions x/lockup/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (

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

}

// ExportGenesis returns the capability module's exported genesis.
func ExportGenesis(ctx sdk.Context, k keeper.LockupKeeper) *types.GenesisState {
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
return &types.GenesisState{}
}
28 changes: 14 additions & 14 deletions x/lockup/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ var (
MaxTime = time.Unix(253402297199, 0).UTC()
)

// LockupKeeper provides a way to manage module storage.
type LockupKeeper struct {
// Keeper provides a way to manage module storage.
type Keeper struct {
cdc codec.Codec
storeKey sdk.StoreKey

Expand All @@ -34,8 +34,8 @@ type LockupKeeper struct {

// NewLockupKeeper returns an instance of Keeper.
func NewLockupKeeper(cdc codec.Codec, storeKey sdk.StoreKey, ak types.AccountKeeper,
bk types.BankKeeper, dk types.DistrKeeper) LockupKeeper {
return LockupKeeper{
bk types.BankKeeper, dk types.DistrKeeper) Keeper {
return Keeper{
cdc: cdc,
storeKey: storeKey,
ak: ak,
Expand All @@ -45,12 +45,12 @@ func NewLockupKeeper(cdc codec.Codec, storeKey sdk.StoreKey, ak types.AccountKee
}

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

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

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

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

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

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

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

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

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

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

// LocksByDenomUnlockingAfter allows to iterate over types.Lock associated with a denom that unlock
// after the provided duration.
// CONTRACT: no writes on store can happen until the function exits.
func (k LockupKeeper) LocksByDenomUnlockingAfter(ctx sdk.Context, denom string, duration time.Duration, do func(lock *types.Lock) (stop bool)) {
func (k Keeper) LocksByDenomUnlockingAfter(ctx sdk.Context, denom string, duration time.Duration, do func(lock *types.Lock) (stop bool)) {
endTime := ctx.BlockTime().Add(duration)
state := k.LocksState(ctx)
state.IterateCoinsByDenomUnlockingAfter(denom, endTime, func(id uint64) (stop bool) {
Expand Down
61 changes: 56 additions & 5 deletions x/lockup/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import (
"github.com/NibiruChain/nibiru/x/lockup/types"
)

type msgServer struct {
keeper *LockupKeeper
}

// NewMsgServerImpl returns an instance of MsgServer.
func NewMsgServerImpl(keeper *LockupKeeper) types.MsgServer {
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
return &msgServer{
keeper: keeper,
}
}

type msgServer struct {
keeper Keeper
}

var _ types.MsgServer = msgServer{}

func (server msgServer) LockTokens(goCtx context.Context, msg *types.MsgLockTokens) (*types.MsgLockTokensResponse, error) {
Expand All @@ -43,3 +43,54 @@ func (server msgServer) InitiateUnlock(ctx context.Context, unlock *types.MsgIni
_, err := server.keeper.UnlockTokens(sdkCtx, unlock.LockId)
return &types.MsgInitiateUnlockResponse{}, err
}

func NewQueryServerImpl(k Keeper) types.QueryServer {
return queryServer{k: k}
}

type queryServer struct {
k Keeper
}

func (q queryServer) LocksByAddress(ctx context.Context, address *types.QueryLocksByAddress) (*types.QueryLocksByAddressResponse, error) {
// TODO(mercilex): make efficient with pagination
sdkCtx := sdk.UnwrapSDKContext(ctx)
addr, err := sdk.AccAddressFromBech32(address.Address)
if err != nil {
return nil, err
}

var locks []*types.Lock
state := q.k.LocksState(sdkCtx)
state.IterateLocksByAddress(addr, func(id uint64) (stop bool) {
lock, err := state.Get(id)
if err != nil {
panic(err)
}
locks = append(locks, lock)
return false
})

return &types.QueryLocksByAddressResponse{Locks: locks}, nil
}

func (q queryServer) LockedCoins(ctx context.Context, request *types.QueryLockedCoinsRequest) (*types.QueryLockedCoinsResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
addr, err := sdk.AccAddressFromBech32(request.Address)
if err != nil {
return nil, err
}
coins := q.k.LocksState(sdkCtx).IterateLockedCoins(addr)
return &types.QueryLockedCoinsResponse{LockedCoins: coins}, nil
}

func (q queryServer) Lock(ctx context.Context, request *types.QueryLockRequest) (*types.QueryLockResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)

lock, err := q.k.LocksState(sdkCtx).Get(request.Id)
if err != nil {
return nil, err
}

return &types.QueryLockResponse{Lock: lock}, nil
}
Loading

0 comments on commit 883dceb

Please sign in to comment.