createPortfolio function

Future<Portfolio?> createPortfolio({
  1. required String name,
  2. Client? client,
  3. required Credential credential,
  4. bool isSandbox = false,
})

Creates a new portfolio.

POST /api/v3/brokerage/portfolios https://docs.cdp.coinbase.com/api-reference/advanced-trade-api/rest-api/portfolios/create-portfolio

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

name - The name of the portfolio to be created. credential - The user's API credentials. isSandbox - Whether to use the sandbox environment.

Returns a Portfolio object.

Implementation

Future<Portfolio?> createPortfolio(
    {required String name,
    http.Client? client,
    required Credential credential,
    bool isSandbox = false}) async {
  Map<String, dynamic> body = {'name': name};

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

  if (response.statusCode == 200) {
    var jsonResponse = jsonDecode(response.body);
    return Portfolio.fromCBJson(jsonResponse['portfolio']);
  } else {
    var url = response.request?.url.toString();
    print('Request to URL $url failed: Response code ${response.statusCode}');
    print('Error Response Message: ${response.body}');
  }
  return null;
}