-
Notifications
You must be signed in to change notification settings - Fork 2
/
accounts.go
221 lines (178 loc) · 5.18 KB
/
accounts.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package cointip
import (
"encoding/json"
"fmt"
"net/http"
)
const (
CurrencyUSD = "USD"
CurrencyBTC = "BTC"
)
type Balance struct {
Amount float64 `json:"amount,string"`
Currency string `json:"currency"`
}
type Account struct {
ID string `json:"id"`
Name string `json:"name"`
Currency string `json:"currency"`
Balance Balance `json:"balance"` // Amount in Cryptocurrency
NativeBalance Balance `json:"native_balance"` // Amount in USD
}
type Transaction struct {
ID string `json:"id"`
Type string `json:"type"`
Status string `json:"status"`
Amount Balance `json:"amount"` // Amount in Cryptocurrency
NativeAmount Balance `json:"native_amount"` // Amount in USD
Description string `json:"description"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
type Address struct {
ID string `json:"id"`
Address string `json:"address"`
Name string `json:"name"`
Network string `json:"network"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
func (c *ApiKeyClient) ListAccounts() ([]*Account, error) {
code, body, err := c.Request("GET", "accounts", nil)
if err != nil {
return nil, err
}
if code != http.StatusOK {
return nil, fmt.Errorf("unexpected status code %d", code)
}
accounts := []*Account{}
err = json.Unmarshal(body, &accounts)
if err != nil {
return nil, err
}
return accounts, nil
}
func (c *ApiKeyClient) GetAccount(id string) (*Account, error) {
code, body, err := c.Request("GET", fmt.Sprintf("accounts/%s", id), nil)
if err != nil {
return nil, err
}
if code != http.StatusOK {
return nil, fmt.Errorf("unexpected status code %d", code)
}
account := &Account{}
err = json.Unmarshal(body, account)
if err != nil {
return nil, err
}
return account, nil
}
func (c *ApiKeyClient) CreateAccount(Name string) (*Account, error) {
code, body, err := c.Request("POST", "accounts", map[string]string{"name": Name})
if err != nil {
return nil, err
}
if code != http.StatusCreated {
return nil, fmt.Errorf("unexpected status code %d", code)
}
account := &Account{}
err = json.Unmarshal(body, account)
if err != nil {
return nil, err
}
return account, nil
}
func (c *ApiKeyClient) DeleteAccount(id string) error {
code, _, err := c.Request("DELETE", fmt.Sprintf("accounts/%s", id), nil)
if err != nil {
return err
}
if code != http.StatusNoContent {
return fmt.Errorf("unexpected status code %d", code)
}
return nil
}
// CreateAddress creates an address for the given account id, letting users deposit funds.
func (c *ApiKeyClient) CreateAddress(id string) (*Address, error) {
code, body, err := c.Request("POST", fmt.Sprintf("accounts/%s/addresses", id), nil)
if err != nil {
return nil, err
}
if code != http.StatusCreated {
return nil, fmt.Errorf("unexpected status code %d", code)
}
addr := &Address{}
err = json.Unmarshal(body, addr)
if err != nil {
return nil, err
}
return addr, nil
}
// Transfer moves funds between accounts. Use this when tipping users.
func (c *ApiKeyClient) Transfer(from, to string, amount *Balance) (*Transaction, error) {
if !(amount.Currency == CurrencyBTC || amount.Currency == CurrencyUSD) {
return nil, fmt.Errorf("invalid currency type: %s", amount.Currency)
}
params := map[string]string{
"type": "transfer",
"to": to,
"amount": fmt.Sprintf("%.8f", amount.Amount),
"currency": amount.Currency,
"description": "cointip transfer",
}
code, body, err := c.Request("POST", fmt.Sprintf("accounts/%s/transactions", from), params)
if err != nil {
return nil, err
}
if code != http.StatusCreated {
return nil, fmt.Errorf("unexpected status code %d", code)
}
tx := &Transaction{}
err = json.Unmarshal(body, tx)
if err != nil {
return nil, err
}
return tx, nil
}
// Withdraw sends funds from an account id to an external address, letting users pull funds from their tipjar.
func (c *ApiKeyClient) Withdraw(from, to string, amount *Balance) (*Transaction, error) {
if !(amount.Currency == CurrencyBTC || amount.Currency == CurrencyUSD) {
return nil, fmt.Errorf("invalid currency type: %s", amount.Currency)
}
params := map[string]string{
"type": "send",
"to": to,
"amount": fmt.Sprintf("%.8f", amount.Amount),
"currency": amount.Currency,
"description": "cointip withdraw",
}
code, body, err := c.Request("POST", fmt.Sprintf("accounts/%s/transactions", from), params)
if err != nil {
return nil, err
}
if code != http.StatusCreated {
return nil, fmt.Errorf("unexpected status code %d", code)
}
tx := &Transaction{}
err = json.Unmarshal(body, tx)
if err != nil {
return nil, err
}
return tx, nil
}
// GetTransaction returns a given transaction by id.
func (c *ApiKeyClient) GetTransaction(id, txID string) (*Transaction, error) {
code, body, err := c.Request("GET", fmt.Sprintf("accounts/%s/transactions/%s", id, txID), nil)
if err != nil {
return nil, err
}
if code != http.StatusOK {
return nil, fmt.Errorf("unexpected status code %d", code)
}
tx := &Transaction{}
err = json.Unmarshal(body, tx)
if err != nil {
return nil, err
}
return tx, nil
}