bbgo_origin/pkg/exchange/max/maxapi/restapi.go

355 lines
9.0 KiB
Go
Raw Normal View History

2020-10-02 02:10:59 +00:00
package max
import (
2022-04-19 05:52:10 +00:00
"context"
2020-10-02 02:10:59 +00:00
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"math"
"net"
2020-10-02 02:10:59 +00:00
"net/http"
"net/http/httputil"
2020-10-02 02:10:59 +00:00
"net/url"
"regexp"
"strings"
"sync"
2020-10-02 02:10:59 +00:00
"sync/atomic"
"time"
2022-04-19 04:10:15 +00:00
"github.com/c9s/requestgen"
2021-06-07 15:11:53 +00:00
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
2022-04-19 04:10:15 +00:00
"github.com/c9s/bbgo/pkg/util"
"github.com/c9s/bbgo/pkg/util/backoff"
2022-04-19 04:10:15 +00:00
"github.com/c9s/bbgo/pkg/version"
2020-10-02 02:10:59 +00:00
)
const (
// ProductionAPIURL is the official MAX API v2 Endpoint
ProductionAPIURL = "https://max-api.maicoin.com/api/v2"
2021-02-22 07:16:12 +00:00
UserAgent = "bbgo/" + version.Version
2020-10-02 02:10:59 +00:00
2022-04-21 05:17:43 +00:00
defaultHTTPTimeout = time.Second * 60
// 2018-09-01 08:00:00 +0800 CST
TimestampSince = 1535760000
2023-04-12 06:58:37 +00:00
maxAllowedNegativeTimeOffset = -20
2020-10-02 02:10:59 +00:00
)
var httpTransportMaxIdleConnsPerHost = http.DefaultMaxIdleConnsPerHost
var httpTransportMaxIdleConns = 100
var httpTransportIdleConnTimeout = 90 * time.Second
var disableUserAgentHeader = false
func init() {
if val, ok := util.GetEnvVarInt("HTTP_TRANSPORT_MAX_IDLE_CONNS_PER_HOST"); ok {
httpTransportMaxIdleConnsPerHost = val
}
if val, ok := util.GetEnvVarInt("HTTP_TRANSPORT_MAX_IDLE_CONNS"); ok {
httpTransportMaxIdleConns = val
}
if val, ok := util.GetEnvVarDuration("HTTP_TRANSPORT_IDLE_CONN_TIMEOUT"); ok {
httpTransportIdleConnTimeout = val
}
if val, ok := util.GetEnvVarBool("DISABLE_MAX_USER_AGENT_HEADER"); ok {
disableUserAgentHeader = val
}
}
2020-10-02 02:10:59 +00:00
var logger = log.WithField("exchange", "max")
var htmlTagPattern = regexp.MustCompile("<[/]?[a-zA-Z-]+.*?>")
// The following variables are used for nonce.
// globalTimeOffset is used for nonce
var globalTimeOffset int64 = 0
2020-10-02 02:10:59 +00:00
// globalServerTimestamp is used for storing the server timestamp, default to Now
var globalServerTimestamp = time.Now().Unix()
2020-10-02 02:10:59 +00:00
// reqCount is used for nonce, this variable counts the API request count.
var reqCount uint64 = 1
var nonceOnce sync.Once
2020-10-02 02:10:59 +00:00
// create an isolated http httpTransport rather than the default one
var httpTransport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
// ForceAttemptHTTP2: true,
// DisableCompression: false,
MaxIdleConns: httpTransportMaxIdleConns,
MaxIdleConnsPerHost: httpTransportMaxIdleConnsPerHost,
IdleConnTimeout: httpTransportIdleConnTimeout,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
var defaultHttpClient = &http.Client{
Timeout: defaultHTTPTimeout,
Transport: httpTransport,
}
2020-10-02 02:10:59 +00:00
type RestClient struct {
2022-05-24 09:40:00 +00:00
requestgen.BaseAPIClient
2020-10-02 02:10:59 +00:00
APIKey, APISecret string
2020-10-02 02:10:59 +00:00
AccountService *AccountService
PublicService *PublicService
TradeService *TradeService
OrderService *OrderService
RewardService *RewardService
2021-05-11 14:35:31 +00:00
WithdrawalService *WithdrawalService
2020-10-02 02:10:59 +00:00
}
2022-05-26 10:49:50 +00:00
func NewRestClient(baseURL string) *RestClient {
2020-10-02 02:10:59 +00:00
u, err := url.Parse(baseURL)
if err != nil {
panic(err)
}
var client = &RestClient{
2022-05-24 09:40:00 +00:00
BaseAPIClient: requestgen.BaseAPIClient{
HttpClient: defaultHttpClient,
2022-05-24 09:40:00 +00:00
BaseURL: u,
},
2020-10-02 02:10:59 +00:00
}
client.AccountService = &AccountService{client}
client.TradeService = &TradeService{client}
client.PublicService = &PublicService{client}
client.OrderService = &OrderService{client}
2021-02-22 10:45:44 +00:00
client.RewardService = &RewardService{client}
2021-05-11 14:35:31 +00:00
client.WithdrawalService = &WithdrawalService{client}
2021-02-22 10:45:44 +00:00
// defaultHttpClient.MaxTokenService = &MaxTokenService{defaultHttpClient}
2020-10-02 02:10:59 +00:00
client.initNonce()
return client
}
// Auth sets api key and secret for usage is requests that requires authentication.
func (c *RestClient) Auth(key string, secret string) *RestClient {
2022-06-17 07:04:23 +00:00
// pragma: allowlist nextline secret
2020-10-02 02:10:59 +00:00
c.APIKey = key
2022-06-17 07:04:23 +00:00
// pragma: allowlist nextline secret
2020-10-02 02:10:59 +00:00
c.APISecret = secret
return c
}
func (c *RestClient) queryAndUpdateServerTimestamp(ctx context.Context) {
2023-04-13 09:29:23 +00:00
op := func() error {
serverTs, err := c.PublicService.Timestamp(ctx)
if err != nil {
return err
}
if serverTs == 0 {
return errors.New("unexpected zero server timestamp")
}
clientTime := time.Now()
offset := serverTs - clientTime.Unix()
if offset < 0 {
// avoid updating a negative offset: server time is before the local time
if offset > maxAllowedNegativeTimeOffset {
return nil
}
2023-04-13 09:29:23 +00:00
// if the offset is greater than 15 seconds, we should restart
logger.Panicf("max exchange server timestamp offset %d is less than the negative offset %d", offset, maxAllowedNegativeTimeOffset)
}
2023-04-13 09:29:23 +00:00
atomic.StoreInt64(&globalServerTimestamp, serverTs)
atomic.StoreInt64(&globalTimeOffset, offset)
logger.Debugf("loaded max server timestamp: %d offset=%d", globalServerTimestamp, offset)
return nil
}
if err := backoff.RetryGeneral(ctx, op); err != nil {
logger.WithError(err).Error("unable to sync timestamp with max")
2020-10-02 02:10:59 +00:00
}
}
2020-10-02 02:10:59 +00:00
func (c *RestClient) initNonce() {
nonceOnce.Do(func() {
go c.queryAndUpdateServerTimestamp(context.Background())
})
2020-10-02 02:10:59 +00:00
}
func (c *RestClient) getNonce() int64 {
// nonce 是以正整數表示的時間戳記,代表了從 Unix epoch 到當前時間所經過的毫秒數(ms)。
// nonce 與伺服器的時間差不得超過正負30秒每個 nonce 只能使用一次。
2020-10-02 02:10:59 +00:00
var seconds = time.Now().Unix()
var rc = atomic.AddUint64(&reqCount, 1)
var offset = atomic.LoadInt64(&globalTimeOffset)
return (seconds+offset)*1000 - 1 + int64(math.Mod(float64(rc), 1000.0))
2020-10-02 02:10:59 +00:00
}
2022-04-19 05:52:10 +00:00
func (c *RestClient) NewAuthenticatedRequest(ctx context.Context, m string, refURL string, params url.Values, payload interface{}) (*http.Request, error) {
return c.newAuthenticatedRequest(ctx, m, refURL, params, payload, nil)
}
// newAuthenticatedRequest creates new http request for authenticated routes.
func (c *RestClient) newAuthenticatedRequest(ctx context.Context, m string, refURL string, params url.Values, data interface{}, rel *url.URL) (*http.Request, error) {
2022-05-24 09:40:00 +00:00
if len(c.APIKey) == 0 {
return nil, errors.New("empty api key")
}
if len(c.APISecret) == 0 {
return nil, errors.New("empty api secret")
}
var err error
if rel == nil {
rel, err = url.Parse(refURL)
if err != nil {
return nil, err
}
2020-10-02 02:10:59 +00:00
}
2020-10-06 10:44:56 +00:00
var p []byte
2022-05-24 09:40:00 +00:00
var payload = map[string]interface{}{
"nonce": c.getNonce(),
"path": c.BaseURL.ResolveReference(rel).Path,
}
2020-10-06 10:44:56 +00:00
switch d := data.(type) {
case map[string]interface{}:
for k, v := range d {
payload[k] = v
}
}
2020-10-06 10:44:56 +00:00
for k, vs := range params {
k = strings.TrimSuffix(k, "[]")
if len(vs) == 1 {
payload[k] = vs[0]
} else {
payload[k] = vs
}
}
p, err = castPayload(payload)
if err != nil {
return nil, err
2020-10-02 02:10:59 +00:00
}
2022-04-19 05:52:10 +00:00
req, err := c.NewRequest(ctx, m, refURL, params, p)
2020-10-02 02:10:59 +00:00
if err != nil {
return nil, err
}
encoded := base64.StdEncoding.EncodeToString(p)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("X-MAX-ACCESSKEY", c.APIKey)
req.Header.Add("X-MAX-PAYLOAD", encoded)
req.Header.Add("X-MAX-SIGNATURE", signPayload(encoded, c.APISecret))
if disableUserAgentHeader {
req.Header.Set("USER-AGENT", "")
} else {
req.Header.Set("USER-AGENT", UserAgent)
}
2020-10-02 02:10:59 +00:00
2023-07-26 06:34:50 +00:00
req.Header.Set("USER-AGENT", "Go-http-client/1.1,bbgo/"+version.Version)
if false {
out, _ := httputil.DumpRequestOut(req, true)
fmt.Println(string(out))
2020-10-02 02:10:59 +00:00
}
return req, nil
2020-10-02 02:10:59 +00:00
}
// ErrorResponse is the custom error type that is returned if the API returns an
// error.
type ErrorField struct {
Code int `json:"code"`
Message string `json:"message"`
}
type ErrorResponse struct {
2022-04-19 04:10:15 +00:00
*requestgen.Response
2020-10-02 02:10:59 +00:00
Err ErrorField `json:"error"`
}
func (r *ErrorResponse) Error() string {
return fmt.Sprintf("%s %s: %d %d %s",
r.Response.Response.Request.Method,
r.Response.Response.Request.URL.String(),
r.Response.Response.StatusCode,
r.Err.Code,
r.Err.Message,
)
}
2021-05-23 04:11:27 +00:00
// ToErrorResponse tries to convert/parse the server response to the standard Error interface object
2022-04-19 04:10:15 +00:00
func ToErrorResponse(response *requestgen.Response) (errorResponse *ErrorResponse, err error) {
2020-10-02 02:10:59 +00:00
errorResponse = &ErrorResponse{Response: response}
contentType := response.Header.Get("content-type")
switch contentType {
case "text/json", "application/json", "application/json; charset=utf-8":
var err = response.DecodeJSON(errorResponse)
if err != nil {
return errorResponse, errors.Wrapf(err, "failed to decode json for response: %d %s", response.StatusCode, string(response.Body))
}
return errorResponse, nil
case "text/html":
// convert 5xx error from the HTML page to the ErrorResponse
errorResponse.Err.Message = htmlTagPattern.ReplaceAllLiteralString(string(response.Body), "")
return errorResponse, nil
case "text/plain":
errorResponse.Err.Message = string(response.Body)
return errorResponse, nil
2020-10-02 02:10:59 +00:00
}
return errorResponse, fmt.Errorf("unexpected response content type %s", contentType)
}
2022-04-19 04:10:15 +00:00
func castPayload(payload interface{}) ([]byte, error) {
if payload == nil {
return nil, nil
}
2022-04-19 04:10:15 +00:00
switch v := payload.(type) {
case string:
return []byte(v), nil
2022-04-19 04:10:15 +00:00
case []byte:
return v, nil
2022-04-19 04:10:15 +00:00
}
body, err := json.Marshal(payload)
return body, err
2022-04-19 04:10:15 +00:00
}
2022-05-24 09:40:00 +00:00
func signPayload(payload string, secret string) string {
var sig = hmac.New(sha256.New, []byte(secret))
_, err := sig.Write([]byte(payload))
if err != nil {
return ""
}
return hex.EncodeToString(sig.Sum(nil))
}