Made use of Pairs and BaseCurrencies variables, formatting changes.

This commit is contained in:
Adrian Gallagher
2015-05-06 20:20:42 +10:00
parent 8183d1a955
commit f6bcdf8a76
14 changed files with 976 additions and 920 deletions

View File

@@ -1,88 +1,90 @@
package main
import (
"strconv"
"bytes"
"errors"
"time"
"log"
"fmt"
"log"
"strconv"
"time"
)
const (
ANX_API_URL = "https://anxpro.com/"
ANX_API_VERSION = "3"
ANX_APIKEY = "apiKey"
ANX_DATA_TOKEN = "dataToken"
ANX_ORDER_NEW = "order/new"
ANX_ORDER_INFO = "order/info"
ANX_SEND = "send"
ANX_SUBACCOUNT_NEW = "subaccount/new"
ANX_API_URL = "https://anxpro.com/"
ANX_API_VERSION = "3"
ANX_APIKEY = "apiKey"
ANX_DATA_TOKEN = "dataToken"
ANX_ORDER_NEW = "order/new"
ANX_ORDER_INFO = "order/info"
ANX_SEND = "send"
ANX_SUBACCOUNT_NEW = "subaccount/new"
ANX_RECEIVE_ADDRESS = "receive"
ANX_CREATE_ADDRESS = "receive/create"
ANX_TICKER = "money/ticker"
ANX_CREATE_ADDRESS = "receive/create"
ANX_TICKER = "money/ticker"
)
type ANX struct {
Name string
Enabled bool
Verbose bool
Websocket bool
RESTPollingDelay time.Duration
APIKey, APISecret string
Name string
Enabled bool
Verbose bool
Websocket bool
RESTPollingDelay time.Duration
APIKey, APISecret string
TakerFee, MakerFee float64
BaseCurrencies []string
Pairs []string
}
type ANXOrder struct {
OrderType string `json:"orderType"`
BuyTradedCurrency bool `json:"buyTradedCurrency"`
TradedCurrency string `json:"tradedCurrency"`
SettlementCurrency string `json:"settlementCurrency"`
TradedCurrencyAmount string `json:"tradedCurrencyAmount"`
SettlementCurrencyAmount string `json:"settlementCurrencyAmount"`
OrderType string `json:"orderType"`
BuyTradedCurrency bool `json:"buyTradedCurrency"`
TradedCurrency string `json:"tradedCurrency"`
SettlementCurrency string `json:"settlementCurrency"`
TradedCurrencyAmount string `json:"tradedCurrencyAmount"`
SettlementCurrencyAmount string `json:"settlementCurrencyAmount"`
LimitPriceInSettlementCurrency string `json:"limitPriceInSettlementCurrency"`
ReplaceExistingOrderUUID string `json:"replaceExistingOrderUuid"`
ReplaceOnlyIfActive bool `json:"replaceOnlyIfActive"`
ReplaceExistingOrderUUID string `json:"replaceExistingOrderUuid"`
ReplaceOnlyIfActive bool `json:"replaceOnlyIfActive"`
}
type ANXOrderResponse struct {
BuyTradedCurrency bool `json:"buyTradedCurrency"`
ExecutedAverageRate string `json:"executedAverageRate"`
BuyTradedCurrency bool `json:"buyTradedCurrency"`
ExecutedAverageRate string `json:"executedAverageRate"`
LimitPriceInSettlementCurrency string `json:"limitPriceInSettlementCurrency"`
OrderID string `json:"orderId"`
OrderStatus string `json:"orderStatus"`
OrderType string `json:"orderType"`
ReplaceExistingOrderUUID string `json:"replaceExistingOrderId"`
SettlementCurrency string `json:"settlementCurrency"`
SettlementCurrencyAmount string `json:"settlementCurrencyAmount"`
SettlementCurrencyOutstanding string `json:"settlementCurrencyOutstanding"`
Timestamp int64 `json:"timestamp"`
TradedCurrency string `json:"tradedCurrency"`
TradedCurrencyAmount string `json:"tradedCurrencyAmount"`
TradedCurrencyOutstanding string `json:"tradedCurrencyOutstanding"`
OrderID string `json:"orderId"`
OrderStatus string `json:"orderStatus"`
OrderType string `json:"orderType"`
ReplaceExistingOrderUUID string `json:"replaceExistingOrderId"`
SettlementCurrency string `json:"settlementCurrency"`
SettlementCurrencyAmount string `json:"settlementCurrencyAmount"`
SettlementCurrencyOutstanding string `json:"settlementCurrencyOutstanding"`
Timestamp int64 `json:"timestamp"`
TradedCurrency string `json:"tradedCurrency"`
TradedCurrencyAmount string `json:"tradedCurrencyAmount"`
TradedCurrencyOutstanding string `json:"tradedCurrencyOutstanding"`
}
type ANXTickerComponent struct {
Currency string `json:"currency"`
Display string `json:"display"`
DisplayShort string `json:"display_short"`
Value float64 `json:"value,string"`
ValueInt int64 `json:"value_int,string"`
Currency string `json:"currency"`
Display string `json:"display"`
DisplayShort string `json:"display_short"`
Value float64 `json:"value,string"`
ValueInt int64 `json:"value_int,string"`
}
type ANXTicker struct {
Result string `json:"result"`
Data struct {
High ANXTickerComponent `json:"high"`
Low ANXTickerComponent `json:"low"`
Avg ANXTickerComponent `json:"avg"`
Vwap ANXTickerComponent `json:"vwap"`
Vol ANXTickerComponent `json:"vol"`
Last ANXTickerComponent `json:"last"`
Buy ANXTickerComponent `json:buy"`
Sell ANXTickerComponent `json:"sell"`
Now float64 `json:"now"`
UpdateTime float64 `json:"dataUpdateTime"`
Data struct {
High ANXTickerComponent `json:"high"`
Low ANXTickerComponent `json:"low"`
Avg ANXTickerComponent `json:"avg"`
Vwap ANXTickerComponent `json:"vwap"`
Vol ANXTickerComponent `json:"vol"`
Last ANXTickerComponent `json:"last"`
Buy ANXTickerComponent `json:buy"`
Sell ANXTickerComponent `json:"sell"`
Now float64 `json:"now"`
UpdateTime float64 `json:"dataUpdateTime"`
} `json:"data"`
}
@@ -96,7 +98,7 @@ func (a *ANX) SetDefaults() {
a.RESTPollingDelay = 10
}
func (a *ANX) GetName() (string) {
func (a *ANX) GetName() string {
return a.Name
}
@@ -104,7 +106,7 @@ func (a *ANX) SetEnabled(enabled bool) {
a.Enabled = enabled
}
func (a *ANX) IsEnabled() (bool) {
func (a *ANX) IsEnabled() bool {
return a.Enabled
}
@@ -121,8 +123,8 @@ func (a *ANX) SetAPIKeys(apiKey, apiSecret string) {
a.APISecret = string(result)
}
func (a *ANX) GetFee(maker bool) (float64) {
if (maker) {
func (a *ANX) GetFee(maker bool) float64 {
if maker {
return a.MakerFee
} else {
return a.TakerFee
@@ -138,13 +140,13 @@ func (a *ANX) Run() {
go func() {
ANXBTC := a.GetTicker("BTCUSD")
log.Printf("ANX BTC: Last %f High %f Low %f Volume %f\n", ANXBTC.Data.Last.Value, ANXBTC.Data.High.Value, ANXBTC.Data.Low.Value, ANXBTC.Data.Vol.Value)
AddExchangeInfo(a.GetName(), "BTC", ANXBTC.Data.Last.Value, ANXBTC.Data.Vol.Value)
AddExchangeInfo(a.GetName(), "BTC", ANXBTC.Data.Last.Value, ANXBTC.Data.Vol.Value)
}()
time.Sleep(time.Second * a.RESTPollingDelay)
}
}
func (a *ANX) GetTicker(currency string) (ANXTicker) {
func (a *ANX) GetTicker(currency string) ANXTicker {
var ticker ANXTicker
err := SendHTTPGetRequest(fmt.Sprintf("%sapi/2/%s/%s", ANX_API_URL, currency, ANX_TICKER), true, &ticker)
if err != nil {
@@ -167,12 +169,12 @@ func (a *ANX) GetAPIKey(username, password, otp, deviceID string) (string, strin
request["deviceId"] = deviceID
type APIKeyResponse struct {
APIKey string `json:"apiKey"`
APISecret string `json:"apiSecret"`
APIKey string `json:"apiKey"`
APISecret string `json:"apiSecret"`
ResultCode string `json:"resultCode"`
Timestamp int64 `json:"timestamp"`
Timestamp int64 `json:"timestamp"`
}
var response APIKeyResponse
var response APIKeyResponse
err := a.SendAuthenticatedHTTPRequest(ANX_APIKEY, request, &response)
@@ -189,14 +191,14 @@ func (a *ANX) GetAPIKey(username, password, otp, deviceID string) (string, strin
return response.APIKey, response.APISecret
}
func (a *ANX) GetDataToken() (string) {
func (a *ANX) GetDataToken() string {
request := make(map[string]interface{})
type DataTokenResponse struct {
ResultCode string `json:"resultCode"`
Timestamp int64 `json:"timestamp"`
Token string `json:"token"`
UUID string `json:"uuid"`
Timestamp int64 `json:"timestamp"`
Token string `json:"token"`
UUID string `json:"uuid"`
}
var response DataTokenResponse
@@ -215,7 +217,7 @@ func (a *ANX) GetDataToken() (string) {
return response.Token
}
func (a *ANX) NewOrder(orderType string, buy bool, tradedCurrency, tradedCurrencyAmount, settlementCurrency, settlementCurrencyAmount, limitPriceSettlement string,
func (a *ANX) NewOrder(orderType string, buy bool, tradedCurrency, tradedCurrencyAmount, settlementCurrency, settlementCurrencyAmount, limitPriceSettlement string,
replace bool, replaceUUID string, replaceIfActive bool) {
request := make(map[string]interface{})
@@ -241,8 +243,8 @@ func (a *ANX) NewOrder(orderType string, buy bool, tradedCurrency, tradedCurrenc
request["order"] = order
type OrderResponse struct {
OrderID string `json:"orderId"`
Timestamp int64 `json:"timestamp"`
OrderID string `json:"orderId"`
Timestamp int64 `json:"timestamp"`
ResultCode string `json:"resultCode"`
}
var response OrderResponse
@@ -265,9 +267,9 @@ func (a *ANX) OrderInfo(orderID string) (ANXOrderResponse, error) {
request["orderId"] = orderID
type OrderInfoResponse struct {
Order ANXOrderResponse `json:"order"`
ResultCode string `json:"resultCode"`
Timestamp int64 `json:"timestamp"`
Order ANXOrderResponse `json:"order"`
ResultCode string `json:"resultCode"`
Timestamp int64 `json:"timestamp"`
}
var response OrderInfoResponse
@@ -277,7 +279,7 @@ func (a *ANX) OrderInfo(orderID string) (ANXOrderResponse, error) {
log.Println(err)
return ANXOrderResponse{}, err
}
if response.ResultCode != "OK" {
log.Printf("Response code is not OK: %s\n", response.ResultCode)
return ANXOrderResponse{}, errors.New(response.ResultCode)
@@ -297,8 +299,8 @@ func (a *ANX) Send(currency, address, otp, amount string) (string, error) {
type SendResponse struct {
TransactionID string `json:"transactionId"`
ResultCode string `json:"resultCode"`
Timestamp int64 `json:"timestamp"`
ResultCode string `json:"resultCode"`
Timestamp int64 `json:"timestamp"`
}
var response SendResponse
@@ -307,7 +309,7 @@ func (a *ANX) Send(currency, address, otp, amount string) (string, error) {
if err != nil {
return "", err
}
if response.ResultCode != "OK" {
log.Printf("Response code is not OK: %s\n", response.ResultCode)
return "", errors.New(response.ResultCode)
@@ -323,7 +325,7 @@ func (a *ANX) CreateNewSubAccount(currency, name string) (string, error) {
type SubaccountResponse struct {
SubAccount string `json:"subAccount"`
ResultCode string `json:"resultCode"`
Timestamp int64 `json:"timestamp"`
Timestamp int64 `json:"timestamp"`
}
var response SubaccountResponse
@@ -349,10 +351,10 @@ func (a *ANX) GetDepositAddress(currency, name string, new bool) (string, error)
}
type AddressResponse struct {
Address string `json:"address"`
Address string `json:"address"`
SubAccount string `json:"subAccount"`
ResultCode string `json:"resultCode"`
Timestamp int64 `json:"timestamp"`
Timestamp int64 `json:"timestamp"`
}
var response AddressResponse
@@ -381,7 +383,7 @@ func (a *ANX) SendAuthenticatedHTTPRequest(path string, params map[string]interf
path = fmt.Sprintf("api/%s/%s", ANX_API_VERSION, path)
if params != nil {
for key, value:= range params {
for key, value := range params {
request[key] = value
}
}
@@ -396,23 +398,23 @@ func (a *ANX) SendAuthenticatedHTTPRequest(path string, params map[string]interf
log.Printf("Request JSON: %s\n", PayloadJson)
}
hmac := GetHMAC(HASH_SHA512, []byte(path + string("\x00") + string(PayloadJson)), []byte(a.APISecret))
hmac := GetHMAC(HASH_SHA512, []byte(path+string("\x00")+string(PayloadJson)), []byte(a.APISecret))
headers := make(map[string]string)
headers["Rest-Key"] = a.APIKey
headers["Rest-Sign"] = Base64Encode([]byte(hmac))
headers["Content-Type"] = "application/json"
resp, err := SendHTTPRequest("POST", ANX_API_URL + path, headers, bytes.NewBuffer(PayloadJson))
resp, err := SendHTTPRequest("POST", ANX_API_URL+path, headers, bytes.NewBuffer(PayloadJson))
if a.Verbose {
log.Printf("Recieved raw: \n%s\n", resp)
}
err = JSONDecode([]byte(resp), &result)
if err != nil {
return errors.New("Unable to JSON Unmarshal response.")
}
return nil
}
}