Skip to content

Commit

Permalink
Merge pull request #1 from jferrl/dev/jferrl/earn
Browse files Browse the repository at this point in the history
chore: some new methods in earn service
  • Loading branch information
jferrl authored Sep 23, 2023
2 parents 8e2217e + 2e50795 commit c479f6b
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 0 deletions.
48 changes: 48 additions & 0 deletions earn.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,54 @@ import (
// methods of the Kraken API.
type Earn service

// EarnFundsOpts .
type EarnFundsOpts struct {
Amount string `json:"amount"`
StrategyID string `json:"strategy_id"`
}

// Allocate allocates funds to the Strategy.
// Docs: https://docs.kraken.com/rest/#tag/Earn/operation/allocateStrategy
func (e *Earn) Allocate(ctx context.Context, opts EarnFundsOpts) (bool, error) {
body, err := newJSONBody(opts)
if err != nil {
return false, err
}

req, err := e.client.newPrivateRequest(ctx, http.MethodPost, "Earn/Allocate", body)
if err != nil {
return false, err
}

var v bool
if err := e.client.do(req, &v); err != nil {
return false, err
}

return v, nil
}

// Deallocate de-allocates funds from the Strategy.
// Docs: https://docs.kraken.com/rest/#tag/Earn/operation/deallocateStrategy
func (e *Earn) Deallocate(ctx context.Context, opts EarnFundsOpts) (bool, error) {
body, err := newJSONBody(opts)
if err != nil {
return false, err
}

req, err := e.client.newPrivateRequest(ctx, http.MethodPost, "Earn/Deallocate", body)
if err != nil {
return false, err
}

var v bool
if err := e.client.do(req, &v); err != nil {
return false, err
}

return v, nil
}

// StatusOpts represents the parameters to get the status of the last allocation or deallocation request.
type StatusOpts struct {
StrategyID string `json:"strategy_id,omitempty"`
Expand Down
114 changes: 114 additions & 0 deletions earn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,117 @@ func TestEarn_Allocations(t *testing.T) {
})
}
}

func TestEarn_Allocate(t *testing.T) {
type fields struct {
apiMock *httptest.Server
}
type args struct {
ctx context.Context
opts EarnFundsOpts
}
tests := []struct {
name string
fields fields
args args
want bool
wantErr bool
}{
{
name: "error building request",
fields: fields{
apiMock: createFakeServer(http.StatusOK, ""),
},
args: args{},
wantErr: true,
},
{
name: "allocate",
fields: fields{
apiMock: createFakeServer(http.StatusOK, "bool_result.json"),
},
args: args{
ctx: context.Background(),
opts: EarnFundsOpts{
Amount: "0.01",
StrategyID: "ESRFUO3-Q62XD-WIOIL7",
},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
baseURL, _ := url.Parse(tt.fields.apiMock.URL + "/")

c := New(tt.fields.apiMock.Client())
c.baseURL = baseURL

got, err := c.Earn.Allocate(tt.args.ctx, tt.args.opts)
if (err != nil) != tt.wantErr {
t.Errorf("Earn.Allocate() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Earn.Allocate() = %v, want %v", got, tt.want)
}
})
}
}

func TestEarn_Deallocate(t *testing.T) {
type fields struct {
apiMock *httptest.Server
}
type args struct {
ctx context.Context
opts EarnFundsOpts
}
tests := []struct {
name string
fields fields
args args
want bool
wantErr bool
}{
{
name: "error building request",
fields: fields{
apiMock: createFakeServer(http.StatusOK, ""),
},
args: args{},
wantErr: true,
},
{
name: "deallocate",
fields: fields{
apiMock: createFakeServer(http.StatusOK, "bool_result.json"),
},
args: args{
ctx: context.Background(),
opts: EarnFundsOpts{
Amount: "0.01",
StrategyID: "ESRFUO3-Q62XD-WIOIL7",
},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
baseURL, _ := url.Parse(tt.fields.apiMock.URL + "/")

c := New(tt.fields.apiMock.Client())
c.baseURL = baseURL

got, err := c.Earn.Deallocate(tt.args.ctx, tt.args.opts)
if (err != nil) != tt.wantErr {
t.Errorf("Earn.Deallocate() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Earn.Deallocate() = %v, want %v", got, tt.want)
}
})
}
}
4 changes: 4 additions & 0 deletions testdata/bool_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"error": [ ],
"result": true
}

0 comments on commit c479f6b

Please sign in to comment.