Skip to main content

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.

Code#

PegasysRouter.sol

Address#

0x017dAd2578372CAEE5c6CddfE35eEDB3728544C4

Read-Only Functions#

factory#

function factory() external pure returns (address);

Returns factory address.

WSYS#

function WSYS() external pure returns (address);

Returns the 0xd3e822f3ef011Ca5f17D82C956D952D8d7C3A1BB.

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.
NameType
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.

addLiquiditySYS#

function addLiquiditySYS(
address token,
uint amountTokenDesired,
uint amountTokenMin,
uint amountSYSMin,
address to,
uint deadline
) external payable returns (uint amountToken, uint amountSYS, uint liquidity);

Adds liquidity to an ERC-20⇄WSYS pool with SYS.

  • 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 amountSYSDesired.
  • Leftover SYS, if any, is returned to msg.sender.
  • If a pool for the passed token and WSYS does not exists, one is created automatically, and exactly amountTokenDesired/msg.value tokens are added.
NameType
tokenaddressA pool token.
amountTokenDesireduintThe amount of token to add as liquidity if the WSYS/token price is <= msg.value/amountTokenDesired (token depreciates).
msg.value (amountSYSDesired)uintThe amount of SYS to add as liquidity if the token/WSYS price is <= amountTokenDesired/msg.value (WSYS depreciates).
amountTokenMinuintBounds the extent to which the WSYS/token price can go up before the transaction reverts. Must be <= amountTokenDesired.
amountSYSMinuintBounds the extent to which the token/WSYS 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.
amountSYSuintThe amount of SYS converted to WSYS 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.

  • msg.sender should have already given the router an allowance of at least liquidity on the pool.
NameType
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.

removeLiquiditySYS#

function removeLiquiditySYS(
address token,
uint liquidity,
uint amountTokenMin,
uint amountSYSMin,
address to,
uint deadline
) external returns (uint amountToken, uint amountSYS);

Removes liquidity from an ERC-20⇄WSYS pool and receive SYS.

  • msg.sender should have already given the router an allowance of at least liquidity on the pool.
NameType
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.
amountSYSMinuintThe minimum amount of SYS 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.
amountSYSuintThe amount of SYS 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.

NameType
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.

removeLiquiditySYSWithPermit#

function removeLiquiditySYSWithPermit(
address token,
uint liquidity,
uint amountTokenMin,
uint amountSYSMin,
address to,
uint deadline,
bool approveMax, uint8 v, bytes32 r, bytes32 s
) external returns (uint amountToken, uint amountSYS);

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

NameType
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.
amountSYSMinuintThe minimum amount of SYS 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.
amountSYSuintThe amount of SYS received.

removeLiquiditySYSSupportingFeeOnTransferTokens#

function removeLiquiditySYSSupportingFeeOnTransferTokens(
address token,
uint liquidity,
uint amountTokenMin,
uint amountSYSMin,
address to,
uint deadline
) external returns (uint amountSYS);

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

  • msg.sender should have already given the router an allowance of at least liquidity on the pool.
NameType
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.
amountSYSMinuintThe minimum amount of SYS that must be received for the transaction not to revert.
toaddressRecipient of the underlying assets.
deadlineuintUnix timestamp after which the transaction will revert.
amountSYSuintThe amount of SYS received.

removeLiquiditySYSWithPermitSupportingFeeOnTransferTokens#

function removeLiquiditySYSWithPermitSupportingFeeOnTransferTokens(
address token,
uint liquidity,
uint amountTokenMin,
uint amountSYSMin,
address to,
uint deadline,
bool approveMax, uint8 v, bytes32 r, bytes32 s
) external returns (uint amountSYS);

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

NameType
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.
amountSYSMinuintThe minimum amount of SYS 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.
amountSYSuintThe amount of SYS 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.
NameType
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.
NameType
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.

swapExactSYSForTokens#

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

Swaps an exact amount of SYS for as many output tokens as possible, along the route determined by the path. The first element of path must be WSYS, 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).

NameType
msg.value (amountIn)uintThe amount of SYS 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.

swapTokensForExactSYS#

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

Receive an exact amount of SYS 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 WSYS, 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 amountInMax on the input token.
  • If the to address is a smart contract, it must have the ability to receive SYS.
NameType
amountOutuintThe amount of SYS 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 SYS.
deadlineuintUnix timestamp after which the transaction will revert.
amountsuint[] memoryThe input token amount and all subsequent output token amounts.

swapExactTokensForSYS#

function swapExactTokensForSYS(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 SYS as possible, along the route determined by the path. The first element of path is the input token, the last must be WSYS, 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 SYS.
NameType
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 SYS.
deadlineuintUnix timestamp after which the transaction will revert.
amountsuint[] memoryThe input token amount and all subsequent output token amounts.

swapSYSForExactTokens#

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

Receive an exact amount of tokens for as little SYS as possible, along the route determined by the path. The first element of path must be WSYS, 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 SYS, if any, is returned to msg.sender.
NameType
amountOutuintThe amount of tokens to receive.
msg.value (amountInMax)uintThe maximum amount of SYS 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.
NameType
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.

swapExactSYSForTokensSupportingFeeOnTransferTokens#

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

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

NameType
msg.value (amountIn)uintThe amount of SYS 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.

swapExactTokensForSYSSupportingFeeOnTransferTokens#

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

Identical to swapExactTokensForSYS, 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 SYS.
NameType
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 SYS.
deadlineuintUnix timestamp after which the transaction will revert.

Interface#

import '@pollum-io/pegasys-protocol/contracts/pegasys-periphery/contracts/interfaces/IPegasysRouter.sol';
// SPDX-License-Identifier: GNU
pragma solidity >=0.6.2;
interface IPegasysRouter {
function factory() external pure returns (address);
function WSYS() external pure returns (address);
function addLiquidity(
address tokenA,
address tokenB,
uint256 amountADesired,
uint256 amountBDesired,
uint256 amountAMin,
uint256 amountBMin,
address to,
uint256 deadline
)
external
returns (
uint256 amountA,
uint256 amountB,
uint256 liquidity
);
function addLiquiditySYS(
address token,
uint256 amountTokenDesired,
uint256 amountTokenMin,
uint256 amountSYSMin,
address to,
uint256 deadline
)
external
payable
returns (
uint256 amountToken,
uint256 amountSYS,
uint256 liquidity
);
function removeLiquidity(
address tokenA,
address tokenB,
uint256 liquidity,
uint256 amountAMin,
uint256 amountBMin,
address to,
uint256 deadline
) external returns (uint256 amountA, uint256 amountB);
function removeLiquiditySYS(
address token,
uint256 liquidity,
uint256 amountTokenMin,
uint256 amountSYSMin,
address to,
uint256 deadline
) external returns (uint256 amountToken, uint256 amountSYS);
function removeLiquidityWithPermit(
address tokenA,
address tokenB,
uint256 liquidity,
uint256 amountAMin,
uint256 amountBMin,
address to,
uint256 deadline,
bool approveMax,
uint8 v,
bytes32 r,
bytes32 s
) external returns (uint256 amountA, uint256 amountB);
function removeLiquiditySYSWithPermit(
address token,
uint256 liquidity,
uint256 amountTokenMin,
uint256 amountSYSMin,
address to,
uint256 deadline,
bool approveMax,
uint8 v,
bytes32 r,
bytes32 s
) external returns (uint256 amountToken, uint256 amountSYS);
function swapExactTokensForTokens(
uint256 amountIn,
uint256 amountOutMin,
address[] calldata path,
address to,
uint256 deadline
) external returns (uint256[] memory amounts);
function swapTokensForExactTokens(
uint256 amountOut,
uint256 amountInMax,
address[] calldata path,
address to,
uint256 deadline
) external returns (uint256[] memory amounts);
function swapExactSYSForTokens(
uint256 amountOutMin,
address[] calldata path,
address to,
uint256 deadline
) external payable returns (uint256[] memory amounts);
function swapTokensForExactSYS(
uint256 amountOut,
uint256 amountInMax,
address[] calldata path,
address to,
uint256 deadline
) external returns (uint256[] memory amounts);
function swapExactTokensForSYS(
uint256 amountIn,
uint256 amountOutMin,
address[] calldata path,
address to,
uint256 deadline
) external returns (uint256[] memory amounts);
function swapSYSForExactTokens(
uint256 amountOut,
address[] calldata path,
address to,
uint256 deadline
) external payable returns (uint256[] memory amounts);
function quote(
uint256 amountA,
uint256 reserveA,
uint256 reserveB
) external pure returns (uint256 amountB);
function getAmountOut(
uint256 amountIn,
uint256 reserveIn,
uint256 reserveOut
) external pure returns (uint256 amountOut);
function getAmountIn(
uint256 amountOut,
uint256 reserveIn,
uint256 reserveOut
) external pure returns (uint256 amountIn);
function getAmountsOut(uint256 amountIn, address[] calldata path)
external
view
returns (uint256[] memory amounts);
function getAmountsIn(uint256 amountOut, address[] calldata path)
external
view
returns (uint256[] memory amounts);
function removeLiquiditySYSSupportingFeeOnTransferTokens(
address token,
uint256 liquidity,
uint256 amountTokenMin,
uint256 amountSYSMin,
address to,
uint256 deadline
) external returns (uint256 amountSYS);
function removeLiquiditySYSWithPermitSupportingFeeOnTransferTokens(
address token,
uint256 liquidity,
uint256 amountTokenMin,
uint256 amountSYSMin,
address to,
uint256 deadline,
bool approveMax,
uint8 v,
bytes32 r,
bytes32 s
) external returns (uint256 amountSYS);
function swapExactTokensForTokensSupportingFeeOnTransferTokens(
uint256 amountIn,
uint256 amountOutMin,
address[] calldata path,
address to,
uint256 deadline
) external;
function swapExactSYSForTokensSupportingFeeOnTransferTokens(
uint256 amountOutMin,
address[] calldata path,
address to,
uint256 deadline
) external payable;
function swapExactTokensForSYSSupportingFeeOnTransferTokens(
uint256 amountIn,
uint256 amountOutMin,
address[] calldata path,
address to,
uint256 deadline
) external;
}

ABI#

import IPegasysRouter from "@pollum-io/pegasys-protocol/artifacts/contracts/pegasys-periphery/interfaces/IPegasysRouter.sol/IPegasysRouter.json";

@pollum-io/pegasys-protocol