Merge pull request #246 from jnlin/fix/ftx-symbol

fix(ftxExchange): setup a symbol mapping table
This commit is contained in:
Yo-An Lin 2021-05-17 21:37:46 +08:00 committed by GitHub
commit d1bfeccc72
5 changed files with 20 additions and 12 deletions

View File

@ -20,6 +20,10 @@ func toGlobalSymbol(original string) string {
return strings.ReplaceAll(TrimUpperString(original), "/", "")
}
func toLocalSymbol(original string) string {
return symbolMap[original]
}
func TrimUpperString(original string) string {
return strings.ToUpper(strings.TrimSpace(original))
}

View File

@ -21,6 +21,7 @@ const (
)
var logger = logrus.WithField("exchange", "ftx")
var symbolMap map[string]string
type Exchange struct {
key, secret string
@ -33,6 +34,7 @@ func NewExchange(key, secret string, subAccount string) *Exchange {
if err != nil {
panic(err)
}
symbolMap = make(map[string]string)
return &Exchange{
restEndpoint: u,
key: key,
@ -73,6 +75,7 @@ func (e *Exchange) QueryMarkets(ctx context.Context) (types.MarketMap, error) {
markets := types.MarketMap{}
for _, m := range resp.Result {
symbol := toGlobalSymbol(m.Name)
symbolMap[symbol] = m.Name
market := types.Market{
Symbol: symbol,
@ -157,7 +160,7 @@ func (e *Exchange) QueryKLines(ctx context.Context, symbol string, interval type
if !isIntervalSupportedInKLine(interval) {
return nil, fmt.Errorf("interval %s is not supported", interval.String())
}
resp, err := e.newRest().HistoricalPrices(ctx, symbol, interval, int64(options.Limit), since, until)
resp, err := e.newRest().HistoricalPrices(ctx, toLocalSymbol(symbol), interval, int64(options.Limit), since, until)
if err != nil {
return nil, err
}
@ -224,7 +227,7 @@ func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *type
for since.Before(until) {
// DO not set limit to `1` since you will always get the same response.
resp, err := e.newRest().Fills(ctx, symbol, since, until, limit, true)
resp, err := e.newRest().Fills(ctx, toLocalSymbol(symbol), since, until, limit, true)
if err != nil {
return nil, err
}
@ -298,11 +301,11 @@ func (e *Exchange) SubmitOrders(ctx context.Context, orders ...types.SubmitOrder
// TODO: currently only support limit and market order
// TODO: support time in force
for _, so := range orders {
if so.TimeInForce != "GTC" {
if so.TimeInForce != "GTC" && so.TimeInForce != "" {
return createdOrders, fmt.Errorf("unsupported TimeInForce %s. only support GTC", so.TimeInForce)
}
or, err := e.newRest().PlaceOrder(ctx, PlaceOrderPayload{
Market: TrimUpperString(so.Symbol),
Market: toLocalSymbol(TrimUpperString(so.Symbol)),
Side: TrimLowerString(string(so.Side)),
Price: so.Price,
Type: TrimLowerString(string(so.Type)),
@ -329,7 +332,7 @@ func (e *Exchange) SubmitOrders(ctx context.Context, orders ...types.SubmitOrder
func (e *Exchange) QueryOpenOrders(ctx context.Context, symbol string) (orders []types.Order, err error) {
// TODO: invoke open trigger orders
resp, err := e.newRest().OpenOrders(ctx, symbol)
resp, err := e.newRest().OpenOrders(ctx, toLocalSymbol(symbol))
if err != nil {
return nil, err
}
@ -362,7 +365,7 @@ func (e *Exchange) QueryClosedOrders(ctx context.Context, symbol string, since,
s := since
var lastOrder order
for hasMoreData {
resp, err := e.newRest().OrdersHistory(ctx, symbol, s, until, limit)
resp, err := e.newRest().OrdersHistory(ctx, toLocalSymbol(symbol), s, until, limit)
if err != nil {
return nil, err
}

View File

@ -109,7 +109,7 @@ func (s *Stream) Subscribe(channel types.Channel, symbol string, _ types.Subscri
s.addSubscription(websocketRequest{
Operation: subscribe,
Channel: orderBookChannel,
Market: TrimUpperString(symbol),
Market: toLocalSymbol(TrimUpperString(symbol)),
})
}

View File

@ -55,9 +55,10 @@ type websocketRequest struct {
}
*/
type loginArgs struct {
Key string `json:"key"`
Signature string `json:"sign"`
Time int64 `json:"time"`
Key string `json:"key"`
Signature string `json:"sign"`
Time int64 `json:"time"`
SubAccount string `json:"subaccount"`
}
func newLoginRequest(key, secret string, t time.Time) websocketRequest {
@ -361,7 +362,7 @@ func toGlobalOrderBook(r orderBookResponse) (types.OrderBook, error) {
}
return types.OrderBook{
// ex. BTC/USDT
Symbol: strings.ToUpper(r.Market),
Symbol: toGlobalSymbol(strings.ToUpper(r.Market)),
Bids: bids,
Asks: asks,
}, nil

View File

@ -58,7 +58,7 @@ func Test_orderBookResponse_toGlobalOrderBook(t *testing.T) {
b, err := toGlobalOrderBook(r)
assert.NoError(t, err)
assert.Equal(t, "BTC/USDT", b.Symbol)
assert.Equal(t, "BTCUSDT", b.Symbol)
isValid, err := b.IsValid()
assert.True(t, isValid)
assert.NoError(t, err)