nntaoli-project / goex

Cryptocurrency Exchange Rest API SDK For Golang Wrapper Support okx,huobi,binance
MIT License
1.81k stars 625 forks source link

panic while using OKX GetDepth with invalid symbol: slice bounds out of range [:-1] #233

Closed cantgetin closed 1 month ago

cantgetin commented 1 month ago

Hey. I was using OKX client GetDepth method like this, I provided wrong symbol like BTCUSDT instead of BTC-USDT and got panic. I think it should not panic and return error instead if something is wrong. It works fine with correct BTC-USDT symbol tho.

client := goex.OKx.Spot.NewPrvApi(
    options.WithApiKey(key),
    options.WithApiSecretKey(secret),
    options.WithPassphrase(passphrase))

res, _, err := client.GetDepth(model.CurrencyPair{
    Symbol: "BTCUSDT",
    }, 50)
if err != nil {
     return nil, nil, err
}

i got following panic:

panic: runtime error: slice bounds out of range [:-1]

goroutine 1 [running]:
github.com/nntaoli-project/goex/v2/okx/common.(*RespUnmarshaler).UnmarshalDepth(0x13e90e0, {0x0, 0x0, 0x0})
        C:/Users/Gasha/go/pkg/mod/github.com/nntaoli-project/goex/v2@v2.0.0-20240604105027-0c34c16cc5eb/okx/common/unmarshaler.go:23 +0xd0
github.com/nntaoli-project/goex/v2/okx/common.(*OKxV5).GetDepth(0xc000152000, {{0x10e07b3, 0x7}, {0x0, 0x0}, {0x0, 0x0}, 0x0, 0x0, 0x0, ...}, ...)
        C:/Users/Gasha/go/pkg/mod/github.com/nntaoli-project/goex/v2@v2.0.0-20240604105027-0c34c16cc5eb/okx/common/pub.go:28 +0x422
crypto-tradebot/internal/apis/okx.(*UseCase).GetOrderBookForSymbol(0xc00014d980?, {0x10e07b3?, 0x0?})
        C:/Users/Gasha/Desktop/crypto-tradebot/internal/apis/okx/okx.go:38 +0x6d
crypto-tradebot/internal/app.Run({0x10dffa4?, 0x4?}, 0xc00002e180, 0xc00012e680)
        C:/Users/Gasha/Desktop/crypto-tradebot/internal/app/app.go:25 +0x12e
main.main()
        C:/Users/Gasha/Desktop/crypto-tradebot/cmd/main.go:20 +0x125
nntaoli commented 1 month ago

The following interface code must be called first: _, _, err := goexv2.OKx.Spot.GetExchangeInfo()