pkg/core: support more function to symbol converter

This commit is contained in:
edwin 2024-08-23 16:10:44 +08:00
parent 80ad5c32b5
commit 26149103f0
3 changed files with 144 additions and 2 deletions

View File

@ -9,6 +9,9 @@ import (
type Converter interface { type Converter interface {
OrderConverter OrderConverter
TradeConverter TradeConverter
KLineConverter
MarketConverter
BalanceConverter
Initialize() error Initialize() error
} }
@ -22,12 +25,33 @@ type TradeConverter interface {
ConvertTrade(trade types.Trade) (types.Trade, error) ConvertTrade(trade types.Trade) (types.Trade, error)
} }
// KLineConverter converts the kline to another kline
type KLineConverter interface {
ConvertKLine(kline types.KLine) (types.KLine, error)
}
// MarketConverter converts the market to another market
type MarketConverter interface {
ConvertMarket(market types.Market) (types.Market, error)
}
// BalanceConverter converts the balance to another balance
type BalanceConverter interface {
ConvertBalance(balance types.Balance) (types.Balance, error)
}
type OrderConvertFunc func(order types.Order) (types.Order, error) type OrderConvertFunc func(order types.Order) (types.Order, error)
type TradeConvertFunc func(trade types.Trade) (types.Trade, error) type TradeConvertFunc func(trade types.Trade) (types.Trade, error)
type KLineConvertFunc func(kline types.KLine) (types.KLine, error)
type MarketConvertFunc func(market types.Market) (types.Market, error)
type BalanceConvertFunc func(balance types.Balance) (types.Balance, error)
type DynamicConverter struct { type DynamicConverter struct {
orderConverter OrderConvertFunc orderConverter OrderConvertFunc
tradeConverter TradeConvertFunc tradeConverter TradeConvertFunc
klineConverter KLineConvertFunc
marketConverter MarketConvertFunc
balanceConverter BalanceConvertFunc
} }
func NewDynamicConverter(orderConverter OrderConvertFunc, tradeConverter TradeConvertFunc) *DynamicConverter { func NewDynamicConverter(orderConverter OrderConvertFunc, tradeConverter TradeConvertFunc) *DynamicConverter {
@ -46,6 +70,18 @@ func (c *DynamicConverter) ConvertTrade(trade types.Trade) (types.Trade, error)
return c.tradeConverter(trade) return c.tradeConverter(trade)
} }
func (c *DynamicConverter) ConvertKLine(kline types.KLine) (types.KLine, error) {
return c.klineConverter(kline)
}
func (c *DynamicConverter) ConvertMarket(market types.Market) (types.Market, error) {
return c.marketConverter(market)
}
func (c *DynamicConverter) ConvertBalance(balance types.Balance) (types.Balance, error) {
return c.balanceConverter(balance)
}
// SymbolConverter converts the symbol to another symbol // SymbolConverter converts the symbol to another symbol
type SymbolConverter struct { type SymbolConverter struct {
FromSymbol string `json:"from"` FromSymbol string `json:"from"`
@ -73,6 +109,10 @@ func (c *SymbolConverter) ConvertOrder(order types.Order) (types.Order, error) {
order.Symbol = c.ToSymbol order.Symbol = c.ToSymbol
} }
if order.SubmitOrder.Market.Symbol == c.FromSymbol {
order.SubmitOrder.Market.Symbol = c.ToSymbol
}
return order, nil return order, nil
} }
@ -83,3 +123,22 @@ func (c *SymbolConverter) ConvertTrade(trade types.Trade) (types.Trade, error) {
return trade, nil return trade, nil
} }
func (c *SymbolConverter) ConvertKLine(kline types.KLine) (types.KLine, error) {
if kline.Symbol == c.FromSymbol {
kline.Symbol = c.ToSymbol
}
return kline, nil
}
func (s *SymbolConverter) ConvertMarket(mkt types.Market) (types.Market, error) {
if mkt.Symbol == s.FromSymbol {
mkt.Symbol = s.ToSymbol
}
return mkt, nil
}
func (c *SymbolConverter) ConvertBalance(balance types.Balance) (types.Balance, error) {
return balance, nil
}

View File

@ -21,11 +21,40 @@ func TestSymbolConverter(t *testing.T) {
order, err := converter.ConvertOrder(types.Order{ order, err := converter.ConvertOrder(types.Order{
SubmitOrder: types.SubmitOrder{ SubmitOrder: types.SubmitOrder{
Symbol: "MAXEXCHANGEUSDT", Symbol: "MAXEXCHANGEUSDT",
Market: types.Market{
Symbol: "MAXEXCHANGEUSDT",
},
}, },
}) })
if assert.NoError(t, err) { if assert.NoError(t, err) {
assert.Equal(t, "MAXUSDT", order.Symbol) assert.Equal(t, "MAXUSDT", order.Symbol)
assert.Equal(t, "MAXUSDT", order.SubmitOrder.Symbol)
assert.Equal(t, "MAXUSDT", order.SubmitOrder.Market.Symbol)
}
kline, err := converter.ConvertKLine(types.KLine{
Symbol: "MAXEXCHANGEUSDT",
})
if assert.NoError(t, err) {
assert.Equal(t, "MAXUSDT", kline.Symbol)
}
market, err := converter.ConvertMarket(types.Market{
Symbol: "MAXEXCHANGEUSDT",
})
if assert.NoError(t, err) {
assert.Equal(t, "MAXUSDT", market.Symbol)
}
balance, err := converter.ConvertBalance(types.Balance{
Currency: "MAXEXCHANGE",
})
if assert.NoError(t, err) {
assert.Equal(t, "MAXEXCHANGE", balance.Currency)
} }
} }

View File

@ -100,6 +100,60 @@ func (c *ConverterManager) ConvertTrade(trade types.Trade) types.Trade {
return trade return trade
} }
func (c *ConverterManager) ConvertKLine(kline types.KLine) types.KLine {
if len(c.converters) == 0 {
return kline
}
for _, converter := range c.converters {
convKline, err := converter.ConvertKLine(kline)
if err != nil {
logrus.WithError(err).Errorf("converter %+v error, kline: %s", converter, kline.String())
continue
}
kline = convKline
}
return kline
}
func (c *ConverterManager) ConvertMarket(market types.Market) types.Market {
if len(c.converters) == 0 {
return market
}
for _, converter := range c.converters {
convMarket, err := converter.ConvertMarket(market)
if err != nil {
logrus.WithError(err).Errorf("converter %+v error, market: %+v", converter, market)
continue
}
market = convMarket
}
return market
}
func (c *ConverterManager) ConvertBalance(balance types.Balance) types.Balance {
if len(c.converters) == 0 {
return balance
}
for _, converter := range c.converters {
convBal, err := converter.ConvertBalance(balance)
if err != nil {
logrus.WithError(err).Errorf("converter %+v error, balance: %s", converter, balance.String())
continue
}
balance = convBal
}
return balance
}
//go:generate callbackgen -type TradeCollector //go:generate callbackgen -type TradeCollector
type TradeCollector struct { type TradeCollector struct {
Symbol string Symbol string