Swap Tokens
Introduction
Token swaps can be executed through the LBRrouter
contract. This contract will abstract some of the complexity of the swap, perform safety checks and will revert if certain conditions were to not be met. The LBRouter
in V2.2 is able to perform swaps through LBPair
V2.2, V2.1, V2.0 and pair contracts from V1.
The rest of the document describes:
Which functions should be called to swap tokens
How to use them in code examples
Swap Functions
Swap functions can be broadly divided into two categories:
When the input amount is specified:
swapExactTokensForTokens
swapExactTokensForNATIVE
swapExactNATIVEForTokens
When the desired output amount is specified:
swapTokensForExactTokens
swapTokensForExactNATIVE
swapNATIVEForExactTokens
When Input Amount Is Specified
swapExactTokensForTokens
- When you specify an exact amount of ERC-20 to swap for another ERC-20. E.g. USDC/USDT and you input USDC; router will then fetch how much USDT to expect as output and perform swap.swapExactTokensForNATIVE
- When you specify an exact amount of ERC-20 to swap for NATIVE tokens. E.g SEI/USDC and you input USDC; router will then fetch how much SEI to expect as output and perform swap.swapExactNATIVEForTokens
- When you specify an exact amount of NATIVE tokens to swap for an ERC-20. E.g SEI/USDC and you input SEI; router will then fetch how much USDC to expect as output and perform swap.
When Output Amount Is Specified
swapTokensForExactTokens
- When you specify exact amount of ERC-20 that you want to receive. E.g. USDC/USDT and you input USDC; router will then fetch how much USDT to transfer from your wallet and perform swap.swapTokensForExactNATIVE
- When you specify exact amount of NATIVE tokens that you want to receive. E.g. SEI/USDT and you input SEI; router will then fetch how much USDT to transfer from your wallet and perform swap.swapNATIVEForExactTokens
- When you specify exact amount of ERC-20 that you want to receive. E.g. SEI/USDT and you input USDT; router will then fetch how much SEI to transfer from your wallet and perform swap.
Code Examples
1. Swap 10 USDC for USDT using swapExactTokensForTokens
with no intermediate swap paths:
swapExactTokensForTokens
with no intermediate swap paths:2. Swap 1 SEI for USDT using swapExactNATIVEForTokens
with no intermediate swap paths:
swapExactNATIVEForTokens
with no intermediate swap paths:3. Swap USDT to get 10 USDC output using swapTokensForExactTokens
that routes through WSEI. In this example, the first swap occurs through USDT/WSEI V2.2 pool and the second swap occurs through WSEI/USDC V1 pool:
swapTokensForExactTokens
that routes through WSEI. In this example, the first swap occurs through USDT/WSEI V2.2 pool and the second swap occurs through WSEI/USDC V1 pool:Last updated