Skip to content

Commit

Permalink
Merge pull request #639 from binance-chain/max_depth
Browse files Browse the repository at this point in the history
[R4R] add levels parameter to depth ABCI query
  • Loading branch information
ackratos authored Aug 12, 2019
2 parents 49b745e + 9798b33 commit 52adabd
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 28 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ BUG FIXES
* [\#641](https://github.com/binance-chain/node/pull/641) [Dex] Add max lock time in time lock plugin

IMPROVEMENTS
* [\#638](https://github.com/binance-chain/node/pull/638) [Pub] BEP39 - add memo to transfer kafka message
* [\#638](https://github.com/binance-chain/node/pull/638) [Pub] BEP39 - add memo to transfer kafka message
* [\#639](https://github.com/binance-chain/node/pull/639) [ABCI] add levels parameter to depth ABCI query
25 changes: 21 additions & 4 deletions plugins/dex/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (
)

// TODO: improve, should be configurable
const MaxDepthLevels = 100 // matches UI requirement
const MaxDepthLevels = 1000 // matches UI requirement
const DefaultDepthLevels = 100 // matches UI requirement

func createAbciQueryHandler(keeper *DexKeeper) app.AbciQueryHandler {
return func(app app.ChainApp, req abci.RequestQuery, path []string) (res *abci.ResponseQuery) {
Expand Down Expand Up @@ -80,16 +81,32 @@ func createAbciQueryHandler(keeper *DexKeeper) app.AbciQueryHandler {
Value: bz,
}
case "orderbook": // args: ["dex", "orderbook"]
//TODO: sync lock, validate pair, level number
//TODO: sync lock, validate pair
if len(path) < 3 {
return &abci.ResponseQuery{
Code: uint32(sdk.CodeUnknownRequest),
Log: "OrderBook query requires the pair symbol",
Log: "OrderBook query requires the pair symbol and levels",
}
}
pair := path[2]
height := app.GetContextForCheckState().BlockHeight()
levels := keeper.GetOrderBookLevels(pair, MaxDepthLevels)
levelLimit := DefaultDepthLevels
if len(path) == 4 {
if l, err := strconv.Atoi(path[3]); err != nil {
return &abci.ResponseQuery{
Code: uint32(sdk.CodeUnknownRequest),
Log: fmt.Sprintf("OrderBook query requires valid int levels parameter: %v", err),
}
} else if l <= 0 || l > MaxDepthLevels {
return &abci.ResponseQuery{
Code: uint32(sdk.CodeUnknownRequest),
Log: "OrderBook query requires valid levels (>0 && <1000)",
}
} else {
levelLimit = l
}
}
levels := keeper.GetOrderBookLevels(pair, levelLimit)
book := store.OrderBook{
Height: height,
Levels: levels,
Expand Down
1 change: 1 addition & 0 deletions plugins/dex/client/cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

const (
flagSymbol = "symbol"
flagLevels = "levels"
)

func AddCommands(cmd *cobra.Command, cdc *wire.Codec) {
Expand Down
8 changes: 7 additions & 1 deletion plugins/dex/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/binance-chain/node/common/client"
"github.com/binance-chain/node/common/types"
"github.com/binance-chain/node/common/utils"
"github.com/binance-chain/node/plugins/dex"
"github.com/binance-chain/node/plugins/dex/order"
"github.com/binance-chain/node/plugins/dex/store"
"github.com/binance-chain/node/wire"
Expand Down Expand Up @@ -95,6 +96,7 @@ func newOrderCmd(cdc *wire.Codec) *cobra.Command {
},
}
cmd.Flags().StringP(flagSymbol, "l", "", "the listed trading pair, such as ADA_BNB")
cmd.Flags().IntP(flagLevels, "L", 100, "maximum level (1,5,10,20,50,100,500,1000) to return")
cmd.Flags().StringP(flagSide, "s", "", "side (buy as 1 or sell as 2) of the order")
cmd.Flags().StringP(flagPrice, "p", "", "price for the order")
cmd.Flags().StringP(flagQty, "q", "", "quantity for the order")
Expand All @@ -114,8 +116,12 @@ func showOrderBookCmd(cdc *wire.Codec) *cobra.Command {
if err != nil {
return err
}
levelsLimit := viper.GetInt(flagLevels)
if levelsLimit <= 0 || levelsLimit > dex.MaxDepthLevels {
return fmt.Errorf("%s should be greater than 0 and not exceed %d", flagLevels, dex.MaxDepthLevels)
}

ob, err := store.GetOrderBook(cdc, ctx, symbol)
ob, err := store.GetOrderBook(cdc, ctx, symbol, levelsLimit)
if err != nil {
return err
}
Expand Down
33 changes: 15 additions & 18 deletions plugins/dex/client/rest/getdepth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"net/http"
"strconv"
"strings"

"github.com/cosmos/cosmos-sdk/client/context"

Expand All @@ -13,12 +12,10 @@ import (
"github.com/binance-chain/node/wire"
)

const allowedLimits = "5,10,20,50,100"
const defaultLimit = "100"
var allowedLimits = [7]int{5, 10, 20, 50, 100, 500, 1000}

// DepthReqHandler creates an http request handler to show market depth data
func DepthReqHandler(cdc *wire.Codec, ctx context.CLIContext) http.HandlerFunc {
allowedLimitsA := strings.Split(allowedLimits, ",")

type params struct {
symbol string
Expand All @@ -36,24 +33,24 @@ func DepthReqHandler(cdc *wire.Codec, ctx context.CLIContext) http.HandlerFunc {

return func(w http.ResponseWriter, r *http.Request) {
limitStr := r.FormValue("limit")
limit, err := strconv.Atoi(limitStr)
if err != nil {
throw(w, http.StatusExpectationFailed, errors.New("invalid limit, supported limits: [5,10,20,50,100,500,1000]"))
return
}

// validate limit param
limitStrOk := defaultLimit
for _, lmt := range allowedLimitsA {
if lmt == limitStr {
limitStrOk = limitStr
limitOk := -1
for _, lmt := range allowedLimits {
if lmt == limit {
limitOk = lmt
break
}
}

limit, _ := strconv.Atoi(defaultLimit)
if len(limitStrOk) > 0 {
var err error
limit, err = strconv.Atoi(limitStrOk)
if err != nil {
throw(w, http.StatusExpectationFailed, errors.New("invalid limit"))
return
}
if limitOk == -1 {
throw(w, http.StatusExpectationFailed, errors.New("invalid limit, supported limits: [5,10,20,50,100,500,1000]"))
return
}

// collect params
Expand All @@ -63,14 +60,14 @@ func DepthReqHandler(cdc *wire.Codec, ctx context.CLIContext) http.HandlerFunc {
}

// validate pair
err := store.ValidatePairSymbol(params.symbol)
err = store.ValidatePairSymbol(params.symbol)
if err != nil {
throw(w, http.StatusNotFound, err)
return
}

// query order book (includes block height)
ob, err := store.GetOrderBook(cdc, ctx, params.symbol)
ob, err := store.GetOrderBook(cdc, ctx, params.symbol, params.limit)
if err != nil {
throw(w, http.StatusInternalServerError, err)
return
Expand Down
8 changes: 4 additions & 4 deletions plugins/dex/store/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

// queryOrderBook queries the store for the serialized order book for a given pair.
func queryOrderBook(cdc *wire.Codec, ctx context.CLIContext, pair string) (*[]byte, error) {
bz, err := ctx.Query(fmt.Sprintf("dex/orderbook/%s", pair), nil)
func queryOrderBook(cdc *wire.Codec, ctx context.CLIContext, pair string, levels int) (*[]byte, error) {
bz, err := ctx.Query(fmt.Sprintf("dex/orderbook/%s/%d", pair, levels), nil)
if err != nil {
return nil, err
}
Expand All @@ -28,8 +28,8 @@ func decodeOrderBook(cdc *wire.Codec, bz *[]byte) (*OrderBook, error) {
}

// GetOrderBook decodes the order book from the serialized store
func GetOrderBook(cdc *wire.Codec, ctx context.CLIContext, pair string) (*OrderBook, error) {
bz, err := queryOrderBook(cdc, ctx, pair)
func GetOrderBook(cdc *wire.Codec, ctx context.CLIContext, pair string, levels int) (*OrderBook, error) {
bz, err := queryOrderBook(cdc, ctx, pair, levels)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 52adabd

Please sign in to comment.