-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubaccounts.go
88 lines (71 loc) · 2.24 KB
/
subaccounts.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
package kraken
import (
"context"
"errors"
"net/http"
"github.com/google/go-querystring/query"
)
// Subaccounts handles communication with the Subaccounts related
// methods of the Kraken API.
type Subaccounts service
// CreateSubaccountOpts represents the parameters to create a Subaccount.
type CreateSubaccountOpts struct {
Username string `url:"username,omitempty"`
Email string `url:"email,omitempty"`
}
// Valid returns true if the CreateSubaccountOpts is valid.
func (o CreateSubaccountOpts) Valid() bool {
return o.Username != "" && o.Email != ""
}
// Create creates a new trading subaccount.
// Docs: https://docs.kraken.com/rest/#tag/Subaccounts/operation/createSubaccount.
func (s *Subaccounts) Create(ctx context.Context, opts CreateSubaccountOpts) (bool, error) {
if !opts.Valid() {
return false, errors.New("invalid options")
}
body, err := query.Values(opts)
if err != nil {
return false, err
}
req, err := s.client.newPrivateRequest(ctx, http.MethodPost, "CreateSubaccount", newFormURLEncodedBody(body))
if err != nil {
return false, err
}
var v bool
if err := s.client.do(req, &v); err != nil {
return false, err
}
return v, nil
}
// TransferOpts represents the parameters to transfer funds.
type TransferOpts struct {
Asset string `url:"asset,omitempty"`
Amount string `url:"amount,omitempty"`
From string `url:"from,omitempty"`
To string `url:"to,omitempty"`
}
// Valid returns true if the TransferOpts is valid.
func (o TransferOpts) Valid() bool {
return o.Asset != "" && o.Amount != "" && o.From != "" && o.To != ""
}
// Transfer transfers funds to and from master and subaccounts.
// Note: AccountTransfer must be called by the master account.
// Docs: https://docs.kraken.com/rest/#tag/Subaccounts/operation/accountTransfer
func (s *Subaccounts) Transfer(ctx context.Context, opts TransferOpts) (*TransferResult, error) {
if !opts.Valid() {
return nil, errors.New("invalid options")
}
body, err := query.Values(opts)
if err != nil {
return nil, err
}
req, err := s.client.newPrivateRequest(ctx, http.MethodPost, "AccountTransfer", newFormURLEncodedBody(body))
if err != nil {
return nil, err
}
var v TransferResult
if err := s.client.do(req, &v); err != nil {
return nil, err
}
return &v, nil
}