diff --git a/pkg/core/converter.go b/pkg/core/converter.go index a154e2758..baa5ecf7f 100644 --- a/pkg/core/converter.go +++ b/pkg/core/converter.go @@ -9,6 +9,9 @@ import ( type Converter interface { OrderConverter TradeConverter + KLineConverter + MarketConverter + BalanceConverter Initialize() error } @@ -22,12 +25,33 @@ type TradeConverter interface { 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 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 { - orderConverter OrderConvertFunc - tradeConverter TradeConvertFunc + orderConverter OrderConvertFunc + tradeConverter TradeConvertFunc + klineConverter KLineConvertFunc + marketConverter MarketConvertFunc + balanceConverter BalanceConvertFunc } 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) } +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 type SymbolConverter struct { FromSymbol string `json:"from"` @@ -73,6 +109,10 @@ func (c *SymbolConverter) ConvertOrder(order types.Order) (types.Order, error) { order.Symbol = c.ToSymbol } + if order.SubmitOrder.Market.Symbol == c.FromSymbol { + order.SubmitOrder.Market.Symbol = c.ToSymbol + } + return order, nil } @@ -83,3 +123,22 @@ func (c *SymbolConverter) ConvertTrade(trade types.Trade) (types.Trade, error) { 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 +} diff --git a/pkg/core/converter_test.go b/pkg/core/converter_test.go index 4718eca2b..7a922fd39 100644 --- a/pkg/core/converter_test.go +++ b/pkg/core/converter_test.go @@ -21,11 +21,40 @@ func TestSymbolConverter(t *testing.T) { order, err := converter.ConvertOrder(types.Order{ SubmitOrder: types.SubmitOrder{ Symbol: "MAXEXCHANGEUSDT", + Market: types.Market{ + Symbol: "MAXEXCHANGEUSDT", + }, }, }) if assert.NoError(t, err) { 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) } } diff --git a/pkg/core/tradecollector.go b/pkg/core/tradecollector.go index ed581704f..d70ce922a 100644 --- a/pkg/core/tradecollector.go +++ b/pkg/core/tradecollector.go @@ -100,6 +100,60 @@ func (c *ConverterManager) ConvertTrade(trade types.Trade) types.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 type TradeCollector struct { Symbol string