/
account.go
125 lines (105 loc) · 3.92 KB
/
account.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package valr
import (
"context"
"fmt"
"net/url"
"time"
)
type Balance struct {
Available string `json:"available"`
Currency string `json:"currency"`
Reserved string `json:"reserved"`
Total string `json:"total"`
UpdatedAt time.Time `json:"updatedAt"`
}
// Balances satisfies the PrivateClient interface.
func (c *client) Balances(ctx context.Context) ([]Balance, error) {
res, err := c.httpClient.Get(ctx, "/account/balances", nil)
if err != nil {
return nil, fmt.Errorf("failed to fetch account balances: %w", err)
}
if !res.IsSuccess() {
return nil, fmt.Errorf("failed to fetch account balances: %d status "+
"code received", res.StatusCode)
}
var balances []Balance
if err = res.JSON(&balances); err != nil {
return nil, fmt.Errorf("failed to unmarshal account balances: %w", err)
}
return balances, nil
}
// Trade contains information regarding a single trade which has been executed.
type Trade struct {
CurrencyPair string `json:"currencyPair"`
ID int64 `json:"tradeId"`
Price string `json:"price"`
Quantity string `json:"quantity"`
Side string `json:"side"`
TradedAt time.Time `json:"tradedAt"`
}
// TradeHistory gets the last 100 recent trades for a given currency pair for
// your account.
func (c *client) TradeHistory(ctx context.Context, pair string) ([]Trade,
error) {
res, err := c.httpClient.Get(ctx, fmt.Sprintf("/account/%s/tradehistory",
pair), nil)
if err != nil {
return nil, fmt.Errorf("failed to fetch transaction history: %w", err)
}
if !res.IsSuccess() {
return nil, fmt.Errorf("failed to fetch transaction history: %d "+
"status code recevied", res.StatusCode)
}
var trades []Trade
if err = res.JSON(&trades); err != nil {
return nil, fmt.Errorf("failed to unmarshal trades: %w", err)
}
return trades, nil
}
// Transaction contains information regarding certain activities of your
// wallets.
type Transaction struct {
AdditionalInfo *TransactionInfo `json:"additionalInfo,omitempty"`
CreditCurrency string `json:"creditCurrency,omitempty"`
CreditValue string `json:"creditValue,omitempty"`
DebitCurrency string `json:"debitCurrency,omitempty"`
DebitValue string `json:"debitValue,omitempty"`
FeeCurrency string `json:"feeCurrency,omitempty"`
FeeValue string `json:"feeValue,omitempty"`
EventAt time.Time `json:"eventAt,omitempty"`
TypeInfo *TransactionTypeInfo `json:"transactionType,omitempty"`
}
// TransactionInfo contains additional information regarding Transactions.
type TransactionInfo struct {
CostPerCoin float64 `json:"costPerCoin,omitempty"`
CostPerCoinSymbol string `json:"costPerCoinSymbol,omitempty"`
CurrencyPairSymbol string `json:"currencyPairSymbol,omitempty"`
OrderID string `json:"orderID,omitempty"`
}
// TransactionTypeInfo contains additional information regarding the type of a
// transaction.
type TransactionTypeInfo struct {
Type TransactionType `json:"type,omitempty"`
Description string `json:"description,omitempty"`
}
// TransactionHistory satisfies the PrivateClient interface.
func (c *client) TransactionHistory(ctx context.Context,
req *TransactionHistoryRequest) ([]Transaction, error) {
params := make(url.Values)
if err := c.encoder.Encode(req, params); err != nil {
return nil, fmt.Errorf("failed to encode request params: %w", err)
}
res, err := c.httpClient.Get(ctx, "/account/transactionhistory", params)
if err != nil {
return nil, fmt.Errorf("failed to fetch transaction history: %w", err)
}
if !res.IsSuccess() {
return nil, fmt.Errorf("failed to fetch transaction history: %d "+
"status code recevied", res.StatusCode)
}
var transactions []Transaction
if err = res.JSON(&transactions); err != nil {
return nil, fmt.Errorf("failed to unmarshal transactions: %w", err)
}
return transactions, nil
}