Merge pull request #1691 from c9s/edwin/bitget/upgrade-public-ws-to-v2

FEATURE: [bitget] upgrade public websocket to v2
This commit is contained in:
bailantaotao 2024-08-13 11:29:26 +08:00 committed by GitHub
commit a91685920e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
38 changed files with 142 additions and 3473 deletions

View File

@ -1,20 +0,0 @@
package bitgetapi
//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Data
//go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Data
import (
"github.com/c9s/requestgen"
)
type CancelOrderBySymbolResponse string
//go:generate GetRequest -url "/api/spot/v1/trade/cancel-symbol-order" -type CancelOrderBySymbolRequest -responseDataType .CancelOrderBySymbolResponse
type CancelOrderBySymbolRequest struct {
client requestgen.AuthenticatedAPIClient
symbol string `param:"symbol"`
}
func (c *RestClient) NewCancelOrderBySymbolRequest() *CancelOrderBySymbolRequest {
return &CancelOrderBySymbolRequest{client: c}
}

View File

@ -1,152 +0,0 @@
// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Data -url /api/spot/v1/trade/cancel-symbol-order -type CancelOrderBySymbolRequest -responseDataType .CancelOrderBySymbolResponse"; DO NOT EDIT.
package bitgetapi
import (
"context"
"encoding/json"
"fmt"
"net/url"
"reflect"
"regexp"
)
func (c *CancelOrderBySymbolRequest) Symbol(symbol string) *CancelOrderBySymbolRequest {
c.symbol = symbol
return c
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (c *CancelOrderBySymbolRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
query := url.Values{}
for _k, _v := range params {
query.Add(_k, fmt.Sprintf("%v", _v))
}
return query, nil
}
// GetParameters builds and checks the parameters and return the result in a map object
func (c *CancelOrderBySymbolRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check symbol field -> json key symbol
symbol := c.symbol
// assign parameter of symbol
params["symbol"] = symbol
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (c *CancelOrderBySymbolRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := c.GetParameters()
if err != nil {
return query, err
}
for _k, _v := range params {
if c.isVarSlice(_v) {
c.iterateSlice(_v, func(it interface{}) {
query.Add(_k+"[]", fmt.Sprintf("%v", it))
})
} else {
query.Add(_k, fmt.Sprintf("%v", _v))
}
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (c *CancelOrderBySymbolRequest) GetParametersJSON() ([]byte, error) {
params, err := c.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}
// GetSlugParameters builds and checks the slug parameters and return the result in a map object
func (c *CancelOrderBySymbolRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
func (c *CancelOrderBySymbolRequest) applySlugsToUrl(url string, slugs map[string]string) string {
for _k, _v := range slugs {
needleRE := regexp.MustCompile(":" + _k + "\\b")
url = needleRE.ReplaceAllString(url, _v)
}
return url
}
func (c *CancelOrderBySymbolRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
sliceValue := reflect.ValueOf(slice)
for _i := 0; _i < sliceValue.Len(); _i++ {
it := sliceValue.Index(_i).Interface()
_f(it)
}
}
func (c *CancelOrderBySymbolRequest) isVarSlice(_v interface{}) bool {
rt := reflect.TypeOf(_v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (c *CancelOrderBySymbolRequest) GetSlugsMap() (map[string]string, error) {
slugs := map[string]string{}
params, err := c.GetSlugParameters()
if err != nil {
return slugs, nil
}
for _k, _v := range params {
slugs[_k] = fmt.Sprintf("%v", _v)
}
return slugs, nil
}
func (c *CancelOrderBySymbolRequest) Do(ctx context.Context) (*CancelOrderBySymbolResponse, error) {
// empty params for GET operation
var params interface{}
query, err := c.GetParametersQuery()
if err != nil {
return nil, err
}
apiURL := "/api/spot/v1/trade/cancel-symbol-order"
req, err := c.client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params)
if err != nil {
return nil, err
}
response, err := c.client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse APIResponse
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
var data CancelOrderBySymbolResponse
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
return nil, err
}
return &data, nil
}

View File

@ -1,25 +0,0 @@
package bitgetapi
//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Data
//go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Data
import (
"github.com/c9s/requestgen"
)
type CancelOrderResponse struct {
OrderId string `json:"orderId"`
ClientOrderId string `json:"clientOrderId"`
}
//go:generate PostRequest -url "/api/spot/v1/trade/cancel-order-v2" -type CancelOrderRequest -responseDataType .CancelOrderResponse
type CancelOrderRequest struct {
client requestgen.AuthenticatedAPIClient
symbol string `param:"symbol"`
orderId *string `param:"orderId"`
clientOrderId *string `param:"clientOid"`
}
func (c *RestClient) NewCancelOrderRequest() *CancelOrderRequest {
return &CancelOrderRequest{client: c}
}

View File

@ -1,177 +0,0 @@
// Code generated by "requestgen -method POST -responseType .APIResponse -responseDataField Data -url /api/spot/v1/trade/cancel-order-v2 -type CancelOrderRequest -responseDataType .CancelOrderResponse"; DO NOT EDIT.
package bitgetapi
import (
"context"
"encoding/json"
"fmt"
"net/url"
"reflect"
"regexp"
)
func (c *CancelOrderRequest) Symbol(symbol string) *CancelOrderRequest {
c.symbol = symbol
return c
}
func (c *CancelOrderRequest) OrderId(orderId string) *CancelOrderRequest {
c.orderId = &orderId
return c
}
func (c *CancelOrderRequest) ClientOrderId(clientOrderId string) *CancelOrderRequest {
c.clientOrderId = &clientOrderId
return c
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (c *CancelOrderRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
query := url.Values{}
for _k, _v := range params {
query.Add(_k, fmt.Sprintf("%v", _v))
}
return query, nil
}
// GetParameters builds and checks the parameters and return the result in a map object
func (c *CancelOrderRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check symbol field -> json key symbol
symbol := c.symbol
// assign parameter of symbol
params["symbol"] = symbol
// check orderId field -> json key orderId
if c.orderId != nil {
orderId := *c.orderId
// assign parameter of orderId
params["orderId"] = orderId
} else {
}
// check clientOrderId field -> json key clientOid
if c.clientOrderId != nil {
clientOrderId := *c.clientOrderId
// assign parameter of clientOrderId
params["clientOid"] = clientOrderId
} else {
}
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (c *CancelOrderRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := c.GetParameters()
if err != nil {
return query, err
}
for _k, _v := range params {
if c.isVarSlice(_v) {
c.iterateSlice(_v, func(it interface{}) {
query.Add(_k+"[]", fmt.Sprintf("%v", it))
})
} else {
query.Add(_k, fmt.Sprintf("%v", _v))
}
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (c *CancelOrderRequest) GetParametersJSON() ([]byte, error) {
params, err := c.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}
// GetSlugParameters builds and checks the slug parameters and return the result in a map object
func (c *CancelOrderRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
func (c *CancelOrderRequest) applySlugsToUrl(url string, slugs map[string]string) string {
for _k, _v := range slugs {
needleRE := regexp.MustCompile(":" + _k + "\\b")
url = needleRE.ReplaceAllString(url, _v)
}
return url
}
func (c *CancelOrderRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
sliceValue := reflect.ValueOf(slice)
for _i := 0; _i < sliceValue.Len(); _i++ {
it := sliceValue.Index(_i).Interface()
_f(it)
}
}
func (c *CancelOrderRequest) isVarSlice(_v interface{}) bool {
rt := reflect.TypeOf(_v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (c *CancelOrderRequest) GetSlugsMap() (map[string]string, error) {
slugs := map[string]string{}
params, err := c.GetSlugParameters()
if err != nil {
return slugs, nil
}
for _k, _v := range params {
slugs[_k] = fmt.Sprintf("%v", _v)
}
return slugs, nil
}
func (c *CancelOrderRequest) Do(ctx context.Context) (*CancelOrderResponse, error) {
params, err := c.GetParameters()
if err != nil {
return nil, err
}
query := url.Values{}
apiURL := "/api/spot/v1/trade/cancel-order-v2"
req, err := c.client.NewAuthenticatedRequest(ctx, "POST", apiURL, query, params)
if err != nil {
return nil, err
}
response, err := c.client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse APIResponse
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
var data CancelOrderResponse
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
return nil, err
}
return &data, nil
}

View File

@ -1,87 +0,0 @@
package bitgetapi
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, ok := testutil.IntegrationTestConfigured(t, "BITGET")
if !ok {
t.Skip("BITGET_* env vars are not configured")
return nil
}
client := NewClient()
client.Auth(key, secret, os.Getenv("BITGET_API_PASSPHRASE"))
return client
}
func TestClient(t *testing.T) {
client := getTestClientOrSkip(t)
ctx := context.Background()
t.Run("GetAllTickersRequest", func(t *testing.T) {
req := client.NewGetAllTickersRequest()
tickers, err := req.Do(ctx)
assert.NoError(t, err)
t.Logf("tickers: %+v", tickers)
})
t.Run("GetSymbolsRequest", func(t *testing.T) {
req := client.NewGetSymbolsRequest()
symbols, err := req.Do(ctx)
assert.NoError(t, err)
t.Logf("symbols: %+v", symbols)
})
t.Run("GetTickerRequest", func(t *testing.T) {
req := client.NewGetTickerRequest()
req.Symbol("BTCUSDT_SPBL")
ticker, err := req.Do(ctx)
assert.NoError(t, err)
t.Logf("ticker: %+v", ticker)
})
t.Run("GetDepthRequest", func(t *testing.T) {
req := client.NewGetDepthRequest()
req.Symbol("BTCUSDT_SPBL")
depth, err := req.Do(ctx)
assert.NoError(t, err)
t.Logf("depth: %+v", depth)
})
t.Run("GetServerTimeRequest", func(t *testing.T) {
req := client.NewGetServerTimeRequest()
serverTime, err := req.Do(ctx)
assert.NoError(t, err)
t.Logf("time: %+v", serverTime)
})
t.Run("GetAccountAssetsRequest", func(t *testing.T) {
req := client.NewGetAccountAssetsRequest()
assets, err := req.Do(ctx)
assert.NoError(t, err)
t.Logf("assets: %+v", assets)
})
t.Run("GetAccountTransfersRequest", func(t *testing.T) {
req := client.NewGetAccountTransfersRequest()
req.CoinId(1)
req.FromType(AccountExchange)
transfers, err := req.Do(ctx)
assert.NoError(t, err)
t.Logf("transfers: %+v", transfers)
})
}

View File

@ -1,29 +0,0 @@
package bitgetapi
//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Data
//go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Data
import (
"github.com/c9s/requestgen"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
type AccountAsset struct {
CoinId int64 `json:"coinId"`
CoinName string `json:"coinName"`
Available fixedpoint.Value `json:"available"`
Frozen fixedpoint.Value `json:"frozen"`
Lock fixedpoint.Value `json:"lock"`
UTime types.MillisecondTimestamp `json:"uTime"`
}
//go:generate GetRequest -url "/api/spot/v1/account/assets" -type GetAccountAssetsRequest -responseDataType []AccountAsset
type GetAccountAssetsRequest struct {
client requestgen.AuthenticatedAPIClient
}
func (c *RestClient) NewGetAccountAssetsRequest() *GetAccountAssetsRequest {
return &GetAccountAssetsRequest{client: c}
}

View File

@ -1,139 +0,0 @@
// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Data -url /api/spot/v1/account/assets -type GetAccountAssetsRequest -responseDataType []AccountAsset"; DO NOT EDIT.
package bitgetapi
import (
"context"
"encoding/json"
"fmt"
"net/url"
"reflect"
"regexp"
)
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (g *GetAccountAssetsRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
query := url.Values{}
for _k, _v := range params {
query.Add(_k, fmt.Sprintf("%v", _v))
}
return query, nil
}
// GetParameters builds and checks the parameters and return the result in a map object
func (g *GetAccountAssetsRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (g *GetAccountAssetsRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := g.GetParameters()
if err != nil {
return query, err
}
for _k, _v := range params {
if g.isVarSlice(_v) {
g.iterateSlice(_v, func(it interface{}) {
query.Add(_k+"[]", fmt.Sprintf("%v", it))
})
} else {
query.Add(_k, fmt.Sprintf("%v", _v))
}
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (g *GetAccountAssetsRequest) GetParametersJSON() ([]byte, error) {
params, err := g.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}
// GetSlugParameters builds and checks the slug parameters and return the result in a map object
func (g *GetAccountAssetsRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
func (g *GetAccountAssetsRequest) applySlugsToUrl(url string, slugs map[string]string) string {
for _k, _v := range slugs {
needleRE := regexp.MustCompile(":" + _k + "\\b")
url = needleRE.ReplaceAllString(url, _v)
}
return url
}
func (g *GetAccountAssetsRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
sliceValue := reflect.ValueOf(slice)
for _i := 0; _i < sliceValue.Len(); _i++ {
it := sliceValue.Index(_i).Interface()
_f(it)
}
}
func (g *GetAccountAssetsRequest) isVarSlice(_v interface{}) bool {
rt := reflect.TypeOf(_v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (g *GetAccountAssetsRequest) GetSlugsMap() (map[string]string, error) {
slugs := map[string]string{}
params, err := g.GetSlugParameters()
if err != nil {
return slugs, nil
}
for _k, _v := range params {
slugs[_k] = fmt.Sprintf("%v", _v)
}
return slugs, nil
}
func (g *GetAccountAssetsRequest) Do(ctx context.Context) ([]AccountAsset, error) {
// no body params
var params interface{}
query := url.Values{}
apiURL := "/api/spot/v1/account/assets"
req, err := g.client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params)
if err != nil {
return nil, err
}
response, err := g.client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse APIResponse
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
var data []AccountAsset
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
return nil, err
}
return data, nil
}

View File

@ -1,28 +0,0 @@
package bitgetapi
//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Data
//go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Data
import (
"github.com/c9s/requestgen"
"github.com/c9s/bbgo/pkg/types"
)
type Account struct {
UserId types.StrInt64 `json:"user_id"`
InviterId types.StrInt64 `json:"inviter_id"`
Ips string `json:"ips"`
Authorities []string `json:"authorities"`
ParentId types.StrInt64 `json:"parentId"`
Trader bool `json:"trader"`
}
//go:generate GetRequest -url "/api/spot/v1/account/getInfo" -type GetAccountRequest -responseDataType .Account
type GetAccountRequest struct {
client requestgen.AuthenticatedAPIClient
}
func (c *RestClient) NewGetAccountRequest() *GetAccountRequest {
return &GetAccountRequest{client: c}
}

View File

@ -1,139 +0,0 @@
// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Data -url /api/spot/v1/account/getInfo -type GetAccountRequest -responseDataType .Account"; DO NOT EDIT.
package bitgetapi
import (
"context"
"encoding/json"
"fmt"
"net/url"
"reflect"
"regexp"
)
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (g *GetAccountRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
query := url.Values{}
for _k, _v := range params {
query.Add(_k, fmt.Sprintf("%v", _v))
}
return query, nil
}
// GetParameters builds and checks the parameters and return the result in a map object
func (g *GetAccountRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (g *GetAccountRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := g.GetParameters()
if err != nil {
return query, err
}
for _k, _v := range params {
if g.isVarSlice(_v) {
g.iterateSlice(_v, func(it interface{}) {
query.Add(_k+"[]", fmt.Sprintf("%v", it))
})
} else {
query.Add(_k, fmt.Sprintf("%v", _v))
}
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (g *GetAccountRequest) GetParametersJSON() ([]byte, error) {
params, err := g.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}
// GetSlugParameters builds and checks the slug parameters and return the result in a map object
func (g *GetAccountRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
func (g *GetAccountRequest) applySlugsToUrl(url string, slugs map[string]string) string {
for _k, _v := range slugs {
needleRE := regexp.MustCompile(":" + _k + "\\b")
url = needleRE.ReplaceAllString(url, _v)
}
return url
}
func (g *GetAccountRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
sliceValue := reflect.ValueOf(slice)
for _i := 0; _i < sliceValue.Len(); _i++ {
it := sliceValue.Index(_i).Interface()
_f(it)
}
}
func (g *GetAccountRequest) isVarSlice(_v interface{}) bool {
rt := reflect.TypeOf(_v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (g *GetAccountRequest) GetSlugsMap() (map[string]string, error) {
slugs := map[string]string{}
params, err := g.GetSlugParameters()
if err != nil {
return slugs, nil
}
for _k, _v := range params {
slugs[_k] = fmt.Sprintf("%v", _v)
}
return slugs, nil
}
func (g *GetAccountRequest) Do(ctx context.Context) (*Account, error) {
// no body params
var params interface{}
query := url.Values{}
apiURL := "/api/spot/v1/account/getInfo"
req, err := g.client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params)
if err != nil {
return nil, err
}
response, err := g.client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse APIResponse
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
var data Account
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
return nil, err
}
return &data, nil
}

View File

@ -1,44 +0,0 @@
package bitgetapi
//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Data
//go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Data
import (
"github.com/c9s/requestgen"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
type AccountType string
const (
AccountExchange AccountType = "EXCHANGE"
AccountContract AccountType = "CONTRACT"
)
type Transfer struct {
CTime types.MillisecondTimestamp `json:"cTime"`
CoinId string `json:"coinId"`
CoinName string `json:"coinName"`
GroupType string `json:"groupType"`
BizType string `json:"bizType"`
Quantity fixedpoint.Value `json:"quantity"`
Balance fixedpoint.Value `json:"balance"`
Fees fixedpoint.Value `json:"fees"`
BillId string `json:"billId"`
}
//go:generate GetRequest -url "/api/spot/v1/account/transferRecords" -type GetAccountTransfersRequest -responseDataType []Transfer
type GetAccountTransfersRequest struct {
client requestgen.AuthenticatedAPIClient
coinId int `param:"coinId"`
fromType AccountType `param:"fromType"`
after string `param:"after"`
before string `param:"before"`
}
func (c *RestClient) NewGetAccountTransfersRequest() *GetAccountTransfersRequest {
return &GetAccountTransfersRequest{client: c}
}

View File

@ -1,193 +0,0 @@
// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Data -url /api/spot/v1/account/transferRecords -type GetAccountTransfersRequest -responseDataType []Transfer"; DO NOT EDIT.
package bitgetapi
import (
"context"
"encoding/json"
"fmt"
"net/url"
"reflect"
"regexp"
)
func (g *GetAccountTransfersRequest) CoinId(coinId int) *GetAccountTransfersRequest {
g.coinId = coinId
return g
}
func (g *GetAccountTransfersRequest) FromType(fromType AccountType) *GetAccountTransfersRequest {
g.fromType = fromType
return g
}
func (g *GetAccountTransfersRequest) After(after string) *GetAccountTransfersRequest {
g.after = after
return g
}
func (g *GetAccountTransfersRequest) Before(before string) *GetAccountTransfersRequest {
g.before = before
return g
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (g *GetAccountTransfersRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
query := url.Values{}
for _k, _v := range params {
query.Add(_k, fmt.Sprintf("%v", _v))
}
return query, nil
}
// GetParameters builds and checks the parameters and return the result in a map object
func (g *GetAccountTransfersRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check coinId field -> json key coinId
coinId := g.coinId
// assign parameter of coinId
params["coinId"] = coinId
// check fromType field -> json key fromType
fromType := g.fromType
// TEMPLATE check-valid-values
switch fromType {
case AccountExchange, AccountContract:
params["fromType"] = fromType
default:
return nil, fmt.Errorf("fromType value %v is invalid", fromType)
}
// END TEMPLATE check-valid-values
// assign parameter of fromType
params["fromType"] = fromType
// check after field -> json key after
after := g.after
// assign parameter of after
params["after"] = after
// check before field -> json key before
before := g.before
// assign parameter of before
params["before"] = before
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (g *GetAccountTransfersRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := g.GetParameters()
if err != nil {
return query, err
}
for _k, _v := range params {
if g.isVarSlice(_v) {
g.iterateSlice(_v, func(it interface{}) {
query.Add(_k+"[]", fmt.Sprintf("%v", it))
})
} else {
query.Add(_k, fmt.Sprintf("%v", _v))
}
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (g *GetAccountTransfersRequest) GetParametersJSON() ([]byte, error) {
params, err := g.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}
// GetSlugParameters builds and checks the slug parameters and return the result in a map object
func (g *GetAccountTransfersRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
func (g *GetAccountTransfersRequest) applySlugsToUrl(url string, slugs map[string]string) string {
for _k, _v := range slugs {
needleRE := regexp.MustCompile(":" + _k + "\\b")
url = needleRE.ReplaceAllString(url, _v)
}
return url
}
func (g *GetAccountTransfersRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
sliceValue := reflect.ValueOf(slice)
for _i := 0; _i < sliceValue.Len(); _i++ {
it := sliceValue.Index(_i).Interface()
_f(it)
}
}
func (g *GetAccountTransfersRequest) isVarSlice(_v interface{}) bool {
rt := reflect.TypeOf(_v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (g *GetAccountTransfersRequest) GetSlugsMap() (map[string]string, error) {
slugs := map[string]string{}
params, err := g.GetSlugParameters()
if err != nil {
return slugs, nil
}
for _k, _v := range params {
slugs[_k] = fmt.Sprintf("%v", _v)
}
return slugs, nil
}
func (g *GetAccountTransfersRequest) Do(ctx context.Context) ([]Transfer, error) {
// empty params for GET operation
var params interface{}
query, err := g.GetParametersQuery()
if err != nil {
return nil, err
}
apiURL := "/api/spot/v1/account/transferRecords"
req, err := g.client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params)
if err != nil {
return nil, err
}
response, err := g.client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse APIResponse
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
var data []Transfer
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
return nil, err
}
return data, nil
}

View File

@ -1,17 +0,0 @@
package bitgetapi
//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Data
//go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Data
import (
"github.com/c9s/requestgen"
)
//go:generate GetRequest -url "/api/spot/v1/market/tickers" -type GetAllTickersRequest -responseDataType []Ticker
type GetAllTickersRequest struct {
client requestgen.APIClient
}
func (c *RestClient) NewGetAllTickersRequest() *GetAllTickersRequest {
return &GetAllTickersRequest{client: c}
}

View File

@ -1,139 +0,0 @@
// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Data -url /api/spot/v1/market/tickers -type GetAllTickersRequest -responseDataType []Ticker"; DO NOT EDIT.
package bitgetapi
import (
"context"
"encoding/json"
"fmt"
"net/url"
"reflect"
"regexp"
)
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (g *GetAllTickersRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
query := url.Values{}
for _k, _v := range params {
query.Add(_k, fmt.Sprintf("%v", _v))
}
return query, nil
}
// GetParameters builds and checks the parameters and return the result in a map object
func (g *GetAllTickersRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (g *GetAllTickersRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := g.GetParameters()
if err != nil {
return query, err
}
for _k, _v := range params {
if g.isVarSlice(_v) {
g.iterateSlice(_v, func(it interface{}) {
query.Add(_k+"[]", fmt.Sprintf("%v", it))
})
} else {
query.Add(_k, fmt.Sprintf("%v", _v))
}
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (g *GetAllTickersRequest) GetParametersJSON() ([]byte, error) {
params, err := g.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}
// GetSlugParameters builds and checks the slug parameters and return the result in a map object
func (g *GetAllTickersRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
func (g *GetAllTickersRequest) applySlugsToUrl(url string, slugs map[string]string) string {
for _k, _v := range slugs {
needleRE := regexp.MustCompile(":" + _k + "\\b")
url = needleRE.ReplaceAllString(url, _v)
}
return url
}
func (g *GetAllTickersRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
sliceValue := reflect.ValueOf(slice)
for _i := 0; _i < sliceValue.Len(); _i++ {
it := sliceValue.Index(_i).Interface()
_f(it)
}
}
func (g *GetAllTickersRequest) isVarSlice(_v interface{}) bool {
rt := reflect.TypeOf(_v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (g *GetAllTickersRequest) GetSlugsMap() (map[string]string, error) {
slugs := map[string]string{}
params, err := g.GetSlugParameters()
if err != nil {
return slugs, nil
}
for _k, _v := range params {
slugs[_k] = fmt.Sprintf("%v", _v)
}
return slugs, nil
}
func (g *GetAllTickersRequest) Do(ctx context.Context) ([]Ticker, error) {
// no body params
var params interface{}
query := url.Values{}
apiURL := "/api/spot/v1/market/tickers"
req, err := g.client.NewRequest(ctx, "GET", apiURL, query, params)
if err != nil {
return nil, err
}
response, err := g.client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse APIResponse
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
var data []Ticker
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
return nil, err
}
return data, nil
}

View File

@ -1,31 +0,0 @@
package bitgetapi
//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Data
//go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Data
import (
"github.com/c9s/requestgen"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
type Candle struct {
Open fixedpoint.Value `json:"open"`
High fixedpoint.Value `json:"high"`
Low fixedpoint.Value `json:"low"`
Close fixedpoint.Value `json:"close"`
QuoteVol fixedpoint.Value `json:"quoteVol"`
BaseVol fixedpoint.Value `json:"baseVol"`
UsdtVol fixedpoint.Value `json:"usdtVol"`
Ts types.MillisecondTimestamp `json:"ts"`
}
//go:generate GetRequest -url "/api/spot/v1/market/candles" -type GetCandlesRequest -responseDataType []Candle
type GetCandlesRequest struct {
client requestgen.APIClient
}
func (c *RestClient) NewGetCandlesRequest() *GetCandlesRequest {
return &GetCandlesRequest{client: c}
}

View File

@ -1,139 +0,0 @@
// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Data -url /api/spot/v1/market/candles -type GetCandlesRequest -responseDataType []Candle"; DO NOT EDIT.
package bitgetapi
import (
"context"
"encoding/json"
"fmt"
"net/url"
"reflect"
"regexp"
)
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (g *GetCandlesRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
query := url.Values{}
for _k, _v := range params {
query.Add(_k, fmt.Sprintf("%v", _v))
}
return query, nil
}
// GetParameters builds and checks the parameters and return the result in a map object
func (g *GetCandlesRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (g *GetCandlesRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := g.GetParameters()
if err != nil {
return query, err
}
for _k, _v := range params {
if g.isVarSlice(_v) {
g.iterateSlice(_v, func(it interface{}) {
query.Add(_k+"[]", fmt.Sprintf("%v", it))
})
} else {
query.Add(_k, fmt.Sprintf("%v", _v))
}
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (g *GetCandlesRequest) GetParametersJSON() ([]byte, error) {
params, err := g.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}
// GetSlugParameters builds and checks the slug parameters and return the result in a map object
func (g *GetCandlesRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
func (g *GetCandlesRequest) applySlugsToUrl(url string, slugs map[string]string) string {
for _k, _v := range slugs {
needleRE := regexp.MustCompile(":" + _k + "\\b")
url = needleRE.ReplaceAllString(url, _v)
}
return url
}
func (g *GetCandlesRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
sliceValue := reflect.ValueOf(slice)
for _i := 0; _i < sliceValue.Len(); _i++ {
it := sliceValue.Index(_i).Interface()
_f(it)
}
}
func (g *GetCandlesRequest) isVarSlice(_v interface{}) bool {
rt := reflect.TypeOf(_v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (g *GetCandlesRequest) GetSlugsMap() (map[string]string, error) {
slugs := map[string]string{}
params, err := g.GetSlugParameters()
if err != nil {
return slugs, nil
}
for _k, _v := range params {
slugs[_k] = fmt.Sprintf("%v", _v)
}
return slugs, nil
}
func (g *GetCandlesRequest) Do(ctx context.Context) ([]Candle, error) {
// no body params
var params interface{}
query := url.Values{}
apiURL := "/api/spot/v1/market/candles"
req, err := g.client.NewRequest(ctx, "GET", apiURL, query, params)
if err != nil {
return nil, err
}
response, err := g.client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse APIResponse
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
var data []Candle
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
return nil, err
}
return data, nil
}

View File

@ -1,30 +0,0 @@
package bitgetapi
//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Data
//go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Data
import (
"github.com/c9s/requestgen"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
type Depth struct {
Asks [][]fixedpoint.Value `json:"asks"`
Bids [][]fixedpoint.Value `json:"bids"`
Timestamp types.MillisecondTimestamp `json:"timestamp"`
}
//go:generate GetRequest -url "/api/spot/v1/market/depth" -type GetDepthRequest -responseDataType .Depth
type GetDepthRequest struct {
client requestgen.APIClient
symbol string `param:"symbol"`
stepType string `param:"type" default:"step0"`
limit *int `param:"limit"`
}
func (c *RestClient) NewGetDepthRequest() *GetDepthRequest {
return &GetDepthRequest{client: c}
}

View File

@ -1,175 +0,0 @@
// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Data -url /api/spot/v1/market/depth -type GetDepthRequest -responseDataType .Depth"; DO NOT EDIT.
package bitgetapi
import (
"context"
"encoding/json"
"fmt"
"net/url"
"reflect"
"regexp"
)
func (g *GetDepthRequest) Symbol(symbol string) *GetDepthRequest {
g.symbol = symbol
return g
}
func (g *GetDepthRequest) StepType(stepType string) *GetDepthRequest {
g.stepType = stepType
return g
}
func (g *GetDepthRequest) Limit(limit int) *GetDepthRequest {
g.limit = &limit
return g
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (g *GetDepthRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
query := url.Values{}
for _k, _v := range params {
query.Add(_k, fmt.Sprintf("%v", _v))
}
return query, nil
}
// GetParameters builds and checks the parameters and return the result in a map object
func (g *GetDepthRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check symbol field -> json key symbol
symbol := g.symbol
// assign parameter of symbol
params["symbol"] = symbol
// check stepType field -> json key type
stepType := g.stepType
// assign parameter of stepType
params["type"] = stepType
// check limit field -> json key limit
if g.limit != nil {
limit := *g.limit
// assign parameter of limit
params["limit"] = limit
} else {
}
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (g *GetDepthRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := g.GetParameters()
if err != nil {
return query, err
}
for _k, _v := range params {
if g.isVarSlice(_v) {
g.iterateSlice(_v, func(it interface{}) {
query.Add(_k+"[]", fmt.Sprintf("%v", it))
})
} else {
query.Add(_k, fmt.Sprintf("%v", _v))
}
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (g *GetDepthRequest) GetParametersJSON() ([]byte, error) {
params, err := g.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}
// GetSlugParameters builds and checks the slug parameters and return the result in a map object
func (g *GetDepthRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
func (g *GetDepthRequest) applySlugsToUrl(url string, slugs map[string]string) string {
for _k, _v := range slugs {
needleRE := regexp.MustCompile(":" + _k + "\\b")
url = needleRE.ReplaceAllString(url, _v)
}
return url
}
func (g *GetDepthRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
sliceValue := reflect.ValueOf(slice)
for _i := 0; _i < sliceValue.Len(); _i++ {
it := sliceValue.Index(_i).Interface()
_f(it)
}
}
func (g *GetDepthRequest) isVarSlice(_v interface{}) bool {
rt := reflect.TypeOf(_v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (g *GetDepthRequest) GetSlugsMap() (map[string]string, error) {
slugs := map[string]string{}
params, err := g.GetSlugParameters()
if err != nil {
return slugs, nil
}
for _k, _v := range params {
slugs[_k] = fmt.Sprintf("%v", _v)
}
return slugs, nil
}
func (g *GetDepthRequest) Do(ctx context.Context) (*Depth, error) {
// empty params for GET operation
var params interface{}
query, err := g.GetParametersQuery()
if err != nil {
return nil, err
}
apiURL := "/api/spot/v1/market/depth"
req, err := g.client.NewRequest(ctx, "GET", apiURL, query, params)
if err != nil {
return nil, err
}
response, err := g.client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse APIResponse
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
var data Depth
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
return nil, err
}
return &data, nil
}

View File

@ -1,43 +0,0 @@
package bitgetapi
//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Data
//go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Data
import (
"github.com/c9s/requestgen"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
type Fill struct {
AccountId types.StrInt64 `json:"accountId"`
Symbol string `json:"symbol"`
OrderId types.StrInt64 `json:"orderId"`
FillId types.StrInt64 `json:"fillId"`
OrderType OrderType `json:"orderType"`
Side OrderSide `json:"side"`
FillPrice fixedpoint.Value `json:"fillPrice"`
FillQuantity fixedpoint.Value `json:"fillQuantity"`
FillTotalAmount fixedpoint.Value `json:"fillTotalAmount"`
CreationTime types.MillisecondTimestamp `json:"cTime"`
FeeCurrency string `json:"feeCcy"`
Fees fixedpoint.Value `json:"fees"`
}
//go:generate GetRequest -url "/api/spot/v1/trade/fills" -type GetFillsRequest -responseDataType .ServerTime
type GetFillsRequest struct {
client requestgen.AuthenticatedAPIClient
// after - order id
after *string `param:"after"`
// before - order id
before *string `param:"before"`
limit *string `param:"limit"`
}
func (c *RestClient) NewGetFillsRequest() *GetFillsRequest {
return &GetFillsRequest{client: c}
}

View File

@ -1,182 +0,0 @@
// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Data -url /api/spot/v1/trade/fills -type GetFillsRequest -responseDataType .ServerTime"; DO NOT EDIT.
package bitgetapi
import (
"context"
"encoding/json"
"fmt"
"github.com/c9s/bbgo/pkg/types"
"net/url"
"reflect"
"regexp"
)
func (g *GetFillsRequest) After(after string) *GetFillsRequest {
g.after = &after
return g
}
func (g *GetFillsRequest) Before(before string) *GetFillsRequest {
g.before = &before
return g
}
func (g *GetFillsRequest) Limit(limit string) *GetFillsRequest {
g.limit = &limit
return g
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (g *GetFillsRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
query := url.Values{}
for _k, _v := range params {
query.Add(_k, fmt.Sprintf("%v", _v))
}
return query, nil
}
// GetParameters builds and checks the parameters and return the result in a map object
func (g *GetFillsRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check after field -> json key after
if g.after != nil {
after := *g.after
// assign parameter of after
params["after"] = after
} else {
}
// check before field -> json key before
if g.before != nil {
before := *g.before
// assign parameter of before
params["before"] = before
} else {
}
// check limit field -> json key limit
if g.limit != nil {
limit := *g.limit
// assign parameter of limit
params["limit"] = limit
} else {
}
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (g *GetFillsRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := g.GetParameters()
if err != nil {
return query, err
}
for _k, _v := range params {
if g.isVarSlice(_v) {
g.iterateSlice(_v, func(it interface{}) {
query.Add(_k+"[]", fmt.Sprintf("%v", it))
})
} else {
query.Add(_k, fmt.Sprintf("%v", _v))
}
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (g *GetFillsRequest) GetParametersJSON() ([]byte, error) {
params, err := g.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}
// GetSlugParameters builds and checks the slug parameters and return the result in a map object
func (g *GetFillsRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
func (g *GetFillsRequest) applySlugsToUrl(url string, slugs map[string]string) string {
for _k, _v := range slugs {
needleRE := regexp.MustCompile(":" + _k + "\\b")
url = needleRE.ReplaceAllString(url, _v)
}
return url
}
func (g *GetFillsRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
sliceValue := reflect.ValueOf(slice)
for _i := 0; _i < sliceValue.Len(); _i++ {
it := sliceValue.Index(_i).Interface()
_f(it)
}
}
func (g *GetFillsRequest) isVarSlice(_v interface{}) bool {
rt := reflect.TypeOf(_v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (g *GetFillsRequest) GetSlugsMap() (map[string]string, error) {
slugs := map[string]string{}
params, err := g.GetSlugParameters()
if err != nil {
return slugs, nil
}
for _k, _v := range params {
slugs[_k] = fmt.Sprintf("%v", _v)
}
return slugs, nil
}
func (g *GetFillsRequest) Do(ctx context.Context) (*types.MillisecondTimestamp, error) {
// empty params for GET operation
var params interface{}
query, err := g.GetParametersQuery()
if err != nil {
return nil, err
}
apiURL := "/api/spot/v1/trade/fills"
req, err := g.client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params)
if err != nil {
return nil, err
}
response, err := g.client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse APIResponse
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
var data types.MillisecondTimestamp
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
return nil, err
}
return &data, nil
}

View File

@ -1,19 +0,0 @@
package bitgetapi
//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Data
//go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Data
import (
"github.com/c9s/requestgen"
)
//go:generate GetRequest -url "/api/spot/v1/trade/open-orders" -type GetOpenOrdersRequest -responseDataType []OrderDetail
type GetOpenOrdersRequest struct {
client requestgen.AuthenticatedAPIClient
symbol string `param:"symbol"`
}
func (c *RestClient) NewGetOpenOrdersRequest() *GetOpenOrdersRequest {
return &GetOpenOrdersRequest{client: c}
}

View File

@ -1,152 +0,0 @@
// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Data -url /api/spot/v1/trade/open-orders -type GetOpenOrdersRequest -responseDataType []OrderDetail"; DO NOT EDIT.
package bitgetapi
import (
"context"
"encoding/json"
"fmt"
"net/url"
"reflect"
"regexp"
)
func (g *GetOpenOrdersRequest) Symbol(symbol string) *GetOpenOrdersRequest {
g.symbol = symbol
return g
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (g *GetOpenOrdersRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
query := url.Values{}
for _k, _v := range params {
query.Add(_k, fmt.Sprintf("%v", _v))
}
return query, nil
}
// GetParameters builds and checks the parameters and return the result in a map object
func (g *GetOpenOrdersRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check symbol field -> json key symbol
symbol := g.symbol
// assign parameter of symbol
params["symbol"] = symbol
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (g *GetOpenOrdersRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := g.GetParameters()
if err != nil {
return query, err
}
for _k, _v := range params {
if g.isVarSlice(_v) {
g.iterateSlice(_v, func(it interface{}) {
query.Add(_k+"[]", fmt.Sprintf("%v", it))
})
} else {
query.Add(_k, fmt.Sprintf("%v", _v))
}
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (g *GetOpenOrdersRequest) GetParametersJSON() ([]byte, error) {
params, err := g.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}
// GetSlugParameters builds and checks the slug parameters and return the result in a map object
func (g *GetOpenOrdersRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
func (g *GetOpenOrdersRequest) applySlugsToUrl(url string, slugs map[string]string) string {
for _k, _v := range slugs {
needleRE := regexp.MustCompile(":" + _k + "\\b")
url = needleRE.ReplaceAllString(url, _v)
}
return url
}
func (g *GetOpenOrdersRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
sliceValue := reflect.ValueOf(slice)
for _i := 0; _i < sliceValue.Len(); _i++ {
it := sliceValue.Index(_i).Interface()
_f(it)
}
}
func (g *GetOpenOrdersRequest) isVarSlice(_v interface{}) bool {
rt := reflect.TypeOf(_v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (g *GetOpenOrdersRequest) GetSlugsMap() (map[string]string, error) {
slugs := map[string]string{}
params, err := g.GetSlugParameters()
if err != nil {
return slugs, nil
}
for _k, _v := range params {
slugs[_k] = fmt.Sprintf("%v", _v)
}
return slugs, nil
}
func (g *GetOpenOrdersRequest) Do(ctx context.Context) ([]OrderDetail, error) {
// empty params for GET operation
var params interface{}
query, err := g.GetParametersQuery()
if err != nil {
return nil, err
}
apiURL := "/api/spot/v1/trade/open-orders"
req, err := g.client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params)
if err != nil {
return nil, err
}
response, err := g.client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse APIResponse
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
var data []OrderDetail
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
return nil, err
}
return data, nil
}

View File

@ -1,41 +0,0 @@
package bitgetapi
//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Data
//go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Data
import (
"github.com/c9s/requestgen"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
type OrderDetail struct {
AccountId types.StrInt64 `json:"accountId"`
Symbol string `json:"symbol"`
OrderId types.StrInt64 `json:"orderId"`
ClientOrderId string `json:"clientOrderId"`
Price fixedpoint.Value `json:"price"`
Quantity fixedpoint.Value `json:"quantity"`
OrderType OrderType `json:"orderType"`
Side OrderSide `json:"side"`
Status OrderStatus `json:"status"`
FillPrice fixedpoint.Value `json:"fillPrice"`
FillQuantity fixedpoint.Value `json:"fillQuantity"`
FillTotalAmount fixedpoint.Value `json:"fillTotalAmount"`
EnterPointSource string `json:"enterPointSource"`
CTime types.MillisecondTimestamp `json:"cTime"`
}
//go:generate PostRequest -url "/api/spot/v1/trade/orderInfo" -type GetOrderDetailRequest -responseDataType []OrderDetail
type GetOrderDetailRequest struct {
client requestgen.AuthenticatedAPIClient
symbol string `param:"symbol"`
orderId *string `param:"orderId"`
clientOrderId *string `param:"clientOid"`
}
func (c *RestClient) NewGetOrderDetailRequest() *GetOrderDetailRequest {
return &GetOrderDetailRequest{client: c}
}

View File

@ -1,177 +0,0 @@
// Code generated by "requestgen -method POST -responseType .APIResponse -responseDataField Data -url /api/spot/v1/trade/orderInfo -type GetOrderDetailRequest -responseDataType []OrderDetail"; DO NOT EDIT.
package bitgetapi
import (
"context"
"encoding/json"
"fmt"
"net/url"
"reflect"
"regexp"
)
func (g *GetOrderDetailRequest) Symbol(symbol string) *GetOrderDetailRequest {
g.symbol = symbol
return g
}
func (g *GetOrderDetailRequest) OrderId(orderId string) *GetOrderDetailRequest {
g.orderId = &orderId
return g
}
func (g *GetOrderDetailRequest) ClientOrderId(clientOrderId string) *GetOrderDetailRequest {
g.clientOrderId = &clientOrderId
return g
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (g *GetOrderDetailRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
query := url.Values{}
for _k, _v := range params {
query.Add(_k, fmt.Sprintf("%v", _v))
}
return query, nil
}
// GetParameters builds and checks the parameters and return the result in a map object
func (g *GetOrderDetailRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check symbol field -> json key symbol
symbol := g.symbol
// assign parameter of symbol
params["symbol"] = symbol
// check orderId field -> json key orderId
if g.orderId != nil {
orderId := *g.orderId
// assign parameter of orderId
params["orderId"] = orderId
} else {
}
// check clientOrderId field -> json key clientOid
if g.clientOrderId != nil {
clientOrderId := *g.clientOrderId
// assign parameter of clientOrderId
params["clientOid"] = clientOrderId
} else {
}
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (g *GetOrderDetailRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := g.GetParameters()
if err != nil {
return query, err
}
for _k, _v := range params {
if g.isVarSlice(_v) {
g.iterateSlice(_v, func(it interface{}) {
query.Add(_k+"[]", fmt.Sprintf("%v", it))
})
} else {
query.Add(_k, fmt.Sprintf("%v", _v))
}
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (g *GetOrderDetailRequest) GetParametersJSON() ([]byte, error) {
params, err := g.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}
// GetSlugParameters builds and checks the slug parameters and return the result in a map object
func (g *GetOrderDetailRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
func (g *GetOrderDetailRequest) applySlugsToUrl(url string, slugs map[string]string) string {
for _k, _v := range slugs {
needleRE := regexp.MustCompile(":" + _k + "\\b")
url = needleRE.ReplaceAllString(url, _v)
}
return url
}
func (g *GetOrderDetailRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
sliceValue := reflect.ValueOf(slice)
for _i := 0; _i < sliceValue.Len(); _i++ {
it := sliceValue.Index(_i).Interface()
_f(it)
}
}
func (g *GetOrderDetailRequest) isVarSlice(_v interface{}) bool {
rt := reflect.TypeOf(_v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (g *GetOrderDetailRequest) GetSlugsMap() (map[string]string, error) {
slugs := map[string]string{}
params, err := g.GetSlugParameters()
if err != nil {
return slugs, nil
}
for _k, _v := range params {
slugs[_k] = fmt.Sprintf("%v", _v)
}
return slugs, nil
}
func (g *GetOrderDetailRequest) Do(ctx context.Context) ([]OrderDetail, error) {
params, err := g.GetParameters()
if err != nil {
return nil, err
}
query := url.Values{}
apiURL := "/api/spot/v1/trade/orderInfo"
req, err := g.client.NewAuthenticatedRequest(ctx, "POST", apiURL, query, params)
if err != nil {
return nil, err
}
response, err := g.client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse APIResponse
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
var data []OrderDetail
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
return nil, err
}
return data, nil
}

View File

@ -1,27 +0,0 @@
package bitgetapi
//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Data
//go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Data
import (
"github.com/c9s/requestgen"
)
//go:generate GetRequest -url "/api/spot/v1/trade/history" -type GetOrderHistoryRequest -responseDataType []OrderDetail
type GetOrderHistoryRequest struct {
client requestgen.AuthenticatedAPIClient
symbol string `param:"symbol"`
// after - order id
after *string `param:"after"`
// before - order id
before *string `param:"before"`
limit *string `param:"limit"`
}
func (c *RestClient) NewGetOrderHistoryRequest() *GetOrderHistoryRequest {
return &GetOrderHistoryRequest{client: c}
}

View File

@ -1,191 +0,0 @@
// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Data -url /api/spot/v1/trade/history -type GetOrderHistoryRequest -responseDataType []OrderDetail"; DO NOT EDIT.
package bitgetapi
import (
"context"
"encoding/json"
"fmt"
"net/url"
"reflect"
"regexp"
)
func (g *GetOrderHistoryRequest) Symbol(symbol string) *GetOrderHistoryRequest {
g.symbol = symbol
return g
}
func (g *GetOrderHistoryRequest) After(after string) *GetOrderHistoryRequest {
g.after = &after
return g
}
func (g *GetOrderHistoryRequest) Before(before string) *GetOrderHistoryRequest {
g.before = &before
return g
}
func (g *GetOrderHistoryRequest) Limit(limit string) *GetOrderHistoryRequest {
g.limit = &limit
return g
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (g *GetOrderHistoryRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
query := url.Values{}
for _k, _v := range params {
query.Add(_k, fmt.Sprintf("%v", _v))
}
return query, nil
}
// GetParameters builds and checks the parameters and return the result in a map object
func (g *GetOrderHistoryRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check symbol field -> json key symbol
symbol := g.symbol
// assign parameter of symbol
params["symbol"] = symbol
// check after field -> json key after
if g.after != nil {
after := *g.after
// assign parameter of after
params["after"] = after
} else {
}
// check before field -> json key before
if g.before != nil {
before := *g.before
// assign parameter of before
params["before"] = before
} else {
}
// check limit field -> json key limit
if g.limit != nil {
limit := *g.limit
// assign parameter of limit
params["limit"] = limit
} else {
}
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (g *GetOrderHistoryRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := g.GetParameters()
if err != nil {
return query, err
}
for _k, _v := range params {
if g.isVarSlice(_v) {
g.iterateSlice(_v, func(it interface{}) {
query.Add(_k+"[]", fmt.Sprintf("%v", it))
})
} else {
query.Add(_k, fmt.Sprintf("%v", _v))
}
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (g *GetOrderHistoryRequest) GetParametersJSON() ([]byte, error) {
params, err := g.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}
// GetSlugParameters builds and checks the slug parameters and return the result in a map object
func (g *GetOrderHistoryRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
func (g *GetOrderHistoryRequest) applySlugsToUrl(url string, slugs map[string]string) string {
for _k, _v := range slugs {
needleRE := regexp.MustCompile(":" + _k + "\\b")
url = needleRE.ReplaceAllString(url, _v)
}
return url
}
func (g *GetOrderHistoryRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
sliceValue := reflect.ValueOf(slice)
for _i := 0; _i < sliceValue.Len(); _i++ {
it := sliceValue.Index(_i).Interface()
_f(it)
}
}
func (g *GetOrderHistoryRequest) isVarSlice(_v interface{}) bool {
rt := reflect.TypeOf(_v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (g *GetOrderHistoryRequest) GetSlugsMap() (map[string]string, error) {
slugs := map[string]string{}
params, err := g.GetSlugParameters()
if err != nil {
return slugs, nil
}
for _k, _v := range params {
slugs[_k] = fmt.Sprintf("%v", _v)
}
return slugs, nil
}
func (g *GetOrderHistoryRequest) Do(ctx context.Context) ([]OrderDetail, error) {
// empty params for GET operation
var params interface{}
query, err := g.GetParametersQuery()
if err != nil {
return nil, err
}
apiURL := "/api/spot/v1/trade/history"
req, err := g.client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params)
if err != nil {
return nil, err
}
response, err := g.client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse APIResponse
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
var data []OrderDetail
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
return nil, err
}
return data, nil
}

View File

@ -1,21 +0,0 @@
package bitgetapi
//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Data
//go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Data
import (
"github.com/c9s/requestgen"
"github.com/c9s/bbgo/pkg/types"
)
type ServerTime = types.MillisecondTimestamp
//go:generate GetRequest -url "/api/spot/v1/public/time" -type GetServerTimeRequest -responseDataType .ServerTime
type GetServerTimeRequest struct {
client requestgen.APIClient
}
func (c *RestClient) NewGetServerTimeRequest() *GetServerTimeRequest {
return &GetServerTimeRequest{client: c}
}

View File

@ -1,140 +0,0 @@
// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Data -url /api/spot/v1/public/time -type GetServerTimeRequest -responseDataType .ServerTime"; DO NOT EDIT.
package bitgetapi
import (
"context"
"encoding/json"
"fmt"
"github.com/c9s/bbgo/pkg/types"
"net/url"
"reflect"
"regexp"
)
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (g *GetServerTimeRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
query := url.Values{}
for _k, _v := range params {
query.Add(_k, fmt.Sprintf("%v", _v))
}
return query, nil
}
// GetParameters builds and checks the parameters and return the result in a map object
func (g *GetServerTimeRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (g *GetServerTimeRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := g.GetParameters()
if err != nil {
return query, err
}
for _k, _v := range params {
if g.isVarSlice(_v) {
g.iterateSlice(_v, func(it interface{}) {
query.Add(_k+"[]", fmt.Sprintf("%v", it))
})
} else {
query.Add(_k, fmt.Sprintf("%v", _v))
}
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (g *GetServerTimeRequest) GetParametersJSON() ([]byte, error) {
params, err := g.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}
// GetSlugParameters builds and checks the slug parameters and return the result in a map object
func (g *GetServerTimeRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
func (g *GetServerTimeRequest) applySlugsToUrl(url string, slugs map[string]string) string {
for _k, _v := range slugs {
needleRE := regexp.MustCompile(":" + _k + "\\b")
url = needleRE.ReplaceAllString(url, _v)
}
return url
}
func (g *GetServerTimeRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
sliceValue := reflect.ValueOf(slice)
for _i := 0; _i < sliceValue.Len(); _i++ {
it := sliceValue.Index(_i).Interface()
_f(it)
}
}
func (g *GetServerTimeRequest) isVarSlice(_v interface{}) bool {
rt := reflect.TypeOf(_v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (g *GetServerTimeRequest) GetSlugsMap() (map[string]string, error) {
slugs := map[string]string{}
params, err := g.GetSlugParameters()
if err != nil {
return slugs, nil
}
for _k, _v := range params {
slugs[_k] = fmt.Sprintf("%v", _v)
}
return slugs, nil
}
func (g *GetServerTimeRequest) Do(ctx context.Context) (*types.MillisecondTimestamp, error) {
// no body params
var params interface{}
query := url.Values{}
apiURL := "/api/spot/v1/public/time"
req, err := g.client.NewRequest(ctx, "GET", apiURL, query, params)
if err != nil {
return nil, err
}
response, err := g.client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse APIResponse
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
var data types.MillisecondTimestamp
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
return nil, err
}
return &data, nil
}

View File

@ -1,47 +0,0 @@
package bitgetapi
//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Data
//go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Data
import (
"github.com/c9s/requestgen"
"github.com/c9s/bbgo/pkg/fixedpoint"
)
type SymbolStatus string
const (
// SymbolOffline represent market is suspended, users cannot trade.
SymbolOffline SymbolStatus = "offline"
// SymbolGray represents market is online, but user trading is not available.
SymbolGray SymbolStatus = "gray"
// SymbolOnline trading begins, users can trade.
SymbolOnline SymbolStatus = "online"
)
type Symbol struct {
Symbol string `json:"symbol"`
SymbolName string `json:"symbolName"`
BaseCoin string `json:"baseCoin"`
QuoteCoin string `json:"quoteCoin"`
MinTradeAmount fixedpoint.Value `json:"minTradeAmount"`
MaxTradeAmount fixedpoint.Value `json:"maxTradeAmount"`
TakerFeeRate fixedpoint.Value `json:"takerFeeRate"`
MakerFeeRate fixedpoint.Value `json:"makerFeeRate"`
PriceScale fixedpoint.Value `json:"priceScale"`
QuantityScale fixedpoint.Value `json:"quantityScale"`
MinTradeUSDT fixedpoint.Value `json:"minTradeUSDT"`
Status SymbolStatus `json:"status"`
BuyLimitPriceRatio fixedpoint.Value `json:"buyLimitPriceRatio"`
SellLimitPriceRatio fixedpoint.Value `json:"sellLimitPriceRatio"`
}
//go:generate GetRequest -url "/api/spot/v1/public/products" -type GetSymbolsRequest -responseDataType []Symbol
type GetSymbolsRequest struct {
client requestgen.APIClient
}
func (c *RestClient) NewGetSymbolsRequest() *GetSymbolsRequest {
return &GetSymbolsRequest{client: c}
}

View File

@ -1,139 +0,0 @@
// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Data -url /api/spot/v1/public/products -type GetSymbolsRequest -responseDataType []Symbol"; DO NOT EDIT.
package bitgetapi
import (
"context"
"encoding/json"
"fmt"
"net/url"
"reflect"
"regexp"
)
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (g *GetSymbolsRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
query := url.Values{}
for _k, _v := range params {
query.Add(_k, fmt.Sprintf("%v", _v))
}
return query, nil
}
// GetParameters builds and checks the parameters and return the result in a map object
func (g *GetSymbolsRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (g *GetSymbolsRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := g.GetParameters()
if err != nil {
return query, err
}
for _k, _v := range params {
if g.isVarSlice(_v) {
g.iterateSlice(_v, func(it interface{}) {
query.Add(_k+"[]", fmt.Sprintf("%v", it))
})
} else {
query.Add(_k, fmt.Sprintf("%v", _v))
}
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (g *GetSymbolsRequest) GetParametersJSON() ([]byte, error) {
params, err := g.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}
// GetSlugParameters builds and checks the slug parameters and return the result in a map object
func (g *GetSymbolsRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
func (g *GetSymbolsRequest) applySlugsToUrl(url string, slugs map[string]string) string {
for _k, _v := range slugs {
needleRE := regexp.MustCompile(":" + _k + "\\b")
url = needleRE.ReplaceAllString(url, _v)
}
return url
}
func (g *GetSymbolsRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
sliceValue := reflect.ValueOf(slice)
for _i := 0; _i < sliceValue.Len(); _i++ {
it := sliceValue.Index(_i).Interface()
_f(it)
}
}
func (g *GetSymbolsRequest) isVarSlice(_v interface{}) bool {
rt := reflect.TypeOf(_v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (g *GetSymbolsRequest) GetSlugsMap() (map[string]string, error) {
slugs := map[string]string{}
params, err := g.GetSlugParameters()
if err != nil {
return slugs, nil
}
for _k, _v := range params {
slugs[_k] = fmt.Sprintf("%v", _v)
}
return slugs, nil
}
func (g *GetSymbolsRequest) Do(ctx context.Context) ([]Symbol, error) {
// no body params
var params interface{}
query := url.Values{}
apiURL := "/api/spot/v1/public/products"
req, err := g.client.NewRequest(ctx, "GET", apiURL, query, params)
if err != nil {
return nil, err
}
response, err := g.client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse APIResponse
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
var data []Symbol
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
return nil, err
}
return data, nil
}

View File

@ -1,40 +0,0 @@
package bitgetapi
//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Data
//go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Data
import (
"github.com/c9s/requestgen"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
type Ticker struct {
Symbol string `json:"symbol"`
High24H fixedpoint.Value `json:"high24h"`
Low24H fixedpoint.Value `json:"low24h"`
Close fixedpoint.Value `json:"close"`
QuoteVol fixedpoint.Value `json:"quoteVol"`
BaseVol fixedpoint.Value `json:"baseVol"`
UsdtVol fixedpoint.Value `json:"usdtVol"`
Ts types.MillisecondTimestamp `json:"ts"`
BuyOne fixedpoint.Value `json:"buyOne"`
SellOne fixedpoint.Value `json:"sellOne"`
BidSz fixedpoint.Value `json:"bidSz"`
AskSz fixedpoint.Value `json:"askSz"`
OpenUtc0 fixedpoint.Value `json:"openUtc0"`
ChangeUtc fixedpoint.Value `json:"changeUtc"`
Change fixedpoint.Value `json:"change"`
}
//go:generate GetRequest -url "/api/spot/v1/market/ticker" -type GetTickerRequest -responseDataType .Ticker
type GetTickerRequest struct {
client requestgen.APIClient
symbol string `param:"symbol"`
}
func (c *RestClient) NewGetTickerRequest() *GetTickerRequest {
return &GetTickerRequest{client: c}
}

View File

@ -1,152 +0,0 @@
// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Data -url /api/spot/v1/market/ticker -type GetTickerRequest -responseDataType .Ticker"; DO NOT EDIT.
package bitgetapi
import (
"context"
"encoding/json"
"fmt"
"net/url"
"reflect"
"regexp"
)
func (g *GetTickerRequest) Symbol(symbol string) *GetTickerRequest {
g.symbol = symbol
return g
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (g *GetTickerRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
query := url.Values{}
for _k, _v := range params {
query.Add(_k, fmt.Sprintf("%v", _v))
}
return query, nil
}
// GetParameters builds and checks the parameters and return the result in a map object
func (g *GetTickerRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check symbol field -> json key symbol
symbol := g.symbol
// assign parameter of symbol
params["symbol"] = symbol
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (g *GetTickerRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := g.GetParameters()
if err != nil {
return query, err
}
for _k, _v := range params {
if g.isVarSlice(_v) {
g.iterateSlice(_v, func(it interface{}) {
query.Add(_k+"[]", fmt.Sprintf("%v", it))
})
} else {
query.Add(_k, fmt.Sprintf("%v", _v))
}
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (g *GetTickerRequest) GetParametersJSON() ([]byte, error) {
params, err := g.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}
// GetSlugParameters builds and checks the slug parameters and return the result in a map object
func (g *GetTickerRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
func (g *GetTickerRequest) applySlugsToUrl(url string, slugs map[string]string) string {
for _k, _v := range slugs {
needleRE := regexp.MustCompile(":" + _k + "\\b")
url = needleRE.ReplaceAllString(url, _v)
}
return url
}
func (g *GetTickerRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
sliceValue := reflect.ValueOf(slice)
for _i := 0; _i < sliceValue.Len(); _i++ {
it := sliceValue.Index(_i).Interface()
_f(it)
}
}
func (g *GetTickerRequest) isVarSlice(_v interface{}) bool {
rt := reflect.TypeOf(_v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (g *GetTickerRequest) GetSlugsMap() (map[string]string, error) {
slugs := map[string]string{}
params, err := g.GetSlugParameters()
if err != nil {
return slugs, nil
}
for _k, _v := range params {
slugs[_k] = fmt.Sprintf("%v", _v)
}
return slugs, nil
}
func (g *GetTickerRequest) Do(ctx context.Context) (*Ticker, error) {
// empty params for GET operation
var params interface{}
query, err := g.GetParametersQuery()
if err != nil {
return nil, err
}
apiURL := "/api/spot/v1/market/ticker"
req, err := g.client.NewRequest(ctx, "GET", apiURL, query, params)
if err != nil {
return nil, err
}
response, err := g.client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse APIResponse
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
var data Ticker
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
return nil, err
}
return &data, nil
}

View File

@ -1,29 +0,0 @@
package bitgetapi
//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Data
//go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Data
import (
"github.com/c9s/requestgen"
)
type OrderResponse struct {
OrderId string `json:"orderId"`
ClientOrderId string `json:"clientOrderId"`
}
//go:generate PostRequest -url "/api/spot/v1/trade/orders" -type PlaceOrderRequest -responseDataType .OrderResponse
type PlaceOrderRequest struct {
client requestgen.AuthenticatedAPIClient
symbol string `param:"symbol"`
orderType OrderType `param:"orderType"`
side OrderSide `param:"side"`
force OrderForce `param:"force"`
price string `param:"price"`
quantity string `param:"quantity"`
clientOrderId *string `param:"clientOrderId"`
}
func (c *RestClient) NewPlaceOrderRequest() *PlaceOrderRequest {
return &PlaceOrderRequest{client: c}
}

View File

@ -1,247 +0,0 @@
// Code generated by "requestgen -method POST -responseType .APIResponse -responseDataField Data -url /api/spot/v1/trade/orders -type PlaceOrderRequest -responseDataType .OrderResponse"; DO NOT EDIT.
package bitgetapi
import (
"context"
"encoding/json"
"fmt"
"net/url"
"reflect"
"regexp"
)
func (p *PlaceOrderRequest) Symbol(symbol string) *PlaceOrderRequest {
p.symbol = symbol
return p
}
func (p *PlaceOrderRequest) OrderType(orderType OrderType) *PlaceOrderRequest {
p.orderType = orderType
return p
}
func (p *PlaceOrderRequest) Side(side OrderSide) *PlaceOrderRequest {
p.side = side
return p
}
func (p *PlaceOrderRequest) Force(force OrderForce) *PlaceOrderRequest {
p.force = force
return p
}
func (p *PlaceOrderRequest) Price(price string) *PlaceOrderRequest {
p.price = price
return p
}
func (p *PlaceOrderRequest) Quantity(quantity string) *PlaceOrderRequest {
p.quantity = quantity
return p
}
func (p *PlaceOrderRequest) ClientOrderId(clientOrderId string) *PlaceOrderRequest {
p.clientOrderId = &clientOrderId
return p
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (p *PlaceOrderRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
query := url.Values{}
for _k, _v := range params {
query.Add(_k, fmt.Sprintf("%v", _v))
}
return query, nil
}
// GetParameters builds and checks the parameters and return the result in a map object
func (p *PlaceOrderRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check symbol field -> json key symbol
symbol := p.symbol
// assign parameter of symbol
params["symbol"] = symbol
// check orderType field -> json key orderType
orderType := p.orderType
// TEMPLATE check-valid-values
switch orderType {
case OrderTypeLimit, OrderTypeMarket:
params["orderType"] = orderType
default:
return nil, fmt.Errorf("orderType value %v is invalid", orderType)
}
// END TEMPLATE check-valid-values
// assign parameter of orderType
params["orderType"] = orderType
// check side field -> json key side
side := p.side
// TEMPLATE check-valid-values
switch side {
case OrderSideBuy, OrderSideSell:
params["side"] = side
default:
return nil, fmt.Errorf("side value %v is invalid", side)
}
// END TEMPLATE check-valid-values
// assign parameter of side
params["side"] = side
// check force field -> json key force
force := p.force
// TEMPLATE check-valid-values
switch force {
case OrderForceGTC, OrderForcePostOnly, OrderForceFOK, OrderForceIOC:
params["force"] = force
default:
return nil, fmt.Errorf("force value %v is invalid", force)
}
// END TEMPLATE check-valid-values
// assign parameter of force
params["force"] = force
// check price field -> json key price
price := p.price
// assign parameter of price
params["price"] = price
// check quantity field -> json key quantity
quantity := p.quantity
// assign parameter of quantity
params["quantity"] = quantity
// check clientOrderId field -> json key clientOrderId
if p.clientOrderId != nil {
clientOrderId := *p.clientOrderId
// assign parameter of clientOrderId
params["clientOrderId"] = clientOrderId
} else {
}
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (p *PlaceOrderRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := p.GetParameters()
if err != nil {
return query, err
}
for _k, _v := range params {
if p.isVarSlice(_v) {
p.iterateSlice(_v, func(it interface{}) {
query.Add(_k+"[]", fmt.Sprintf("%v", it))
})
} else {
query.Add(_k, fmt.Sprintf("%v", _v))
}
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (p *PlaceOrderRequest) GetParametersJSON() ([]byte, error) {
params, err := p.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}
// GetSlugParameters builds and checks the slug parameters and return the result in a map object
func (p *PlaceOrderRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
func (p *PlaceOrderRequest) applySlugsToUrl(url string, slugs map[string]string) string {
for _k, _v := range slugs {
needleRE := regexp.MustCompile(":" + _k + "\\b")
url = needleRE.ReplaceAllString(url, _v)
}
return url
}
func (p *PlaceOrderRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
sliceValue := reflect.ValueOf(slice)
for _i := 0; _i < sliceValue.Len(); _i++ {
it := sliceValue.Index(_i).Interface()
_f(it)
}
}
func (p *PlaceOrderRequest) isVarSlice(_v interface{}) bool {
rt := reflect.TypeOf(_v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (p *PlaceOrderRequest) GetSlugsMap() (map[string]string, error) {
slugs := map[string]string{}
params, err := p.GetSlugParameters()
if err != nil {
return slugs, nil
}
for _k, _v := range params {
slugs[_k] = fmt.Sprintf("%v", _v)
}
return slugs, nil
}
func (p *PlaceOrderRequest) Do(ctx context.Context) (*OrderResponse, error) {
params, err := p.GetParameters()
if err != nil {
return nil, err
}
query := url.Values{}
apiURL := "/api/spot/v1/trade/orders"
req, err := p.client.NewAuthenticatedRequest(ctx, "POST", apiURL, query, params)
if err != nil {
return nil, err
}
response, err := p.client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse APIResponse
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
var data OrderResponse
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
return nil, err
}
return &data, nil
}

View File

@ -1,41 +0,0 @@
package bitgetapi
type SideType string
const (
SideTypeBuy SideType = "buy"
SideTypeSell SideType = "sell"
)
type OrderType string
const (
OrderTypeLimit OrderType = "limit"
OrderTypeMarket OrderType = "market"
)
type OrderSide string
const (
OrderSideBuy OrderSide = "buy"
OrderSideSell OrderSide = "sell"
)
type OrderForce string
const (
OrderForceGTC OrderForce = "normal"
OrderForcePostOnly OrderForce = "post_only"
OrderForceFOK OrderForce = "fok"
OrderForceIOC OrderForce = "ioc"
)
type OrderStatus string
const (
OrderStatusInit OrderStatus = "init"
OrderStatusNew OrderStatus = "new"
OrderStatusPartialFill OrderStatus = "partial_fill"
OrderStatusFullFill OrderStatus = "full_fill"
OrderStatusCancelled OrderStatus = "cancelled"
)

View File

@ -8,6 +8,7 @@ import (
const (
PrivateWebSocketURL = "wss://ws.bitget.com/v2/ws/private"
PublicWebSocketURL = "wss://ws.bitget.com/v2/ws/public"
)
type APIResponse = bitgetapi.APIResponse

View File

@ -120,7 +120,7 @@ func (s *Stream) Unsubscribe() {
func (s *Stream) createEndpoint(_ context.Context) (string, error) {
var url string
if s.PublicOnly {
url = bitgetapi.PublicWebSocketURL
url = v2.PublicWebSocketURL
} else {
url = v2.PrivateWebSocketURL
}
@ -245,7 +245,7 @@ func (s *Stream) ping(conn *websocket.Conn) error {
func convertSubscription(sub types.Subscription) (WsArg, error) {
arg := WsArg{
// support spot only
InstType: instSp,
InstType: instSpV2,
Channel: "",
InstId: sub.Symbol,
}

View File

@ -179,7 +179,7 @@ func TestStream_parseWebSocketEvent(t *testing.T) {
input := `{
"event":"subscribe",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"books5",
"instId":"BTCUSDT"
}
@ -191,7 +191,7 @@ func TestStream_parseWebSocketEvent(t *testing.T) {
assert.Equal(t, WsEvent{
Event: WsEventSubscribe,
Arg: WsArg{
InstType: instSp,
InstType: instSpV2,
Channel: ChannelOrderBook5,
InstId: "BTCUSDT",
},
@ -204,7 +204,7 @@ func TestStream_parseWebSocketEvent(t *testing.T) {
input := `{
"event":"unsubscribe",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"books5",
"instId":"BTCUSDT"
}
@ -216,7 +216,7 @@ func TestStream_parseWebSocketEvent(t *testing.T) {
assert.Equal(t, WsEvent{
Event: WsEventUnsubscribe,
Arg: WsArg{
InstType: instSp,
InstType: instSpV2,
Channel: ChannelOrderBook5,
InstId: "BTCUSDT",
},
@ -227,7 +227,7 @@ func TestStream_parseWebSocketEvent(t *testing.T) {
input := `{
"event":"error",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"books5",
"instId":"BTCUSDT-"
},
@ -245,7 +245,7 @@ func TestStream_parseWebSocketEvent(t *testing.T) {
Msg: "instType:sp,channel:books5,instId:BTCUSDT- doesn't exist",
Op: "subscribe",
Arg: WsArg{
InstType: instSp,
InstType: instSpV2,
Channel: ChannelOrderBook5,
InstId: "BTCUSDT-",
},
@ -256,7 +256,7 @@ func TestStream_parseWebSocketEvent(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"books5",
"instId":"BTCUSDT"
},
@ -349,24 +349,26 @@ func Test_parseWebSocketEvent_MarketTrade(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"trade",
"instId":"BTCUSDT"
},
"data":[
[
"1697697791663",
"28303.43",
"0.0452",
"sell"
],
[
"1697697794663",
"28345.67",
"0.1234",
"sell"
]
],
{
"ts":"1723476690562",
"price":"59440.52",
"size":"0.018545",
"side":"sell",
"tradeId":"1206914205132210181"
},
{
"ts":"1723476690562",
"price":"59440.52",
"size":"0.001255",
"side":"sell",
"tradeId":"1206914205132210179"
}
],
"ts":1697697791670
}`
@ -378,17 +380,19 @@ func Test_parseWebSocketEvent_MarketTrade(t *testing.T) {
assert.Equal(t, MarketTradeEvent{
Events: MarketTradeSlice{
{
Ts: types.NewMillisecondTimestampFromInt(1697697791663),
Price: fixedpoint.NewFromFloat(28303.43),
Size: fixedpoint.NewFromFloat(0.0452),
Side: "sell",
Ts: types.NewMillisecondTimestampFromInt(1723476690562),
Price: fixedpoint.NewFromFloat(59440.52),
Size: fixedpoint.NewFromFloat(0.018545),
Side: "sell",
TradeId: 1206914205132210181,
},
{
Ts: types.NewMillisecondTimestampFromInt(1697697794663),
Price: fixedpoint.NewFromFloat(28345.67),
Size: fixedpoint.NewFromFloat(0.1234),
Side: "sell",
Ts: types.NewMillisecondTimestampFromInt(1723476690562),
Price: fixedpoint.NewFromFloat(59440.52),
Size: fixedpoint.NewFromFloat(0.001255),
Side: "sell",
TradeId: 1206914205132210179,
},
},
actionType: actionType,
@ -407,115 +411,66 @@ func Test_parseWebSocketEvent_MarketTrade(t *testing.T) {
})
})
t.Run("Unexpected length of market trade", func(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"channel":"trade",
"instId":"BTCUSDT"
},
"data":[
[
"1697697791663",
"28303.43",
"28303.43",
"0.0452",
"sell"
]
],
"ts":1697697791670
}`
_, err := parseWebSocketEvent([]byte(input))
assert.ErrorContains(t, err, "unexpected trades length")
})
t.Run("Unexpected timestamp", func(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"trade",
"instId":"BTCUSDT"
},
"data":[
[
"TIMESTAMP",
"28303.43",
"0.0452",
"sell"
]
],
"data":[{"ts":"TIMESTAMP","price":"59440.52","size":"0.018545","side":"sell","tradeId":"1206914205132210181"},{"ts":"1723476690562","price":"59440.52","size":"0.001255","side":"sell","tradeId":"1206914205132210179"}],
"ts":1697697791670
}`
_, err := parseWebSocketEvent([]byte(input))
assert.ErrorContains(t, err, "timestamp")
assert.ErrorContains(t, err, "failed to unmarshal data")
})
t.Run("Unexpected price", func(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"channel":"trade",
"instId":"BTCUSDT"
},
"data":[
[
"1697697791663",
"1p",
"0.0452",
"sell"
]
],
"ts":1697697791670
}`
_, err := parseWebSocketEvent([]byte(input))
assert.ErrorContains(t, err, "price")
})
t.Run("Unexpected size", func(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"channel":"trade",
"instId":"BTCUSDT"
},
"data":[
[
"1697697791663",
"28303.43",
"2v",
"sell"
]
],
"ts":1697697791670
}`
_, err := parseWebSocketEvent([]byte(input))
assert.ErrorContains(t, err, "size")
})
// TODO: If a non-numeric value causes panic, then let's comment out this test for now.
//t.Run("Unexpected price", func(t *testing.T) {
// input := `{
// "action":"%s",
// "arg":{
// "instType":"SPOT",
// "channel":"trade",
// "instId":"BTCUSDT"
// },
// "data":[{"ts":"1723476690562","price":"UNEXPECTED","size":"0.018545","side":"sell","tradeId":"1206914205132210181"},{"ts":"1723476690562","price":"59440.52","size":"0.001255","side":"sell","tradeId":"1206914205132210179"}],
// "ts":1697697791670
// }`
// _, err := parseWebSocketEvent([]byte(input))
// assert.ErrorContains(t, err, "failed to unmarshal data")
//})
//
// TODO: If a non-numeric value causes panic, then let's comment out this test for now.
//t.Run("Unexpected size", func(t *testing.T) {
// input := `{
// "action":"%s",
// "arg":{
// "instType":"SPOT",
// "channel":"trade",
// "instId":"BTCUSDT"
// },
// "data":[{"ts":"1723476690562","price":"59440.52","size":"2v","side":"sell","tradeId":"1206914205132210181"},{"ts":"1723476690562","price":"59440.52","size":"0.001255","side":"sell","tradeId":"1206914205132210179"}],
// "ts":1697697791670
// }`
// _, err := parseWebSocketEvent([]byte(input))
// assert.ErrorContains(t, err, "failed to unmarshal data")
//})
t.Run("Unexpected side", func(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"trade",
"instId":"BTCUSDT"
},
"data":[
[
"1697697791663",
"28303.43",
"0.0452",
12345
]
],
"data":[{"ts":"1723476690562","price":"59440.52","size":"0.018545","side":"ssss","tradeId":"1206914205132210181"},{"ts":"1723476690562","price":"59440.52","size":"0.001255","side":"sell","tradeId":"1206914205132210179"}],
"ts":1697697791670
}`
_, err := parseWebSocketEvent([]byte(input))
assert.ErrorContains(t, err, "side")
assert.ErrorContains(t, err, "failed to unmarshal data")
})
}
@ -524,12 +479,12 @@ func Test_parseWebSocketEvent_KLine(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"candle5m",
"instId":"BTCUSDT"
},
"data":[
["1698744600000","34361.49","34458.98","34355.53","34416.41","99.6631"]
["1698744600000","34361.49","34458.98","34355.53","34416.41","99.6631", "123456", "123"]
],
"ts":1697697791670
}`
@ -549,6 +504,7 @@ func Test_parseWebSocketEvent_KLine(t *testing.T) {
LowestPrice: fixedpoint.NewFromFloat(34355.53),
ClosePrice: fixedpoint.NewFromFloat(34416.41),
Volume: fixedpoint.NewFromFloat(99.6631),
QuoteVolume: fixedpoint.NewFromFloat(123456),
},
},
actionType: actionType,
@ -571,12 +527,12 @@ func Test_parseWebSocketEvent_KLine(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"candle5m",
"instId":"BTCUSDT"
},
"data":[
["1698744600000","34361.45","34458.98","34355.53","34416.41","99.6631", "123456"]
["1698744600000","34361.49","34458.98","34355.53","34416.41","99.6631", "123456", "123", "123"]
],
"ts":1697697791670
}`
@ -588,12 +544,12 @@ func Test_parseWebSocketEvent_KLine(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"candle5m",
"instId":"BTCUSDT"
},
"data":[
["timestamp","34361.49","34458.98","34355.53","34416.41","99.6631"]
["timestamp","34361.49","34458.98","34355.53","34416.41","99.6631", "123456", "123"]
],
"ts":1697697791670
}`
@ -605,12 +561,12 @@ func Test_parseWebSocketEvent_KLine(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"candle5m",
"instId":"BTCUSDT"
},
"data":[
["1698744600000","1p","34458.98","34355.53","34416.41","99.6631"]
["1698744600000","1p","34458.98","34355.53","34416.41","99.6631", "123456", "123"]
],
"ts":1697697791670
}`
@ -622,12 +578,12 @@ func Test_parseWebSocketEvent_KLine(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"candle5m",
"instId":"BTCUSDT"
},
"data":[
["1698744600000","34361.45","3p","34355.53","34416.41","99.6631"]
["1698744600000","34361.49","3p","34355.53","34416.41","99.6631", "123456", "123"]
],
"ts":1697697791670
}`
@ -639,12 +595,12 @@ func Test_parseWebSocketEvent_KLine(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"candle5m",
"instId":"BTCUSDT"
},
"data":[
["1698744600000","34361.45","34458.98","1p","34416.41","99.6631"]
["1698744600000","34361.49","34458.98","1p","34416.41","99.6631", "123456", "123"]
],
"ts":1697697791670
}`
@ -656,12 +612,12 @@ func Test_parseWebSocketEvent_KLine(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"candle5m",
"instId":"BTCUSDT"
},
"data":[
["1698744600000","34361.45","34458.98","34355.53","1c","99.6631"]
["1698744600000","34361.49","34458.98","34355.53","1c","99.6631", "123456", "123"]
],
"ts":1697697791670
}`
@ -673,12 +629,12 @@ func Test_parseWebSocketEvent_KLine(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"candle5m",
"instId":"BTCUSDT"
},
"data":[
["1698744600000","34361.45","34458.98","34355.53","34416.41", "1v"]
["1698744600000","34361.49","34458.98","34355.53","34416.41","1v", "123456", "123"]
],
"ts":1697697791670
}`
@ -698,7 +654,7 @@ func Test_convertSubscription(t *testing.T) {
})
assert.NoError(t, err)
assert.Equal(t, WsArg{
InstType: instSp,
InstType: instSpV2,
Channel: ChannelOrderBook5,
InstId: "BTCUSDT",
}, res)
@ -713,7 +669,7 @@ func Test_convertSubscription(t *testing.T) {
})
assert.NoError(t, err)
assert.Equal(t, WsArg{
InstType: instSp,
InstType: instSpV2,
Channel: ChannelOrderBook15,
InstId: "BTCUSDT",
}, res)
@ -728,7 +684,7 @@ func Test_convertSubscription(t *testing.T) {
})
assert.NoError(t, err)
assert.Equal(t, WsArg{
InstType: instSp,
InstType: instSpV2,
Channel: ChannelOrderBook,
InstId: "BTCUSDT",
}, res)
@ -741,7 +697,7 @@ func Test_convertSubscription(t *testing.T) {
})
assert.NoError(t, err)
assert.Equal(t, WsArg{
InstType: instSp,
InstType: instSpV2,
Channel: ChannelTrade,
InstId: "BTCUSDT",
}, res)
@ -757,7 +713,7 @@ func Test_convertSubscription(t *testing.T) {
})
assert.NoError(t, err)
assert.Equal(t, WsArg{
InstType: instSp,
InstType: instSpV2,
Channel: ChannelType(localInterval),
InstId: "BTCUSDT",
}, res)

View File

@ -14,7 +14,6 @@ import (
type InstType string
const (
instSp InstType = "sp"
instSpV2 InstType = "SPOT"
)
@ -163,6 +162,22 @@ const (
SideSell SideType = "sell"
)
func (s *SideType) UnmarshalJSON(b []byte) error {
var a string
err := json.Unmarshal(b, &a)
if err != nil {
return err
}
switch SideType(a) {
case SideSell, SideBuy:
*s = SideType(a)
return nil
default:
return fmt.Errorf("unexpected side type: %s", b)
}
}
func (s SideType) ToGlobal() (types.SideType, error) {
switch s {
case SideBuy:
@ -175,77 +190,15 @@ func (s SideType) ToGlobal() (types.SideType, error) {
}
type MarketTrade struct {
Ts types.MillisecondTimestamp
Price fixedpoint.Value
Size fixedpoint.Value
Side SideType
Ts types.MillisecondTimestamp
Price fixedpoint.Value
Size fixedpoint.Value
Side SideType
TradeId types.StrInt64
}
type MarketTradeSlice []MarketTrade
func (m *MarketTradeSlice) UnmarshalJSON(b []byte) error {
if m == nil {
return errors.New("nil pointer of market trade slice")
}
s, err := parseMarketTradeSliceJSON(b)
if err != nil {
return err
}
*m = s
return nil
}
// ParseMarketTradeSliceJSON tries to parse a 2 dimensional string array into a MarketTradeSlice
//
// [
//
// [
// "1697694819663",
// "28312.97",
// "0.1653",
// "sell"
// ],
// [
// "1697694818663",
// "28313",
// "0.1598",
// "buy"
// ]
//
// ]
func parseMarketTradeSliceJSON(in []byte) (slice MarketTradeSlice, err error) {
var rawTrades [][]json.RawMessage
err = json.Unmarshal(in, &rawTrades)
if err != nil {
return slice, err
}
for _, raw := range rawTrades {
if len(raw) != 4 {
return nil, fmt.Errorf("unexpected trades length: %d, data: %q", len(raw), raw)
}
var trade MarketTrade
if err = json.Unmarshal(raw[0], &trade.Ts); err != nil {
return nil, fmt.Errorf("failed to unmarshal into timestamp: %q", raw[0])
}
if err = json.Unmarshal(raw[1], &trade.Price); err != nil {
return nil, fmt.Errorf("failed to unmarshal into price: %q", raw[1])
}
if err = json.Unmarshal(raw[2], &trade.Size); err != nil {
return nil, fmt.Errorf("failed to unmarshal into size: %q", raw[2])
}
if err = json.Unmarshal(raw[3], &trade.Side); err != nil {
return nil, fmt.Errorf("failed to unmarshal into side: %q", raw[3])
}
slice = append(slice, trade)
}
return slice, nil
}
func (m MarketTrade) ToGlobal(symbol string) (types.Trade, error) {
side, err := m.Side.ToGlobal()
if err != nil {
@ -253,7 +206,7 @@ func (m MarketTrade) ToGlobal(symbol string) (types.Trade, error) {
}
return types.Trade{
ID: 0, // not supported
ID: uint64(m.TradeId),
OrderID: 0, // not supported
Exchange: types.ExchangeBitget,
Price: m.Price,
@ -285,21 +238,27 @@ var (
types.Interval30m: "candle30m",
types.Interval1h: "candle1H",
types.Interval4h: "candle4H",
types.Interval12h: "candle12H",
types.Interval1d: "candle1D",
types.Interval1w: "candle1W",
types.Interval6h: "candle6Hutc",
types.Interval12h: "candle12Hutc",
types.Interval1d: "candle1Dutc",
types.Interval3d: "candle3Dutc",
types.Interval1w: "candle1Wutc",
types.Interval1mo: "candle1Mutc",
}
toGlobalInterval = map[string]types.Interval{
"candle1m": types.Interval1m,
"candle5m": types.Interval5m,
"candle15m": types.Interval15m,
"candle30m": types.Interval30m,
"candle1H": types.Interval1h,
"candle4H": types.Interval4h,
"candle12H": types.Interval12h,
"candle1D": types.Interval1d,
"candle1W": types.Interval1w,
"candle1m": types.Interval1m,
"candle5m": types.Interval5m,
"candle15m": types.Interval15m,
"candle30m": types.Interval30m,
"candle1H": types.Interval1h,
"candle4H": types.Interval4h,
"candle6Hutc": types.Interval6h,
"candle12Hutc": types.Interval12h,
"candle1Dutc": types.Interval1d,
"candle3Dutc": types.Interval3d,
"candle1Wutc": types.Interval1w,
"candle1Mutc": types.Interval1mo,
}
// we align utc time zone
@ -345,6 +304,7 @@ type KLine struct {
LowestPrice fixedpoint.Value
ClosePrice fixedpoint.Value
Volume fixedpoint.Value
QuoteVolume fixedpoint.Value
}
func (k KLine) ToGlobal(interval types.Interval, symbol string) types.KLine {
@ -361,7 +321,7 @@ func (k KLine) ToGlobal(interval types.Interval, symbol string) types.KLine {
High: k.HighestPrice,
Low: k.LowestPrice,
Volume: k.Volume,
QuoteVolume: fixedpoint.Zero, // not supported
QuoteVolume: k.QuoteVolume,
TakerBuyBaseAssetVolume: fixedpoint.Zero, // not supported
TakerBuyQuoteAssetVolume: fixedpoint.Zero, // not supported
LastTradeID: 0, // not supported
@ -400,9 +360,10 @@ func parseKLineSliceJSON(in []byte) (slice KLineSlice, err error) {
}
for _, raw := range rawKLines {
if len(raw) != 6 {
if len(raw) != 8 {
return nil, fmt.Errorf("unexpected kline length: %d, data: %q", len(raw), raw)
}
// even though it supports 8 fields, we only parse the ones we need.
var kline KLine
if err = json.Unmarshal(raw[0], &kline.StartTime); err != nil {
return nil, fmt.Errorf("failed to unmarshal into timestamp: %q", raw[0])
@ -422,6 +383,9 @@ func parseKLineSliceJSON(in []byte) (slice KLineSlice, err error) {
if err = json.Unmarshal(raw[5], &kline.Volume); err != nil {
return nil, fmt.Errorf("failed to unmarshal into volume: %q", raw[5])
}
if err = json.Unmarshal(raw[6], &kline.QuoteVolume); err != nil {
return nil, fmt.Errorf("failed to unmarshal into quote volume: %q", raw[6])
}
slice = append(slice, kline)
}