Calculate a route

To calculate a route for a swap, assuming we have the following input parameters.

For example, when swapping 1 ETH for 2500 USDC

  • Input token - ETH or WETH

  • Output token - USDC

  • Input amount - 1 ETH (or 1e18 in uint)

Get candidate pools

The first step is to look up candidate pools, assuming we use the direct route.

Call getPool() function in the Pool Master contract with encoded input/output tokens and pool types.

Different protocol versions have different pool master contracts, and need to be requested individually.

Get pool info and filtering

Once we got candidate pools, we could call pool functions to get pool info, including pool reservers, and pool's amplification coefficient, and fees (from fee manager).

Calculate a swap

The next step is to calculate a swap using the pool info, and input parameters. Applying different pool algorithms (xy=k for Classic, or hybrid algorithm of Stable and Aqua Pools).

We will get an output result - whether the swap can be handled by the pool, and the output amount.

Last updated