Skip to content

Commit

Permalink
Add functions + tests for handling coinbasepro profiles aka portfolios (
Browse files Browse the repository at this point in the history
  • Loading branch information
sknr authored Apr 18, 2021
1 parent f2b4b86 commit 67d3834
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
49 changes: 49 additions & 0 deletions profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package coinbasepro

import (
"fmt"
)

type Profile struct {
ID string `json:"id"`
UserID string `json:"user_id"`
Name string `json:"name"`
Active bool `json:"active"`
IsDefault bool `json:"is_default"`
CreatedAt Time `json:"created_at,string"`
}

type ProfileTransfer struct {
From string `json:"from"`
To string `json:"to"`
Currency string `json:"currency"`
Amount string `json:"amount"`
}

// Client Funcs

// GetProfiles retrieves a list of profiles
func (c *Client) GetProfiles() ([]Profile, error) {
var profiles []Profile

url := fmt.Sprintf("/profiles")
_, err := c.Request("GET", url, nil, &profiles)
return profiles, err
}

// GetProfile retrieves a single profile
func (c *Client) GetProfile(id string) (Profile, error) {
var profile Profile

url := fmt.Sprintf("/profiles/%s", id)
_, err := c.Request("GET", url, nil, &profile)
return profile, err
}

// CreateProfileTransfer transfers a currency amount from one profile to another
func (c *Client) CreateProfileTransfer(newTransfer *ProfileTransfer) error {
url := fmt.Sprintf("/profiles/transfer")
_, err := c.Request("POST", url, newTransfer, nil)

return err
}
76 changes: 76 additions & 0 deletions profile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package coinbasepro

import (
"errors"
"fmt"
"testing"
)

func TestGetProfiles(t *testing.T) {
client := NewTestClient()
profiles, err := client.GetProfiles()
if err != nil {
t.Error(err)
}

for _, p := range profiles {
if p.ID == "" {
t.Error(errors.New("profile id missing"))
}
}
}

func TestGetProfile(t *testing.T) {
client := NewTestClient()
profiles, err := client.GetProfiles()
if err != nil {
t.Error(err)
}

profile, err := client.GetProfile(profiles[0].ID)
if err != nil {
t.Error(err)
}

if profile.ID == "" {
t.Error(errors.New("profile id missing"))
}
}

func TestCreateProfileTransfer(t *testing.T) {
client := NewTestClient()
profiles, err := client.GetProfiles()
if err != nil {
t.Error(err)
}

var fromProfile, toProfile Profile
for _, profile := range profiles {
if profile.IsDefault {
fromProfile = profile
continue
}

if profile.Active {
toProfile = profile
break
}
}

if toProfile.ID == "" {
t.Skip(fmt.Sprintf("needed at least two active profiles for this test"))
}

// Send from first profile to second profile
newTransfer := ProfileTransfer{
From: fromProfile.ID,
To: toProfile.ID,
Currency: "USD",
Amount: "9.99",
}

err = client.CreateProfileTransfer(&newTransfer)
if err != nil {
t.Error(err)
}
}

2 comments on commit 67d3834

@sknr
Copy link
Contributor Author

@sknr sknr commented on 67d3834 May 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Philip,
I was wondering if I did break the last build with my commit or might there be a problem with the authentication? Because normally there should always exist at least one profile (default profile).

@paperspace-philip
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's an issue with travis-ci. travis-ci is shutting down so I'm going to migrate these tests to github actions.

Please sign in to comment.