Swap / Quote / Pair address

Pair(=Gauge) address, getting a quote, executing a swap easily

Like always, the only contract you need to interact is the Vault contract.

Implementation contracts and ABI :

Vault

ABI :

Facet Overview : Louper

SwapHelperFacet1(Swap/Quote) : Scan link

SwapHelperFacet2(Liquidity) : Scan link

1. Getting pair address for two tokens.

  • Pool itself is a Gauge.

  • We DON'T USE WETH. Use address(0) as token address to find native ETH pairs.

function pairFor(address tokenA, address tokenB, bool stable) public view returns (address pair);

address(0) could be returned when the corresponding pair isn't created yet.

2. Quote swap

Note that it is not a 'view' but in a form of 'write function'. so that you should query with staticCall() to get the result, not just call().

Use address(0) for ETH pairs instead of WETH address

struct route {
       address from;
       address to;
       bool stable;
   }
function getAmountsOut(uint256 amountIn, route[] memory routes) external returns (uint256[] memory amounts);

*We support int128 instead of uint256 internally because we accommodate negative calculations. However, there's no need to use negative values in a regular swap, and for interface compatibility, we will accept inputs as uint256 and then convert them internally. Please be aware that inputs exceeding int128 will cause a revert.

3. Execute Swap

  • We support all these 3 interface for the swap.

  • Again, We DON'T USE WETH. Use address(0) as token address to use route including native ETH pairs.

function swapExactTokensForTokens(
        uint256 amountIn,
        uint256 amountOutMin,
        route[] calldata routes,
        address to,
        uint256 deadline
    ) external returns (uint256);
function swapExactETHForTokens(
        uint256 amountOutMin, 
        route[] calldata routes, 
        address to, 
        uint256 deadline)
        external
        payable returns (uint256);
function swapExactTokensForETH(
        uint256 amountIn,
        uint256 amountOutMin,
        route[] calldata routes,
        address to,
        uint256 deadline
    ) external returns (uint256);

Example code

// Only contract you need is the Vault(=Universal Router) contract.
// Get Quote for A->ETH,
// Add ETH-A liquidity
// swap ETH->A
amountOut = (await vault.callStatic.getAmountsOut(amountIn, [{from: tokenAaddress, to: address(0), stable: false}]))[1]
vault.addLiquidityETH(tokenAaddress, false, amountWOB, 0, amountETH, 0, 0xTO_ADDRESS, DEADLINE)
vault.swapExactETHForTokens(0, [{from: address(0), to: tokenAaddress, stable: false}], 0xTO_ADDRESS, DEADLINE, {value: AMOUNT_ETH})

*To speed up the search, liquidity size below $100 is excluded from the path search in our swap Frontend. So if you want to test swap with a mock token in our frontend, please create an LP of at least 100($50+50) before swapping.

Last updated