diff --git a/api/exchange.go b/api/exchange.go index cc046c4d1..64e149387 100644 --- a/api/exchange.go +++ b/api/exchange.go @@ -75,13 +75,18 @@ type Constrainable interface { GetOrderConstraints(pair *model.TradingPair) *model.OrderConstraints } +// OrderbookFetcher extracts out the method that should go into ExchangeShim for now +type OrderbookFetcher interface { + GetOrderBook(pair *model.TradingPair, maxCount int32) (*model.OrderBook, error) +} + // TradeAPI is the interface we use as a generic API for trading on any crypto exchange type TradeAPI interface { GetAssetConverter() *model.AssetConverter Constrainable - GetOrderBook(pair *model.TradingPair, maxCount int32) (*model.OrderBook, error) + OrderbookFetcher GetTrades(pair *model.TradingPair, maybeCursor interface{}) (*TradesResult, error) @@ -207,4 +212,5 @@ type ExchangeShim interface { GetBalanceHack(asset horizon.Asset) (*Balance, error) LoadOffersHack() ([]horizon.Offer, error) Constrainable + OrderbookFetcher } diff --git a/cmd/trade.go b/cmd/trade.go index e7041b563..aecba28dc 100644 --- a/cmd/trade.go +++ b/cmd/trade.go @@ -242,12 +242,6 @@ func init() { // we want to delete all the offers and exit here since there is something wrong with our setup deleteAllOffersAndExit(l, botConfig, client, sdex, exchangeShim) } - if !isTradingSdex && submitMode != api.SubmitModeBoth { - log.Println() - log.Println("cannot run on a non-SDEX exchange with SUBMIT_MODE set to something other than \"both\"") - // we want to delete all the offers and exit here since there is something wrong with our setup - deleteAllOffersAndExit(l, botConfig, client, sdex, exchangeShim) - } if !isTradingSdex && botConfig.FillTrackerSleepMillis != 0 { log.Println() log.Println("cannot run on a non-SDEX exchange with FILL_TRACKER_SLEEP_MILLIS set to something other than 0") diff --git a/plugins/batchedExchange.go b/plugins/batchedExchange.go index 4ff97858f..411522c80 100644 --- a/plugins/batchedExchange.go +++ b/plugins/batchedExchange.go @@ -154,6 +154,11 @@ func (b BatchedExchange) GetOrderConstraints(pair *model.TradingPair) *model.Ord return b.inner.GetOrderConstraints(pair) } +// GetOrderBook impl +func (b BatchedExchange) GetOrderBook(pair *model.TradingPair, maxCount int32) (*model.OrderBook, error) { + return b.inner.GetOrderBook(pair, maxCount) +} + // SubmitOps performs any finalization or submission step needed by the exchange func (b BatchedExchange) SubmitOps(ops []build.TransactionMutator, asyncCallback func(hash string, e error)) error { var e error diff --git a/plugins/sdexMakerFilter.go b/plugins/makerModeFilter.go similarity index 80% rename from plugins/sdexMakerFilter.go rename to plugins/makerModeFilter.go index 7e1f7bb61..524d4036c 100644 --- a/plugins/sdexMakerFilter.go +++ b/plugins/makerModeFilter.go @@ -12,28 +12,30 @@ import ( "github.com/stellar/kelp/support/utils" ) -// MakeSdexMakerModeFilter makes a submit filter based on the passed in submitMode -func MakeSdexMakerModeFilter(submitMode api.SubmitMode, sdex *SDEX, tradingPair *model.TradingPair) SubmitFilter { +type makerModeFilter struct { + tradingPair *model.TradingPair + exchangeShim api.ExchangeShim + sdex *SDEX +} + +// MakeFilterMakerMode makes a submit filter based on the passed in submitMode +func MakeFilterMakerMode(submitMode api.SubmitMode, exchangeShim api.ExchangeShim, sdex *SDEX, tradingPair *model.TradingPair) SubmitFilter { if submitMode == api.SubmitModeMakerOnly { - return &sdexMakerFilter{ - tradingPair: tradingPair, - sdex: sdex, + return &makerModeFilter{ + tradingPair: tradingPair, + exchangeShim: exchangeShim, + sdex: sdex, } } return nil } -type sdexMakerFilter struct { - tradingPair *model.TradingPair - sdex *SDEX -} - -var _ SubmitFilter = &sdexMakerFilter{} +var _ SubmitFilter = &makerModeFilter{} -func (f *sdexMakerFilter) Apply(ops []build.TransactionMutator, sellingOffers []horizon.Offer, buyingOffers []horizon.Offer) ([]build.TransactionMutator, error) { - ob, e := f.sdex.GetOrderBook(f.tradingPair, math.MaxInt32) +func (f *makerModeFilter) Apply(ops []build.TransactionMutator, sellingOffers []horizon.Offer, buyingOffers []horizon.Offer) ([]build.TransactionMutator, error) { + ob, e := f.exchangeShim.GetOrderBook(f.tradingPair, 50) if e != nil { - return nil, fmt.Errorf("could not fetch SDEX orderbook: %s", e) + return nil, fmt.Errorf("could not fetch orderbook: %s", e) } ops, e = f.filterOps(ops, ob, sellingOffers, buyingOffers) @@ -54,8 +56,8 @@ func isNewLevel(lastPrice *model.Number, priceNumber *model.Number, isSell bool) return false } -func (f *sdexMakerFilter) collateOffers(traderOffers []horizon.Offer, isSell bool) ([]api.Level, error) { - oc := f.sdex.GetOrderConstraints(f.tradingPair) +func (f *makerModeFilter) collateOffers(traderOffers []horizon.Offer, isSell bool) ([]api.Level, error) { + oc := f.exchangeShim.GetOrderConstraints(f.tradingPair) levels := []api.Level{} var lastPrice *model.Number @@ -84,7 +86,7 @@ func (f *sdexMakerFilter) collateOffers(traderOffers []horizon.Offer, isSell boo return levels, nil } -func (f *sdexMakerFilter) topOrderPriceExcludingTrader(obSide []model.Order, traderOffers []horizon.Offer, isSell bool) (*model.Number, error) { +func (f *makerModeFilter) topOrderPriceExcludingTrader(obSide []model.Order, traderOffers []horizon.Offer, isSell bool) (*model.Number, error) { traderLevels, e := f.collateOffers(traderOffers, isSell) if e != nil { return nil, fmt.Errorf("unable to collate offers: %s", e) @@ -107,7 +109,7 @@ func (f *sdexMakerFilter) topOrderPriceExcludingTrader(obSide []model.Order, tra return nil, nil } -func (f *sdexMakerFilter) filterOps( +func (f *makerModeFilter) filterOps( ops []build.TransactionMutator, ob *model.OrderBook, sellingOffers []horizon.Offer, @@ -115,7 +117,7 @@ func (f *sdexMakerFilter) filterOps( ) ([]build.TransactionMutator, error) { baseAsset, quoteAsset, e := f.sdex.Assets() if e != nil { - return nil, fmt.Errorf("could not get sdex assets: %s", e) + return nil, fmt.Errorf("could not get assets: %s", e) } topBidPrice, e := f.topOrderPriceExcludingTrader(ob.Bids(), buyingOffers, false) @@ -167,11 +169,11 @@ func (f *sdexMakerFilter) filterOps( } } } - log.Printf("dropped %d, transformed %d, kept %d ops in sdexMakerFilter from original %d ops, len(filteredOps) = %d\n", numDropped, numTransformed, numKeep, len(ops), len(filteredOps)) + log.Printf("makerModeFilter: dropped %d, transformed %d, kept %d ops from original %d ops, len(filteredOps) = %d\n", numDropped, numTransformed, numKeep, len(ops), len(filteredOps)) return filteredOps, nil } -func (f *sdexMakerFilter) transformOfferMakerMode( +func (f *makerModeFilter) transformOfferMakerMode( baseAsset horizon.Asset, quoteAsset horizon.Asset, topBidPrice *model.Number, @@ -193,10 +195,10 @@ func (f *sdexMakerFilter) transformOfferMakerMode( if !isSell && topAskPrice != nil { // invert price when buying keep = 1/sellPrice < topAskPrice.AsFloat() - log.Printf("sdexMakerFilter: buying, keep = (op price) %.7f < %.7f (topAskPrice): keep = %v", 1/sellPrice, topAskPrice.AsFloat(), keep) + log.Printf("makerModeFilter: buying, keep = (op price) %.7f < %.7f (topAskPrice): keep = %v", 1/sellPrice, topAskPrice.AsFloat(), keep) } else if isSell && topBidPrice != nil { keep = sellPrice > topBidPrice.AsFloat() - log.Printf("sdexMakerFilter: selling, keep = (op price) %.7f > %.7f (topBidPrice): keep = %v", sellPrice, topBidPrice.AsFloat(), keep) + log.Printf("makerModeFilter: selling, keep = (op price) %.7f > %.7f (topBidPrice): keep = %v", sellPrice, topBidPrice.AsFloat(), keep) } else { price := sellPrice action := "selling" @@ -205,7 +207,7 @@ func (f *sdexMakerFilter) transformOfferMakerMode( action = " buying" } keep = true - log.Printf("sdexMakerFilter: %s, no market (op price = %.7f): keep = %v", action, price, keep) + log.Printf("makerModeFilter: %s, no market (op price = %.7f): keep = %v", action, price, keep) } if keep { diff --git a/plugins/orderConstraintsFilter.go b/plugins/orderConstraintsFilter.go index ee97189b4..6c025bc78 100644 --- a/plugins/orderConstraintsFilter.go +++ b/plugins/orderConstraintsFilter.go @@ -19,8 +19,8 @@ type orderConstraintsFilter struct { var _ SubmitFilter = &orderConstraintsFilter{} -// MakeOrderConstraintsFilter makes a submit filter based on the passed in orderConstraints -func MakeOrderConstraintsFilter( +// MakeFilterOrderConstraints makes a submit filter based on the passed in orderConstraints +func MakeFilterOrderConstraints( oc *model.OrderConstraints, baseAsset horizon.Asset, quoteAsset horizon.Asset, @@ -83,7 +83,7 @@ func (f *orderConstraintsFilter) Apply( } } - log.Printf("dropped %d, kept %d ops in orderConstraintsFilter from original %d ops, len(filteredOps) = %d\n", numDropped, numKeep, len(ops), len(filteredOps)) + log.Printf("orderConstraintsFilter: dropped %d, kept %d ops from original %d ops, len(filteredOps) = %d\n", numDropped, numKeep, len(ops), len(filteredOps)) return filteredOps, nil } diff --git a/trader/trader.go b/trader/trader.go index 3f1d752f1..0a700ace0 100644 --- a/trader/trader.go +++ b/trader/trader.go @@ -70,10 +70,10 @@ func MakeBot( submitFilters := []plugins.SubmitFilter{} oc := exchangeShim.GetOrderConstraints(tradingPair) - orderConstraintsFilter := plugins.MakeOrderConstraintsFilter(oc, assetBase, assetQuote) + orderConstraintsFilter := plugins.MakeFilterOrderConstraints(oc, assetBase, assetQuote) submitFilters = append(submitFilters, orderConstraintsFilter) - sdexSubmitFilter := plugins.MakeSdexMakerModeFilter(submitMode, sdex, tradingPair) + sdexSubmitFilter := plugins.MakeFilterMakerMode(submitMode, exchangeShim, sdex, tradingPair) if sdexSubmitFilter != nil { submitFilters = append(submitFilters, sdexSubmitFilter) }