Executing a Swap
The following pages will walk you through how to execute both a fixed-input and fixed-output swap on Ethereum’s testnet Sepolia. They’ll consistently exchange AUSD for CTK since this is our main pair on testnets. You can also adapt the scripts to do the inverse for testing purposes.
Prerequisites
Wallet Client and Getting Gas
Just like all of the other guides, be sure that you have followed the steps here to configure your Wallet Client. If this has been done correctly you should be able to import your client at the top of your script:
You also need to make sure that your wallet does have Sepolia ETH for any gas fees on testnet. The instructions for using a faucet to get a small amount of Sepolia are also found in our quick-start guide.
Testnet AUSD
In order to be able to initiate a swap you must have at least one of the Tokens that you’re swapping. Since our examples show swapping from AUSD to CTK, you need to have AUSD in your wallet at the start. You can also choose to get some of both if you want.
Instructions for getting testnet AUSD are in our Getting Testnet Tokens guide.
Whitelisted Wallet
The AgoraStableSwapPair is permissioned, meaning that only approved wallets may call its swap functions.
Before spending gas, make sure that your address has the APPROVED_SWAPPER role. If it does not, the swap will not be executed.
Checking your wallet’s role
To check whether the wallet is whitelisted, we’ll call hasRole on the contract with the role identifier (APPROVED_SWAPPER ) and the wallet address.
If running this returns true then you’re all set!
Self-whitelisting
Testnet only
This self-service whitelisting is only available on testnets. On mainnet, whitelisting requires completing KYC verification through Agora.
On testnets, Agora deploys a Whitelister contract that holds the WHITELISTER_ROLE on all pairs. This allows any user to whitelist themselves by calling setApprovedSwapper.
The Whitelister contract address is the same on all testnets:
See Protocol Deployments for the full list of testnet addresses.
Call the setApprovedSwapper function with your wallet address to grant yourself the APPROVED_SWAPPER role on all testnet pairs:
Helper Function:
Using this helper function:
Output:
Once whitelisted, you can proceed to execute swaps on any testnet pair.
Fetch token decimals
In order to calculate the swap amounts correctly, you need to know the decimal precision of both tokens in the pair. The AgoraStableSwapPair contract exposes the decimals for both tokens through getter functions:
Define a swapPath
An important step in swapping using our pair contract is making sure that you determine a swapPath. This essentially means that you clearly define which token you are inputting and what token will be output.
This keeps things clear so that you make sure you don’t accidentally swap in the wrong direction.
- Setting a
swapPathof[token0, token1]means you will swap token0 for token1 - Setting a
swapPathof[token1, token0]means you will swap token1 for token0
Finding token order in the pair
How do I know what token is token0 and which is token1 in an Agora pair?
Each Agora pair has a name function. The name returned is purposefully configured to show the order in the pair.
The above example will return a name of “CTK/AUSD”. With that name, it means CTK is token0 and AUSD is token1.
Example swapPath
Because you funded your wallet with AUSD earlier (see Getting Testnet Tokens), we’ll set our swapPathto swap AUSD for CTK:

