listPortfolios function

Future<List<Portfolio>> listPortfolios({
  1. Client? client,
  2. required Credential credential,
  3. bool isSandbox = false,
})

Gets a list of portfolios for the current user.

GET /api/v3/brokerage/portfolios https://docs.cdp.coinbase.com/api-reference/advanced-trade-api/rest-api/portfolios/list-portfolios

This function makes a GET request to the /portfolios endpoint of the Coinbase Advanced Trade API.

credential - The user's API credentials. isSandbox - Whether to use the sandbox environment.

Returns a list of Portfolio objects.

Implementation

Future<List<Portfolio>> listPortfolios(
    {http.Client? client,
    required Credential credential,
    bool isSandbox = false}) async {
  List<Portfolio> portfolios = [];

  http.Response response = await getAuthorized('/portfolios',
      client: client, credential: credential, isSandbox: isSandbox);

  if (response.statusCode == 200) {
    String data = response.body;
    var jsonResponse = jsonDecode(data);
    var jsonPortfolios = jsonResponse['portfolios'];

    for (var jsonObject in jsonPortfolios) {
      portfolios.add(Portfolio.fromCBJson(jsonObject));
    }
  } else {
    throw CoinbaseException(
        'Failed to list portfolios', response.statusCode, response.body);
  }

  return portfolios;
}