adshao / go-binance

A Go SDK for Binance API
MIT License
1.48k stars 663 forks source link

The futures module adds the continuous contract Kline rest interface and websocket interface #467

Closed xyq-c-cpp closed 1 year ago

xyq-c-cpp commented 1 year ago

Hello, I used this module and found that the relevant interfaces of the future part of the continuous contract kline were not implemented. So I implemented a copy and hope that the author can incorporate it.

rest api: image

websocket: image

adshao commented 1 year ago

chatgpt: check if these code is good:

// WsContinuousKlineEvent define websocket continuous kline event
type WsContinuousKlineEvent struct {
    Event        string            `json:"e"`
    Time         int64             `json:"E"`
    PairSymbol   string            `json:"ps"`
    ContractType string            `json:"ct"`
    Kline        WsContinuousKline `json:"k"`
}

// WsContinuousKline define websocket continuous kline
type WsContinuousKline struct {
    StartTime            int64  `json:"t"`
    EndTime              int64  `json:"T"`
    Interval             string `json:"i"`
    FirstTradeID         int64  `json:"f"`
    LastTradeID          int64  `json:"L"`
    Open                 string `json:"o"`
    Close                string `json:"c"`
    High                 string `json:"h"`
    Low                  string `json:"l"`
    Volume               string `json:"v"`
    TradeNum             int64  `json:"n"`
    IsFinal              bool   `json:"x"`
    QuoteVolume          string `json:"q"`
    ActiveBuyVolume      string `json:"V"`
    ActiveBuyQuoteVolume string `json:"Q"`
}

type WsPairContractTypeIntervalPair struct {
    Pair         string
    ContractType string
    Interval     string
}

// WsContinuousKlineHandler handle websocket continuous kline event
type WsContinuousKlineHandler func(event *WsContinuousKlineEvent)

// WsContinuousKlineServe serve websocket continuous kline handler with a pair and contractType and interval like 15m, 30s
func WsContinuousKlineServe(pair string, contractType string, interval string, handler WsContinuousKlineHandler,
    errHandler ErrHandler) (doneC, stopC chan struct{}, err error) {
    endpoint := fmt.Sprintf("%s/%s_%s@continuousKline_%s", getWsEndpoint(), strings.ToLower(pair),
        strings.ToLower(contractType), interval)
    cfg := newWsConfig(endpoint)
    wsHandler := func(message []byte) {
        event := new(WsContinuousKlineEvent)
        err := json.Unmarshal(message, event)
        if err != nil {
            errHandler(err)
            return
        }
        handler(event)
    }
    return wsServe(cfg, wsHandler, errHandler)
}

// WsCombinedContinuousKlineServe is similar to WsKlineServe, but it handles multiple pairs of different contractType with it interval
func WsCombinedContinuousKlineServe(PairContractTypeIntervalList []WsPairContractTypeIntervalPair,
    handler WsContinuousKlineHandler, errHandler ErrHandler) (doneC, stopC chan struct{}, err error) {
    endpoint := getCombinedEndpoint()
    for _, val := range PairContractTypeIntervalList {
        endpoint += fmt.Sprintf("%s_%s@continuousKline_%s", strings.ToLower(val.Pair),
            strings.ToLower(val.ContractType), val.Interval) + "/"
    }
    endpoint = endpoint[:len(endpoint)-1]
    cfg := newWsConfig(endpoint)
    wsHandler := func(message []byte) {
        j, err := newJSON(message)
        if err != nil {
            errHandler(err)
            return
        }

        data := j.Get("data").MustMap()

        jsonData, _ := json.Marshal(data)

        event := new(WsContinuousKlineEvent)
        err = json.Unmarshal(jsonData, event)
        if err != nil {
            errHandler(err)
            return
        }

        handler(event)
    }
    return wsServe(cfg, wsHandler, errHandler)
}
adshao commented 1 year ago

Code Review: Based on the code alone, it is difficult to determine if the code is "good" or not. However, it appears that the code defines structs and functions for handling continuous kline websocket events, with error handling provided. The code also includes comments explaining the purpose of each struct and function.

In order to determine if the code is good or not, it would be necessary to examine how it is used within a larger codebase, and whether it effectively accomplishes the desired functionality with good performance and maintainability.

xyq-c-cpp commented 1 year ago

@adshao I checked that the naming is not reasonable. I will modify it later. "In order to determine if the code is good or not, it would be necessary to examine how it is used within a larger codebase, and whether it effectively accomplishes the desired functionality with good performance and maintainability." => Do you need some demo examples?

adshao commented 1 year ago

@adshao I checked that the naming is not reasonable. I will modify it later. "In order to determine if the code is good or not, it would be necessary to examine how it is used within a larger codebase, and whether it effectively accomplishes the desired functionality with good performance and maintainability." => Do you need some demo examples?

Sorry, the comment above was created by chatgpt action which is my another project 😄 chatgpt-code-review-action I will manually review the code later, thanks.