2023-07-21 06:55:27 +00:00
|
|
|
package bybitapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
2023-08-09 03:41:04 +00:00
|
|
|
"time"
|
2023-07-21 06:55:27 +00:00
|
|
|
|
2023-07-26 13:40:39 +00:00
|
|
|
"github.com/google/uuid"
|
2023-07-21 06:55:27 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/testutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getTestClientOrSkip(t *testing.T) *RestClient {
|
|
|
|
if b, _ := strconv.ParseBool(os.Getenv("CI")); b {
|
|
|
|
t.Skip("skip test for CI")
|
|
|
|
}
|
|
|
|
|
|
|
|
key, secret, ok := testutil.IntegrationTestConfigured(t, "BYBIT")
|
|
|
|
if !ok {
|
|
|
|
t.Skip("BYBIT_* env vars are not configured")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := NewClient()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
client.Auth(key, secret)
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient(t *testing.T) {
|
|
|
|
client := getTestClientOrSkip(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
t.Run("GetAccountInfoRequest", func(t *testing.T) {
|
|
|
|
req := client.NewGetAccountRequest()
|
|
|
|
accountInfo, err := req.Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
t.Logf("accountInfo: %+v", accountInfo)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("GetInstrumentsInfoRequest", func(t *testing.T) {
|
|
|
|
req := client.NewGetInstrumentsInfoRequest()
|
|
|
|
instrumentsInfo, err := req.Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
t.Logf("instrumentsInfo: %+v", instrumentsInfo)
|
|
|
|
})
|
2023-07-24 15:27:43 +00:00
|
|
|
|
|
|
|
t.Run("GetTicker", func(t *testing.T) {
|
|
|
|
req := client.NewGetTickersRequest()
|
|
|
|
apiResp, err := req.Symbol("BTCUSDT").Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
t.Logf("apiResp: %+v", apiResp)
|
|
|
|
|
|
|
|
req = client.NewGetTickersRequest()
|
|
|
|
tickers, err := req.Symbol("BTCUSDT").DoWithResponseTime(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
t.Logf("tickers: %+v", tickers)
|
|
|
|
})
|
2023-07-25 13:30:49 +00:00
|
|
|
|
|
|
|
t.Run("GetOpenOrderRequest", func(t *testing.T) {
|
|
|
|
cursor := ""
|
|
|
|
for {
|
|
|
|
req := client.NewGetOpenOrderRequest().Limit(1)
|
|
|
|
if len(cursor) != 0 {
|
|
|
|
req = req.Cursor(cursor)
|
|
|
|
}
|
|
|
|
openOrders, err := req.Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
for _, o := range openOrders.List {
|
|
|
|
t.Logf("openOrders: %+v", o)
|
|
|
|
}
|
|
|
|
if len(openOrders.NextPageCursor) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
cursor = openOrders.NextPageCursor
|
|
|
|
}
|
|
|
|
})
|
2023-07-26 13:40:39 +00:00
|
|
|
|
2023-07-27 02:31:24 +00:00
|
|
|
t.Run("PlaceOrderRequest", func(t *testing.T) {
|
2023-07-26 13:40:39 +00:00
|
|
|
req := client.NewPlaceOrderRequest().
|
|
|
|
Symbol("DOTUSDT").
|
|
|
|
Side(SideBuy).
|
|
|
|
OrderType(OrderTypeLimit).
|
|
|
|
Qty("1").
|
|
|
|
Price("4.6").
|
|
|
|
OrderLinkId(uuid.NewString()).
|
|
|
|
TimeInForce(TimeInForceGTC)
|
|
|
|
apiResp, err := req.Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
t.Logf("apiResp: %+v", apiResp)
|
|
|
|
|
2023-07-27 09:39:50 +00:00
|
|
|
_, err = strconv.ParseUint(apiResp.OrderId, 10, 64)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2023-07-26 13:40:39 +00:00
|
|
|
ordersResp, err := client.NewGetOpenOrderRequest().OrderLinkId(apiResp.OrderLinkId).Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, len(ordersResp.List), 1)
|
|
|
|
t.Logf("apiResp: %+v", ordersResp.List[0])
|
|
|
|
})
|
2023-07-26 14:24:20 +00:00
|
|
|
|
2023-07-27 02:31:24 +00:00
|
|
|
t.Run("CancelOrderRequest", func(t *testing.T) {
|
2023-07-26 14:24:20 +00:00
|
|
|
req := client.NewPlaceOrderRequest().
|
|
|
|
Symbol("DOTUSDT").
|
|
|
|
Side(SideBuy).
|
|
|
|
OrderType(OrderTypeLimit).
|
|
|
|
Qty("1").
|
|
|
|
Price("4.6").
|
|
|
|
OrderLinkId(uuid.NewString()).
|
|
|
|
TimeInForce(TimeInForceGTC)
|
|
|
|
apiResp, err := req.Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
t.Logf("apiResp: %+v", apiResp)
|
|
|
|
|
|
|
|
ordersResp, err := client.NewGetOpenOrderRequest().OrderLinkId(apiResp.OrderLinkId).Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, len(ordersResp.List), 1)
|
|
|
|
t.Logf("apiResp: %+v", ordersResp.List[0])
|
|
|
|
|
|
|
|
cancelReq := client.NewCancelOrderRequest().
|
|
|
|
Symbol("DOTUSDT").
|
|
|
|
OrderLinkId(apiResp.OrderLinkId)
|
|
|
|
cancelResp, err := cancelReq.Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
t.Logf("apiResp: %+v", cancelResp)
|
|
|
|
|
|
|
|
ordersResp, err = client.NewGetOpenOrderRequest().OrderLinkId(apiResp.OrderLinkId).Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, len(ordersResp.List), 1)
|
|
|
|
assert.Equal(t, ordersResp.List[0].OrderStatus, OrderStatusCancelled)
|
|
|
|
t.Logf("apiResp: %+v", ordersResp.List[0])
|
|
|
|
})
|
2023-07-27 09:44:47 +00:00
|
|
|
|
|
|
|
t.Run("GetOrderHistoriesRequest", func(t *testing.T) {
|
|
|
|
req := client.NewPlaceOrderRequest().
|
|
|
|
Symbol("DOTUSDT").
|
|
|
|
Side(SideBuy).
|
|
|
|
OrderType(OrderTypeLimit).
|
|
|
|
Qty("1").
|
|
|
|
Price("4.6").
|
|
|
|
OrderLinkId(uuid.NewString()).
|
|
|
|
TimeInForce(TimeInForceGTC)
|
|
|
|
apiResp, err := req.Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
t.Logf("apiResp: %+v", apiResp)
|
|
|
|
|
|
|
|
ordersResp, err := client.NewGetOpenOrderRequest().OrderLinkId(apiResp.OrderLinkId).Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, len(ordersResp.List), 1)
|
|
|
|
t.Logf("apiResp: %+v", ordersResp.List[0])
|
|
|
|
|
|
|
|
orderResp, err := client.NewGetOrderHistoriesRequest().Symbol("DOTUSDT").Cursor("0").Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
t.Logf("apiResp: %#v", orderResp)
|
|
|
|
})
|
2023-08-09 04:50:30 +00:00
|
|
|
|
|
|
|
t.Run("GetWalletBalancesRequest", func(t *testing.T) {
|
|
|
|
req := client.NewGetWalletBalancesRequest().Coin("BTC")
|
|
|
|
apiResp, err := req.Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
t.Logf("apiResp: %+v", apiResp)
|
|
|
|
})
|
2023-08-09 03:41:04 +00:00
|
|
|
|
|
|
|
t.Run("GetKLinesRequest", func(t *testing.T) {
|
|
|
|
startTime := time.Date(2023, 8, 8, 9, 28, 0, 0, time.UTC)
|
|
|
|
endTime := time.Date(2023, 8, 8, 9, 45, 0, 0, time.UTC)
|
|
|
|
req := client.NewGetKLinesRequest().
|
|
|
|
Symbol("BTCUSDT").Interval("15").StartTime(startTime).EndTime(endTime)
|
|
|
|
apiResp, err := req.Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
t.Logf("apiResp: %+v", apiResp.List)
|
|
|
|
})
|
2023-08-10 07:02:30 +00:00
|
|
|
|
|
|
|
t.Run("GetFeeRatesRequest", func(t *testing.T) {
|
|
|
|
req := client.NewGetFeeRatesRequest()
|
|
|
|
apiResp, err := req.Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
t.Logf("apiResp: %+v", apiResp)
|
|
|
|
})
|
2023-07-21 06:55:27 +00:00
|
|
|
}
|