diff --git a/btcchinahttp.go b/btcchinahttp.go index bad817eb..ee9d0b69 100644 --- a/btcchinahttp.go +++ b/btcchinahttp.go @@ -711,7 +711,7 @@ func (b *BTCChina) SendAuthenticatedHTTPRequest(method string, params []interfac } } } - encoded += strings.Join(items, ",") + encoded += JoinStrings(items, ",") } if b.Verbose { log.Println(encoded) diff --git a/common.go b/common.go index ca2bdde8..be167575 100644 --- a/common.go +++ b/common.go @@ -87,6 +87,10 @@ func Base64Encode(input []byte) string { return base64.StdEncoding.EncodeToString(input) } +func StringContains(input, substring string) bool { + return strings.Contains(input, substring) +} + func JoinStrings(input []string, seperator string) string { return strings.Join(input, seperator) } diff --git a/cryptsywebsocket.go b/cryptsywebsocket.go index 12d3f56a..869695b6 100644 --- a/cryptsywebsocket.go +++ b/cryptsywebsocket.go @@ -3,7 +3,6 @@ package main import ( "github.com/toorop/go-pusher" "log" - "strings" ) type CryptsyPusherTrade struct { @@ -72,7 +71,7 @@ func (c *Cryptsy) PusherClient(marketID []string) { for c.Enabled && c.Websocket { select { case data := <-dataChannel: - if strings.Contains(data.Data, "topbuy") { + if StringContains(data.Data, "topbuy") { result := CryptsyPusherTicker{} err := JSONDecode([]byte(data.Data), &result) if err != nil { diff --git a/currency.go b/currency.go index 6548368f..1f6a7aeb 100644 --- a/currency.go +++ b/currency.go @@ -1,28 +1,28 @@ package main import ( + "errors" "fmt" + "log" "net/url" "strings" "time" - "errors" - "log" ) type Rate struct { - Id string `json:"id"` - Name string `json:"Name"` + Id string `json:"id"` + Name string `json:"Name"` Rate float64 `json:",string"` - Date string `json:"Date"` - Time string `json:"Time"` - Ask float64 `json:",string"` - Bid float64 `json:",string"` + Date string `json:"Date"` + Time string `json:"Time"` + Ask float64 `json:",string"` + Bid float64 `json:",string"` } type YahooJSONResponseInfo struct { - Count int `json:"count"` + Count int `json:"count"` Created time.Time `json:"created"` - Lang string `json:"lang"` + Lang string `json:"lang"` } type YahooJSONResponse struct { @@ -35,41 +35,40 @@ type YahooJSONResponse struct { } const ( - YAHOO_YQL_URL = "http://query.yahooapis.com/v1/public/yql" - YAHOO_DATABASE = "store://datatables.org/alltableswithkeys" + YAHOO_YQL_URL = "http://query.yahooapis.com/v1/public/yql" + YAHOO_DATABASE = "store://datatables.org/alltableswithkeys" DEFAULT_CURRENCIES = "USD,AUD,EUR,CNY" - ) var ( - CurrencyStore YahooJSONResponse + CurrencyStore YahooJSONResponse ErrCurrencyDataNotFetched = errors.New("Yahoo currency data has not been fetched yet.") - ErrCurrencyNotFound = errors.New("Unable to find specified currency.") - ErrQueryingYahoo = errors.New("Unable to query Yahoo currency values.") + ErrCurrencyNotFound = errors.New("Unable to find specified currency.") + ErrQueryingYahoo = errors.New("Unable to query Yahoo currency values.") ) -func RetrieveConfigCurrencyPairs(config Config) (error) { +func RetrieveConfigCurrencyPairs(config Config) error { currencyPairs := "" for _, exchange := range config.Exchanges { - if (exchange.Enabled) { + if exchange.Enabled { var result []string - if strings.Contains(exchange.BaseCurrencies, ",") { - result = strings.Split(exchange.BaseCurrencies, ",") + if StringContains(exchange.BaseCurrencies, ",") { + result = SplitStrings(exchange.BaseCurrencies, ",") } else { - if strings.Contains(DEFAULT_CURRENCIES, exchange.BaseCurrencies) { - result = strings.Split(DEFAULT_CURRENCIES, ",") + if StringContains(DEFAULT_CURRENCIES, exchange.BaseCurrencies) { + result = SplitStrings(DEFAULT_CURRENCIES, ",") } else { - result = strings.Split(exchange.BaseCurrencies + "," + DEFAULT_CURRENCIES, ",") + result = SplitStrings(exchange.BaseCurrencies+","+DEFAULT_CURRENCIES, ",") } } for _, s := range result { - if (!strings.Contains(currencyPairs, s)) { + if !StringContains(currencyPairs, s) { currencyPairs += s + "," } } } } - currencyPairs = currencyPairs[0:len(currencyPairs)-1] + currencyPairs = currencyPairs[0 : len(currencyPairs)-1] err := QueryYahooCurrencyValues(currencyPairs) if err != nil { @@ -80,8 +79,8 @@ func RetrieveConfigCurrencyPairs(config Config) (error) { return nil } -func MakecurrencyPairs(supportedCurrencies string) (string) { - currencies := strings.Split(supportedCurrencies, ",") +func MakecurrencyPairs(supportedCurrencies string) string { + currencies := SplitStrings(supportedCurrencies, ",") pairs := "" count := len(currencies) for i := 0; i < count; i++ { @@ -92,7 +91,7 @@ func MakecurrencyPairs(supportedCurrencies string) (string) { } } } - return pairs[0:len(pairs)-1] + return pairs[0 : len(pairs)-1] } func ConvertCurrency(amount float64, from, to string) (float64, error) { @@ -109,7 +108,7 @@ func ConvertCurrency(amount float64, from, to string) (float64, error) { return 0, ErrCurrencyNotFound } -func QueryYahooCurrencyValues(currencies string) (error) { +func QueryYahooCurrencyValues(currencies string) error { currencyPairs := MakecurrencyPairs(currencies) log.Printf("Supported currency pairs: %s\n", currencyPairs) @@ -128,10 +127,10 @@ func QueryYahooCurrencyValues(currencies string) (error) { } err = JSONDecode([]byte(resp), &CurrencyStore) - + if err != nil { return err } - + return nil -} \ No newline at end of file +} diff --git a/events.go b/events.go index a8d7993f..c7487b79 100644 --- a/events.go +++ b/events.go @@ -2,40 +2,39 @@ package main import ( "errors" - "log" "fmt" - "strings" + "log" "strconv" ) const ( - ITEM_PRICE = "PRICE" - GREATER_THAN = ">" + ITEM_PRICE = "PRICE" + GREATER_THAN = ">" GREATER_THAN_OR_EQUAL = ">=" - LESS_THAN = "<" - LESS_THAN_OR_EQUAL = "<=" - IS_EQUAL = "==" - ACTION_SMS_NOTIFY = "SMS" - ACTION_CONSOLE_PRINT = "CONSOLE_PRINT" + LESS_THAN = "<" + LESS_THAN_OR_EQUAL = "<=" + IS_EQUAL = "==" + ACTION_SMS_NOTIFY = "SMS" + ACTION_CONSOLE_PRINT = "CONSOLE_PRINT" ) var ( - ErrInvalidItem = errors.New("Invalid item.") + ErrInvalidItem = errors.New("Invalid item.") ErrInvalidCondition = errors.New("Invalid conditional option.") - ErrInvalidAction = errors.New("Invalid action.") + ErrInvalidAction = errors.New("Invalid action.") ErrExchangeDisabled = errors.New("Desired exchange is disabled.") ) type Event struct { - ID int - Exchange string - Item string + ID int + Exchange string + Item string Condition string - Action string - Executed bool + Action string + Executed bool } -var Events[] *Event +var Events []*Event func AddEvent(Exchange, Item, Condition, Action string) (int, error) { err := IsValidEvent(Exchange, Item, Condition, Action) @@ -49,7 +48,7 @@ func AddEvent(Exchange, Item, Condition, Action string) (int, error) { if len(Events) == 0 { Event.ID = 0 } else { - Event.ID = len(Events)+1 + Event.ID = len(Events) + 1 } Event.Exchange = Exchange @@ -61,7 +60,7 @@ func AddEvent(Exchange, Item, Condition, Action string) (int, error) { return Event.ID, nil } -func RemoveEvent(EventID int) (bool) { +func RemoveEvent(EventID int) bool { for i, x := range Events { if x.ID == EventID { Events = append(Events[:i], Events[i+1:]...) @@ -83,9 +82,9 @@ func GetEventCounter() (int, int) { return total, executed } -func (e *Event) ExecuteAction() (bool) { - if strings.Contains(e.Action, ",") { - action := strings.Split(e.Action, ",") +func (e *Event) ExecuteAction() bool { + if StringContains(e.Action, ",") { + action := SplitStrings(e.Action, ",") if action[0] == ACTION_SMS_NOTIFY { message := fmt.Sprintf("Event triggered: %s", e.EventToString()) if action[1] == "ALL" { @@ -100,19 +99,19 @@ func (e *Event) ExecuteAction() (bool) { return true } -func (e *Event) EventToString() (string) { - condition := strings.Split(e.Condition, ",") - return fmt.Sprintf("If the %s on %s is %s then %s.", e.Item, e.Exchange, condition[0] + " " + condition[1], e.Action) +func (e *Event) EventToString() string { + condition := SplitStrings(e.Condition, ",") + return fmt.Sprintf("If the %s on %s is %s then %s.", e.Item, e.Exchange, condition[0]+" "+condition[1], e.Action) } -func (e *Event) CheckCondition() (bool) { +func (e *Event) CheckCondition() bool { lastPrice := 0.00 - condition := strings.Split(e.Condition,",") + condition := SplitStrings(e.Condition, ",") targetPrice, _ := strconv.ParseFloat(condition[1], 64) if bot.exchange.bitfinex.GetName() == e.Exchange { lastPrice = bot.exchange.bitfinex.GetTicker("btcusd").Last - } else if bot.exchange.bitstamp.GetName() == e.Exchange { + } else if bot.exchange.bitstamp.GetName() == e.Exchange { lastPrice = bot.exchange.bitstamp.GetTicker().Last } else if bot.exchange.coinbase.GetName() == e.Exchange { lastPrice = bot.exchange.coinbase.GetTicker("BTC-USD").Price @@ -143,27 +142,32 @@ func (e *Event) CheckCondition() (bool) { } switch condition[0] { - case GREATER_THAN: { + case GREATER_THAN: + { if lastPrice > targetPrice { return e.ExecuteAction() } } - case GREATER_THAN_OR_EQUAL: { + case GREATER_THAN_OR_EQUAL: + { if lastPrice >= targetPrice { return e.ExecuteAction() } } - case LESS_THAN: { + case LESS_THAN: + { if lastPrice < targetPrice { return e.ExecuteAction() } } - case LESS_THAN_OR_EQUAL: { + case LESS_THAN_OR_EQUAL: + { if lastPrice <= targetPrice { return e.ExecuteAction() } } - case IS_EQUAL: { + case IS_EQUAL: + { if lastPrice == targetPrice { return e.ExecuteAction() } @@ -172,7 +176,7 @@ func (e *Event) CheckCondition() (bool) { return false } -func IsValidEvent(Exchange, Item, Condition, Action string) (error) { +func IsValidEvent(Exchange, Item, Condition, Action string) error { if !IsValidExchange(Exchange) { return ErrExchangeDisabled } @@ -181,18 +185,18 @@ func IsValidEvent(Exchange, Item, Condition, Action string) (error) { return ErrInvalidItem } - if !strings.Contains(Condition, ",") { + if !StringContains(Condition, ",") { return ErrInvalidCondition } - condition := strings.Split(Condition, ",") + condition := SplitStrings(Condition, ",") if !IsValidCondition(condition[0]) || len(condition[1]) == 0 { return ErrInvalidCondition } - if strings.Contains(Action, ",") { - action := strings.Split(Action, ",") + if StringContains(Action, ",") { + action := SplitStrings(Action, ",") if action[0] != ACTION_SMS_NOTIFY { return ErrInvalidAction @@ -226,7 +230,7 @@ func CheckEvents() { } } -func IsValidExchange(Exchange string) (bool) { +func IsValidExchange(Exchange string) bool { if bot.exchange.bitfinex.GetName() == Exchange && bot.exchange.bitfinex.IsEnabled() || bot.exchange.bitstamp.GetName() == Exchange && bot.exchange.bitstamp.IsEnabled() || bot.exchange.btcchina.GetName() == Exchange && bot.exchange.btcchina.IsEnabled() || @@ -241,31 +245,31 @@ func IsValidExchange(Exchange string) (bool) { bot.exchange.okcoinChina.GetName() == Exchange && bot.exchange.okcoinChina.IsEnabled() || bot.exchange.okcoinIntl.GetName() == Exchange && bot.exchange.okcoinIntl.IsEnabled() || bot.exchange.anx.GetName() == Exchange && bot.exchange.anx.IsEnabled() { - return true - } - return false + return true + } + return false } -func IsValidCondition(Condition string) (bool) { +func IsValidCondition(Condition string) bool { switch Condition { - case GREATER_THAN, GREATER_THAN_OR_EQUAL, LESS_THAN, LESS_THAN_OR_EQUAL, IS_EQUAL: - return true + case GREATER_THAN, GREATER_THAN_OR_EQUAL, LESS_THAN, LESS_THAN_OR_EQUAL, IS_EQUAL: + return true } return false } -func IsValidAction(Action string) (bool) { +func IsValidAction(Action string) bool { switch Action { - case ACTION_SMS_NOTIFY, ACTION_CONSOLE_PRINT: - return true + case ACTION_SMS_NOTIFY, ACTION_CONSOLE_PRINT: + return true } return false } -func IsValidItem(Item string) (bool) { +func IsValidItem(Item string) bool { switch Item { - case ITEM_PRICE: - return true + case ITEM_PRICE: + return true } return false -} \ No newline at end of file +} diff --git a/kraken.go b/kraken.go index bd54b011..ca5eac53 100644 --- a/kraken.go +++ b/kraken.go @@ -149,9 +149,9 @@ func (k *Kraken) GetTicker(symbol string) interface{} { log.Println(err) return "" } - if strings.Contains(symbol, "LTC") { + if StringContains(symbol, "LTC") { return result["XLTCZUSD"] - } else if strings.Contains(symbol, "XBT") { + } else if StringContains(symbol, "XBT") { return result["XXBTZUSD"] } return nil diff --git a/okcoinwebsocket.go b/okcoinwebsocket.go index c69de0b6..14345701 100644 --- a/okcoinwebsocket.go +++ b/okcoinwebsocket.go @@ -499,7 +499,7 @@ func (o *OKCoin) WebsocketClient(currencies []string) { } switch true { - case strings.Contains(channelStr, "ticker") && !strings.Contains(channelStr, "future"): + case StringContains(channelStr, "ticker") && !StringContains(channelStr, "future"): ticker := OKCoinWebsocketTicker{} err = JSONDecode(dataJSON, &ticker) @@ -507,7 +507,7 @@ func (o *OKCoin) WebsocketClient(currencies []string) { log.Println(err) continue } - case strings.Contains(channelStr, "ticker") && strings.Contains(channelStr, "future"): + case StringContains(channelStr, "ticker") && StringContains(channelStr, "future"): ticker := OKCoinWebsocketFuturesTicker{} err = JSONDecode(dataJSON, &ticker) @@ -515,7 +515,7 @@ func (o *OKCoin) WebsocketClient(currencies []string) { log.Println(err) continue } - case strings.Contains(channelStr, "depth"): + case StringContains(channelStr, "depth"): orderbook := OKCoinWebsocketOrderbook{} err = JSONDecode(dataJSON, &orderbook) @@ -523,7 +523,7 @@ func (o *OKCoin) WebsocketClient(currencies []string) { log.Println(err) continue } - case strings.Contains(channelStr, "trades_v1") || strings.Contains(channelStr, "trade_v1"): + case StringContains(channelStr, "trades_v1") || StringContains(channelStr, "trade_v1"): type TradeResponse struct { Data [][]string } @@ -536,7 +536,7 @@ func (o *OKCoin) WebsocketClient(currencies []string) { continue } // to-do: convert from string array to trade struct - case strings.Contains(channelStr, "kline"): + case StringContains(channelStr, "kline"): klines := []interface{}{} err := JSONDecode(dataJSON, &klines) @@ -544,7 +544,7 @@ func (o *OKCoin) WebsocketClient(currencies []string) { log.Println(err) continue } - case strings.Contains(channelStr, "spot") && strings.Contains(channelStr, "realtrades"): + case StringContains(channelStr, "spot") && StringContains(channelStr, "realtrades"): if string(dataJSON) == "null" { continue } @@ -555,7 +555,7 @@ func (o *OKCoin) WebsocketClient(currencies []string) { log.Println(err) continue } - case strings.Contains(channelStr, "future") && strings.Contains(channelStr, "realtrades"): + case StringContains(channelStr, "future") && StringContains(channelStr, "realtrades"): if string(dataJSON) == "null" { continue } @@ -566,7 +566,7 @@ func (o *OKCoin) WebsocketClient(currencies []string) { log.Println(err) continue } - case strings.Contains(channelStr, "spot") && strings.Contains(channelStr, "trade") || strings.Contains(channelStr, "futures") && strings.Contains(channelStr, "trade"): + case StringContains(channelStr, "spot") && StringContains(channelStr, "trade") || StringContains(channelStr, "futures") && StringContains(channelStr, "trade"): tradeOrder := OKCoinWebsocketTradeOrderResponse{} err := JSONDecode(dataJSON, &tradeOrder) @@ -574,7 +574,7 @@ func (o *OKCoin) WebsocketClient(currencies []string) { log.Println(err) continue } - case strings.Contains(channelStr, "cancel_order"): + case StringContains(channelStr, "cancel_order"): cancelOrder := OKCoinWebsocketTradeOrderResponse{} err := JSONDecode(dataJSON, &cancelOrder) @@ -582,7 +582,7 @@ func (o *OKCoin) WebsocketClient(currencies []string) { log.Println(err) continue } - case strings.Contains(channelStr, "spot") && strings.Contains(channelStr, "userinfo"): + case StringContains(channelStr, "spot") && StringContains(channelStr, "userinfo"): userinfo := OKCoinWebsocketUserinfo{} err = JSONDecode(dataJSON, &userinfo) @@ -590,7 +590,7 @@ func (o *OKCoin) WebsocketClient(currencies []string) { log.Println(err) continue } - case strings.Contains(channelStr, "futureusd_userinfo"): + case StringContains(channelStr, "futureusd_userinfo"): userinfo := OKCoinWebsocketFuturesUserInfo{} err = JSONDecode(dataJSON, &userinfo) @@ -598,7 +598,7 @@ func (o *OKCoin) WebsocketClient(currencies []string) { log.Println(err) continue } - case strings.Contains(channelStr, "spot") && strings.Contains(channelStr, "order_info"): + case StringContains(channelStr, "spot") && StringContains(channelStr, "order_info"): type OrderInfoResponse struct { Result bool `json:"result"` Orders []OKCoinWebsocketOrder `json:"orders"` @@ -610,7 +610,7 @@ func (o *OKCoin) WebsocketClient(currencies []string) { log.Println(err) continue } - case strings.Contains(channelStr, "futureusd_order_info"): + case StringContains(channelStr, "futureusd_order_info"): type OrderInfoResponse struct { Result bool `json:"result"` Orders []OKCoinWebsocketFuturesOrder `json:"orders"` @@ -622,7 +622,7 @@ func (o *OKCoin) WebsocketClient(currencies []string) { log.Println(err) continue } - case strings.Contains(channelStr, "future_index"): + case StringContains(channelStr, "future_index"): index := OKCoinWebsocketFutureIndex{} err = JSONDecode(dataJSON, &index)