EcosystemNFTsDocs

Footer

Xchange

  • Swap
  • Liquidity
  • Loans
  • Fund
  • Vote

Tokens

  • X7R
  • X7DAO
  • X7D
  • X7101-X7105

Explore

  • Dashboard
  • Guides
  • Integration
  • Whitepaper
  • Breakdowns
  • Onchains
  • FAQs

Community

  • Telegram
  • Twitter
  • Discord
  • Warpcast
  • Blog
  • Links

Permissionless Trustless Decentralized Exchange

Launch your project on Xchange with 10-1000x the initial liquidity across multiple blockchains where anyone can Swap, Borrow and Lend!

X7 Finance·Trust No One. Trust Code. Long Live DeFi.
  • Telegram
  • Twitter
  • Discordś
  • Warpcast
  • YouTube
  • Reddit
  • GitHub
  • Pioneer 1488 avatar
    Guides
    View
  • Pioneer 1488 avatar
    Integration
    View
  • Pioneer 1488 avatar
    Whitepaper
    View
  • Pioneer 1488 avatar
    Breakdowns
    View
  • Pioneer 1488 avatar
    Onchains
    View
  • Pioneer 1488 avatar
    FAQ
    View
    • Intro

Xchange Router

Because routers are stateless and do not hold token balances, they can be replaced safely and trustlessly, if necessary. This may happen if more efficient smart contract patterns are discovered, or if additional functionality is desired. For this reason, routers have release numbers, starting at V1. This is currently recommended release, V2.

Code

XchangeRouterV2.sol

Address

XchangeRouterV2 is deployed at:

ChainAddress
Ethereum (Mainnet)0x7DE83EB89eE6E5506f3C9221c5371D536d442000 view
Polygon0x7DE83EB89eE6E5506f3C9221c5371D536d442000 view
Arbitrum0x7DE83EB89eE6E5506f3C9221c5371D536d442000 view
Optimism0x7DE83EB89eE6E5506f3C9221c5371D536d442000 view
Base0x7DE83EB89eE6E5506f3C9221c5371D536d442000 view
Binance Smart Chain0x7DE83EB89eE6E5506f3C9221c5371D536d442000 view
ChainRouter init_code
Ethereum (Mainnet)579e9bdec156a1150f17cf9884a4421f309e7e9be6b26dcfbd9a52883418ee21
Polygon-
Arbitrum-
Optimism-
Base-
Binance Smart Chain-

Read-Only Functions

factory function factory() external pure returns (address); Returns factory address.

WETH

function WETH() external pure returns (address); Returns the canonical WETH address on the Ethereum mainnet, appropriate testnets.

quote

See quote.

getAmountOut

See getAmountOut.

getAmountIn

See getAmountIn.

getAmountsOut

function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts); See getAmountsOut.

getAmountsIn

function getAmountsIn(uint amountOut, address[] memory path) public view returns (uint[] memory amounts); See getAmountsIn.

State-Changing Functions

addLiquidity

function addLiquidity( address tokenA, address tokenB, uint amountADesired, uint amountBDesired, uint amountAMin, uint amountBMin, address to, uint deadline ) external returns (uint amountA, uint amountB, uint liquidity);

Adds liquidity to an ERC-20⇄ERC-20 pool.

  • To cover all possible scenarios, msg.sender should have already given the router an allowance of at least amountADesired/amountBDesired on tokenA/tokenB.
  • Always adds assets at the ideal ratio, according to the price when the transaction is executed.
  • If a pool for the passed tokens does not exists, one is created automatically, and exactly amountADesired/amountBDesired tokens are added.
NameTypeNote
tokenAaddressA pool token.
tokenBaddressA pool token.
amountADesireduintThe amount of tokenA to add as liquidity if the B/A price is <= amountBDesired/amountADesired (A depreciates).
amountBDesireduintThe amount of tokenB to add as liquidity if the A/B price is <= amountADesired/amountBDesired (B depreciates).
amountAMinuintBounds the extent to which the B/A price can go up before the transaction reverts. Must be <= amountADesired.
amountBMinuintBounds the extent to which the A/B price can go up before the transaction reverts. Must be <= amountBDesired.
toaddressRecipient of the liquidity tokens.
deadlineuintUnix timestamp after which the transaction will revert.
amountAuintThe amount of tokenA sent to the pool.
amountBuintThe amount of tokenB sent to the pool.
liquidityuintThe amount of liquidity tokens minted.

addLiquidityETH

function addLiquidityETH( address token, uint amountTokenDesired, uint amountTokenMin, uint amountETHMin, address to, uint deadline ) external payable returns (uint amountToken, uint amountETH, uint liquidity);

Adds liquidity to an ERC-20⇄WETH pool with ETH.

  • To cover all possible scenarios, msg.sender should have already given the router an allowance of at least amountTokenDesired on token.
  • Always adds assets at the ideal ratio, according to the price when the transaction is executed. msg.value is treated as a amountETHDesired.
  • Leftover ETH, if any, is returned to msg.sender.
  • If a pool for the passed token and WETH does not exists, one is created automatically, and exactly amountTokenDesired/msg.value tokens are added.
NameTypeNote
tokenaddressA pool token.
amountTokenDesireduintThe amount of token to add as liquidity if the WETH/token price is <= msg.value/amountTokenDesired (token depreciates).
msg.value (amountETHDesired)uintThe amount of ETH to add as liquidity if the token/WETH price is <= amountTokenDesired/msg.value (WETH depreciates).
amountTokenMinuintBounds the extent to which the WETH/token price can go up before the transaction reverts. Must be <= amountTokenDesired.
amountETHMinuintBounds the extent to which the token/WETH price can go up before the transaction reverts. Must be <= msg.value.
toaddressRecipient of the liquidity tokens.
deadlineuintUnix timestamp after which the transaction will revert.
amountTokenuintThe amount of token sent to the pool.
amountETHuintThe amount of ETH converted to WETH and sent to the pool.
liquidityuintThe amount of liquidity tokens minted.

removeLiquidity

function removeLiquidity( address tokenA, address tokenB, uint liquidity, uint amountAMin, uint amountBMin, address to, uint deadline ) external returns (uint amountA, uint amountB);
  • Removes liquidity from an ERC-20⇄ERC-20 pool.

Note: msg.sender should have already given the router an allowance of at least liquidity on the pool.

NameTypeDescription
tokenAaddressA pool token.
tokenBaddressA pool token.
liquidityuintThe amount of liquidity tokens to remove.
amountAMinuintThe minimum amount of tokenA that must be received for the transaction not to revert.
amountBMinuintThe minimum amount of tokenB that must be received for the transaction not to revert.
toaddressRecipient of the underlying assets.
deadlineuintUnix timestamp after which the transaction will revert.
amountAuintThe amount of tokenA received.
amountBuintThe amount of tokenB received.

removeLiquidityETH

function removeLiquidityETH( address token, uint liquidity, uint amountTokenMin, uint amountETHMin, address to, uint deadline ) external returns (uint amountToken, uint amountETH);

Removes liquidity from an ERC-20⇄WETH pool and receive ETH.

NameTypeDescription
tokenaddressA pool token.
liquidityuintThe amount of liquidity tokens to remove.
amountTokenMinuintThe minimum amount of token that must be received for the transaction not to revert.
amountETHMinuintThe minimum amount of ETH that must be received for the transaction not to revert.
toaddressRecipient of the underlying assets.
deadlineuintUnix timestamp after which the transaction will revert.
amountTokenuintThe amount of token received.
amountETHuintThe amount of ETH received.

removeLiquidityWithPermit

function removeLiquidityWithPermit( address tokenA, address tokenB, uint liquidity, uint amountAMin, uint amountBMin, address to, uint deadline, bool approveMax, uint8 v, bytes32 r, bytes32 s ) external returns (uint amountA, uint amountB);

Removes liquidity from an ERC-20⇄ERC-20 pool without pre-approval, thanks to permit.

NameTypeDescription
tokenAaddressA pool token.
tokenBaddressA pool token.
liquidityuintThe amount of liquidity tokens to remove.
amountAMinuintThe minimum amount of tokenA that must be received for the
transaction not to revert.
amountBMinuintThe minimum amount of tokenB that must be received for the
transaction not to revert.
toaddressRecipient of the underlying assets.
deadlineuintUnix timestamp after which the transaction will revert.
approveMaxboolWhether or not the approval amount in the signature is for
liquidity or uint(-1).
vuint8The v component of the permit signature.
rbytes32The r component of the permit signature.
sbytes32The s component of the permit signature.
amountAuintThe amount of tokenA received.
amountBuintThe amount of tokenB received.

removeLiquidityETHWithPermit

function removeLiquidityETHWithPermit( address token, uint liquidity, uint amountTokenMin, uint amountETHMin, address to, uint deadline, bool approveMax, uint8 v, bytes32 r, bytes32 s ) external returns (uint amountToken, uint amountETH);

Removes liquidity from an ERC-20⇄WETTH pool and receive ETH without pre-approval, thanks to permit.

NameTypeDescription
tokenaddressA pool token.
liquidityuintThe amount of liquidity tokens to remove.
amountTokenMinuintThe minimum amount of token that must be received for the
transaction not to revert.
amountETHMinuintThe minimum amount of ETH that must be received for the
transaction not to revert.
toaddressRecipient of the underlying assets.
deadlineuintUnix timestamp after which the transaction will revert.
approveMaxboolWhether or not the approval amount in the signature is for
liquidity or uint(-1).
vuint8The v component of the permit signature.
rbytes32The r component of the permit signature.
sbytes32The s component of the permit signature.
amountTokenuintThe amount of token received.
amountETHuintThe amount of ETH received.

removeLiquidityETHSupportingFeeOnTransferTokens

function removeLiquidityETHSupportingFeeOnTransferTokens( address token, uint liquidity, uint amountTokenMin, uint amountETHMin, address to, uint deadline ) external returns (uint amountETH);

Identical to removeLiquidityETH, but succeeds for tokens that take a fee on transfer.

Note: msg.sender should have already given the router an allowance of at least liquidity on the pool.

NameTypeDescription
tokenaddressA pool token.
liquidityuintThe amount of liquidity tokens to remove.
amountTokenMinuintThe minimum amount of token that must be received for the
transaction not to revert.
amountETHMinuintThe minimum amount of ETH that must be received for the
transaction not to revert.
toaddressRecipient of the underlying assets.
deadlineuintUnix timestamp after which the transaction will revert.
amountETHuintThe amount of ETH received.

removeLiquidityETHWithPermitSupportingFeeOnTransferTokens

function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens( address token, uint liquidity, uint amountTokenMin, uint amountETHMin, address to, uint deadline, bool approveMax, uint8 v, bytes32 r, bytes32 s ) external returns (uint amountETH);

Identical to removeLiquidityETHWithPermit, but succeeds for tokens that take a fee on transfer.

NameTypeDescription
tokenaddressA pool token.
liquidityuintThe amount of liquidity tokens to remove.
amountTokenMinuintThe minimum amount of token that must be received for the
transaction not to revert.
amountETHMinuintThe minimum amount of ETH that must be received for the
transaction not to revert.
toaddressRecipient of the underlying assets.
deadlineuintUnix timestamp after which the transaction will revert.
approveMaxboolWhether or not the approval amount in the signature is for
liquidity or uint(-1).
vuint8The v component of the permit signature.
rbytes32The r component of the permit signature.
sbytes32The s component of the permit signature.
amountETHuintThe amount of ETH received.

swapExactTokensForTokens

function swapExactTokensForTokens( uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline ) external returns (uint[] memory amounts);

Swaps an exact amount of input tokens for as many output tokens as possible, along the route determined by the path. The first element of path is the input token, the last is the output token, and any intermediate elements represent intermediate pairs to trade through (if, for example, a direct pair does not exist).

msg.sender should have already given the router an allowance of at least amountIn on the input token.

NameTypeDescription
amountInuintThe amount of input tokens to send.
amountOutMinuintThe minimum amount of output tokens that must be received for the transaction not to revert.
pathaddress[] calldataAn array of token addresses. path.length must be >= 2. Pools for each consecutive pair of
addresses must exist and have liquidity.
toaddressRecipient of the output tokens.
deadlineuintUnix timestamp after which the transaction will revert.
amountsuint[] memoryThe input token amount and all subsequent output token amounts.

swapTokensForExactTokens

function swapTokensForExactTokens( uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline ) external returns (uint[] memory amounts);

Receive an exact amount of output tokens for as few input tokens as possible, along the route determined by the path. The first element of path is the input token, the last is the output token, and any intermediate elements represent intermediate tokens to trade through (if, for example, a direct pair does not exist).

msg.sender should have already given the router an allowance of at least amountInMax on the input token.

NameTypeDescription
amountOutuintThe amount of output tokens to receive.
amountInMaxuintThe maximum amount of input tokens that can be required before the transaction reverts.
pathaddress[] calldataAn array of token addresses. path.length must be >= 2. Pools for each consecutive pair of
addresses must exist and have liquidity.
toaddressRecipient of the output tokens.
deadlineuintUnix timestamp after which the transaction will revert.
amountsuint[] memoryThe input token amount and all subsequent output token amounts.

swapExactETHForTokens

function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline) external payable returns (uint[] memory amounts);

Swaps an exact amount of ETH for as many output tokens as possible, along the route determined by the path. The first element of path must be WETH, the last is the output token, and any intermediate elements represent intermediate pairs to trade through (if, for example, a direct pair does not exist).

NameTypeDescription
msg.valueuintThe amount of ETH to send.
amountOutMinuintThe minimum amount of output tokens that must be received for the transaction not to revert.
pathaddress[] calldataAn array of token addresses. path.length must be >= 2. Pools for each consecutive pair of
addresses must exist and have liquidity.
toaddressRecipient of the output tokens.
deadlineuintUnix timestamp after which the transaction will revert.
amountsuint[] memoryThe input token amount and all subsequent output token amounts.

swapTokensForExactETH

function swapTokensForExactETH(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts);

Receive an exact amount of ETH for as few input tokens as possible, along the route determined by the path. The first element of path is the input token, the last must be WETH, and any intermediate elements represent intermediate pairs to trade through (if, for example, a direct pair does not exist).

note msg.sender should have already given the router an allowance of at least amountInMax on the input token.

If the to address is a smart contract, it must have the ability to receive ETH.

NameTypeDescription
amountOutuintThe amount of ETH to receive.
amountInMaxuintThe maximum amount of input tokens that can be required before the transaction reverts.
pathaddress[] calldataAn array of token addresses. path.length must be >= 2. Pools for each consecutive pair of
addresses must exist and have liquidity.
toaddressRecipient of ETH.
deadlineuintUnix timestamp after which the transaction will revert.
amountsuint[] memoryThe input token amount and all subsequent output token amounts.

swapExactTokensForETH

function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts);

Swaps an exact amount of tokens for as much ETH as possible, along the route determined by the path. The first element of path is the input token, the last must be WETH, and any intermediate elements represent intermediate pairs to trade through (if, for example, a direct pair does not exist).

If the to address is a smart contract, it must have the ability to receive ETH.

NameTypeDescription
amountInuintThe amount of input tokens to send.
amountOutMinuintThe minimum amount of output tokens that must be received for the transaction not to revert.
pathaddress[] calldataAn array of token addresses. path.length must be >= 2. Pools for each consecutive pair of
addresses must exist and have liquidity.
toaddressRecipient of the ETH.
deadlineuintUnix timestamp after which the transaction will revert.
amountsuint[] memoryThe input token amount and all subsequent output token amounts.

swapETHForExactTokens

function swapETHForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline) external payable returns (uint[] memory amounts);

Receive an exact amount of tokens for as little ETH as possible, along the route determined by the path. The first element of path must be WETH, the last is the output token and any intermediate elements represent intermediate pairs to trade through (if, for example, a direct pair does not exist).

Leftover ETH, if any, is returned to msg.sender.

NameTypeDescription
amountOutuintThe amount of tokens to receive.
msg.valueuintThe maximum amount of ETH that can be required before the transaction reverts.
pathaddress[] calldataAn array of token addresses. path.length must be >= 2. Pools for each consecutive pair of
addresses must exist and have liquidity.
toaddressRecipient of the output tokens.
deadlineuintUnix timestamp after which the transaction will revert.
amountsuint[] memoryThe input token amount and all subsequent output token amounts.

swapExactTokensForTokensSupportingFeeOnTransferTokens

function swapExactTokensForTokensSupportingFeeOnTransferTokens( uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline ) external;

Identical to swapExactTokensForTokens, but succeeds for tokens that take a fee on transfer.

msg.sender should have already given the router an allowance of at least amountIn on the input token.

NameTypeDescription
amountInuintThe amount of input tokens to send.
amountOutMinuintThe minimum amount of output tokens that must be received for the transaction not to revert.
pathaddress[] calldataAn array of token addresses. path.length must be >= 2. Pools for each consecutive pair of
addresses must exist and have liquidity.
toaddressRecipient of the output tokens.
deadlineuintUnix timestamp after which the transaction will revert.

swapExactETHForTokensSupportingFeeOnTransferTokens

function swapExactETHForTokensSupportingFeeOnTransferTokens( uint amountOutMin, address[] calldata path, address to, uint deadline ) external payable;

Identical to swapExactETHForTokens, but succeeds for tokens that take a fee on transfer.

NameTypeDescription
msg.valueuintThe amount of ETH to send.
amountOutMinuintThe minimum amount of output tokens that must be received for the transaction not to revert.
pathaddress[] calldataAn array of token addresses. path.length must be >= 2. Pools for each consecutive pair of
addresses must exist and have liquidity.
toaddressRecipient of the output tokens.
deadlineuintUnix timestamp after which the transaction will revert.

swapExactTokensForETHSupportingFeeOnTransferTokens

function swapExactTokensForETHSupportingFeeOnTransferTokens( uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline ) external;

Identical to swapExactTokensForETH, but succeeds for tokens that take a fee on transfer.

If the to address is a smart contract, it must have the ability to receive ETH.

NameTypeDescription
amountInuintThe amount of input tokens to send.
amountOutMinuintThe minimum amount of output tokens that must be received for the transaction not to revert.
pathaddress[] calldataAn array of token addresses. path.length must be >= 2. Pools for each consecutive pair of
addresses must exist and have liquidity.
toaddressRecipient of the ETH.
deadlineuintUnix timestamp after which the transaction will revert.

Interface

pragma solidity >=0.6.2; interface IXchangeRouterV1 { function factory() external pure returns (address); function WETH() external pure returns (address); function addLiquidity( address tokenA, address tokenB, uint amountADesired, uint amountBDesired, uint amountAMin, uint amountBMin, address to, uint deadline ) external returns (uint amountA, uint amountB, uint liquidity); function addLiquidityETH( address token, uint amountTokenDesired, uint amountTokenMin, uint amountETHMin, address to, uint deadline ) external payable returns (uint amountToken, uint amountETH, uint liquidity); function removeLiquidity( address tokenA, address tokenB, uint liquidity, uint amountAMin, uint amountBMin, address to, uint deadline ) external returns (uint amountA, uint amountB); function removeLiquidityETH( address token, uint liquidity, uint amountTokenMin, uint amountETHMin, address to, uint deadline ) external returns (uint amountToken, uint amountETH); function removeLiquidityWithPermit( address tokenA, address tokenB, uint liquidity, uint amountAMin, uint amountBMin, address to, uint deadline, bool approveMax, uint8 v, bytes32 r, bytes32 s ) external returns (uint amountA, uint amountB); function removeLiquidityETHWithPermit( address token, uint liquidity, uint amountTokenMin, uint amountETHMin, address to, uint deadline, bool approveMax, uint8 v, bytes32 r, bytes32 s ) external returns (uint amountToken, uint amountETH); function swapExactTokensForTokens( uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline ) external returns (uint[] memory amounts); function swapTokensForExactTokens( uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline ) external returns (uint[] memory amounts); function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline) external payable returns (uint[] memory amounts); function swapTokensForExactETH(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts); function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts); function swapETHForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline) external payable returns (uint[] memory amounts); function quote(uint amountA, uint reserveA, uint reserveB) external pure returns (uint amountB); function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) external pure returns (uint amountOut); function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) external pure returns (uint amountIn); function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts); function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts); } interface IXchangeRouterV2 is IXchangeRouterV2 { function removeLiquidityETHSupportingFeeOnTransferTokens( address token, uint liquidity, uint amountTokenMin, uint amountETHMin, address to, uint deadline ) external returns (uint amountETH); function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens( address token, uint liquidity, uint amountTokenMin, uint amountETHMin, address to, uint deadline, bool approveMax, uint8 v, bytes32 r, bytes32 s ) external returns (uint amountETH); function swapExactTokensForTokensSupportingFeeOnTransferTokens( uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline ) external; function swapExactETHForTokensSupportingFeeOnTransferTokens( uint amountOutMin, address[] calldata path, address to, uint deadline ) external payable; function swapExactTokensForETHSupportingFeeOnTransferTokens( uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline ) external; }

ABI SDK

Next
Intro →

-

  • Pioneer 1488 avatar
    Guides
    View
  • Pioneer 1488 avatar
    Integration
    View
  • Pioneer 1488 avatar
    Whitepaper
    View
  • Pioneer 1488 avatar
    Breakdowns
    View
  • Pioneer 1488 avatar
    Onchains
    View
  • Pioneer 1488 avatar
    FAQ
    View

On this page

  1. Code

    1. Address
    2. Read-Only Functions
    3. WETH
  2. State-Changing Functions

    1. addLiquidity
    2. addLiquidityETH
    3. removeLiquidity
    4. removeLiquidityETH
    5. removeLiquidityWithPermit
    6. removeLiquidityETHWithPermit
    7. removeLiquidityETHSupportingFeeOnTransferTokens
    8. removeLiquidityETHWithPermitSupportingFeeOnTransferTokens
    9. swapExactTokensForTokens
    10. swapTokensForExactTokens
    11. swapExactETHForTokens
    12. swapTokensForExactETH
    13. swapExactTokensForETH
    14. swapETHForExactTokens
    15. swapExactTokensForTokensSupportingFeeOnTransferTokens
    16. swapExactETHForTokensSupportingFeeOnTransferTokens
    17. swapExactTokensForETHSupportingFeeOnTransferTokens
  3. Interface