Skip to content

Commit

Permalink
addrses comments
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen committed May 17, 2023
1 parent daee3ff commit d757584
Show file tree
Hide file tree
Showing 16 changed files with 908 additions and 161 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ require (
github.com/firefart/nonamedreturns v1.0.1 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/fzipp/gocyclo v0.5.1 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-critic/go-critic v0.6.3 // indirect
github.com/go-kit/kit v0.12.0 // indirect
github.com/go-kit/log v0.2.0 // indirect
Expand Down Expand Up @@ -142,6 +143,7 @@ require (
github.com/gostaticanalysis/nilerr v0.1.1 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2 // indirect
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
Expand Down Expand Up @@ -269,7 +271,7 @@ require (
replace (
github.com/CosmWasm/wasmd => github.com/sei-protocol/sei-wasmd v0.0.1
github.com/confio/ics23/go => github.com/cosmos/cosmos-sdk/ics23/go v0.8.0
github.com/cosmos/cosmos-sdk => github.com/sei-protocol/sei-cosmos v0.2.29-new-type
github.com/cosmos/cosmos-sdk => github.com/sei-protocol/sei-cosmos v0.2.30
github.com/cosmos/iavl => github.com/sei-protocol/sei-iavl v0.1.3
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
github.com/keybase/go-keychain => github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4
Expand Down
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ github.com/fullstorydev/grpcurl v1.6.0/go.mod h1:ZQ+ayqbKMJNhzLmbpCiurTVlaK2M/3n
github.com/fzipp/gocyclo v0.5.1 h1:L66amyuYogbxl0j2U+vGqJXusPF2IkduvXLnYD5TFgw=
github.com/fzipp/gocyclo v0.5.1/go.mod h1:rXPyn8fnlpa0R2csP/31uerbiVBugk5whMdlyaLkLoA=
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff/go.mod h1:x7DCsMOv1taUwEWCzT4cmDeAkigA5/QCwUodaVOe8Ww=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
Expand Down Expand Up @@ -596,6 +597,8 @@ github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t
github.com/grpc-ecosystem/grpc-gateway v1.12.1/go.mod h1:8XEsbTttt/W+VvjtQhLACqCisSPWTxCZ7sBRjU6iH9c=
github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2 h1:gDLXvp5S9izjldquuoAhDzccbskOL6tDC5jMSyx3zxE=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2/go.mod h1:7pdNwVWBBHGiCxa9lAszqCJMbfTISJ7oMftp8+UGV08=
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c h1:6rhixN/i8ZofjG1Y75iExal34USq5p+wiN1tpie8IrU=
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c/go.mod h1:NMPJylDgVpX0MLRlPy15sqSwOFv/U1GZ2m21JhFfek0=
github.com/hashicorp/consul/api v1.3.0/go.mod h1:MmDNSzIMUjNpY/mQ398R4bk2FnqQLoPndWW5VkKPlCE=
Expand Down Expand Up @@ -1067,8 +1070,8 @@ github.com/seccomp/libseccomp-golang v0.9.2-0.20220502022130-f33da4d89646/go.mod
github.com/securego/gosec/v2 v2.11.0 h1:+PDkpzR41OI2jrw1q6AdXZCbsNGNGT7pQjal0H0cArI=
github.com/securego/gosec/v2 v2.11.0/go.mod h1:SX8bptShuG8reGC0XS09+a4H2BoWSJi+fscA+Pulbpo=
github.com/segmentio/fasthash v1.0.3/go.mod h1:waKX8l2N8yckOgmSsXJi7x1ZfdKZ4x7KRMzBtS3oedY=
github.com/sei-protocol/sei-cosmos v0.2.29-new-type h1:yOk+0lKm7eI7QAKXXRVvVM/ojFLTS3o8KLD8cQcs7uE=
github.com/sei-protocol/sei-cosmos v0.2.29-new-type/go.mod h1:HaL4lCXp1N4BITUzrT3m7ztLQ27ac4yWNdrnsAT3d+I=
github.com/sei-protocol/sei-cosmos v0.2.30 h1:a3Dm5r73NoGelaUCKig/xqp0NFGMdkDb7oQ7MA8zFY8=
github.com/sei-protocol/sei-cosmos v0.2.30/go.mod h1:HaL4lCXp1N4BITUzrT3m7ztLQ27ac4yWNdrnsAT3d+I=
github.com/sei-protocol/sei-iavl v0.1.3 h1:0hvW1NtmBlZ7ZkerQcM/n+2tFKg0vUlYWK8q/OeuCgw=
github.com/sei-protocol/sei-iavl v0.1.3/go.mod h1:7PfkEVT5dcoQE+s/9KWdoXJ8VVVP1QpYYPLdxlkSXFk=
github.com/sei-protocol/sei-tendermint v0.2.17 h1:TwOVyF8F+Of2G8UEt0ShHDFQcFNmbGy4/c5Uui5UHqg=
Expand Down
28 changes: 28 additions & 0 deletions proto/dex/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import "dex/asset_list.proto";
import "dex/pair.proto";
import "dex/order.proto";
import "dex/match_result.proto";
import "dex/enums.proto";
// this line is used by starport scaffolding # 1

option go_package = "github.com/sei-protocol/sei-chain/x/dex/types";
Expand Down Expand Up @@ -100,6 +101,8 @@ service Query {

rpc GetMatchResult(QueryGetMatchResultRequest) returns (QueryGetMatchResultResponse) {}

rpc GetOrderCount(QueryGetOrderCountRequest) returns (QueryGetOrderCountResponse) {}

// this line is used by starport scaffolding # 2
}

Expand Down Expand Up @@ -370,4 +373,29 @@ message QueryGetMatchResultResponse {
(gogoproto.jsontag) = "result"
];
}

message QueryGetOrderCountRequest {
string contractAddr = 1 [
(gogoproto.jsontag) = "contract_address"
];
string priceDenom = 2 [
(gogoproto.jsontag) = "price_denom"
];
string assetDenom = 3 [
(gogoproto.jsontag) = "asset_denom"
];
string price = 4 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.jsontag) = "price"
];
PositionDirection positionDirection = 5 [
(gogoproto.jsontag) = "position_direction"
];
}

message QueryGetOrderCountResponse {
uint64 count = 1 [
(gogoproto.jsontag) = "count"
];
}
// this line is used by starport scaffolding # 3
1 change: 1 addition & 0 deletions x/dex/client/cli/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func GetQueryCmd() *cobra.Command {
cmd.AddCommand(CmdGetOrders())
cmd.AddCommand(CmdGetOrdersByID())
cmd.AddCommand(CmdGetMatchResult())
cmd.AddCommand(CmdGetOrderCount())

// this line is used by starport scaffolding # 1

Expand Down
52 changes: 52 additions & 0 deletions x/dex/client/cli/query/query_get_order_count.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package query

import (
"context"
"fmt"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sei-protocol/sei-chain/x/dex/types"
"github.com/spf13/cobra"
)

func CmdGetOrderCount() *cobra.Command {
cmd := &cobra.Command{
Use: "get-order-count [contract] [price denom] [asset denom] [LONG|SHORT] [price]",
Short: "get number of orders at a price leve",
Args: cobra.ExactArgs(5),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

queryClient := types.NewQueryClient(clientCtx)

var direction types.PositionDirection
switch args[3] {
case "LONG":
direction = types.PositionDirection_LONG
case "SHORT":
direction = types.PositionDirection_SHORT
default:
return fmt.Errorf("unknown direction %s", args[3])
}
price := sdk.MustNewDecFromStr(args[4])
res, err := queryClient.GetOrderCount(context.Background(), &types.QueryGetOrderCountRequest{
ContractAddr: args[0],
PriceDenom: args[1],
AssetDenom: args[2],
PositionDirection: direction,
Price: &price,
})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
2 changes: 1 addition & 1 deletion x/dex/keeper/msgserver/msg_server_place_orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (k msgServer) PlaceOrders(goCtx context.Context, msg *types.MsgPlaceOrders)
idsInResp := []uint64{}
maxOrderPerPrice := k.GetMaxOrderPerPrice(ctx)
for _, order := range msg.GetOrders() {
if k.GetOrderCount(ctx, msg.GetContractAddr(), order.PriceDenom, order.AssetDenom, order.PositionDirection, order.Price) >= maxOrderPerPrice {
if k.GetOrderCountState(ctx, msg.GetContractAddr(), order.PriceDenom, order.AssetDenom, order.PositionDirection, order.Price) >= maxOrderPerPrice {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "order book already has more than %d orders for %s-%s-%s %s at %s", maxOrderPerPrice, msg.GetContractAddr(), order.PriceDenom, order.AssetDenom, order.PositionDirection, order.Price)
}
priceTicksize, found := k.Keeper.GetPriceTickSizeForPair(ctx, msg.GetContractAddr(), types.Pair{PriceDenom: order.PriceDenom, AssetDenom: order.AssetDenom})
Expand Down
5 changes: 3 additions & 2 deletions x/dex/keeper/msgserver/msg_server_register_pairs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ func (k msgServer) RegisterPairs(goCtx context.Context, msg *types.MsgRegisterPa
// Validation such that only the user who stored the code can register pairs
for _, batchPair := range msg.Batchcontractpair {
contractAddr := batchPair.ContractAddr
if uint64(len(k.GetAllRegisteredPairs(ctx, contractAddr))) >= maxPairsPerContract {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "contract %s already has %d pairs registered", contractAddr, maxPairsPerContract)
existingPairCount := uint64(len(k.GetAllRegisteredPairs(ctx, contractAddr)))
if existingPairCount >= maxPairsPerContract {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "contract %s already has %d pairs registered", contractAddr, existingPairCount)
}
contractInfo, err := k.GetContract(ctx, contractAddr)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions x/dex/keeper/order_count.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (k Keeper) SetOrderCount(ctx sdk.Context, contractAddr string, priceDenom s
return nil
}

func (k Keeper) GetOrderCount(ctx sdk.Context, contractAddr string, priceDenom string, assetDenom string, direction types.PositionDirection, price sdk.Dec) uint64 {
func (k Keeper) GetOrderCountState(ctx sdk.Context, contractAddr string, priceDenom string, assetDenom string, direction types.PositionDirection, price sdk.Dec) uint64 {
store := prefix.NewStore(
ctx.KVStore(k.storeKey),
types.OrderCountPrefix(contractAddr, priceDenom, assetDenom, direction == types.PositionDirection_LONG),
Expand All @@ -42,7 +42,7 @@ func (k Keeper) GetOrderCount(ctx sdk.Context, contractAddr string, priceDenom s
}

func (k Keeper) DecreaseOrderCount(ctx sdk.Context, contractAddr string, priceDenom string, assetDenom string, direction types.PositionDirection, price sdk.Dec, count uint64) error {
oldCount := k.GetOrderCount(ctx, contractAddr, priceDenom, assetDenom, direction, price)
oldCount := k.GetOrderCountState(ctx, contractAddr, priceDenom, assetDenom, direction, price)
newCount := uint64(0)
if oldCount > count {
newCount = oldCount - count
Expand All @@ -51,6 +51,6 @@ func (k Keeper) DecreaseOrderCount(ctx sdk.Context, contractAddr string, priceDe
}

func (k Keeper) IncreaseOrderCount(ctx sdk.Context, contractAddr string, priceDenom string, assetDenom string, direction types.PositionDirection, price sdk.Dec, count uint64) error {
oldCount := k.GetOrderCount(ctx, contractAddr, priceDenom, assetDenom, direction, price)
oldCount := k.GetOrderCountState(ctx, contractAddr, priceDenom, assetDenom, direction, price)
return k.SetOrderCount(ctx, contractAddr, priceDenom, assetDenom, direction, price, oldCount+count)
}
17 changes: 15 additions & 2 deletions x/dex/keeper/order_count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,21 @@ func TestGetSetOrderCount(t *testing.T) {
types.PositionDirection_LONG,
types.PositionDirection_SHORT,
} {
require.Equal(t, uint64(0), keeper.GetOrderCount(ctx, keepertest.TestContract, keepertest.TestPair.PriceDenom, keepertest.TestPair.AssetDenom, direction, sdk.NewDec(1)))
require.Equal(t, uint64(0), keeper.GetOrderCountState(ctx, keepertest.TestContract, keepertest.TestPair.PriceDenom, keepertest.TestPair.AssetDenom, direction, sdk.NewDec(1)))
require.Nil(t, keeper.SetOrderCount(ctx, keepertest.TestContract, keepertest.TestPair.PriceDenom, keepertest.TestPair.AssetDenom, direction, sdk.NewDec(1), 5))
require.Equal(t, uint64(5), keeper.GetOrderCount(ctx, keepertest.TestContract, keepertest.TestPair.PriceDenom, keepertest.TestPair.AssetDenom, direction, sdk.NewDec(1)))
require.Equal(t, uint64(5), keeper.GetOrderCountState(ctx, keepertest.TestContract, keepertest.TestPair.PriceDenom, keepertest.TestPair.AssetDenom, direction, sdk.NewDec(1)))
}
}

func TestIncreaseOrderCount(t *testing.T) {
keeper, ctx := keepertest.DexKeeper(t)
keeper.IncreaseOrderCount(ctx, keepertest.TestContract, keepertest.TestPair.PriceDenom, keepertest.TestPair.AssetDenom, types.PositionDirection_LONG, sdk.NewDec(1), 10)
require.Equal(t, uint64(10), keeper.GetOrderCountState(ctx, keepertest.TestContract, keepertest.TestPair.PriceDenom, keepertest.TestPair.AssetDenom, types.PositionDirection_LONG, sdk.NewDec(1)))
}

func TestDecreaseOrderCount(t *testing.T) {
keeper, ctx := keepertest.DexKeeper(t)
require.Nil(t, keeper.SetOrderCount(ctx, keepertest.TestContract, keepertest.TestPair.PriceDenom, keepertest.TestPair.AssetDenom, types.PositionDirection_LONG, sdk.NewDec(1), 10))
keeper.DecreaseOrderCount(ctx, keepertest.TestContract, keepertest.TestPair.PriceDenom, keepertest.TestPair.AssetDenom, types.PositionDirection_LONG, sdk.NewDec(1), 5)
require.Equal(t, uint64(5), keeper.GetOrderCountState(ctx, keepertest.TestContract, keepertest.TestPair.PriceDenom, keepertest.TestPair.AssetDenom, types.PositionDirection_LONG, sdk.NewDec(1)))
}
19 changes: 19 additions & 0 deletions x/dex/keeper/query/grpc_query_get_order_count.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package query

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sei-protocol/sei-chain/x/dex/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func (k KeeperWrapper) GetOrderCount(c context.Context, req *types.QueryGetOrderCountRequest) (*types.QueryGetOrderCountResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(c)

return &types.QueryGetOrderCountResponse{Count: k.GetOrderCountState(ctx, req.ContractAddr, req.PriceDenom, req.AssetDenom, req.PositionDirection, *req.Price)}, nil
}
29 changes: 29 additions & 0 deletions x/dex/keeper/query/grpc_query_get_order_count_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package query_test

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
keepertest "github.com/sei-protocol/sei-chain/testutil/keeper"
"github.com/sei-protocol/sei-chain/x/dex/keeper/query"
"github.com/sei-protocol/sei-chain/x/dex/types"
"github.com/stretchr/testify/require"
)

func TestGetOrderCount(t *testing.T) {
keeper, ctx := keepertest.DexKeeper(t)
wrapper := query.KeeperWrapper{Keeper: keeper}
wctx := sdk.WrapSDKContext(ctx)
keeper.SetOrderCount(ctx, keepertest.TestContract, keepertest.TestPair.PriceDenom, keepertest.TestPair.AssetDenom, types.PositionDirection_LONG, sdk.NewDec(1), 5)
price := sdk.NewDec(1)
query := types.QueryGetOrderCountRequest{
ContractAddr: keepertest.TestContract,
PriceDenom: keepertest.TestPriceDenom,
AssetDenom: keepertest.TestAssetDenom,
PositionDirection: types.PositionDirection_LONG,
Price: &price,
}
resp, err := wrapper.GetOrderCount(wctx, &query)
require.Nil(t, err)
require.Equal(t, uint64(5), resp.Count)
}
15 changes: 15 additions & 0 deletions x/dex/migrations/v14_to_v15.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package migrations

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sei-protocol/sei-chain/x/dex/keeper"
"github.com/sei-protocol/sei-chain/x/dex/types"
)

func V14ToV15(ctx sdk.Context, dexkeeper keeper.Keeper) error {
// This isn't the cleanest migration since it could potentially revert any dex params we have changed
// but we haven't, so we'll just do this.
defaultParams := types.DefaultParams()
dexkeeper.SetParams(ctx, defaultParams)
return nil
}
24 changes: 24 additions & 0 deletions x/dex/migrations/v14_to_v15_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package migrations_test

import (
"testing"

keepertest "github.com/sei-protocol/sei-chain/testutil/keeper"
"github.com/sei-protocol/sei-chain/x/dex/migrations"
"github.com/sei-protocol/sei-chain/x/dex/types"
"github.com/stretchr/testify/require"
)

func TestMigrate14to15(t *testing.T) {
dexkeeper, ctx := keepertest.DexKeeper(t)
// write old params
prevParams := types.Params{}
dexkeeper.SetParams(ctx, prevParams)

// migrate to default params
err := migrations.V14ToV15(ctx, *dexkeeper)
require.NoError(t, err)
params := dexkeeper.GetParams(ctx)
require.Equal(t, params.MaxOrderPerPrice, uint64(types.DefaultMaxOrderPerPrice))
require.Equal(t, params.MaxPairsPerContract, uint64(types.DefaultMaxPairsPerContract))
}
5 changes: 4 additions & 1 deletion x/dex/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
_ = cfg.RegisterMigration(types.ModuleName, 13, func(ctx sdk.Context) error {
return migrations.V13ToV14(ctx, am.keeper)
})
_ = cfg.RegisterMigration(types.ModuleName, 14, func(ctx sdk.Context) error {
return migrations.V14ToV15(ctx, am.keeper)
})
}

// RegisterInvariants registers the capability module's invariants.
Expand All @@ -227,7 +230,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion implements ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 14 }
func (AppModule) ConsensusVersion() uint64 { return 15 }

func (am AppModule) getAllContractInfo(ctx sdk.Context) []types.ContractInfoV2 {
// Do not process any contract that has zero rent balance
Expand Down
Loading

0 comments on commit d757584

Please sign in to comment.