pkg/exchange: support broker id

This commit is contained in:
edwin 2024-10-21 15:09:24 +08:00
parent 5a1249e871
commit b1f86adab5
3 changed files with 40 additions and 8 deletions

View File

@ -65,26 +65,40 @@ var log = logrus.WithFields(logrus.Fields{
var ErrSymbolRequired = errors.New("symbol is a required parameter")
type Exchange struct {
key, secret, passphrase string
key, secret, passphrase, brokerId string
client *okexapi.RestClient
timeNowFunc func() time.Time
}
func New(key, secret, passphrase string) *Exchange {
type Option func(exchange *Exchange)
func WithBrokerId(id string) Option {
return func(exchange *Exchange) {
exchange.brokerId = id
}
}
func New(key, secret, passphrase string, opts ...Option) *Exchange {
client := okexapi.NewClient()
if len(key) > 0 && len(secret) > 0 {
client.Auth(key, secret, passphrase)
}
return &Exchange{
ex := &Exchange{
key: key,
secret: secret,
passphrase: passphrase,
client: client,
timeNowFunc: time.Now,
}
for _, o := range opts {
o(ex)
}
return ex
}
func (e *Exchange) Name() types.ExchangeName {
@ -263,6 +277,10 @@ func (e *Exchange) SubmitOrder(ctx context.Context, order types.SubmitOrder) (*t
orderReq.ClientOrderID(order.ClientOrderID)
}
if len(e.brokerId) != 0 {
orderReq.Tag(e.brokerId)
}
timeNow := time.Now()
orders, err := orderReq.Do(ctx)
if err != nil {

View File

@ -42,7 +42,7 @@ type PlaceOrderRequest struct {
// A combination of case-sensitive alphanumerics, all numbers, or all letters of up to 32 characters.
clientOrderID *string `param:"clOrdId"`
// A combination of case-sensitive alphanumerics, all numbers, or all letters of up to 8 characters.
// A combination of case-sensitive alphanumerics, all numbers, or all letters of up to 16 characters.
tag *string `param:"tag"`
// "buy" or "sell"

View File

@ -284,15 +284,29 @@ func (r *PlaceOrderRequest) Do(ctx context.Context) ([]OrderResponse, error) {
}
var apiResponse APIResponse
type responseUnmarshaler interface {
Unmarshal(data []byte) error
}
if unmarshaler, ok := interface{}(&apiResponse).(responseUnmarshaler); ok {
if err := unmarshaler.Unmarshal(response.Body); err != nil {
return nil, err
}
} else {
// The line below checks the content type, however, some API server might not send the correct content type header,
// Hence, this is commented for backward compatibility
// response.IsJSON()
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
}
type responseValidator interface {
Validate() error
}
validator, ok := interface{}(apiResponse).(responseValidator)
if ok {
if validator, ok := interface{}(&apiResponse).(responseValidator); ok {
if err := validator.Validate(); err != nil {
return nil, err
}