forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet_routes.go
77 lines (68 loc) · 2.23 KB
/
wallet_routes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"encoding/json"
"errors"
"log"
"net/http"
"github.com/thrasher-/gocryptotrader/exchanges"
)
type AllEnabledExchangeAccounts struct {
Data []exchange.ExchangeAccountInfo `json:"data"`
}
func GetCollatedExchangeAccountInfoByCoin(accounts []exchange.ExchangeAccountInfo) map[string]exchange.ExchangeAccountCurrencyInfo {
result := make(map[string]exchange.ExchangeAccountCurrencyInfo)
for i := 0; i < len(accounts); i++ {
for j := 0; j < len(accounts[i].Currencies); j++ {
currencyName := accounts[i].Currencies[j].CurrencyName
avail := accounts[i].Currencies[j].TotalValue
onHold := accounts[i].Currencies[j].Hold
info, ok := result[currencyName]
if !ok {
accountInfo := exchange.ExchangeAccountCurrencyInfo{CurrencyName: currencyName, Hold: onHold, TotalValue: avail}
result[currencyName] = accountInfo
} else {
info.Hold += onHold
info.TotalValue += avail
result[currencyName] = info
}
}
}
return result
}
func GetAccountCurrencyInfoByExchangeName(accounts []exchange.ExchangeAccountInfo, exchangeName string) (exchange.ExchangeAccountInfo, error) {
for i := 0; i < len(accounts); i++ {
if accounts[i].ExchangeName == exchangeName {
return accounts[i], nil
}
}
return exchange.ExchangeAccountInfo{}, errors.New(exchange.ErrExchangeNotFound)
}
func GetAllEnabledExchangeAccountInfo() AllEnabledExchangeAccounts {
var response AllEnabledExchangeAccounts
for _, individualBot := range bot.exchanges {
if individualBot != nil && individualBot.IsEnabled() {
individualExchange, err := individualBot.GetExchangeAccountInfo()
if err != nil {
log.Println("Error encountered retrieving exchange account for '" + individualExchange.ExchangeName + "'")
}
response.Data = append(response.Data, individualExchange)
}
}
return response
}
func SendAllEnabledAccountInfo(w http.ResponseWriter, r *http.Request) {
response := GetAllEnabledExchangeAccountInfo()
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 WalletRoutes = Routes{
Route{
"AllEnabledAccountInfo",
"GET",
"/exchanges/enabled/accounts/all",
SendAllEnabledAccountInfo,
},
}