Swapping - Fixed Output
If you want to run this example, be sure that you have followed the steps here to configure your Wallet Client.
SwapTokensForExactTokens - Provide a the number of tokens you want your swap to result in and receive the number of tokens you need to send.
You’ll get a quote before executing the swap to ensure you agree with the variable input amount
Calculate the desired output (AmountOut)
For the fixed-output flow we pick how much CTK we want to receive and convert that to raw units with the token’s decimals.
We’ll again be using the getTokenDecimal function function to do this. It’s also important to remember our swapPath example showing that AUSD is token1 in the pair.
Why?
swapTokensForExactTokens guarantees you receive a fixed amount of CTK (token0) units. The contract will calculate how much AUSD (token1) it needs in order to guarantee that amount.
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.
This output means the pair will charge 1000.20 AUSD, which already includes any fees. 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.
This means that we need to 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:
If you want, you can choose to open the returned hash on the Sepolia Explorer and wait for it to process.
No matter what, if the transaction is successfully executed, the destination wallet balance should show +1000 CTK, and no more than 1000.20 AUSD was spent.
For the full example, please refer to our GitHub repository.

