From 4593828f68e34db1e73ee0ac10cb8897c26cff2b Mon Sep 17 00:00:00 2001 From: Samuel Reid <43227667+cranktakular@users.noreply.github.com> Date: Tue, 28 Nov 2023 16:45:21 +1100 Subject: [PATCH] Coinbase wrapper coherence continues --- exchanges/coinbasepro/coinbasepro_test.go | 41 +++++++++-- exchanges/coinbasepro/coinbasepro_wrapper.go | 73 ++++++++++++-------- 2 files changed, 80 insertions(+), 34 deletions(-) diff --git a/exchanges/coinbasepro/coinbasepro_test.go b/exchanges/coinbasepro/coinbasepro_test.go index 6d70c36c38d..e17bb24fd52 100644 --- a/exchanges/coinbasepro/coinbasepro_test.go +++ b/exchanges/coinbasepro/coinbasepro_test.go @@ -2650,8 +2650,21 @@ 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) } @@ -2659,9 +2672,29 @@ func TestFetchTradablePairs(t *testing.T) { 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) } } diff --git a/exchanges/coinbasepro/coinbasepro_wrapper.go b/exchanges/coinbasepro/coinbasepro_wrapper.go index 03b10a5f298..ba11926b859 100644 --- a/exchanges/coinbasepro/coinbasepro_wrapper.go +++ b/exchanges/coinbasepro/coinbasepro_wrapper.go @@ -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 }