2023-07-21 09:05:19 +00:00
|
|
|
package okexapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"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, passphrase, ok := testutil.IntegrationTestWithPassphraseConfigured(t, "OKEX")
|
|
|
|
if !ok {
|
2023-08-21 07:31:30 +00:00
|
|
|
t.Skip("Please configure all credentials about OKEX")
|
2023-07-21 09:05:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-06 11:14:21 +00:00
|
|
|
client, err := NewClient()
|
|
|
|
assert.NoError(t, err)
|
2023-07-21 09:05:19 +00:00
|
|
|
client.Auth(key, secret, passphrase)
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_GetInstrumentsRequest(t *testing.T) {
|
2023-09-06 11:14:21 +00:00
|
|
|
client, err := NewClient()
|
|
|
|
assert.NoError(t, err)
|
2023-07-21 09:05:19 +00:00
|
|
|
ctx := context.Background()
|
2023-09-06 11:14:21 +00:00
|
|
|
req := client.NewGetInstrumentsRequest()
|
2023-07-21 09:05:19 +00:00
|
|
|
|
|
|
|
instruments, err := req.
|
|
|
|
InstrumentType(InstrumentTypeSpot).
|
|
|
|
Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, instruments)
|
|
|
|
t.Logf("instruments: %+v", instruments)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_GetFundingRateRequest(t *testing.T) {
|
2023-09-06 11:14:21 +00:00
|
|
|
client, err := NewClient()
|
|
|
|
assert.NoError(t, err)
|
2023-07-21 09:05:19 +00:00
|
|
|
ctx := context.Background()
|
2023-09-06 11:14:21 +00:00
|
|
|
req := client.NewGetFundingRate()
|
2023-07-21 09:05:19 +00:00
|
|
|
|
|
|
|
instrument, err := req.
|
|
|
|
InstrumentID("BTC-USDT-SWAP").
|
|
|
|
Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, instrument)
|
|
|
|
t.Logf("instrument: %+v", instrument)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_PlaceOrderRequest(t *testing.T) {
|
|
|
|
client := getTestClientOrSkip(t)
|
|
|
|
ctx := context.Background()
|
2023-09-06 11:14:21 +00:00
|
|
|
req := client.NewPlaceOrderRequest()
|
2023-07-21 09:05:19 +00:00
|
|
|
|
|
|
|
order, err := req.
|
2023-08-11 01:28:58 +00:00
|
|
|
InstrumentID("BTC-USDT").
|
2023-07-21 09:05:19 +00:00
|
|
|
TradeMode("cash").
|
2023-08-11 01:28:58 +00:00
|
|
|
Side(SideTypeBuy).
|
2023-07-21 09:05:19 +00:00
|
|
|
OrderType(OrderTypeLimit).
|
2023-08-11 01:28:58 +00:00
|
|
|
Price("15000").
|
|
|
|
Quantity("0.0001").
|
2023-07-21 09:05:19 +00:00
|
|
|
Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, order)
|
2023-08-11 01:28:58 +00:00
|
|
|
t.Logf("place order: %+v", order)
|
2023-07-21 09:05:19 +00:00
|
|
|
}
|
2023-08-09 07:05:26 +00:00
|
|
|
|
|
|
|
func TestClient_GetPendingOrderRequest(t *testing.T) {
|
|
|
|
client := getTestClientOrSkip(t)
|
|
|
|
ctx := context.Background()
|
2023-09-06 11:14:21 +00:00
|
|
|
req := client.NewGetPendingOrderRequest()
|
2023-08-09 07:05:26 +00:00
|
|
|
odr_type := []string{string(OrderTypeLimit), string(OrderTypeIOC)}
|
|
|
|
|
|
|
|
pending_order, err := req.
|
2023-08-11 01:28:58 +00:00
|
|
|
InstrumentID("BTC-USDT").
|
2023-08-09 07:05:26 +00:00
|
|
|
OrderTypes(odr_type).
|
|
|
|
Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
2023-08-11 01:28:58 +00:00
|
|
|
assert.NotEmpty(t, pending_order)
|
|
|
|
t.Logf("pending order: %+v", pending_order)
|
2023-08-09 07:05:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_GetOrderDetailsRequest(t *testing.T) {
|
|
|
|
client := getTestClientOrSkip(t)
|
|
|
|
ctx := context.Background()
|
2023-09-06 11:14:21 +00:00
|
|
|
req := client.NewGetOrderDetailsRequest()
|
2023-08-09 07:05:26 +00:00
|
|
|
|
|
|
|
orderDetail, err := req.
|
|
|
|
InstrumentID("BTC-USDT").
|
2023-08-11 01:28:58 +00:00
|
|
|
OrderID("609869603774656544").
|
2023-08-09 07:05:26 +00:00
|
|
|
Do(ctx)
|
2023-08-11 01:28:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, orderDetail)
|
|
|
|
t.Logf("order detail: %+v", orderDetail)
|
2023-08-09 07:05:26 +00:00
|
|
|
}
|