Now adds a universal way to retrieve all enabled currencies

New endpoint to retrieve values for all enabled currency data for all enabled exchanges and return it as JSON object for front end
This commit is contained in:
GloriousCode
2016-07-13 21:43:48 +10:00
parent d561bac891
commit 7223875230
24 changed files with 268 additions and 177 deletions

View File

@@ -193,7 +193,12 @@ func (a *ANX) GetTickerPrice(currency string) TickerPrice {
ticker := a.GetTicker(currency)
tickerPrice.Ask = ticker.Data.Buy.Value
tickerPrice.Bid = ticker.Data.Sell.Value
tickerPrice.CryptoCurrency = currency
tickerPrice.Low = ticker.Data.Low.Value
tickerPrice.Last = ticker.Data.Last.Value
tickerPrice.Volume = ticker.Data.Vol.Value
tickerPrice.High = ticker.Data.High.Value
return tickerPrice
}

View File

@@ -3,12 +3,13 @@ package main
import (
"errors"
"fmt"
"github.com/gorilla/websocket"
"log"
"net/url"
"strconv"
"strings"
"time"
"github.com/gorilla/websocket"
)
const (
@@ -211,6 +212,10 @@ func (b *Bitfinex) Setup(exch Exchanges) {
}
}
func (k *Bitfinex) GetEnabledCurrencies() []string {
return k.EnabledPairs
}
func (b *Bitfinex) Start() {
go b.Run()
}
@@ -284,7 +289,6 @@ func (b *Bitfinex) GetTicker(symbol string, values url.Values) (BitfinexTicker,
return response, nil
}
func (b *Bitfinex) GetTickerPrice(currency string) TickerPrice {
var tickerPrice TickerPrice
ticker, err := b.GetTicker(currency, nil)
@@ -294,7 +298,12 @@ func (b *Bitfinex) GetTickerPrice(currency string) TickerPrice {
}
tickerPrice.Ask = ticker.Ask
tickerPrice.Bid = ticker.Bid
tickerPrice.CryptoCurrency = currency
tickerPrice.Low = ticker.Low
tickerPrice.Last = ticker.Last
tickerPrice.Volume = ticker.Volume
tickerPrice.High = ticker.High
return tickerPrice
}

View File

@@ -169,6 +169,10 @@ func (b *Bitstamp) Setup(exch Exchanges) {
}
}
func (k *Bitstamp) GetEnabledCurrencies() []string {
return k.EnabledPairs
}
func (b *Bitstamp) SetEnabled(enabled bool) {
b.Enabled = enabled
}
@@ -243,7 +247,12 @@ func (b *Bitstamp) GetTickerPrice(currency string) TickerPrice {
}
tickerPrice.Ask = ticker.Ask
tickerPrice.Bid = ticker.Bid
tickerPrice.CryptoCurrency = currency
tickerPrice.Low = ticker.Low
tickerPrice.Last = ticker.Last
tickerPrice.Volume = ticker.Volume
tickerPrice.High = ticker.High
return tickerPrice
}

View File

@@ -61,6 +61,10 @@ func (b *BrightonPeak) Setup(exch Exchanges) {
}
}
func (k *BrightonPeak) GetEnabledCurrencies() []string {
return k.EnabledPairs
}
func (b *BrightonPeak) Start() {
go b.Run()
}
@@ -134,7 +138,7 @@ func (b *BrightonPeak) GetTicker(symbol string) (AlphapointTicker, error) {
return b.API.GetTicker(symbol)
}
func (b *BrightonPeak) GetTickerPrice(currency string) TickerPrice {
func (b *BrightonPeak) GetTickerPrice(currency string) TickerPrice {
var tickerPrice TickerPrice
ticker, err := b.GetTicker(currency)
if err != nil {
@@ -143,7 +147,12 @@ func (b *BrightonPeak) GetTickerPrice(currency string) TickerPrice {
}
tickerPrice.Ask = ticker.Ask
tickerPrice.Bid = ticker.Bid
tickerPrice.CryptoCurrency = currency
tickerPrice.Low = ticker.Low
tickerPrice.Last = ticker.Last
tickerPrice.Volume = ticker.Volume
tickerPrice.High = ticker.High
return tickerPrice
}

View File

@@ -198,6 +198,10 @@ func (b *BTCC) Setup(exch Exchanges) {
}
}
func (k *BTCC) GetEnabledCurrencies() []string {
return k.EnabledPairs
}
//Start is run if exchange is enabled, after Setup
func (b *BTCC) Start() {
go b.Run()
@@ -272,12 +276,17 @@ func (b *BTCC) GetTicker(symbol string) BTCCTicker {
return resp.Ticker
}
func (b *BTCC) GetTickerPrice(currency string) TickerPrice {
func (b *BTCC) GetTickerPrice(currency string) TickerPrice {
var tickerPrice TickerPrice
ticker := b.GetTicker(currency)
tickerPrice.Ask = ticker.Sell
tickerPrice.Bid = ticker.Buy
tickerPrice.CryptoCurrency = currency
tickerPrice.Low = ticker.Low
tickerPrice.Last = ticker.Last
tickerPrice.Volume = ticker.Vol
tickerPrice.High = ticker.High
return tickerPrice
}

View File

@@ -116,6 +116,10 @@ func (b *BTCE) Setup(exch Exchanges) {
}
}
func (k *BTCE) GetEnabledCurrencies() []string {
return k.EnabledPairs
}
func (b *BTCE) Start() {
go b.Run()
}

View File

@@ -125,6 +125,10 @@ func (b *BTCMarkets) Setup(exch Exchanges) {
}
}
func (k *BTCMarkets) GetEnabledCurrencies() []string {
return k.EnabledPairs
}
func (b *BTCMarkets) Start() {
go b.Run()
}
@@ -197,7 +201,9 @@ func (b *BTCMarkets) GetTickerPrice(currency string) TickerPrice {
}
tickerPrice.Ask = ticker.BestAsk
tickerPrice.Bid = ticker.BestBID
tickerPrice.CryptoCurrency = currency
tickerPrice.Last = ticker.LastPrice
return tickerPrice
}

View File

@@ -159,6 +159,10 @@ func (c *Coinbase) Setup(exch Exchanges) {
}
}
func (k *Coinbase) GetEnabledCurrencies() []string {
return k.EnabledPairs
}
func (c *Coinbase) Start() {
go c.Run()
}
@@ -367,7 +371,9 @@ func (c *Coinbase) GetTickerPrice(currency string) TickerPrice {
return TickerPrice{}
}
tickerPrice.Ask = ticker.Price
tickerPrice.CryptoCurrency = currency
tickerPrice.Volume = ticker.Size
return tickerPrice
}

View File

@@ -137,6 +137,10 @@ func (g *Gemini) Setup(exch Exchanges) {
}
}
func (k *Gemini) GetEnabledCurrencies() []string {
return k.EnabledPairs
}
func (g *Gemini) Start() {
go g.Run()
}
@@ -189,7 +193,7 @@ func (g *Gemini) Run() {
/// Once GetTicker is created, then add in GetTickerPrice code plz - Scott
func (g *Gemini) GetTickerPrice(currency string) TickerPrice {
var tickerPrice TickerPrice
return tickerPrice
}

View File

@@ -79,6 +79,10 @@ func (h *HUOBI) Setup(exch Exchanges) {
}
}
func (k *HUOBI) GetEnabledCurrencies() []string {
return k.EnabledPairs
}
func (h *HUOBI) Start() {
go h.Run()
}
@@ -137,7 +141,12 @@ func (h *HUOBI) GetTickerPrice(currency string) TickerPrice {
ticker := h.GetTicker(currency)
tickerPrice.Ask = ticker.Sell
tickerPrice.Bid = ticker.Buy
tickerPrice.CryptoCurrency = currency
tickerPrice.Low = ticker.Low
tickerPrice.Last = ticker.Last
tickerPrice.Volume = ticker.Vol
tickerPrice.High = ticker.High
return tickerPrice
}

View File

@@ -6,7 +6,6 @@ type IBotExchange interface {
SetDefaults()
GetName() string
IsEnabled() bool
GetTickerPrice(currency string) TickerPrice
//GetEnabledCurrencies() []string
GetTickerPrice(currency string) TickerPrice
GetEnabledCurrencies() []string
}

View File

@@ -86,6 +86,10 @@ func (i *ItBit) Setup(exch Exchanges) {
}
}
func (k *ItBit) GetEnabledCurrencies() []string {
return k.EnabledPairs
}
func (i *ItBit) Start() {
go i.Run()
}
@@ -140,7 +144,7 @@ func (i *ItBit) GetTickerPrice(currency string) TickerPrice {
tickerPrice.Ask = ticker.Ask
tickerPrice.Bid = ticker.Bid
return tickerPrice
}

View File

@@ -90,6 +90,10 @@ func (k *Kraken) Setup(exch Exchanges) {
}
}
func (k *Kraken) GetEnabledCurrencies() []string {
return k.EnabledPairs
}
func (k *Kraken) Start() {
go k.Run()
}
@@ -233,13 +237,13 @@ func (k *Kraken) GetTicker(symbol string) error {
func (k *Kraken) GetTickerPrice(currency string) TickerPrice {
var tickerPrice TickerPrice
/*
ticker, err := i.GetTicker(currency)
if err != nil {
log.Println(err)
return tickerPrice
}
tickerPrice.Ask = ticker.Ask
tickerPrice.Bid = ticker.Bid
ticker, err := i.GetTicker(currency)
if err != nil {
log.Println(err)
return tickerPrice
}
tickerPrice.Ask = ticker.Ask
tickerPrice.Bid = ticker.Bid
*/
return tickerPrice
}

View File

@@ -95,6 +95,10 @@ func (l *LakeBTC) Setup(exch Exchanges) {
}
}
func (k *LakeBTC) GetEnabledCurrencies() []string {
return k.EnabledPairs
}
func (l *LakeBTC) Start() {
go l.Run()
}
@@ -151,15 +155,17 @@ func (l *LakeBTC) GetTicker() LakeBTCTickerResponse {
func (l *LakeBTC) GetTickerPrice(currency string) TickerPrice {
var tickerPrice TickerPrice
ticker := l.GetTicker()
if(currency == "USD") {
if currency == "USD" {
tickerPrice.Ask = ticker.USD.Ask
tickerPrice.Bid = ticker.USD.Bid
} else if(currency =="CNY") {
} else if currency == "CNY" {
tickerPrice.Ask = ticker.CNY.Ask
tickerPrice.Bid = ticker.CNY.Bid
}
tickerPrice.CryptoCurrency = currency
return tickerPrice
}

View File

@@ -74,6 +74,10 @@ func (l *LocalBitcoins) Setup(exch Exchanges) {
}
}
func (k *LocalBitcoins) GetEnabledCurrencies() []string {
return k.EnabledPairs
}
func (l *LocalBitcoins) Start() {
go l.Run()
}
@@ -144,7 +148,8 @@ func (l *LocalBitcoins) GetTickerPrice(currency string) TickerPrice {
return tickerPrice
}
tickerPrice.Ask = ticker[currency].Rates.Last
tickerPrice.CryptoCurrency = currency
return tickerPrice
}

54
main.go
View File

@@ -3,12 +3,12 @@ package main
import (
"errors"
"log"
"net/http"
"os"
"os/signal"
"runtime"
"strconv"
"syscall"
"net/http"
)
type Exchange struct {
@@ -41,8 +41,9 @@ type Bot struct {
var bot Bot
func SetupBotConfiguration(s IBotExchange, exch Exchanges) {
s.Setup(exch)
s.SetDefaults()
if s.GetName() == exch.Name {
s.Setup(exch)
if s.IsEnabled() {
log.Printf("%s: Exchange support: %s (Authenticated API support: %s - Verbose mode: %s).\n", exch.Name, IsEnabled(exch.Enabled), IsEnabled(exch.AuthenticatedAPISupport), IsEnabled(exch.Verbose))
s.Start()
@@ -85,8 +86,6 @@ func main() {
log.Println("SMS support disabled.")
}
log.Printf("Available Exchanges: %d. Enabled Exchanges: %d.\n", len(bot.config.Exchanges), GetEnabledExchanges())
log.Println("Bot Exchange support:")
@@ -94,25 +93,24 @@ func main() {
bot.exchange.okcoinChina.APIUrl = OKCOIN_API_URL_CHINA
bot.exchanges = []IBotExchange{
&bot.exchange.anx,
&bot.exchange.kraken,
&bot.exchange.btcc,
&bot.exchange.bitstamp,
&bot.exchange.brightonpeak,
&bot.exchange.bitfinex,
&bot.exchange.btce,
&bot.exchange.btcmarkets,
&bot.exchange.coinbase,
&bot.exchange.gemini,
&bot.exchange.okcoinChina,
&bot.exchange.okcoinIntl,
&bot.exchange.itbit,
&bot.exchange.lakebtc,
&bot.exchange.localbitcoins,
&bot.exchange.poloniex,
&bot.exchange.huobi,
new(ANX),
new(Kraken),
new(BTCC),
new(Bitstamp),
new(BrightonPeak),
new(Bitfinex),
new(BTCE),
new(BTCMarkets),
new(Coinbase),
new(Gemini),
new(OKCoin),
new(OKCoin),
new(ItBit),
new(LakeBTC),
new(LocalBitcoins),
new(Poloniex),
new(HUOBI),
}
for i := 0; i < len(bot.exchanges); i++ {
if bot.exchanges[i] != nil {
bot.exchanges[i].SetDefaults()
@@ -133,22 +131,22 @@ func main() {
}
}
}
if bot.config.Webserver.Enabled {
if bot.config.Webserver.Enabled {
err := CheckWebserverValues()
if err != nil {
log.Println(err) // non fatal event
bot.config.Webserver.Enabled = false
//bot.config.Webserver.Enabled = false
} else {
log.Println("HTTP Webserver support enabled.")
router := NewRouter(bot.exchanges)
log.Fatal(http.ListenAndServe(bot.config.Webserver.ListenAddress, router))
router := NewRouter(bot.exchanges)
log.Fatal(http.ListenAndServe(bot.config.Webserver.ListenAddress, router))
}
}
if !bot.config.Webserver.Enabled {
log.Println("HTTP Webserver support disabled.")
}
<-bot.shutdown
Shutdown()
}

View File

@@ -2,12 +2,13 @@ package main
import (
"errors"
"github.com/gorilla/websocket"
"log"
"net/url"
"strconv"
"strings"
"time"
"github.com/gorilla/websocket"
)
const (
@@ -229,6 +230,10 @@ func (o *OKCoin) Setup(exch Exchanges) {
}
}
func (k *OKCoin) GetEnabledCurrencies() []string {
return k.EnabledPairs
}
func (o *OKCoin) Start() {
go o.Run()
}
@@ -322,7 +327,7 @@ func (o *OKCoin) GetTicker(symbol string) (OKCoinTicker, error) {
return resp.Ticker, nil
}
func (o *OKCoin) GetTickerPrice(currency string) TickerPrice {
func (o *OKCoin) GetTickerPrice(currency string) TickerPrice {
var tickerPrice TickerPrice
ticker, err := o.GetTicker(currency)
if err != nil {
@@ -331,7 +336,12 @@ func (o *OKCoin) GetTickerPrice(currency string) TickerPrice {
}
tickerPrice.Ask = ticker.Sell
tickerPrice.Bid = ticker.Buy
tickerPrice.CryptoCurrency = currency
tickerPrice.Low = ticker.Low
tickerPrice.Last = ticker.Last
tickerPrice.Volume = ticker.Vol
tickerPrice.High = ticker.High
return tickerPrice
}

View File

@@ -105,6 +105,10 @@ func (p *Poloniex) Setup(exch Exchanges) {
}
}
func (k *Poloniex) GetEnabledCurrencies() []string {
return k.EnabledPairs
}
func (p *Poloniex) Start() {
go p.Run()
}
@@ -161,16 +165,21 @@ func (p *Poloniex) GetTicker() (map[string]PoloniexTicker, error) {
return resp.Data, nil
}
func (p *Poloniex) GetTickerPrice(currency string) TickerPrice {
func (p *Poloniex) GetTickerPrice(currency string) TickerPrice {
var tickerPrice TickerPrice
ticker, err := p.GetTicker()
if err != nil {
log.Println(err)
return tickerPrice
}
tickerPrice.CryptoCurrency = currency
tickerPrice.Ask = ticker[currency].Last
tickerPrice.Bid = ticker[currency].HighestBid
tickerPrice.High = ticker[currency].HighestBid
tickerPrice.Last = ticker[currency].Last
tickerPrice.Low = ticker[currency].LowestAsk
tickerPrice.Volume = ticker[currency].BaseVolume
return tickerPrice
}

View File

@@ -1,9 +1,10 @@
package main
import (
"gopkg.in/jcelliott/turnpike.v2"
"log"
"strconv"
"gopkg.in/jcelliott/turnpike.v2"
)
const (
@@ -132,7 +133,7 @@ func PoloniexOnDepthOrTrade(args []interface{}, kwargs map[string]interface{}) {
func (p *Poloniex) WebsocketClient() {
for p.Enabled && p.Websocket {
c, err := turnpike.NewWebsocketClient(turnpike.JSON, POLONIEX_WEBSOCKET_ADDRESS)
c, err := turnpike.NewWebsocketClient(turnpike.JSON, POLONIEX_WEBSOCKET_ADDRESS, nil)
if err != nil {
log.Printf("%s Unable to connect to Websocket. Error: %s\n", p.GetName(), err)
continue

View File

@@ -1,34 +1,11 @@
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/gorilla/mux"
"fmt"
"net/http"
)
func Index(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w,bot.exchanges[0].GetName())
fmt.Fprintln(w, bot.exchanges[0].GetName())
}
func TodoIndex(w http.ResponseWriter, r *http.Request) {
todos := Todos{
Todo{Name: "Write presentation"},
Todo{Name: "Host meetup"},
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(todos); err != nil {
panic(err)
}
}
func TodoShow(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
todoId := vars["todoId"]
fmt.Fprintln(w, "Todo show:", todoId)
}

View File

@@ -1,24 +1,25 @@
package main
import (
"net/http"
"github.com/gorilla/mux"
"net/http"
"github.com/gorilla/mux"
)
func NewRouter(exchanges []IBotExchange) *mux.Router {
router := mux.NewRouter().StrictSlash(true)
allRoutes := append(routes,exchangeRoutes...)
for _, route := range allRoutes {
var handler http.Handler
handler = route.HandlerFunc
handler = Logger(handler, route.Name)
router := mux.NewRouter().StrictSlash(true)
allRoutes := append(routes, exchangeRoutes...)
for _, route := range allRoutes {
var handler http.Handler
handler = route.HandlerFunc
handler = Logger(handler, route.Name)
router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(handler)
router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(handler)
}
return router
}
}
return router
}

View File

@@ -1,11 +0,0 @@
package main
import "time"
type Todo struct {
Name string `json:"name"`
Completed bool `json:"completed"`
Due time.Time `json:"due"`
}
type Todos []Todo

View File

@@ -5,14 +5,14 @@ import (
)
type TickerPrice struct {
CryptoCurrency string
FiatCurrency string
Last float64
High float64
Low float64
Bid float64
Ask float64
Volume float64
CryptoCurrency string `json:"CryptoCurrency"`
FiatCurrency string `json:"FiatCurrency"`
Last float64 `json:"Last"`
High float64 `json:"High"`
Low float64 `json:"Low"`
Bid float64 `json:"Bid"`
Ask float64 `json:"Ask"`
Volume float64 `json:"Volume"`
}
type Ticker struct {

View File

@@ -1,66 +1,84 @@
package main
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
"encoding/json"
"log"
"net/http"
"github.com/gorilla/mux"
)
func jsonTickerResponse(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
currency := vars["currency"]
exchangeName := vars["exchangeName"]
var response TickerPrice;
for i := 0; i < len(bot.exchanges); i++ {
if bot.exchanges[i] != nil {
if(bot.exchanges[i].IsEnabled() && bot.exchanges[i].GetName() == exchangeName) {
response = bot.exchanges[i].GetTickerPrice(currency)
}
}
}
vars := mux.Vars(r)
currency := vars["currency"]
exchangeName := vars["exchangeName"]
var response TickerPrice
for i := 0; i < len(bot.exchanges); i++ {
if bot.exchanges[i] != nil {
if bot.exchanges[i].IsEnabled() && bot.exchanges[i].GetName() == exchangeName {
response = bot.exchanges[i].GetTickerPrice(currency)
}
}
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(response); err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
encoder := json.NewEncoder(w)
if err := encoder.Encode(response); err != nil {
panic(err)
}
}
type AllEnabledExchangeCurrencies struct {
Data []EnabledExchangeCurrencies `json:"data"`
}
type EnabledExchangeCurrencies struct {
ExchangeName string `json:"exchangeName"`
ExchangeValues []TickerPrice `json:"exchangeValues"`
}
func getAllActiveTickersResponse(w http.ResponseWriter, r *http.Request) {
var response[] TickerPrice
//bot.config.Cryptocurrencies
for i := 0; i < len(bot.exchanges); i++ {
if bot.exchanges[i] != nil {
if(bot.exchanges[i].IsEnabled()) {
for _, exch := range bot.config.Exchanges {
if(bot.exchanges[i].GetName() == exch.Name) {
for _, enabledPair := range exch.BaseCurrencies {
response = append(response, bot.exchanges[i].GetTickerPrice(enabledPair))
}
}
}
}
}
}
var response AllEnabledExchangeCurrencies
log.Println(bot.exchanges[15])
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(response); err != nil {
panic(err)
}
for _, individualBot := range bot.exchanges {
if individualBot != nil && individualBot.IsEnabled() {
var individualExchange EnabledExchangeCurrencies
individualExchange.ExchangeName = individualBot.GetName()
log.Println("Getting enabled currencies for '" + individualBot.GetName() + "'")
currencies := individualBot.GetEnabledCurrencies()
log.Println(currencies)
for _, currency := range currencies {
tickerPrice := individualBot.GetTickerPrice(currency)
log.Println(tickerPrice)
individualExchange.ExchangeValues = append(individualExchange.ExchangeValues, tickerPrice)
}
response.Data = append(response.Data, individualExchange)
}
}
log.Println(response)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(response); err != nil {
panic(err)
}
}
var exchangeRoutes = Routes{
Route{
"AllActiveExchangesAndCurrencies",
"GET",
"/exchanges/enabled/latest/all",
getAllActiveTickersResponse,
},
Route{
"IndividualExchangeAndCurrency",
"GET",
"/exchanges/{exchangeName}/latest/{currency}",
jsonTickerResponse,
},
}
Route{
"AllActiveExchangesAndCurrencies",
"GET",
"/exchanges/enabled/latest/all",
getAllActiveTickersResponse,
},
Route{
"IndividualExchangeAndCurrency",
"GET",
"/exchanges/{exchangeName}/latest/{currency}",
jsonTickerResponse,
},
}