bbgo_origin/pkg/exchange/ftx/ftxapi/client_test.go

110 lines
2.5 KiB
Go
Raw Normal View History

2022-02-24 02:30:34 +00:00
package ftxapi
import (
"context"
"os"
"regexp"
2022-03-02 03:12:31 +00:00
"strconv"
2022-02-24 02:30:34 +00:00
"testing"
"time"
"github.com/stretchr/testify/assert"
2022-03-02 03:12:31 +00:00
"github.com/c9s/bbgo/pkg/fixedpoint"
2022-02-24 02:30:34 +00:00
)
func maskSecret(s string) string {
re := regexp.MustCompile(`\b(\w{4})\w+\b`)
s = re.ReplaceAllString(s, "$1******")
return s
}
func integrationTestConfigured(t *testing.T) (key, secret string, ok bool) {
var hasKey, hasSecret bool
key, hasKey = os.LookupEnv("FTX_API_KEY")
secret, hasSecret = os.LookupEnv("FTX_API_SECRET")
ok = hasKey && hasSecret && os.Getenv("TEST_FTX") == "1"
if ok {
t.Logf("ftx api integration test enabled, key = %s, secret = %s", maskSecret(key), maskSecret(secret))
}
return key, secret, ok
}
2022-03-02 03:12:31 +00:00
func TestClient_Requests(t *testing.T) {
2022-02-24 02:30:34 +00:00
key, secret, ok := integrationTestConfigured(t)
if !ok {
t.SkipNow()
return
}
ctx, cancel := context.WithTimeout(context.TODO(), 15 * time.Second)
defer cancel()
client := NewClient()
client.Auth(key, secret, "")
2022-03-02 03:12:31 +00:00
testCases := []struct {
name string
tt func(t *testing.T)
} {
2022-05-08 10:36:25 +00:00
{
name: "GetMarketsRequest",
tt: func(t *testing.T) {
req := client.NewGetMarketsRequest()
markets ,err := req.Do(ctx)
assert.NoError(t, err)
assert.NotNil(t, markets)
t.Logf("markets: %+v", markets)
},
},
2022-03-02 03:12:31 +00:00
{
name: "GetAccountRequest",
tt: func(t *testing.T) {
req := client.NewGetAccountRequest()
account ,err := req.Do(ctx)
assert.NoError(t, err)
assert.NotNil(t, account)
t.Logf("account: %+v", account)
},
},
{
name: "PlaceOrderRequest",
tt: func(t *testing.T) {
req := client.NewPlaceOrderRequest()
req.PostOnly(true).
Size(fixedpoint.MustNewFromString("1.0")).
Price(fixedpoint.MustNewFromString("10.0")).
OrderType(OrderTypeLimit).
Side(SideBuy).
Market("LTC/USDT")
createdOrder,err := req.Do(ctx)
if assert.NoError(t, err) {
assert.NotNil(t, createdOrder)
t.Logf("createdOrder: %+v", createdOrder)
req2 := client.NewCancelOrderRequest(strconv.FormatInt(createdOrder.Id, 10))
ret, err := req2.Do(ctx)
assert.NoError(t, err)
t.Logf("cancelOrder: %+v", ret)
assert.True(t, ret.Success)
}
},
},
{
name: "GetFillsRequest",
tt: func(t *testing.T) {
req := client.NewGetFillsRequest()
2022-05-08 10:36:25 +00:00
req.Market("CRO/USD")
2022-03-02 03:12:31 +00:00
fills, err := req.Do(ctx)
assert.NoError(t, err)
assert.NotNil(t, fills)
t.Logf("fills: %+v", fills)
},
},
}
for _, testCase := range testCases {
t.Run(testCase.name, testCase.tt)
}
2022-02-24 02:30:34 +00:00
}