From c11a665ef83a40c37e35edde466229178bbdc1fb Mon Sep 17 00:00:00 2001 From: dasbd72 Date: Sat, 2 Mar 2024 14:04:57 +0800 Subject: [PATCH] bitfinex add version into request --- bitfinex/client.go | 10 +++++----- bitfinex/request.go | 23 +++++++++++++++++------ bitfinex/v2_funding_service.go | 2 ++ bitfinex/v2_wallet_service.go | 1 + 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/bitfinex/client.go b/bitfinex/client.go index 1d3c7f6..2a31902 100644 --- a/bitfinex/client.go +++ b/bitfinex/client.go @@ -23,8 +23,8 @@ type Client struct { } const ( - basePublicApiURL = "https://api-pub.bitfinex.com/v2" - basePrivateApiURL = "https://api.bitfinex.com/v2" + basePublicApiURL = "https://api-pub.bitfinex.com" + basePrivateApiURL = "https://api.bitfinex.com" apiKeyHeader = "bfx-apikey" nonceHeader = "bfx-nonce" @@ -84,7 +84,7 @@ func (c *Client) CallAPI(ctx context.Context, r *Request, opts ...RequestOption) func (c *Client) getHttpRequest(ctx context.Context, r *Request) (*http.Request, error) { apiEndpoint := c.publicApiEndpoint - path := fmt.Sprintf("%s%s", r.endpoint, r.subEndpoint) + path := r.endpoint query := url.Values{} header := http.Header{} body := "" @@ -112,10 +112,10 @@ func (c *Client) getHttpRequest(ctx context.Context, r *Request) (*http.Request, nonce := currentTimestamp() header.Set(apiKeyHeader, c.apiKey) header.Set(nonceHeader, nonce) - header.Set(signatureHeader, sign(c.apiSecret, fmt.Sprintf("/api/v2%s%s%s", path, nonce, body))) + header.Set(signatureHeader, sign(c.apiSecret, fmt.Sprintf("/api%s%s%s%s", r.version, path, nonce, body))) } // create request - req, err := http.NewRequestWithContext(ctx, r.method, fmt.Sprintf("%s%s", apiEndpoint, path), bytes.NewBuffer([]byte(body))) + req, err := http.NewRequestWithContext(ctx, r.method, fmt.Sprintf("%s%s%s", apiEndpoint, r.version, path), bytes.NewBuffer([]byte(body))) if err != nil { return nil, err } diff --git a/bitfinex/request.go b/bitfinex/request.go index d66dcac..01988f9 100644 --- a/bitfinex/request.go +++ b/bitfinex/request.go @@ -1,9 +1,18 @@ package bitfinex -import "net/http" +import ( + "net/http" +) type SecType int +const ( + // Version1 is for v1 API + Version1 = "/v1" + // Version2 is for v2 API + Version2 = "/v2" +) + const ( // SecTypePublic is for public API SecTypePublic SecType = iota @@ -13,17 +22,18 @@ const ( // Request define an API request, build with Request_builder type Request struct { - method string - endpoint string - subEndpoint string - secType SecType - params map[string]interface{} + method string + endpoint string + version string + secType SecType + params map[string]interface{} } // Request_builder define a builder for Request type Request_builder struct { Method string Endpoint string + Version string SecType SecType Params map[string]interface{} } @@ -39,6 +49,7 @@ func (b Request_builder) Build() *Request { return &Request{ method: b.Method, endpoint: b.Endpoint, + version: b.Version, secType: b.SecType, params: b.Params, } diff --git a/bitfinex/v2_funding_service.go b/bitfinex/v2_funding_service.go index 5e424c0..4444cde 100644 --- a/bitfinex/v2_funding_service.go +++ b/bitfinex/v2_funding_service.go @@ -71,6 +71,7 @@ func (c *Client) GetFundingStats(ctx context.Context, symbol string, opts ...Req res, err := c.CallAPI(ctx, Request_builder{ Method: http.MethodGet, Endpoint: fmt.Sprintf("/funding/stats/%s/hist", symbol), + Version: Version2, SecType: SecTypePublic, }.Build(), opts...) if err != nil { @@ -120,6 +121,7 @@ func (c *Client) GetActiveFundingOffers(ctx context.Context, symbol string, opts res, err := c.CallAPI(ctx, Request_builder{ Method: http.MethodPost, Endpoint: fmt.Sprintf("/auth/r/funding/offers/%s", symbol), + Version: Version2, SecType: SecTypePrivate, }.Build(), opts...) if err != nil { diff --git a/bitfinex/v2_wallet_service.go b/bitfinex/v2_wallet_service.go index 1badef2..882de8d 100644 --- a/bitfinex/v2_wallet_service.go +++ b/bitfinex/v2_wallet_service.go @@ -47,6 +47,7 @@ func (c *Client) GetWallets(ctx context.Context, opts ...RequestOption) (*GetWal res, err := c.CallAPI(ctx, Request_builder{ Method: http.MethodPost, Endpoint: "/auth/r/wallets", + Version: Version2, SecType: SecTypePrivate, }.Build(), opts...) if err != nil {