createOrder function

Future<Order?> createOrder({
  1. required String side,
  2. required String productId,
  3. String? profileId,
  4. String? type,
  5. num? size,
  6. num? price,
  7. num? funds,
  8. String? timeInForce,
  9. String? stp,
  10. String? stop,
  11. num? stopPrice,
  12. String? cancelAfter,
  13. bool? postOnly,
  14. String? clientOid,
  15. required Credential credential,
  16. bool isSandbox = false,
})

Creates a new order.

This function makes a POST request to the /orders endpoint of the Coinbase Pro API.

side - The side of the order ('buy' or 'sell'). productId - The product ID to place the order for. profileId - The profile ID to place the order for. type - The type of order ('limit', 'market', or 'stop'). size - The size of the order. price - The price of the order. funds - The amount of funds to use for the order. timeInForce - The time in force for the order. stp - The self-trade prevention flag for the order. stop - The stop type for the order. stopPrice - The stop price for the order. cancelAfter - The cancel after time for the order. postOnly - A flag to indicate that the order should only be a Limit order. clientOid - A client-supplied order ID. credential - The user's API credentials. isSandbox - Whether to use the sandbox environment.

Returns an Order object, or null if the order could not be created.

Implementation

Future<Order?> createOrder(
    {required String side,
    required String productId,
    String? profileId,
    String? type,
    num? size,
    num? price,
    num? funds,
    String? timeInForce,
    String? stp,
    String? stop,
    num? stopPrice,
    String? cancelAfter,
    bool? postOnly,
    String? clientOid,
    required Credential credential,
    bool isSandbox = false}) async {
  Order? order;

  Map<String, dynamic>? body = {
    'side': side,
    'product_id': productId,
  };

  (profileId != null) ? body.addAll({'profile_id': profileId}) : null;
  (type != null && _validType(type)) ? body.addAll({'type': type}) : null;
  (size != null) ? body.addAll({'size': size}) : null;
  (price != null) ? body.addAll({'price': price}) : null;
  (funds != null) ? body.addAll({'funds': funds}) : null;
  if (timeInForce != null && _validTIF(timeInForce)) {
    body.addAll({'time_in_force': timeInForce});
  }
  (stp != null && _validSTP(stp)) ? body.addAll({'stp': stp}) : null;
  (stop != null && _validStop(stop)) ? body.addAll({'stop': stop}) : null;
  (stopPrice != null) ? body.addAll({'stop_price': stopPrice}) : null;
  if (cancelAfter != null && _validCA(cancelAfter)) {
    body.addAll({'cancel_after': cancelAfter});
  }
  (postOnly != null) ? body.addAll({'post_only': postOnly}) : null;
  (clientOid != null) ? body.addAll({'client_oid': clientOid}) : null;

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

  if (response.statusCode == 200) {
    String data = response.body;
    var jsonResponse = jsonDecode(data);
    order = Order.fromCBJson(jsonResponse);
  } else {
    var url = response.request?.url.toString();
    print('Request to URL $url failed: Response code ${response.statusCode}');
    print('Error Response Message: ${response.body}');
  }

  return order;
}