From bb4805a7c2f4618d00c290b8ff622f1bfc01b2fb Mon Sep 17 00:00:00 2001 From: Michael Bang Date: Sat, 7 Oct 2017 23:55:55 +0200 Subject: [PATCH 1/2] Implement AccountTransactions. --- AccountTransactionType.go | 70 +++++++++++++++++++++++++++++++++++++++ bitstamp.go | 58 +++++++++++++++++++++++++++++--- 2 files changed, 124 insertions(+), 4 deletions(-) create mode 100644 AccountTransactionType.go diff --git a/AccountTransactionType.go b/AccountTransactionType.go new file mode 100644 index 0000000..091995b --- /dev/null +++ b/AccountTransactionType.go @@ -0,0 +1,70 @@ +package bitstamp + +import ( + "strconv" + "strings" + "time" +) + +const ( + UserDeposit UserTransactionType = iota + UserWithdrawal + UserMarketTrade + UserSubAccountTransfer +) + +type UserTransactionType int8 + +func (t *UserTransactionType) UnmarshalJSON(b []byte) error { + s := strings.Trim(string(b), "\"") + i, err := strconv.ParseInt(s, 10, 8) + *t = UserTransactionType(i) + return err +} + +func (t UserTransactionType) String() string { + switch t { + case UserDeposit: + return "Deposit" + case UserWithdrawal: + return "Withdrawal" + case UserMarketTrade: + return "MarketTrade" + case UserSubAccountTransfer: + return "SubAccountTransfer" + default: + return "" + } +} + +type accountTransactionsResult struct { + DateTime Time `json:"datetime"` + Id int64 `json:"id"` + Type UserTransactionType `json:"type"` + Usd Float `json:"usd"` + Eur Float `json:"eur"` + Btc Float `json:"btc"` + Xrp Float `json:"xrp"` + Ltc Float `json:"ltc"` + Eth Float `json:"eth"` + BtcUsd Float `json:"btc_usd"` + UsdBtc Float `json:"usd_btc"` + Fee Float `json:"fee"` + OrderId int64 `json:"order_id"` +} + +type AccountTransactionResult struct { + DateTime time.Time `json:"datetime"` + Id int64 `json:"id"` + Type UserTransactionType `json:"type"` + Usd float64 `json:"usd"` + Eur float64 `json:"eur"` + Btc float64 `json:"btc"` + Xrp float64 `json:"xrp"` + Ltc float64 `json:"ltc"` + Eth float64 `json:"eth"` + BtcUsd float64 `json:"btc_usd"` + UsdBtc float64 `json:"usd_btc"` + Fee float64 `json:"fee"` + OrderId int64 `json:"order_id"` +} diff --git a/bitstamp.go b/bitstamp.go index 8e5faba..0efc953 100644 --- a/bitstamp.go +++ b/bitstamp.go @@ -18,6 +18,18 @@ var _cliId, _key, _secret string var _url string = "https://www.bitstamp.net/api/v2" +const bitstampTimeLayout = "2006-01-02 15:04:05" + +type Time time.Time + +func (t *Time) UnmarshalJSON(b []byte) error { + s := strings.Trim(string(b), "\"") + + _t, err := time.Parse(bitstampTimeLayout, s) + *t = Time(_t) + return err +} + type AccountBalanceResult struct { UsdBalance float64 `json:"usd_balance,string"` BtcBalance float64 `json:"btc_balance,string"` @@ -65,7 +77,7 @@ type TickerResult struct { type BuyOrderResult struct { Id int64 `json:"id,string"` - DateTime string `json:"datetime"` + DateTime Time `json:"datetime"` Type int `json:"type,string"` Price float64 `json:"price,string"` Amount float64 `json:"amount,string"` @@ -73,7 +85,7 @@ type BuyOrderResult struct { type SellOrderResult struct { Id int64 `json:"id,string"` - DateTime string `json:"datetime"` + DateTime Time `json:"datetime"` Type int `json:"type,string"` Price float64 `json:"price,string"` Amount float64 `json:"amount,string"` @@ -81,13 +93,22 @@ type SellOrderResult struct { type OpenOrder struct { Id int64 `json:"id,string"` - DateTime string `json:"datetime"` + DateTime Time `json:"datetime"` Type int `json:"type,string"` Price float64 `json:"price,string"` Amount float64 `json:"amount,string"` CurrencyPair string `json:"currency_pair"` } +type Float float64 + +func (f *Float) UnmarshalJSON(b []byte) error { + s := strings.Trim(string(b), "\"") + _f, err := strconv.ParseFloat(s, 64) + *f = Float(_f) + return err +} + func SetAuth(clientId, key, secret string) { _cliId = clientId _key = key @@ -153,7 +174,7 @@ func privateQuery(path string, values url.Values, v interface{}) error { } //parse the JSON response into the response object - //log.Println(string(body)) + // log.Println(string(body)) return json.Unmarshal(body, v) } @@ -251,3 +272,32 @@ func OpenOrders() (*[]OpenOrder, error) { } return result, nil } + +func AccountTransactions() ([]AccountTransactionResult, error) { + internalTs := make([]accountTransactionsResult, 0) + err := privateQuery("/user_transactions/", url.Values{}, &internalTs) + if err != nil { + return nil, err + } + + ts := make([]AccountTransactionResult, len(internalTs)) + for i, t := range internalTs { + ts[i] = AccountTransactionResult{ + DateTime: time.Time(t.DateTime), + Id: t.Id, + Type: t.Type, + Usd: float64(t.Usd), + Eur: float64(t.Eur), + Btc: float64(t.Btc), + Xrp: float64(t.Xrp), + Ltc: float64(t.Ltc), + Eth: float64(t.Eth), + BtcUsd: float64(t.BtcUsd), + UsdBtc: float64(t.UsdBtc), + Fee: float64(t.Fee), + OrderId: t.OrderId, + } + } + + return ts, nil +} From 8ed837dc79bc6e1496560cd7e8a524b724daf4bd Mon Sep 17 00:00:00 2001 From: Michael Bang Date: Sun, 7 Jan 2018 23:40:04 +0100 Subject: [PATCH 2/2] account_transaction: remove unnecessary code. Add comment. --- ...ansactionType.go => account_transaction.go | 20 ++++++++++--- bitstamp.go | 30 ++++--------------- 2 files changed, 21 insertions(+), 29 deletions(-) rename AccountTransactionType.go => account_transaction.go (74%) diff --git a/AccountTransactionType.go b/account_transaction.go similarity index 74% rename from AccountTransactionType.go rename to account_transaction.go index 091995b..68c41b1 100644 --- a/AccountTransactionType.go +++ b/account_transaction.go @@ -3,7 +3,6 @@ package bitstamp import ( "strconv" "strings" - "time" ) const ( @@ -37,8 +36,21 @@ func (t UserTransactionType) String() string { } } -type accountTransactionsResult struct { - DateTime Time `json:"datetime"` +type Float float64 + +func (f *Float) UnmarshalJSON(b []byte) error { + s := strings.Trim(string(b), "\"") + _f, err := strconv.ParseFloat(s, 64) + *f = Float(_f) + return err +} + +// accountTransactionResult is used internally to ease the process of unmarshalling. +// For some reason, for the transaction endpoint, Bitstamp is sends numbers +// encoded as both strings and floats, intermingled. This means that we cannot +// use the `json:"name,string"` automagic conversion here. +type accountTransactionResult struct { + DateTime string `json:"datetime"` Id int64 `json:"id"` Type UserTransactionType `json:"type"` Usd Float `json:"usd"` @@ -54,7 +66,7 @@ type accountTransactionsResult struct { } type AccountTransactionResult struct { - DateTime time.Time `json:"datetime"` + DateTime string `json:"datetime"` Id int64 `json:"id"` Type UserTransactionType `json:"type"` Usd float64 `json:"usd"` diff --git a/bitstamp.go b/bitstamp.go index 90761ae..1685543 100644 --- a/bitstamp.go +++ b/bitstamp.go @@ -18,17 +18,6 @@ var _cliId, _key, _secret string var _url string = "https://www.bitstamp.net/api/v2" -const bitstampTimeLayout = "2006-01-02 15:04:05" - -type Time time.Time - -func (t *Time) UnmarshalJSON(b []byte) error { - s := strings.Trim(string(b), "\"") - _t, err := time.Parse(bitstampTimeLayout, s) - *t = Time(_t) - return err -} - type ErrorResult struct { Status string `json:"status,string"` Reason string `json:"reason,string"` @@ -88,7 +77,7 @@ type TickerResult struct { type BuyOrderResult struct { Id int64 `json:"id,string"` - DateTime Time `json:"datetime"` + DateTime string `json:"datetime"` Type int `json:"type,string"` Price float64 `json:"price,string"` Amount float64 `json:"amount,string"` @@ -96,7 +85,7 @@ type BuyOrderResult struct { type SellOrderResult struct { Id int64 `json:"id,string"` - DateTime Time `json:"datetime"` + DateTime string `json:"datetime"` Type int `json:"type,string"` Price float64 `json:"price,string"` Amount float64 `json:"amount,string"` @@ -115,22 +104,13 @@ type OrderBookItem struct { type OpenOrder struct { Id int64 `json:"id,string"` - DateTime Time `json:"datetime"` + DateTime string `json:"datetime"` Type int `json:"type,string"` Price float64 `json:"price,string"` Amount float64 `json:"amount,string"` CurrencyPair string `json:"currency_pair"` } -type Float float64 - -func (f *Float) UnmarshalJSON(b []byte) error { - s := strings.Trim(string(b), "\"") - _f, err := strconv.ParseFloat(s, 64) - *f = Float(_f) - return err -} - func SetAuth(clientId, key, secret string) { _cliId = clientId _key = key @@ -334,7 +314,7 @@ func OpenOrders() (*[]OpenOrder, error) { } func AccountTransactions() ([]AccountTransactionResult, error) { - internalTs := make([]accountTransactionsResult, 0) + internalTs := make([]accountTransactionResult, 0) err := privateQuery("/user_transactions/", url.Values{}, &internalTs) if err != nil { return nil, err @@ -343,7 +323,7 @@ func AccountTransactions() ([]AccountTransactionResult, error) { ts := make([]AccountTransactionResult, len(internalTs)) for i, t := range internalTs { ts[i] = AccountTransactionResult{ - DateTime: time.Time(t.DateTime), + DateTime: t.DateTime, Id: t.Id, Type: t.Type, Usd: float64(t.Usd),