Skip to content

Commit

Permalink
Coinbase wrapper coherence continues
Browse files Browse the repository at this point in the history
  • Loading branch information
cranktakular committed Nov 28, 2023
1 parent 3560599 commit 4593828
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 34 deletions.
41 changes: 37 additions & 4 deletions exchanges/coinbasepro/coinbasepro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2650,18 +2650,51 @@ func TestSetup(t *testing.T) {
}
}

func TestWrapperStart(t *testing.T) {
wg := sync.WaitGroup{}
err := c.Start(context.Background(), &wg)
if err != nil {
t.Error(err)
}
}

func TestFetchTradablePairs(t *testing.T) {
_, err := c.FetchTradablePairs(context.Background(), asset.Spot)
sharedtestvalues.SkipTestIfCredentialsUnset(t, c)
_, err := c.FetchTradablePairs(context.Background(), asset.Empty)
if !errors.Is(err, asset.ErrNotSupported) {
t.Errorf(errExpectMismatch, err, asset.ErrNotSupported)
}
_, err = c.FetchTradablePairs(context.Background(), asset.Spot)
if err != nil {
t.Error(err)
}
_, err = c.FetchTradablePairs(context.Background(), asset.Futures)
if err != nil {
t.Error(err)
}
_, err = c.FetchTradablePairs(context.Background(), asset.Empty)
if !errors.Is(err, asset.ErrNotSupported) {
t.Errorf(errExpectMismatch, err, asset.ErrNotSupported)
}

func TestUpdateTradablePairs(t *testing.T) {
sharedtestvalues.SkipTestIfCredentialsUnset(t, c)
err := c.UpdateTradablePairs(context.Background(), false)
if err != nil {
t.Error(err)
}
}

func TestUpdateAccountInfo(t *testing.T) {
sharedtestvalues.SkipTestIfCredentialsUnset(t, c)
_, err := c.UpdateAccountInfo(context.Background(), asset.Spot)
if err != nil {
t.Error(err)
}
}

func TestFetchAccountInfo(t *testing.T) {
sharedtestvalues.SkipTestIfCredentialsUnset(t, c)
_, err := c.FetchAccountInfo(context.Background(), asset.Spot)
if err != nil {
t.Error(err)
}
}

Expand Down
73 changes: 43 additions & 30 deletions exchanges/coinbasepro/coinbasepro_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,39 +283,52 @@ func (c *CoinbasePro) UpdateTradablePairs(ctx context.Context, forceUpdate bool)
// UpdateAccountInfo retrieves balances for all enabled currencies for the
// coinbasepro exchange
func (c *CoinbasePro) UpdateAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) {
var response account.Holdings
var (
response account.Holdings
accountBalance []Account
done bool
err error
cursor string
)
response.Exchange = c.Name
// accountBalance, err := c.GetAccounts(ctx)
// if err != nil {
// return response, err
// }

// accountCurrencies := make(map[string][]account.Balance)
// for i := range accountBalance {
// profileID := accountBalance[i].ProfileID
// currencies := accountCurrencies[profileID]
// accountCurrencies[profileID] = append(currencies, account.Balance{
// Currency: currency.NewCode(accountBalance[i].Currency),
// Total: accountBalance[i].Balance,
// Hold: accountBalance[i].Hold,
// Free: accountBalance[i].Available,
// AvailableWithoutBorrow: accountBalance[i].Available - accountBalance[i].FundedAmount,
// Borrowed: accountBalance[i].FundedAmount,
// })
// }

// if response.Accounts, err = account.CollectBalances(accountCurrencies, assetType); err != nil {
// return account.Holdings{}, err
// }
for !done {
accountResp, err := c.GetAllAccounts(ctx, 250, cursor)
if err != nil {
return response, err
}
accountBalance = append(accountBalance, accountResp.Accounts...)
done = !accountResp.HasNext
cursor = accountResp.Cursor
}

accountCurrencies := make(map[string][]account.Balance)
for i := range accountBalance {
profileID := accountBalance[i].UUID
currencies := accountCurrencies[profileID]
accountCurrencies[profileID] = append(currencies, account.Balance{
Currency: currency.NewCode(accountBalance[i].Currency),
Total: accountBalance[i].AvailableBalance.Value,
Hold: accountBalance[i].Hold.Value,
Free: accountBalance[i].AvailableBalance.Value -
accountBalance[i].Hold.Value,
AvailableWithoutBorrow: accountBalance[i].AvailableBalance.Value,
Borrowed: 0,
})
}

if response.Accounts, err = account.CollectBalances(accountCurrencies, assetType); err != nil {
return account.Holdings{}, err
}

// creds, err := c.GetCredentials(ctx)
// if err != nil {
// return account.Holdings{}, err
// }
// err = account.Process(&response, creds)
// if err != nil {
// return account.Holdings{}, err
// }
creds, err := c.GetCredentials(ctx)
if err != nil {
return account.Holdings{}, err
}
err = account.Process(&response, creds)
if err != nil {
return account.Holdings{}, err
}

return response, nil
}
Expand Down

0 comments on commit 4593828

Please sign in to comment.