getOrders function

Future<List<Order>> getOrders({
  1. int? limit = 1000,
  2. String? cursor,
  3. Client? client,
  4. required Credential credential,
  5. bool isSandbox = false,
})

Gets a list of historical orders for the current user.

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

This function makes a GET request to the /orders/historical/batch endpoint of the Coinbase Advanced Trade API. It supports pagination using a cursor.

limit - A limit on the number of orders to be returned. cursor - A cursor for pagination. credential - The user's API credentials. isSandbox - Whether to use the sandbox environment.

Returns a list of Order objects.

Implementation

Future<List<Order>> getOrders(
    {int? limit = 1000,
    String? cursor,
    http.Client? client,
    required Credential credential,
    bool isSandbox = false}) async {
  List<Order> orders = [];
  Map<String, dynamic>? queryParameters = {'limit': '$limit'};
  (cursor != null) ? queryParameters.addAll({'cursor': cursor}) : null;

  http.Response response = await getAuthorized('/orders/historical/batch',
      queryParameters: queryParameters,
      client: client,
      credential: credential,
      isSandbox: isSandbox);

  if (response.statusCode == 200) {
    String data = response.body;
    var jsonResponse = jsonDecode(data);
    var jsonAccounts = jsonResponse['orders'];
    String? jsonCursor = jsonResponse['cursor'];

    for (var jsonObject in jsonAccounts) {
      orders.add(Order.fromCBJson(jsonObject));
    }
    // Recursive Break
    if (jsonCursor != null && jsonCursor != '') {
      // Recursive Call
      List<Order> paginatedAccounts = await getOrders(
          limit: limit,
          cursor: jsonCursor,
          credential: credential,
          isSandbox: isSandbox);
      orders.addAll(paginatedAccounts);
    }
  } else {
    throw CoinbaseException(
        'Failed to get orders', response.statusCode, response.body);
  }

  return orders;
}