Added dynamic updating of available currencies based on Exchange products.

This commit is contained in:
Adrian Gallagher
2015-05-14 14:59:40 +10:00
parent 58606e300f
commit b0b119d790
5 changed files with 88 additions and 8 deletions

View File

@@ -196,7 +196,18 @@ func (b *Bitfinex) Run() {
if err != nil {
log.Printf("%s Failed to get available symbols.\n", b.GetName())
} else {
b.AvailablePairs = SplitStrings(StringToUpper(JoinStrings(exchangeProducts, ",")), ",")
exchangeProducts = SplitStrings(StringToUpper(JoinStrings(exchangeProducts, ",")), ",")
diff := StringSliceDifference(b.AvailablePairs, exchangeProducts)
if len(diff) > 0 {
exch, err := GetExchangeConfig(b.Name)
if err != nil {
log.Println(err)
} else {
log.Printf("%s Updating available pairs. Difference: %s.\n", b.Name, diff)
exch.AvailablePairs = JoinStrings(exchangeProducts, ",")
UpdateExchangeConfig(exch)
}
}
}
for b.Enabled {

View File

@@ -148,7 +148,17 @@ func (c *Coinbase) Run() {
currencies = append(currencies, x.ID[0:3]+x.ID[4:])
}
}
c.AvailablePairs = currencies
diff := StringSliceDifference(c.AvailablePairs, currencies)
if len(diff) > 0 {
exch, err := GetExchangeConfig(c.Name)
if err != nil {
log.Println(err)
} else {
log.Printf("%s Updating available pairs. Changed %s.\n", c.Name, diff)
exch.AvailablePairs = JoinStrings(currencies, ",")
UpdateExchangeConfig(exch)
}
}
}
for c.Enabled {

View File

@@ -87,6 +87,28 @@ func Base64Encode(input []byte) string {
return base64.StdEncoding.EncodeToString(input)
}
func StringSliceDifference(slice1 []string, slice2 []string) []string {
var diff []string
for i := 0; i < 2; i++ {
for _, s1 := range slice1 {
found := false
for _, s2 := range slice2 {
if s1 == s2 {
found = true
break
}
}
if !found {
diff = append(diff, s1)
}
}
if i == 0 {
slice1, slice2 = slice2, slice1
}
}
return diff
}
func StringContains(input, substring string) bool {
return strings.Contains(input, substring)
}

View File

@@ -18,6 +18,7 @@ var (
ErrExchangeEnabledPairsEmpty = "Exchange %s: Enabled pairs is empty."
ErrExchangeBaseCurrenciesEmpty = "Exchange %s: Base currencies is empty."
ErrExchangeAuthAPIDefaultOrEmptyValues = "WARNING -- Exchange %s: Authenticated API support disabled due to default/empty APIKey/Secret/ClientID values."
ErrExchangeNotFound = "Exchange %s: Not found."
)
type SMSContacts struct {
@@ -49,6 +50,25 @@ type Exchanges struct {
BaseCurrencies string
}
func GetExchangeConfig(name string) (Exchanges, error) {
for i, _ := range bot.config.Exchanges {
if bot.config.Exchanges[i].Name == name {
return bot.config.Exchanges[i], nil
}
}
return Exchanges{}, fmt.Errorf(ErrExchangeNotFound, name)
}
func UpdateExchangeConfig(e Exchanges) error {
for i, _ := range bot.config.Exchanges {
if bot.config.Exchanges[i].Name == e.Name {
bot.config.Exchanges[i] = e
return nil
}
}
return fmt.Errorf(ErrExchangeNotFound, e.Name)
}
func CheckConfigValues() error {
for i, exch := range bot.config.Exchanges {
if exch.Enabled {

View File

@@ -46,7 +46,7 @@ type Cryptsy struct {
Market map[string]CryptsyMarket
Ticker map[string]CryptsyTicker
Volume map[string]CryptsyVolume
Currencies map[string]CryptsyCurrency
Currencies []CryptsyCurrency
}
type CryptsyMarket struct {
@@ -145,7 +145,6 @@ func (c *Cryptsy) SetDefaults() {
c.Market = make(map[string]CryptsyMarket)
c.Ticker = make(map[string]CryptsyTicker)
c.Volume = make(map[string]CryptsyVolume)
c.Currencies = make(map[string]CryptsyCurrency)
}
func (c *Cryptsy) GetName() string {
@@ -179,6 +178,27 @@ func (c *Cryptsy) Run() {
go c.PusherClient()
}
err := c.GetMarkets()
if err != nil {
log.Println(err)
} else {
markets := []string{}
for x, _ := range c.Market {
markets = append(markets, x)
}
diff := StringSliceDifference(c.AvailablePairs, markets)
if len(diff) > 0 {
exch, err := GetExchangeConfig(c.Name)
if err != nil {
log.Println(err)
} else {
log.Printf("%s Updating available pairs. Difference: %s.\n", c.Name, diff)
exch.AvailablePairs = JoinStrings(markets, ",")
UpdateExchangeConfig(exch)
}
}
}
for c.Enabled {
err := c.GetMarkets()
if err != nil {
@@ -347,10 +367,7 @@ func (c *Cryptsy) GetCurrencies() error {
if !response.Success {
return errors.New("Unable to get Cryptsy currency data.")
}
for _, x := range response.Data {
c.Currencies[x.ID] = x
}
c.Currencies = response.Data
return nil
}