2022-04-27 14:25:14 +00:00
|
|
|
package binanceapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
"net/http/httputil"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2022-05-31 09:08:52 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/testutil"
|
|
|
|
)
|
2022-04-27 14:25:14 +00:00
|
|
|
|
2022-04-27 17:12:37 +00:00
|
|
|
func getTestClientOrSkip(t *testing.T) *RestClient {
|
2022-05-31 09:08:52 +00:00
|
|
|
key, secret, ok := testutil.IntegrationTestConfigured(t, "BINANCE")
|
2022-04-27 14:25:14 +00:00
|
|
|
if !ok {
|
|
|
|
t.SkipNow()
|
2022-04-27 17:12:37 +00:00
|
|
|
return nil
|
2022-04-27 14:25:14 +00:00
|
|
|
}
|
|
|
|
|
2022-05-28 17:42:08 +00:00
|
|
|
client := NewClient("")
|
2022-04-27 14:25:14 +00:00
|
|
|
client.Auth(key, secret)
|
2022-04-27 17:12:37 +00:00
|
|
|
return client
|
|
|
|
}
|
2022-04-27 14:25:14 +00:00
|
|
|
|
2022-04-27 17:12:37 +00:00
|
|
|
func TestClient_GetTradeFeeRequest(t *testing.T) {
|
|
|
|
client := getTestClientOrSkip(t)
|
2022-04-27 14:25:14 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
err := client.SetTimeOffsetFromServer(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
req := client.NewGetTradeFeeRequest()
|
|
|
|
tradeFees, err := req.Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, tradeFees)
|
|
|
|
t.Logf("tradeFees: %+v", tradeFees)
|
|
|
|
}
|
2022-04-27 17:12:37 +00:00
|
|
|
|
|
|
|
func TestClient_GetDepositAddressRequest(t *testing.T) {
|
|
|
|
client := getTestClientOrSkip(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
err := client.SetTimeOffsetFromServer(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
req := client.NewGetDepositAddressRequest()
|
|
|
|
req.Coin("BTC")
|
|
|
|
address, err := req.Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, address)
|
|
|
|
assert.NotEmpty(t, address.Url)
|
|
|
|
assert.NotEmpty(t, address.Address)
|
|
|
|
t.Logf("deposit address: %+v", address)
|
|
|
|
}
|
|
|
|
|
2022-04-27 17:35:23 +00:00
|
|
|
func TestClient_GetDepositHistoryRequest(t *testing.T) {
|
|
|
|
client := getTestClientOrSkip(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
err := client.SetTimeOffsetFromServer(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
req := client.NewGetDepositHistoryRequest()
|
|
|
|
history, err := req.Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, history)
|
|
|
|
assert.NotEmpty(t, history)
|
|
|
|
t.Logf("deposit history: %+v", history)
|
|
|
|
}
|
|
|
|
|
2022-04-29 05:34:19 +00:00
|
|
|
func TestClient_NewSpotRebateHistoryRequest(t *testing.T) {
|
|
|
|
client := getTestClientOrSkip(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
err := client.SetTimeOffsetFromServer(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
req := client.NewGetSpotRebateHistoryRequest()
|
|
|
|
history, err := req.Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, history)
|
|
|
|
assert.NotEmpty(t, history)
|
|
|
|
t.Logf("spot rebate history: %+v", history)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_NewGetMarginInterestRateHistoryRequest(t *testing.T) {
|
|
|
|
client := getTestClientOrSkip(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
err := client.SetTimeOffsetFromServer(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
req := client.NewGetMarginInterestRateHistoryRequest()
|
|
|
|
req.Asset("BTC")
|
|
|
|
history, err := req.Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, history)
|
|
|
|
assert.NotEmpty(t, history)
|
|
|
|
t.Logf("interest rate history: %+v", history)
|
|
|
|
}
|
2022-04-27 14:25:14 +00:00
|
|
|
|
|
|
|
func TestClient_privateCall(t *testing.T) {
|
2022-05-31 09:08:52 +00:00
|
|
|
key, secret, ok := testutil.IntegrationTestConfigured(t, "BINANCE")
|
2022-04-27 14:25:14 +00:00
|
|
|
if !ok {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
2022-05-28 17:42:08 +00:00
|
|
|
client := NewClient("")
|
2022-04-27 14:25:14 +00:00
|
|
|
client.Auth(key, secret)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
err := client.SetTimeOffsetFromServer(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
req, err := client.NewAuthenticatedRequest(ctx, "GET", "/sapi/v1/asset/tradeFee", nil, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, req)
|
|
|
|
|
|
|
|
resp, err := client.SendRequest(req)
|
|
|
|
if assert.NoError(t, err) {
|
2022-06-17 04:01:15 +00:00
|
|
|
var feeStructs []struct {
|
|
|
|
Symbol string `json:"symbol"`
|
2022-04-27 14:25:14 +00:00
|
|
|
MakerCommission string `json:"makerCommission"`
|
|
|
|
TakerCommission string `json:"takerCommission"`
|
|
|
|
}
|
|
|
|
err = resp.DecodeJSON(&feeStructs)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.NotEmpty(t, feeStructs)
|
|
|
|
}
|
|
|
|
} else {
|
2022-06-17 04:01:15 +00:00
|
|
|
dump, _ := httputil.DumpRequest(req, true)
|
2022-04-27 14:25:14 +00:00
|
|
|
log.Printf("request: %s", dump)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_setTimeOffsetFromServer(t *testing.T) {
|
2022-05-28 17:42:08 +00:00
|
|
|
client := NewClient("")
|
2022-04-27 14:25:14 +00:00
|
|
|
err := client.SetTimeOffsetFromServer(context.Background())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|