In many scenarios, you might want to know how much X of TokenA you'll receive for Y of TokenB. Fortunately, Balancer has query functions to simulate transactions.
If you are using
minAmountsOut, this is ONLY useful if you simulate these calls OUTSIDE of the transaction you end up making. You SHOULD NOT call these functions to calculate limits from a smart contract at transaction time.
There are valid use cases for calling these functions on chain, but do not use them to determine limits.
Calculating these values ahead of time is useful for enforcing slippage tolerances to mitigate losses due to sandwich attacks. If you are querying these values at execution time, you end up getting your values mid-sandwich! This leaves you entirely vulnerable to the attacker's manipulation, and is as foolish as using a 100% slippage tolerance.
To calculate the inputs/outputs for a trade (you can specify given-in or given-out), you will use the
queryBatchSwapfunction in the
Vault. This functionality is important if not crucial for calculating your limits when constructing your
returns (int256 assetDeltas)
returns (uint256 bptOut, uint256 amountsIn)
returns (uint256 bptIn, uint256 amountsOut)