Merge pull request #1535 from c9s/edwin/okx/allow-char-in-place-order

FIX: [okx] allow char in place order
This commit is contained in:
bailantaotao 2024-02-20 15:25:34 +08:00 committed by GitHub
commit 7436a7e2e1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 42 additions and 7 deletions

View File

@ -3,6 +3,7 @@ package okex
import ( import (
"context" "context"
"fmt" "fmt"
"regexp"
"strconv" "strconv"
"time" "time"
@ -33,6 +34,9 @@ var (
queryClosedOrderRateLimiter = rate.NewLimiter(rate.Every(100*time.Millisecond), 10) queryClosedOrderRateLimiter = rate.NewLimiter(rate.Every(100*time.Millisecond), 10)
queryTradeLimiter = rate.NewLimiter(rate.Every(100*time.Millisecond), 10) queryTradeLimiter = rate.NewLimiter(rate.Every(100*time.Millisecond), 10)
queryKLineLimiter = rate.NewLimiter(rate.Every(100*time.Millisecond), 20) queryKLineLimiter = rate.NewLimiter(rate.Every(100*time.Millisecond), 20)
// clientOrderIdRegex combine of case-sensitive alphanumerics, all numbers, or all letters of up to 32 characters.
clientOrderIdRegex = regexp.MustCompile("^[a-zA-Z0-9]{0,32}$")
) )
const ( const (
@ -241,11 +245,12 @@ func (e *Exchange) SubmitOrder(ctx context.Context, order types.SubmitOrder) (*t
return nil, fmt.Errorf("place order rate limiter wait error: %w", err) return nil, fmt.Errorf("place order rate limiter wait error: %w", err)
} }
_, err = strconv.ParseInt(order.ClientOrderID, 10, 64) if len(order.ClientOrderID) > 0 {
if err != nil { if ok := clientOrderIdRegex.MatchString(order.ClientOrderID); !ok {
return nil, fmt.Errorf("client order id should be numberic: %s, err: %w", order.ClientOrderID, err) return nil, fmt.Errorf("client order id should be case-sensitive alphanumerics, all numbers, or all letters of up to 32 characters: %s", order.ClientOrderID)
} }
orderReq.ClientOrderID(order.ClientOrderID) orderReq.ClientOrderID(order.ClientOrderID)
}
orders, err := orderReq.Do(ctx) orders, err := orderReq.Do(ctx)
if err != nil { if err != nil {
@ -357,9 +362,8 @@ func (e *Exchange) CancelOrders(ctx context.Context, orders ...types.Order) erro
req.InstrumentID(toLocalSymbol(order.Symbol)) req.InstrumentID(toLocalSymbol(order.Symbol))
req.OrderID(strconv.FormatUint(order.OrderID, 10)) req.OrderID(strconv.FormatUint(order.OrderID, 10))
if len(order.ClientOrderID) > 0 { if len(order.ClientOrderID) > 0 {
_, err := strconv.ParseInt(order.ClientOrderID, 10, 64) if ok := clientOrderIdRegex.MatchString(order.ClientOrderID); !ok {
if err != nil { return fmt.Errorf("client order id should be case-sensitive alphanumerics, all numbers, or all letters of up to 32 characters: %s", order.ClientOrderID)
return fmt.Errorf("client order id should be numberic: %s, err: %w", order.ClientOrderID, err)
} }
req.ClientOrderID(order.ClientOrderID) req.ClientOrderID(order.ClientOrderID)
} }

View File

@ -0,0 +1,31 @@
package okex
import (
"strings"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)
func Test_clientOrderIdRegex(t *testing.T) {
t.Run("empty client order id", func(t *testing.T) {
assert.True(t, clientOrderIdRegex.MatchString(""))
})
t.Run("mixed of digit and char", func(t *testing.T) {
assert.True(t, clientOrderIdRegex.MatchString("1s2f3g4h5j"))
})
t.Run("mixed of 16 chars and 16 digit", func(t *testing.T) {
assert.True(t, clientOrderIdRegex.MatchString(strings.Repeat("s", 16)+strings.Repeat("1", 16)))
})
t.Run("out of maximum length", func(t *testing.T) {
assert.False(t, clientOrderIdRegex.MatchString(strings.Repeat("s", 33)))
})
t.Run("invalid char: `-`", func(t *testing.T) {
assert.False(t, clientOrderIdRegex.MatchString(uuid.NewString()))
})
}