Added verbosity variable in config to toggle Exchange output.

This commit is contained in:
Adrian Gallagher
2015-02-26 15:26:41 +11:00
parent 92abe3bd95
commit 749dd07453
12 changed files with 197 additions and 41 deletions

View File

@@ -81,6 +81,7 @@ type SymbolsDetails struct {
type Bitfinex struct {
Name string
Enabled bool
Verbose bool
APIKey, APISecret string
Ticker BitfinexTicker
Stats []BitfinexStats
@@ -93,6 +94,7 @@ type Bitfinex struct {
func (b *Bitfinex) SetDefaults() {
b.Name = "Bitfinex"
b.Enabled = true
b.Verbose = false
}
func (b *Bitfinex) GetName() (string) {
@@ -169,15 +171,16 @@ func (b *Bitfinex) SendAuthenticatedHTTPRequest(path string, params map[string]i
}
PayloadJson, err := json.Marshal(request)
log.Printf("Request JSON: %s\n", PayloadJson)
if b.Verbose {
log.Printf("Request JSON: %s\n", PayloadJson)
}
if err != nil {
return errors.New("SendAuthenticatedHTTPRequest: Unable to JSON request")
}
PayloadBase64 := base64.StdEncoding.EncodeToString(PayloadJson)
log.Printf("Base64: %s\n", PayloadBase64)
hmac := hmac.New(sha512.New384, []byte(b.APISecret))
hmac.Write([]byte(PayloadBase64))
signature := hex.EncodeToString(hmac.Sum(nil))
@@ -200,7 +203,11 @@ func (b *Bitfinex) SendAuthenticatedHTTPRequest(path string, params map[string]i
}
contents, _ := ioutil.ReadAll(resp.Body)
log.Printf("Recieved raw: \n%s\n", string(contents))
if b.Verbose {
log.Printf("Recieved raw: \n%s\n", string(contents))
}
err = json.Unmarshal(contents, &result)
if err != nil {

View File

@@ -39,6 +39,7 @@ const (
type Bitstamp struct {
Name string
Enabled bool
Verbose bool
ClientID, APIKey, APISecret string
Ticker BitstampTicker
Orderbook Orderbook
@@ -87,6 +88,7 @@ type ConversionRate struct {
func (b *Bitstamp) SetDefaults() {
b.Name = "Bitstamp"
b.Enabled = true
b.Verbose = false
}
func (b *Bitstamp) GetName() (string) {
@@ -276,7 +278,10 @@ func (b *Bitstamp) SendAuthenticatedHTTPRequest(path string, values url.Values,
reqBody := strings.NewReader(values.Encode())
path = BITSTAMP_API_URL + path
fmt.Println("Sending POST request to " + path)
if b.Verbose {
fmt.Println("Sending POST request to " + path)
}
req, err := http.NewRequest("POST", path, reqBody)
if err != nil {
@@ -293,7 +298,10 @@ func (b *Bitstamp) SendAuthenticatedHTTPRequest(path string, values url.Values,
}
contents, _ := ioutil.ReadAll(resp.Body)
fmt.Printf("Recieved raw: %s\n", string(contents))
if b.Verbose {
fmt.Printf("Recieved raw: %s\n", string(contents))
}
err = json.Unmarshal(contents, &result)

View File

@@ -24,6 +24,7 @@ const (
type BTCChina struct {
Name string
Enabled bool
Verbose bool
APISecret, APIKey string
Fee float64
}
@@ -45,6 +46,7 @@ func (b *BTCChina) SetDefaults() {
b.Name = "BTC China"
b.Enabled = true
b.Fee = 0
b.Verbose = false
}
func (b *BTCChina) GetName() (string) {
@@ -161,7 +163,9 @@ func (b *BTCChina) SendAuthenticatedHTTPRequest(method string, params []string)
encoded := fmt.Sprintf("tonce=%s&accesskey=%s&requestmethod=post&id=%d&method=%s&params=%s", nonce, b.APIKey, 1, method, params)
fmt.Println(encoded)
if b.Verbose {
log.Println(encoded)
}
hmac := hmac.New(sha1.New, []byte(b.APISecret))
hmac.Write([]byte(encoded))
@@ -171,18 +175,21 @@ func (b *BTCChina) SendAuthenticatedHTTPRequest(method string, params []string)
postData["method"] = method
postData["params"] = []string{}
postData["id"] = 1
data, err := json.Marshal(postData)
fmt.Println(string(data))
if b.Verbose {
log.Println(string(data))
}
if err != nil {
return errors.New("Unable to JSON POST data")
}
log.Printf("Sending POST request to %s calling method %s with params %s\n", "https://api.btcchina.com/api_trade_v1.php", method, data)
reqBody := strings.NewReader(string(data))
if b.Verbose {
log.Printf("Sending POST request to %s calling method %s with params %s\n", "https://api.btcchina.com/api_trade_v1.php", method, data)
}
reqBody := strings.NewReader(string(data))
b64 := base64.StdEncoding.EncodeToString([]byte(b.APIKey + ":" + hash))
req, err := http.NewRequest("POST", "https://api.btcchina.com/api_trade_v1.php", reqBody)
@@ -203,7 +210,11 @@ func (b *BTCChina) SendAuthenticatedHTTPRequest(method string, params []string)
}
contents, _ := ioutil.ReadAll(resp.Body)
log.Printf("Recv'd :%s", string(contents))
if b.Verbose {
log.Printf("Recv'd :%s\n", string(contents))
}
resp.Body.Close()
return nil

View File

@@ -27,6 +27,7 @@ const (
type BTCE struct {
Name string
Enabled bool
Verbose bool
APIKey, APISecret string
Fee float64
}
@@ -48,6 +49,7 @@ func (b *BTCE) SetDefaults() {
b.Name = "BTCE"
b.Enabled = true
b.Fee = 0.2
b.Verbose = false
}
func (b *BTCE) GetName() (string) {
@@ -175,10 +177,11 @@ func (b *BTCE) SendAuthenticatedHTTPRequest(method string, values url.Values) (e
encoded := values.Encode()
hmac.Write([]byte(encoded))
if b.Verbose {
fmt.Printf("Sending POST request to %s calling method %s with params %s\n", BTCE_API_URL, method, encoded)
}
fmt.Printf("Sending POST request to %s calling method %s with params %s\n", BTCE_API_URL, method, encoded)
reqBody := strings.NewReader(encoded)
req, err := http.NewRequest("POST", BTCE_API_URL, reqBody)
if err != nil {
@@ -197,7 +200,11 @@ func (b *BTCE) SendAuthenticatedHTTPRequest(method string, values url.Values) (e
}
contents, _ := ioutil.ReadAll(resp.Body)
fmt.Printf("Recieved raw: %s\n", string(contents))
if b.Verbose {
fmt.Printf("Recieved raw: %s\n", string(contents))
}
resp.Body.Close()
return nil

View File

@@ -21,6 +21,7 @@ const (
type BTCMarkets struct {
Name string
Enabled bool
Verbose bool
Fee float64
APIKey, APISecret string
}
@@ -38,6 +39,7 @@ func (b *BTCMarkets) SetDefaults() {
b.Name = "BTC Markets"
b.Enabled = true
b.Fee = 0.85
b.Verbose = false
}
func (b *BTCMarkets) GetName() (string) {
@@ -105,7 +107,11 @@ func (b *BTCMarkets) SendAuthenticatedRequest(reqType, path, data string) (error
hmac := hmac.New(sha512.New, []byte(b.APISecret))
hmac.Write([]byte(request))
log.Printf("Sending %s request to %s path %s with params %s\n", reqType, BTCMARKETS_API_URL + path, path, request)
if b.Verbose {
log.Printf("Sending %s request to %s path %s with params %s\n", reqType, BTCMARKETS_API_URL + path, path, request)
}
req, err := http.NewRequest(reqType, BTCMARKETS_API_URL + path, strings.NewReader(""))
if err != nil {
@@ -130,7 +136,11 @@ func (b *BTCMarkets) SendAuthenticatedRequest(reqType, path, data string) (error
}
contents, _ := ioutil.ReadAll(resp.Body)
log.Printf("Recieved raw: %s\n", string(contents))
if b.Verbose {
log.Printf("Recieved raw: %s\n", string(contents))
}
resp.Body.Close()
return nil
}

View File

@@ -12,6 +12,7 @@ type Config struct {
type Exchanges struct {
Name string
Enabled bool
Verbose bool
APIKey string
APISecret string
ClientID string

View File

@@ -6,7 +6,8 @@
"APIKey": "Key",
"APISecret": "Secret",
"BaseCurrencies": "USD",
"Enabled": true
"Enabled": true,
"Verbose": false
},
{
"Name": "Bitstamp",
@@ -15,7 +16,8 @@
"APIKey": "Key",
"APISecret": "Secret",
"BaseCurrencies": "USD",
"Enabled": true
"Enabled": true,
"Verbose": false
},
{
"Name": "BTC China",
@@ -23,7 +25,8 @@
"APIKey": "Key",
"APISecret": "Secret",
"BaseCurrencies": "CNY",
"Enabled": true
"Enabled": true,
"Verbose": false
},
{
"Name": "BTCE",
@@ -31,7 +34,8 @@
"APIKey": "Key",
"APISecret": "Secret",
"BaseCurrencies": "USD,RUB,EUR,CNY,GBP",
"Enabled": true
"Enabled": true,
"Verbose": false
},
{
"Name": "BTC Markets",
@@ -39,7 +43,8 @@
"APIKey": "Key",
"APISecret": "Secret",
"BaseCurrencies": "AUD",
"Enabled": true
"Enabled": true,
"Verbose": false
},
{
"Name": "Huobi",
@@ -47,7 +52,8 @@
"APIKey": "Key",
"APISecret": "Secret",
"BaseCurrencies": "CNY",
"Enabled": true
"Enabled": true,
"Verbose": false
},
{
"Name": "ITBIT",
@@ -55,7 +61,8 @@
"APIKey": "Key",
"APISecret": "Secret",
"BaseCurrencies": "USD,SGD,EUR",
"Enabled": true
"Enabled": true,
"Verbose": false
},
{
"Name": "LakeBTC",
@@ -63,7 +70,8 @@
"APIKey": "Key",
"APISecret": "Secret",
"BaseCurrencies": "CNY",
"Enabled": true
"Enabled": true,
"Verbose": false
},
{
"Name": "OKCOIN China",
@@ -71,7 +79,8 @@
"APIKey": "Key",
"APISecret": "Secret",
"BaseCurrencies": "CNY",
"Enabled": true
"Enabled": true,
"Verbose": false
},
{
"Name": "OKCOIN International",
@@ -79,7 +88,8 @@
"APIKey": "Key",
"APISecret": "Secret",
"BaseCurrencies": "USD",
"Enabled": true
"Enabled": true,
"Verbose": false
}
]
}

View File

@@ -20,6 +20,7 @@ const (
type HUOBI struct {
Name string
Enabled bool
Verbose bool
AccessKey, SecretKey string
Fee float64
}
@@ -42,6 +43,7 @@ func (h *HUOBI) SetDefaults() {
h.Name = "Huobi"
h.Enabled = true
h.Fee = 0
h.Verbose = false
}
func (h *HUOBI) GetName() (string) {
@@ -201,11 +203,12 @@ func (h *HUOBI) SendAuthenticatedRequest(method string, v url.Values) (error) {
hasher.Write([]byte(v.Encode() + "&secret_key=" + h.SecretKey))
signature := strings.ToUpper(hex.EncodeToString(hasher.Sum(nil)))
v.Set("sign", signature)
encoded := v.Encode()
fmt.Printf("Signature: %s\n", signature)
fmt.Printf("Sending POST request to %s with params %s\n", HUOBI_API_URL, encoded)
if h.Verbose {
fmt.Printf("Signature: %s\n", signature)
fmt.Printf("Sending POST request to %s with params %s\n", HUOBI_API_URL, encoded)
}
reqBody := strings.NewReader(encoded)
req, err := http.NewRequest("POST", HUOBI_API_URL, reqBody)
@@ -224,7 +227,11 @@ func (h *HUOBI) SendAuthenticatedRequest(method string, v url.Values) (error) {
}
contents, _ := ioutil.ReadAll(resp.Body)
fmt.Printf("Recieved raw: %s\n", string(contents))
if h.Verbose {
fmt.Printf("Recieved raw: %s\n", string(contents))
}
resp.Body.Close()
return nil
}

View File

@@ -23,6 +23,7 @@ const (
type ItBit struct {
Name string
Enabled bool
Verbose bool
ClientKey, APISecret, UserID string
MakerFee, TakerFee float64
}
@@ -52,6 +53,7 @@ func (i *ItBit) SetDefaults() {
i.Enabled = true
i.MakerFee = -0.10
i.TakerFee = 0.50
i.Verbose = false
}
func (i *ItBit) GetName() (string) {
@@ -260,8 +262,11 @@ func (i *ItBit) SendAuthenticatedHTTPRequest(method string, path string, params
}
}
PayloadJson, err := json.Marshal(request)
log.Printf("Request JSON: %s\n", PayloadJson)
PayloadJson, err := json.Marshal(request)
if i.Verbose {
log.Printf("Request JSON: %s\n", PayloadJson)
}
if err != nil {
return errors.New("SendAuthenticatedHTTPRequest: Unable to JSON request")
@@ -286,7 +291,11 @@ func (i *ItBit) SendAuthenticatedHTTPRequest(method string, path string, params
}
contents, _ := ioutil.ReadAll(resp.Body)
log.Printf("Recieved raw: %s\n", string(contents))
if i.Verbose {
log.Printf("Recieved raw: %s\n", string(contents))
}
resp.Body.Close()
return nil
}

View File

@@ -32,6 +32,7 @@ const (
type LakeBTC struct {
Name string
Enabled bool
Verbose bool
Email, APISecret string
TakerFee, MakerFee float64
}
@@ -54,6 +55,7 @@ func (l *LakeBTC) SetDefaults() {
l.Enabled = true
l.TakerFee = 0.2
l.MakerFee = 0.15
l.Verbose = false
}
func (l *LakeBTC) GetName() (string) {
@@ -179,8 +181,10 @@ func (l *LakeBTC) SendAuthenticatedHTTPRequest(method, params string) (err error
encoded := v.Encode()
hmac.Write([]byte(encoded))
if l.Verbose {
fmt.Printf("Sending POST request to %s calling method %s with params %s\n", LAKEBTC_API_URL, method, encoded)
}
fmt.Printf("Sending POST request to %s calling method %s with params %s\n", LAKEBTC_API_URL, method, encoded)
reqBody := strings.NewReader(encoded)
hash := hex.EncodeToString(hmac.Sum(nil))
b64 := base64.StdEncoding.EncodeToString([]byte(l.Email + ":" + hash))
@@ -203,7 +207,11 @@ func (l *LakeBTC) SendAuthenticatedHTTPRequest(method, params string) (err error
}
contents, _ := ioutil.ReadAll(resp.Body)
fmt.Printf("Recieved raw: %s\n", string(contents))
if l.Verbose {
fmt.Printf("Recieved raw: %s\n", string(contents))
}
resp.Body.Close()
return nil

70
main.go
View File

@@ -69,6 +69,13 @@ func main() {
} else {
log.Printf("%s enabled.\n", exch.Name)
exchange.btcchina.SetAPIKeys(exch.APIKey, exch.APISecret)
if exch.Verbose {
exchange.btcchina.Verbose = true
log.Printf("%s Verbose output enabled.\n", exch.Name)
} else {
log.Printf("%s Verbose output disabled.\n", exch.Name)
}
}
} else if exchange.bitstamp.GetName() == exch.Name {
if !exch.Enabled {
@@ -78,6 +85,13 @@ func main() {
log.Printf("%s enabled.\n", exch.Name)
exchange.bitstamp.SetAPIKeys(exch.ClientID, exch.APIKey, exch.APISecret)
exchange.bitstamp.GetBalance()
if exch.Verbose {
exchange.bitstamp.Verbose = true
log.Printf("%s Verbose output enabled.\n", exch.Name)
} else {
log.Printf("%s Verbose output disabled.\n", exch.Name)
}
}
} else if exchange.bitfinex.GetName() == exch.Name {
if !exch.Enabled {
@@ -87,6 +101,13 @@ func main() {
log.Printf("%s enabled.\n", exch.Name)
exchange.bitfinex.SetAPIKeys(exch.APIKey, exch.APISecret)
exchange.bitfinex.GetAccountFeeInfo()
if exch.Verbose {
exchange.bitfinex.Verbose = true
log.Printf("%s Verbose output enabled.\n", exch.Name)
} else {
log.Printf("%s Verbose output disabled.\n", exch.Name)
}
}
} else if exchange.btce.GetName() == exch.Name {
if !exch.Enabled {
@@ -95,6 +116,13 @@ func main() {
} else {
log.Printf("%s enabled.\n", exch.Name)
exchange.btce.SetAPIKeys(exch.APIKey, exch.APISecret)
if exch.Verbose {
exchange.btce.Verbose = true
log.Printf("%s Verbose output enabled.\n", exch.Name)
} else {
log.Printf("%s Verbose output disabled.\n", exch.Name)
}
}
} else if exchange.btcmarkets.GetName() == exch.Name {
if !exch.Enabled {
@@ -103,6 +131,13 @@ func main() {
} else {
log.Printf("%s enabled.\n", exch.Name)
exchange.btcmarkets.SetAPIKeys(exch.APIKey, exch.APISecret)
if exch.Verbose {
exchange.btcmarkets.Verbose = true
log.Printf("%s Verbose output enabled.\n", exch.Name)
} else {
log.Printf("%s Verbose output disabled.\n", exch.Name)
}
}
} else if exchange.okcoinChina.GetName() == exch.Name {
if !exch.Enabled {
@@ -111,6 +146,13 @@ func main() {
} else {
log.Printf("%s enabled.\n", exch.Name)
exchange.okcoinChina.SetAPIKeys(exch.APIKey, exch.APISecret)
if exch.Verbose {
exchange.okcoinChina.Verbose = true
log.Printf("%s Verbose output enabled.\n", exch.Name)
} else {
log.Printf("%s Verbose output disabled.\n", exch.Name)
}
}
} else if exchange.okcoinIntl.GetName() == exch.Name {
if !exch.Enabled {
@@ -119,6 +161,13 @@ func main() {
} else {
log.Printf("%s enabled.\n", exch.Name)
exchange.okcoinIntl.SetAPIKeys(exch.APIKey, exch.APISecret)
if exch.Verbose {
exchange.okcoinIntl.Verbose = true
log.Printf("%s Verbose output enabled.\n", exch.Name)
} else {
log.Printf("%s Verbose output disabled.\n", exch.Name)
}
}
} else if exchange.itbit.GetName() == exch.Name {
if !exch.Enabled {
@@ -127,6 +176,13 @@ func main() {
} else {
log.Printf("%s enabled.\n", exch.Name)
exchange.itbit.SetAPIKeys(exch.APIKey, exch.APISecret)
if exch.Verbose {
exchange.itbit.Verbose = true
log.Printf("%s Verbose output enabled.\n", exch.Name)
} else {
log.Printf("%s Verbose output disabled.\n", exch.Name)
}
}
} else if exchange.lakebtc.GetName() == exch.Name {
if !exch.Enabled {
@@ -135,6 +191,13 @@ func main() {
} else {
log.Printf("%s enabled.\n", exch.Name)
exchange.lakebtc.SetAPIKeys(exch.APIKey, exch.APISecret)
if exch.Verbose {
exchange.lakebtc.Verbose = true
log.Printf("%s Verbose output enabled.\n", exch.Name)
} else {
log.Printf("%s Verbose output disabled.\n", exch.Name)
}
}
} else if exchange.huobi.GetName() == exch.Name {
if !exch.Enabled {
@@ -143,6 +206,13 @@ func main() {
} else {
log.Printf("%s enabled.\n", exch.Name)
exchange.huobi.SetAPIKeys(exch.APIKey, exch.APISecret)
if exch.Verbose {
exchange.huobi.Verbose = true
log.Printf("%s Verbose output enabled.\n", exch.Name)
} else {
log.Printf("%s Verbose output disabled.\n", exch.Name)
}
}
}
}

View File

@@ -20,6 +20,7 @@ const (
type OKCoin struct {
Name string
Enabled bool
Verbose bool
APIUrl, PartnerID, SecretKey string
TakerFee, MakerFee float64
}
@@ -60,6 +61,7 @@ func (o *OKCoin) SetDefaults() {
o.Name = "OKCOIN China"
}
o.Enabled = true
o.Verbose = false
}
func (o *OKCoin) GetName() (string) {
@@ -410,10 +412,12 @@ func (o *OKCoin) SendAuthenticatedHTTPRequest(method string, v url.Values) (err
v.Set("sign", signature)
encoded := v.Encode() + "&partner=" + o.PartnerID
fmt.Printf("Signature: %s\n", signature)
path := o.APIUrl + method
fmt.Printf("Sending POST request to %s with params %s\n", path, encoded)
if o.Verbose {
fmt.Printf("Signature: %s\n", signature)
fmt.Printf("Sending POST request to %s with params %s\n", path, encoded)
}
reqBody := strings.NewReader(encoded)
req, err := http.NewRequest("POST", path, reqBody)
@@ -432,7 +436,11 @@ func (o *OKCoin) SendAuthenticatedHTTPRequest(method string, v url.Values) (err
}
contents, _ := ioutil.ReadAll(resp.Body)
fmt.Printf("Recieved raw: %s\n", string(contents))
if o.Verbose {
fmt.Printf("Recieved raw: %s\n", string(contents))
}
resp.Body.Close()
return nil