From 3c9af0cb098e0476512de2d002693b9b6cae426b Mon Sep 17 00:00:00 2001
From: Nikhil Saraf <1028334+nikhilsaraf@users.noreply.github.com>
Date: Sun, 5 May 2019 20:17:06 -0700
Subject: [PATCH] allow setting custom URL for CCXT-rest server (#167), closes
 #131

---
 cmd/trade.go                              |  9 +++++++++
 examples/configs/trader/sample_trader.cfg |  3 +++
 support/sdk/ccxt.go                       | 16 +++++++++++++++-
 trader/config.go                          |  1 +
 4 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/cmd/trade.go b/cmd/trade.go
index a447a747a..73aac2356 100644
--- a/cmd/trade.go
+++ b/cmd/trade.go
@@ -23,6 +23,7 @@ import (
 	"github.com/stellar/kelp/support/monitoring"
 	"github.com/stellar/kelp/support/networking"
 	"github.com/stellar/kelp/support/prefs"
+	"github.com/stellar/kelp/support/sdk"
 	"github.com/stellar/kelp/support/utils"
 	"github.com/stellar/kelp/trader"
 )
@@ -423,6 +424,14 @@ func runTradeCmd(options inputs) {
 		}
 	}
 
+	if botConfig.CcxtRestURL != nil {
+		e := sdk.SetBaseURL(*botConfig.CcxtRestURL)
+		if e != nil {
+			logger.Fatal(l, fmt.Errorf("unable to set CCXT-rest URL to '%s': %s", *botConfig.CcxtRestURL, e))
+		}
+	}
+	l.Infof("using CCXT-rest URL: %s\n", sdk.GetBaseURL())
+
 	ieif := plugins.MakeIEIF(botConfig.IsTradingSdex())
 	network := utils.ParseNetwork(botConfig.HorizonURL)
 	exchangeShim, sdex := makeExchangeShimSdex(
diff --git a/examples/configs/trader/sample_trader.cfg b/examples/configs/trader/sample_trader.cfg
index b2e48ccaf..72cb8c84d 100644
--- a/examples/configs/trader/sample_trader.cfg
+++ b/examples/configs/trader/sample_trader.cfg
@@ -48,6 +48,9 @@ FILL_TRACKER_DELETE_CYCLES_THRESHOLD=0
 # the url for your horizon instance. If this url contains the string "test" then the bot assumes it is using the test network.
 HORIZON_URL="https://horizon-testnet.stellar.org"
 
+# the URL to use for your CCXT-rest instance. Defaults to http://localhost:3000 if unset
+#CCXT_REST_URL="http://localhost:3000"
+
 # specify parameters for how we compute the operation fee from the /fee_stats endpoint
 [FEE]
 # trigger when "ledger_capacity_usage" in /fee_stats is >= this value
diff --git a/support/sdk/ccxt.go b/support/sdk/ccxt.go
index 219c65298..c2bf7ec6d 100644
--- a/support/sdk/ccxt.go
+++ b/support/sdk/ccxt.go
@@ -16,7 +16,21 @@ import (
 )
 
 // ccxtBaseURL should not have suffix of '/'
-const ccxtBaseURL = "http://localhost:3000"
+var ccxtBaseURL = "http://localhost:3000"
+
+// SetBaseURL allows setting the base URL for ccxt
+func SetBaseURL(baseURL string) error {
+	if strings.HasSuffix(baseURL, "/") {
+		return fmt.Errorf("invalid format for baseURL, should not end with trailing '/': %s", baseURL)
+	}
+	ccxtBaseURL = baseURL
+	return nil
+}
+
+// GetBaseURL returns the base URL for ccxt
+func GetBaseURL() string {
+	return ccxtBaseURL
+}
 
 // Ccxt Rest SDK (https://github.com/franz-see/ccxt-rest, https://github.com/ccxt/ccxt/)
 type Ccxt struct {
diff --git a/trader/config.go b/trader/config.go
index 6d7f9e0d8..a2060cafd 100644
--- a/trader/config.go
+++ b/trader/config.go
@@ -32,6 +32,7 @@ type BotConfig struct {
 	FillTrackerSleepMillis             uint32     `valid:"-" toml:"FILL_TRACKER_SLEEP_MILLIS"`
 	FillTrackerDeleteCyclesThreshold   int64      `valid:"-" toml:"FILL_TRACKER_DELETE_CYCLES_THRESHOLD"`
 	HorizonURL                         string     `valid:"-" toml:"HORIZON_URL"`
+	CcxtRestURL                        *string    `valid:"-" toml:"CCXT_REST_URL"`
 	Fee                                *FeeConfig `valid:"-" toml:"FEE"`
 	CentralizedPricePrecisionOverride  *int8      `valid:"-" toml:"CENTRALIZED_PRICE_PRECISION_OVERRIDE"`
 	CentralizedVolumePrecisionOverride *int8      `valid:"-" toml:"CENTRALIZED_VOLUME_PRECISION_OVERRIDE"`