-
Notifications
You must be signed in to change notification settings - Fork 2
/
dovewallet.go
140 lines (112 loc) · 3.62 KB
/
dovewallet.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package dovewallet
import (
"encoding/json"
"github.com/shopspring/decimal"
"time"
)
const (
API_BASE = "https://api.dovewallet.com/"
API_VERSION = "v1.1"
)
// New returns an instantiated dovewallet struct
func New(apiKey, apiSecret string) *DoveWallet {
client := NewClient(apiKey, apiSecret)
return &DoveWallet{client}
}
// dovewallet represents a Dove Wallet client
type DoveWallet struct {
client *client
}
type requestParams struct {
Params []requestParam
}
type requestParam struct {
Key string
Value string
}
type DoveWalletClient interface {
GetBalances() (balanceResponse BalancesResponse, err error)
GetOrderHistory(market string, walletId int64, count int, startAt *time.Time) (orderHistoryResponse OrderHistoryResponse, err error)
GetOrder(uuid string) (orderResponse OrderResponse, err error)
GetOpenOrders(market string, walletId int64) (orderHistoryResponse OrderHistoryResponse, err error)
OpenLimitOrder(direction, market string, quantity, rate decimal.Decimal, walletId int64) (limitOrderResponse OrderResponse, err error)
}
// set enable/disable http request/response dump
func (c *DoveWallet) SetDebug(enable bool) {
c.client.debug = enable
}
// Account
// GetBalances is used to retrieve all balances from your account
func (d *DoveWallet) GetBalances() (balanceResponse BalancesResponse, err error) {
r, err := d.client.do("GET", "account/getbalances", requestParams{}, true)
if err != nil {
return
}
err = json.Unmarshal(r, &balanceResponse)
return
}
func (d *DoveWallet) GetOrderHistory(market string, walletId int64, count int, startAt *time.Time) (orderHistoryResponse OrderHistoryResponse, err error) {
reqParams := requestParams{}
if startAt != nil {
reqParams.Params = append(reqParams.Params, requestParam{Key: "startat", Value: startAt.Format(TIME_FORMAT)})
}
reqParams.Params = append(reqParams.Params, requestParam{Key: "market", Value: market})
resource := "account/getorderhistory"
r, err := d.client.do("GET", resource, reqParams, true)
if err != nil {
return
}
if err != nil {
return
}
err = json.Unmarshal(r, &orderHistoryResponse)
return
}
func (d *DoveWallet) GetOrder(uuid string) (orderResponse OrderResponse, err error) {
reqParams := requestParams{[]requestParam{{Key: "uuid", Value: uuid}}}
resource := "account/getorder"
r, err := d.client.do("GET", resource, reqParams, true)
if err != nil {
return
}
err = json.Unmarshal(r, &orderResponse)
return
}
func (d *DoveWallet) GetOpenOrders(market string, walletId int64) (orderHistoryResponse OrderHistoryResponse, err error) {
resource := "market/getopenorders"
reqParams := requestParams{[]requestParam{{Key: "market", Value: market}}}
r, err := d.client.do("GET", resource, reqParams, true)
if err != nil {
return
}
err = json.Unmarshal(r, &orderHistoryResponse)
return
}
func (d *DoveWallet) OpenLimitOrder(direction, market string, quantity, rate decimal.Decimal, walletId int64) (limitOrderResponse OrderResponse, err error) {
orderType := "buylimit"
if direction == "SELL" {
orderType = "selllimit"
}
resource := "market/" + orderType
reqParams := requestParams{[]requestParam{
{Key: "market", Value: market},
{Key: "quantity", Value: quantity.String()},
{Key: "rate", Value: rate.String()},
},
}
r, err := d.client.do("GET", resource, reqParams, true)
if err != nil {
return
}
err = json.Unmarshal(r, &limitOrderResponse) /**/
return
}
func (rp requestParams) Len() int {
return len(rp.Params)
}
func (rp requestParams) Swap(i, j int) {
rp.Params[i], rp.Params[j] = rp.Params[j], rp.Params[i]
}
func (rp requestParams) Less(i, j int) bool {
return rp.Params[i].Key < rp.Params[j].Key
}