Skip to content

Commit

Permalink
[incentivization] add: query server (#464)
Browse files Browse the repository at this point in the history
* add: query server

* chore: lint

* chore: cleanup UTC times

Co-authored-by: fdymylja <[email protected]>
Co-authored-by: Walter White <[email protected]>
  • Loading branch information
3 people authored May 23, 2022
1 parent 0e5b534 commit efccccc
Show file tree
Hide file tree
Showing 30 changed files with 1,371 additions and 181 deletions.
24 changes: 24 additions & 0 deletions proto/incentivization/v1/incentivization.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import "google/protobuf/duration.proto";
import "gogoproto/gogo.proto";
import "google/api/annotations.proto";

import "cosmos/base/query/v1beta1/pagination.proto";
import "cosmos/base/v1beta1/coin.proto";


Expand Down Expand Up @@ -95,5 +96,28 @@ message GenesisState {
}

service Query {
rpc IncentivizationProgram(QueryIncentivizationProgramRequest) returns (QueryIncentivizationProgramResponse){
option (google.api.http).get = "/nibiru/incentivization/v1/incentivization_program";
};

rpc IncentivizationPrograms(QueryIncentivizationProgramsRequest) returns (QueryIncentivizationProgramsResponse) {
option (google.api.http).get = "/nibiru/incentivization/v1/incentivization_programs";
}
}

message QueryIncentivizationProgramRequest {
uint64 id = 1;
}

message QueryIncentivizationProgramResponse {
IncentivizationProgram incentivization_program = 1;
}

message QueryIncentivizationProgramsRequest {
cosmos.base.query.v1beta1.PageRequest pagination = 1;
}

message QueryIncentivizationProgramsResponse {
repeated IncentivizationProgram incentivization_programs = 1;
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}
5 changes: 3 additions & 2 deletions x/dex/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions x/dex/types/params.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions x/dex/types/pool.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions x/dex/types/query.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions x/dex/types/tx.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions x/epochs/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions x/epochs/types/query.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 35 additions & 1 deletion x/incentivization/keeper/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package keeper
import (
"context"

"github.com/cosmos/cosmos-sdk/store/prefix"
"github.com/cosmos/cosmos-sdk/types/query"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/NibiruChain/nibiru/x/incentivization/types"
Expand Down Expand Up @@ -69,5 +72,36 @@ func NewQueryServer(k Keeper) types.QueryServer {
}

type queryServer struct {
Keeper
k Keeper
}

func (q queryServer) IncentivizationProgram(ctx context.Context, request *types.QueryIncentivizationProgramRequest) (*types.QueryIncentivizationProgramResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
program, err := q.k.IncentivizationProgramsState(sdkCtx).Get(request.Id)
if err != nil {
return nil, err
}
return &types.QueryIncentivizationProgramResponse{IncentivizationProgram: program}, nil
}

func (q queryServer) IncentivizationPrograms(ctx context.Context, request *types.QueryIncentivizationProgramsRequest) (*types.QueryIncentivizationProgramsResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
store := prefix.NewStore(sdkCtx.KVStore(q.k.storeKey), incentivizationProgramObjectsNamespace)

var programs []*types.IncentivizationProgram
pageResp, err := query.Paginate(store, request.Pagination, func(key []byte, value []byte) error {
bytes := store.Get(key)
program := new(types.IncentivizationProgram)
q.k.cdc.MustUnmarshal(bytes, program)
programs = append(programs, program)
return nil
})
if err != nil {
return nil, err
}

return &types.QueryIncentivizationProgramsResponse{
IncentivizationPrograms: programs,
Pagination: pageResp,
}, nil
}
56 changes: 56 additions & 0 deletions x/incentivization/keeper/servers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package keeper_test

import (
"testing"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/stretchr/testify/require"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/NibiruChain/nibiru/x/incentivization/keeper"
"github.com/NibiruChain/nibiru/x/incentivization/types"
"github.com/NibiruChain/nibiru/x/testutil"
)

func TestQueryServer_IncentivizationProgram(t *testing.T) {
app := testutil.NewTestApp(false)
q := keeper.NewQueryServer(app.IncentivizationKeeper)
ctx := app.NewContext(false, tmproto.Header{Time: time.Now()})

// init
program, err := app.IncentivizationKeeper.CreateIncentivizationProgram(ctx, "lp", 24*time.Hour, time.Now().Add(1*time.Second).UTC(), 100)
require.NoError(t, err)

resp, err := q.IncentivizationProgram(sdk.WrapSDKContext(ctx), &types.QueryIncentivizationProgramRequest{})
require.NoError(t, err)
require.Equal(t, program, resp.IncentivizationProgram)
}

func TestQueryServer_IncentivizationPrograms(t *testing.T) {
app := testutil.NewTestApp(false)
q := keeper.NewQueryServer(app.IncentivizationKeeper)
ctx := app.NewContext(false, tmproto.Header{Time: time.Now()})

// init
_, err := app.IncentivizationKeeper.CreateIncentivizationProgram(ctx, "lp", 24*time.Hour, time.Now().Add(1*time.Second).UTC(), 100)
require.NoError(t, err)

program2, err := app.IncentivizationKeeper.CreateIncentivizationProgram(ctx, "lp", 24*time.Hour, time.Now().Add(1*time.Second).UTC(), 100)
require.NoError(t, err)

program3, err := app.IncentivizationKeeper.CreateIncentivizationProgram(ctx, "lp", 24*time.Hour, time.Now().Add(1*time.Second).UTC(), 100)
require.NoError(t, err)

// query
resp, err := q.IncentivizationPrograms(sdk.WrapSDKContext(ctx), &types.QueryIncentivizationProgramsRequest{Pagination: &query.PageRequest{
Offset: 1,
Limit: 2,
}})
require.NoError(t, err)

require.Equal(t, []*types.IncentivizationProgram{
program2, program3,
}, resp.IncentivizationPrograms)
}
14 changes: 7 additions & 7 deletions x/incentivization/keeper/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ const (
)

var (
incentivizationProgramNamespace = []byte{0x0}
incentivizationProgramIDNamespace = append(incentivizationProgramNamespace, 0x0)
incentivizationProgramIDKey = []byte{0x1}
incentiviationProgramObjectNamespace = append(incentivizationProgramNamespace, 0x1)
incentivizationProgramDenomIndex = append(incentivizationProgramNamespace, 0x2)
incentivizationProgramDenomMap = append(incentivizationProgramNamespace, 0x3)
incentivizationProgramNamespace = []byte{0x0}
incentivizationProgramIDNamespace = append(incentivizationProgramNamespace, 0x0)
incentivizationProgramIDKey = []byte{0x1}
incentivizationProgramObjectsNamespace = append(incentivizationProgramNamespace, 0x1)
incentivizationProgramDenomIndex = append(incentivizationProgramNamespace, 0x2)
incentivizationProgramDenomMap = append(incentivizationProgramNamespace, 0x3)
)

func (k Keeper) IncentivizationProgramsState(ctx sdk.Context) IncentivizationProgramState {
Expand All @@ -31,7 +31,7 @@ func newIncentivizationProgramState(ctx sdk.Context, key sdk.StoreKey, cdc codec
cdc: cdc,
ctx: ctx,
programID: prefix.NewStore(store, incentivizationProgramIDNamespace),
incentivizationPrograms: prefix.NewStore(store, incentiviationProgramObjectNamespace),
incentivizationPrograms: prefix.NewStore(store, incentivizationProgramObjectsNamespace),
denomToIncentivizationProgramIndex: prefix.NewStore(store, incentivizationProgramDenomIndex),
denomMap: prefix.NewStore(store, incentivizationProgramDenomMap),
}
Expand Down
Loading

0 comments on commit efccccc

Please sign in to comment.