Quote and settle swaps
Quote a pool
Use amounts in each token's smallest unit.
getQuote(tokenOut, tokenIn, amountIn)returns gross output.getAmountOut(tokenOut, tokenIn, amountIn)returns output after both fees.getAmountIn(tokenOut, tokenIn, amountOut)estimates the input for a target net output. The pool rounds up and adds one input unit.
Set RPC_URL to your Gnosis RPC URL. Set POOL_ADDRESS, TOKEN_OUT, TOKEN_IN, and AMOUNT_IN for the swap. Use cast call to get a quote without sending a transaction.
cast call "$POOL_ADDRESS" 'getAmountOut(address,address,uint256)(uint256)' \
"$TOKEN_OUT" "$TOKEN_IN" "$AMOUNT_IN" --rpc-url "$RPC_URL"To swap through more than one pool, see Routing.
Execute with a minimum output and deadline
Approve the pool to spend the input token. Call the six-argument withdraw overload. Give minAmountOut in the output token's smallest unit. Give deadline as a Unix timestamp in seconds.
withdraw(tokenOut, tokenIn, amountIn, recipient, minAmountOut, deadline)The pool measures the input it receives and uses that amount to calculate the output and fees. The call reverts after the deadline. It also reverts if the recipient receives less than minAmountOut. Changes to pool settings or token transfer fees can change the final amount. If the pool lacks liquidity, the swap reverts.
Account for fees and settlement
Rates use parts per million. 10_000 equals 1%. The pool applies its fee rate to gross output. If the protocol fee is active, its base is the greater of the pool fee or 1% of gross output. The pool deducts both fees from gross output. It pays the protocol recipient in the output token.
With 100 tokens of gross output, a 2% pool fee is 2 tokens. A 10% protocol rate on that fee adds 0.2 tokens. The pool sends 97.8 tokens before any token transfer fee.
Use SwapSettlement to reconcile a swap. It records measured input, gross output, the amount sent, the amount received, and both fees. The legacy Swap event reports only the pool fee in fee.