Added Alphapoint Implementation, this will be used as a base class for Exchanges which rely on it.

This commit is contained in:
Adrian Gallagher
2015-05-22 22:49:51 +10:00
parent fea5d812eb
commit 68dc15bd2f

520
alphapointhttp.go Normal file
View File

@@ -0,0 +1,520 @@
package main
import (
"bytes"
"errors"
"fmt"
"log"
"strconv"
"time"
)
const (
ALPHAPOINT_API_URL = "https://api.dwvx.com.au:8400"
ALPHAPOINT_API_VERSION = "1"
ALPHAPOINT_TICKER = "GetTicker"
ALPHAPOINT_TRADES = "GetTrades"
ALPHAPOINT_TRADESBYDATE = "GetTradesByDate"
ALPHAPOINT_ORDERBOOK = "GetOrderBook"
ALPHAPOINT_PRODUCT_PAIRS = "GetProductPairs"
ALPHAPOINT_PRODUCTS = "GetProducts"
ALPHAPOINT_CREATE_ACCOUNT = "CreateAccount"
ALPHAPOINT_USERINFO = "GetUserInfo"
ALPHAPOINT_ACCOUNT_INFO = "GetAccountInfo"
ALPHAPOINT_ACCOUNT_TRADES = "GetAccountTrades"
ALPHAPOINT_DEPOSIT_ADDRESSES = "GetDepositAddresses"
ALPHAPOINT_WITHDRAW = "Withdraw"
ALPHAPOINT_CREATE_ORDER = "CreateOrder"
ALPHAPOINT_MODIFY_ORDER = "ModifyOrder"
ALPHAPOINT_CANCEL_ORDER = "CancelOrder"
ALPHAPOINT_CANCEALLORDERS = "CancelAllOrders"
ALPHAPOINT_OPEN_ORDERS = "GetAccountOpenOrders"
ALPHAPOINT_ORDER_FEE
)
type Alphapoint struct {
APIUrl, APIKey, UserID, APISecret string
}
type AlphapointTrade struct {
TID int64 `json:"tid"`
Price float64 `json:"px"`
Quantity float64 `json:"qty"`
Unixtime int `json:"unixtime"`
UTCTicks int64 `json:"utcticks"`
IncomingOrderSide int `json:"incomingOrderSide"`
IncomingServerOrderID int `json:"incomingServerOrderId"`
BookServerOrderID int `json:"bookServerOrderId"`
}
type AlphapointTrades struct {
IsAccepted bool `json:"isAccepted"`
RejectReason string `json:"rejectReason"`
DateTimeUTC int64 `json:"dateTimeUtc"`
Instrument string `json:"ins"`
StartIndex int `json:"startIndex"`
Count int `json:"count"`
Trades []AlphapointTrade `json:"trades"`
}
type AlphapointTradesByDate struct {
IsAccepted bool `json:"isAccepted"`
RejectReason string `json:"rejectReason"`
DateTimeUTC int64 `json:"dateTimeUtc"`
Instrument string `json:"ins"`
StartDate int64 `json:"startDate"`
EndDate int64 `json:"endDate"`
Trades []AlphapointTrade `json:"trades"`
}
type AlphapointTicker struct {
High float64 `json:"high"`
Last float64 `json:"last"`
Bid float64 `json:"bid"`
Volume float64 `json:"volume"`
Low float64 `json:"low"`
Ask float64 `json:"ask"`
Total24HrQtyTraded float64 `json:"Total24HrQtyTraded"`
Total24HrNumTrades float64 `json:"Total24HrNumTrades"`
SellOrderCount float64 `json:"sellOrderCount"`
BuyOrderCount float64 `json:"buyOrderCount"`
NumOfCreateOrders float64 `json:"numOfCreateOrders"`
IsAccepted bool `json:"isAccepted"`
RejectReason string `json:"rejectReason"`
}
type AlphapointOrderbookEntry struct {
Quantity float64 `json:"qty"`
Price float64 `json:"px"`
}
type AlphapointOrderbook struct {
Bids []AlphapointOrderbookEntry `json:"bids"`
Asks []AlphapointOrderbookEntry `json:"asks"`
IsAccepted bool `json:"isAccepted"`
RejectReason string `json:"rejectReason"`
}
type AlphapointProductPair struct {
Name string `json:"name"`
Productpaircode int `json:"productPairCode"`
Product1Label string `json:"product1Label"`
Product1Decimalplaces int `json:"product1DecimalPlaces"`
Product2Label string `json:"product2Label"`
Product2Decimalplaces int `json:"product2DecimalPlaces"`
}
type AlphapointProductPairs struct {
ProductPairs []AlphapointProductPair `json:"productPairs"`
IsAccepted bool `json:"isAccepted"`
RejectReason string `json:"rejectReason"`
}
type AlphapointProduct struct {
Name string `json:"name"`
IsDigital bool `json:"isDigital"`
ProductCode int `json:"productCode"`
DecimalPlaces int `json:"decimalPlaces"`
FullName string `json:"fullName"`
}
type AlphapointProducts struct {
Products []AlphapointProduct `json:"products"`
IsAccepted bool `json:"isAccepted"`
RejectReason string `json:"rejectReason"`
}
type AlphapointAccountInfo struct {
Currencies []struct {
Name string `json:"name"`
Balance int `json:"balance"`
Hold int `json:"hold"`
} `json:"currencies"`
Productpairs []struct {
ProductPairName string `json:"productPairName"`
ProductPairCode int `json:"productPairCode"`
TradeCount int `json:"tradeCount"`
TradeVolume int `json:"tradeVolume"`
} `json:"productPairs"`
IsAccepted bool `json:"isAccepted"`
RejectReason string `json:"rejectReason"`
}
type AlphapointOrder struct {
ServerOrderID int64 `json:"ServerOrderId"`
AccountID int `json:"AccountId"`
Price float64 `json:"price"`
QuantityTotal float64 `json:"QtyTotal"`
QuantityRemaining float64 `json:"QtyRemaining"`
ReceiveTime int64 `json:"ReceiveTime"`
Side int `json:"side"`
}
type AlphapointOrderInfo struct {
Instrument string `json:"ins"`
Orders []AlphapointOrder `json:"openOrders"`
}
func (a *Alphapoint) GetTicker(symbol string) (AlphapointTicker, error) {
request := make(map[string]interface{})
request["productPair"] = symbol
response := AlphapointTicker{}
err := a.SendRequest("POST", ALPHAPOINT_TICKER, request, &response)
if err != nil {
return response, err
}
if !response.IsAccepted {
return response, errors.New(response.RejectReason)
}
return response, nil
}
func (a *Alphapoint) GetTrades(symbol string, startIndex, count int) (AlphapointTrades, error) {
request := make(map[string]interface{})
request["ins"] = symbol
request["startIndex"] = startIndex
request["Count"] = count
response := AlphapointTrades{}
err := a.SendRequest("POST", ALPHAPOINT_TRADES, request, &response)
if err != nil {
return response, err
}
if !response.IsAccepted {
return response, errors.New(response.RejectReason)
}
return response, nil
}
func (a *Alphapoint) GetTradesByDate(symbol string, startDate, endDate int64) (AlphapointTradesByDate, error) {
request := make(map[string]interface{})
request["ins"] = symbol
request["startDate"] = startDate
request["endDate"] = endDate
response := AlphapointTradesByDate{}
err := a.SendRequest("POST", ALPHAPOINT_TRADESBYDATE, request, &response)
if err != nil {
return response, err
}
if !response.IsAccepted {
return response, errors.New(response.RejectReason)
}
return response, nil
}
func (a *Alphapoint) GetOrderbook(symbol string) (AlphapointOrderbook, error) {
request := make(map[string]interface{})
request["productPair"] = symbol
response := AlphapointOrderbook{}
err := a.SendRequest("POST", ALPHAPOINT_ORDERBOOK, request, &response)
if err != nil {
return response, err
}
if !response.IsAccepted {
return response, errors.New(response.RejectReason)
}
return response, nil
}
func (a *Alphapoint) GetProductPairs() (AlphapointProductPairs, error) {
response := AlphapointProductPairs{}
err := a.SendRequest("POST", ALPHAPOINT_PRODUCT_PAIRS, nil, &response)
if err != nil {
return response, err
}
if !response.IsAccepted {
return response, errors.New(response.RejectReason)
}
return response, nil
}
func (a *Alphapoint) GetProducts() (AlphapointProducts, error) {
response := AlphapointProducts{}
err := a.SendRequest("POST", ALPHAPOINT_PRODUCTS, nil, &response)
if err != nil {
return response, err
}
if !response.IsAccepted {
return response, errors.New(response.RejectReason)
}
return response, nil
}
func (a *Alphapoint) CreateAccount(firstName, lastName, email, phone, password string) error {
request := make(map[string]interface{})
request["firstname"] = firstName
request["lastname"] = lastName
request["email"] = email
request["phone"] = phone
request["password"] = password
type Response struct {
IsAccepted bool `json:"isAccepted"`
RejectReason string `json:"rejectReason"`
}
response := Response{}
err := a.SendAuthenticatedHTTPRequest("POST", ALPHAPOINT_CREATE_ACCOUNT, request, &response)
if err != nil {
log.Println(err)
}
if !response.IsAccepted {
return errors.New(response.RejectReason)
}
return nil
}
func (a *Alphapoint) GetUserInfo() error {
err := a.SendAuthenticatedHTTPRequest("POST", ALPHAPOINT_USERINFO, map[string]interface{}{}, nil)
if err != nil {
return err
}
return nil
}
func (a *Alphapoint) GetAccountInfo() (AlphapointAccountInfo, error) {
response := AlphapointAccountInfo{}
err := a.SendAuthenticatedHTTPRequest("POST", ALPHAPOINT_ACCOUNT_INFO, map[string]interface{}{}, &response)
if err != nil {
return response, err
}
if !response.IsAccepted {
return response, errors.New(response.RejectReason)
}
return response, nil
}
func (a *Alphapoint) GetAccountTrades(symbol string, startIndex, count int) (AlphapointTrades, error) {
request := make(map[string]interface{})
request["ins"] = symbol
request["startIndex"] = startIndex
request["count"] = count
response := AlphapointTrades{}
err := a.SendAuthenticatedHTTPRequest("POST", ALPHAPOINT_ACCOUNT_TRADES, request, &response)
if err != nil {
return response, err
}
if !response.IsAccepted {
return response, errors.New(response.RejectReason)
}
return response, nil
}
func (a *Alphapoint) GetDepositAddresses() error {
type Response struct {
IsAccepted bool `json:"isAccepted"`
RejectReason string `json:"rejectReason"`
}
response := Response{}
err := a.SendAuthenticatedHTTPRequest("POST", ALPHAPOINT_DEPOSIT_ADDRESSES, map[string]interface{}{}, &response)
if err != nil {
return err
}
if !response.IsAccepted {
return errors.New(response.RejectReason)
}
return nil
}
func (a *Alphapoint) WithdrawCoins(symbol, product string, amount float64, address string) error {
request := make(map[string]interface{})
request["ins"] = symbol
request["product"] = product
request["amount"] = strconv.FormatFloat(amount, 'f', 8, 64)
request["sendToAddress"] = address
type Response struct {
IsAccepted bool `json:"isAccepted"`
RejectReason string `json:"rejectReason"`
}
response := Response{}
err := a.SendAuthenticatedHTTPRequest("POST", ALPHAPOINT_WITHDRAW, request, &response)
if err != nil {
return err
}
if !response.IsAccepted {
return errors.New(response.RejectReason)
}
return nil
}
func (a *Alphapoint) CreateOrder(symbol, side string, orderType int, quantity, price float64) (int64, error) {
request := make(map[string]interface{})
request["ins"] = symbol
request["side"] = side
request["orderType"] = "0"
request["qty"] = strconv.FormatFloat(quantity, 'f', 8, 64)
request["px"] = strconv.FormatFloat(price, 'f', 2, 64)
type Response struct {
IsAccepted bool `json:"isAccepted"`
RejectReason string `json:"rejectReason"`
DateTimeUTC int64 `json:"dateTimeUtc"`
OrderID int64 `json:"serverOrderId,string"`
}
response := Response{}
err := a.SendAuthenticatedHTTPRequest("POST", ALPHAPOINT_CREATE_ORDER, request, &response)
if err != nil {
return 0, err
}
if !response.IsAccepted {
return 0, errors.New(response.RejectReason)
}
return response.OrderID, nil
}
func (a *Alphapoint) ModifyOrder(symbol string, OrderID, action int64) (int64, error) {
request := make(map[string]interface{})
request["ins"] = symbol
request["serverOrderId"] = OrderID
request["modifyAction"] = action
type Response struct {
IsAccepted bool `json:"isAccepted"`
RejectReason string `json:"rejectReason"`
DateTimeUTC int64 `json:"dateTimeUtc"`
OrderID int64 `json:"serverOrderId,string"`
}
response := Response{}
err := a.SendAuthenticatedHTTPRequest("POST", ALPHAPOINT_MODIFY_ORDER, request, &response)
if err != nil {
return 0, err
}
if !response.IsAccepted {
return 0, errors.New(response.RejectReason)
}
return response.OrderID, nil
}
func (a *Alphapoint) CancelOrder(symbol string, OrderID int64) (int64, error) {
request := make(map[string]interface{})
request["ins"] = symbol
request["serverOrderId"] = OrderID
type Response struct {
IsAccepted bool `json:"isAccepted"`
RejectReason string `json:"rejectReason"`
DateTimeUTC int64 `json:"dateTimeUtc"`
OrderID int64 `json:"serverOrderId,string"`
}
response := Response{}
err := a.SendAuthenticatedHTTPRequest("POST", ALPHAPOINT_CANCEL_ORDER, request, &response)
if err != nil {
return 0, err
}
if !response.IsAccepted {
return 0, errors.New(response.RejectReason)
}
return response.OrderID, nil
}
func (a *Alphapoint) CancelAllOrders(symbol string) error {
request := make(map[string]interface{})
request["ins"] = symbol
type Response struct {
IsAccepted bool `json:"isAccepted"`
RejectReason string `json:"rejectReason"`
}
response := Response{}
err := a.SendAuthenticatedHTTPRequest("POST", ALPHAPOINT_CANCEALLORDERS, request, &response)
if err != nil {
return err
}
if !response.IsAccepted {
return errors.New(response.RejectReason)
}
return nil
}
func (a *Alphapoint) GetOrders() (AlphapointOrderInfo, error) {
type Response struct {
Orders AlphapointOrderInfo `json:"openOrdersInfo"`
IsAccepted bool `json:"isAccepted"`
RejectReason string `json:"rejectReason"`
}
response := Response{}
err := a.SendAuthenticatedHTTPRequest("POST", ALPHAPOINT_OPEN_ORDERS, map[string]interface{}{}, &response)
if err != nil {
return AlphapointOrderInfo{}, err
}
if !response.IsAccepted {
return AlphapointOrderInfo{}, errors.New(response.RejectReason)
}
return response.Orders, nil
}
func (a *Alphapoint) SendRequest(method, path string, data map[string]interface{}, result interface{}) error {
headers := make(map[string]string)
headers["Content-Type"] = "application/json"
path = fmt.Sprintf("%s/ajax/v%s/%s", ALPHAPOINT_API_URL, ALPHAPOINT_API_VERSION, path)
PayloadJson, err := JSONEncode(data)
if err != nil {
return errors.New("SendAuthenticatedHTTPRequest: Unable to JSON request")
}
resp, err := SendHTTPRequest(method, path, headers, bytes.NewBuffer(PayloadJson))
if err != nil {
return err
}
err = JSONDecode([]byte(resp), &result)
if err != nil {
return errors.New("Unable to JSON Unmarshal response.")
}
return nil
}
func (a *Alphapoint) SendAuthenticatedHTTPRequest(method, path string, data map[string]interface{}, result interface{}) error {
headers := make(map[string]string)
headers["Content-Type"] = "application/json"
data["apiKey"] = a.APIKey
nonce := strconv.FormatInt(time.Now().Unix(), 10)
data["apiNonce"] = nonce
hmac := GetHMAC(HASH_SHA256, []byte(nonce+a.UserID+a.APIKey), []byte(a.APISecret))
data["apiSig"] = StringToUpper(HexEncodeToString(hmac))
path = fmt.Sprintf("%s/ajax/v%s/%s", ALPHAPOINT_API_URL, ALPHAPOINT_API_VERSION, path)
PayloadJson, err := JSONEncode(data)
if err != nil {
return errors.New("SendAuthenticatedHTTPRequest: Unable to JSON request")
}
resp, err := SendHTTPRequest(method, path, headers, bytes.NewBuffer(PayloadJson))
if err != nil {
return err
}
err = JSONDecode([]byte(resp), &result)
if err != nil {
return errors.New("Unable to JSON Unmarshal response.")
}
return nil
}