placeMarketOrder method

Future<TransactionBlock> placeMarketOrder({
  1. required String accountCap,
  2. required String poolId,
  3. required int quantity,
  4. required OrderType orderType,
  5. String? baseCoin,
  6. String? quoteCoin,
  7. int? clientOrderId,
  8. String? recipientAddress,
  9. TransactionBlock? txb,
})

Place a market order.

poolId Object id of pool, created after invoking createPool.

quantity Amount of quote asset to swap in base asset.

orderType bid for buying base with quote, ask for selling base for quote.

baseCoin the objectId or the coin object of the base coin.

quoteCoin the objectId or the coin object of the quote coin.

clientOrderId a client side defined order id for bookkeeping purpose. eg: "1" , "2", ... If omitted, the sdk will assign an increasing number starting from 0. But this number might be duplicated if you are using multiple sdk instances.

recipientAddress the address to receive the swapped asset. If omitted, this.currentAddress will be used. The function

Implementation

/// assign an increasing number starting from 0. But this number might be duplicated if you are using multiple sdk instances.
///
/// [recipientAddress] the address to receive the swapped asset. If omitted, `this.currentAddress` will be used. The function
	Future<TransactionBlock> placeMarketOrder({
		required String accountCap,
		required String poolId,
		required int quantity,
		required OrderType orderType,
		String? baseCoin,
		String? quoteCoin,
		int? clientOrderId,
		String? recipientAddress,
		TransactionBlock? txb
	}) async {
  recipientAddress ??= currentAddress;
  txb ??= TransactionBlock();
		final [baseAssetType, quoteAssetType] = await getPoolTypeArgs(poolId);
		if (baseCoin == null && orderType == OrderType.ask) {
			throw ArgumentError('Must specify a valid base coin for an ask order');
		} else if (quoteCoin == null && orderType == OrderType.bid) {
			throw ArgumentError('Must specify a valid quote coin for a bid order');
		}
		final emptyCoin = txb.moveCall(
			"0x2::coin::zero",
			typeArguments: [baseCoin != null ? quoteAssetType : baseAssetType],
			arguments: [],
		);

		final resp = txb.moveCall(
			"$PACKAGE_ID::$MODULE_CLOB::place_market_order",
			typeArguments: [baseAssetType, quoteAssetType],
			arguments: [
				txb.object(poolId),
      txb.object(_checkAccountCap(accountCap)),
				txb.pureInt(clientOrderId ?? _nextClientOrderId()),
      txb.pureInt(quantity),
				txb.pureBool(orderType == OrderType.bid),
				baseCoin != null ? txb.object(baseCoin) : emptyCoin,
				quoteCoin != null ? txb.object(quoteCoin) : emptyCoin,
				txb.object(SUI_CLOCK_OBJECT_ID),
			],
		);
  final base_coin_ret = resp[0];
  final quote_coin_ret = resp[1];
		final recipient = _checkAddress(recipientAddress);
		txb.transferObjects([base_coin_ret], txb.pureAddress(recipient));
		txb.transferObjects([quote_coin_ret], txb.pureAddress(recipient));

		return txb;
	}