Swapping - Fixed Input
SwapTokensForExactTokens - Useful when the user wants a fixed output amount and a variable input amount. You’ll get a quote before executing the swap to ensure you agree with the variable input amount
Decide the desired output AmountOut
Because smart contracts store token balances in raw units, we convert the human amount into on-chain units using the token’s decimals.
Why?
swapTokensForExactTokens guarantees you receive this many token1 units; the contract will calculate how much token0 it needs in return.
Getting a quote for AmountIn
Get a quote from the pair for how much AUSD it would require (including the current purchase fee) to receive the fixed amountOut that was configured.
getAmountsIn returns an array matching the path. In our case the result means:
- 1000.2 AUSD (raw) → 1000 CTK (raw)
So the pair will charge 1000.20 AUSD, which already includes its purchase-fee markup. If you agree with this quote, the next step is to approve the pair to spend up to amountInMax AUSD before submitting the swap.
Approve the allowance
AgoraStableSwapPair cannot pull AUSD from your wallet unless the ERC-20 allowance is set first. We grant the pair permission to spend up to amountInMax AUSD, and—as a best-practice—simulate the transaction before broadcasting it
Swapping tokens
With allowance set, we trigger the atomic swap. The swap call will be swapTokensForExactTokens, and it will take the following args:
Open the hash on SnowTrace to watch it confirm.
Once mined, your wallet balance reflects +1000 CTK, and no more than 1000.20 AUSD was spent.

For the full example, please refer to our GitHub repository.

