Skip to main content

3. Querying Expected Output Amount

There is a view function that calculates the amountOut you will receive when you input an amountIn into a book. Alternatively, you can refer to our off-chain TypeScript implementation available in the SDK for additional guidance.

Below is the interface for getExpectedOutput. If you want to allow unlimited slippage, simply set the limitPrice parameter to 0.


/**
* @notice Returns the expected output for a spend order
* @param book_id The id of the book to take from
* @param limit_price The maximum price to take at
* @param base_amount The amount of base asset to spend
* @param min_quote_amount The minimum amount of quote asset to receive
* @param hook_data Additional data to pass to the hook
*/
fn get_expected_output(
self: @TContractState,
book_id: felt252,
limit_price: u256,
base_amount: u256,
min_quote_amount: u256,
hook_data: Span<felt252>,
) -> (u256, u256);

Books are always matched from higher to lower prices. This means that, for an ETH/USDC bid book, once the price stored in the contract is converted into a human-readable value, the order remains the same. However, for an ask book, the order is reversed.

Below is an example TypeScript implementation of getExpectedOutput: view.ts#L936