swapExactBaseForQuote method
Swap exact base for quote.
poolId Object id of pool, created after invoking createPool, eg: "0xcaee8e1c046b58e55196105f1436a2337dcaa0c340a7a8c8baf65e4afb8823a4"
tokenObjectIn Object id of the token to swap: eg: "0x6e566fec4c388eeb78a7dab832c9f0212eb2ac7e8699500e203def5b41b9c70d"
amountIn amount of token to buy or sell, eg: 10000000
currentAddress current user address, eg: "0xbddc9d4961b46a130c2e1f38585bbc6fa8077ce54bcb206b26874ac08d607966"
clientOrderId a client side defined order number for bookkeeping purpose. eg: "1" , "2", ...
Implementation
///
/// [poolId] Object id of pool, created after invoking createPool, eg: "0xcaee8e1c046b58e55196105f1436a2337dcaa0c340a7a8c8baf65e4afb8823a4"
///
/// [tokenObjectIn] Object id of the token to swap: eg: "0x6e566fec4c388eeb78a7dab832c9f0212eb2ac7e8699500e203def5b41b9c70d"
///
/// [amountIn] amount of token to buy or sell, eg: 10000000
///
/// [currentAddress] current user address, eg: "0xbddc9d4961b46a130c2e1f38585bbc6fa8077ce54bcb206b26874ac08d607966"
///
/// [clientOrderId] a client side defined order number for bookkeeping purpose. eg: "1" , "2", ...
Future<TransactionBlock> swapExactBaseForQuote({
required String poolId,
required TransactionObjectInput tokenObjectIn,
required int amountIn,
required String currentAddress,
int? clientOrderId,
TransactionBlock? txb
}) async {
txb ??= TransactionBlock();
final [baseAsset, quoteAsset] = await getPoolTypeArgs(poolId);
// in this case, we assume that the tokenIn--tokenOut always exists.
final resp = txb.moveCall(
"$PACKAGE_ID::$MODULE_CLOB::swap_exact_base_for_quote",
typeArguments: [baseAsset, quoteAsset],
arguments: [
txb.object(poolId),
txb.pureInt(clientOrderId ?? _nextClientOrderId()),
txb.object(_checkAccountCap()),
txb.pureInt(amountIn),
tokenObjectIn is String ? txb.object(tokenObjectIn) : tokenObjectIn,
txb.moveCall(
"0x2::coin::zero",
typeArguments: [quoteAsset],
arguments: [],
),
txb.object(SUI_CLOCK_OBJECT_ID),
],
);
final base_coin_ret = resp[0];
final quote_coin_ret = resp[1];
txb.transferObjects([base_coin_ret], txb.pureAddress(currentAddress));
txb.transferObjects([quote_coin_ret], txb.pureAddress(currentAddress));
return txb;
}