mirror of
https://github.com/colinear-labs/chain.git
synced 2026-03-05 13:24:26 -08:00
scaffold base auction & nextAuction types
This commit is contained in:
63
x/cosmostest/keeper/auction.go
Normal file
63
x/cosmostest/keeper/auction.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package keeper
|
||||
|
||||
import (
|
||||
"cosmos-test/x/cosmostest/types"
|
||||
"github.com/cosmos/cosmos-sdk/store/prefix"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
|
||||
// SetAuction set a specific auction in the store from its index
|
||||
func (k Keeper) SetAuction(ctx sdk.Context, auction types.Auction) {
|
||||
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.AuctionKeyPrefix))
|
||||
b := k.cdc.MustMarshal(&auction)
|
||||
store.Set(types.AuctionKey(
|
||||
auction.Index,
|
||||
), b)
|
||||
}
|
||||
|
||||
// GetAuction returns a auction from its index
|
||||
func (k Keeper) GetAuction(
|
||||
ctx sdk.Context,
|
||||
index string,
|
||||
|
||||
) (val types.Auction, found bool) {
|
||||
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.AuctionKeyPrefix))
|
||||
|
||||
b := store.Get(types.AuctionKey(
|
||||
index,
|
||||
))
|
||||
if b == nil {
|
||||
return val, false
|
||||
}
|
||||
|
||||
k.cdc.MustUnmarshal(b, &val)
|
||||
return val, true
|
||||
}
|
||||
|
||||
// RemoveAuction removes a auction from the store
|
||||
func (k Keeper) RemoveAuction(
|
||||
ctx sdk.Context,
|
||||
index string,
|
||||
|
||||
) {
|
||||
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.AuctionKeyPrefix))
|
||||
store.Delete(types.AuctionKey(
|
||||
index,
|
||||
))
|
||||
}
|
||||
|
||||
// GetAllAuction returns all auction
|
||||
func (k Keeper) GetAllAuction(ctx sdk.Context) (list []types.Auction) {
|
||||
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.AuctionKeyPrefix))
|
||||
iterator := sdk.KVStorePrefixIterator(store, []byte{})
|
||||
|
||||
defer iterator.Close()
|
||||
|
||||
for ; iterator.Valid(); iterator.Next() {
|
||||
var val types.Auction
|
||||
k.cdc.MustUnmarshal(iterator.Value(), &val)
|
||||
list = append(list, val)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
63
x/cosmostest/keeper/auction_test.go
Normal file
63
x/cosmostest/keeper/auction_test.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package keeper_test
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
keepertest "cosmos-test/testutil/keeper"
|
||||
"cosmos-test/testutil/nullify"
|
||||
"cosmos-test/x/cosmostest/keeper"
|
||||
"cosmos-test/x/cosmostest/types"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// Prevent strconv unused error
|
||||
var _ = strconv.IntSize
|
||||
|
||||
func createNAuction(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.Auction {
|
||||
items := make([]types.Auction, n)
|
||||
for i := range items {
|
||||
items[i].Index = strconv.Itoa(i)
|
||||
|
||||
keeper.SetAuction(ctx, items[i])
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
func TestAuctionGet(t *testing.T) {
|
||||
keeper, ctx := keepertest.CosmostestKeeper(t)
|
||||
items := createNAuction(keeper, ctx, 10)
|
||||
for _, item := range items {
|
||||
rst, found := keeper.GetAuction(ctx,
|
||||
item.Index,
|
||||
)
|
||||
require.True(t, found)
|
||||
require.Equal(t,
|
||||
nullify.Fill(&item),
|
||||
nullify.Fill(&rst),
|
||||
)
|
||||
}
|
||||
}
|
||||
func TestAuctionRemove(t *testing.T) {
|
||||
keeper, ctx := keepertest.CosmostestKeeper(t)
|
||||
items := createNAuction(keeper, ctx, 10)
|
||||
for _, item := range items {
|
||||
keeper.RemoveAuction(ctx,
|
||||
item.Index,
|
||||
)
|
||||
_, found := keeper.GetAuction(ctx,
|
||||
item.Index,
|
||||
)
|
||||
require.False(t, found)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuctionGetAll(t *testing.T) {
|
||||
keeper, ctx := keepertest.CosmostestKeeper(t)
|
||||
items := createNAuction(keeper, ctx, 10)
|
||||
require.ElementsMatch(t,
|
||||
nullify.Fill(items),
|
||||
nullify.Fill(keeper.GetAllAuction(ctx)),
|
||||
)
|
||||
}
|
||||
57
x/cosmostest/keeper/grpc_query_auction.go
Normal file
57
x/cosmostest/keeper/grpc_query_auction.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package keeper
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"cosmos-test/x/cosmostest/types"
|
||||
"github.com/cosmos/cosmos-sdk/store/prefix"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/types/query"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
func (k Keeper) AuctionAll(c context.Context, req *types.QueryAllAuctionRequest) (*types.QueryAllAuctionResponse, error) {
|
||||
if req == nil {
|
||||
return nil, status.Error(codes.InvalidArgument, "invalid request")
|
||||
}
|
||||
|
||||
var auctions []types.Auction
|
||||
ctx := sdk.UnwrapSDKContext(c)
|
||||
|
||||
store := ctx.KVStore(k.storeKey)
|
||||
auctionStore := prefix.NewStore(store, types.KeyPrefix(types.AuctionKeyPrefix))
|
||||
|
||||
pageRes, err := query.Paginate(auctionStore, req.Pagination, func(key []byte, value []byte) error {
|
||||
var auction types.Auction
|
||||
if err := k.cdc.Unmarshal(value, &auction); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
auctions = append(auctions, auction)
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, err.Error())
|
||||
}
|
||||
|
||||
return &types.QueryAllAuctionResponse{Auction: auctions, Pagination: pageRes}, nil
|
||||
}
|
||||
|
||||
func (k Keeper) Auction(c context.Context, req *types.QueryGetAuctionRequest) (*types.QueryGetAuctionResponse, error) {
|
||||
if req == nil {
|
||||
return nil, status.Error(codes.InvalidArgument, "invalid request")
|
||||
}
|
||||
ctx := sdk.UnwrapSDKContext(c)
|
||||
|
||||
val, found := k.GetAuction(
|
||||
ctx,
|
||||
req.Index,
|
||||
)
|
||||
if !found {
|
||||
return nil, status.Error(codes.NotFound, "not found")
|
||||
}
|
||||
|
||||
return &types.QueryGetAuctionResponse{Auction: val}, nil
|
||||
}
|
||||
126
x/cosmostest/keeper/grpc_query_auction_test.go
Normal file
126
x/cosmostest/keeper/grpc_query_auction_test.go
Normal file
@@ -0,0 +1,126 @@
|
||||
package keeper_test
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/types/query"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
keepertest "cosmos-test/testutil/keeper"
|
||||
"cosmos-test/testutil/nullify"
|
||||
"cosmos-test/x/cosmostest/types"
|
||||
)
|
||||
|
||||
// Prevent strconv unused error
|
||||
var _ = strconv.IntSize
|
||||
|
||||
func TestAuctionQuerySingle(t *testing.T) {
|
||||
keeper, ctx := keepertest.CosmostestKeeper(t)
|
||||
wctx := sdk.WrapSDKContext(ctx)
|
||||
msgs := createNAuction(keeper, ctx, 2)
|
||||
for _, tc := range []struct {
|
||||
desc string
|
||||
request *types.QueryGetAuctionRequest
|
||||
response *types.QueryGetAuctionResponse
|
||||
err error
|
||||
}{
|
||||
{
|
||||
desc: "First",
|
||||
request: &types.QueryGetAuctionRequest{
|
||||
Index: msgs[0].Index,
|
||||
},
|
||||
response: &types.QueryGetAuctionResponse{Auction: msgs[0]},
|
||||
},
|
||||
{
|
||||
desc: "Second",
|
||||
request: &types.QueryGetAuctionRequest{
|
||||
Index: msgs[1].Index,
|
||||
},
|
||||
response: &types.QueryGetAuctionResponse{Auction: msgs[1]},
|
||||
},
|
||||
{
|
||||
desc: "KeyNotFound",
|
||||
request: &types.QueryGetAuctionRequest{
|
||||
Index: strconv.Itoa(100000),
|
||||
},
|
||||
err: status.Error(codes.NotFound, "not found"),
|
||||
},
|
||||
{
|
||||
desc: "InvalidRequest",
|
||||
err: status.Error(codes.InvalidArgument, "invalid request"),
|
||||
},
|
||||
} {
|
||||
t.Run(tc.desc, func(t *testing.T) {
|
||||
response, err := keeper.Auction(wctx, tc.request)
|
||||
if tc.err != nil {
|
||||
require.ErrorIs(t, err, tc.err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
require.Equal(t,
|
||||
nullify.Fill(tc.response),
|
||||
nullify.Fill(response),
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuctionQueryPaginated(t *testing.T) {
|
||||
keeper, ctx := keepertest.CosmostestKeeper(t)
|
||||
wctx := sdk.WrapSDKContext(ctx)
|
||||
msgs := createNAuction(keeper, ctx, 5)
|
||||
|
||||
request := func(next []byte, offset, limit uint64, total bool) *types.QueryAllAuctionRequest {
|
||||
return &types.QueryAllAuctionRequest{
|
||||
Pagination: &query.PageRequest{
|
||||
Key: next,
|
||||
Offset: offset,
|
||||
Limit: limit,
|
||||
CountTotal: total,
|
||||
},
|
||||
}
|
||||
}
|
||||
t.Run("ByOffset", func(t *testing.T) {
|
||||
step := 2
|
||||
for i := 0; i < len(msgs); i += step {
|
||||
resp, err := keeper.AuctionAll(wctx, request(nil, uint64(i), uint64(step), false))
|
||||
require.NoError(t, err)
|
||||
require.LessOrEqual(t, len(resp.Auction), step)
|
||||
require.Subset(t,
|
||||
nullify.Fill(msgs),
|
||||
nullify.Fill(resp.Auction),
|
||||
)
|
||||
}
|
||||
})
|
||||
t.Run("ByKey", func(t *testing.T) {
|
||||
step := 2
|
||||
var next []byte
|
||||
for i := 0; i < len(msgs); i += step {
|
||||
resp, err := keeper.AuctionAll(wctx, request(next, 0, uint64(step), false))
|
||||
require.NoError(t, err)
|
||||
require.LessOrEqual(t, len(resp.Auction), step)
|
||||
require.Subset(t,
|
||||
nullify.Fill(msgs),
|
||||
nullify.Fill(resp.Auction),
|
||||
)
|
||||
next = resp.Pagination.NextKey
|
||||
}
|
||||
})
|
||||
t.Run("Total", func(t *testing.T) {
|
||||
resp, err := keeper.AuctionAll(wctx, request(nil, 0, 0, true))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(msgs), int(resp.Pagination.Total))
|
||||
require.ElementsMatch(t,
|
||||
nullify.Fill(msgs),
|
||||
nullify.Fill(resp.Auction),
|
||||
)
|
||||
})
|
||||
t.Run("InvalidRequest", func(t *testing.T) {
|
||||
_, err := keeper.AuctionAll(wctx, nil)
|
||||
require.ErrorIs(t, err, status.Error(codes.InvalidArgument, "invalid request"))
|
||||
})
|
||||
}
|
||||
24
x/cosmostest/keeper/grpc_query_next_auction.go
Normal file
24
x/cosmostest/keeper/grpc_query_next_auction.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package keeper
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"cosmos-test/x/cosmostest/types"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
func (k Keeper) NextAuction(c context.Context, req *types.QueryGetNextAuctionRequest) (*types.QueryGetNextAuctionResponse, error) {
|
||||
if req == nil {
|
||||
return nil, status.Error(codes.InvalidArgument, "invalid request")
|
||||
}
|
||||
ctx := sdk.UnwrapSDKContext(c)
|
||||
|
||||
val, found := k.GetNextAuction(ctx)
|
||||
if !found {
|
||||
return nil, status.Error(codes.NotFound, "not found")
|
||||
}
|
||||
|
||||
return &types.QueryGetNextAuctionResponse{NextAuction: val}, nil
|
||||
}
|
||||
49
x/cosmostest/keeper/grpc_query_next_auction_test.go
Normal file
49
x/cosmostest/keeper/grpc_query_next_auction_test.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package keeper_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
keepertest "cosmos-test/testutil/keeper"
|
||||
"cosmos-test/testutil/nullify"
|
||||
"cosmos-test/x/cosmostest/types"
|
||||
)
|
||||
|
||||
func TestNextAuctionQuery(t *testing.T) {
|
||||
keeper, ctx := keepertest.CosmostestKeeper(t)
|
||||
wctx := sdk.WrapSDKContext(ctx)
|
||||
item := createTestNextAuction(keeper, ctx)
|
||||
for _, tc := range []struct {
|
||||
desc string
|
||||
request *types.QueryGetNextAuctionRequest
|
||||
response *types.QueryGetNextAuctionResponse
|
||||
err error
|
||||
}{
|
||||
{
|
||||
desc: "First",
|
||||
request: &types.QueryGetNextAuctionRequest{},
|
||||
response: &types.QueryGetNextAuctionResponse{NextAuction: item},
|
||||
},
|
||||
{
|
||||
desc: "InvalidRequest",
|
||||
err: status.Error(codes.InvalidArgument, "invalid request"),
|
||||
},
|
||||
} {
|
||||
t.Run(tc.desc, func(t *testing.T) {
|
||||
response, err := keeper.NextAuction(wctx, tc.request)
|
||||
if tc.err != nil {
|
||||
require.ErrorIs(t, err, tc.err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
require.Equal(t,
|
||||
nullify.Fill(tc.response),
|
||||
nullify.Fill(response),
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
33
x/cosmostest/keeper/next_auction.go
Normal file
33
x/cosmostest/keeper/next_auction.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package keeper
|
||||
|
||||
import (
|
||||
"cosmos-test/x/cosmostest/types"
|
||||
"github.com/cosmos/cosmos-sdk/store/prefix"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
|
||||
// SetNextAuction set nextAuction in the store
|
||||
func (k Keeper) SetNextAuction(ctx sdk.Context, nextAuction types.NextAuction) {
|
||||
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.NextAuctionKey))
|
||||
b := k.cdc.MustMarshal(&nextAuction)
|
||||
store.Set([]byte{0}, b)
|
||||
}
|
||||
|
||||
// GetNextAuction returns nextAuction
|
||||
func (k Keeper) GetNextAuction(ctx sdk.Context) (val types.NextAuction, found bool) {
|
||||
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.NextAuctionKey))
|
||||
|
||||
b := store.Get([]byte{0})
|
||||
if b == nil {
|
||||
return val, false
|
||||
}
|
||||
|
||||
k.cdc.MustUnmarshal(b, &val)
|
||||
return val, true
|
||||
}
|
||||
|
||||
// RemoveNextAuction removes nextAuction from the store
|
||||
func (k Keeper) RemoveNextAuction(ctx sdk.Context) {
|
||||
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.NextAuctionKey))
|
||||
store.Delete([]byte{0})
|
||||
}
|
||||
38
x/cosmostest/keeper/next_auction_test.go
Normal file
38
x/cosmostest/keeper/next_auction_test.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package keeper_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
keepertest "cosmos-test/testutil/keeper"
|
||||
"cosmos-test/testutil/nullify"
|
||||
"cosmos-test/x/cosmostest/keeper"
|
||||
"cosmos-test/x/cosmostest/types"
|
||||
)
|
||||
|
||||
func createTestNextAuction(keeper *keeper.Keeper, ctx sdk.Context) types.NextAuction {
|
||||
item := types.NextAuction{}
|
||||
keeper.SetNextAuction(ctx, item)
|
||||
return item
|
||||
}
|
||||
|
||||
func TestNextAuctionGet(t *testing.T) {
|
||||
keeper, ctx := keepertest.CosmostestKeeper(t)
|
||||
item := createTestNextAuction(keeper, ctx)
|
||||
rst, found := keeper.GetNextAuction(ctx)
|
||||
require.True(t, found)
|
||||
require.Equal(t,
|
||||
nullify.Fill(&item),
|
||||
nullify.Fill(&rst),
|
||||
)
|
||||
}
|
||||
|
||||
func TestNextAuctionRemove(t *testing.T) {
|
||||
keeper, ctx := keepertest.CosmostestKeeper(t)
|
||||
createTestNextAuction(keeper, ctx)
|
||||
keeper.RemoveNextAuction(ctx)
|
||||
_, found := keeper.GetNextAuction(ctx)
|
||||
require.False(t, found)
|
||||
}
|
||||
Reference in New Issue
Block a user