Swap Tokens

Introduction

Token swaps can be executed through the LBRouter 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 AVAX/USDC and you input USDC; router will then fetch how much AVAX 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 AVAX/USDC and you input AVAX; 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. AVAX/USDT and you input AVAX; 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. AVAX/USDT and you input USDT; router will then fetch how much AVAX to transfer from your wallet and perform swap.

Code Examples

1. Swap 10 USDC for USDT using swapExactTokensForTokens with no intermediate swap paths:

2. Swap 1 AVAX for USDT using swapExactNATIVEForTokens with no intermediate swap paths:

3. Swap USDT to get 10 USDC output using swapTokensForExactTokens that routes through WAVAX. In this example, the first swap occurs through USDT/WAVAX V2.2 pool and the second swap occurs through WAVAX/USDC V1 pool:

Last updated