Skip to content

Commit

Permalink
Link up websocket handler to routes after refactor and various improv…
Browse files Browse the repository at this point in the history
…ements
  • Loading branch information
thrasher- committed Sep 14, 2017
1 parent 0682dce commit 6e9bda8
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 164 deletions.
10 changes: 10 additions & 0 deletions exchanges/stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ func Add(exchange string, p pair.CurrencyPair, assetType string, price, volume f
return
}

if p.FirstCurrency == "XBT" {
newPair := pair.NewCurrencyPair("BTC", p.SecondCurrency.String())
Append(exchange, newPair, assetType, price, volume)
}

if p.SecondCurrency == "USDT" {
newPair := pair.NewCurrencyPair(p.FirstCurrency.String(), "USD")
Append(exchange, newPair, assetType, price, volume)
}

Append(exchange, p, assetType, price, volume)
}

Expand Down
14 changes: 14 additions & 0 deletions exchanges/stats/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@ func TestAdd(t *testing.T) {
if len(Items) != 1 {
t.Error("Test Failed - stats Add did not add exchange info.")
}

p.FirstCurrency = "XBT"
Add("ANX", p, "SPOT", 1201, 43)

if Items[1].Pair.Pair() != "XBTUSD" {
t.Fatal("Test failed. stats Add did not add exchange info.")
}

p = pair.NewCurrencyPair("ETH", "USDT")
Add("ANX", p, "SPOT", 300, 1000)

if Items[2].Pair.Pair() != "ETHUSD" {
t.Fatal("Test failed. stats Add did not add exchange info.")
}
}

func TestAppend(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"errors"
"fmt"

"github.com/thrasher-/gocryptotrader/exchanges/stats"

"github.com/thrasher-/gocryptotrader/currency/pair"
exchange "github.com/thrasher-/gocryptotrader/exchanges"
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-/gocryptotrader/exchanges/stats"
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
)

Expand Down
12 changes: 9 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,15 @@ func SeedExchangeAccountInfo(data []exchange.AccountInfo) {
currencyName)
port.RemoveExchangeAddress(exchangeName, currencyName)
} else {
log.Printf("Portfolio: Updating %s %s entry with balance %f.\n",
exchangeName, currencyName, total)
port.UpdateExchangeAddressBalance(exchangeName, currencyName, total)
balance, ok := port.GetAddressBalance(exchangeName, currencyName, portfolio.PortfolioAddressExchange)
if !ok {
continue
}
if balance != total {
log.Printf("Portfolio: Updating %s %s entry with balance %f.\n",
exchangeName, currencyName, total)
port.UpdateExchangeAddressBalance(exchangeName, currencyName, total)
}
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions portfolio/portfolio.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ func GetCryptoIDAddress(address string, coinType string) (float64, error) {
}

// GetAddressBalance acceses the portfolio base and returns the balance by passed
// in address
func (p *Base) GetAddressBalance(address string) (float64, bool) {
// in address, coin type and description
func (p *Base) GetAddressBalance(address, coinType, description string) (float64, bool) {
for x := range p.Addresses {
if p.Addresses[x].Address == address {
if p.Addresses[x].Address == address &&
p.Addresses[x].Description == description &&
p.Addresses[x].CoinType == coinType {
return p.Addresses[x].Balance, true
}
}
Expand Down
4 changes: 2 additions & 2 deletions portfolio/portfolio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ func TestGetAddressBalance(t *testing.T) {
portfolio := Base{}
portfolio.AddAddress(ltcAddress, ltc, description, balance)

addBalance, _ := portfolio.GetAddressBalance("LdP8Qox1VAhCzLJNqrr74YovaWYyNBUWvL")
addBalance, _ := portfolio.GetAddressBalance("LdP8Qox1VAhCzLJNqrr74YovaWYyNBUWvL", ltc, description)
if addBalance != balance {
t.Error("Test Failed - Portfolio GetAddressBalance() Error: Incorrect value")
}

addBalance, found := portfolio.GetAddressBalance("WigWham")
addBalance, found := portfolio.GetAddressBalance("WigWham", ltc, description)
if addBalance != 0 {
t.Error("Test Failed - Portfolio GetAddressBalance() Error: Incorrect value")
}
Expand Down
22 changes: 12 additions & 10 deletions restful_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ func NewRouter(exchanges []exchange.IBotExchange) *mux.Router {
router := mux.NewRouter().StrictSlash(true)

routes = Routes{
Route{
"",
"GET",
"/",
getIndex,
},
Route{
"GetAllSettings",
"GET",
Expand Down Expand Up @@ -94,6 +100,12 @@ func NewRouter(exchanges []exchange.IBotExchange) *mux.Router {
"/exchanges/{exchangeName}/orderbook/latest/{currency}",
RESTGetOrderbook,
},
Route{
"ws",
"GET",
"/ws",
WebsocketClientHandler,
},
}

for _, route := range routes {
Expand All @@ -114,13 +126,3 @@ func getIndex(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "<html>GoCryptoTrader RESTful interface. For the web GUI, please visit the <a href=https://github.com/thrasher-/gocryptotrader/blob/master/web/README.md>web GUI readme.</a></html>")
w.WriteHeader(http.StatusOK)
}

// IndexRoute maps the index route to the getIndex function
var IndexRoute = Routes{
Route{
"",
"GET",
"/",
getIndex,
},
}
Loading

0 comments on commit 6e9bda8

Please sign in to comment.