createMarketOrder function

Future<Map<String, dynamic>?> createMarketOrder({
  1. required String clientOrderId,
  2. required String productId,
  3. required String side,
  4. String? quoteSize,
  5. String? baseSize,
  6. required Credential credential,
  7. bool isSandbox = false,
  8. Client? client,
})

Creates a market order. IOC: Immediate or Cancel. Buy or sell a specified quantity of an Asset at the current best available market price.

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

clientOrderId - A unique ID for the order, generated by the client. productId - The ID of the product to trade. side - The side of the order (BUY or SELL). quoteSize - The amount of quote currency to spend on a BUY order, or the amount of base currency to sell on a SELL order. baseSize - The amount of base currency to buy on a BUY order, or the amount of quote currency to receive on a SELL order. credential - The user's API credentials. isSandbox - Whether to use the sandbox environment.

Returns a map containing the result of the order creation, or null if the request fails.

Implementation

Future<Map<String, dynamic>?> createMarketOrder(
    {required String clientOrderId,
    required String productId,
    required String side,
    String? quoteSize,
    String? baseSize,
    required Credential credential,
    bool isSandbox = false,
    Client? client}) async {
  if (quoteSize == null && baseSize == null) {
    throw ArgumentError('Either quoteSize or baseSize must be provided.');
  }
  if (quoteSize != null && baseSize != null) {
    throw ArgumentError('Only one of quoteSize or baseSize can be provided.');
  }

  // Market IOC: Buy or sell a specified quantity of an Asset at the current best available market price.
  Map<String, dynamic>? marketMarketIOC = {};

  (quoteSize != null)
      ? marketMarketIOC.addAll({'quote_size': quoteSize})
      : null;
  (baseSize != null) ? marketMarketIOC.addAll({'base_size': baseSize}) : null;

  final orderConfiguration = {'market_market_ioc': marketMarketIOC};

  return _createOrder(
      clientOrderId: clientOrderId,
      productId: productId,
      side: side,
      orderConfiguration: orderConfiguration,
      credential: credential,
      isSandbox: isSandbox,
      client: client);
}