Skip to content

Commit

Permalink
Merge pull request #3 from linkpoolio/binance-and-error-handling
Browse files Browse the repository at this point in the history
Binance Fix, Logrus and Set Pairs error handling
  • Loading branch information
jleeh authored Jul 23, 2018
2 parents 955b99e + 295be17 commit 7e181cd
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 41 deletions.
23 changes: 22 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 16 additions & 10 deletions exchange/binance.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package exchange

import (
"github.com/adshao/go-binance"
"context"
"fmt"
"log"
)

type Binance struct {
Expand All @@ -17,6 +14,15 @@ type BinanceProduct struct {
Volume string `json:"quoteVolume"`
}

type BinanceSymbol struct {
BaseAsset string `json:"baseAsset"`
QuoteAsset string `json:"quoteAsset"`
}

type BinanceInfo struct {
Symbols []*BinanceSymbol `json:"symbols"`
}

func (exc *Binance) GetResponse(base, quote string) (*Response, *Error) {
var bp BinanceProduct
config := exc.GetConfig()
Expand All @@ -27,24 +33,24 @@ func (exc *Binance) GetResponse(base, quote string) (*Response, *Error) {
return &Response{config.Name, ToFloat64(bp.LastPrice), ToFloat64(bp.Volume)}, nil
}

func (exc *Binance) SetPairs() {
clientInterface := exc.GetConfig().Client
client := clientInterface.(*binance.Client)
func (exc *Binance) SetPairs() *Error {
var bi BinanceInfo
config := exc.GetConfig()

exchangeInfo, err := client.NewExchangeInfoService().Do(context.Background())
err := HttpGet(config, "/exchangeInfo", &bi)
if err != nil {
log.Fatal(err)
return err
}

for _, product := range exchangeInfo.Symbols {
for _, product := range bi.Symbols {
exc.Pairs = append(exc.Pairs, &Pair{product.BaseAsset, product.QuoteAsset})
}
return nil
}

func (exc *Binance) GetConfig() *Config {
return &Config{
Name: "Binance",
BaseUrl: "https://www.binance.com/api/v1",
Client: binance.NewClient("", ""),
Pairs: exc.Pairs}
}
6 changes: 3 additions & 3 deletions exchange/bitfinex.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package exchange

import (
"fmt"
"log"
"strings"
)

Expand All @@ -27,12 +26,12 @@ func (exc *Bitfinex) GetResponse(base, quote string) (*Response, *Error) {
return &Response{Name: config.Name, Price: ToFloat64(ticker.LastPrice), Volume: volume}, nil
}

func (exc *Bitfinex) SetPairs() {
func (exc *Bitfinex) SetPairs() *Error {
var pairs []string
config := exc.GetConfig()
err := HttpGet(config, "/symbols", &pairs)
if err != nil {
log.Fatal(err)
return err
}
// We have to assume all BTC pairs are 3char base, 3char quote. No base/quote given in API.
for _, pair := range pairs {
Expand All @@ -42,6 +41,7 @@ func (exc *Bitfinex) SetPairs() {
&Pair{Base: strings.ToUpper(pair[0:3]), Quote: strings.ToUpper(pair[3:6])})
}
}
return nil
}

func (exc *Bitfinex) GetConfig() *Config {
Expand Down
6 changes: 3 additions & 3 deletions exchange/bitstamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package exchange
import (
"fmt"
"strings"
"log"
)

type Bitstamp struct {
Expand Down Expand Up @@ -32,19 +31,20 @@ func (exc *Bitstamp) GetResponse(base, quote string) (*Response, *Error) {
return &Response{exc.GetConfig().Name, ToFloat64(bst.Last), volume}, nil
}

func (exc *Bitstamp) SetPairs() {
func (exc *Bitstamp) SetPairs() *Error {
var pairs []BitstampPair
config := exc.GetConfig()
err := HttpGet(config, "/trading-pairs-info/", &pairs)
if err != nil {
log.Fatal(err)
return err
}
for _, pair := range pairs {
if pair.Trading == "Enabled" {
currencies := strings.Split(pair.Name, "/")
exc.Pairs = append(exc.Pairs, &Pair{currencies[0], currencies[1]})
}
}
return nil
}

func (exc *Bitstamp) GetConfig() *Config {
Expand Down
6 changes: 3 additions & 3 deletions exchange/bittrex.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package exchange

import (
"fmt"
"log"
)

type Bittrex struct {
Expand Down Expand Up @@ -43,16 +42,17 @@ func (exc *Bittrex) GetResponse(base, quote string) (*Response, *Error) {
return &Response{Name: config.Name, Price: summaries.Result[0].Last, Volume: summaries.Result[0].Volume}, nil
}

func (exc *Bittrex) SetPairs() {
func (exc *Bittrex) SetPairs() *Error {
var markets BittrexMarkets
config := exc.GetConfig()
err := HttpGet(config, "/public/getmarkets", &markets)
if err != nil {
log.Fatal(err)
return err
}
for _, pair := range markets.Result {
exc.Pairs = append(exc.Pairs, &Pair{Base: pair.BaseCurrency, Quote: pair.MarketCurrency})
}
return nil
}

func (exc *Bittrex) GetConfig() *Config {
Expand Down
7 changes: 4 additions & 3 deletions exchange/gdax.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package exchange
import (
"github.com/preichenberger/go-gdax"
"fmt"
"log"
)

type GDAX struct {
Expand All @@ -23,17 +22,19 @@ func (exc *GDAX) GetResponse(base, quote string) (*Response, *Error) {
return &Response{exc.GetConfig().Name, ticker.Price, ticker.Volume * ticker.Price}, nil
}

func (exc *GDAX) SetPairs() {
func (exc *GDAX) SetPairs() *Error {
clientInterface := exc.GetConfig().Client
client := clientInterface.(*gdax.Client)

products, err := client.GetProducts()
if err != nil {
log.Fatal(err)
return &Error{Exchange: exc.GetConfig().Name, Message: err.Error()}
}
for _, product := range products {
exc.Pairs = append(exc.Pairs, &Pair{product.BaseCurrency, product.QuoteCurrency})
}

return nil
}

func (exc *GDAX) GetConfig() *Config {
Expand Down
6 changes: 3 additions & 3 deletions exchange/hitbtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package exchange

import (
"fmt"
"log"
)

type HitBtc struct {
Expand Down Expand Up @@ -30,16 +29,17 @@ func (exc *HitBtc) GetResponse(base, quote string) (*Response, *Error) {
return &Response{config.Name, ToFloat64(ticker.Last), ToFloat64(ticker.VolumeQuote)}, nil
}

func (exc *HitBtc) SetPairs() {
func (exc *HitBtc) SetPairs() *Error {
var pairs []HitBtcPair
config := exc.GetConfig()
err := HttpGet(config, "/public/symbol/", &pairs)
if err != nil {
log.Fatal(err)
return err
}
for _, pair := range pairs {
exc.Pairs = append(exc.Pairs, &Pair{Base: pair.Base, Quote: pair.Quote})
}
return nil
}

func (exc *HitBtc) GetConfig() *Config {
Expand Down
6 changes: 3 additions & 3 deletions exchange/huobi.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package exchange

import (
"fmt"
"log"
"strings"
)

Expand Down Expand Up @@ -45,16 +44,17 @@ func (exc *Huobi) GetResponse(base, quote string) (*Response, *Error) {
return &Response{config.Name, market.Ticker.Close, market.Ticker.Volume}, nil
}

func (exc *Huobi) SetPairs() {
func (exc *Huobi) SetPairs() *Error {
var pairs HuobiPairs
config := exc.GetConfig()
err := HttpGet(config, "/v1/common/symbols", &pairs)
if err != nil {
log.Fatal(err)
return err
}
for _, pair := range pairs.Data {
exc.Pairs = append(exc.Pairs, &Pair{Base: strings.ToUpper(pair.Base), Quote: strings.ToUpper(pair.Quote)})
}
return nil
}

func (exc *Huobi) GetConfig() *Config {
Expand Down
2 changes: 1 addition & 1 deletion exchange/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type Exchange interface {
GetConfig() *Config
GetPairs() []*Pair
GetResponse(base, quote string) (*Response, *Error)
SetPairs()
SetPairs() *Error
}

func GetSupportedExchanges() []Exchange {
Expand Down
6 changes: 3 additions & 3 deletions exchange/zb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package exchange

import (
"fmt"
"log"
"strings"
)

Expand Down Expand Up @@ -31,18 +30,19 @@ func (exc *ZB) GetResponse(base, quote string) (*Response, *Error) {
return &Response{Name: config.Name, Price: ToFloat64(market.Ticker.Last), Volume: volume}, nil
}

func (exc *ZB) SetPairs() {
func (exc *ZB) SetPairs() *Error {
var pairs map[string]interface{}
config := exc.GetConfig()
err := HttpGet(config, "/markets", &pairs)
if err != nil {
log.Fatal(err)
return err
}

for pair := range pairs {
details := strings.Split(pair, "_")
exc.Pairs = append(exc.Pairs, &Pair{Base: strings.ToUpper(details[0]), Quote: strings.ToUpper(details[1])})
}
return nil
}

func (exc *ZB) GetConfig() *Config {
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package main
import (
"fmt"
"github.com/linkpoolio/asset-price-cl-ea/web"
"log"
log "github.com/sirupsen/logrus"
"net/http"
)

func main() {
web.InitialiseConfig()

log.Print("Chainlink Asset Price Adaptor")
log.Print("chainlink asset price adaptor")
log.Printf("Starting to serve on port %d", web.Config.Port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", web.Config.Port), web.Api().MakeHandler()))
}
8 changes: 4 additions & 4 deletions web/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package web

import (
"github.com/ant0ine/go-json-rest/rest"
"log"
log "github.com/sirupsen/logrus"
)

func Api() *rest.Api{
Expand All @@ -15,11 +15,11 @@ func Api() *rest.Api{
log.Fatal(err)
}

log.Print("Starting trading pairs ticker...")
log.Print("starting trading pairs ticker")
StartPairsTicker()
log.Print("Set trading pairs, starting API...")
log.Print("set trading pairs, starting API...")

api.SetApp(router)
log.Print("API started!")
log.Print("api started")
return api
}
10 changes: 8 additions & 2 deletions web/price_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"strconv"
"strings"
"time"
"log"
log "github.com/sirupsen/logrus"
)

type Log struct {
Expand Down Expand Up @@ -180,7 +180,13 @@ func setExchangePairs() {
for _, exc := range exchanges {
go func(exc exchange.Exchange) {
defer wg.Done()
exc.SetPairs()
err := exc.SetPairs()
if err != nil {
log.WithFields(log.Fields{
"exchange": err.Exchange,
"msg": err.Message,
}).Error("error from exchange on setting pairs")
}
}(exc)
}

Expand Down

0 comments on commit 7e181cd

Please sign in to comment.